How to Implement OSI Layer Protocol in NS2

To implement an Open Systems Interconnection (OSI) Layer protocol in NS2 has encompasses to generate or emulate a protocol that performs within one of the seven layers of the OSI model: Physical, Data Link, Network, Transport, Session, Presentation, or Application.  All of these layers help diverse functions in a networked communication system. Relaying on the needs, we might need to execute a protocol at a particular OSI layer, like a custom network-layer protocol or a transport-layer protocol.

The given below is the procedure to execute a protocol for OSI layers in NS2 that concentrates on either the Network or Transport layers, that usually mimicked in NS2.

Step-by-Step Implementation:

Step 1: Define the OSI Layer Protocol Requirements

Regulate which OSI layer we want to target and the particular behaviour of the protocol:

  • Network Layer: Manages routing, addressing, and packet forwarding like IP, OSPF.
  • Transport Layer: To handle the end-to-end communication, error handling, and flow control such as TCP, UDP.
  • Data Link Layer: Handles the interaction among two directly connected nodes that has framing, error detection, and MAC addressing like Ethernet.
  • Application Layer: To mimic the higher-level protocols such as HTTP, FTP, etc.

Step 2: Set Up NS2

Make sure NS2 is installed on the system. Validate the installation by executing a basic simulation. NS2 natively supports various protocols via numerous OSI layers, however for custom protocols; we will need to execute them yourself.

Step 3: Implement the OSI Layer Protocol in C++

Example: Implementing a Network Layer Protocol (Custom Routing Protocol)

The given below is the simple structure for deploying a Network Layer protocol, like a custom routing protocol.

#include <agent.h>

#include <packet.h>

#include <trace.h>

#include <address.h>

#include <map>

class CustomNetworkLayerAgent : public Agent {

public:

CustomNetworkLayerAgent();

void recv(Packet* p, Handler* h);

void routePacket(Packet* p);

void forwardPacket(Packet* p, int nextHop);

protected:

std::map<int, int> routingTable_; // Destination -> Next Hop

};

// Constructor

CustomNetworkLayerAgent::CustomNetworkLayerAgent() : Agent(PT_UDP) {

// Example routing table setup (Destination -> Next Hop)

routingTable_[2] = 3; // Route to destination 2 via node 3

routingTable_[3] = 2; // Route to destination 3 via node 2

}

// Packet reception

void CustomNetworkLayerAgent::recv(Packet* p, Handler* h) {

routePacket(p);

}

// Route the packet based on the destination address

void CustomNetworkLayerAgent::routePacket(Packet* p) {

hdr_ip* iph = hdr_ip::access(p);

int dest = iph->dst();

if (routingTable_.find(dest) != routingTable_.end()) {

forwardPacket(p, routingTable_[dest]);

} else {

// Drop packet if no route is found

drop(p);

}

}

// Forward packet to the next hop

void CustomNetworkLayerAgent::forwardPacket(Packet* p, int nextHop) {

// Forward the packet to the next hop

send(p, nextHop);

}

Example: Implementing a Transport Layer Protocol (Custom Transport Protocol)

The below are the simple structure for executing a Transport Layer protocol, like a custom reliable transport protocol.

#include <agent.h>

#include <packet.h>

#include <trace.h>

#include <address.h>

class CustomTransportLayerAgent : public Agent {

public:

CustomTransportLayerAgent();

void recv(Packet* p, Handler* h);

void sendAck(Packet* p);

void retransmit(Packet* p);

protected:

std::map<int, Packet*> unacknowledgedPackets_; // Sequence Number -> Packet

};

// Constructor

CustomTransportLayerAgent::CustomTransportLayerAgent() : Agent(PT_TCP) {

// Initialization code here

}

// Packet reception

void CustomTransportLayerAgent::recv(Packet* p, Handler* h) {

hdr_cmn* cmnh = hdr_cmn::access(p);

if (cmnh->ptype() == PT_ACK) {

// Process acknowledgment

int ackNum = cmnh->seqno();

unacknowledgedPackets_.erase(ackNum);

} else {

// Process data packet and send acknowledgment

sendAck(p);

}

}

// Send an acknowledgment for the received packet

void CustomTransportLayerAgent::sendAck(Packet* p) {

hdr_cmn* cmnh = hdr_cmn::access(p);

int seqNum = cmnh->seqno();

// Create and send an acknowledgment packet

Packet* ack = allocpkt();

hdr_cmn* ackHdr = hdr_cmn::access(ack);

ackHdr->ptype() = PT_ACK;

ackHdr->seqno() = seqNum;

send(ack, 0);

}

// Retransmit a packet if acknowledgment is not received

void CustomTransportLayerAgent::retransmit(Packet* p) {

hdr_cmn* cmnh = hdr_cmn::access(p);

int seqNum = cmnh->seqno();

if (unacknowledgedPackets_.find(seqNum) != unacknowledgedPackets_.end()) {

send(p->copy(), 0);

}

}

Step 4: Integrate the Protocol into NS2

  1. Modify the Makefile: Add the new CustomNetworkLayerAgent or CustomTransportLayerAgent class to the NS2 Makefile so that it acquires compiled with the rest of the simulator.
  2. Recompile NS2:

make clean

make

Step 5: Create a Tcl Script to Simulate the OSI Layer Protocol

Just the once the protocol is executed and compiled, make a Tcl script to mimic a network using the custom protocol.

Example Tcl Script for Network Layer Protocol:

# Create a simulator object

set ns [new Simulator]

# Define the topology

set val(chan)   Channel/WiredChannel

set val(ll)     LL

set val(ifq)    Queue/DropTail/PriQueue

set val(ifqlen) 50

set val(stop)   50.0

# Initialize the topology object

set topo [new Topography]

$topo load_flatgrid 500 500

# Create nodes representing routers and hosts

set router1 [$ns node]

set router2 [$ns node]

set host1 [$ns node]

set host2 [$ns node]

# Establish links between routers and hosts

$ns duplex-link $router1 $router2 10Mb 10ms DropTail

$ns duplex-link $router1 $host1 100Mb 1ms DropTail

$ns duplex-link $router2 $host2 100Mb 1ms DropTail

# Attach Network Layer agents to routers

set nlayer1 [new Agent/CustomNetworkLayerAgent]

set nlayer2 [new Agent/CustomNetworkLayerAgent]

$ns attach-agent $router1 $nlayer1

$ns attach-agent $router2 $nlayer2

# Set up traffic sources and sinks

set udp [new Agent/UDP]

set null [new Agent/Null]

$ns attach-agent $host1 $udp

$ns attach-agent $host2 $null

$ns connect $udp $null

# Start the simulation

$ns at 0.0 “$udp start”

$ns at $val(stop) “stop”

$ns at $val(stop) “exit 0”

proc stop {} {

global ns

$ns flush-trace

exit 0

}

# Run the simulation

$ns run

Step 6: Run the Simulation

  1. Save the Tcl script (osi_layer_protocol.tcl).
  2. Open a terminal and navigate to the directory in which we saved the Tcl script.
  3. Execute the simulation using the following command:

ns osi_layer_protocol.tcl

Step 7: Analyse the Results

  • Use trace files and the network animator (NAM) to evaluate the performance of OSI Layer Protocol that concentrates on parameters like packet delivery ratio, delay, and routing or transport efficiency.
  • Assess on how efficiently the protocol performs within its selected OSI layer and communicates with other layers.

Additional Considerations

  • Layer Interactions: To deliberate on how the protocol communicates with protocols at other OSI layers, particularly in more complex simulations.
  • Scalability: Validate the protocol in larger networks with more nodes and traffic to assess the scalability and robustness.
  • Advanced Features: Execute an advanced features such as error correction, congestion control (Transport Layer), or more complex routing techniques (Network Layer).

Here, we clearly demonstrate how to set up a basic Open Systems Interconnection and implement a simple protocol for enhance the networked communication. We plan to provide the additional information on how to implement the OSI layer in other simulation settings.

Are you looking for the best OSI Layer Protocol in NS2, we can help you with implementation results. Feel free to reach out to us for timely delivery.