A Discrete-Event Network Simulator
API
openflow-switch.cc
Go to the documentation of this file.
1 /*
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Blake Hurd <naimorai@gmail.com>
17  * Modified by: Josh Pelkey <joshpelkey@gmail.com>
18  */
19 
20 // Network topology
21 //
22 // n0 n1
23 // | |
24 // ----------
25 // | Switch |
26 // ----------
27 // | |
28 // n2 n3
29 //
30 //
31 // - CBR/UDP flows from n0 to n1 and from n3 to n0
32 // - DropTail queues
33 // - Tracing of queues and packet receptions to file "openflow-switch.tr"
34 // - If order of adding nodes and netdevices is kept:
35 // n0 = 00:00:00;00:00:01, n1 = 00:00:00:00:00:03, n3 = 00:00:00:00:00:07
36 // and port number corresponds to node number, so port 0 is connected to n0, for example.
37 
38 #include "ns3/applications-module.h"
39 #include "ns3/core-module.h"
40 #include "ns3/csma-module.h"
41 #include "ns3/internet-module.h"
42 #include "ns3/log.h"
43 #include "ns3/network-module.h"
44 #include "ns3/openflow-module.h"
45 
46 #include <fstream>
47 #include <iostream>
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE("OpenFlowCsmaSwitchExample");
52 
53 bool verbose = false;
54 bool use_drop = false;
56 
57 bool
58 SetVerbose(const std::string& value)
59 {
60  verbose = true;
61  return true;
62 }
63 
64 bool
65 SetDrop(const std::string& value)
66 {
67  use_drop = true;
68  return true;
69 }
70 
71 bool
72 SetTimeout(const std::string& value)
73 {
74  try
75  {
76  timeout = ns3::Seconds(std::stof(value));
77  return true;
78  }
79  catch (...)
80  {
81  return false;
82  }
83  return false;
84 }
85 
86 int
87 main(int argc, char* argv[])
88 {
89  //
90  // Allow the user to override any of the defaults and the above Bind() at
91  // run-time, via command-line arguments
92  //
93  CommandLine cmd(__FILE__);
94  cmd.AddValue("v", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
95  cmd.AddValue("verbose", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
96  cmd.AddValue("d", "Use Drop Controller (Learning if not specified).", MakeCallback(&SetDrop));
97  cmd.AddValue("drop",
98  "Use Drop Controller (Learning if not specified).",
100  cmd.AddValue("t",
101  "Learning Controller Timeout (has no effect if drop controller is specified).",
103  cmd.AddValue("timeout",
104  "Learning Controller Timeout (has no effect if drop controller is specified).",
106 
107  cmd.Parse(argc, argv);
108 
109  if (verbose)
110  {
111  LogComponentEnable("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
112  LogComponentEnable("OpenFlowInterface", LOG_LEVEL_INFO);
113  LogComponentEnable("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
114  }
115 
116  //
117  // Explicitly create the nodes required by the topology (shown above).
118  //
119  NS_LOG_INFO("Create nodes.");
121  terminals.Create(4);
122 
124  csmaSwitch.Create(1);
125 
126  NS_LOG_INFO("Build Topology");
128  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
129  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
130 
131  // Create the csma links, from each terminal to the switch
134  for (int i = 0; i < 4; i++)
135  {
137  terminalDevices.Add(link.Get(0));
138  switchDevices.Add(link.Get(1));
139  }
140 
141  // Create the switch netdevice, which will do the packet switching
144 
145  if (use_drop)
146  {
147  Ptr<ns3::ofi::DropController> controller = CreateObject<ns3::ofi::DropController>();
149  }
150  else
151  {
152  Ptr<ns3::ofi::LearningController> controller = CreateObject<ns3::ofi::LearningController>();
153  if (!timeout.IsZero())
154  {
155  controller->SetAttribute("ExpirationTime", TimeValue(timeout));
156  }
158  }
159 
160  // Add internet stack to the terminals
162  internet.Install(terminals);
163 
164  // We've got the "hardware" in place. Now we need to add IP addresses.
165  NS_LOG_INFO("Assign IP Addresses.");
167  ipv4.SetBase("10.1.1.0", "255.255.255.0");
168  ipv4.Assign(terminalDevices);
169 
170  // Create an OnOff application to send UDP datagrams from n0 to n1.
171  NS_LOG_INFO("Create Applications.");
172  uint16_t port = 9; // Discard port (RFC 863)
173 
174  OnOffHelper onoff("ns3::UdpSocketFactory",
175  Address(InetSocketAddress(Ipv4Address("10.1.1.2"), port)));
176  onoff.SetConstantRate(DataRate("500kb/s"));
177 
178  ApplicationContainer app = onoff.Install(terminals.Get(0));
179  // Start the application
180  app.Start(Seconds(1.0));
181  app.Stop(Seconds(10.0));
182 
183  // Create an optional packet sink to receive these packets
184  PacketSinkHelper sink("ns3::UdpSocketFactory",
186  app = sink.Install(terminals.Get(1));
187  app.Start(Seconds(0.0));
188 
189  //
190  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
191  //
192  onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.1"), port)));
193  app = onoff.Install(terminals.Get(3));
194  app.Start(Seconds(1.1));
195  app.Stop(Seconds(10.0));
196 
197  app = sink.Install(terminals.Get(0));
198  app.Start(Seconds(0.0));
199 
200  NS_LOG_INFO("Configure Tracing.");
201 
202  //
203  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
204  // Trace output will be sent to the file "openflow-switch.tr"
205  //
206  AsciiTraceHelper ascii;
207  csma.EnableAsciiAll(ascii.CreateFileStream("openflow-switch.tr"));
208 
209  //
210  // Also configure some tcpdump traces; each interface will be traced.
211  // The output files will be named:
212  // openflow-switch-<nodeId>-<interfaceId>.pcap
213  // and can be read by the "tcpdump -r" command (use "-tt" option to
214  // display timestamps correctly)
215  //
216  csma.EnablePcapAll("openflow-switch", false);
217 
218  //
219  // Now, do the actual simulation.
220  //
221  NS_LOG_INFO("Run Simulation.");
222  Simulator::Run();
224  NS_LOG_INFO("Done.");
225 
226  return 0;
227 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
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)
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:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
bool IsZero() const
Exactly equivalent to t == 0.
Definition: nstime.h:315
uint16_t port
Definition: dsdv-manet.cc:44
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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:302
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:704
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
csma
Definition: second.py:63
cmd
Definition: second.py:40
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