Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple NS-3 patch for GSoC 2019.
diff -uprN original/ns-3-dev/scratch/ns3-patch-example.cc modified/ns-3-dev/scratch/ns3-patch-example.cc
--- original/ns-3-dev/scratch/ns3-patch-example.cc 1970-01-01 08:00:00.000000000 +0800
+++ modified/ns-3-dev/scratch/ns3-patch-example.cc 2019-04-09 17:25:14.000000000 +0800
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Liangcheng Yu
+ *
+ */
+
+#include "ns3/core-module.h"
+#include "ns3/internet-module.h"
+#include "ns3/packet.h"
+#include "ns3/ptr.h"
+
+using namespace ns3;
+
+NS_LOG_COMPONENT_DEFINE ("Ns3PatchExample");
+
+int
+main (int argc, char *argv[])
+{
+ NS_LOG_UNCOND ("Ns3PatchExample");
+
+//==================================================================================
+/*
+ Simple example of showing how a packet goes top down and bottom up between
+ the L3Protocol and the NetDevice as required by GSoC 2019 NS-3.
+ This is just a naive example where no application, socket, addressing, routing are involved.
+*/
+//==================================================================================
+
+ // 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 hypothetical protocol
+ testNode->RegisterProtocolHandler (
+ MakeCallback (&HypotheticalL3Protocol::Receive, testProtocol),
+ HypotheticalL3Protocol::PROT_NUMBER, testNetDevice
+ );
+
+ // Create a ping-like packet to test the naive protocol
+ Ptr<Packet> p = Create<Packet> (1024); // 1024 dummy bytes of data
+
+ /*
+ Ask the HypotheticalL3Protocol to send the packet to the LoopBackNetDevice top-down
+ and automatically received the same packet bottom-up via the callback.
+ Notice that here we skip the application, socket, since we are only interested in
+ showing the interaction between L3Protocol and the NetDevice in the patch.
+ */
+ testProtocol->Send(p, testNetDevice);
+
+ Simulator::Run ();
+ // The terminal will print out the information when the send function and receive callback are triggered.
+ Simulator::Destroy ();
+}
diff -uprN original/ns-3-dev/src/internet/model/hypothetical-l3-protocol.cc modified/ns-3-dev/src/internet/model/hypothetical-l3-protocol.cc
--- original/ns-3-dev/src/internet/model/hypothetical-l3-protocol.cc 1970-01-01 08:00:00.000000000 +0800
+++ modified/ns-3-dev/src/internet/model/hypothetical-l3-protocol.cc 2019-04-09 16:52:53.000000000 +0800
@@ -0,0 +1,114 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Liangcheng Yu
+ *
+ */
+#include "ns3/packet.h"
+#include "ns3/log.h"
+#include "ns3/node.h"
+#include "ns3/net-device.h"
+#include "ns3/object-vector.h"
+#include "ns3/trace-source-accessor.h"
+#include "ns3/pointer.h"
+#include "ns3/string.h"
+
+#include "hypothetical-l3-protocol.h"
+#include "ipv4-interface.h"
+#include "ns3/traffic-control-layer.h"
+
+namespace ns3 {
+
+NS_LOG_COMPONENT_DEFINE ("HypotheticalL3Protocol");
+
+const uint16_t HypotheticalL3Protocol::PROT_NUMBER = 0x0063;
+
+NS_OBJECT_ENSURE_REGISTERED (HypotheticalL3Protocol);
+
+TypeId
+HypotheticalL3Protocol::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("ns3::HypotheticalL3Protocol")
+ .SetParent<Object> ()
+ .AddConstructor<HypotheticalL3Protocol> ()
+ .SetGroupName ("Internet")
+ ;
+ return tid;
+}
+
+HypotheticalL3Protocol::HypotheticalL3Protocol ()
+ : m_tc (0)
+{
+ NS_LOG_FUNCTION (this);
+}
+
+HypotheticalL3Protocol::~HypotheticalL3Protocol ()
+{
+ NS_LOG_FUNCTION (this);
+}
+
+void
+HypotheticalL3Protocol::SetNode (Ptr<Node> node)
+{
+ NS_LOG_FUNCTION (this << node);
+ m_node = node;
+}
+
+void
+HypotheticalL3Protocol::SetTrafficControl (Ptr<TrafficControlLayer> tc)
+{
+ NS_LOG_FUNCTION (this << tc);
+ m_tc = tc;
+}
+
+/*
+ * This method is called by AggregateObject and completes the aggregation
+ * by setting the node in the ipv4 stack
+ */
+void
+HypotheticalL3Protocol::NotifyNewAggregate ()
+{
+ NS_LOG_FUNCTION (this);
+ if (m_node == 0)
+ {
+ Ptr<Node>node = this->GetObject<Node> ();
+ //verify that it's a valid node and that
+ //the node was not set before
+ if (node != 0)
+ {
+ this->SetNode (node);
+ }
+ }
+ Object::NotifyNewAggregate ();
+}
+
+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;
+}
+
+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);
+}
+
+} // namespace ns3
diff -uprN original/ns-3-dev/src/internet/model/hypothetical-l3-protocol.h modified/ns-3-dev/src/internet/model/hypothetical-l3-protocol.h
--- original/ns-3-dev/src/internet/model/hypothetical-l3-protocol.h 1970-01-01 08:00:00.000000000 +0800
+++ modified/ns-3-dev/src/internet/model/hypothetical-l3-protocol.h 2019-04-09 15:40:28.000000000 +0800
@@ -0,0 +1,110 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 INRIA
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Liangcheng Yu
+ *
+ */
+#ifndef HYPOTHETICAL_L3_PROTOCOL_H
+#define HYPOTHETICAL_L3_PROTOCOL_H
+
+#include <list>
+#include "ns3/net-device.h"
+#include "ns3/address.h"
+#include "ns3/ptr.h"
+#include "ns3/traced-callback.h"
+
+namespace ns3 {
+
+class NetDevice;
+class Node;
+class Packet;
+class TrafficControlLayer;
+
+/**
+ * The hypothetical Layer 3 protocol.
+ */
+
+class HypotheticalL3Protocol : public Object
+{
+public:
+ /**
+ * \brief Get the type ID.
+ * \return the object TypeId
+ */
+ static TypeId GetTypeId (void);
+ static const uint16_t PROT_NUMBER; //!< protocol number (0x0063)
+
+ HypotheticalL3Protocol ();
+ virtual ~HypotheticalL3Protocol ();
+
+ /**
+ * \brief Set the node the ARP L3 protocol is associated with
+ * \param node the node
+ */
+ void SetNode (Ptr<Node> node);
+
+ /**
+ * \brief Set the TrafficControlLayer.
+ * \param tc TrafficControlLayer object
+ */
+ void SetTrafficControl (Ptr<TrafficControlLayer> tc);
+
+ /**
+ * \brief Receive a packet
+ * \param device the source NetDevice
+ * \param p the packet
+ * \param protocol the protocol
+ * \param from the source address
+ * \param to the destination address
+ * \param packetType type of packet (i.e., unicast, multicast, etc.)
+ */
+ void Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from, const Address &to,
+ NetDevice::PacketType packetType);
+
+ void Send (Ptr<Packet> p, Ptr<NetDevice> netDevice);
+
+protected:
+ /*
+ * This function will notify other components connected to the node that a new stack member is now connected
+ * This will be used to notify Layer 3 protocol of layer 4 protocol stack to connect them together.
+ */
+ virtual void NotifyNewAggregate ();
+private:
+ /**
+ * \brief Copy constructor
+ *
+ * Defined and unimplemented to avoid misuse
+ * \param o
+ */
+ HypotheticalL3Protocol (const HypotheticalL3Protocol &o);
+ /**
+ * \brief Copy constructor
+ *
+ * Defined and unimplemented to avoid misuse
+ * \param o
+ * \returns
+ */
+ HypotheticalL3Protocol &operator = (const HypotheticalL3Protocol &o);
+
+ Ptr<Node> m_node; //!< node the L3 protocol is associated with
+ Ptr<TrafficControlLayer> m_tc; //!< The associated TrafficControlLayer
+
+};
+
+} // namespace ns3
+
+#endif /* HYPOTHETICAL_L3_PROTOCOL_H */
diff -uprN original/ns-3-dev/src/internet/wscript modified/ns-3-dev/src/internet/wscript
--- original/ns-3-dev/src/internet/wscript 2019-04-09 17:43:19.000000000 +0800
+++ modified/ns-3-dev/src/internet/wscript 2019-04-09 14:21:05.000000000 +0800
@@ -111,6 +111,7 @@ def build(bld):
'model/tcp-header.cc',
'model/ipv4-interface.cc',
'model/ipv4-l3-protocol.cc',
+ 'model/hypothetical-l3-protocol.cc',
'model/ipv4-end-point.cc',
'model/udp-l4-protocol.cc',
'model/tcp-l4-protocol.cc',
@@ -314,6 +315,7 @@ def build(bld):
# used by routing
'model/ipv4-interface.h',
'model/ipv4-l3-protocol.h',
+ 'model/hypothetical-l3-protocol.h',
'model/ipv6-l3-protocol.h',
'model/ipv6-extension.h',
'model/ipv6-extension-demux.h',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.