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