Monitor mode Wifi header manipulation fixes and Click based example
authorLalith Suresh <suresh.lalith@gmail.com>
Sun, 17 Jul 2011 11:32:05 +0530
changeset 73360ac15aa63508
parent 7335 a9fae1ab88e0
child 7337 4cbcbe42aaa6
Monitor mode Wifi header manipulation fixes and Click based example
examples/wireless/wifi-monitor-mac-high.cc
examples/wireless/wscript
src/click/examples/nsclick-monitor.click
src/click/examples/nsclick-wifi-single-interface.click
src/click/examples/wscript
src/click/model/ipv4-l3-click-protocol.cc
src/wifi/model/mac-low.cc
src/wifi/model/monitor-wifi-mac.h
src/wifi/model/wifi-net-device.cc
src/wifi/model/wifi-net-device.h
     1.1 --- a/examples/wireless/wifi-monitor-mac-high.cc	Sat Jul 16 13:57:50 2011 -0400
     1.2 +++ b/examples/wireless/wifi-monitor-mac-high.cc	Sun Jul 17 11:32:05 2011 +0530
     1.3 @@ -79,6 +79,7 @@
     1.4  #include "ns3/olsr-helper.h"
     1.5  #include "ns3/ipv4-static-routing-helper.h"
     1.6  #include "ns3/ipv4-list-routing-helper.h"
     1.7 +#include "ns3/click-internet-stack-helper.h"
     1.8  
     1.9  #include <iostream>
    1.10  #include <fstream>
    1.11 @@ -192,7 +193,6 @@
    1.12        wifinetdev->SetMonitorMode ();	// Sets the NetDevice in Monitor mode
    1.13      }
    1.14  
    1.15 -  c.Add (monitorNode);
    1.16    devices.Add (monitorDevices);
    1.17  
    1.18    MobilityHelper mobility;
    1.19 @@ -205,6 +205,7 @@
    1.20                                   "LayoutType", StringValue ("RowFirst"));
    1.21    mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
    1.22    mobility.Install (c);
    1.23 +  mobility.Install (monitorNode);
    1.24  
    1.25    // Enable OLSR
    1.26    OlsrHelper olsr;
    1.27 @@ -218,6 +219,10 @@
    1.28    internet.SetRoutingHelper (list); // has effect on the next Install ()
    1.29    internet.Install (c);
    1.30  
    1.31 +  ClickInternetStackHelper clickInternet;
    1.32 +  clickInternet.SetClickFile ("nsclick-wifi-single-interface.click");
    1.33 +  clickInternet.Install (monitorNode);
    1.34 +
    1.35    Ipv4AddressHelper ipv4;
    1.36    NS_LOG_INFO ("Assign IP Addresses.");
    1.37    ipv4.SetBase ("10.1.1.0", "255.255.255.0");
     2.1 --- a/examples/wireless/wscript	Sat Jul 16 13:57:50 2011 -0400
     2.2 +++ b/examples/wireless/wscript	Sun Jul 17 11:32:05 2011 +0530
     2.3 @@ -34,9 +34,6 @@
     2.4      obj = bld.create_ns3_program('wifi-simple-adhoc-grid', ['internet', 'mobility', 'wifi', 'olsr', 'config-store', 'tools', 'point-to-point', 'visualizer'])
     2.5      obj.source = 'wifi-simple-adhoc-grid.cc'
     2.6  
     2.7 -    obj = bld.create_ns3_program('wifi-monitor-mac', ['internet', 'mobility', 'wifi', 'olsr', 'config-store', 'tools', 'point-to-point'])
     2.8 -    obj.source = 'wifi-monitor-mac.cc'
     2.9 -
    2.10      obj = bld.create_ns3_program('wifi-simple-infra', ['internet', 'mobility', 'wifi','config-store', 'visualizer'])
    2.11      obj.source = 'wifi-simple-infra.cc'
    2.12  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/click/examples/nsclick-monitor.click	Sun Jul 17 11:32:05 2011 +0530
     3.3 @@ -0,0 +1,116 @@
     3.4 +// nsclick-wifi-single-interface.click
     3.5 +//
     3.6 +// Copyright (c) 2011, Deutsche Telekom Laboratories
     3.7 +//
     3.8 +// This program is free software; you can redistribute it and/or
     3.9 +// modify it under the terms of the GNU General Public License version
    3.10 +// 2 as published by the Free Software Foundation;
    3.11 +//
    3.12 +// This program is distributed in the hope that it will be useful,
    3.13 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 +// GNU General Public License for more details.
    3.16 +//
    3.17 +// You should have received a copy of the GNU General Public License
    3.18 +// along with this program; if not, write to the Free Software
    3.19 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.20 +//
    3.21 +// Author: Ruben Merz <ruben@net.t-labs.tu-berlin.de>
    3.22 +//
    3.23 +// This is a single host Click configuration for wifi.
    3.24 +// The node broadcasts ARP requests if it wants to find a destination
    3.25 +// address, and it responds to ARP requests made for it.
    3.26 +
    3.27 +elementclass WiFiSimHost {
    3.28 +  $ipaddr, $hwaddr |
    3.29 +
    3.30 +  cl::Classifier(12/0806 20/0001,12/0806 20/0002, -);
    3.31 +  forhost::IPClassifier(dst host $ipaddr,-);
    3.32 +  arpquerier::ARPQuerier(eth0);
    3.33 +  arpresponder::ARPResponder(eth0);
    3.34 +
    3.35 +  ethout::Queue 	
    3.36 +    -> ToDump(out_eth0.pcap,PER_NODE 1)
    3.37 +    -> ToSimDevice(eth0);
    3.38 +
    3.39 +  // All packets received on eth0 are silently
    3.40 +  // dropped if they are destined for another location
    3.41 +  FromSimDevice(eth0, SNAPLEN 4096)
    3.42 +    -> RadiotapDecap ()
    3.43 +    -> PrintWifi (MonitorNodeClick)
    3.44 +    -> ToDump(in_eth0.pcap,PER_NODE 1,ENCAP 802_11)
    3.45 +    -> WifiDecap ()
    3.46 +    -> cl;
    3.47 +
    3.48 +  // ARP queries from other nodes go to the ARP responder element
    3.49 +  cl[0] -> arpresponder;
    3.50 +
    3.51 +  // ARP responses go to our ARP query element
    3.52 +  cl[1] -> [1]arpquerier;
    3.53 +
    3.54 +  // All other packets get checked whether they are meant for us
    3.55 +  cl[2]				
    3.56 +    -> Strip (14)
    3.57 +    -> CheckIPHeader2
    3.58 +    -> MarkIPHeader
    3.59 +    -> GetIPAddress(16) // Sets destination IP address annotation from packet data
    3.60 +    -> forhost; 
    3.61 +
    3.62 +  // Packets for us are pushed outside
    3.63 +  forhost[0]
    3.64 +    ->[0]output;
    3.65 +
    3.66 +  // Packets for other folks or broadcast packets get sent to output 1
    3.67 +  forhost[1]
    3.68 +    -> ToDump(discard.pcap,2000,PER_NODE 1,ENCAP IP)
    3.69 +    -> [1]output;
    3.70 +
    3.71 +  // Incoming packets get pushed into the ARP query module
    3.72 +  input[0]
    3.73 +    -> arpquerier;
    3.74 +
    3.75 +  // Both the ARP query and response modules send data out to
    3.76 +  // the simulated network device, eth0.
    3.77 +  arpquerier
    3.78 +    -> ToDump(out_arpquery.pcap,PER_NODE 1)
    3.79 +    -> ethout;
    3.80 +
    3.81 +  arpresponder
    3.82 +    -> ToDump(out_arprespond.pcap,PER_NODE 1)
    3.83 +    -> ethout;
    3.84 +
    3.85 +}
    3.86 +
    3.87 +elementclass TapSimHost {
    3.88 +  $dev |
    3.89 +
    3.90 +  // Packets go to "tap0" which sends them to the kernel
    3.91 +  input[0]
    3.92 +    -> ToDump(tokernel.pcap,2000,IP,PER_NODE 1) 
    3.93 +    -> ToSimDevice($dev,IP);
    3.94 +
    3.95 +  // Packets sent out by the "kernel" get pushed outside
    3.96 +  FromSimDevice($dev, SNAPLEN 4096)
    3.97 +    -> CheckIPHeader2
    3.98 +    -> ToDump(fromkernel.pcap,2000,IP,PER_NODE 1)
    3.99 +    -> GetIPAddress(16)
   3.100 +    -> [0]output;
   3.101 +}
   3.102 +
   3.103 +// Instantiate elements
   3.104 +wifi::WiFiSimHost(eth0:ip,eth0:eth);
   3.105 +kernel::TapSimHost(tap0);
   3.106 +
   3.107 +// Users can do some processing between the two elements
   3.108 +wifi[0] -> kernel;
   3.109 +kernel -> wifi;
   3.110 +// Packets not for us are discarded
   3.111 +wifi[1] -> Discard;
   3.112 +
   3.113 +// It is mandatory to use an IPRouteTable element with ns-3-click
   3.114 +// (but we do not use it in this example)
   3.115 +rt :: LinearIPLookup (172.16.1.0/24 0.0.0.0 1);
   3.116 +// We are actually not using the routing table
   3.117 +Idle () -> rt;
   3.118 +rt[0] -> Discard;
   3.119 +rt[1] -> Discard;
     4.1 --- a/src/click/examples/nsclick-wifi-single-interface.click	Sat Jul 16 13:57:50 2011 -0400
     4.2 +++ b/src/click/examples/nsclick-wifi-single-interface.click	Sun Jul 17 11:32:05 2011 +0530
     4.3 @@ -35,7 +35,7 @@
     4.4  
     4.5    // All packets received on eth0 are silently
     4.6    // dropped if they are destined for another location
     4.7 -  FromSimDevice(eth0,4096)
     4.8 +  FromSimDevice(eth0, SNAPLEN 4096)
     4.9      -> ToDump(in_eth0.pcap,PER_NODE 1,ENCAP ETHER)
    4.10      -> cl;
    4.11  
    4.12 @@ -87,7 +87,7 @@
    4.13      -> ToSimDevice($dev,IP);
    4.14  
    4.15    // Packets sent out by the "kernel" get pushed outside
    4.16 -  FromSimDevice($dev,4096)
    4.17 +  FromSimDevice($dev, SNAPLEN 4096)
    4.18      -> CheckIPHeader2
    4.19      -> ToDump(fromkernel.pcap,2000,IP,PER_NODE 1)
    4.20      -> GetIPAddress(16)
     5.1 --- a/src/click/examples/wscript	Sat Jul 16 13:57:50 2011 -0400
     5.2 +++ b/src/click/examples/wscript	Sun Jul 17 11:32:05 2011 +0530
     5.3 @@ -20,3 +20,7 @@
     5.4     obj = bld.create_ns3_program('nsclick-routing',
     5.5                                  ['click', 'csma', 'internet', 'applications'])
     5.6     obj.source = 'nsclick-routing.cc'
     5.7 +
     5.8 +   obj = bld.create_ns3_program('wifi-monitor-mac',
     5.9 +                                ['click', 'wifi', 'internet', 'applications', 'olsr'])
    5.10 +   obj.source = 'wifi-monitor-mac.cc'
     6.1 --- a/src/click/model/ipv4-l3-click-protocol.cc	Sat Jul 16 13:57:50 2011 -0400
     6.2 +++ b/src/click/model/ipv4-l3-click-protocol.cc	Sun Jul 17 11:32:05 2011 +0530
     6.3 @@ -30,6 +30,7 @@
     6.4  #include "ns3/net-device.h"
     6.5  #include "ns3/uinteger.h"
     6.6  #include "ns3/object-vector.h"
     6.7 +#include "ns3/wifi-net-device.h"
     6.8  
     6.9  #include "ns3/ipv4-raw-socket-impl.h"
    6.10  #include "ns3/arp-l3-protocol.h"
    6.11 @@ -718,13 +719,20 @@
    6.12    NS_LOG_FUNCTION (this << device << p << from << to);
    6.13    Ptr<Packet> packet = p->Copy ();
    6.14  
    6.15 -  // Add an ethernet frame. This allows
    6.16 -  // Click to work with csma and wifi
    6.17 -  EthernetHeader hdr;
    6.18 -  hdr.SetSource (Mac48Address::ConvertFrom (from));
    6.19 -  hdr.SetDestination (Mac48Address::ConvertFrom (to));
    6.20 -  hdr.SetLengthType (protocol);
    6.21 -  packet->AddHeader (hdr);
    6.22 +  // If we're running a Wifi Device in Monitor mode,
    6.23 +  // then we need to use Click's Wifi MAC. Else
    6.24 +  // we attach a 'fake' ethernet header.
    6.25 +  if (!(DynamicCast<WifiNetDevice> (device) &&
    6.26 +        DynamicCast<WifiNetDevice> (device)->GetMonitorMode ()))
    6.27 +    {
    6.28 +      //Add an ethernet frame. This allows
    6.29 +      // Click to work with csma and wifi
    6.30 +      EthernetHeader hdr;
    6.31 +      hdr.SetSource (Mac48Address::ConvertFrom (from));
    6.32 +      hdr.SetDestination (Mac48Address::ConvertFrom (to));
    6.33 +      hdr.SetLengthType (protocol);
    6.34 +      packet->AddHeader (hdr);
    6.35 +    }
    6.36  
    6.37    Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (GetRoutingProtocol ());
    6.38    click->Receive (packet->Copy (), Mac48Address::ConvertFrom (device->GetAddress ()), Mac48Address::ConvertFrom (to));
     7.1 --- a/src/wifi/model/mac-low.cc	Sat Jul 16 13:57:50 2011 -0400
     7.2 +++ b/src/wifi/model/mac-low.cc	Sun Jul 17 11:32:05 2011 +0530
     7.3 @@ -677,7 +677,15 @@
     7.4     * packet queue.
     7.5     */
     7.6    WifiMacHeader hdr;
     7.7 -  packet->RemoveHeader (hdr);
     7.8 +
     7.9 +  if (!m_monitorMode)
    7.10 +    {
    7.11 +      packet->RemoveHeader (hdr);
    7.12 +    }
    7.13 +  else
    7.14 +    {
    7.15 +      packet->Copy ()->RemoveHeader (hdr);
    7.16 +    }
    7.17    m_currentRadiotapHdr = &radiotaphdr;
    7.18  
    7.19    bool isPrevNavZero = IsNavZero ();
     8.1 --- a/src/wifi/model/monitor-wifi-mac.h	Sat Jul 16 13:57:50 2011 -0400
     8.2 +++ b/src/wifi/model/monitor-wifi-mac.h	Sun Jul 17 11:32:05 2011 +0530
     8.3 @@ -22,7 +22,6 @@
     8.4  #define MONITOR_WIFI_MAC_H
     8.5  
     8.6  #include "ns3/wifi-mac.h"
     8.7 -#include "ns3/radiotap-header.h"
     8.8  
     8.9  #include "dca-txop.h"
    8.10  #include "edca-txop-n.h"
     9.1 --- a/src/wifi/model/wifi-net-device.cc	Sat Jul 16 13:57:50 2011 -0400
     9.2 +++ b/src/wifi/model/wifi-net-device.cc	Sun Jul 17 11:32:05 2011 +0530
     9.3 @@ -29,6 +29,8 @@
     9.4  #include "ns3/node.h"
     9.5  #include "ns3/trace-source-accessor.h"
     9.6  #include "ns3/log.h"
     9.7 +#include "ns3/radiotap-header.h"
     9.8 +#include "ns3/wifi-mac-header.h"
     9.9  
    9.10  NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
    9.11  
    9.12 @@ -294,7 +296,13 @@
    9.13      }
    9.14    else
    9.15      {
    9.16 -      packet->Copy ()->RemoveHeader (llc);
    9.17 +      Ptr<Packet> pCopy = packet->Copy ();
    9.18 +      RadiotapHeader rtHeader;
    9.19 +      WifiMacHeader wmHeader;
    9.20 +      pCopy->RemoveHeader (rtHeader);
    9.21 +      pCopy->RemoveHeader (wmHeader);
    9.22 +      pCopy->RemoveHeader (llc);
    9.23 +
    9.24      }
    9.25    enum NetDevice::PacketType type;
    9.26    if (to.IsBroadcast ())
    9.27 @@ -371,6 +379,12 @@
    9.28  }
    9.29  
    9.30  bool
    9.31 +WifiNetDevice::GetMonitorMode (void) const
    9.32 +{
    9.33 +  return m_monitor;
    9.34 +}
    9.35 +
    9.36 +bool
    9.37  WifiNetDevice::SupportsSendFrom (void) const
    9.38  {
    9.39    return m_mac->SupportsSendFrom ();
    10.1 --- a/src/wifi/model/wifi-net-device.h	Sat Jul 16 13:57:50 2011 -0400
    10.2 +++ b/src/wifi/model/wifi-net-device.h	Sun Jul 17 11:32:05 2011 +0530
    10.3 @@ -25,6 +25,7 @@
    10.4  #include "ns3/packet.h"
    10.5  #include "ns3/traced-callback.h"
    10.6  #include "ns3/mac48-address.h"
    10.7 +#include "ns3/radiotap-header.h"
    10.8  #include <string>
    10.9  
   10.10  namespace ns3 {
   10.11 @@ -86,6 +87,12 @@
   10.12     */
   10.13    void SetMonitorMode (void);  
   10.14  
   10.15 +  /**
   10.16 +   * Sets device in Monitor Mode. Must be done only if WifiNetDevice
   10.17 +   * has a MonitorMacHigh underneath
   10.18 +   */
   10.19 +  bool GetMonitorMode (void) const;  
   10.20 +
   10.21    // inherited from NetDevice base class.
   10.22    virtual void SetIfIndex (const uint32_t index);
   10.23    virtual uint32_t GetIfIndex (void) const;