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 
277  void ReadCallback (uint8_t *buf, ssize_t len);
278 
287  void ForwardToBridgedDevice (uint8_t *buf, ssize_t len);
288 
308  Ptr<Packet> Filter (Ptr<Packet> packet, Address *src, Address *dst, uint16_t *type);
309 
313  void NotifyLinkUp (void);
314 
321 
332 
337 
338 
342  uint32_t m_ifIndex;
343 
347  uint16_t m_mtu;
348 
353  int m_sock;
354 
360 
366 
372 
378 
386 
391 
396 
401  std::string m_tapDeviceName;
402 
407 
420 
425 
435 
439  uint8_t *m_packetBuffer;
440 
446  uint32_t m_nodeId;
447 
453  bool m_linkUp;
454 
460  bool m_verbose;
461 
466 };
467 
468 } // namespace ns3
469 
470 #endif /* TAP_BRIDGE_H */
a polymophic address class
Definition: address.h:91
An identifier for simulation events.
Definition: event-id.h:54
A class that asynchronously reads from a file descriptor.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
Describes an IPv6 address.
Definition: ipv6-address.h:50
an EUI-48 address
Definition: mac48-address.h:44
Network layer to device interface.
Definition: net-device.h:96
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
A network Node.
Definition: node.h:57
Class to perform the actual reading from a socket.
Definition: tap-bridge.h:43
FdReader::Data DoRead(void)
The read implementation.
Definition: tap-bridge.cc:53
A bridge to make it appear that a real host process is connected to an ns-3 net device.
Definition: tap-bridge.h:108
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:917
TapBridge::Mode GetMode(void)
Get the operating mode of this device.
Definition: tap-bridge.cc:1072
void StopTapDevice(void)
Tear down the device.
Definition: tap-bridge.cc:243
void StartTapDevice(void)
Spin up the device.
Definition: tap-bridge.cc:201
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1133
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
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1184
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1119
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:446
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1219
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:1044
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:434
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1168
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:439
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:453
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Definition: tap-bridge.h:465
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: tap-bridge.cc:1051
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: tap-bridge.cc:1148
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1112
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1065
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1205
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device.
Definition: tap-bridge.h:331
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1126
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:353
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:342
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1105
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1155
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1212
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device.
Definition: tap-bridge.h:320
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1176
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1191
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1198
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
Definition: tap-bridge.h:385
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:178
void NotifyLinkUp(void)
Notifies that the link is up and ready.
Definition: tap-bridge.cc:1094
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1140
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:371
uint16_t m_mtu
The common mtu to use for the net devices.
Definition: tap-bridge.h:347
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Definition: tap-bridge.cc:696
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Definition: tap-bridge.h:411
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:1037
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:424
bool m_verbose
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:460
static TypeId GetTypeId(void)
Get the type ID.
Definition: tap-bridge.cc:79
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
virtual ~TapBridge()
Definition: tap-bridge.cc:158
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1087
Mode m_mode
The operating mode of the bridge.
Definition: tap-bridge.h:377
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:1030
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
Definition: tap-bridge.h:336
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:365
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
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
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:190
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
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:429
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1058
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:120
@ USE_BRIDGE
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:124
@ ILLEGAL
mode not set
Definition: tap-bridge.h:121
@ USE_LOCAL
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:123
@ CONFIGURE_LOCAL
ns-3 creates and configures tap device
Definition: tap-bridge.h:122
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:359
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:390
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:406
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:395
std::string m_tapDeviceName
The name of the device to create on the host.
Definition: tap-bridge.h:401
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1079
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
Ptr< NetDevice > GetBridgedNetDevice(void)
Get the bridged net device.
Definition: tap-bridge.cc:910
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode.
Definition: tap-bridge.h:419
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A structure representing data read.