VirtualNetDevice: Single Receive() (promisc. one); add trace sources to align better with CsmaNetDevice. Add SetAddress()
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu Jun 11 16:02:10 2009 +0100 (8 months ago)
changeset 45608566a9f6725a
parent 4559 c9bdb91e40cb
child 4561 2df865ff4369
VirtualNetDevice: Single Receive() (promisc. one); add trace sources to align better with CsmaNetDevice. Add SetAddress()
examples/virtual-net-device.cc
src/devices/virtual-net-device/virtual-net-device.cc
src/devices/virtual-net-device/virtual-net-device.h
     1.1 --- a/examples/virtual-net-device.cc	Tue Jun 09 18:00:52 2009 +0100
     1.2 +++ b/examples/virtual-net-device.cc	Thu Jun 11 16:02:10 2009 +0100
     1.3 @@ -103,19 +103,19 @@
     1.4    void N3SocketRecv (Ptr<Socket> socket)
     1.5    {
     1.6      Ptr<Packet> packet = socket->Recv (65535, 0);
     1.7 -    m_n3Tap->Receive (packet, 0x0800, Address ());
     1.8 +    m_n3Tap->Receive (packet, 0x0800, m_n3Tap->GetAddress (), m_n3Tap->GetAddress (), NetDevice::PACKET_HOST);
     1.9    }
    1.10  
    1.11    void N0SocketRecv (Ptr<Socket> socket)
    1.12    {
    1.13      Ptr<Packet> packet = socket->Recv (65535, 0);
    1.14 -    m_n0Tap->Receive (packet, 0x0800, Address ());
    1.15 +    m_n0Tap->Receive (packet, 0x0800, m_n0Tap->GetAddress (), m_n0Tap->GetAddress (), NetDevice::PACKET_HOST);
    1.16    }
    1.17  
    1.18    void N1SocketRecv (Ptr<Socket> socket)
    1.19    {
    1.20      Ptr<Packet> packet = socket->Recv (65535, 0);
    1.21 -    m_n0Tap->Receive (packet, 0x0800, Address ());
    1.22 +    m_n1Tap->Receive (packet, 0x0800, m_n1Tap->GetAddress (), m_n1Tap->GetAddress (), NetDevice::PACKET_HOST);
    1.23    }
    1.24  
    1.25  public:
    1.26 @@ -138,7 +138,8 @@
    1.27      
    1.28      // n0 tap device
    1.29      m_n0Tap = CreateObject<VirtualNetDevice> ();
    1.30 -    m_n0Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
    1.31 +    m_n0Tap->SetAddress (Mac48Address ("11:00:01:02:03:01"));
    1.32 +    m_n0Tap->SetSendCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
    1.33      n0->AddDevice (m_n0Tap);
    1.34      Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4> ();
    1.35      uint32_t i = ipv4->AddInterface (m_n0Tap);
    1.36 @@ -147,7 +148,8 @@
    1.37      
    1.38      // n1 tap device
    1.39      m_n1Tap = CreateObject<VirtualNetDevice> ();
    1.40 -    m_n1Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
    1.41 +    m_n1Tap->SetAddress (Mac48Address ("11:00:01:02:03:02"));
    1.42 +    m_n1Tap->SetSendCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
    1.43      n1->AddDevice (m_n1Tap);
    1.44      ipv4 = n1->GetObject<Ipv4> ();
    1.45      i = ipv4->AddInterface (m_n1Tap);
    1.46 @@ -156,7 +158,8 @@
    1.47  
    1.48      // n3 tap device
    1.49      m_n3Tap = CreateObject<VirtualNetDevice> ();
    1.50 -    m_n3Tap->SetSendFromCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
    1.51 +    m_n3Tap->SetAddress (Mac48Address ("11:00:01:02:03:04"));
    1.52 +    m_n3Tap->SetSendCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
    1.53      n3->AddDevice (m_n3Tap);
    1.54      ipv4 = n3->GetObject<Ipv4> ();
    1.55      i = ipv4->AddInterface (m_n3Tap);
     2.1 --- a/src/devices/virtual-net-device/virtual-net-device.cc	Tue Jun 09 18:00:52 2009 +0100
     2.2 +++ b/src/devices/virtual-net-device/virtual-net-device.cc	Thu Jun 11 16:02:10 2009 +0100
     2.3 @@ -41,10 +41,26 @@
     2.4    static TypeId tid = TypeId ("ns3::VirtualNetDevice")
     2.5      .SetParent<NetDevice> ()
     2.6      .AddConstructor<VirtualNetDevice> ()
     2.7 -    .AddTraceSource ("Rx", "Received payload from the MAC layer.",
     2.8 -                     MakeTraceSourceAccessor (&VirtualNetDevice::m_rxTrace))
     2.9 -    .AddTraceSource ("Tx", "Send payload to the MAC layer.",
    2.10 -                     MakeTraceSourceAccessor (&VirtualNetDevice::m_txTrace))
    2.11 +    .AddTraceSource ("MacTx", 
    2.12 +                     "Trace source indicating a packet has arrived for transmission by this device",
    2.13 +                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macTxTrace))
    2.14 +    .AddTraceSource ("MacPromiscRx", 
    2.15 +                     "A packet has been received by this device, has been passed up from the physical layer "
    2.16 +                     "and is being forwarded up the local protocol stack.  This is a promiscuous trace,",
    2.17 +                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macPromiscRxTrace))
    2.18 +    .AddTraceSource ("MacRx", 
    2.19 +                     "A packet has been received by this device, has been passed up from the physical layer "
    2.20 +                     "and is being forwarded up the local protocol stack.  This is a non-promiscuous trace,",
    2.21 +                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macRxTrace))
    2.22 +    //
    2.23 +    // Trace sources designed to simulate a packet sniffer facility (tcpdump). 
    2.24 +    //
    2.25 +    .AddTraceSource ("Sniffer", 
    2.26 +                     "Trace source simulating a non-promiscuous packet sniffer attached to the device",
    2.27 +                     MakeTraceSourceAccessor (&VirtualNetDevice::m_snifferTrace))
    2.28 +    .AddTraceSource ("PromiscSniffer", 
    2.29 +                     "Trace source simulating a promiscuous packet sniffer attached to the device",
    2.30 +                     MakeTraceSourceAccessor (&VirtualNetDevice::m_promiscSnifferTrace))
    2.31      ;
    2.32    return tid;
    2.33  }
    2.34 @@ -59,7 +75,7 @@
    2.35  
    2.36  
    2.37  void
    2.38 -VirtualNetDevice::SetSendFromCallback (SendFromCallback sendCb)
    2.39 +VirtualNetDevice::SetSendCallback (SendCallback sendCb)
    2.40  {
    2.41    m_sendCb = sendCb;
    2.42  }
    2.43 @@ -103,27 +119,34 @@
    2.44  }
    2.45  
    2.46  bool
    2.47 -VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, const Address &address)
    2.48 +VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
    2.49 +                           const Address &source, const Address &destination,
    2.50 +                           PacketType packetType)
    2.51  {
    2.52 -  if (m_rxCallback (this, packet, protocol, address))
    2.53 +  // 
    2.54 +  // For all kinds of packetType we receive, we hit the promiscuous sniffer
    2.55 +  // hook and pass a copy up to the promiscuous callback.  Pass a copy to 
    2.56 +  // make sure that nobody messes with our packet.
    2.57 +  //
    2.58 +  m_promiscSnifferTrace (packet);
    2.59 +  if (!m_promiscRxCallback.IsNull ())
    2.60      {
    2.61 -      m_rxTrace (packet);
    2.62 -      return true;
    2.63 +      m_macPromiscRxTrace (packet);
    2.64 +      m_promiscRxCallback (this, packet, protocol, source, destination, packetType);
    2.65      }
    2.66 -  return false;
    2.67 -}
    2.68  
    2.69 -bool
    2.70 -VirtualNetDevice::PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
    2.71 -                              const Address &source, const Address &destination,
    2.72 -                              PacketType packetType)
    2.73 -{
    2.74 -  if (m_promiscRxCallback (this, packet, protocol, source, destination, packetType))
    2.75 +  //
    2.76 +  // If this packet is not destined for some other host, it must be for us
    2.77 +  // as either a broadcast, multicast or unicast.  We need to hit the mac
    2.78 +  // packet received trace hook and forward the packet up the stack.
    2.79 +  //
    2.80 +  if (packetType != PACKET_OTHERHOST)
    2.81      {
    2.82 -      m_rxTrace (packet);
    2.83 -      return true;
    2.84 +      m_snifferTrace (packet);
    2.85 +      m_macRxTrace (packet);
    2.86 +      return m_rxCallback (this, packet, protocol, source);
    2.87      }
    2.88 -  return false;
    2.89 +  return true;
    2.90  }
    2.91  
    2.92  
    2.93 @@ -148,7 +171,13 @@
    2.94  Address
    2.95  VirtualNetDevice::GetAddress (void) const
    2.96  {
    2.97 -  return Mac48Address ();
    2.98 +  return m_myAddress;
    2.99 +}
   2.100 +
   2.101 +void
   2.102 +VirtualNetDevice::SetAddress (Address addr)
   2.103 +{
   2.104 +  m_myAddress = addr;
   2.105  }
   2.106  
   2.107  uint16_t
   2.108 @@ -206,15 +235,21 @@
   2.109  bool
   2.110  VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
   2.111  {
   2.112 -  return SendFrom (packet, GetAddress (), dest, protocolNumber);
   2.113 +  m_macTxTrace (packet);
   2.114 +  if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
   2.115 +    {
   2.116 +      return true;
   2.117 +    }
   2.118 +  return false;
   2.119  }
   2.120  
   2.121  bool
   2.122  VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
   2.123  {
   2.124 +  NS_ASSERT (m_supportsSendFrom);
   2.125 +  m_macTxTrace (packet);
   2.126    if (m_sendCb (packet, source, dest, protocolNumber))
   2.127      {
   2.128 -      m_txTrace (packet);
   2.129        return true;
   2.130      }
   2.131    return false;
     3.1 --- a/src/devices/virtual-net-device/virtual-net-device.h	Tue Jun 09 18:00:52 2009 +0100
     3.2 +++ b/src/devices/virtual-net-device/virtual-net-device.h	Thu Jun 11 16:02:10 2009 +0100
     3.3 @@ -37,7 +37,7 @@
     3.4   * \brief A virtual device, similar to Linux TUN/TAP interfaces.
     3.5   *
     3.6   * A VirtualNetDevice is a "virtual" NetDevice implementation which
     3.7 - * delegates to a user callback (see method SetSendFromCallback()) the
     3.8 + * delegates to a user callback (see method SetSendCallback()) the
     3.9   * task of actually transmitting a packet.  It also allows the user
    3.10   * code to inject the packet as if it had been received by the
    3.11   * VirtualNetDevice.  Together, these features allow one to build tunnels.
    3.12 @@ -55,7 +55,7 @@
    3.13     * Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
    3.14     * For more information, consult the documentation of NetDevice::SendFrom().
    3.15     */
    3.16 -  typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendFromCallback;
    3.17 +  typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendCallback;
    3.18  
    3.19    static TypeId GetTypeId (void);
    3.20    VirtualNetDevice ();
    3.21 @@ -66,7 +66,7 @@
    3.22     * \brief Set the user callback to be called when a L2 packet is to be transmitted
    3.23     * \param transmitCb the new transmit callback
    3.24     */
    3.25 -  void SetSendFromCallback (SendFromCallback transmitCb);
    3.26 +  void SetSendCallback (SendCallback transmitCb);
    3.27  
    3.28    /**
    3.29     * \brief Configure whether the virtual device needs ARP
    3.30 @@ -102,31 +102,25 @@
    3.31     * \param packet packet sent from below up to Network Device
    3.32     * \param protocol Protocol type
    3.33     * \param source the address of the sender of this packet.
    3.34 -   * \returns true if the packet was forwarded successfully,
    3.35 -   *          false otherwise.
    3.36 -   *
    3.37 -   * Forward a "virtually received" packet up the node's protocol
    3.38 -   * stack.
    3.39 -   */
    3.40 -  bool Receive (Ptr<Packet> packet, uint16_t protocol, const Address &source);
    3.41 -
    3.42 -
    3.43 -  /**
    3.44 -   * \param packet packet sent from below up to Network Device
    3.45 -   * \param protocol Protocol type
    3.46 -   * \param source the address of the sender of this packet.
    3.47     * \param destination the address of the receiver of this packet.
    3.48     * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
    3.49     * \returns true if the packet was forwarded successfully, false otherwise.
    3.50     *
    3.51 -   * Forward a "virtually received (in promiscuous mode)" packet up
    3.52 +   * Forward a "virtually received" packet up
    3.53     * the node's protocol stack.
    3.54     */
    3.55 -  bool PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
    3.56 -                       const Address &source, const Address &destination,
    3.57 -                       PacketType packetType);
    3.58 +  bool Receive (Ptr<Packet> packet, uint16_t protocol,
    3.59 +                const Address &source, const Address &destination,
    3.60 +                PacketType packetType);
    3.61  
    3.62  
    3.63 +  /**
    3.64 +   * Set the MAC address of the the network device.
    3.65 +   *
    3.66 +   * \param addr The Address to use as the address of the device.
    3.67 +   */
    3.68 +  void SetAddress (Address addr);
    3.69 +
    3.70    // inherited from NetDevice base class.
    3.71    virtual void SetIfIndex(const uint32_t index);
    3.72    virtual uint32_t GetIfIndex(void) const;
    3.73 @@ -157,9 +151,13 @@
    3.74  
    3.75  private:
    3.76  
    3.77 -  SendFromCallback m_sendCb;
    3.78 -  TracedCallback<Ptr<const Packet> > m_rxTrace;
    3.79 -  TracedCallback<Ptr<const Packet> > m_txTrace;
    3.80 +  Address m_myAddress;
    3.81 +  SendCallback m_sendCb;
    3.82 +  TracedCallback<Ptr<const Packet> > m_macRxTrace;
    3.83 +  TracedCallback<Ptr<const Packet> > m_macTxTrace;
    3.84 +  TracedCallback<Ptr<const Packet> > m_macPromiscRxTrace;
    3.85 +  TracedCallback<Ptr<const Packet> > m_snifferTrace;
    3.86 +  TracedCallback<Ptr<const Packet> > m_promiscSnifferTrace;
    3.87    Ptr<Node> m_node;
    3.88    ReceiveCallback m_rxCallback;
    3.89    PromiscReceiveCallback m_promiscRxCallback;