A Discrete-Event Network Simulator
API
ofswitch13-first.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 a single OpenFlow switch.
23  * The switch is managed by the default learning controller application.
24  *
25  * Learning Controller
26  * |
27  * +-----------------+
28  * Host 0 === | OpenFlow switch | === 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 the switch node
77  Ptr<Node> switchNode = CreateObject<Node>();
78 
79  // Use the CsmaHelper to connect host nodes to the switch node
80  CsmaHelper csmaHelper;
81  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
82  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
83 
84  NetDeviceContainer hostDevices;
85  NetDeviceContainer switchPorts;
86  for (size_t i = 0; i < hosts.GetN(); i++)
87  {
88  NodeContainer pair(hosts.Get(i), switchNode);
89  NetDeviceContainer link = csmaHelper.Install(pair);
90  hostDevices.Add(link.Get(0));
91  switchPorts.Add(link.Get(1));
92  }
93 
94  // Create the controller node
95  Ptr<Node> controllerNode = CreateObject<Node>();
96 
97  // Configure the OpenFlow network domain
98  Ptr<OFSwitch13InternalHelper> of13Helper = CreateObject<OFSwitch13InternalHelper>();
99  of13Helper->InstallController(controllerNode);
100  of13Helper->InstallSwitch(switchNode, switchPorts);
101  of13Helper->CreateOpenFlowChannels();
102 
103  // Install the TCP/IP stack into hosts nodes
104  InternetStackHelper internet;
105  internet.Install(hosts);
106 
107  // Set IPv4 host addresses
108  Ipv4AddressHelper ipv4helpr;
109  Ipv4InterfaceContainer hostIpIfaces;
110  ipv4helpr.SetBase("10.1.1.0", "255.255.255.0");
111  hostIpIfaces = ipv4helpr.Assign(hostDevices);
112 
113  // Configure ping application between hosts
114  PingHelper pingHelper(Ipv4Address(hostIpIfaces.GetAddress(1)));
115  pingHelper.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
116  ApplicationContainer pingApps = pingHelper.Install(hosts.Get(0));
117  pingApps.Start(Seconds(1));
118 
119  // Enable datapath stats and pcap traces at hosts, switch(es), and controller(s)
120  if (trace)
121  {
122  of13Helper->EnableOpenFlowPcap("openflow");
123  of13Helper->EnableDatapathStats("switch-stats");
124  csmaHelper.EnablePcap("switch", switchPorts, true);
125  csmaHelper.EnablePcap("host", hostDevices);
126  }
127 
128  // Run the simulation
129  Simulator::Stop(Seconds(simTime));
130  Simulator::Run();
132 }
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.
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