A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <string.h>
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 */