A Discrete-Event Network Simulator
API
ofswitch13-custom-switch.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 the default learning controller application.
23  * The switch datapath can be customized by the command line parameters.
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  // Custom switch datapath attributes
49  uint32_t flowSize = 10;
50  uint32_t groupSize = 10;
51  uint32_t meterSize = 10;
52  uint32_t pipeTabs = 1;
53  DataRate pipeLoad = DataRate("1Mbps");
54 
55  // Configure command line parameters
57  cmd.AddValue("simTime", "Simulation time (seconds)", simTime);
58  cmd.AddValue("verbose", "Enable verbose output", verbose);
59  cmd.AddValue("trace", "Enable datapath stats and pcap traces", trace);
60  cmd.AddValue("flowSize", "The flow table size", flowSize);
61  cmd.AddValue("groupSize", "The group table size", groupSize);
62  cmd.AddValue("meterSize", "The meter table size", meterSize);
63  cmd.AddValue("pipeTabs", "The number of pipeline flow tables", pipeTabs);
64  cmd.AddValue("pipeLoad", "The pipeline processing capacity", pipeLoad);
65  cmd.Parse(argc, argv);
66 
67  if (verbose)
68  {
70  LogComponentEnable("OFSwitch13Interface", LOG_LEVEL_ALL);
71  LogComponentEnable("OFSwitch13Device", LOG_LEVEL_ALL);
72  LogComponentEnable("OFSwitch13Port", LOG_LEVEL_ALL);
73  LogComponentEnable("OFSwitch13Queue", LOG_LEVEL_ALL);
74  LogComponentEnable("OFSwitch13SocketHandler", LOG_LEVEL_ALL);
75  LogComponentEnable("OFSwitch13Controller", LOG_LEVEL_ALL);
76  LogComponentEnable("OFSwitch13LearningController", LOG_LEVEL_ALL);
77  LogComponentEnable("OFSwitch13Helper", LOG_LEVEL_ALL);
78  LogComponentEnable("OFSwitch13InternalHelper", LOG_LEVEL_ALL);
79  }
80 
81  // Enable checksum computations (required by OFSwitch13 module)
82  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
83 
84  // Create two host nodes
85  NodeContainer hosts;
86  hosts.Create(2);
87 
88  // Create the switch node
89  Ptr<Node> switchNode = CreateObject<Node>();
90 
91  // Use the CsmaHelper to connect host nodes to the switch node
92  CsmaHelper csmaHelper;
93  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
94  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
95 
96  NetDeviceContainer hostDevices;
97  NetDeviceContainer switchPorts;
98  for (size_t i = 0; i < hosts.GetN(); i++)
99  {
100  NodeContainer pair(hosts.Get(i), switchNode);
101  NetDeviceContainer link = csmaHelper.Install(pair);
102  hostDevices.Add(link.Get(0));
103  switchPorts.Add(link.Get(1));
104  }
105 
106  // Create the controller node
107  Ptr<Node> controllerNode = CreateObject<Node>();
108 
109  // Create the OpenFlow helper with custom switch attributes.
110  Ptr<OFSwitch13InternalHelper> of13Helper = CreateObject<OFSwitch13InternalHelper>();
111  of13Helper->SetDeviceAttribute("CpuCapacity", DataRateValue(pipeLoad));
112  of13Helper->SetDeviceAttribute("PipelineTables", UintegerValue(pipeTabs));
113  of13Helper->SetDeviceAttribute("FlowTableSize", UintegerValue(flowSize));
114  of13Helper->SetDeviceAttribute("GroupTableSize", UintegerValue(groupSize));
115  of13Helper->SetDeviceAttribute("MeterTableSize", UintegerValue(meterSize));
116 
117  // Configure the OpenFlow network domain
118  of13Helper->InstallController(controllerNode);
119  of13Helper->InstallSwitch(switchNode, switchPorts);
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, true);
144  csmaHelper.EnablePcap("host", hostDevices);
145  }
146 
147  // Run the simulation
148  Simulator::Stop(Seconds(simTime));
149  Simulator::Run();
151 }
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
Class for representing data rates.
Definition: data-rate.h:90
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
Hold an unsigned integer type.
Definition: uinteger.h: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