A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
openflow-switch.cc
Go to the documentation of this file.
1/*
2 *
3 * SPDX-License-Identifier: GPL-2.0-only
4 *
5 * Author: Blake Hurd <naimorai@gmail.com>
6 * Modified by: Josh Pelkey <joshpelkey@gmail.com>
7 */
8
9// Network topology
10//
11// n0 n1
12// | |
13// ----------
14// | Switch |
15// ----------
16// | |
17// n2 n3
18//
19//
20// - CBR/UDP flows from n0 to n1 and from n3 to n0
21// - DropTail queues
22// - Tracing of queues and packet receptions to file "openflow-switch.tr"
23// - If order of adding nodes and netdevices is kept:
24// n0 = 00:00:00;00:00:01, n1 = 00:00:00:00:00:03, n3 = 00:00:00:00:00:07
25// and port number corresponds to node number, so port 0 is connected to n0, for example.
26
27#include "ns3/applications-module.h"
28#include "ns3/core-module.h"
29#include "ns3/csma-module.h"
30#include "ns3/internet-module.h"
31#include "ns3/log.h"
32#include "ns3/network-module.h"
33#include "ns3/openflow-module.h"
34
35#include <fstream>
36#include <iostream>
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("OpenFlowCsmaSwitchExample");
41
42bool verbose = false;
43bool use_drop = false;
45
46bool
47SetVerbose(const std::string& value)
48{
49 verbose = true;
50 return true;
51}
52
53bool
54SetDrop(const std::string& value)
55{
56 use_drop = true;
57 return true;
58}
59
60bool
61SetTimeout(const std::string& value)
62{
63 try
64 {
65 timeout = ns3::Seconds(std::stof(value));
66 return true;
67 }
68 catch (...)
69 {
70 return false;
71 }
72 return false;
73}
74
75int
76main(int argc, char* argv[])
77{
78 //
79 // Allow the user to override any of the defaults and the above Bind() at
80 // run-time, via command-line arguments
81 //
82 CommandLine cmd(__FILE__);
83 cmd.AddValue("v", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
84 cmd.AddValue("verbose", "Verbose (turns on logging).", MakeCallback(&SetVerbose));
85 cmd.AddValue("d", "Use Drop Controller (Learning if not specified).", MakeCallback(&SetDrop));
86 cmd.AddValue("drop",
87 "Use Drop Controller (Learning if not specified).",
89 cmd.AddValue("t",
90 "Learning Controller Timeout (has no effect if drop controller is specified).",
92 cmd.AddValue("timeout",
93 "Learning Controller Timeout (has no effect if drop controller is specified).",
95
96 cmd.Parse(argc, argv);
97
98 if (verbose)
99 {
100 LogComponentEnable("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
101 LogComponentEnable("OpenFlowInterface", LOG_LEVEL_INFO);
102 LogComponentEnable("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
103 }
104
105 //
106 // Explicitly create the nodes required by the topology (shown above).
107 //
108 NS_LOG_INFO("Create nodes.");
110 terminals.Create(4);
111
113 csmaSwitch.Create(1);
114
115 NS_LOG_INFO("Build Topology");
117 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
118 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
119
120 // Create the csma links, from each terminal to the switch
123 for (int i = 0; i < 4; i++)
124 {
125 NetDeviceContainer link = csma.Install(NodeContainer(terminals.Get(i), csmaSwitch));
126 terminalDevices.Add(link.Get(0));
127 switchDevices.Add(link.Get(1));
128 }
129
130 // Create the switch netdevice, which will do the packet switching
133
134 if (use_drop)
135 {
137 swtch.Install(switchNode, switchDevices, controller);
138 }
139 else
140 {
142 if (!timeout.IsZero())
143 {
144 controller->SetAttribute("ExpirationTime", TimeValue(timeout));
145 }
146 swtch.Install(switchNode, switchDevices, controller);
147 }
148
149 // Add internet stack to the terminals
151 internet.Install(terminals);
152
153 // We've got the "hardware" in place. Now we need to add IP addresses.
154 NS_LOG_INFO("Assign IP Addresses.");
156 ipv4.SetBase("10.1.1.0", "255.255.255.0");
157 ipv4.Assign(terminalDevices);
158
159 // Create an OnOff application to send UDP datagrams from n0 to n1.
160 NS_LOG_INFO("Create Applications.");
161 uint16_t port = 9; // Discard port (RFC 863)
162
163 OnOffHelper onoff("ns3::UdpSocketFactory",
165 onoff.SetConstantRate(DataRate("500kb/s"));
166
167 ApplicationContainer app = onoff.Install(terminals.Get(0));
168 // Start the application
169 app.Start(Seconds(1));
170 app.Stop(Seconds(10));
171
172 // Create an optional packet sink to receive these packets
173 PacketSinkHelper sink("ns3::UdpSocketFactory",
175 app = sink.Install(terminals.Get(1));
176 app.Start(Seconds(0));
177
178 //
179 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
180 //
181 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.1"), port)));
182 app = onoff.Install(terminals.Get(3));
183 app.Start(Seconds(1.1));
184 app.Stop(Seconds(10));
185
186 app = sink.Install(terminals.Get(0));
187 app.Start(Seconds(0));
188
189 NS_LOG_INFO("Configure Tracing.");
190
191 //
192 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
193 // Trace output will be sent to the file "openflow-switch.tr"
194 //
195 AsciiTraceHelper ascii;
196 csma.EnableAsciiAll(ascii.CreateFileStream("openflow-switch.tr"));
197
198 //
199 // Also configure some tcpdump traces; each interface will be traced.
200 // The output files will be named:
201 // openflow-switch-<nodeId>-<interfaceId>.pcap
202 // and can be read by the "tcpdump -r" command (use "-tt" option to
203 // display timestamps correctly)
204 //
205 csma.EnablePcapAll("openflow-switch", false);
206
207 //
208 // Now, do the actual simulation.
209 //
210 NS_LOG_INFO("Run Simulation.");
213 NS_LOG_INFO("Done.");
214
215 return 0;
216}
a polymophic address class
Definition address.h:90
AttributeValue implementation for Address.
Definition address.h:275
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
AttributeValue implementation for DataRate.
Definition data-rate.h:285
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.
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.
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
bool IsZero() const
Exactly equivalent to t == 0.
Definition nstime.h:304
AttributeValue implementation for Time.
Definition nstime.h:1431
uint16_t port
Definition dsdv-manet.cc:33
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
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:291
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:684
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
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:44