Patch Requirement

  • Add a new, simple, basic, L3 protocol, with the protocol number 99, which should be registered on the node.
  • Ask the L3 protocol to send a ping-like packet and receive a response in the running example.
  • No routing, addressing are needed. It merely needs to be able to send a ‘ping-like’ packet to the NetDevice, and receive a packet (destined to protocol 99) from a NetDevice. A simple print on the screen at the send and receive moment is enough to show a working implementation.

Key Elements of the Solution

  • Declare the correct protocol number in the HypotheticalL3Protocol.
const uint16_t HypotheticalL3Protocol::PROT_NUMBER = 0x0063;
  • Make a systems print at the Handler Receive for the HypotheticalL3Protocol. This callback will be registered later to bind the node and the NetDevice.
void 
HypotheticalL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
                        const Address &to, NetDevice::PacketType packetType)
{
  Ptr<Packet> packet = p->Copy ();
  std::cout << "[Simple Print] HypotheticalL3Protocol with protocol number " << HypotheticalL3Protocol::PROT_NUMBER << " received (bottom-up) a packet of size " << packet->GetSize () << std::endl;
}
  • Expose an interface for sending the packet to target NetDevice, note that this is just for patch.
void
HypotheticalL3Protocol::Send (Ptr<Packet> p, Ptr<NetDevice> netDevice)
{
  std::cout << "[Simple Print] HypotheticalL3Protocol with protocol number " << HypotheticalL3Protocol::PROT_NUMBER << " sent (top-down) a packet of size " << p->GetSize () << std::endl;
  netDevice->Send (p, Mac48Address ("00:00:00:00:00:00"), HypotheticalL3Protocol::PROT_NUMBER);
}
  • Instantiate the Node, NetDevice, HypotheticalL3Protocol and aggregate them to the Node.
// Create an example node, hypothetical L3 protocol, and NetDevice
Ptr<Node> testNode = CreateObject<Node> ();
Ptr<LoopbackNetDevice> testNetDevice  = CreateObject<LoopbackNetDevice> ();
Ptr<HypotheticalL3Protocol> testProtocol = CreateObject<HypotheticalL3Protocol> ();
// Aggregate the NetDevice to the Node
testNode->AddDevice(testNetDevice);
// Aggregate the hypothetical propocol into the Node
testNode->AggregateObject (testProtocol);
  • Register the protocol handler. This is the key of the patch. NS-3 uses extensively the callback mechanism to split module dependencies. Since we could not know a priori the protocol to handle the incoming packets received at NetDevice, we need this indirection that would allow us to flexibly specify the handler during the simulation.
testNode->RegisterProtocolHandler (
    MakeCallback (&HypotheticalL3Protocol::Receive, testProtocol), 
    HypotheticalL3Protocol::PROT_NUMBER, testNetDevice
    );
  • Ask the a hypothetical L3 protocol to directly call methods on a NetDevice (e.g., Send()) to perform operations. The LoopBackNetDevice will trigger the HypotheticalL3Protocol Handler since it is registered and the protocol number of the packet received match the registered protocol number, i.e., 99.
// Create a ping-like packet to test the naive protocol
Ptr<Packet> p = Create<Packet> (1024);  // 1024 dummy bytes of data
testProtocol->Send(p, testNetDevice);
  • Check the terminal prints and verify that the packet is sent top-down and bottom-up.
Ns3PatchExample
[Simple Print] HypotheticalL3Protocol with protocol number 99 sent (top-down) a packet of size 1024
[Simple Print] HypotheticalL3Protocol with protocol number 99 received (bottom-up) a packet of size 1024

Creating the Patch

  • We could just use the UNIX diff tool as this is a simple patch. I created a GitHub Gist to store the patch.
diff -uprN original-ns3-dir modified-ns3-dir
  • For complicated patches, the Rietveld tool could be used.