A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
49using namespace ns3;
50
51NS_LOG_COMPONENT_DEFINE("OpenFlowCsmaSwitchExample");
52
53bool verbose = false;
54bool use_drop = false;
56
57bool
58SetVerbose(const std::string& value)
59{
60 verbose = true;
61 return true;
62}
63
64bool
65SetDrop(const std::string& value)
66{
67 use_drop = true;
68 return true;
69}
70
71bool
72SetTimeout(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
86int
87main(int argc, char* argv[])
88{
89#ifdef NS3_OPENFLOW
90 //
91 // Allow the user to override any of the defaults and the above Bind() at
92 // run-time, via command-line arguments
93 //
94 CommandLine cmd(__FILE__);
95 cmd.AddValue("v", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
96 cmd.AddValue("verbose", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
97 cmd.AddValue("d", "Use Drop Controller (Learning if not specified).", MakeCallback(&SetDrop));
98 cmd.AddValue("drop",
99 "Use Drop Controller (Learning if not specified).",
101 cmd.AddValue("t",
102 "Learning Controller Timeout (has no effect if drop controller is specified).",
104 cmd.AddValue("timeout",
105 "Learning Controller Timeout (has no effect if drop controller is specified).",
107
108 cmd.Parse(argc, argv);
109
110 if (verbose)
111 {
112 LogComponentEnable("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
113 LogComponentEnable("OpenFlowInterface", LOG_LEVEL_INFO);
114 LogComponentEnable("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
115 }
116
117 //
118 // Explicitly create the nodes required by the topology (shown above).
119 //
120 NS_LOG_INFO("Create nodes.");
122 terminals.Create(4);
123
125 csmaSwitch.Create(1);
126
127 NS_LOG_INFO("Build Topology");
129 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
130 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
131
132 // Create the csma links, from each terminal to the switch
135 for (int i = 0; i < 4; i++)
136 {
137 NetDeviceContainer link = csma.Install(NodeContainer(terminals.Get(i), csmaSwitch));
138 terminalDevices.Add(link.Get(0));
139 switchDevices.Add(link.Get(1));
140 }
141
142 // Create the switch netdevice, which will do the packet switching
145
146 if (use_drop)
147 {
148 Ptr<ns3::ofi::DropController> controller = CreateObject<ns3::ofi::DropController>();
149 swtch.Install(switchNode, switchDevices, controller);
150 }
151 else
152 {
153 Ptr<ns3::ofi::LearningController> controller = CreateObject<ns3::ofi::LearningController>();
154 if (!timeout.IsZero())
155 {
156 controller->SetAttribute("ExpirationTime", TimeValue(timeout));
157 }
158 swtch.Install(switchNode, switchDevices, controller);
159 }
160
161 // Add internet stack to the terminals
163 internet.Install(terminals);
164
165 // We've got the "hardware" in place. Now we need to add IP addresses.
166 NS_LOG_INFO("Assign IP Addresses.");
168 ipv4.SetBase("10.1.1.0", "255.255.255.0");
169 ipv4.Assign(terminalDevices);
170
171 // Create an OnOff application to send UDP datagrams from n0 to n1.
172 NS_LOG_INFO("Create Applications.");
173 uint16_t port = 9; // Discard port (RFC 863)
174
175 OnOffHelper onoff("ns3::UdpSocketFactory",
177 onoff.SetConstantRate(DataRate("500kb/s"));
178
179 ApplicationContainer app = onoff.Install(terminals.Get(0));
180 // Start the application
181 app.Start(Seconds(1.0));
182 app.Stop(Seconds(10.0));
183
184 // Create an optional packet sink to receive these packets
185 PacketSinkHelper sink("ns3::UdpSocketFactory",
187 app = sink.Install(terminals.Get(1));
188 app.Start(Seconds(0.0));
189
190 //
191 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
192 //
193 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.1"), port)));
194 app = onoff.Install(terminals.Get(3));
195 app.Start(Seconds(1.1));
196 app.Stop(Seconds(10.0));
197
198 app = sink.Install(terminals.Get(0));
199 app.Start(Seconds(0.0));
200
201 NS_LOG_INFO("Configure Tracing.");
202
203 //
204 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
205 // Trace output will be sent to the file "openflow-switch.tr"
206 //
207 AsciiTraceHelper ascii;
208 csma.EnableAsciiAll(ascii.CreateFileStream("openflow-switch.tr"));
209
210 //
211 // Also configure some tcpdump traces; each interface will be traced.
212 // The output files will be named:
213 // openflow-switch-<nodeId>-<interfaceId>.pcap
214 // and can be read by the "tcpdump -r" command (use "-tt" option to
215 // display timestamps correctly)
216 //
217 csma.EnablePcapAll("openflow-switch", false);
218
219 //
220 // Now, do the actual simulation.
221 //
222 NS_LOG_INFO("Run Simulation.");
225 NS_LOG_INFO("Done.");
226#else
227 NS_LOG_INFO("NS-3 OpenFlow is not enabled. Cannot run simulation.");
228#endif // NS3_OPENFLOW
229
230 return 0;
231}
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
holds a vector of ns3::Application pointers.
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
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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: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
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: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:702
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:33
ns csma
Definition: second.py:56
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