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 
43 {
44 private:
45  FdReader::Data DoRead (void);
46 };
47 
48 class Node;
49 
107 class TapBridge : public NetDevice
108 {
109 public:
114  static TypeId GetTypeId (void);
115 
120  enum Mode {
125  };
126 
127  TapBridge ();
128  virtual ~TapBridge ();
129 
138 
151  void SetBridgedNetDevice (Ptr<NetDevice> bridgedDevice);
152 
165  void Start (Time tStart);
166 
174  void Stop (Time tStop);
175 
181  void SetMode (TapBridge::Mode mode);
182 
188  TapBridge::Mode GetMode (void);
189 
190  //
191  // The following methods are inherited from NetDevice base class and are
192  // documented there.
193  //
194  virtual void SetIfIndex (const uint32_t index);
195  virtual uint32_t GetIfIndex (void) const;
196  virtual Ptr<Channel> GetChannel (void) const;
197  virtual void SetAddress (Address address);
198  virtual Address GetAddress (void) const;
199  virtual bool SetMtu (const uint16_t mtu);
200  virtual uint16_t GetMtu (void) const;
201  virtual bool IsLinkUp (void) const;
202  virtual void AddLinkChangeCallback (Callback<void> callback);
203  virtual bool IsBroadcast (void) const;
204  virtual Address GetBroadcast (void) const;
205  virtual bool IsMulticast (void) const;
206  virtual Address GetMulticast (Ipv4Address multicastGroup) const;
207  virtual bool IsPointToPoint (void) const;
208  virtual bool IsBridge (void) const;
209  virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
210  virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
211  virtual Ptr<Node> GetNode (void) const;
212  virtual void SetNode (Ptr<Node> node);
213  virtual bool NeedsArp (void) const;
216  virtual bool SupportsSendFrom () const;
217  virtual Address GetMulticast (Ipv6Address addr) const;
218 
219 protected:
226  virtual void DoDispose (void);
227 
238  bool ReceiveFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
239  Address const &src, Address const &dst, PacketType packetType);
240 
249  bool DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
250  Address const &src);
251 
252 private:
253 
260  void CreateTap (void);
261 
265  void StartTapDevice (void);
266 
270  void StopTapDevice (void);
271 
275  void ReadCallback (uint8_t *buf, ssize_t len);
276 
285  void ForwardToBridgedDevice (uint8_t *buf, ssize_t len);
286 
306  Ptr<Packet> Filter (Ptr<Packet> packet, Address *src, Address *dst, uint16_t *type);
307 
311  void NotifyLinkUp (void);
312 
319 
330 
335 
336 
340  uint32_t m_ifIndex;
341 
345  uint16_t m_mtu;
346 
351  int m_sock;
352 
358 
364 
370 
376 
384 
389 
394 
399  std::string m_tapDeviceName;
400 
405 
418 
423 
433 
437  uint8_t *m_packetBuffer;
438 
444  uint32_t m_nodeId;
445 
451  bool m_linkUp;
452 
457 };
458 
459 } // namespace ns3
460 
461 #endif /* TAP_BRIDGE_H */
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:393
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:357
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:409
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:375
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
Class to perform the actual reading from a socket.
Definition: tap-bridge.h:42
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1049
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Forward calls to a chain of Callback.
ns-3 creates and configures tap device
Definition: tap-bridge.h:122
uint32_t m_nodeId
a copy of the node id so the read thread doesn't have to GetNode() in in order to find the node ID...
Definition: tap-bridge.h:444
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:606
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:363
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:124
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode...
Definition: tap-bridge.h:417
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, Address const &src)
Receives a packet from a bridged Device.
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)
Get the type ID.
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:451
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:437
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:107
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)
Receives a packet from a bridged Device.
Definition: tap-bridge.cc:928
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:340
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:456
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:427
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:399
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:334
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:369
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:388
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:329
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:404
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:432
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:383
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:120
Describes an IPv6 address.
Definition: ipv6-address.h:48
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:405
A network Node.
Definition: node.h:56
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:123
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:422
void NotifyLinkUp(void)
Notifies that the link is up and ready.
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:345
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:351
void ForwardToBridgedDevice(uint8_t *buf, ssize_t len)
Forward a packet received from the tap device to the bridged ns-3 device.
Definition: tap-bridge.cc:691
a unique identifier for an interface.
Definition: type-id.h:58
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device...
Definition: tap-bridge.h:318
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1189