How to Implement Network Namespaces in NS2
To implement the Network namespaces in ns2 has series to follow the steps and it is not support directly executed in ns2 by the way of the concept is more related to operating system such as Linux, in which the network namespaces delivers separated network stacks for different processes. But, in NS2, we can mimic a similar concept of network isolation by logically isolating groups of nodes into diverse sub-networks or domains, in which the interaction among nodes can be restricted or controlled.
This can be attained by describing the separated groups of nodes that only interacts with nodes within their own group but clearly transferred to interact with nodes in other groups.
Here, is the implementation procedure to implement the network namespace in ns2:
Key Ideas for Simulating Network Namespaces in NS2:
- Node Grouping: Group nodes into separate namespaces (or sub-networks) in which they primarily interact within their group.
- Inter-Namespace Communication: Mimic routing or gateway nodes that handle communication among diverse namespaces.
- Traffic Isolation: Make sure that traffic among namespaces is separated, and communication is only permit via defined routes or gateways.
Steps to Simulate Network Namespaces in NS2
- Define Separate Sub-networks (Namespaces): Group nodes logically into isolate namespaces like Namespace 1, Namespace 2 and hamper interaction among them.
- Assign Agents to Nodes in Each Namespace: Organize agents (e.g., TCP/UDP) for nodes within each namespace.
- Implement Gateway or Router for Cross-Namespace Communication: describe nodes or links that behave like gateways for interaction among namespaces.
- Control Inter-Namespace Traffic: Use routing protocols or custom logic to describe on how and when traffic flows among namespaces.
Example: Simulating Two Network Namespaces in NS2
In this sample, we mimic two network namespaces in which nodes within each namespace can interact with each other. A gateway node is used to handle inter among the namespaces.
Step 1: Define Nodes and Group Them Into Separate Namespaces
# Define the simulator object
set ns [new Simulator]
# Define trace and nam files for output
set tracefile [open out.tr w]
set namfile [open out.nam w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Define a ‘finish’ procedure to end the simulation and visualize in NAM
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# Set up the topography for the simulation area
set topo [new Topography]
$topo load_flatgrid 1000 1000
# Define the wireless channel
set chan [new Channel/WirelessChannel]
# Configure wireless nodes for both namespaces (Namespace 1 and Namespace 2)
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channel $chan
# Namespace 1 nodes
set node1_ns1 [$ns node]
set node2_ns1 [$ns node]
set node3_ns1 [$ns node]
# Namespace 2 nodes
set node1_ns2 [$ns node]
set node2_ns2 [$ns node]
set node3_ns2 [$ns node]
# Set node positions for NAM visualization (Namespace 1 nodes)
$node1_ns1 set X_ 100; $node1_ns1 set Y_ 100; $node1_ns1 set Z_ 0
$node2_ns1 set X_ 200; $node2_ns1 set Y_ 200; $node2_ns1 set Z_ 0
$node3_ns1 set X_ 300; $node3_ns1 set Y_ 100; $node3_ns1 set Z_ 0
# Set node positions for NAM visualization (Namespace 2 nodes)
$node1_ns2 set X_ 700; $node1_ns2 set Y_ 700; $node1_ns2 set Z_ 0
$node2_ns2 set X_ 800; $node2_ns2 set Y_ 800; $node2_ns2 set Z_ 0
$node3_ns2 set X_ 900; $node3_ns2 set Y_ 700; $node3_ns2 set Z_ 0
# Gateway node for cross-namespace communication
set gateway [$ns node]
$gateway set X_ 500; $gateway set Y_ 500; $gateway set Z_ 0
Step 2: Assign Agents to Nodes in Each Namespace
# Attach TCP agents for communication within Namespace 1
set tcp_ns1_node1 [new Agent/TCP]
set tcp_sink_ns1_node2 [new Agent/TCPSink]
$ns attach-agent $node1_ns1 $tcp_ns1_node1
$ns attach-agent $node2_ns1 $tcp_sink_ns1_node2
$ns connect $tcp_ns1_node1 $tcp_sink_ns1_node2
# Attach UDP agents for communication within Namespace 2
set udp_ns2_node1 [new Agent/UDP]
set udp_sink_ns2_node2 [new Agent/Null]
$ns attach-agent $node1_ns2 $udp_ns2_node1
$ns attach-agent $node2_ns2 $udp_sink_ns2_node2
$ns connect $udp_ns2_node1 $udp_sink_ns2_node2
# Set up traffic (FTP) for communication in Namespace 1
set ftp_ns1 [new Application/FTP]
$ftp_ns1 attach-agent $tcp_ns1_node1
$ftp_ns1 start 1.0 ;# Start FTP traffic at 1 second
$ftp_ns1 stop 5.0 ;# Stop FTP traffic at 5 seconds
# Set up CBR (Constant Bit Rate) traffic in Namespace 2
set cbr_ns2 [new Application/Traffic/CBR]
$cbr_ns2 attach-agent $udp_ns2_node1
$cbr_ns2 set packetSize_ 512
$cbr_ns2 set interval_ 0.2
$cbr_ns2 start 2.0 ;# Start CBR traffic at 2 seconds
$cbr_ns2 stop 6.0 ;# Stop CBR traffic at 6 seconds
Step 3: Configure the Gateway for Cross-Namespace Communication
# Gateway node connects the two namespaces for cross-namespace communication
# Attach TCP agent to the gateway for traffic between Namespace 1 and 2
set tcp_ns1_to_ns2 [new Agent/TCP]
set tcp_sink_ns2_to_ns1 [new Agent/TCPSink]
$ns attach-agent $gateway $tcp_ns1_to_ns2
$ns attach-agent $node3_ns2 $tcp_sink_ns2_to_ns1
$ns connect $tcp_ns1_to_ns2 $tcp_sink_ns2_to_ns1
# Set up FTP traffic between Namespace 1 and Namespace 2 via the gateway
set ftp_ns1_to_ns2 [new Application/FTP]
$ftp_ns1_to_ns2 attach-agent $tcp_ns1_to_ns2
$ftp_ns1_to_ns2 start 3.0 ;# Start cross-namespace traffic at 3 seconds
$ftp_ns1_to_ns2 stop 7.0 ;# Stop cross-namespace traffic at 7 seconds
Step 4: Schedule the End of the Simulation
# Schedule the end of the simulation
$ns at 10.0 “finish”
# Run the simulation
$ns run
Explanation of the Script
- Namespaces (Sub-networks): Nodes are grouped into two logical namespaces (Namespace 1 and Namespace 2). These namespaces are inaccessible; meaning nodes in each namespace can interact within their namespace but needs the gateway node for cross-namespace communication.
- Agent Setup:
- In Namespace 1, we use TCP agents to mimic interaction among node1_ns1 and node2_ns1.
- In Namespace 2, we use UDP agents to mimic interaction among node1_ns2 and node2_ns2.
- Gateway: The gateway node behaves like the intermediary among the Namespace 1 and Namespace 2 that allows cross-namespace communication using a TCP connection.
- Traffic Generation: FTP traffic is used for interaction within Namespace 1 and among namespaces, though CBR traffic is used in Namespace 2.
Run the Simulation
Save the script as network_namespaces.tcl and executed it using:
ns network_namespaces.tcl
This will create a trace file (out.tr) and a NAM file (out.nam). The trace file logs the packet transmissions, and the NAM files permits to envision the namespaces and interaction.
Visualize the Simulation in NAM
To envision the simulation in NAM, use the following command:
nam out.nam
In NAM, we will observe nodes interacting within their respective namespaces and via the gateway for cross-namespace traffic.
Advanced Features for Simulating Network Namespaces
- Dynamic Routing: Use dynamic routing protocols like AODV to mimic routing within and among namespaces.
- Multiple Gateways: Add more gateways to permit more complex routing and interaction among namespaces.
- Mobility: Establish node mobility in one or both namespaces to mimic dynamic network environments.
Example:
$ns at 0.0 “$node1_ns1 setdest 400 400 5.0”
$ns at 0.0 “$node1_ns2 setdest 600 600 10.0”
- Traffic Control: Execute custom logic to control or throttle cross-namespace traffic using routing rules or policies.
- Hierarchical Namespace Simulation: To mimic hierarchical namespaces, in which certain nodes behaves like routers or coordinators for particular groups of namespaces.
In the entire page was successfully demonstrated the complete procedure to implement the Network namespaces that was executed using ns2 simulation tool. We plan to deliver more information regarding the Network namespaces. If you need assistance don’t hesitate to visit ns2project.com we help you with implementing Network Namespaces in the NS2 tool. We offer guidance to ensure you achieve excellent results.