NS3 Layer Tracing
NS3 Layer Tracing
h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
/**
The network looks like this
n0--| |--n2
|--n4---n5--|
n1--| |--n3
LAN1 LAN2
to enable IP-level logging. You will notice that the packet will be printed as it travels from node 0, to node 4, to node 5, to node 2.
Then node 2 will reply to node 0. The packet will be printed, starting from the IPv4 header
To enable data-link layer tracing, and print MAC headers, run it with --mac
./waf --run LayerTracing --mac
You will notice that the packet has its data-link layer header replaced as it traverses different links.
It will have an Ethernet header going from node 0, to node 4
Node 4 removes the ethernet header, and attaches a PPP header when it passes it to node 5. This is because the link between node 4 & node 5 is
of type PPP.
In addition, before packets are sent, nodes will make ARP messages to obtain the MAC address corresponding to IPv4 Addresses before messages
are sent.
*/
NS_LOG_COMPONENT_DEFINE ("TracingExample");
NodeContainer lan1_nodes;
NodeContainer lan2_nodes;
NodeContainer routers;
lan1_nodes.Create (n);
lan2_nodes.Create (n);
routers.Create (2);
/*
Setup the connection between the middle router nodes
*/
PointToPointHelper p2p;
p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
p2p.SetDeviceAttribute ("DataRate", DataRateValue(DataRate ("10Mbps")));
NetDeviceContainer router_devs = p2p.Install (routers);
CsmaHelper csma1;
csma1.SetChannelAttribute ("Delay", StringValue ("10us"));
csma1.SetChannelAttribute ("DataRate", StringValue ("1Gbps"));
NetDeviceContainer lan1_devs = csma1.Install (lan1_nodes);
CsmaHelper csma2;
csma2.SetChannelAttribute ("Delay", StringValue ("10us"));
csma2.SetChannelAttribute ("DataRate", StringValue ("1Gbps"));
NetDeviceContainer lan2_devs = csma2.Install (lan2_nodes);
//Setup IPs
InternetStackHelper stack;
stack.InstallAll ();
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer lan1_interface = address.Assign (lan1_devs);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer lan2_interface = address.Assign (lan2_devs);
address.SetBase ("100.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer router_interface = address.Assign (router_devs);
//Incoming packets on CsmaNetDevice on data-link layer level (MAC). They will have Ethernet header.
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::CsmaNetDevice/MacTx", MakeCallback (&MacTxTrace));
//Incoming packet on a PointToPointNetDevice on data-link layer level (MAC). They will have PPP header.
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx", MakeCallback (&MacTxTrace));
//MAC Reception traces.
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::CsmaNetDevice/MacRx", MakeCallback (&MacRxTrace));
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacRx", MakeCallback (&MacRxTrace));
Packet::EnablePrinting ();
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
Simulator::Stop ( Seconds (simulation_time));
Simulator::Run ();
flowMonitor->SerializeToXmlFile ("layer-tracing.xml", true, true);
Simulator::Destroy ();
return 0;