To implement software-Defined Networking (SDN) in ns-3 by creating a network topology where control plane is separated from the data plane, allowing a central controller to manage the network. The following steps will guide to implement the SDN-based network in ns-3 using open flow for the control plane.
Prerequisites
- ns-3 installed on your system.
- Basic understanding of ns-3, C++ programming, and SDN concepts.
Step-by-step to Implement SDN in ns-3
- Install ns-3: Ensure you have ns-3 installed. You can download it from the official website and follow the installation instructions.
wget https://www.nsnam.org/release/ns-allinone-3.xx.tar.bz2
tar -xjf ns-allinone-3.xx.tar.bz2
cd ns-allinone-3.xx
./build.py –enable-examples –enable-tests
Include Necessary Headers: Include the required headers for OpenFlow and network modules.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/mobility-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/openflow-module.h”
Create Network Topology: Create nodes for hosts, switches, and the controller.
NodeContainer hosts;
hosts.Create(2);
NodeContainer switches;
switches.Create(1);
NodeContainer controllerNode;
controllerNode.Create(1);
Set Up Point-to-Point Links: Configure point-to-point links between the nodes.
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“100Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer hostDevices;
NetDeviceContainer switchDevices;
for (uint32_t i = 0; i < hosts.GetN(); ++i) {
NetDeviceContainer link = pointToPoint.Install(NodeContainer(hosts.Get(i), switches.Get(0)));
hostDevices.Add(link.Get(0));
switchDevices.Add(link.Get(1));
}
Install Internet Stack: Install the internet stack on the host nodes.
InternetStackHelper internet;
internet.Install(hosts);
Assign IP Addresses: Assign IP addresses to the host devices.
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer hostInterfaces = address.Assign(hostDevices);
Set Up OpenFlow Switch: Configure the OpenFlow switch and connect it to the controller.
OpenFlowSwitchHelper ofSwitchHelper;
Ptr<ns3::ofi::Controller> controller = CreateObject<ns3::ofi::Controller>();
ofSwitchHelper.Install(switches.Get(0), switchDevices, controllerNode.Get(0));
Create Applications: Create and install applications on the host nodes to simulate traffic.
uint16_t port = 9;
OnOffHelperonoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(hostInterfaces.GetAddress(1), port)));
onoff.SetConstantRate(DataRate(“500kb/s”));
ApplicationContainer app = onoff.Install(hosts.Get(0));
app.Start(Seconds(1.0));
app.Stop(Seconds(10.0));
PacketSinkHelpersink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
app = sink.Install(hosts.Get(1));
app.Start(Seconds(0.0));
app.Stop(Seconds(10.0));
Run the Simulation: Finally, run the simulation and clean up.
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
Example Complete Script
Below is an example complete script for setting up a basic SDN network in ns-3:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/openflow-module.h”
using namespace ns3;
int main(int argc, char *argv[])
{
NodeContainer hosts;
hosts.Create(2);
NodeContainer switches;
switches.Create(1);
NodeContainer controllerNode;
controllerNode.Create(1);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“100Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer hostDevices;
NetDeviceContainer switchDevices;
for (uint32_t i = 0; i < hosts.GetN(); ++i) {
NetDeviceContainer link = pointToPoint.Install(NodeContainer(hosts.Get(i), switches.Get(0)));
hostDevices.Add(link.Get(0));
switchDevices.Add(link.Get(1));
}
InternetStackHelper internet;
internet.Install(hosts);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer hostInterfaces = address.Assign(hostDevices);
OpenFlowSwitchHelper ofSwitchHelper;
Ptr<ns3::ofi::Controller> controller = CreateObject<ns3::ofi::Controller>();
ofSwitchHelper.Install(switches.Get(0), switchDevices, controllerNode.Get(0));
uint16_t port = 9;
OnOffHelper onoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(hostInterfaces.GetAddress(1), port)));
onoff.SetConstantRate(DataRate(“500kb/s”));
ApplicationContainer app = onoff.Install(hosts.Get(0));
app.Start(Seconds(1.0));
app.Stop(Seconds(10.0));
PacketSinkHelper sink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
app = sink.Install(hosts.Get(1));
app.Start(Seconds(0.0));
app.Stop(Seconds(10.0));
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Running the Script
Compile and run the script using the following commands:
./waf configure –enable-examples
./waf build
./waf –run <script-name>
Finally, Software-defined Networking (SDN) in ns-3 using OpenFlow for the control plane. All types of SDN-based network in ns-3 programs are handled by us.