Professional Documents
Culture Documents
2011
Ns-3 Tutorial
Stein Kristiansen (steikr@ifi.uio.no)
09.03.2010
Ns-3 Overview
Written in C++
Well documented
Manual, tutorial, Doxygen and examples
Examples for wireless networks: ns-3.10/examples/wireless
Outline
Ns-3 Overview
Installation
Ns-3 Objects
Scripting Step-by-Step
Resources
Installing ns-3
$ wget http://www.nsnam.org/releases/ns-allinone-3.10.tar.bz2
$ tar xjf ns-allinone-3.10.tar.bz2
$ cd ns-allinone-3.10
$ ./build
$ cd ns-3-dev
$ ./waf --run first
$ wget http://www.nsnam.org/releases/ns-allinone-3.10.tar.bz2
$ tar xjf ns-allinone-3.10.tar.bz2
$ cd ns-allinone-3.10/ns-3.10
$ ./configure d debug
$ ./waf --run first
Example Scripts
Ns-3 Objects
$ cp r manet NS3FOLDER/scratch
$ cd NS3FOLDER
$ ./waf --run manet --numnodes=16 --cols=4 --spacing=100
1.
2.
3.
09.03.2011
Ns-3 Objects
Smart-Pointers
Properties
Can be aggregated
Object Aggregation
TypeId GetTypeId(void)
Object can be aggregated to access each other, and for the user to easily access
individual objects in an aggregation
Object aggregation:
TypeId
RoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::olsr::RoutingProtocol")
.SetParent<Ipv4RoutingProtocol> ()
.AddConstructor<RoutingProtocol> ()
.AddAttribute ("HelloInterval", "HELLO messages emission interval.",
TimeValue (Seconds (2)),
MakeTimeAccessor (&RoutingProtocol::m_helloInterval),
MakeTimeChecker ())
Attributes
node
mobility
node
mobility
node->AggregateObject (mobility);
return tid;
09.03.2011
cmd.Parse (argc,argv);
uint32_t numNodes = rows * cols;
NodeContainer nodes;
nodes.Create (numNodes);
nodes
numNodes - 1
09.03.2011
Antenna
Set capturing format (explained later)
Applications
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.Set ("RxGain", DoubleValue (-10) );
wifiPhy.SetPcapDataLinkType(YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
Protocols
Antenna and NIC
Step 4: Channel
Use of helpers to
configure the channel
Can also use e.g.,
ns3::TwoRayGroundPropagationLossModel
wifiMac.SetType ("ns3::AdhocWifiMac");
MAC
YansWifiChannelHelper wifiChannel ;
wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
wifiPhy.SetChannel (wifiChannel.Create ());
nodes
MAC
MAC
MAC
MAC
X = node,Y = device
$ tcpdump r MANET-X-Y.pcap
09.03.2011
OlsrHelper olsr;
InternetStackHelper internet;
internet.SetRoutingHelper
(olsr);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
nodes
OLSR
UDP
ARP
TCP
IP
ICMP
internet.Install (nodes);
nodeSpacing
Cols
nodeSpacing
Rows *
Cols
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (nodeSpacing),
"DeltaY", DoubleValue (nodeSpacing),
"GridWidth", UintegerValue (cols));
09.03.2011
Several alternatives
Random walk:
Default: Uniform:2:4
Y
Uniform:2:0
MobilityHelper mobility;
// Set positions
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (0, 500, 0, 500)),
"Speed", StringValue(Uniform:2:0"),
Distance, DoubleValue(30));
mobility.Install(nodes);
E.g., for the routing protocol, whenever there is a change in the route table
E.g., for the routing protocol, whenever there is a change in the route table
olsr-routing-protocol.cc
TypeId
RoutingProtocol::GetTypeId (void)
{
.AddTraceSource ("RoutingTableChanged", "The OLSR routing table has changed.",
MakeTraceSourceAccessor (&RoutingProtocol::m_routingTableChanged))
;
return tid;
}
E.g., for the routing protocol, whenever there is a change in the route table
olsr-routing-protocol.h
simulation-script.cc
void RouteChange(std::string source, uint32_t size) {
std::cout << "Routechange at " << source << ", new size: " << size << std::endl;
}
int main(int argc, char *argv[]) {
Config::Connect("/NodeList/*/$ns3::olsr::RoutingProtocol/RoutingTableChanged",
MakeCallback (&RouteChange));
sample output:
routingTableObject->TraceConnect(RoutingTableChanged,
MakeCallback(&RouteChange));
09.03.2011
return 0;
} // End of main()
Thats All
#include visualizer-trace-helper.h
int main(int argc, char *argv[]) {
Resources
Wiki
Manual
Tutorials
Presentations
Doxygen
Questions?
Good luck with the assignments!
Ns-3 examples
<ns3 folder>/examples
<ns3 folder>/samples
chain and manet
Appendix
Use Ipv4StaticRoutingHelper
Static routes
User defined locations
Constant positions
The Ns-3 logging facility
Ipv4StaticRoutingHelper staticRouting;
InternetStackHelper internet;
internet.SetRoutingHelper(staticRouting);
internet.Install (nodes);
09.03.2011
Use Ipv4StaticRoutingHelper
// Set addresses
SetStaticRoute(nodes.Get(0), "10.0.0.3", "10.0.0.2", 1);
SetStaticRoute(nodes.Get(0), "10.0.0.2", "10.0.0.2 ,1);
SetStaticRoute(nodes.Get(1), "10.0.0.1", "10.0.0.1", 1);
SetStaticRoute(nodes.Get(1), "10.0.0.3", "10.0.0.3", 1);
SetStaticRoute(nodes.Get(2), "10.0.0.1", "10.0.0.2", 1);
SetStaticRoute(nodes.Get(2), "10.0.0.2", "10.0.0.2", 1);
void SetStaticRoute(Ptr<Node> n, const char* destination, const char* nextHop, uint32_t interface)
{
Ipv4StaticRoutingHelper staticRouting;
Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
Ptr<Ipv4StaticRouting> a = staticRouting.GetStaticRouting (ipv4);
a->AddHostRouteTo (Ipv4Address (destination), Ipv4Address (nextHop), interface);
}
10.0.0.1
10.0.0.2
10.0.0.3
// Set addresses
// Set addresses
10.0.0.1
10.0.0.2
10.0.0.3
[0,0,0]
[nodeSpacing,0,0]
10.0.0.1
10.0.0.2
10.0.0.3
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
positionAlloc->Add(Vector(0.0, nodeSpacing, 0.0));
mobility.SetPositionAllocator(positionAlloc);
MobilityHelper mobility;
// Set positions
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);