Intelligent Wireless Network Group
Department of Computer Engineering
Faculty of Engineering, Kasetsart University
http://iwing.cpe.ku.ac.th
ns-3 Tutorial (Part III)
Hands-On:
Point-to-point and CSMA - Ethernet
WLAN 2013
Exploring the directories
cd examples/tutorial
2
Topology
Point-to-point
Simple Example: first.cc
3
Topology in ns-3 (first.cc)
Application Application
Application Application
Sockets-like
API
Packet(s)
Protocol Protocol
stack stack
Node Node
NetDevice Channel NetDevice
Ethernet NIC Point-to-Point Channel Ethernet NIC
4
vi examples/tutorial/first.cc
emacs mode line: formatting conventions (coding style)
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
GNU General Public License
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
5
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");
6
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);
7
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
8
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
9
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"));
10
first.cc
create the NetDevice objects
NetDeviceContainer devices;
Install NICs to Nodes and link Point-to-point
devices = pointToPoint.Install (nodes);
Node -0 Node -1
Point-to-point
Netdevice-0 Netdevice-0
Channel
11
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
Internet Stack Internet Stack
Point-to-point
Netdevice-0 Netdevice-0
Channel
12
first.cc
to associate the devices on our nodes with IP addresses
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0","255.255.255.0");
begin allocating IP addresses
from network 10.1.1.0 using the mask 255.255.255.0
In our case, 10.1.1.1/24 followed by 10.1.1.2/24
13
first.cc
performs the actual address assignment
Ipv4InterfaceContainer interfaces =
address.Assign (devices);
Node -0 Node -1
10.1.1.1/24 Internet Stack Internet Stack 10.1.1.2/24
Point-to-point
Netdevice-0 Netdevice-0
Channel
14
first.cc
set up a UDP echo server application
UdpEchoServerHelper echoServer (9);
Set up a UDP echo server application on one of the nodes
Require the port number as a parameter to the constructor
15
first.cc
install server application @node(1)
ApplicationContainer serverApps =
echoServer.Install (nodes.Get (1));
Node -0 Node -1
Server App P-9 echoServer
10.1.1.1/24 Internet Stack Internet Stack 10.1.1.2/24
Point-to-point
Netdevice-0 Netdevice-0
Channel
16
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
Server App P-9 echoServer
10.1.1.1/24 Internet Stack Internet Stack 10.1.1.2/24
Popint-to-point
Netdevice-0 Netdevice-0
Channel
17
first.cc
Set remote add and port
UdpEchoClientHelper echoClient (interfaces.GetAddress(1),9);
Pass parameters (to helper) to set the Remote Address and
Remote Port for echoClient to connect
Remote: 10.1.1.2 , Port 9
Set echo Client’s attributes
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue (Seconds(1.)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
18
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
echoClient Client App Server App P-9 echoServer
10.1.1.1/24 Internet Stack Internet Stack 10.1.1.2/24
Point-to-point
Netdevice-0 Netdevice-0
Channel
19
first.cc
to actually run the simulation
Simulator::Run ();
to clean up
Simulator::Destroy ();
return 0;
}
Node -0 Node -1
echoClient Client App Server App P-9 echoServer
10.1.1.1/24 Internet Stack Internet Stack 10.1.1.2/24
Point-to-point
Netdevice-0 Netdevice-0
Channel
20
Classes
Container
NodeContainer
NetDeviceContainer
Ipv4InterfaceContainer
ApplicationContainer
Helper
PointToPointHelper
InternetStackHelper
Ipv4AddressHelper
UdpEchoServerHelper
UdpEchoClientHelper
21
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
22
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
23
Logging
Adding log in the code before creating nodes
NS_LOG_INFO (“WLAN Class KU 2013”);
./waf
export NS_LOG=
./waf --run scratch/myfirst
export NS_LOG=FirstScriptExample=info
./waf --run scratch/myfirst
24
Move on …
Building a Bus Network Topology
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
node(2) node(3) node(4)
10.1.2.4
echoServer
Simple Example: second.cc
25
second.cc
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
26
second.cc
nCsma = nCsma == 0 ? 1 : nCsma;
NodeContainer p2pNodes;
p2pNodes.Create (2);
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
27
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);
28
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));
29
second.cc
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
30
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
31
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
node(2) node(3) node(4)
Sent 1024 bytes to 10.1.2.4 10.1.2.4
Received 1024 bytes from 10.1.1.1 echoServer
from the UDP
Received 1024 bytes from 10.1.2.4 echo server
32
Pcap Output
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second",csmaDevices.Get(1),true);
top level directory
second-0-0.pcap
second-1-0.pcap
second-2-0.pcap
33
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
node(2) node(3) node(4)
10.1.2.4
echoServer
tcpdump -nn -tt -r second-0-0.pcap
reading from file second-0-0.pcap, link-type PPP (PPP)
2.000000 IP 10.1.1.1.49153 > 10.1.2.4.9: UDP, length 1024
2.007602 IP 10.1.2.4.9 > 10.1.1.1.49153: UDP, length 1024
34
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
node(2) node(3) node(4)
10.1.2.4
echoServer
tcpdump -nn -tt -r second-1-0.pcap
reading from file second-1-0.pcap, link-type PPP (PPP)
2.003686 IP 10.1.1.1.49153 > 10.1.2.4.9: UDP, length 1024
2.003915 IP 10.1.2.4.9 > 10.1.1.1.49153: UDP, length 1024
35
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
node(2) node(3) node(4)
10.1.2.4
tcpdump -nn -tt -r second-2-0.pcap echoServer
reading from file second-2-0.pcap, link-type EN10MB (Ethernet)
2.003696 arp who-has 10.1.2.4 (ff:ff:ff:ff:ff:ff) tell 10.1.2.1
2.003707 arp reply 10.1.2.4 is-at 00:00:00:00:00:06
2.003801 IP 10.1.1.1.49153 > 10.1.2.4.9: UDP, length 1024
2.003811 arp who-has 10.1.2.1 (ff:ff:ff:ff:ff:ff) tell 10.1.2.4
2.003822 arp reply 10.1.2.1 is-at 00:00:00:00:00:03
2.003915 IP 10.1.2.4.9 > 10.1.1.1.49153: UDP, length 1024
36
More on second.cc
wireshark second-2-0.pcap
./waf --run “scratch/mysecond –nCsma=50”
37