How to Begin Implement Flooding Routing in NS3
To execute Flooding Routing using NS3, we want to make a routing protocol in which packets are transmit to every node within the network. This mechanism is frequently leveraged in network discovery and it is a primary concept for protocols such as OSPF and DSR. Flooding routing is simple however it can cause high overhead by reason of duplicate packet transmissions. Below is a sequential procedure to execute Flooding Routing in NS3:
Steps to Begin Implement Flooding Routing in NS3
Step 1: Understand Flooding Routing Basics
- Key Characteristics:
- Broadcasting: Every single node transmits the incoming packets to every neighbor excluding the sender.
- Loop Prevention: Make use of methods such as sequence numbers, preventing rebroadcasting the similar packet.
- Use Cases: Network discovery, topology exploration, and broadcast protocols.
- Challenges:
- High overhead by reason of duplicate packets.
- Ability for broadcast storms within dense networks.
Step 2: Set Up NS3
- Install NS3:
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Verify Installation: Execute a sample script:
./ns3 run examples/tutorial/first
Step 3: Plan Flooding Routing Design
- Core Components:
- Packet Forwarding: Transmit the packets to every neighbor apart from the sender.
- Duplicate Detection: Leverage a series of number or packet ID for avoiding loops.
- Packet Handling: Lost duplicate packets and then share new ones.
- Workflow:
- Node inherits a packet.
- If the packet is new one but it isn’t seen before, then we can execute and transmit it toward neighbors.
- Else, reject the packet.
Step 4: Implement Flooding Routing
Step 4.1: Define the Protocol Class
Describe the protocol class to prolong the Ipv4RoutingProtocol class:
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/socket.h”
#include <set>
using namespace ns3;
class FloodingRouting : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId(void);
FloodingRouting();
virtual ~FloodingRouting();
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;
private:
void SendFloodPacket(Ptr<Packet> packet, const Ipv4Header &header);
bool IsDuplicatePacket(const Ipv4Header &header);
Ptr<Socket> m_socket;
std::set<std::pair<Ipv4Address, uint32_t>> m_seenPackets; // (Source Address, Sequence Number)
};
Step 4.2: Implement Core Functions
- Duplicate Detection: According to the source address and sequence number, sustain a collection of seen packets.
bool FloodingRouting::IsDuplicatePacket(const Ipv4Header &header) {
auto packetInfo = std::make_pair(header.GetSource(), header.GetIdentification());
if (m_seenPackets.find(packetInfo) != m_seenPackets.end()) {
return true; // Packet already seen
}
m_seenPackets.insert(packetInfo);
return false;
}
- Broadcasting Packets: Transmit the packets to every neighbor apart from the sender.
void FloodingRouting::SendFloodPacket(Ptr<Packet> packet, const Ipv4Header &header) {
for (uint32_t i = 0; i < GetNode()->GetNDevices(); ++i) {
Ptr<NetDevice> device = GetNode()->GetDevice(i);
if (device->GetAddress() != header.GetSource()) {
Ptr<Packet> newPacket = packet->Copy();
Ipv4Header newHeader = header;
newHeader.SetSource(GetNode()->GetObject<Ipv4>()->GetAddress(i, 0).GetLocal());
m_socket->SendTo(newPacket, 0, InetSocketAddress(Ipv4Address(“255.255.255.255”), 9999));
}
}
}
- Packet Handling: Manage the received packets and send them as they aren’t duplicates.
bool FloodingRouting::RouteInput(Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) {
if (IsDuplicatePacket(header)) {
return false; // Drop duplicate packet
}
if (header.GetDestination() == GetNode()->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal()) {
lcb(packet, header, idev); // Deliver locally
return true;
}
SendFloodPacket(packet->Copy(), header); // Flood the packet
return true;
}
Step 5: Integrate the Protocol into NS3
Step 5.1: Register the Protocol
Record the Flooding Routing protocol including TypeId system of NS3:
TypeId FloodingRouting::GetTypeId(void) {
static TypeId tid = TypeId(“ns3::FloodingRouting”)
.SetParent<Ipv4RoutingProtocol>()
.SetGroupName(“Internet”)
.AddConstructor<FloodingRouting>();
return tid;
}
Step 5.2: Add to Nodes
Incorporate the protocol to a simulation script:
#include “ns3/internet-stack-helper.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<FloodingRouting> floodingRouting = CreateObject<FloodingRouting>();
stack.SetRoutingHelper(floodingRouting);
stack.Install(nodes);
Simulator::Run();
Simulator::Destroy();
return 0;
}
Step 6: Test and Debug
- Enable Logging: For debugging, we will need to utilize NS3’s logging system:
export NS_LOG=”FloodingRouting=level_all|prefix_time”
./ns3 run my-simulation
- Validate Behavior:
- Confirm that packet transmission to every node.
- Verify for duplicate packet control.
Step 7: Extend and Optimize
- Enhancements:
- Integrate a time-to-live (TTL) field for restricting the packet transmission.
- Execute chosen flooding, minimizing the overhead.
- Performance Testing:
- Experiment with diverse node densities within large-scale networks.
- Examine the effectiveness of packet delivery to utilise FlowMonitor.
By employing NS3 environment, we completed in-depth implementations for effectively executing the Flooding Routing and we are capable of extending it further if more insights are required.
Click Here to watch our latest output video using NS3 simulator
Click Here to watch our latest projects screenshots using NS3 simulator