A Discrete-Event Network Simulator
API
tap-bridge.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #ifndef TAP_BRIDGE_H
19 #define TAP_BRIDGE_H
20 
21 #include "ns3/address.h"
22 #include "ns3/callback.h"
23 #include "ns3/data-rate.h"
24 #include "ns3/event-id.h"
25 #include "ns3/fd-reader.h"
26 #include "ns3/mac48-address.h"
27 #include "ns3/net-device.h"
28 #include "ns3/node.h"
29 #include "ns3/nstime.h"
30 #include "ns3/packet.h"
31 #include "ns3/ptr.h"
32 #include "ns3/traced-callback.h"
33 
34 #include <cstring>
35 
36 namespace ns3
37 {
38 
44 {
45  private:
46  FdReader::Data DoRead() override;
47 };
48 
49 class Node;
50 
108 class TapBridge : public NetDevice
109 {
110  public:
115  static TypeId GetTypeId();
116 
121  enum Mode
122  {
127  };
128 
129  TapBridge();
130  ~TapBridge() override;
131 
140 
153  void SetBridgedNetDevice(Ptr<NetDevice> bridgedDevice);
154 
167  void Start(Time tStart);
168 
176  void Stop(Time tStop);
177 
183  void SetMode(TapBridge::Mode mode);
184 
191 
192  //
193  // The following methods are inherited from NetDevice base class and are
194  // documented there.
195  //
196  void SetIfIndex(const uint32_t index) override;
197  uint32_t GetIfIndex() const override;
198  Ptr<Channel> GetChannel() const override;
199  void SetAddress(Address address) override;
200  Address GetAddress() const override;
201  bool SetMtu(const uint16_t mtu) override;
202  uint16_t GetMtu() const override;
203  bool IsLinkUp() const override;
204  void AddLinkChangeCallback(Callback<void> callback) override;
205  bool IsBroadcast() const override;
206  Address GetBroadcast() const override;
207  bool IsMulticast() const override;
208  Address GetMulticast(Ipv4Address multicastGroup) const override;
209  bool IsPointToPoint() const override;
210  bool IsBridge() const override;
211  bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) override;
212  bool SendFrom(Ptr<Packet> packet,
213  const Address& source,
214  const Address& dest,
215  uint16_t protocolNumber) override;
216  Ptr<Node> GetNode() const override;
217  void SetNode(Ptr<Node> node) override;
218  bool NeedsArp() const override;
221  bool SupportsSendFrom() const override;
222  Address GetMulticast(Ipv6Address addr) const override;
223 
224  protected:
231  void DoDispose() override;
232 
244  Ptr<const Packet> packet,
245  uint16_t protocol,
246  const Address& src,
247  const Address& dst,
248  PacketType packetType);
249 
259  Ptr<const Packet> packet,
260  uint16_t protocol,
261  const Address& src);
262 
263  private:
270  void CreateTap();
271 
275  void StartTapDevice();
276 
280  void StopTapDevice();
281 
287  void ReadCallback(uint8_t* buf, ssize_t len);
288 
297  void ForwardToBridgedDevice(uint8_t* buf, ssize_t len);
298 
318  Ptr<Packet> Filter(Ptr<Packet> packet, Address* src, Address* dst, uint16_t* type);
319 
323  void NotifyLinkUp();
324 
331 
342 
347 
351  uint32_t m_ifIndex;
352 
356  uint16_t m_mtu;
357 
362  int m_sock;
363 
369 
375 
381 
387 
395 
400 
405 
410  std::string m_tapDeviceName;
411 
416 
429 
434 
444 
448  uint8_t* m_packetBuffer;
449 
455  uint32_t m_nodeId;
456 
462  bool m_linkUp{false};
463 
469  bool m_verbose;
470 
475 };
476 
477 } // namespace ns3
478 
479 #endif /* TAP_BRIDGE_H */
a polymophic address class
Definition: address.h:101
An identifier for simulation events.
Definition: event-id.h:55
A class that asynchronously reads from a file descriptor.
Definition: fd-reader.h:57
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Describes an IPv6 address.
Definition: ipv6-address.h:49
an EUI-48 address
Definition: mac48-address.h:46
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
A network Node.
Definition: node.h:57
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Class to perform the actual reading from a socket.
Definition: tap-bridge.h:44
FdReader::Data DoRead() override
The read implementation.
Definition: tap-bridge.cc:55
A bridge to make it appear that a real host process is connected to an ns-3 net device.
Definition: tap-bridge.h:109
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:939
bool ReceiveFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &src, const Address &dst, PacketType packetType)
Receives a packet from a bridged Device.
Definition: tap-bridge.cc:986
void CreateTap()
Call out to a separate process running as suid root in order to get our tap device created.
Definition: tap-bridge.cc:270
void SetIfIndex(const uint32_t index) override
Definition: tap-bridge.cc:1060
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:455
static TypeId GetTypeId()
Get the type ID.
Definition: tap-bridge.cc:81
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:443
void AddLinkChangeCallback(Callback< void > callback) override
Definition: tap-bridge.cc:1142
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
Definition: tap-bridge.h:448
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:462
Address GetAddress() const override
Definition: tap-bridge.cc:1088
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Definition: tap-bridge.h:474
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
Definition: tap-bridge.cc:1095
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device.
Definition: tap-bridge.h:341
~TapBridge() override
Definition: tap-bridge.cc:167
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Definition: tap-bridge.cc:1235
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
Definition: tap-bridge.cc:1206
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:362
void StopTapDevice()
Tear down the device.
Definition: tap-bridge.cc:252
Address GetBroadcast() const override
Definition: tap-bridge.cc:1156
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
Definition: tap-bridge.h:351
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &src)
Receives a packet from a bridged Device.
Definition: tap-bridge.cc:975
bool NeedsArp() const override
Definition: tap-bridge.cc:1228
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
Definition: tap-bridge.cc:1242
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device.
Definition: tap-bridge.h:330
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
Definition: tap-bridge.h:394
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:187
bool SetMtu(const uint16_t mtu) override
Definition: tap-bridge.cc:1109
bool IsLinkUp() const override
Definition: tap-bridge.cc:1135
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
Definition: tap-bridge.cc:1178
bool IsBridge() const override
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1185
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:380
uint16_t m_mtu
The common mtu to use for the net devices.
Definition: tap-bridge.h:356
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Definition: tap-bridge.cc:712
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Definition: tap-bridge.h:420
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
Definition: tap-bridge.h:433
bool m_verbose
Flag indicating whether or not the link is up.
Definition: tap-bridge.h:469
bool SupportsSendFrom() const override
Definition: tap-bridge.cc:1249
void StartTapDevice()
Spin up the device.
Definition: tap-bridge.cc:210
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:870
Mode m_mode
The operating mode of the bridge.
Definition: tap-bridge.h:386
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
Definition: tap-bridge.h:346
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:374
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:739
void Stop(Time tStop)
Set a stop time for the device.
Definition: tap-bridge.cc:199
void NotifyLinkUp()
Notifies that the link is up and ready.
Definition: tap-bridge.cc:1124
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
Definition: tap-bridge.h:438
bool IsBroadcast() const override
Definition: tap-bridge.cc:1149
Mode
Enumeration of the operating modes supported in the class.
Definition: tap-bridge.h:122
@ USE_BRIDGE
ns-3 uses a pre-created tap, and bridges to a bridging net device
Definition: tap-bridge.h:126
@ ILLEGAL
mode not set
Definition: tap-bridge.h:123
@ USE_LOCAL
ns-3 uses a pre-created tap, without configuring it
Definition: tap-bridge.h:125
@ CONFIGURE_LOCAL
ns-3 creates and configures tap device
Definition: tap-bridge.h:124
void SetAddress(Address address) override
Set the address of this interface.
Definition: tap-bridge.cc:1081
Ptr< Node > GetNode() const override
Definition: tap-bridge.cc:1214
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:368
bool IsMulticast() const override
Definition: tap-bridge.cc:1163
Time m_tStart
Time to start spinning up the device.
Definition: tap-bridge.h:399
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
Definition: tap-bridge.h:415
TapBridge::Mode GetMode()
Get the operating mode of this device.
Definition: tap-bridge.cc:1102
Ptr< NetDevice > GetBridgedNetDevice()
Get the bridged net device.
Definition: tap-bridge.cc:932
Time m_tStop
Time to start tearing down the device.
Definition: tap-bridge.h:404
void SetNode(Ptr< Node > node) override
Definition: tap-bridge.cc:1221
std::string m_tapDeviceName
The name of the device to create on the host.
Definition: tap-bridge.h:410
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1170
void DoDispose() override
Call out to a separate process running as suid root in order to get our tap device created.
Definition: tap-bridge.cc:180
uint16_t GetMtu() const override
Definition: tap-bridge.cc:1117
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Definition: tap-bridge.cc:1198
Ptr< Channel > GetChannel() const override
Definition: tap-bridge.cc:1074
uint32_t GetIfIndex() const override
Definition: tap-bridge.cc:1067
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode.
Definition: tap-bridge.h:428
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A structure representing data read.
Definition: fd-reader.h:91