A Discrete-Event Network Simulator
API
openflow-switch.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Network topology
17 //
18 // n0 n1
19 // | |
20 // ----------
21 // | Switch |
22 // ----------
23 // | |
24 // n2 n3
25 //
26 //
27 // - CBR/UDP flows from n0 to n1 and from n3 to n0
28 // - DropTail queues
29 // - Tracing of queues and packet receptions to file "openflow-switch.tr"
30 // - If order of adding nodes and netdevices is kept:
31 // n0 = 00:00:00;00:00:01, n1 = 00:00:00:00:00:03, n3 = 00:00:00:00:00:07
32 // and port number corresponds to node number, so port 0 is connected to n0, for example.
33 
34 #include "ns3/applications-module.h"
35 #include "ns3/core-module.h"
36 #include "ns3/csma-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/log.h"
39 #include "ns3/network-module.h"
40 #include "ns3/openflow-module.h"
41 
42 #include <fstream>
43 #include <iostream>
44 
45 using namespace ns3;
46 
47 NS_LOG_COMPONENT_DEFINE("OpenFlowCsmaSwitchExample");
48 
49 bool verbose = false;
50 bool use_drop = false;
52 
53 bool
54 SetVerbose(const std::string& value)
55 {
56  verbose = true;
57  return true;
58 }
59 
60 bool
61 SetDrop(const std::string& value)
62 {
63  use_drop = true;
64  return true;
65 }
66 
67 bool
68 SetTimeout(const std::string& value)
69 {
70  try
71  {
72  timeout = ns3::Seconds(std::stof(value));
73  return true;
74  }
75  catch (...)
76  {
77  return false;
78  }
79  return false;
80 }
81 
82 int
83 main(int argc, char* argv[])
84 {
85 #ifdef NS3_OPENFLOW
86  //
87  // Allow the user to override any of the defaults and the above Bind() at
88  // run-time, via command-line arguments
89  //
90  CommandLine cmd(__FILE__);
91  cmd.AddValue("v", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
92  cmd.AddValue("verbose", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
93  cmd.AddValue("d", "Use Drop Controller (Learning if not specified).", MakeCallback(&SetDrop));
94  cmd.AddValue("drop",
95  "Use Drop Controller (Learning if not specified).",
97  cmd.AddValue("t",
98  "Learning Controller Timeout (has no effect if drop controller is specified).",
100  cmd.AddValue("timeout",
101  "Learning Controller Timeout (has no effect if drop controller is specified).",
103 
104  cmd.Parse(argc, argv);
105 
106  if (verbose)
107  {
108  LogComponentEnable("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
109  LogComponentEnable("OpenFlowInterface", LOG_LEVEL_INFO);
110  LogComponentEnable("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
111  }
112 
113  //
114  // Explicitly create the nodes required by the topology (shown above).
115  //
116  NS_LOG_INFO("Create nodes.");
117  NodeContainer terminals;
118  terminals.Create(4);
119 
120  NodeContainer csmaSwitch;
121  csmaSwitch.Create(1);
122 
123  NS_LOG_INFO("Build Topology");
125  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
126  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
127 
128  // Create the csma links, from each terminal to the switch
129  NetDeviceContainer terminalDevices;
130  NetDeviceContainer switchDevices;
131  for (int i = 0; i < 4; i++)
132  {
133  NetDeviceContainer link = csma.Install(NodeContainer(terminals.Get(i), csmaSwitch));
134  terminalDevices.Add(link.Get(0));
135  switchDevices.Add(link.Get(1));
136  }
137 
138  // Create the switch netdevice, which will do the packet switching
139  Ptr<Node> switchNode = csmaSwitch.Get(0);
140  OpenFlowSwitchHelper swtch;
141 
142  if (use_drop)
143  {
144  Ptr<ns3::ofi::DropController> controller = CreateObject<ns3::ofi::DropController>();
145  swtch.Install(switchNode, switchDevices, controller);
146  }
147  else
148  {
149  Ptr<ns3::ofi::LearningController> controller = CreateObject<ns3::ofi::LearningController>();
150  if (!timeout.IsZero())
151  {
152  controller->SetAttribute("ExpirationTime", TimeValue(timeout));
153  }
154  swtch.Install(switchNode, switchDevices, controller);
155  }
156 
157  // Add internet stack to the terminals
158  InternetStackHelper internet;
159  internet.Install(terminals);
160 
161  // We've got the "hardware" in place. Now we need to add IP addresses.
162  NS_LOG_INFO("Assign IP Addresses.");
163  Ipv4AddressHelper ipv4;
164  ipv4.SetBase("10.1.1.0", "255.255.255.0");
165  ipv4.Assign(terminalDevices);
166 
167  // Create an OnOff application to send UDP datagrams from n0 to n1.
168  NS_LOG_INFO("Create Applications.");
169  uint16_t port = 9; // Discard port (RFC 863)
170 
171  OnOffHelper onoff("ns3::UdpSocketFactory",
172  Address(InetSocketAddress(Ipv4Address("10.1.1.2"), port)));
173  onoff.SetConstantRate(DataRate("500kb/s"));
174 
175  ApplicationContainer app = onoff.Install(terminals.Get(0));
176  // Start the application
177  app.Start(Seconds(1.0));
178  app.Stop(Seconds(10.0));
179 
180  // Create an optional packet sink to receive these packets
181  PacketSinkHelper sink("ns3::UdpSocketFactory",
183  app = sink.Install(terminals.Get(1));
184  app.Start(Seconds(0.0));
185 
186  //
187  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
188  //
189  onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.1"), port)));
190  app = onoff.Install(terminals.Get(3));
191  app.Start(Seconds(1.1));
192  app.Stop(Seconds(10.0));
193 
194  app = sink.Install(terminals.Get(0));
195  app.Start(Seconds(0.0));
196 
197  NS_LOG_INFO("Configure Tracing.");
198 
199  //
200  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
201  // Trace output will be sent to the file "openflow-switch.tr"
202  //
203  AsciiTraceHelper ascii;
204  csma.EnableAsciiAll(ascii.CreateFileStream("openflow-switch.tr"));
205 
206  //
207  // Also configure some tcpdump traces; each interface will be traced.
208  // The output files will be named:
209  // openflow-switch-<nodeId>-<interfaceId>.pcap
210  // and can be read by the "tcpdump -r" command (use "-tt" option to
211  // display timestamps correctly)
212  //
213  csma.EnablePcapAll("openflow-switch", false);
214 
215  //
216  // Now, do the actual simulation.
217  //
218  NS_LOG_INFO("Run Simulation.");
219  Simulator::Run();
221  NS_LOG_INFO("Done.");
222 #else
223  NS_LOG_INFO("NS-3 OpenFlow is not enabled. Cannot run simulation.");
224 #endif // NS3_OPENFLOW
225 
226  return 0;
227 }
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
AttributeValue implementation for DataRate.
an Inet address class
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
static Ipv4Address GetAny()
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
Add capability to switch multiple LAN segments (IEEE 802.1D bridging)
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c, Ptr< ns3::ofi::Controller > controller)
This method creates an ns3::OpenFlowSwitchNetDevice with the attributes configured by OpenFlowSwitchH...
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
bool IsZero() const
Exactly equivalent to t == 0.
Definition: nstime.h:314
AttributeValue implementation for Time.
Definition: nstime.h:1423
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
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
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:707
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
csma
Definition: second.py:56
value
Definition: second.py:41
cmd
Definition: second.py:33
ns3::Time timeout
bool SetVerbose(const std::string &value)
bool SetTimeout(const std::string &value)
bool use_drop
bool SetDrop(const std::string &value)
bool verbose
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55