src/devices/tap-bridge/tap-bridge.h
changeset 4285 2b197a6b4460
parent 4180 1ec09ce94019
child 4289 2f5b0ec50102
equal deleted inserted replaced
4261:82be63aaf35a 4285:2b197a6b4460
    36 namespace ns3 {
    36 namespace ns3 {
    37 
    37 
    38 class Node;
    38 class Node;
    39 
    39 
    40 /**
    40 /**
    41  * \ingroup devices
    41  * \ingroup tap-bridge
    42  * \defgroup tap-bridge TapBridge
       
    43  * 
    42  * 
    44  * \brief A bridge to make it appear that a host is connected to an ns-3 net device.
    43  * \brief A bridge to make it appear that a real host process is connected to 
    45  *
    44  * an ns-3 net device.
    46  * The Tap Bridge lives in a kind of a gray world somewhere between a Linux host and
    45  *
    47  * an ns-3 bridge device.  From the Linux perspective, this code appears as the user
    46  * The Tap Bridge lives in a kind of a gray world somewhere between a
    48  * mode handler for a Tap net device.  That is, when the Linux host writes to the
    47  * Linux host and an ns-3 bridge device.  From the Linux perspective,
    49  * /dev/tap device that we create for it, the write is redirected into the TapBridge
    48  * this code appears as the user mode handler for a Tap net device.  That
    50  * and from that perspective, becomes a read.  The TapBridge then redirects the data
    49  * is, when the Linux host writes to a /dev/tap device (that is either
    51  * written (by the Linux host) to the tap device on out the ns-3 net device to which
    50  * manually or automatically created depending on basic operating mode 
    52  * we are bridged.  When a packet comes in from the ns-3 world to the ns-3 net device
    51  * -- more on this later), the write is redirected into the TapBridge that
    53  * we are bridging, it appears via a callback from that net device.  Our job is to
    52  * lives in the ns-3 world; and from this perspective, becomes a read.
    54  * take those bits and write them back to the host using the user mode handler for
    53  * In other words, a Linux process writes a packet to a tap device and
    55  * /dev/tapx.  This write to the device will then appear to the Linux host as if a 
    54  * this packet is redirected to an ns-3 process where it is received by
    56  * packet has arrived on its device.
    55  * the TapBridge as a result of a read operation there.  The TapBridge
    57  *
    56  * then sends the packet to the ns-3 net device to which it is bridged.
    58  * The upshot is that the Tap Bridge appears to bridge a tap device on a Linux host 
    57  * In the other direction, a packet received by an ns-3 net device is
    59  * in the "real world" to an ns-3 net device in the simulation.  In order to do this
    58  * bridged to the TapBridge (it appears via a callback from that net
    60  * we need a "ghost node" in the simulation to hold the bridged ns-3 net device and 
    59  * device.  The TapBridge then takes that packet and writes it back to
    61  * this Tap Bridge.  This node will not be able to actually do anything else in the 
    60  * the host using the Linux TAP mechanism.  This write to the device will
    62  * simulation with respect to the Tap Bridge and its bridged net device.  This is 
    61  * then appear to the Linux host as if a packet has arrived on its
    63  * because:
    62  * device.
    64  *
       
    65  * - Bits sent to the Tap Bridge using its Send() method are completely ignored.  
       
    66  *   The Tap Bridge is not, itself, connected to any network.
       
    67  * - The bridged ns-3 net device is has had its receive callback disconnected from
       
    68  *   the ns-3 node and reconnected to the Tap Bridge.  All data received by a 
       
    69  *   bridged device will be sent to the Linux host and will not be received by the
       
    70  *   node.  You can send but you cannot ever receive.
       
    71  * 
    63  * 
    72  * You will be able to perform typical ns-3 operations on the ghost node if you so
    64  * The upshot is that the Tap Bridge appears to bridge a tap device on a
    73  * desire.  The internet stack, for example, must be there and functional on that
    65  * Linux host in the "real world" to an ns-3 net device in the simulation
    74  * node in order to participate in IP address assignment and global routing.
    66  * and make is appear that a ns-3 net device is actually installed in the
    75  * However, interfaces talking any Tap Bridge or associated bridged net devices 
    67  * Linux host.  In order to do this on the ns-3 side, we need a "ghost
    76  * will not work completely.  If you understand exactly what you are doing, you 
    68  * node" in the simulation to hold the bridged ns-3 net device and the
    77  * can set up other interfaces and devices on the ghost node and use them; but we 
    69  * TapBridge.  This node should not actually do anything else in the
    78  * generally recommend that you treat this node as a ghost of the Linux host and 
    70  * simulation since its job is simply to make the net device appear in
    79  * leave it alone.
    71  * Linux.  This is not just arbitrary policy, it is because:
       
    72  *
       
    73  * - Bits sent to the Tap Bridge from higher layers in the ghost node (using
       
    74  *   the TapBridge Send() method) are completely ignored.  The Tap Bridge is 
       
    75  *   not, itself, connected to any network, neither in Linux nor in ns-3;
       
    76  * - The bridged ns-3 net device is has had its receive callback disconnected
       
    77  *   from the ns-3 node and reconnected to the Tap Bridge.  All data received 
       
    78  *   by a bridged device will be sent to the Linux host and will not be 
       
    79  *   received by the node.  From the perspective of the ghost node, you can 
       
    80  *   send over this device but you cannot ever receive.
       
    81  *
       
    82  * Of course, if you understand all of the issues you can take control of
       
    83  * your own destiny and do whatever you want -- we do not actively
       
    84  * prevent you from using the ghost node for anything you decide.  You
       
    85  * will be able to perform typical ns-3 operations on the ghost node if
       
    86  * you so desire.  The internet stack, for example, must be there and
       
    87  * functional on that node in order to participate in IP address
       
    88  * assignment and global routing.  However, as mentioned above,
       
    89  * interfaces talking any Tap Bridge or associated bridged net devices
       
    90  * will not work completely.  If you understand exactly what you are
       
    91  * doing, you can set up other interfaces and devices on the ghost node
       
    92  * and use them; or take advantage of the operational send side of the
       
    93  * bridged devices to create traffic generators.  We generally recommend
       
    94  * that you treat this node as a ghost of the Linux host and leave it to
       
    95  * itself, though.
    80  */
    96  */
    81 
       
    82 /**
       
    83  * \ingroup tap-bridge
       
    84  * \brief A bridge to make it appear that a host is connected to an ns-3 net device.
       
    85  */
       
    86 
       
    87 class TapBridge : public NetDevice
    97 class TapBridge : public NetDevice
    88 {
    98 {
    89 public:
    99 public:
    90   static TypeId GetTypeId (void);
   100   static TypeId GetTypeId (void);
       
   101 
       
   102   /**
       
   103    * Enumeration of the operating modes supported in the class.
       
   104    *
       
   105    */
       
   106   enum Mode {
       
   107     ILLEGAL,         /**< mode not set */
       
   108     LOCAL_DEVICE,   /**< ns-3 creates and configures TAP device */
       
   109     BRIDGED_DEVICE, /**< user creates and configures TAP */  
       
   110   };
    91 
   111 
    92   TapBridge ();
   112   TapBridge ();
    93   virtual ~TapBridge ();
   113   virtual ~TapBridge ();
    94 
   114 
    95   /** \brief Get the bridged net device.
   115   /** \brief Get the bridged net device.
   131    * @param tStop the stop time
   151    * @param tStop the stop time
   132    *
   152    *
   133    * \see TapBridge::Start
   153    * \see TapBridge::Start
   134    */
   154    */
   135   void Stop (Time tStop);
   155   void Stop (Time tStop);
       
   156 
       
   157   /**
       
   158    * Set the operating mode of this device.
       
   159    *
       
   160    * \param mode The operating mode of this device.
       
   161    */
       
   162   void SetMode (TapBridge::Mode mode);
       
   163 
       
   164   /**
       
   165    * Get the operating mode of this device.
       
   166    *
       
   167    * \returns The operating mode of this device.
       
   168    */
       
   169   TapBridge::Mode  GetMode (void);
   136 
   170 
   137   //
   171   //
   138   // The following methods are inherited from NetDevice base class and are
   172   // The following methods are inherited from NetDevice base class and are
   139   // documented there.
   173   // documented there.
   140   //
   174   //
   336    */
   370    */
   337   Ptr<SystemThread> m_readThread;
   371   Ptr<SystemThread> m_readThread;
   338 
   372 
   339   /**
   373   /**
   340    * \internal
   374    * \internal
       
   375    *     
       
   376    * The operating mode of the bridge.  Tells basically who creates and
       
   377    * configures the underlying network tap.
       
   378    */
       
   379   Mode m_mode;
       
   380 
       
   381   /**
       
   382    * \internal
   341    *
   383    *
   342    * The (unused) MAC address of the TapBridge net device.  Since the TapBridge
   384    * The (unused) MAC address of the TapBridge net device.  Since the TapBridge
   343    * is implemented as a ns-3 net device, it is required to implement certain
   385    * is implemented as a ns-3 net device, it is required to implement certain
   344    * functionality.  In this case, the TapBridge is automatically assigned a
   386    * functionality.  In this case, the TapBridge is automatically assigned a
   345    * MAC address, but it is not used.  The MAC address assigned to the internet
   387    * MAC address, but it is not used.
   346    * host actually comes from the bridged (N.B. the "ed") device and not from 
       
   347    * the bridge device.
       
   348    */
   388    */
   349   Mac48Address m_address;
   389   Mac48Address m_address;
   350 
   390 
   351   /**
   391   /**
   352    * \internal
   392    * \internal
   384    */
   424    */
   385   Ipv4Address m_tapIp;
   425   Ipv4Address m_tapIp;
   386   /**
   426   /**
   387    * \internal
   427    * \internal
   388    *
   428    *
   389    * The MAC address to use as the hardware address on the host.
   429    * The MAC address to use as the hardware address on the host.  This can
       
   430    * come from one of two places depending on the operating mode.  
       
   431    *
       
   432    * If the TapBridge is in LocalDevice mode, this value comes from the MAC
       
   433    * address assigned to the bridged ns-3 net device and matches the MAC 
       
   434    * address of the underlying network TAP which we configured to have the 
       
   435    * same value.
       
   436    * 
       
   437    * If the TapBridge is in BridgedDevice mode, this value is learned from
       
   438    * from the packets received by the underlying netowrk TAP.  This is
       
   439    * because we did not configure the TAP, but have got to spoof packets
       
   440    * destined for there.
   390    */
   441    */
   391   Mac48Address m_tapMac;
   442   Mac48Address m_tapMac;
   392 
   443 
   393   /**
   444   /**
   394    * \internal
   445    * \internal