Department of Electrical Engineering Sharif University of Technology Data Networks Dr. M. R. Pakravan 1 Experimenting Network Layer with NS3 Goals 1) Become familiar with Network Simulator 3 2) Simulate routing algorithm in fixed wired network 3) Simulate routing algorithms for Ad hoc networks Introduction The network layer is concerned with getting packets from the source all the way to the destination. Getting to the destination may require making many hops at intermediate routers along the way. This function clearly contrasts with that of the data link layer, which has the more modest goal of just moving frames from one end of a wire to the other. To achieve its goals, the network layer must know about the topology of the communication subnet (i.e., the set of all routers) and choose appropriate paths through it. It must also take care to choose routes to avoid overloading some of the communication lines and routers while leaving others idle. It is up to the network layer to deal with them. A router in the network needs to be able to look at a packet’s destination address and then determine the output port which is the best choice to get the packet to that destination. The router makes this decision by consulting a forwarding table. Routing algorithms are required to build the routing tables and hence forwarding tables. The basic problem of routing is to find the lowest-cost path between any two nodes, where the cost of a path equals the sum of the costs of all the edges that make up the path. Routing is achieved in most practical networks by running routing protocols among the nodes. The protocols provide a distributed, dynamic way to solve the problem of finding the lowest-cost path in the presence of link and node failures, and changing edge costs. In this exercise you will see some NS3 features to simulate routing.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Department of Electrical Engineering Sharif University of Technology
Data Networks
Dr. M. R. Pakravan
1
Experimenting Network Layer
with NS3
Goals
1) Become familiar with Network Simulator 3
2) Simulate routing algorithm in fixed wired network
3) Simulate routing algorithms for Ad hoc networks
Introduction
The network layer is concerned with getting packets from the source all the way to the destination.
Getting to the destination may require making many hops at intermediate routers along the way. This
function clearly contrasts with that of the data link layer, which has the more modest goal of just moving
frames from one end of a wire to the other.
To achieve its goals, the network layer must know about the topology of the communication subnet (i.e.,
the set of all routers) and choose appropriate paths through it. It must also take care to choose routes to
avoid overloading some of the communication lines and routers while leaving others idle. It is up to the
network layer to deal with them.
A router in the network needs to be able to look at a packet’s destination address and then determine
the output port which is the best choice to get the packet to that destination. The router makes this
decision by consulting a forwarding table. Routing algorithms are required to build the routing tables and
hence forwarding tables.
The basic problem of routing is to find the lowest-cost path between any two nodes, where the cost of a
path equals the sum of the costs of all the edges that make up the path.
Routing is achieved in most practical networks by running routing protocols among the nodes. The
protocols provide a distributed, dynamic way to solve the problem of finding the lowest-cost path in the
presence of link and node failures, and changing edge costs. In this exercise you will see some NS3
features to simulate routing.
Department of Electrical Engineering Sharif University of Technology
Data Networks
Dr. M. R. Pakravan
2
I) Routing in Fixed wired Networks
In this part we want to run a simulation to investigate the routing algorithm in the following topology. The
topology is the same as the example, the sink tree of which is calculated in the class based on Dijkstra’s
shortest path algorithm.
8
9
A
E B
CD
I H
G
F
J
First thing you should know that NS3 code is in C++ syntax. In fact NS3 is a library for C++ .It consists of
several modules for different parts of network simulation. When you want to do a simulation you include
the modules you need and then you write your code.
For this part add these libraries:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/netanim-module.h"
#include "ns3/applications-module.h"
#include "ns3/animation-interface.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/ipv4-static-routing-helper.h"
Department of Electrical Engineering Sharif University of Technology
Data Networks
Dr. M. R. Pakravan
3
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/flow-monitor-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
Next line will show that this project is implemented in a C++ namespace called ns3. “using” statement
introduces the ns-3 namespace into the current (global) declarative region. This is a fancy way of
saying that after this declaration, you will not have to type ns3:: scope resolution operator before all of
the ns-3 code in order to use it.
using namespace ns3;
The next line enables you in showing some messages while running the code:
NS_LOG_COMPONENT_DEFINE ("Lab3_part1");
Now you can start the main function of your program.
int main (int argc, char *argv[]) { //add your codes here }
First we declare some parameters for our simulation. You should write all other codes on your main
“CommandLine” object allows the user to override any of the defaults at run-time:
Department of Electrical Engineering Sharif University of Technology
Data Networks
Dr. M. R. Pakravan
4
CommandLine cmd; cmd.AddValue ("PacketSize", "size of application packet sent", PacketSize); cmd.AddValue ("DataRate", "rate of pacekts sent", DataRate); cmd.AddValue ("tracing", "turn on ascii and pcap tracing", tracing); cmd.Parse (argc, argv);
Now you should configure an on/off application to generate data. Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue(PacketSize)); Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (DataRate)); Config::SetDefault ( "ns3::Ipv4GlobalRouting::RespondToInterfaceEvents",BooleanValue(true));
Now enable Packet Meta data for animator and the name of file for animation output:
ns3::PacketMetadata::Enable();
std::string animFile = "lab3_part1.xml" ;
Now let’s create the network topology. Network nodes are stored in a container class called
NodeContainer.
NodeContainer nodes; nodes.Create (num_Nodes);
Now you should group nodes to make links between them.