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 
458  bool m_verbose;
459 
464 };
465 
466 } // namespace ns3
467 
468 #endif /* TAP_BRIDGE_H */
ns3::TapBridge::USE_LOCAL
@ USE_LOCAL
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:123
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::TapBridge::GetBridgedNetDevice
Ptr< NetDevice > GetBridgedNetDevice(void)
Get the bridged net device.
Definition: tap-bridge.cc:910
ns3::TapBridge::m_linkUp
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:451
ns3::TapBridge::m_node
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
Definition: tap-bridge.h:334
ns3::TapBridge::m_tapMac
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
ns3::TapBridge::StartTapDevice
void StartTapDevice(void)
Spin up the device.
Definition: tap-bridge.cc:201
ns3::TapBridge::GetChannel
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:1044
ns3::TapBridge::m_mtu
uint16_t m_mtu
The common mtu to use for the net devices.
Definition: tap-bridge.h:345
ns3::TapBridge::IsLinkUp
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1105
ns3::EventId
An identifier for simulation events.
Definition: event-id.h:54
ns3::Callback< void >
ns3::TapBridge::m_rxCallback
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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TapBridge::ForwardToBridgedDevice
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:721
ns3::TapBridge::m_tapDeviceName
std::string m_tapDeviceName
The name of the device to create on the host.
Definition: tap-bridge.h:399
ns3::TapBridge::GetMode
TapBridge::Mode GetMode(void)
Get the operating mode of this device.
Definition: tap-bridge.cc:1072
ns3::TapBridge::IsMulticast
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1133
ns3::TapBridge::SetReceiveCallback
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1205
ns3::TapBridge::m_nodeId
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
ns3::Node
A network Node.
Definition: node.h:57
ns3::TapBridgeFdReader::DoRead
FdReader::Data DoRead(void)
The read implementation.
Definition: tap-bridge.cc:53
ns3::TapBridge::ReceiveFromBridgedDevice
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:958
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::TapBridge::Stop
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:190
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::TapBridge::Mode
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:120
ns3::Mac48Address
an EUI-48 address
Definition: mac48-address.h:44
ns3::TapBridge::SetAddress
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: tap-bridge.cc:1051
ns3::TapBridge::m_mode
Mode m_mode
The operating mode of the bridge.
Definition: tap-bridge.h:375
ns3::TapBridge::USE_BRIDGE
@ USE_BRIDGE
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:124
ns3::TapBridge::SetIfIndex
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:1030
ns3::TapBridge::m_stopEvent
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
ns3::TapBridge::DoDispose
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:171
ns3::TapBridge::GetMtu
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1087
ns3::TapBridge::m_address
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
Definition: tap-bridge.h:383
ns3::TapBridge::GetMulticast
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1140
ns3::TapBridge::m_fdReader
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
ns3::TapBridge::m_tStop
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:393
ns3::TapBridge::GetBroadcast
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1126
ns3::TapBridge::m_packetBuffer
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:437
ns3::Ptr< NetDevice >
ns3::TapBridge::IsBroadcast
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1119
ns3::TapBridge::AddLinkChangeCallback
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1112
ns3::TapBridge::m_bridgedDevice
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:427
ns3::TapBridge::m_tapGateway
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:404
ns3::TapBridge::m_tapNetmask
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:422
ns3::TapBridge::SetBridgedNetDevice
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:917
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::TapBridge::SetNode
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1191
ns3::TapBridge::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: tap-bridge.cc:79
ns3::TapBridge::ReadCallback
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Definition: tap-bridge.cc:696
ns3::TapBridge::StopTapDevice
void StopTapDevice(void)
Tear down the device.
Definition: tap-bridge.cc:243
ns3::TapBridge::m_linkChangeCallbacks
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Definition: tap-bridge.h:463
ns3::TapBridge::IsBridge
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1155
first.address
address
Definition: first.py:44
ns3::TapBridge::TapBridge
TapBridge()
Definition: tap-bridge.cc:144
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::TapBridge::CreateTap
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:261
ns3::TapBridge::GetIfIndex
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:1037
ns3::TapBridge::m_startEvent
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
ns3::TapBridge::SendFrom
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1176
ns3::TapBridge::m_ifIndex
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:340
ns3::TapBridge::ILLEGAL
@ ILLEGAL
mode not set
Definition: tap-bridge.h:121
ns3::TapBridge::m_verbose
bool m_verbose
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:458
ns3::TapBridge::Send
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1168
ns3::TapBridge::SetPromiscReceiveCallback
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1212
ns3::TapBridge::m_tapIp
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Definition: tap-bridge.h:409
ns3::TapBridge::SetMtu
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1079
ns3::TapBridge::~TapBridge
virtual ~TapBridge()
Definition: tap-bridge.cc:158
ns3::TapBridge::IsPointToPoint
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: tap-bridge.cc:1148
ns3::TapBridge::NotifyLinkUp
void NotifyLinkUp(void)
Notifies that the link is up and ready.
Definition: tap-bridge.cc:1094
ns3::FdReader
A class that asynchronously reads from a file descriptor.
Definition: unix-fd-reader.h:48
ns3::TapBridge::GetNode
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1184
ns3::TapBridge::m_tStart
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:388
ns3::TapBridge::m_ns3AddressRewritten
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
ns3::TapBridge::GetAddress
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1058
ns3::TapBridge::DiscardFromBridgedDevice
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:950
ns3::Ipv4Mask
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
ns3::TapBridge::SupportsSendFrom
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1219
ns3::TapBridge::NeedsArp
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1198
ns3::TapBridge::m_promiscRxCallback
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
ns3::NetDevice::PacketType
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
ns3::TapBridge::CONFIGURE_LOCAL
@ CONFIGURE_LOCAL
ns-3 creates and configures tap device
Definition: tap-bridge.h:122
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition: traced-callback.h:53
ns3::TapBridgeFdReader
Class to perform the actual reading from a socket.
Definition: tap-bridge.h:43
ns3::TapBridge::SetMode
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1065
ns3::TapBridge::Filter
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:848
ns3::TapBridge::m_sock
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
ns3::TapBridge
A bridge to make it appear that a real host process is connected to an ns-3 net device.
Definition: tap-bridge.h:108
ns3::TapBridge::Start
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:178
ns3::NetDevice
Network layer to device interface.
Definition: net-device.h:96
ns3::FdReader::Data
A structure representing data read.
Definition: unix-fd-reader.h:77