How to Begin Implement Non Adaptive Routing in NS3
To implement the Non-Adaptive Routing suggest the routing methods in which routes are encode and static, regardless of network environment. This kind of routing does not alter according to their congestion, connection failures, or dynamic variations in network topology. It is frequently used in developments in which the network surroundings are predictable or for clarify the replication of settings.
Here’s how you can implement Non-Adaptive Routing in NS-3:
Steps to Begin Implement Non Adaptive Routing in NS3
Step 1: Understand Non-Adaptive Routing
- Key Characteristics:
- Routes are pre-computed or allocate the statically.
- During replication does not modify the routing decisions.
- Frequently has executed using the fixed routing tables.
- Common Use Cases:
- It is basic network topologies by static paths.
- Expected the congestion designs in which the adaptive routing is redundant.
Step 2: Set Up NS-3
- Install NS-3:
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Verify Installation: Run a sample script:
./ns3 run examples/tutorial/first
Step 3: Plan Non-Adaptive Routing Protocol Design
- Core Components:
- Static Routing Table: Plots the destinations for next hops and precise output devices.
- Packet Forwarding: Lookup the fixed table for forwarding decisions.
- Workflow:
- Express the fixed routing entries during initialization.
- Fixed routing table for used the packet sending.
- Assure no bring up-to-date for the routing table during replication.
Step 4: Implement Non-Adaptive Routing
Step 4.1: Define the Protocol Class
Encompass the Ipv4RoutingProtocol class:
#include “ns3/ipv4-routing-protocol.h”
#include <map>
using namespace ns3;
class NonAdaptiveRouting : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId(void);
NonAdaptiveRouting();
virtual ~NonAdaptiveRouting();
virtual Ptr<Ipv4Route> RouteOutput(Ptr<Packet> packet, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) override;
virtual bool RouteInput(Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) override;
void AddStaticRoute(Ipv4Address destination, Ipv4Address nextHop, Ptr<NetDevice> outputDevice);
private:
struct RoutingEntry {
Ipv4Address nextHop;
Ptr<NetDevice> outputDevice;
};
std::map<Ipv4Address, RoutingEntry> m_routingTable; // Destination -> RoutingEntry
};
Step 4.2: Implement Core Functions
- Add Static Routes: Populate the fixed a routing table by prearranged the routes.
void NonAdaptiveRouting::AddStaticRoute(Ipv4Address destination, Ipv4Address nextHop, Ptr<NetDevice> outputDevice) {
m_routingTable[destination] = {nextHop, outputDevice};
}
- Route Output: Use the fixed routing table for establish the route for outgoing packets.
Ptr<Ipv4Route> NonAdaptiveRouting::RouteOutput(Ptr<Packet> packet, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) {
Ptr<Ipv4Route> route = Create<Ipv4Route>();
auto it = m_routingTable.find(header.GetDestination());
if (it != m_routingTable.end()) {
route->SetDestination(header.GetDestination());
route->SetGateway(it->second.nextHop);
route->SetOutputDevice(it->second.outputDevice);
return route;
}
sockerr = Socket::ERROR_NOROUTETOHOST;
return nullptr;
}
- Route Input: Forward packets based on the static routing table.
bool NonAdaptiveRouting::RouteInput(Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) {
if (header.GetDestination() == GetNode()->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal()) {
lcb(packet, header, idev); // Deliver locally
return true;
}
auto it = m_routingTable.find(header.GetDestination());
if (it != m_routingTable.end()) {
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(header.GetDestination());
route->SetGateway(it->second.nextHop);
route->SetOutputDevice(it->second.outputDevice);
ucb(route, packet, header);
return true;
}
return false; // No route found
}
Step 5: Register the Protocol
Store the protocol by NS-3’s TypeId system:
TypeId NonAdaptiveRouting::GetTypeId(void) {
static TypeId tid = TypeId(“ns3::NonAdaptiveRouting”)
.SetParent<Ipv4RoutingProtocol>()
.SetGroupName(“Internet”)
.AddConstructor<NonAdaptiveRouting>();
return tid;
}
Step 6: Integrate into a Simulation
- Simulation Script Example:
#include “ns3/internet-stack-helper.h”
#include “ns3/non-adaptive-routing.h”
int main(int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create(4);
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“10Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices = p2p.Install(nodes.Get(0), nodes.Get(1));
devices.Add(p2p.Install(nodes.Get(1), nodes.Get(2)));
devices.Add(p2p.Install(nodes.Get(2), nodes.Get(3)));
InternetStackHelper stack;
Ptr<NonAdaptiveRouting> nonAdaptiveRouting = CreateObject<NonAdaptiveRouting>();
// Add static routes
nonAdaptiveRouting->AddStaticRoute(Ipv4Address(“10.1.1.2”), Ipv4Address(“10.1.1.1”), devices.Get(0));
nonAdaptiveRouting->AddStaticRoute(Ipv4Address(“10.1.1.3”), Ipv4Address(“10.1.1.2”), devices.Get(1));
stack.SetRoutingHelper(nonAdaptiveRouting);
stack.Install(nodes);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Step 7: Test and Debug
- Enable Logging: Use the tool NS-3’s logging system for debugging:
export NS_LOG=”NonAdaptiveRouting=level_all|prefix_time”
./ns3 run my-simulation
- Verify Behavior:
- Test the packets are observe the pre-defined static routes.
- During the replication has follow assure the no changes for dynamic.
Step 8: Extend and Optimize
- Enhancements:
- Encourage the load balancing through divide congestion with numerous fixed routes.
- Enhance the error handling for unreachable destinations.
- Performance Testing:
- Investigation the protocol for various network topologies.
- Calculate the routing efficiency and packet delivery.
Through employing the ns3, you can simulate and measure the performance for Non-Adaptive Routing projects that were simulated and visualized the results in the above following steps. Any queries related to this project will be clarified in a different man