A Discrete-Event Network Simulator
API
bridge-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Gustavo Carneiro <gjc@inescporto.pt>
17  */
18 #include "bridge-net-device.h"
19 #include "ns3/node.h"
20 #include "ns3/channel.h"
21 #include "ns3/packet.h"
22 #include "ns3/log.h"
23 #include "ns3/boolean.h"
24 #include "ns3/simulator.h"
25 #include "ns3/uinteger.h"
26 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("BridgeNetDevice");
36 
37 NS_OBJECT_ENSURE_REGISTERED (BridgeNetDevice);
38 
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::BridgeNetDevice")
44  .SetParent<NetDevice> ()
45  .SetGroupName("Bridge")
46  .AddConstructor<BridgeNetDevice> ()
47  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
48  UintegerValue (1500),
51  MakeUintegerChecker<uint16_t> ())
52  .AddAttribute ("EnableLearning",
53  "Enable the learning mode of the Learning Bridge",
54  BooleanValue (true),
57  .AddAttribute ("ExpirationTime",
58  "Time it takes for learned MAC state entry to expire.",
59  TimeValue (Seconds (300)),
61  MakeTimeChecker ())
62  ;
63  return tid;
64 }
65 
66 
68  : m_node (0),
69  m_ifIndex (0)
70 {
72  m_channel = CreateObject<BridgeChannel> ();
73 }
74 
76 {
78 }
79 
80 void
82 {
84  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin (); iter != m_ports.end (); iter++)
85  {
86  *iter = 0;
87  }
88  m_ports.clear ();
89  m_channel = 0;
90  m_node = 0;
92 }
93 
94 void
95 BridgeNetDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol,
96  Address const &src, Address const &dst, PacketType packetType)
97 {
99  NS_LOG_DEBUG ("UID is " << packet->GetUid ());
100 
103 
104  if (!m_promiscRxCallback.IsNull ())
105  {
106  m_promiscRxCallback (this, packet, protocol, src, dst, packetType);
107  }
108 
109  switch (packetType)
110  {
111  case PACKET_HOST:
112  if (dst48 == m_address)
113  {
114  Learn (src48, incomingPort);
115  m_rxCallback (this, packet, protocol, src);
116  }
117  break;
118 
119  case PACKET_BROADCAST:
120  case PACKET_MULTICAST:
121  m_rxCallback (this, packet, protocol, src);
122  ForwardBroadcast (incomingPort, packet, protocol, src48, dst48);
123  break;
124 
125  case PACKET_OTHERHOST:
126  if (dst48 == m_address)
127  {
128  Learn (src48, incomingPort);
129  m_rxCallback (this, packet, protocol, src);
130  }
131  else
132  {
133  ForwardUnicast (incomingPort, packet, protocol, src48, dst48);
134  }
135  break;
136  }
137 }
138 
139 void
141  uint16_t protocol, Mac48Address src, Mac48Address dst)
142 {
144  NS_LOG_DEBUG ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
145  << ", packet=" << packet << ", protocol="<<protocol
146  << ", src=" << src << ", dst=" << dst << ")");
147 
148  Learn (src, incomingPort);
149  Ptr<NetDevice> outPort = GetLearnedState (dst);
150  if (outPort != NULL && outPort != incomingPort)
151  {
152  NS_LOG_LOGIC ("Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () << "'");
153  outPort->SendFrom (packet->Copy (), src, dst, protocol);
154  }
155  else
156  {
157  NS_LOG_LOGIC ("No learned state: send through all ports");
158  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
159  iter != m_ports.end (); iter++)
160  {
161  Ptr<NetDevice> port = *iter;
162  if (port != incomingPort)
163  {
164  NS_LOG_LOGIC ("LearningBridgeForward (" << src << " => " << dst << "): "
165  << incomingPort->GetInstanceTypeId ().GetName ()
166  << " --> " << port->GetInstanceTypeId ().GetName ()
167  << " (UID " << packet->GetUid () << ").");
168  port->SendFrom (packet->Copy (), src, dst, protocol);
169  }
170  }
171  }
172 }
173 
174 void
176  uint16_t protocol, Mac48Address src, Mac48Address dst)
177 {
179  NS_LOG_DEBUG ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
180  << ", packet=" << packet << ", protocol="<<protocol
181  << ", src=" << src << ", dst=" << dst << ")");
182  Learn (src, incomingPort);
183 
184  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
185  iter != m_ports.end (); iter++)
186  {
187  Ptr<NetDevice> port = *iter;
188  if (port != incomingPort)
189  {
190  NS_LOG_LOGIC ("LearningBridgeForward (" << src << " => " << dst << "): "
191  << incomingPort->GetInstanceTypeId ().GetName ()
192  << " --> " << port->GetInstanceTypeId ().GetName ()
193  << " (UID " << packet->GetUid () << ").");
194  port->SendFrom (packet->Copy (), src, dst, protocol);
195  }
196  }
197 }
198 
200 {
202  if (m_enableLearning)
203  {
204  LearnedState &state = m_learnState[source];
205  state.associatedPort = port;
207  }
208 }
209 
211 {
213  if (m_enableLearning)
214  {
215  Time now = Simulator::Now ();
216  std::map<Mac48Address, LearnedState>::iterator iter =
217  m_learnState.find (source);
218  if (iter != m_learnState.end ())
219  {
220  LearnedState &state = iter->second;
221  if (state.expirationTime > now)
222  {
223  return state.associatedPort;
224  }
225  else
226  {
227  m_learnState.erase (iter);
228  }
229  }
230  }
231  return NULL;
232 }
233 
234 uint32_t
236 {
238  return m_ports.size ();
239 }
240 
241 
244 {
246  return m_ports[n];
247 }
248 
249 void
251 {
253  NS_ASSERT (bridgePort != this);
254  if (!Mac48Address::IsMatchingType (bridgePort->GetAddress ()))
255  {
256  NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be added to bridge.");
257  }
258  if (!bridgePort->SupportsSendFrom ())
259  {
260  NS_FATAL_ERROR ("Device does not support SendFrom: cannot be added to bridge.");
261  }
262  if (m_address == Mac48Address ())
263  {
264  m_address = Mac48Address::ConvertFrom (bridgePort->GetAddress ());
265  }
266 
267  NS_LOG_DEBUG ("RegisterProtocolHandler for " << bridgePort->GetInstanceTypeId ().GetName ());
269  0, bridgePort, true);
270  m_ports.push_back (bridgePort);
271  m_channel->AddChannel (bridgePort->GetChannel ());
272 }
273 
274 void
275 BridgeNetDevice::SetIfIndex (const uint32_t index)
276 {
278  m_ifIndex = index;
279 }
280 
281 uint32_t
283 {
285  return m_ifIndex;
286 }
287 
290 {
292  return m_channel;
293 }
294 
295 void
297 {
300 }
301 
302 Address
304 {
306  return m_address;
307 }
308 
309 bool
310 BridgeNetDevice::SetMtu (const uint16_t mtu)
311 {
313  m_mtu = mtu;
314  return true;
315 }
316 
317 uint16_t
319 {
321  return m_mtu;
322 }
323 
324 
325 bool
327 {
329  return true;
330 }
331 
332 
333 void
335 {}
336 
337 
338 bool
340 {
342  return true;
343 }
344 
345 
346 Address
348 {
350  return Mac48Address ("ff:ff:ff:ff:ff:ff");
351 }
352 
353 bool
355 {
357  return true;
358 }
359 
360 Address
362 {
363  NS_LOG_FUNCTION (this << multicastGroup);
364  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
365  return multicast;
366 }
367 
368 
369 bool
371 {
373  return false;
374 }
375 
376 bool
378 {
380  return true;
381 }
382 
383 
384 bool
385 BridgeNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
386 {
388  return SendFrom (packet, m_address, dest, protocolNumber);
389 }
390 
391 bool
392 BridgeNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
393 {
396 
397  // try to use the learned state if data is unicast
398  if (!dst.IsGroup ())
399  {
400  Ptr<NetDevice> outPort = GetLearnedState (dst);
401  if (outPort != NULL)
402  {
403  outPort->SendFrom (packet, src, dest, protocolNumber);
404  return true;
405  }
406  }
407 
408  // data was not unicast or no state has been learned for that mac
409  // address => flood through all ports.
410  Ptr<Packet> pktCopy;
411  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
412  iter != m_ports.end (); iter++)
413  {
414  pktCopy = packet->Copy ();
415  Ptr<NetDevice> port = *iter;
416  port->SendFrom (pktCopy, src, dest, protocolNumber);
417  }
418 
419  return true;
420 }
421 
422 
423 Ptr<Node>
425 {
427  return m_node;
428 }
429 
430 
431 void
433 {
435  m_node = node;
436 }
437 
438 
439 bool
441 {
443  return true;
444 }
445 
446 
447 void
449 {
451  m_rxCallback = cb;
452 }
453 
454 void
456 {
458  m_promiscRxCallback = cb;
459 }
460 
461 bool
463 {
465  return true;
466 }
467 
469 {
470  NS_LOG_FUNCTION (this << addr);
471  return Mac48Address::GetMulticast (addr);
472 }
473 
474 } // namespace ns3
void AddBridgePort(Ptr< NetDevice > bridgePort)
Add a &#39;port&#39; to a bridge device.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
static bool IsMatchingType(const Address &address)
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void Learn(Mac48Address source, Ptr< NetDevice > port)
Learns the port a MAC address is sending from.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Packet addressed to someone else.
Definition: net-device.h:304
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Ptr< NetDevice > GetLearnedState(Mac48Address source)
Gets the port associated to a source address.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void ForwardBroadcast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
Forwards a broadcast or a multicast packet.
virtual bool IsMulticast(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual void SetIfIndex(const uint32_t index)
Time expirationTime
time it takes for learned MAC state to expire
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
virtual bool IsLinkUp(void) const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
virtual Ptr< Node > GetNode(void) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
static TypeId GetTypeId(void)
Get the type ID.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
virtual bool NeedsArp(void) const
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void SetAddress(Address address)
Set the address of this interface.
a virtual net device that bridges multiple LAN segments
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetAddress(void) const
uint32_t GetNBridgePorts(void) const
Gets the number of bridged &#39;ports&#39;, i.e., the NetDevices currently bridged.
NetDevice::PromiscReceiveCallback m_promiscRxCallback
promiscuous receive callback
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual void AddLinkChangeCallback(Callback< void > callback)
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Ptr< NetDevice > associatedPort
port associated with the address
static Mac48Address GetMulticast(Ipv4Address address)
AttributeValue implementation for Time.
Definition: nstime.h:1353
std::vector< Ptr< NetDevice > > m_ports
bridged ports
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual bool SetMtu(const uint16_t mtu)
uint32_t m_ifIndex
Interface index.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
static Mac48Address ConvertFrom(const Address &address)
Ptr< NetDevice > GetBridgePort(uint32_t n) const
Gets the n-th bridged port.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
address
Definition: first.py:44
NetDevice::ReceiveCallback m_rxCallback
receive callback
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Mac48Address m_address
MAC address of the NetDevice.
virtual bool IsBroadcast(void) const
an EUI-48 address
Definition: mac48-address.h:43
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:1354
bool IsGroup(void) const
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
virtual uint32_t GetIfIndex(void) const
virtual void DoDispose(void)
Destructor implementation.
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:229
Ptr< Node > m_node
node owning this NetDevice
virtual Address GetBroadcast(void) const
std::map< Mac48Address, LearnedState > m_learnState
Container for known address statuses.
Structure holding the status of an address.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
uint16_t m_mtu
MTU of the bridged NetDevice.
void ForwardUnicast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
Forwards a unicast packet.
Packet addressed oo us.
Definition: net-device.h:298
Network layer to device interface.
Definition: net-device.h:95
bool m_enableLearning
true if the bridge will learn the node status
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::BridgeNetDevice declaration.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< BridgeChannel > m_channel
virtual bridged channel
Time m_expirationTime
time it takes for learned MAC state to expire
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
virtual uint16_t GetMtu(void) const
virtual bool SupportsSendFrom() const
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Packet addressed to multicast group.
Definition: net-device.h:302
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
virtual void SetNode(Ptr< Node > node)
a unique identifier for an interface.
Definition: type-id.h:58
virtual Ptr< Channel > GetChannel(void) const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Packet addressed to all.
Definition: net-device.h:300
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
void ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &source, Address const &destination, PacketType packetType)
Receives a packet from one bridged port.