Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement star protocol in ns3

To implement a new routing protocol like STAR (Source Tree Adaptive Routing) in ns3, we have to create the protocol’s core functionality, integrate it with ns3, and provides helper classes for easier usage and testing.

Here are the steps for implementing the STAR protocol in ns3.

Prerequisites

  • ns3 installation :

make sure that ns3 is installed on your computer.

  • C++ Programming :

You should have knowledge in C++.

  • Understanding of ns3 :

You should be familiar with ns3 modules and basic simulation scripts.

Steps to implement STAR in ns3

  1. Set up your environment

Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.

 

  1. Create a new STAR module

Go to the src directory in ns3 and create a new directory for your protocol.

cd ns-3.XX/src

mkdir star

cd star

mkdir model helper test

 

  1. Define STAR classes and header files

In the model directory, create header and implementation files for STAR.

star-routing-protocol.h

#ifndef STAR_ROUTING_PROTOCOL_H

#define STAR_ROUTING_PROTOCOL_H

#include “ns3/ipv4-routing-protocol.h”

#include “ns3/ipv4-address.h”

#include “ns3/timer.h”

#include “ns3/node.h”

#include “ns3/socket.h”

#include “ns3/net-device.h”

#include “ns3/packet.h”

#include “ns3/nstime.h”

namespace ns3 {

class StarRoutingProtocol : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId (void);

StarRoutingProtocol ();

virtual ~StarRoutingProtocol ();

virtual void DoDispose (void);

// Implement required virtual functions from Ipv4RoutingProtocol

virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header,

Ptr<NetDevice> oif,

Socket::SocketErrno &sockerr);

virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header,

Ptr<const NetDevice> idev,

UnicastForwardCallback ucb,

MulticastForwardCallback mcb,

LocalDeliverCallback lcb,

ErrorCallback ecb);

virtual void NotifyInterfaceUp (uint32_t interface);

virtual void NotifyInterfaceDown (uint32_t interface);

virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);

virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);

virtual void SetIpv4 (Ptr<Ipv4> ipv4);

private:

// Define internal states and methods here

Ptr<Ipv4> m_ipv4;

};

} // namespace ns3

#endif // STAR_ROUTING_PROTOCOL_H

star-routing-protocol.cc

#include “star-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/simulator.h”

#include “ns3/ipv4-header.h”

#include “ns3/ipv4-route.h”

#include “ns3/inet-socket-address.h”

#include “ns3/inet6-socket-address.h”

#include “ns3/udp-socket-factory.h”

#include “ns3/uinteger.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“StarRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (StarRoutingProtocol);

TypeId StarRoutingProtocol::GetTypeId (void) {

static TypeId tid = TypeId (“ns3::StarRoutingProtocol”)

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName (“Internet”)

.AddConstructor<StarRoutingProtocol> ();

return tid;

}

StarRoutingProtocol::StarRoutingProtocol () {

NS_LOG_FUNCTION (this);

}

StarRoutingProtocol::~StarRoutingProtocol () {

NS_LOG_FUNCTION (this);

}

void StarRoutingProtocol::DoDispose () {

NS_LOG_FUNCTION (this);

Ipv4RoutingProtocol::DoDispose ();

}

Ptr<Ipv4Route> StarRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,

Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) {

NS_LOG_FUNCTION (this << p << header << oif);

// Implement route output logic here

return Ptr<Ipv4Route> ();

}

bool StarRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header,

Ptr<const NetDevice> idev, UnicastForwardCallback ucb,

MulticastForwardCallback mcb, LocalDeliverCallback lcb,

ErrorCallback ecb) {

NS_LOG_FUNCTION (this << p << header << idev);

// Implement route input logic here

return false;

}

void StarRoutingProtocol::NotifyInterfaceUp (uint32_t interface) {

NS_LOG_FUNCTION (this << interface);

}

void StarRoutingProtocol::NotifyInterfaceDown (uint32_t interface) {

NS_LOG_FUNCTION (this << interface);

}

void StarRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {

NS_LOG_FUNCTION (this << interface << address);

}

void StarRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {

NS_LOG_FUNCTION (this << interface << address);

}

void StarRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4) {

NS_LOG_FUNCTION (this << ipv4);

m_ipv4 = ipv4;

}

} // namespace ns3

 

  1. Integrate with ns3 build system

To include your new protocol, update the wscript file in the src directory.

src/star/wscript

def build(bld):

module = bld.create_ns3_module(‘star’, [‘core’, ‘network’, ‘internet’])

module.source = [

‘model/star-routing-protocol.cc’,

]

headers = bld(features=’ns3header’)

headers.module = ‘star’

headers.source = [

‘model/star-routing-protocol.h’,

]

 

  1. Create STAR helper class

To facilitate the use and to test STAR, create helper classes.

star-helper.h

#ifndef STAR_HELPER_H

#define STAR_HELPER_H

#include “ns3/star-routing-protocol.h”

#include “ns3/node-container.h”

#include “ns3/ipv4-routing-helper.h”

namespace ns3 {

class StarHelper : public Ipv4RoutingHelper {

public:

StarHelper ();

StarHelper* Copy (void) const;

virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;

private:

};

} // namespace ns3

#endif // STAR_HELPER_H

star-helper.cc

#include “star-helper.h”

#include “ns3/star-routing-protocol.h”

#include “ns3/ipv4-routing-protocol.h”

#include “ns3/ipv4-list-routing.h”

#include “ns3/log.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“StarHelper”);

StarHelper::StarHelper () {

}

StarHelper* StarHelper::Copy (void) const {

return new StarHelper (*this);

}

Ptr<Ipv4RoutingProtocol> StarHelper::Create (Ptr<Node> node) const {

Ptr<StarRoutingProtocol> agent = CreateObject<StarRoutingProtocol> ();

node->AggregateObject (agent);

return agent;

}

} // namespace ns3

 

  1. Build and test

Rebuild ns3 to include your new STAR module.

./waf configure

./waf build

 

Create test scripts to verify the implementation of your STAR protocol.

scratch/test-star.cc

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/star-helper.h”

#include “ns3/ipv4-static-routing-helper.h”

#include “ns3/ipv4-list-routing-helper.h”

using namespace ns3;

int main (int argc, char *argv[]) {

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (2);

InternetStackHelper internet;

StarHelper star;

Ipv4ListRoutingHelper list;

list.Add (star, 10);

internet.SetRoutingHelper (list);

internet.Install (nodes);

// Add additional setup code here

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

  1. Run and debug

Run your test script and debug any issues that arise.

./waf –run scratch/test-star

Overall, we had learned on implementing the Source Tree Adaptive Routing (STAR) in ns3 by creating protocol’s core functionality, integrating it with and providing helper classes for easier usage and testing. Also, we provide more detailed information on STAR (Source Tree Adaptive Routing).

Work on STAR (Source Tree Adaptive Routing) with ns3simulation.com for best programming results.