A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tap-bridge.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #ifndef TAP_BRIDGE_H
20 #define TAP_BRIDGE_H
21 
22 #include <cstring>
23 #include "ns3/address.h"
24 #include "ns3/net-device.h"
25 #include "ns3/node.h"
26 #include "ns3/callback.h"
27 #include "ns3/packet.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/event-id.h"
30 #include "ns3/nstime.h"
31 #include "ns3/data-rate.h"
32 #include "ns3/ptr.h"
33 #include "ns3/mac48-address.h"
34 #include "ns3/unix-fd-reader.h"
35 
36 namespace ns3 {
37 
39 {
40 private:
41  FdReader::Data DoRead (void);
42 };
43 
44 class Node;
45 
103 class TapBridge : public NetDevice
104 {
105 public:
106  static TypeId GetTypeId (void);
107 
112  enum Mode {
117  };
118 
119  TapBridge ();
120  virtual ~TapBridge ();
121 
130 
143  void SetBridgedNetDevice (Ptr<NetDevice> bridgedDevice);
144 
157  void Start (Time tStart);
158 
166  void Stop (Time tStop);
167 
173  void SetMode (TapBridge::Mode mode);
174 
180  TapBridge::Mode GetMode (void);
181 
182  //
183  // The following methods are inherited from NetDevice base class and are
184  // documented there.
185  //
186  virtual void SetIfIndex (const uint32_t index);
187  virtual uint32_t GetIfIndex (void) const;
188  virtual Ptr<Channel> GetChannel (void) const;
189  virtual void SetAddress (Address address);
190  virtual Address GetAddress (void) const;
191  virtual bool SetMtu (const uint16_t mtu);
192  virtual uint16_t GetMtu (void) const;
193  virtual bool IsLinkUp (void) const;
194  virtual void AddLinkChangeCallback (Callback<void> callback);
195  virtual bool IsBroadcast (void) const;
196  virtual Address GetBroadcast (void) const;
197  virtual bool IsMulticast (void) const;
198  virtual Address GetMulticast (Ipv4Address multicastGroup) const;
199  virtual bool IsPointToPoint (void) const;
200  virtual bool IsBridge (void) const;
201  virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
202  virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
203  virtual Ptr<Node> GetNode (void) const;
204  virtual void SetNode (Ptr<Node> node);
205  virtual bool NeedsArp (void) const;
208  virtual bool SupportsSendFrom () const;
209  virtual Address GetMulticast (Ipv6Address addr) const;
210 
211 protected:
220  virtual void DoDispose (void);
221 
222  bool ReceiveFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
223  Address const &src, Address const &dst, PacketType packetType);
224 
225  bool DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &src);
226 
227 private:
228 
237  void CreateTap (void);
238 
244  void StartTapDevice (void);
245 
251  void StopTapDevice (void);
252 
258  void ReadCallback (uint8_t *buf, ssize_t len);
259 
260  /*
261  * \internal
262  *
263  * Forward a packet received from the tap device to the bridged ns-3
264  * device
265  *
266  * \param buf A character buffer containing the actual packet bits that were
267  * received from the host.
268  * \param buf The length of the buffer.
269  */
270  void ForwardToBridgedDevice (uint8_t *buf, ssize_t len);
271 
292  Ptr<Packet> Filter (Ptr<Packet> packet, Address *src, Address *dst, uint16_t *type);
293 
294  void NotifyLinkUp (void);
295 
304 
317 
324 
325 
331  uint32_t m_ifIndex;
332 
338  uint16_t m_mtu;
339 
346  int m_sock;
347 
355 
363 
371 
379 
389 
396 
403 
410  std::string m_tapDeviceName;
411 
418 
435 
442 
456 
460  uint8_t *m_packetBuffer;
461 
462  /*
463  * a copy of the node id so the read thread doesn't have to GetNode() in
464  * in order to find the node ID. Thread unsafe reference counting in
465  * multithreaded apps is not a good thing.
466  */
467  uint32_t m_nodeId;
468 
476  bool m_linkUp;
477 
484 };
485 
486 } // namespace ns3
487 
488 #endif /* TAP_BRIDGE_H */
EventId m_startEvent
Definition: tap-bridge.h:354
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:1013
Ipv4Address m_tapIp
Definition: tap-bridge.h:424
virtual void DoDispose(void)
Definition: tap-bridge.cc:165
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1160
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1153
A structure representing data read.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1109
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:886
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1034
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1048
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:115
forward calls to a chain of CallbackAn ns3::TracedCallback has almost exactly the same API as a norma...
uint32_t m_nodeId
Definition: tap-bridge.h:467
void StartTapDevice(void)
Definition: tap-bridge.cc:195
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:172
EventId m_stopEvent
Definition: tap-bridge.h:362
Mac48Address m_tapMac
Definition: tap-bridge.h:434
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src)
Definition: tap-bridge.cc:919
virtual ~TapBridge()
Definition: tap-bridge.cc:152
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:184
static TypeId GetTypeId(void)
Definition: tap-bridge.cc:79
a polymophic address class
Definition: address.h:86
A class that asynchronously reads from a file descriptor.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: tap-bridge.cc:1117
void CreateTap(void)
Definition: tap-bridge.cc:255
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:460
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1081
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:999
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1124
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1137
A bridge to make it appear that a real host process is connected to an ns-3 net device.
Definition: tap-bridge.h:103
Ptr< NetDevice > GetBridgedNetDevice(void)
Get the bridged net device.
Definition: tap-bridge.cc:879
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:112
FdReader::Data DoRead(void)
The read implementation.
Definition: tap-bridge.cc:53
bool ReceiveFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src, Address const &dst, PacketType packetType)
Definition: tap-bridge.cc:927
uint32_t m_ifIndex
Definition: tap-bridge.h:331
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1145
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1056
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1167
void StopTapDevice(void)
Definition: tap-bridge.cc:237
TracedCallback m_linkChangeCallbacks
Definition: tap-bridge.h:483
Ptr< NetDevice > m_bridgedDevice
Definition: tap-bridge.h:448
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: tap-bridge.cc:1020
void ReadCallback(uint8_t *buf, ssize_t len)
Definition: tap-bridge.cc:665
ns-3 creates and configures tap device
Definition: tap-bridge.h:114
std::string m_tapDeviceName
Definition: tap-bridge.h:410
Ptr< Node > m_node
Definition: tap-bridge.h:323
Ptr< TapBridgeFdReader > m_fdReader
Definition: tap-bridge.h:370
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:116
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Definition: tap-bridge.h:316
Ipv4Address m_tapGateway
Definition: tap-bridge.h:417
an EUI-48 address
Definition: mac48-address.h:41
bool m_ns3AddressRewritten
Definition: tap-bridge.h:455
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1174
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1181
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1027
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1095
Mac48Address m_address
Definition: tap-bridge.h:388
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ptr< Packet > Filter(Ptr< Packet > packet, Address *src, Address *dst, uint16_t *type)
Definition: tap-bridge.cc:817
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
TapBridge::Mode GetMode(void)
Get the operating mode of this device.
Definition: tap-bridge.cc:1041
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1102
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1074
an identifier for simulation events.
Definition: event-id.h:46
Network layer to device interface.
Definition: net-device.h:75
A network Node.
Definition: node.h:55
Ipv4Mask m_tapNetmask
Definition: tap-bridge.h:441
void NotifyLinkUp(void)
Definition: tap-bridge.cc:1063
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:1006
tuple address
Definition: first.py:37
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:274
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1088
uint16_t m_mtu
Definition: tap-bridge.h:338
void ForwardToBridgedDevice(uint8_t *buf, ssize_t len)
Definition: tap-bridge.cc:690
a unique identifier for an interface.
Definition: type-id.h:49
NetDevice::ReceiveCallback m_rxCallback
Definition: tap-bridge.h:303
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1188