A Discrete-Event Network Simulator
API
ofswitch13-multiple-domains.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 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  * Vitor M. Eichemberger <vitor.marge@gmail.com>
19  */
20 
21 /*
22  * Two hosts connected to different OpenFlow switches.
23  * Each switch is managed by an independent default learning controller application.
24  *
25  * Learning Controller Learning Controller
26  * | |
27  * +----------+ +----------+
28  * Host 0 === | Switch 0 | ======== | Switch 1 | === Host 1
29  * +----------+ +----------+
30  */
31 
32 #include <ns3/core-module.h>
33 #include <ns3/csma-module.h>
34 #include <ns3/internet-apps-module.h>
35 #include <ns3/internet-module.h>
36 #include <ns3/network-module.h>
37 #include <ns3/ofswitch13-module.h>
38 
39 using namespace ns3;
40 
41 int
42 main(int argc, char* argv[])
43 {
44  uint16_t simTime = 10;
45  bool verbose = false;
46  bool trace = false;
47 
48  // Configure command line parameters
50  cmd.AddValue("simTime", "Simulation time (seconds)", simTime);
51  cmd.AddValue("verbose", "Enable verbose output", verbose);
52  cmd.AddValue("trace", "Enable datapath stats and pcap traces", trace);
53  cmd.Parse(argc, argv);
54 
55  if (verbose)
56  {
58  LogComponentEnable("OFSwitch13Interface", LOG_LEVEL_ALL);
59  LogComponentEnable("OFSwitch13Device", LOG_LEVEL_ALL);
60  LogComponentEnable("OFSwitch13Port", LOG_LEVEL_ALL);
61  LogComponentEnable("OFSwitch13Queue", LOG_LEVEL_ALL);
62  LogComponentEnable("OFSwitch13SocketHandler", LOG_LEVEL_ALL);
63  LogComponentEnable("OFSwitch13Controller", LOG_LEVEL_ALL);
64  LogComponentEnable("OFSwitch13LearningController", LOG_LEVEL_ALL);
65  LogComponentEnable("OFSwitch13Helper", LOG_LEVEL_ALL);
66  LogComponentEnable("OFSwitch13InternalHelper", LOG_LEVEL_ALL);
67  }
68 
69  // Enable checksum computations (required by OFSwitch13 module)
70  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
71 
72  // Create two host nodes
73  NodeContainer hosts;
74  hosts.Create(2);
75 
76  // Create two switch nodes
77  NodeContainer switches;
78  switches.Create(2);
79 
80  // Use the CsmaHelper to connect hosts and switches
81  CsmaHelper csmaHelper;
82  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
83  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
84 
85  NodeContainer pair;
86  NetDeviceContainer pairDevs;
87  NetDeviceContainer hostDevices;
88  NetDeviceContainer switchPorts[2];
89  switchPorts[0] = NetDeviceContainer();
90  switchPorts[1] = NetDeviceContainer();
91 
92  // Connect host 0 to first switch
93  pair = NodeContainer(hosts.Get(0), switches.Get(0));
94  pairDevs = csmaHelper.Install(pair);
95  hostDevices.Add(pairDevs.Get(0));
96  switchPorts[0].Add(pairDevs.Get(1));
97 
98  // Connect host 1 to second switch
99  pair = NodeContainer(hosts.Get(1), switches.Get(1));
100  pairDevs = csmaHelper.Install(pair);
101  hostDevices.Add(pairDevs.Get(0));
102  switchPorts[1].Add(pairDevs.Get(1));
103 
104  // Connect the switches
105  pair = NodeContainer(switches.Get(0), switches.Get(1));
106  pairDevs = csmaHelper.Install(pair);
107  switchPorts[0].Add(pairDevs.Get(0));
108  switchPorts[1].Add(pairDevs.Get(1));
109 
110  // Create two controller nodes
111  NodeContainer controllers;
112  controllers.Create(2);
113 
114  // Configure both OpenFlow network domains
115  Ptr<OFSwitch13InternalHelper> of13Helper0 = CreateObject<OFSwitch13InternalHelper>();
116  of13Helper0->InstallController(controllers.Get(0));
117  of13Helper0->InstallSwitch(switches.Get(0), switchPorts[0]);
118  of13Helper0->CreateOpenFlowChannels();
119 
120  Ptr<OFSwitch13InternalHelper> of13Helper1 = CreateObject<OFSwitch13InternalHelper>();
121  of13Helper1->InstallController(controllers.Get(1));
122  of13Helper1->InstallSwitch(switches.Get(1), switchPorts[1]);
123  of13Helper1->CreateOpenFlowChannels();
124 
125  // Install the TCP/IP stack into hosts nodes
126  InternetStackHelper internet;
127  internet.Install(hosts);
128 
129  // Set IPv4 host addresses
130  Ipv4AddressHelper ipv4helpr;
131  Ipv4InterfaceContainer hostIpIfaces;
132  ipv4helpr.SetBase("10.1.1.0", "255.255.255.0");
133  hostIpIfaces = ipv4helpr.Assign(hostDevices);
134 
135  // Configure ping application between hosts
136  PingHelper pingHelper(Ipv4Address(hostIpIfaces.GetAddress(1)));
137  pingHelper.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
138  ApplicationContainer pingApps = pingHelper.Install(hosts.Get(0));
139  pingApps.Start(Seconds(1));
140 
141  // Enable datapath stats and pcap traces at hosts, switch(es), and controller(s)
142  if (trace)
143  {
144  of13Helper0->EnableOpenFlowPcap("openflow-0");
145  of13Helper0->EnableDatapathStats("switch-stats");
146  of13Helper1->EnableOpenFlowPcap("openflow-1");
147  of13Helper1->EnableDatapathStats("switch-stats");
148  csmaHelper.EnablePcap("switch", switchPorts[0], true);
149  csmaHelper.EnablePcap("switch", switchPorts[1], true);
150  csmaHelper.EnablePcap("host", hostDevices);
151  }
152 
153  // Run the simulation
154  Simulator::Stop(Seconds(simTime));
155  Simulator::Run();
157 }
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.
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.
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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