ns3 Tut3
ns3 Tut3
2
Exploring the directories
cd examples/tutorial
3
Topology
Point-to-point
4
Topology in ns-3 (first.cc)
Application Application
Application Application
Sockets-like
API
Packet(s)
Protocol Protocol
stack stack
Node Node
5
vi examples/tutorial/first.cc
emacs mode line: formatting conventions (coding style)
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
6
first.cc
Module Includes
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h“
C++ namespace called ns3
using namespace ns3;
Logging
NS_LOG_COMPONENT_DEFINE("FirstScriptExample");
7
first.cc
define a main function
int
main (int argc, char *argv[])
{
enable two logging components
LogComponentEnable
("UdpEchoClientApplication",LOG_LEVEL_INFO);
LogComponentEnable
("UdpEchoServerApplication",LOG_LEVEL_INFO);
8
NodeContainer
Provides a convenient way
to create, manage and
access any Node objects
Node-2
Stores pointer to those Node-1
objects internally Node -0
The nodes as they stand in
the script do nothing
To construct a topology is to
Container picture from www.TheContainerMan.com
connect our nodes together
into a network
9
PointToPointHelper
To construct a point to point link
Perform the low-level work required to put the link
together
Typically these two things NetDevice and Channel
are intimately tied together (cannot expect to
interchange)
Ethernet devices and CSMA channels
Wifi devices and wireless channels
10
first.cc
create the ns-3 Node objects
NodeContainer nodes;
nodes.Create (2);
instantiates a PointToPointHelper object on the stack
PointToPointHelper pointToPoint;
as the “DataRate” when it creates a PointToPointNetDevice object
pointToPoint.SetDeviceAttribute ("DataRate",
StringValue ("5Mbps"));
as the “Delay” when it creates a PointToPointChannel object
pointToPoint.SetChannelAttribute ("Delay",
StringValue ("2ms"));
11
first.cc
create the NetDevice objects
NetDeviceContainer devices;
Node -0 Node -1
Point-to-point
Netdevice-0 Netdevice-0
Channel
12
first.cc
install an Internet Stack
(TCP, UDP, IP, etc.) on each of the nodes in the node container
InternetStackHelper stack;
stack.Install (nodes);
Node -0 Node -1
13
first.cc
to associate the devices on our nodes with IP addresses
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0","255.255.255.0");
14
first.cc
performs the actual address assignment
Ipv4InterfaceContainer interfaces =
address.Assign (devices);
Node -0 Node -1
15
first.cc
set up a UDP echo server application
UdpEchoServerHelper echoServer (9);
16
first.cc
install server application @node(1)
ApplicationContainer serverApps =
echoServer.Install (nodes.Get (1));
Node -0 Node -1
17
first.cc echo server application starts (enable itself) at 1 sec
and to Stop (disable itself) at 10 sec
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
Node -0 Node -1
18
first.cc
Set remote add and port
UdpEchoClientHelper echoClient (interfaces.GetAddress(1),9);
19
first.cc
install client application @node(0)
ApplicationContainer clientApps =
echoClient.Install (nodes.Get (0));
Set echo Client’s attributes
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Node -0 Node -1
20
first.cc
to actually run the simulation
Simulator::Run ();
to clean up
Simulator::Destroy ();
return 0;
}
Node -0 Node -1
21
Classes
Container
NodeContainer
NetDeviceContainer
Ipv4InterfaceContainer
ApplicationContainer
Helper
PointToPointHelper
InternetStackHelper
Ipv4AddressHelper
UdpEchoServerHelper
UdpEchoClientHelper
22
Building and Running Your Script
Copy examples/tutorial/first.cc into the scratch
directory
cp examples/tutorial/first.cc scratch/myfirst.cc
Now build your script using waf
./waf
Run it out of the scratch directory
./waf --run scratch/myfirst
Output
Waf: Entering directory `/home/anan/tarballs/ns-allinone-3.10/ns-3.10/build'
[ 568/1204] cxx: scratch/myfirst.cc -> build/debug/scratch/myfirst_3.o
[1204/1204] cxx_link: build/debug/scratch/myfirst_3.o -> build/debug/scratch/myfirst
Waf: Leaving directory `/home/anan/tarballs/ns-allinone-3.10/ns-3.10/build'
'build' finished successfully (8.591s)
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2
23
output
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2
Point-to-point
echoClient echoServer
10.1.1.1/24 10.1.1.2/24
24
Logging
Adding log in the code before creating nodes
NS_LOG_INFO (“JCSSE 2011”);
./waf
export NS_LOG=
./waf --run scratch/myfirst
export NS_LOG=FirstScriptExample=info
./waf --run scratch/myfirst
25
Move on …
Building a Bus Network Topology
10.1.1.0
Point-to-point
10.1.1.1
10.1.2.1
echoClient LAN 10.1.2.0
28
second.cc
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
29
second.cc
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
30
second.cc
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
31
Building and Running Your Script
Copy examples/tutorial/second.cc into the
scratch directory
cp examples/tutorial/second.cc scratch/mysecond.cc
Now build your script using waf
./waf
Run it out of the scratch directory
./waf --run scratch/mysecond
32
Output… mysecond
UDP echo client
sending a packet 10.1.1.0
Point-to-point
to the server
node(0) 10.1.1.2 node(1)
10.1.1.1 10.1.2.1
received its LAN 10.1.2.0
echoClient
echo back from
the server
33
Pcap Output
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second",csmaDevices.Get(1),true);
34
node(0)- netDevice(0)
10.1.1.0
Point-to-point
node(0) 10.1.1.2 node(1)
10.1.1.1 10.1.2.1
echoClient LAN 10.1.2.0
35
node(1)- netDevice(0)
10.1.1.0
Point-to-point
node(0) 10.1.1.2 node(1)
10.1.1.1 10.1.2.1
echoClient LAN 10.1.2.0
36
node(2)- netDevice(0)
10.1.1.0
Point-to-point
node(0) 10.1.1.2 node(1)
10.1.1.1 10.1.2.1
echoClient LAN 10.1.2.0
38