ns3_intro_slides
ns3_intro_slides
Adil Alsuhaim
School of Computing
Clemson University
October 5, 2020
https://www.nsnam.org/wiki/Installation
I Installation Prerequisites. The minimal requirement for ns-3 is to have C++ &
python3 is installed in the system.
I ns-3 uses a python script to compile & run your C++ code
I It is also possible to use clang++ instead of g++
./bake.py check
I If the python script fails due to missing python libraries, use pip to install them.
I pip is a python package-management system.
I Install pip with sudo apt-get install python3-pip
I Run ./bake.py show to see what bake will install & download.
./bake.py download
I Build it
./bake.py build
I Run an example.
I All ns-3 simulations must be run using ./waf from the ns-3 root directory.
I ns-3 uses the g++ command to compile C++ code. You can congure ns-3 to use
another C++ compiler such as clang++. If you do that, the entire ns-3 code must
be compiled again.
I You can download these IDEs from the internet for free.
I Those IDEs allows us to have syntax highlighting and auto-completion features if you
set them up properly with ns-3.
code .
${workspaceFolder}/build/ns3/**
/home/adil/ns-3.30.1/build
I Import File System" and import the bake/source directory that was created with
the bake tool.
I Navigate to the ns-3 folder. If you installed ns-3.31, then that's the ns-3 folder.
I Open the le scratch/scratch-simulator.cc
I Check if the C++ is highlighted with colors for keywords and class names.
I If you did not get syntax highlighting, try to re-import one directory higher or lower.
I To run ns-3, we typically want to use the terminal, navigate to ns-3 root folder,
and use ./waf
${workspace_loc:/EclipseProjectName}/ns-3.30/waf
${workspace_loc:/EclipseProjectName}/ns-3.30/build
${workspace_loc:/EclipseProjectName}/ns-3.30/waf
${workspace_loc:/EclipseProjectName}/ns-3.30/waf
--run ${string_prompt}
I Now when you click Run" with this tool, Eclipse prompt you to enter the ns-3
project name.
I ns-3 comes with many examples. Relative to ns-3 root directory, there are
examples under the examples directory. In addition, every module under src also
contains its own examples directory.
I To run the example ./examples/udp/udp-echo.cc run the following command
from ns-3 root directory:
I The example does not print anything to the terminal, but it will create output les of
type .pcap and .tr
I To examine .pcap les, open them with a packet analyzer like Wireshark.
I the resulting .tr le is an ASCII trace of the packets.
Introduction to ns-3 Simulation Adil Alsuhaim 15 / 1
ns-3 directory structure - Creating projects under scratch
I To create an ns-3 project, create a folder under scratch, let's say you called it
Project1.
I Under Project1, one .cc le must contain a main function
I To run Project1, you need to be at the ns-3 root directory, and run
I We can supply some arguments, the rest will be their default values
I We can also run it without any arguments. The default values will be used for all
dev1 PHY
Implemented in device or separate class
... ...
Uses sub classes of ns3::Channel
ApplicationList
app0
app1 subclass of ns3::Application
+ StartApplication(): void
+ StopApplication(): void
Extends
CustomApplication
+ StartApplication() : void
OcbWifiMac Uses
+ m_edca: EdcaQueues
YansWifiChannel
NodeContainer nodes;
nodes.Create (10); //create 10 nodes
//Get a pointer to the first node
Ptr<Node> n0 = nodes.Get (0);
//iterate over a NodeContainer
for (uint32_t i=0 ; i<nodes.GetN() ; i++)
{
Ptr<Node> ni = nodes.Get (i);
//...
}
I A node can contain multiple devices. You can iterate through them in a loop.
I Channels. dene a connection between a set of nodes. Nodes that are within the
same broadcast domain share one ns3::Channel object.
I ns3::Channel is an abstract class. Concrete sub-classes include
PointToPointChannel, CsmaChannel (for Ethernet), and YansWifiChannel for
WiFi.
Packet::EnablePrinting (); //Must be done before you start your simulation, or you'll get an error
I Then you can use ToString(), which only works if you enabled metadata. To get a
string representation of a Packet object named p
I You can examine & remove packet headers using PeekHeader and RemoveHeader.
This can be tricky if you want to get the expected results, as you need to
examine/remove headers in order.
EthernetHeader ethernet;
if (p->PeekHeader (ethernet)) //works if there's enough data to cover an EthernetHeader (14 bytes or more)
{
std::cout << "Src MAC: " << ethernet.GetSource () << " Dest MAC: " << ethernet.GetDestination () << std::endl;
}
I This is a packet without header. PeekHeader works, but doesn't give the expected
results.
MyDataTag t;
t.SetValue (22.5);
p->AddPacketTag (t);
MyDataTag t;
if (p->PeekPacketTag (t)){
std::cout << "Packet Info: " << t.GetValue () << std::endl;
}
I Helpers are a convenient way to congure nodes. However, you can still congure
nodes manually if you want more control.
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
I We use InternetStackHelper to
InternetStackHelper stack;
setup IP networking for a set of nodes. stack.Install (nodes);
I TheUdpEchoServerHelper is used
to install a
UdpEchoServer, which is a
C++ sub-class of ns3::Application, UdpEchoServerHelper echoServer (9999);
to node 1
ApplicationContainer serverApps = echoServer.Install
I it listens on port number 9999. ,→ (nodes.Get (1));
I The application starts after 1 second serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
from the start of simulation.
I It stops after 10 seconds of start of
simulation.
CsmaChannel
CsmaNetDevice PointToPointNetDevice PointToPointNetDevice
CsmaNetDevice PointToPointChannel
CsmaNetDevice
lan1_nodes.Create (n1);
lan2_nodes.Create (n2);
router_nodes.Create (3);
//Add router 1 to LAN 1
lan1_nodes.Add (router_nodes.Get (0));
//Doing the same for LAN 2: Router 3 is on LAN 2, so we add it to the node container
lan2_nodes.Add (router_nodes.Get (1));
CsmaHelper csma2;
csma2.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma2.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
//Actually install CsmaNetDevice to LAN 2, including Router 3
NetDeviceContainer lan2Devices;
lan2Devices = csma2.Install (lan2_nodes);
//For routers to be able to forward packets, they need to have routing rules.
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
I The Packet class does not inherit from ns3::Object. To create a Packet with a
payload of size 200 bytes (all zeros), we will use Create<T> function as follows.
I To copy an object, we will use the functions Copy & CopyObject depending on
whether the object we want to copy is of a type that is a subclass of ns3::Object
I Most
ns-3 C++ classes implement the functions GetTypeId &
GetInstanceTypeId
I In GetTypeId, the type is registered as ns3::TypeId, along with:
I Attributes. which are attributes of a given class. Attributes are inherited by subclasses.
I TraceSources. which represent an event that is triggered during the simulation. You can
connect them to a callback function to handle what happens when the event occurs.
I We can use the Attributes and TraceSources using the Config namespace
I Let us say we want to change the packet size after the UdpEchoClient application
was created. We do this, as one would expect
Config::Set("/NodeList/*/ApplicationList/*/$ns3::UdpEchoClient/PacketSize", UintegerValue(512));
I The second asterisk means match all applications in a node, and the following
$ns3::UdpEchoClient specify the matching type.
TypeId
UdpClient::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::UdpClient")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<UdpClient> ()
.AddAttribute ("MaxPackets",
"The maximum number of packets the application will send",
UintegerValue (100),
MakeUintegerAccessor (&UdpClient::m_count),
MakeUintegerChecker<uint32_t> ())
.AddAttribute ("Interval",
"The time to wait between packets", TimeValue (Seconds (1.0)),
MakeTimeAccessor (&UdpClient::m_interval),
MakeTimeChecker ())
...
...
.AddAttribute ("TxQueue",
"A queue to use as the transmit queue in the device.",
PointerValue (),
MakePointerAccessor (&PointToPointNetDevice::m_queue),
MakePointerChecker<Queue<Packet> > ())
.AddTraceSource ("MacTx",
"Trace source indicating a packet has arrived "
"for transmission by this device",
MakeTraceSourceAccessor (&PointToPointNetDevice::m_macTxTrace),
"ns3::Packet::TracedCallback")
...
I When using Config to use Attributes and TraceSources, a run-time error may occur
if it was improper.
I Usually, when the signiture of the callback function does not match the required
signiture.
I You might have developed the habit of printing out to the terminal to inspect how
your code is working.
I It helps investigate whether certain code is reached.
I It can be used to print intermediary values.
I However, if you want stop printing you either have to remove the printing statements,
or commenting them out.
I It takes time!
I ns-3 provides a way to enable/disable print statement with its Logging functionality.
I ns-3 components can be dened as a log component for which printing can be
enabled & disabled on multiple levels.
Level Meaning
NS_LOG="UdpEchoClientApplication:UdpEchoServerApplication=info" ./waf
,→ --run examples/tutorial/first
I When you enable logging from the command-line, you only enable the specied level.
level.
Introduction to ns-3 Simulation Adil Alsuhaim 67 / 1
Log prex
I ns-3 can add prexes to log messages. For example, adding the function name before
the log message. Let us say we want to enable all levels for our components, but we
want the node name as a prex.
I The prex can be node, time, level or all. The all prex is the default.
I all prints out a prex of current simulation time (in seconds), followed by the node id
(if possible, or -1), then the function in which the log statement is, then the log level
followed by the log message
I To make your simulation generate an XML le for netanim, add the lines
https://github.com/addola/NS3-HelperScripts/tree/master/
examples/SimpleUdpAppExample