How to Begin Implement Temporary Ordered Routing in NS3
To begin implementing Temporally Ordered Routing Algorithm (TORA) within NS3, we can create a routing protocol, which offers dynamic and distributed routing for mobile ad-hoc networks (MANETs). TORA targets to reduce the overhead by sustaining several routes and leveraging localized route changes.
Below is a systematic approach on how to execute TORA in NS3:
Steps to Begin Implement TORA in NS3
Step 1: Understand TORA
- Key Concepts:
- Directed Acyclic Graph (DAG): Paths are sustained by DAG rooted at the destination.
- Three Phases:
- Route Creation: Launch routes to a destination node.
- Route Maintenance: If topology modifies then renovate routes.
- Route Erasure: Remove invalid routes once no valid paths are exist.
- Messages Used:
- QUERY (QRY): Invites routes to a destination.
- UPDATE (UPD): Replies to QRY or publicizes a route update.
- CLEAR (CLR): It helps to remove invalid paths.
Step 2: Set Up NS3 Environment
- Create a Protocol Directory:
- Make a new protocol directory in src/, for instance src/tora-routing/.
- Add Necessary Files:
- tora-routing-protocol.cc: Execution file.
- tora-routing-helper.h and tora-routing-helper.cc: Helper classes.
- tora-routing-protocol.h: Header file for TORA.
- Update Build System:
- Change the wscript within src/ with new component for updating the system.
Step 3: Design TORA Protocol
Define the Protocol Class
Execute the TORA protocol for prolonging the Ipv4RoutingProtocol class.
Header File (tora-routing-protocol.h)
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/socket.h”
#include “ns3/timer.h”
#include <map>
class ToraRoutingProtocol : public ns3::Ipv4RoutingProtocol {
public:
static ns3::TypeId GetTypeId (void);
ToraRoutingProtocol ();
virtual ~ToraRoutingProtocol ();
// Override Ipv4RoutingProtocol methods
virtual ns3::Ptr<ns3::Ipv4Route> RouteOutput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<ns3::NetDevice> oif,
ns3::Socket::SocketErrno &sockerr);
virtual bool RouteInput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<const ns3::NetDevice> idev,
ns3::UnicastForwardCallback ucb,
ns3::MulticastForwardCallback mcb,
ns3::LocalDeliverCallback lcb,
ns3::ErrorCallback ecb);
void SendQuery (ns3::Ipv4Address destination);
void SendUpdate (ns3::Ipv4Address destination, uint32_t height);
void HandleQuery (ns3::Ptr<const ns3::Packet> packet);
void HandleUpdate (ns3::Ptr<const ns3::Packet> packet);
private:
struct RouteEntry {
uint32_t height;
ns3::Ipv4Address nextHop;
};
std::map<ns3::Ipv4Address, RouteEntry> m_routingTable;
ns3::Ipv4Address m_selfAddress;
ns3::Ptr<ns3::Socket> m_socket;
};
Implement Core Functions
Route Creation
Make use of QUERY messages to launch routes.
void ToraRoutingProtocol::SendQuery (ns3::Ipv4Address destination) {
ns3::Ptr<ns3::Packet> packet = ns3::Create<ns3::Packet> ();
// Serialize QUERY message
// …
m_socket->SendTo(packet, 0, ns3::InetSocketAddress(ns3::Ipv4Address::GetBroadcast(), 9999));
}
void ToraRoutingProtocol::HandleQuery (ns3::Ptr<const ns3::Packet> packet) {
// Deserialize QUERY message
ns3::Ipv4Address destination = …; // Extract destination
if (m_routingTable.find(destination) != m_routingTable.end()) {
// Respond with UPDATE if route exists
SendUpdate(destination, m_routingTable[destination].height);
}
}
Route Maintenance
Sustain routes with the support of UPDATE messages.
void ToraRoutingProtocol::SendUpdate (ns3::Ipv4Address destination, uint32_t height) {
ns3::Ptr<ns3::Packet> packet = ns3::Create<ns3::Packet> ();
// Serialize UPDATE message
// …
m_socket->SendTo(packet, 0, ns3::InetSocketAddress(ns3::Ipv4Address::GetBroadcast(), 9999));
}
void ToraRoutingProtocol::HandleUpdate (ns3::Ptr<const ns3::Packet> packet) {
// Deserialize UPDATE message
ns3::Ipv4Address destination = …; // Extract destination
uint32_t height = …; // Extract height
// Update routing table
m_routingTable[destination].height = height;
m_routingTable[destination].nextHop = packet->GetSource();
}
Route Erasure
Remove invalid routes to apply CLEAR messages.
void ToraRoutingProtocol::ClearRoutes (ns3::Ipv4Address destination) {
m_routingTable.erase(destination);
// Broadcast CLEAR message to notify neighbors
}
Packet Forwarding
Leverage the routing table to send packets.
ns3::Ptr<ns3::Ipv4Route> ToraRoutingProtocol::RouteOutput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<ns3::NetDevice> oif,
ns3::Socket::SocketErrno &sockerr) {
auto it = m_routingTable.find(header.GetDestination());
if (it != m_routingTable.end()) {
ns3::Ptr<ns3::Ipv4Route> route = ns3::Create<ns3::Ipv4Route> ();
route->SetGateway(it->second.nextHop);
route->SetOutputDevice(oif);
return route;
}
sockerr = ns3::Socket::ERROR_NOROUTETOHOST;
return nullptr;
}
Step 4: Write a Helper Class
Make a helper class to make simpler incorporation.
#include “tora-routing-protocol.h”
class ToraRoutingHelper {
public:
void Install (ns3::NodeContainer nodes) {
for (auto it = nodes.Begin(); it != nodes.End(); ++it) {
ns3::Ptr<ToraRoutingProtocol> protocol = ns3::CreateObject<ToraRoutingProtocol>();
(*it)->AggregateObject(protocol);
}
}
};
Step 5: Write a Simulation Script
Example Simulation Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “tora-routing-helper.h”
using namespace ns3;
int main (int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create (5);
InternetStackHelper stack;
stack.Install (nodes);
ToraRoutingHelper toraHelper;
toraHelper.Install(nodes);
PointToPointHelper p2p;
p2p.SetDeviceAttribute (“DataRate”, StringValue (“10Mbps”));
p2p.SetChannelAttribute (“Delay”, StringValue (“2ms”));
p2p.Install (nodes.Get(0), nodes.Get(1));
p2p.Install (nodes.Get(1), nodes.Get(2));
// Additional links…
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 6: Compile and Run
- Build the Protocol:
./waf configure
./waf build
- Run the Simulation:
./waf –run your-script
Step 7: Analyze and Extend
- Tracing:
- Analyze the behaviour of routing to utilize AsciiTraceHelper or PcapHelper.
- Enhancements:
- Integrate support for mobility and dynamic route changes.
- Enhance the TORA protocol for energy efficiency or congestion control.
We have illustrated implementation approaches for Temporary Ordered Routing using NS3, offering comprehensive explanations to ensure conceptual understanding. Additional details will be included in future documentation