A Discrete-Event Network Simulator
API
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:
218  virtual void DoDispose (void);
219 
220  bool ReceiveFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
221  Address const &src, Address const &dst, PacketType packetType);
222 
223  bool DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &src);
224 
225 private:
226 
233  void CreateTap (void);
234 
238  void StartTapDevice (void);
239 
243  void StopTapDevice (void);
244 
248  void ReadCallback (uint8_t *buf, ssize_t len);
249 
250  /*
251  * Forward a packet received from the tap device to the bridged ns-3
252  * device
253  *
254  * \param buf A character buffer containing the actual packet bits that were
255  * received from the host.
256  * \param buf The length of the buffer.
257  */
258  void ForwardToBridgedDevice (uint8_t *buf, ssize_t len);
259 
278  Ptr<Packet> Filter (Ptr<Packet> packet, Address *src, Address *dst, uint16_t *type);
279 
280  void NotifyLinkUp (void);
281 
288 
299 
304 
305 
309  uint32_t m_ifIndex;
310 
314  uint16_t m_mtu;
315 
320  int m_sock;
321 
327 
333 
339 
345 
353 
358 
363 
368  std::string m_tapDeviceName;
369 
374 
387 
392 
402 
406  uint8_t *m_packetBuffer;
407 
408  /*
409  * a copy of the node id so the read thread doesn't have to GetNode() in
410  * in order to find the node ID. Thread unsafe reference counting in
411  * multithreaded apps is not a good thing.
412  */
413  uint32_t m_nodeId;
414 
420  bool m_linkUp;
421 
426 };
427 
428 } // namespace ns3
429 
430 #endif /* TAP_BRIDGE_H */
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:362
EventId m_startEvent
The ID of the ns-3 event used to schedule the start up of the underlying host Tap device and ns-3 rea...
Definition: tap-bridge.h:326
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:1014
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Definition: tap-bridge.h:378
virtual void DoDispose(void)
Call out to a separate process running as suid root in order to get our tap device created...
Definition: tap-bridge.cc:166
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1161
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1154
A structure representing data read.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1110
Mode m_mode
The operating mode of the bridge.
Definition: tap-bridge.h:344
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:887
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1035
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1049
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:226
Forward calls to a chain of Callback.
ns-3 creates and configures tap device
Definition: tap-bridge.h:114
uint32_t m_nodeId
Definition: tap-bridge.h:413
void StartTapDevice(void)
Spin up the device.
Definition: tap-bridge.cc:196
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:272
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:173
EventId m_stopEvent
The ID of the ns-3 event used to schedule the tear down of the underlying host Tap device and ns-3 re...
Definition: tap-bridge.h:332
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:116
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode...
Definition: tap-bridge.h:386
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src)
Definition: tap-bridge.cc:920
virtual ~TapBridge()
Definition: tap-bridge.cc:153
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:185
static TypeId GetTypeId(void)
Definition: tap-bridge.cc:79
a polymophic address class
Definition: address.h:90
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:420
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:1118
void CreateTap(void)
Call out to a separate process running as suid root in order to get our tap device created...
Definition: tap-bridge.cc:256
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:406
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1082
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:1000
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1125
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1138
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:880
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:928
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:309
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1146
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1057
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1168
void StopTapDevice(void)
Tear down the device.
Definition: tap-bridge.cc:238
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Definition: tap-bridge.h:425
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:396
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: tap-bridge.cc:1021
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Definition: tap-bridge.cc:666
std::string m_tapDeviceName
The name of the device to create on the host.
Definition: tap-bridge.h:368
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
Definition: tap-bridge.h:303
Ptr< TapBridgeFdReader > m_fdReader
Includes the ns-3 read thread used to do blocking reads on the fd corresponding to the host device...
Definition: tap-bridge.h:338
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:357
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:298
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:373
an EUI-48 address
Definition: mac48-address.h:43
bool m_ns3AddressRewritten
Whether the MAC address of the underlying ns-3 device has already been rewritten is stored in this va...
Definition: tap-bridge.h:401
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1175
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1182
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1028
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1096
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
Definition: tap-bridge.h:352
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:112
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ptr< Packet > Filter(Ptr< Packet > packet, Address *src, Address *dst, uint16_t *type)
The host we are bridged to is in the evil real world.
Definition: tap-bridge.cc:818
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
TapBridge::Mode GetMode(void)
Get the operating mode of this device.
Definition: tap-bridge.cc:1042
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1103
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1075
An identifier for simulation events.
Definition: event-id.h:53
Network layer to device interface.
Definition: net-device.h:75
A network Node.
Definition: node.h:55
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:115
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:391
void NotifyLinkUp(void)
Definition: tap-bridge.cc:1064
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:1007
tuple address
Definition: first.py:37
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1089
uint16_t m_mtu
The common mtu to use for the net devices.
Definition: tap-bridge.h:314
int m_sock
The socket (actually interpreted as fd) to use to talk to the Tap device on the real internet host...
Definition: tap-bridge.h:320
void ForwardToBridgedDevice(uint8_t *buf, ssize_t len)
Definition: tap-bridge.cc:691
a unique identifier for an interface.
Definition: type-id.h:57
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:287
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1189