A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tap-bridge.h
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:
220  virtual void DoDispose (void);
221 
222  bool ReceiveFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
223  Address const &src, Address const &dst, PacketType packetType);
224 
225  bool DiscardFromBridgedDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, Address const &src);
226 
227 private:
228 
237  void CreateTap (void);
238 
244  void StartTapDevice (void);
245 
251  void StopTapDevice (void);
252 
258  void ReadCallback (uint8_t *buf, ssize_t len);
259 
260  /*
261  * \internal
262  *
263  * Forward a packet received from the tap device to the bridged ns-3
264  * device
265  *
266  * \param buf A character buffer containing the actual packet bits that were
267  * received from the host.
268  * \param buf The length of the buffer.
269  */
270  void ForwardToBridgedDevice (uint8_t *buf, ssize_t len);
271 
292  Ptr<Packet> Filter (Ptr<Packet> packet, Address *src, Address *dst, uint16_t *type);
293 
302 
315 
322 
323 
329  uint32_t m_ifIndex;
330 
336  uint16_t m_mtu;
337 
344  int m_sock;
345 
353 
361 
369 
377 
387 
394 
401 
408  std::string m_tapDeviceName;
409 
416 
433 
440 
454 
458  uint8_t *m_packetBuffer;
459 
460  /*
461  * a copy of the node id so the read thread doesn't have to GetNode() in
462  * in order to find the node ID. Thread unsafe reference counting in
463  * multithreaded apps is not a good thing.
464  */
465  uint32_t m_nodeId;
466 };
467 
468 } // namespace ns3
469 
470 #endif /* TAP_BRIDGE_H */
EventId m_startEvent
Definition: tap-bridge.h:352
virtual Ptr< Channel > GetChannel(void) const
Definition: tap-bridge.cc:993
Ipv4Address m_tapIp
Definition: tap-bridge.h:422
virtual void DoDispose(void)
Definition: tap-bridge.cc:181
keep track of time unit.
Definition: nstime.h:149
virtual void SetNode(Ptr< Node > node)
Definition: tap-bridge.cc:1129
virtual Ptr< Node > GetNode(void) const
Definition: tap-bridge.cc:1122
A structure representing data read.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: tap-bridge.cc:1078
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
Definition: tap-bridge.cc:866
void SetMode(TapBridge::Mode mode)
Definition: tap-bridge.cc:1014
virtual bool SetMtu(const uint16_t mtu)
Definition: tap-bridge.cc:1028
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
void StartTapDevice(void)
Definition: tap-bridge.cc:211
void Start(Time tStart)
Set a start time for the device.
Definition: tap-bridge.cc:188
EventId m_stopEvent
Definition: tap-bridge.h:360
Mac48Address m_tapMac
Definition: tap-bridge.h:432
void Stop(Time tStop)
Definition: tap-bridge.cc:200
a polymophic address class
Definition: address.h:86
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:1086
void CreateTap(void)
Definition: tap-bridge.cc:268
uint8_t * m_packetBuffer
Definition: tap-bridge.h:458
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: tap-bridge.cc:1051
virtual void SetIfIndex(const uint32_t index)
Definition: tap-bridge.cc:979
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: tap-bridge.cc:1093
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1106
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:859
FdReader::Data DoRead(void)
The read implementation.
Definition: tap-bridge.cc:69
uint32_t m_ifIndex
Definition: tap-bridge.h:329
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: tap-bridge.cc:1114
virtual uint16_t GetMtu(void) const
Definition: tap-bridge.cc:1036
virtual bool NeedsArp(void) const
Definition: tap-bridge.cc:1136
void StopTapDevice(void)
Definition: tap-bridge.cc:250
Ptr< NetDevice > m_bridgedDevice
Definition: tap-bridge.h:446
virtual void SetAddress(Address address)
Definition: tap-bridge.cc:1000
void ReadCallback(uint8_t *buf, ssize_t len)
Definition: tap-bridge.cc:646
std::string m_tapDeviceName
Definition: tap-bridge.h:408
Ptr< Node > m_node
Definition: tap-bridge.h:321
Ptr< TapBridgeFdReader > m_fdReader
Definition: tap-bridge.h:368
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Definition: tap-bridge.h:314
Ipv4Address m_tapGateway
Definition: tap-bridge.h:415
an EUI-48 address
Definition: mac48-address.h:41
bool m_ns3AddressRewritten
Definition: tap-bridge.h:453
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: tap-bridge.cc:1143
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Definition: tap-bridge.cc:1150
virtual Address GetAddress(void) const
Definition: tap-bridge.cc:1007
virtual Address GetBroadcast(void) const
Definition: tap-bridge.cc:1064
Mac48Address m_address
Definition: tap-bridge.h:386
Describes an IPv6 address.
Definition: ipv6-address.h:44
Ptr< Packet > Filter(Ptr< Packet > packet, Address *src, Address *dst, uint16_t *type)
Definition: tap-bridge.cc:798
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
TapBridge::Mode GetMode(void)
Definition: tap-bridge.cc:1021
virtual bool IsMulticast(void) const
Definition: tap-bridge.cc:1071
virtual bool IsLinkUp(void) const
Definition: tap-bridge.cc:1044
an identifier for simulation events.
Definition: event-id.h:46
Network layer to device interface.
Definition: net-device.h:75
A network Node.
Definition: node.h:56
Ipv4Mask m_tapNetmask
Definition: tap-bridge.h:439
virtual uint32_t GetIfIndex(void) const
Definition: tap-bridge.cc:986
virtual bool IsBroadcast(void) const
Definition: tap-bridge.cc:1057
uint16_t m_mtu
Definition: tap-bridge.h:336
a unique identifier for an interface.
Definition: type-id.h:44
NetDevice::ReceiveCallback m_rxCallback
Definition: tap-bridge.h:301
virtual bool SupportsSendFrom() const
Definition: tap-bridge.cc:1157