VirtualNetDevice: Single Receive() (promisc. one); add trace sources to align better with CsmaNetDevice. Add SetAddress()
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu, 11 Jun 2009 16:02:10 +0100
changeset 4545 8566a9f6725a
parent 4544 c9bdb91e40cb
child 4546 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
--- a/examples/virtual-net-device.cc	Tue Jun 09 18:00:52 2009 +0100
+++ b/examples/virtual-net-device.cc	Thu Jun 11 16:02:10 2009 +0100
@@ -103,19 +103,19 @@
   void N3SocketRecv (Ptr<Socket> socket)
   {
     Ptr<Packet> packet = socket->Recv (65535, 0);
-    m_n3Tap->Receive (packet, 0x0800, Address ());
+    m_n3Tap->Receive (packet, 0x0800, m_n3Tap->GetAddress (), m_n3Tap->GetAddress (), NetDevice::PACKET_HOST);
   }
 
   void N0SocketRecv (Ptr<Socket> socket)
   {
     Ptr<Packet> packet = socket->Recv (65535, 0);
-    m_n0Tap->Receive (packet, 0x0800, Address ());
+    m_n0Tap->Receive (packet, 0x0800, m_n0Tap->GetAddress (), m_n0Tap->GetAddress (), NetDevice::PACKET_HOST);
   }
 
   void N1SocketRecv (Ptr<Socket> socket)
   {
     Ptr<Packet> packet = socket->Recv (65535, 0);
-    m_n0Tap->Receive (packet, 0x0800, Address ());
+    m_n1Tap->Receive (packet, 0x0800, m_n1Tap->GetAddress (), m_n1Tap->GetAddress (), NetDevice::PACKET_HOST);
   }
 
 public:
@@ -138,7 +138,8 @@
     
     // n0 tap device
     m_n0Tap = CreateObject<VirtualNetDevice> ();
-    m_n0Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
+    m_n0Tap->SetAddress (Mac48Address ("11:00:01:02:03:01"));
+    m_n0Tap->SetSendCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
     n0->AddDevice (m_n0Tap);
     Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4> ();
     uint32_t i = ipv4->AddInterface (m_n0Tap);
@@ -147,7 +148,8 @@
     
     // n1 tap device
     m_n1Tap = CreateObject<VirtualNetDevice> ();
-    m_n1Tap->SetSendFromCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
+    m_n1Tap->SetAddress (Mac48Address ("11:00:01:02:03:02"));
+    m_n1Tap->SetSendCallback (MakeCallback (&Tunnel::N0N1VirtualSend, this));
     n1->AddDevice (m_n1Tap);
     ipv4 = n1->GetObject<Ipv4> ();
     i = ipv4->AddInterface (m_n1Tap);
@@ -156,7 +158,8 @@
 
     // n3 tap device
     m_n3Tap = CreateObject<VirtualNetDevice> ();
-    m_n3Tap->SetSendFromCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
+    m_n3Tap->SetAddress (Mac48Address ("11:00:01:02:03:04"));
+    m_n3Tap->SetSendCallback (MakeCallback (&Tunnel::N3VirtualSend, this));
     n3->AddDevice (m_n3Tap);
     ipv4 = n3->GetObject<Ipv4> ();
     i = ipv4->AddInterface (m_n3Tap);
--- a/src/devices/virtual-net-device/virtual-net-device.cc	Tue Jun 09 18:00:52 2009 +0100
+++ b/src/devices/virtual-net-device/virtual-net-device.cc	Thu Jun 11 16:02:10 2009 +0100
@@ -41,10 +41,26 @@
   static TypeId tid = TypeId ("ns3::VirtualNetDevice")
     .SetParent<NetDevice> ()
     .AddConstructor<VirtualNetDevice> ()
-    .AddTraceSource ("Rx", "Received payload from the MAC layer.",
-                     MakeTraceSourceAccessor (&VirtualNetDevice::m_rxTrace))
-    .AddTraceSource ("Tx", "Send payload to the MAC layer.",
-                     MakeTraceSourceAccessor (&VirtualNetDevice::m_txTrace))
+    .AddTraceSource ("MacTx", 
+                     "Trace source indicating a packet has arrived for transmission by this device",
+                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macTxTrace))
+    .AddTraceSource ("MacPromiscRx", 
+                     "A packet has been received by this device, has been passed up from the physical layer "
+                     "and is being forwarded up the local protocol stack.  This is a promiscuous trace,",
+                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macPromiscRxTrace))
+    .AddTraceSource ("MacRx", 
+                     "A packet has been received by this device, has been passed up from the physical layer "
+                     "and is being forwarded up the local protocol stack.  This is a non-promiscuous trace,",
+                     MakeTraceSourceAccessor (&VirtualNetDevice::m_macRxTrace))
+    //
+    // Trace sources designed to simulate a packet sniffer facility (tcpdump). 
+    //
+    .AddTraceSource ("Sniffer", 
+                     "Trace source simulating a non-promiscuous packet sniffer attached to the device",
+                     MakeTraceSourceAccessor (&VirtualNetDevice::m_snifferTrace))
+    .AddTraceSource ("PromiscSniffer", 
+                     "Trace source simulating a promiscuous packet sniffer attached to the device",
+                     MakeTraceSourceAccessor (&VirtualNetDevice::m_promiscSnifferTrace))
     ;
   return tid;
 }
@@ -59,7 +75,7 @@
 
 
 void
-VirtualNetDevice::SetSendFromCallback (SendFromCallback sendCb)
+VirtualNetDevice::SetSendCallback (SendCallback sendCb)
 {
   m_sendCb = sendCb;
 }
@@ -103,27 +119,34 @@
 }
 
 bool
-VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, const Address &address)
+VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
+                           const Address &source, const Address &destination,
+                           PacketType packetType)
 {
-  if (m_rxCallback (this, packet, protocol, address))
+  // 
+  // For all kinds of packetType we receive, we hit the promiscuous sniffer
+  // hook and pass a copy up to the promiscuous callback.  Pass a copy to 
+  // make sure that nobody messes with our packet.
+  //
+  m_promiscSnifferTrace (packet);
+  if (!m_promiscRxCallback.IsNull ())
     {
-      m_rxTrace (packet);
-      return true;
+      m_macPromiscRxTrace (packet);
+      m_promiscRxCallback (this, packet, protocol, source, destination, packetType);
     }
-  return false;
-}
 
-bool
-VirtualNetDevice::PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
-                              const Address &source, const Address &destination,
-                              PacketType packetType)
-{
-  if (m_promiscRxCallback (this, packet, protocol, source, destination, packetType))
+  //
+  // If this packet is not destined for some other host, it must be for us
+  // as either a broadcast, multicast or unicast.  We need to hit the mac
+  // packet received trace hook and forward the packet up the stack.
+  //
+  if (packetType != PACKET_OTHERHOST)
     {
-      m_rxTrace (packet);
-      return true;
+      m_snifferTrace (packet);
+      m_macRxTrace (packet);
+      return m_rxCallback (this, packet, protocol, source);
     }
-  return false;
+  return true;
 }
 
 
@@ -148,7 +171,13 @@
 Address
 VirtualNetDevice::GetAddress (void) const
 {
-  return Mac48Address ();
+  return m_myAddress;
+}
+
+void
+VirtualNetDevice::SetAddress (Address addr)
+{
+  m_myAddress = addr;
 }
 
 uint16_t
@@ -206,15 +235,21 @@
 bool
 VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
 {
-  return SendFrom (packet, GetAddress (), dest, protocolNumber);
+  m_macTxTrace (packet);
+  if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
+    {
+      return true;
+    }
+  return false;
 }
 
 bool
 VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
 {
+  NS_ASSERT (m_supportsSendFrom);
+  m_macTxTrace (packet);
   if (m_sendCb (packet, source, dest, protocolNumber))
     {
-      m_txTrace (packet);
       return true;
     }
   return false;
--- a/src/devices/virtual-net-device/virtual-net-device.h	Tue Jun 09 18:00:52 2009 +0100
+++ b/src/devices/virtual-net-device/virtual-net-device.h	Thu Jun 11 16:02:10 2009 +0100
@@ -37,7 +37,7 @@
  * \brief A virtual device, similar to Linux TUN/TAP interfaces.
  *
  * A VirtualNetDevice is a "virtual" NetDevice implementation which
- * delegates to a user callback (see method SetSendFromCallback()) the
+ * delegates to a user callback (see method SetSendCallback()) the
  * task of actually transmitting a packet.  It also allows the user
  * code to inject the packet as if it had been received by the
  * VirtualNetDevice.  Together, these features allow one to build tunnels.
@@ -55,7 +55,7 @@
    * Callback the be invoked when the VirtualNetDevice is asked to queue/transmit a packet.
    * For more information, consult the documentation of NetDevice::SendFrom().
    */
-  typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendFromCallback;
+  typedef Callback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> SendCallback;
 
   static TypeId GetTypeId (void);
   VirtualNetDevice ();
@@ -66,7 +66,7 @@
    * \brief Set the user callback to be called when a L2 packet is to be transmitted
    * \param transmitCb the new transmit callback
    */
-  void SetSendFromCallback (SendFromCallback transmitCb);
+  void SetSendCallback (SendCallback transmitCb);
 
   /**
    * \brief Configure whether the virtual device needs ARP
@@ -102,30 +102,24 @@
    * \param packet packet sent from below up to Network Device
    * \param protocol Protocol type
    * \param source the address of the sender of this packet.
-   * \returns true if the packet was forwarded successfully,
-   *          false otherwise.
-   *
-   * Forward a "virtually received" packet up the node's protocol
-   * stack.
-   */
-  bool Receive (Ptr<Packet> packet, uint16_t protocol, const Address &source);
-
-
-  /**
-   * \param packet packet sent from below up to Network Device
-   * \param protocol Protocol type
-   * \param source the address of the sender of this packet.
    * \param destination the address of the receiver of this packet.
    * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
    * \returns true if the packet was forwarded successfully, false otherwise.
    *
-   * Forward a "virtually received (in promiscuous mode)" packet up
+   * Forward a "virtually received" packet up
    * the node's protocol stack.
    */
-  bool PromiscReceive (Ptr<Packet> packet, uint16_t protocol,
-                       const Address &source, const Address &destination,
-                       PacketType packetType);
+  bool Receive (Ptr<Packet> packet, uint16_t protocol,
+                const Address &source, const Address &destination,
+                PacketType packetType);
+
 
+  /**
+   * Set the MAC address of the the network device.
+   *
+   * \param addr The Address to use as the address of the device.
+   */
+  void SetAddress (Address addr);
 
   // inherited from NetDevice base class.
   virtual void SetIfIndex(const uint32_t index);
@@ -157,9 +151,13 @@
 
 private:
 
-  SendFromCallback m_sendCb;
-  TracedCallback<Ptr<const Packet> > m_rxTrace;
-  TracedCallback<Ptr<const Packet> > m_txTrace;
+  Address m_myAddress;
+  SendCallback m_sendCb;
+  TracedCallback<Ptr<const Packet> > m_macRxTrace;
+  TracedCallback<Ptr<const Packet> > m_macTxTrace;
+  TracedCallback<Ptr<const Packet> > m_macPromiscRxTrace;
+  TracedCallback<Ptr<const Packet> > m_snifferTrace;
+  TracedCallback<Ptr<const Packet> > m_promiscSnifferTrace;
   Ptr<Node> m_node;
   ReceiveCallback m_rxCallback;
   PromiscReceiveCallback m_promiscRxCallback;