A Discrete-Event Network Simulator
API
main.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 University of Campinas (Unicamp)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
18  */
19 
20 /*
21  * - Two hosts connected to different OpenFlow switches.
22  * - Both switches are managed by the tunnel controller application.
23  * - The ports interconnecting the switches are configured as logical
24  * ports, allowing switches to de/encapsulate IP traffic using the GTP/UDP/IP
25  * tunneling protocol.
26  *
27  * Tunnel Controller
28  * |
29  * +---------------+
30  * | |
31  * +----------+ +----------+
32  * Host 0 === | Switch 0 | OOOOO | Switch 1 | === Host 1
33  * +----------+ +----------+
34  */
35 
36 #include "gtp-tunnel-app.h"
37 #include "tunnel-controller.h"
38 
39 #include <ns3/applications-module.h>
40 #include <ns3/core-module.h>
41 #include <ns3/csma-module.h>
42 #include <ns3/internet-apps-module.h>
43 #include <ns3/internet-module.h>
44 #include <ns3/network-module.h>
45 #include <ns3/ofswitch13-module.h>
46 
47 using namespace ns3;
48 
49 int
50 main(int argc, char* argv[])
51 {
52  uint16_t simTime = 10;
53  bool verbose = false;
54  bool trace = false;
55 
56  // Configure command line parameters
58  cmd.AddValue("simTime", "Simulation time (seconds)", simTime);
59  cmd.AddValue("verbose", "Enable verbose output", verbose);
60  cmd.AddValue("trace", "Enable datapath stats and pcap traces", trace);
61  cmd.Parse(argc, argv);
62 
63  if (verbose)
64  {
66  LogComponentEnable("OFSwitch13Interface", LOG_LEVEL_ALL);
67  LogComponentEnable("OFSwitch13Device", LOG_LEVEL_ALL);
68  LogComponentEnable("OFSwitch13Port", LOG_LEVEL_ALL);
69  LogComponentEnable("OFSwitch13Queue", LOG_LEVEL_ALL);
70  LogComponentEnable("OFSwitch13SocketHandler", LOG_LEVEL_ALL);
71  LogComponentEnable("OFSwitch13Controller", LOG_LEVEL_ALL);
72  LogComponentEnable("OFSwitch13Helper", LOG_LEVEL_ALL);
73  LogComponentEnable("OFSwitch13InternalHelper", LOG_LEVEL_ALL);
74  LogComponentEnable("TunnelController", LOG_LEVEL_ALL);
75  LogComponentEnable("GtpTunnelApp", LOG_LEVEL_ALL);
76  }
77 
78  // Enable checksum computations (required by OFSwitch13 module)
79  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
80 
81  // Create two host nodes
82  NodeContainer hosts;
83  hosts.Create(2);
84 
85  // Install the TCP/IP stack into hosts nodes
86  InternetStackHelper internet;
87  internet.Install(hosts);
88 
89  // Create two switch nodes
90  NodeContainer switches;
91  switches.Create(2);
92 
93  // Create the controller node
94  Ptr<Node> controllerNode = CreateObject<Node>();
95 
96  // Configure the OpenFlow network domain (don't create the OpenFlow channel now)
97  Ptr<OFSwitch13InternalHelper> of13Helper = CreateObject<OFSwitch13InternalHelper>();
98  Ptr<TunnelController> tunnelController = CreateObject<TunnelController>();
99  of13Helper->InstallController(controllerNode, tunnelController);
100  OFSwitch13DeviceContainer ofDevices = of13Helper->InstallSwitch(switches);
101  Ptr<OFSwitch13Device> sw0 = ofDevices.Get(0);
102  Ptr<OFSwitch13Device> sw1 = ofDevices.Get(1);
103 
104  // Use the CsmaHelper to connect hosts and switches
105  CsmaHelper csmaHelper;
106  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
107  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
108 
109  NetDeviceContainer pairDevs;
110  NetDeviceContainer hostDevices;
111  NetDeviceContainer hostSwitchDevices;
112 
113  // Connect host 0 to first switch
114  pairDevs = csmaHelper.Install(NodeContainer(hosts.Get(0), switches.Get(0)));
115  hostDevices.Add(pairDevs.Get(0));
116  hostSwitchDevices.Add(pairDevs.Get(1));
117  sw0->AddSwitchPort(pairDevs.Get(1)); // Port #1 at switch 0
118 
119  // Connect host 1 to second switch
120  pairDevs = csmaHelper.Install(NodeContainer(hosts.Get(1), switches.Get(1)));
121  hostDevices.Add(pairDevs.Get(0));
122  hostSwitchDevices.Add(pairDevs.Get(1));
123  sw1->AddSwitchPort(pairDevs.Get(1)); // Port #1 at switch 1
124 
125  // Set IPv4 host addresses on different networks
126  Ipv4InterfaceContainer hostIpIfaces;
127  Ipv4InterfaceContainer ipIfaces;
128  Ipv4AddressHelper ipv4Helper;
129  Ipv4Address ipDomain1("10.1.1.0");
130  Ipv4Address ipDomain2("10.2.2.0");
131  Ipv4Mask mask24("255.255.255.0");
132  Ipv4Address sw0GatewayIp;
133  Ipv4Address sw1GatewayIp;
134 
135  ipv4Helper.SetBase(ipDomain1, mask24);
136  ipIfaces = ipv4Helper.Assign(NetDeviceContainer(hostDevices.Get(0)));
137  hostIpIfaces.Add(ipIfaces.Get(0));
138  sw0GatewayIp = ipv4Helper.NewAddress();
139 
140  ipv4Helper.SetBase(ipDomain2, mask24);
141  ipIfaces = ipv4Helper.Assign(NetDeviceContainer(hostDevices.Get(1)));
142  hostIpIfaces.Add(ipIfaces.Get(0));
143  sw1GatewayIp = ipv4Helper.NewAddress();
144 
145  // Save ARP entries on the controller, so it can respond to ARP requests
146  tunnelController->SaveArpEntry(
147  sw0GatewayIp,
148  Mac48Address::ConvertFrom(hostSwitchDevices.Get(0)->GetAddress()));
149  tunnelController->SaveArpEntry(
150  sw1GatewayIp,
151  Mac48Address::ConvertFrom(hostSwitchDevices.Get(1)->GetAddress()));
152  tunnelController->SaveArpEntry(hostIpIfaces.GetAddress(0),
153  Mac48Address::ConvertFrom(hostDevices.Get(0)->GetAddress()));
154  tunnelController->SaveArpEntry(hostIpIfaces.GetAddress(1),
155  Mac48Address::ConvertFrom(hostDevices.Get(1)->GetAddress()));
156 
157  // Configure static routes on host nodes
158  Ipv4StaticRoutingHelper ipv4RoutingHelper;
159  Ptr<Ipv4StaticRouting> hostStaticRouting;
160  hostStaticRouting = ipv4RoutingHelper.GetStaticRouting(hosts.Get(0)->GetObject<Ipv4>());
161  hostStaticRouting->AddNetworkRouteTo(ipDomain2, mask24, sw0GatewayIp, 1);
162  hostStaticRouting = ipv4RoutingHelper.GetStaticRouting(hosts.Get(1)->GetObject<Ipv4>());
163  hostStaticRouting->AddNetworkRouteTo(ipDomain1, mask24, sw1GatewayIp, 1);
164 
165  // Connect switch 0 to switch 1. These CSMA devices will not be added as
166  // switch ports. Instead, each one will be configured as standard ns-3 device
167  // (with IP address and an UDP socket binded to it). They will be used to
168  // implement the UDP/IP tunneling process. The GtpTunnelApp application
169  // running on top of the UDP socket will be in charge of adding and removing
170  // the GTP headers, and forwarding the packets to a VirtualNetDevice device
171  // on the same node. This VirtualNetDevice will be configured as switch port
172  // and will finally interact with the OpenFlow device.
173  pairDevs = csmaHelper.Install(NodeContainer(switches.Get(0), switches.Get(1)));
174  Ptr<CsmaNetDevice> physicalDev0 = DynamicCast<CsmaNetDevice>(pairDevs.Get(0));
175  Ptr<CsmaNetDevice> physicalDev1 = DynamicCast<CsmaNetDevice>(pairDevs.Get(1));
176 
177  // Set IPv4 tunnel endpoint addresses
178  Ipv4Address tunnelDomain("192.168.1.0");
179  ipv4Helper.SetBase(tunnelDomain, mask24);
180  ipIfaces = ipv4Helper.Assign(pairDevs);
181 
182  // Create the virtual net devices to work as logical ports on the switches.
183  // These logical ports will connect to the tunnel handler application.
185  Ptr<VirtualNetDevice> logicalPort0 = CreateObject<VirtualNetDevice>();
186  logicalPort0->SetAttribute("Mtu", UintegerValue(1500));
187  logicalPort0->SetAddress(Mac48Address::Allocate());
188  port = sw0->AddSwitchPort(logicalPort0); // Port #2 on switch 0
189  tunnelController->SaveTunnelEndpoint(sw0->GetDatapathId(),
190  port->GetPortNo(),
191  ipIfaces.GetAddress(1));
192 
193  Ptr<VirtualNetDevice> logicalPort1 = CreateObject<VirtualNetDevice>();
194  logicalPort1->SetAttribute("Mtu", UintegerValue(1500));
195  logicalPort1->SetAddress(Mac48Address::Allocate());
196  port = sw1->AddSwitchPort(logicalPort1); // Port #2 on switch 1
197  tunnelController->SaveTunnelEndpoint(sw1->GetDatapathId(),
198  port->GetPortNo(),
199  ipIfaces.GetAddress(0));
200 
201  // Create the tunnel handler applications
202  Ptr<GtpTunnelApp> tunnelApp0 = CreateObject<GtpTunnelApp>(logicalPort0, physicalDev0);
203  switches.Get(0)->AddApplication(tunnelApp0);
204 
205  Ptr<GtpTunnelApp> tunnelApp1 = CreateObject<GtpTunnelApp>(logicalPort1, physicalDev1);
206  switches.Get(1)->AddApplication(tunnelApp1);
207 
208  // Finally, create the OpenFlow channels
209  of13Helper->CreateOpenFlowChannels();
210 
211  // Configure ping application between hosts
212  PingHelper pingHelper(Ipv4Address(hostIpIfaces.GetAddress(1)));
213  pingHelper.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
214  ApplicationContainer pingApps = pingHelper.Install(hosts.Get(0));
215  pingApps.Start(Seconds(1));
216 
217  // Enable datapath stats and pcap traces at hosts, switch(es), and controller(s)
218  if (trace)
219  {
220  of13Helper->EnableOpenFlowPcap("openflow");
221  of13Helper->EnableDatapathStats("switch-stats");
222  csmaHelper.EnablePcap("ofswitch", switches, true);
223  csmaHelper.EnablePcap("host", hostDevices);
224  }
225 
226  // Run the simulation
227  Simulator::Stop(Seconds(simTime));
228  Simulator::Run();
230 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
AttributeValue implementation for DataRate.
Hold variables of type enum.
Definition: enum.h:56
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4Address NewAddress()
Increment the IP address counter used to allocate IP addresses.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
Holds a vector of ns3::OFSwitch13Device pointers.
Ptr< OFSwitch13Device > Get(uint32_t i) const
Get the Ptr<OFSwitch13Device> stored in this container at a given index.
static void EnableDatapathLogs(std::string prefix="", bool explicitFilename=false)
Enable OpenFlow datapath logs at all OpenFlow switch devices on the simulation.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:200
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Create a ping application and associate it to a node.
Definition: ping-helper.h:48
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
AttributeValue implementation for Time.
Definition: nstime.h:1423
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetAddress(Address address) override
Set the address of this interface.
uint16_t port
Definition: dsdv-manet.cc:45
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:305
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
cmd
Definition: second.py:33
bool verbose