A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
27 NS_LOG_COMPONENT_DEFINE ("BridgeNetDevice");
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (BridgeNetDevice)
32  ;
33 
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::BridgeNetDevice")
39  .SetParent<NetDevice> ()
40  .AddConstructor<BridgeNetDevice> ()
41  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
42  UintegerValue (1500),
43  MakeUintegerAccessor (&BridgeNetDevice::SetMtu,
45  MakeUintegerChecker<uint16_t> ())
46  .AddAttribute ("EnableLearning",
47  "Enable the learning mode of the Learning Bridge",
48  BooleanValue (true),
49  MakeBooleanAccessor (&BridgeNetDevice::m_enableLearning),
50  MakeBooleanChecker ())
51  .AddAttribute ("ExpirationTime",
52  "Time it takes for learned MAC state entry to expire.",
53  TimeValue (Seconds (300)),
54  MakeTimeAccessor (&BridgeNetDevice::m_expirationTime),
55  MakeTimeChecker ())
56  ;
57  return tid;
58 }
59 
60 
62  : m_node (0),
63  m_ifIndex (0)
64 {
66  m_channel = CreateObject<BridgeChannel> ();
67 }
68 
70 {
72 }
73 
74 void
76 {
78  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin (); iter != m_ports.end (); iter++)
79  {
80  *iter = 0;
81  }
82  m_ports.clear ();
83  m_channel = 0;
84  m_node = 0;
86 }
87 
88 void
89 BridgeNetDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol,
90  Address const &src, Address const &dst, PacketType packetType)
91 {
93  NS_LOG_DEBUG ("UID is " << packet->GetUid ());
94 
97 
99  {
100  m_promiscRxCallback (this, packet, protocol, src, dst, packetType);
101  }
102 
103  switch (packetType)
104  {
105  case PACKET_HOST:
106  if (dst48 == m_address)
107  {
108  m_rxCallback (this, packet, protocol, src);
109  }
110  break;
111 
112  case PACKET_BROADCAST:
113  case PACKET_MULTICAST:
114  m_rxCallback (this, packet, protocol, src);
115  ForwardBroadcast (incomingPort, packet, protocol, src48, dst48);
116  break;
117 
118  case PACKET_OTHERHOST:
119  if (dst48 == m_address)
120  {
121  m_rxCallback (this, packet, protocol, src);
122  }
123  else
124  {
125  ForwardUnicast (incomingPort, packet, protocol, src48, dst48);
126  }
127  break;
128  }
129 }
130 
131 void
133  uint16_t protocol, Mac48Address src, Mac48Address dst)
134 {
136  NS_LOG_DEBUG ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
137  << ", packet=" << packet << ", protocol="<<protocol
138  << ", src=" << src << ", dst=" << dst << ")");
139 
140  Learn (src, incomingPort);
141  Ptr<NetDevice> outPort = GetLearnedState (dst);
142  if (outPort != NULL && outPort != incomingPort)
143  {
144  NS_LOG_LOGIC ("Learning bridge state says to use port `" << outPort->GetInstanceTypeId ().GetName () << "'");
145  outPort->SendFrom (packet->Copy (), src, dst, protocol);
146  }
147  else
148  {
149  NS_LOG_LOGIC ("No learned state: send through all ports");
150  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
151  iter != m_ports.end (); iter++)
152  {
153  Ptr<NetDevice> port = *iter;
154  if (port != incomingPort)
155  {
156  NS_LOG_LOGIC ("LearningBridgeForward (" << src << " => " << dst << "): "
157  << incomingPort->GetInstanceTypeId ().GetName ()
158  << " --> " << port->GetInstanceTypeId ().GetName ()
159  << " (UID " << packet->GetUid () << ").");
160  port->SendFrom (packet->Copy (), src, dst, protocol);
161  }
162  }
163  }
164 }
165 
166 void
168  uint16_t protocol, Mac48Address src, Mac48Address dst)
169 {
171  NS_LOG_DEBUG ("LearningBridgeForward (incomingPort=" << incomingPort->GetInstanceTypeId ().GetName ()
172  << ", packet=" << packet << ", protocol="<<protocol
173  << ", src=" << src << ", dst=" << dst << ")");
174  Learn (src, incomingPort);
175 
176  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
177  iter != m_ports.end (); iter++)
178  {
179  Ptr<NetDevice> port = *iter;
180  if (port != incomingPort)
181  {
182  NS_LOG_LOGIC ("LearningBridgeForward (" << src << " => " << dst << "): "
183  << incomingPort->GetInstanceTypeId ().GetName ()
184  << " --> " << port->GetInstanceTypeId ().GetName ()
185  << " (UID " << packet->GetUid () << ").");
186  port->SendFrom (packet->Copy (), src, dst, protocol);
187  }
188  }
189 }
190 
192 {
194  if (m_enableLearning)
195  {
196  LearnedState &state = m_learnState[source];
197  state.associatedPort = port;
199  }
200 }
201 
203 {
205  if (m_enableLearning)
206  {
207  Time now = Simulator::Now ();
208  std::map<Mac48Address, LearnedState>::iterator iter =
209  m_learnState.find (source);
210  if (iter != m_learnState.end ())
211  {
212  LearnedState &state = iter->second;
213  if (state.expirationTime > now)
214  {
215  return state.associatedPort;
216  }
217  else
218  {
219  m_learnState.erase (iter);
220  }
221  }
222  }
223  return NULL;
224 }
225 
226 uint32_t
228 {
230  return m_ports.size ();
231 }
232 
233 
236 {
238  return m_ports[n];
239 }
240 
241 void
243 {
245  NS_ASSERT (bridgePort != this);
246  if (!Mac48Address::IsMatchingType (bridgePort->GetAddress ()))
247  {
248  NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be added to bridge.");
249  }
250  if (!bridgePort->SupportsSendFrom ())
251  {
252  NS_FATAL_ERROR ("Device does not support SendFrom: cannot be added to bridge.");
253  }
254  if (m_address == Mac48Address ())
255  {
256  m_address = Mac48Address::ConvertFrom (bridgePort->GetAddress ());
257  }
258 
259  NS_LOG_DEBUG ("RegisterProtocolHandler for " << bridgePort->GetInstanceTypeId ().GetName ());
261  0, bridgePort, true);
262  m_ports.push_back (bridgePort);
263  m_channel->AddChannel (bridgePort->GetChannel ());
264 }
265 
266 void
267 BridgeNetDevice::SetIfIndex (const uint32_t index)
268 {
270  m_ifIndex = index;
271 }
272 
273 uint32_t
275 {
277  return m_ifIndex;
278 }
279 
282 {
284  return m_channel;
285 }
286 
287 void
289 {
292 }
293 
294 Address
296 {
298  return m_address;
299 }
300 
301 bool
302 BridgeNetDevice::SetMtu (const uint16_t mtu)
303 {
305  m_mtu = mtu;
306  return true;
307 }
308 
309 uint16_t
311 {
313  return m_mtu;
314 }
315 
316 
317 bool
319 {
321  return true;
322 }
323 
324 
325 void
327 {}
328 
329 
330 bool
332 {
334  return true;
335 }
336 
337 
338 Address
340 {
342  return Mac48Address ("ff:ff:ff:ff:ff:ff");
343 }
344 
345 bool
347 {
349  return true;
350 }
351 
352 Address
354 {
355  NS_LOG_FUNCTION (this << multicastGroup);
356  Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
357  return multicast;
358 }
359 
360 
361 bool
363 {
365  return false;
366 }
367 
368 bool
370 {
372  return true;
373 }
374 
375 
376 bool
377 BridgeNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
378 {
380  return SendFrom (packet, m_address, dest, protocolNumber);
381 }
382 
383 bool
384 BridgeNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
385 {
388 
389  // try to use the learned state if data is unicast
390  if (!dst.IsGroup ())
391  {
392  Ptr<NetDevice> outPort = GetLearnedState (dst);
393  if (outPort != NULL)
394  {
395  outPort->SendFrom (packet, src, dest, protocolNumber);
396  return true;
397  }
398  }
399 
400  // data was not unicast or no state has been learned for that mac
401  // address => flood through all ports.
402  for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin ();
403  iter != m_ports.end (); iter++)
404  {
405  Ptr<NetDevice> port = *iter;
406  port->SendFrom (packet, src, dest, protocolNumber);
407  }
408 
409  return true;
410 }
411 
412 
413 Ptr<Node>
415 {
417  return m_node;
418 }
419 
420 
421 void
423 {
425  m_node = node;
426 }
427 
428 
429 bool
431 {
433  return true;
434 }
435 
436 
437 void
439 {
441  m_rxCallback = cb;
442 }
443 
444 void
446 {
448  m_promiscRxCallback = cb;
449 }
450 
451 bool
453 {
455  return true;
456 }
457 
459 {
460  NS_LOG_FUNCTION (this << addr);
461  return Mac48Address::GetMulticast (addr);
462 }
463 
464 } // namespace ns3
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)
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void Learn(Mac48Address source, Ptr< NetDevice > port)
Hold a bool native type.
Definition: boolean.h:38
Packet addressed to someone else.
Definition: net-device.h:278
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Ptr< NetDevice > GetLearnedState(Mac48Address source)
void ForwardBroadcast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
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
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Definition: packet.cc:393
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual bool IsMulticast(void) const
virtual Ptr< Channel > GetChannel(void) const
static TypeId GetTypeId(void)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
virtual void SetAddress(Address address)
Set the address of this interface.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
NetDevice::PromiscReceiveCallback m_promiscRxCallback
virtual void AddLinkChangeCallback(Callback< void > callback)
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
static Mac48Address GetMulticast(Ipv4Address address)
Ptr< NetDevice > GetBridgePort(uint32_t n) const
hold objects of type ns3::Time
Definition: nstime.h:961
std::vector< Ptr< NetDevice > > m_ports
virtual uint16_t GetMtu(void) const
Hold an unsigned integer type.
Definition: uinteger.h:46
NS_LOG_COMPONENT_DEFINE("BridgeNetDevice")
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)
Definition: callback.h:1238
virtual Address GetBroadcast(void) const
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
static Mac48Address ConvertFrom(const Address &address)
Ptr< Packet > Copy(void) const
Definition: packet.cc:122
virtual bool NeedsArp(void) const
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
NetDevice::ReceiveCallback m_rxCallback
virtual Address GetAddress(void) const
bool IsGroup(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
an EUI-48 address
Definition: mac48-address.h:41
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
virtual Ptr< Node > GetNode(void) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:217
std::map< Mac48Address, LearnedState > m_learnState
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ForwardUnicast(Ptr< NetDevice > incomingPort, Ptr< const Packet > packet, uint16_t protocol, Mac48Address src, Mac48Address dst)
Packet addressed oo us.
Definition: net-device.h:272
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
Ptr< BridgeChannel > m_channel
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
uint32_t GetNBridgePorts(void) const
tuple address
Definition: first.py:37
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
Packet addressed to multicast group.
Definition: net-device.h:276
virtual void SetNode(Ptr< Node > node)
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual bool IsLinkUp(void) const
Packet addressed to all.
Definition: net-device.h:274
void ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &source, Address const &destination, PacketType packetType)