18 #include "bridge-net-device.h"
20 #include "ns3/channel.h"
21 #include "ns3/packet.h"
23 #include "ns3/boolean.h"
24 #include "ns3/simulator.h"
25 #include "ns3/uinteger.h"
31 NS_OBJECT_ENSURE_REGISTERED (BridgeNetDevice);
35 BridgeNetDevice::GetTypeId (
void)
37 static TypeId tid = TypeId (
"ns3::BridgeNetDevice")
38 .SetParent<NetDevice> ()
39 .AddConstructor<BridgeNetDevice> ()
40 .AddAttribute (
"Mtu",
"The MAC-level Maximum Transmission Unit",
44 MakeUintegerChecker<uint16_t> ())
45 .AddAttribute (
"EnableLearning",
46 "Enable the learning mode of the Learning Bridge",
48 MakeBooleanAccessor (&BridgeNetDevice::m_enableLearning),
49 MakeBooleanChecker ())
50 .AddAttribute (
"ExpirationTime",
51 "Time it takes for learned MAC state entry to expire.",
53 MakeTimeAccessor (&BridgeNetDevice::m_expirationTime),
60 BridgeNetDevice::BridgeNetDevice ()
65 m_channel = CreateObject<BridgeChannel> ();
68 BridgeNetDevice::~BridgeNetDevice()
77 for (std::vector<
Ptr<NetDevice> >::iterator iter = m_ports.begin (); iter != m_ports.end (); iter++)
97 if (!m_promiscRxCallback.IsNull ())
99 m_promiscRxCallback (
this, packet, protocol, src, dst, packetType);
105 if (dst48 == m_address)
107 m_rxCallback (
this, packet, protocol, src);
113 m_rxCallback (
this, packet, protocol, src);
114 ForwardBroadcast (incomingPort, packet, protocol, src48, dst48);
118 if (dst48 == m_address)
120 m_rxCallback (
this, packet, protocol, src);
124 ForwardUnicast (incomingPort, packet, protocol, src48, dst48);
131 BridgeNetDevice::ForwardUnicast (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet,
132 uint16_t protocol, Mac48Address src, Mac48Address dst)
135 NS_LOG_DEBUG (
"LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
136 <<
", packet=" << packet <<
", protocol="<<protocol
137 <<
", src=" << src <<
", dst=" << dst <<
")");
139 Learn (src, incomingPort);
140 Ptr<NetDevice> outPort = GetLearnedState (dst);
141 if (outPort != NULL && outPort != incomingPort)
143 NS_LOG_LOGIC (
"Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () <<
"'");
144 outPort->SendFrom (packet->Copy (), src, dst, protocol);
148 NS_LOG_LOGIC (
"No learned state: send through all ports");
149 for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
150 iter != m_ports.end (); iter++)
152 Ptr<NetDevice> port = *iter;
153 if (port != incomingPort)
155 NS_LOG_LOGIC (
"LearningBridgeForward (" << src <<
" => " << dst <<
"): "
156 << incomingPort->GetInstanceTypeId ().GetName ()
157 <<
" --> " << port->GetInstanceTypeId ().GetName ()
158 <<
" (UID " << packet->GetUid () <<
").");
159 port->SendFrom (packet->Copy (), src, dst, protocol);
166 BridgeNetDevice::ForwardBroadcast (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet,
167 uint16_t protocol, Mac48Address src, Mac48Address dst)
170 NS_LOG_DEBUG (
"LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
171 <<
", packet=" << packet <<
", protocol="<<protocol
172 <<
", src=" << src <<
", dst=" << dst <<
")");
173 Learn (src, incomingPort);
175 for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
176 iter != m_ports.end (); iter++)
178 Ptr<NetDevice> port = *iter;
179 if (port != incomingPort)
181 NS_LOG_LOGIC (
"LearningBridgeForward (" << src <<
" => " << dst <<
"): "
182 << incomingPort->GetInstanceTypeId ().GetName ()
183 <<
" --> " << port->GetInstanceTypeId ().GetName ()
184 <<
" (UID " << packet->GetUid () <<
").");
185 port->SendFrom (packet->Copy (), src, dst, protocol);
190 void BridgeNetDevice::Learn (Mac48Address source, Ptr<NetDevice> port)
193 if (m_enableLearning)
195 LearnedState &state = m_learnState[source];
196 state.associatedPort = port;
201 Ptr<NetDevice> BridgeNetDevice::GetLearnedState (Mac48Address source)
204 if (m_enableLearning)
207 std::map<Mac48Address, LearnedState>::iterator iter =
208 m_learnState.find (source);
209 if (iter != m_learnState.end ())
211 LearnedState &state = iter->second;
212 if (state.expirationTime > now)
214 return state.associatedPort;
218 m_learnState.erase (iter);
226 BridgeNetDevice::GetNBridgePorts (
void)
const
229 return m_ports.size ();
234 BridgeNetDevice::GetBridgePort (uint32_t n)
const
247 NS_FATAL_ERROR (
"Device does not support eui 48 addresses: cannot be added to bridge.");
249 if (!bridgePort->SupportsSendFrom ())
251 NS_FATAL_ERROR (
"Device does not support SendFrom: cannot be added to bridge.");
258 NS_LOG_DEBUG (
"RegisterProtocolHandler for " << bridgePort->GetInstanceTypeId ().GetName ());
260 0, bridgePort,
true);
261 m_ports.push_back (bridgePort);
262 m_channel->AddChannel (bridgePort->GetChannel ());
379 return SendFrom (packet, m_address, dest, protocolNumber);
394 outPort->SendFrom (packet, src, dest, protocolNumber);
401 for (std::vector<
Ptr<NetDevice> >::iterator iter = m_ports.begin ();
402 iter != m_ports.end (); iter++)
405 port->SendFrom (packet, src, dest, protocolNumber);
447 m_promiscRxCallback = cb;
void AddBridgePort(Ptr< NetDevice > bridgePort)
Add a 'port' to a bridge device.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
static bool IsMatchingType(const Address &address)
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual void SetIfIndex(const uint32_t index)
virtual uint32_t GetIfIndex(void) const
uint64_t GetUid(void) const
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual bool IsMulticast(void) const
virtual Ptr< Channel > GetChannel(void) const
virtual void DoDispose(void)
#define NS_LOG_FUNCTION_NOARGS()
virtual void SetAddress(Address address)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual void AddLinkChangeCallback(Callback< void > callback)
#define NS_FATAL_ERROR(msg)
fatal error handling
a polymophic address class
static Mac48Address GetMulticast(Ipv4Address address)
virtual uint16_t GetMtu(void) const
virtual bool SetMtu(const uint16_t mtu)
virtual bool SupportsSendFrom() const
virtual bool IsBroadcast(void) const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual Address GetBroadcast(void) const
#define NS_LOG_LOGIC(msg)
static Mac48Address ConvertFrom(const Address &address)
virtual bool NeedsArp(void) const
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
virtual Address GetAddress(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual Ptr< Node > GetNode(void) const
virtual void DoDispose(void)
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Describes an IPv6 address.
Ipv4 addresses are stored in host order in this class.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
#define NS_LOG_DEBUG(msg)
virtual void SetNode(Ptr< Node > node)
virtual bool IsLinkUp(void) const