A Discrete-Event Network Simulator
API
ofswitch13-multiple-controllers.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 a single OpenFlow switch.
22  * The switch is managed by to different controllers applications (Controllers 0 and 1).
23  *
24  * Controller 0 Controller 1
25  * | |
26  * +-------------+
27  * |
28  * +-----------------+
29  * Host 0 === | OpenFlow switch | === Host 1
30  * +-----------------+
31  */
32 
33 #include <ns3/core-module.h>
34 #include <ns3/csma-module.h>
35 #include <ns3/internet-apps-module.h>
36 #include <ns3/internet-module.h>
37 #include <ns3/network-module.h>
38 #include <ns3/ofswitch13-module.h>
39 
40 using namespace ns3;
41 
42 class Controller0;
43 class Controller1;
44 
45 int
46 main(int argc, char* argv[])
47 {
48  uint16_t simTime = 10;
49  bool verbose = false;
50  bool trace = false;
51 
52  // Configure command line parameters
54  cmd.AddValue("simTime", "Simulation time (seconds)", simTime);
55  cmd.AddValue("verbose", "Enable verbose output", verbose);
56  cmd.AddValue("trace", "Enable datapath stats and pcap traces", trace);
57  cmd.Parse(argc, argv);
58 
59  if (verbose)
60  {
62  LogComponentEnable("OFSwitch13Interface", LOG_LEVEL_ALL);
63  LogComponentEnable("OFSwitch13Device", LOG_LEVEL_ALL);
64  LogComponentEnable("OFSwitch13Port", LOG_LEVEL_ALL);
65  LogComponentEnable("OFSwitch13Queue", LOG_LEVEL_ALL);
66  LogComponentEnable("OFSwitch13SocketHandler", LOG_LEVEL_ALL);
67  LogComponentEnable("OFSwitch13Controller", LOG_LEVEL_ALL);
68  LogComponentEnable("OFSwitch13LearningController", LOG_LEVEL_ALL);
69  LogComponentEnable("OFSwitch13Helper", LOG_LEVEL_ALL);
70  LogComponentEnable("OFSwitch13InternalHelper", LOG_LEVEL_ALL);
71  }
72 
73  // Enable checksum computations (required by OFSwitch13 module)
74  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
75 
76  // Create two host nodes
77  NodeContainer hosts;
78  hosts.Create(2);
79 
80  // Create the switch node
81  Ptr<Node> switchNode = CreateObject<Node>();
82 
83  // Use the CsmaHelper to connect host nodes to the switch node
84  CsmaHelper csmaHelper;
85  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
86  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
87 
88  NetDeviceContainer hostDevices;
89  NetDeviceContainer switchPorts;
90  for (size_t i = 0; i < hosts.GetN(); i++)
91  {
92  NodeContainer pair(hosts.Get(i), switchNode);
93  NetDeviceContainer link = csmaHelper.Install(pair);
94  hostDevices.Add(link.Get(0));
95  switchPorts.Add(link.Get(1));
96  }
97 
98  // Create two controller nodes
99  NodeContainer controllers;
100  controllers.Create(2);
101 
102  // Configure the OpenFlow network domain
103  Ptr<OFSwitch13InternalHelper> of13Helper = CreateObject<OFSwitch13InternalHelper>();
104  Ptr<Controller0> ctrl0 = CreateObject<Controller0>();
105  Ptr<Controller1> ctrl1 = CreateObject<Controller1>();
106  of13Helper->InstallController(controllers.Get(0), ctrl0);
107  of13Helper->InstallController(controllers.Get(1), ctrl1);
108  of13Helper->InstallSwitch(switchNode, switchPorts);
109  of13Helper->CreateOpenFlowChannels();
110 
111  // Install the TCP/IP stack into hosts nodes
112  InternetStackHelper internet;
113  internet.Install(hosts);
114 
115  // Set IPv4 host addresses
116  Ipv4AddressHelper ipv4helpr;
117  Ipv4InterfaceContainer hostIpIfaces;
118  ipv4helpr.SetBase("10.1.1.0", "255.255.255.0");
119  hostIpIfaces = ipv4helpr.Assign(hostDevices);
120 
121  // Configure ping application between hosts
122  PingHelper pingHelper(Ipv4Address(hostIpIfaces.GetAddress(1)));
123  pingHelper.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
124  ApplicationContainer pingApps = pingHelper.Install(hosts.Get(0));
125  pingApps.Start(Seconds(1));
126 
127  // Enable datapath stats and pcap traces at hosts, switch(es), and controller(s)
128  if (trace)
129  {
130  of13Helper->EnableOpenFlowPcap("openflow");
131  of13Helper->EnableDatapathStats("switch-stats");
132  csmaHelper.EnablePcap("switch", switchPorts, true);
133  csmaHelper.EnablePcap("host", hostDevices);
134  }
135 
136  // Run the simulation
137  Simulator::Stop(Seconds(simTime));
138  Simulator::Run();
140 }
141 
144 {
145  protected:
146  // Inherited from OFSwitch13Controller
147  void HandshakeSuccessful(Ptr<const RemoteSwitch> swtch) override;
148 };
149 
150 void
152 {
153  DpctlExecute(swtch->GetDpId(), "flow-mod cmd=add,table=0,prio=1 in_port=1 write:output=2");
154 }
155 
158 {
159  protected:
160  // Inherited from OFSwitch13Controller
161  void HandshakeSuccessful(Ptr<const RemoteSwitch> swtch) override;
162 };
163 
164 void
166 {
167  DpctlExecute(swtch->GetDpId(), "flow-mod cmd=add,table=0,prio=1 in_port=2 write:output=1");
168 }
Controller 0 installs the rule to forward packets from host 0 to 1 (port 1 to 2).
void HandshakeSuccessful(Ptr< const RemoteSwitch > swtch) override
Function invoked after a successfully handshake procedure between this controller and a remote switch...
Controller 1 installs the rule to forward packets from host 1 to 0 (port 2 to 1).
void HandshakeSuccessful(Ptr< const RemoteSwitch > swtch) override
Function invoked after a successfully handshake procedure between this controller and a remote switch...
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.
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
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
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.
OpenFlow 1.3 controller base class that can handle a collection of OpenFlow switches and provides the...
static void EnableDatapathLogs(std::string prefix="", bool explicitFilename=false)
Enable OpenFlow datapath logs at all OpenFlow switch devices on the simulation.
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
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