NS3 Lab1 Manual Task
NS3 Lab1 Manual Task
(NS3-I)
Sanjay K. Sahay
Dept. of CSIS, BITS Pilani, K.K. Birla Goa Campus
0.1 Theory
The Network Simulator 3 (NS-3) is a powerful, open-source discrete-event network simulator
widely used in academic research and teaching. It enables the design, configuration, and anal-
ysis of complex network scenarios without the need for real hardware deployment. Whether
you are experimenting with novel protocols, evaluating performance in large-scale topologies,
or studying how diverse network layers interact, NS-3 provides a flexible and highly customiz-
able environment.
Why NS-3?
• Realistic Models: NS-3 offers detailed implementations of protocols across layers (Eth-
ernet, Wi-Fi, TCP, UDP, etc.) with configurable parameters like bandwidth, latency, and
error rates. This realism helps generate results that closely approximate real-world be-
haviors.
• Rich Library of Modules: From point-to-point links to sophisticated wireless and mo-
bility models, NS-3’s modular design lets you combine or extend components (e.g., LTE,
Wi-Fi, 5G).
• Scalability and Efficiency: While some simulators struggle with large numbers of nodes,
NS-3 is optimized to handle thousands of nodes in a single experiment, if hardware re-
sources permit.
• Educational Value: Students learning networking concepts benefit from building and
visualizing scenario scripts, tweaking parameters (e.g., queue lengths, routing protocols),
and seeing how changes affect throughput, latency, or packet loss.
1
• Safe Environment for Novel Ideas: If you are designing new algorithms or custom pro-
tocols, testing them in a simulator is safer and faster than deploying them on production
networks.
• Detailed Statistics and Tracing: NS-3’s tracing framework yields packet-level logs,
queue-depth histories, and event timestamps. This granularity allows thorough analysis
of network metrics and fine-grained debugging of any issues.
By using NS-3, you will learn how to construct network topologies in code, configure link
properties, and run simulations that produce consistent, repeatable results. The lab sessions
that follow demonstrate how to create simple scenarios, gather trace data, and interpret metrics
like throughput, latency, and packet loss. This understanding will prove invaluable for both aca-
demic research and real-world network evaluations, as it builds a foundation for more complex
simulations and advanced performance analysis.
1. Installing NS-3
• Check the home directory, a file ns-allinone-3.36.1.tar.bz2 will be available, else down-
load ns-allinone-3.36.1.tar.bz2 from local quanta.
This process takes some time depends on the speed of your system.
2
• Now go to the directory
Hello Simulator
(You will get this output)
• To run the examples, copy the examples/tutorial/first.cc, etc. to the scratch folder and
execute the file as shown below
3
• Switches, Routers: If you’re simulating an Ethernet or CSMA-based LAN, you might
create a node with multiple NetDevices bridging them to emulate a switch. Routers
are basically nodes with forwarding enabled, IP addresses set on each interface, and
routing tables configured.
4. Visualization Options
• NetAnim (Animation Tool): An external Qt-based utility which reads .xml trace files
generated by NS-3 scripts. It can animate nodes moving around (in wireless scenarios),
show packet transmissions, etc (See the directory NetAnim)
• A console-based log or summary after the simulation ends, e.g., "Sent 1024 bytes
at t=2.0s".
• Optionally, .pcap files (one per node’s NetDevice) that can be opened in Wireshark to
see the “wire-level” packet traces, even though it’s all virtual in the simulator.
• If you incorporate NetAnim, you open a .xml file in the NetAnim GUI, which shows
a 2D layout of nodes and animations of packets. This is quite basic but sufficient to
illustrate movement in a wireless scenario or packet flow in a small topology.
• Single P2P Link: Two nodes, each assigned an IP on a /24 network, exchange UDP or
TCP traffic. Compare throughput under different link speeds or delay configurations.
• Small LAN: Four or five nodes connected over a CSMA channel, run a UdpEcho or
OnOff application to see how collisions or queue parameters might impact performance.
• Trace Collection: Enable pcap output or ASCII tracing to capture all packets. Inspect
the logs or open them in Wireshark to confirm addresses, protocols, and timing.
4
Interpreting Results
• Typically, you gather stats (throughput, packet drops, latency) from a built-in NS-3 fea-
ture like FlowMonitor.
• Alternatively, parse log messages or event hooks that you add to the scenario code.
• Summarize your findings: e.g., "When link is 10Mbps with 10ms delay,
TCP throughput was 8Mbps. With 5ms delay, we got 9.2Mbps."
2. Three Nodes on a Switch: Illustrate ARP traffic and simple LAN communication.
3. Two LANs Interconnected by a Router: Show how basic IP forwarding occurs between
two subnets.
All scripts assume a working NS-3 environment (see Section 0.1.1). Place these scripts in
your NS-3 directory (e.g., scratch/), build them, and then run them with your chosen build
system (waf or meson/ninja).
// Create 2 nodes
NodeContainer nodes;
5
nodes.Create(2);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
6
• Run:
Observing Results
• Delay and Throughput Tuning: Change the link DataRate or Delay in the script to see
how it impacts packet travel time or overall throughput.
Topology Sketch
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
7
// NodeContainer for LAN2
NodeContainer lan2Nodes;
lan2Nodes.Create(4); // 4 end-hosts
8
Ptr<Ipv4> routerIpv4 = router->GetObject<Ipv4>();
routerIpv4->SetAttribute("IpForward", BooleanValue(true));
// UdpEchoServer on serverNode
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(serverNode);
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(20.0));
// UdpEchoClient on clientNode,
// targeting serverNode’s IP in 10.1.1.x
// find its assigned interface
Ipv4Address serverAddr = lan1Interfaces.GetAddress(0);
UdpEchoClientHelper echoClient(serverAddr, 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(5));
echoClient.SetAttribute("Interval", TimeValue(Seconds(2.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(512));
// Enable pcap
csma1.EnablePcapAll("two-lans-router-lan1", false);
csma2.EnablePcapAll("two-lans-router-lan2", false);
// Run simulation
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
9
it can route from 10.1.2.0/24 to 10.1.1.0/24.
• ARP Across Subnets: Hosts in LAN1 only ARP for the router interface’s IP within
the same subnet. Meanwhile, the client in LAN2 will ARP for 10.1.2.x gateway
(router interface). The router does the route lookup to forward to 10.1.1.x on the
other interface.
• PCAP Files: You’ll see separate captures for each LAN (e.g., two-lans-router-lan1
-2-0.pcap for router on LAN1, two-lans-router-lan2-5-0.pcap for router
on LAN2, etc.). Examining these in Wireshark reveals how the router receives packets
on one interface and forwards them out the other.
• Potential Enhancements: - Add more nodes on each side. - Introduce different DataRates
or Delays on each LAN. - Use a TCP application instead of UDP to observe handshake
and congestion control.
• Scenario 1 clarifies basic point-to-point usage, addressing, and simple UDP echo traffic.
• Scenario 3 covers multi-subnet routing, letting you see how IP forwarding occurs in the
router node. This concept extends to building more elaborate topologies or connecting
additional routers and LANs.
In all cases, analyzing the generated pcap files with Wireshark or reviewing NS-3 console
logs/traces can reveal extensive details about packet flows, protocol behavior, and timing. By
tweaking parameters (link speeds, delays, application rates), you can see immediate changes
in throughput, latency, and ARP or routing overhead. This iterative experimentation is at the
heart of using NS-3 for networking research and education.
Objective: Familiarize yourself with a minimal NS-3 setup by creating two nodes con-
nected by a PointToPoint link. Use UdpEcho applications to confirm successful
packet exchange, and collect basic timing/throughput data.
10
Tasks:
• Create a C++ script (p2p-two-nodes.cc) to instantiate exactly 2 nodes.
• Use PointToPointHelper with attributes like:
– DataRate = "5Mbps"
– Delay = "2ms"
• Assign IP addresses (e.g., 10.1.1.0/24).
• Install a UdpEchoServer on Node B, listening on a designated port (e.g., 9).
• Install a UdpEchoClient on Node A, sending 3 packets of 512 bytes each at
intervals of 2 seconds.
• Enable pcap tracing, run the simulation for 10 seconds, and stop.
What to Observe:
• Console logs indicating packet transmissions and receptions (e.g., “Sent 512 bytes”).
• A .pcap file named (for instance) p2p-two-nodes-0-0.pcap containing
ARP and UDP packets, which you can inspect in Wireshark.
• How changing the DataRate or Delay affects packet arrival time or throughput.
Expected Results:
• Node A should successfully send and receive the echoed packets from Node B.
• ARP resolution (e.g., “Who has 10.1.1.2?”) in the pcap before UDP flow starts.
• Potential minimal packet loss if you deliberately lower the link rate or increase the
packet size beyond the capacity (depending on buffer settings).
2. Three-Node LAN via CSMA and ARP Demonstration
Objective: Illustrate how multiple nodes share a broadcast domain (like a switch), fo-
cusing on ARP requests and potential collisions. Use UdpEcho between two nodes
while a third node remains idle (or also sends/receives data).
Tasks:
• Create a C++ script (three-csma-arp.cc) with three nodes in a CsmaHelper
network:
– DataRate = "100Mbps"
– Delay = "2ms"
• Install UdpEchoServer on Node 2 (port 9). Install UdpEchoClient on Node
0 (sending 5 packets at 1 s intervals).
• Leave Node 1 idle or optionally install another UdpEchoClient for additional
traffic.
• Capture traffic with csma.EnablePcapAll("three-csma-arp") for Wire-
shark analysis.
11
What to Observe:
• ARP broadcast frames: Node 0 asking “Who has 10.1.2.3?” for Node 2’s IP.
• Node 1 seeing these broadcasts even if it’s not actively transmitting.
• Throughput or delay differences if you reduce DataRate or adjust packet sizes.
Expected Results:
• Confirmation that ARP resolves the MAC addresses within the LAN.
• UdpEcho traffic flows from Node 0 to Node 2 once ARP completes.
• Possibly see collisions or packet reorder if the CsmaHelper is configured with
certain attributes (like a lower DataRate and multiple active senders).
Objective: Demonstrate basic IP routing across two subnets. Each LAN has multiple
nodes, with a single router node bridging them. Observe how traffic from one LAN can
reach a server on the other LAN via simple IP forwarding.
Tasks:
What to Observe:
• ARP interactions within each LAN: Nodes discovering the router’s MAC, the router
discovering the end-host MAC, etc.
• IP-level forwarding inside the router: Packets entering interface on 10.1.2.x,
being routed, then exiting interface on 10.1.1.x.
• Potential use of Ipv4GlobalRoutingHelper or static routes if you want ad-
vanced routing. Typically, IP forwarding is enough for small subnets.
12
Expected Results:
• Successful echo traffic between LAN1 and LAN2 hosts, verifying that the router is
indeed forwarding.
• Additional console logs or UdpEcho messages showing how many bytes were sent,
received, and any timing details.
• If you block or disable IP forwarding, traffic fails to cross subnets, illustrating the
necessity of a properly configured router.
Objective: Deepen your understanding of how adjusting link rates, delays, and queue
sizes influences throughput, latency, and packet loss in any of the above scenarios.
Tasks:
What to Observe:
• For each link rate/delay combination, record the measured throughput, end-to-end
latency, and any drops if applicable.
• Identify the “breaking point” where if the data rate is too low or the delay is too
high, the application experiences poor performance (higher round-trip time, possi-
ble timeouts).
• If you enable an ErrorModel, watch the pcap for any packets flagged as cor-
rupted or missing from the flow sequence.
Expected Results:
13
By performing these four experiments, you’ll progress from a simple two-node point-to-
point test to multi-node LAN setups, then to multi-subnet routing scenarios and more advanced
parameter tweaking. Throughout each experiment, the key is to:
• Write (or modify) a straightforward C++ script defining nodes, channels, and applica-
tions.
• Analyze the traffic in Wireshark or console output to verify correct addressing, ARP
resolution, and IP routing.
This comprehensive practice will build essential skills for future NS-3 labs, research projects,
or advanced experimentation, providing a robust foundation in discrete-event network simula-
tion.
—xxx—
14