A Discrete-Event Network Simulator
API
openflow-switch.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 
17 // Network topology
18 //
19 // n0 n1
20 // | |
21 // ----------
22 // | Switch |
23 // ----------
24 // | |
25 // n2 n3
26 //
27 //
28 // - CBR/UDP flows from n0 to n1 and from n3 to n0
29 // - DropTail queues
30 // - Tracing of queues and packet receptions to file "openflow-switch.tr"
31 // - If order of adding nodes and netdevices is kept:
32 // n0 = 00:00:00;00:00:01, n1 = 00:00:00:00:00:03, n3 = 00:00:00:00:00:07
33 // and port number corresponds to node number, so port 0 is connected to n0, for example.
34 
35 #include <iostream>
36 #include <fstream>
37 
38 #include "ns3/core-module.h"
39 #include "ns3/network-module.h"
40 #include "ns3/csma-module.h"
41 #include "ns3/internet-module.h"
42 #include "ns3/applications-module.h"
43 #include "ns3/openflow-module.h"
44 #include "ns3/log.h"
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE ("OpenFlowCsmaSwitchExample");
49 
50 bool verbose = false;
51 bool use_drop = false;
53 
54 bool
55 SetVerbose (std::string value)
56 {
57  verbose = true;
58  return true;
59 }
60 
61 bool
62 SetDrop (std::string value)
63 {
64  use_drop = true;
65  return true;
66 }
67 
68 bool
69 SetTimeout (std::string value)
70 {
71  try {
72  timeout = ns3::Seconds (atof (value.c_str ()));
73  return true;
74  }
75  catch (...) { return false; }
76  return false;
77 }
78 
79 int
80 main (int argc, char *argv[])
81 {
82  #ifdef NS3_OPENFLOW
83  //
84  // Allow the user to override any of the defaults and the above Bind() at
85  // run-time, via command-line arguments
86  //
88  cmd.AddValue ("v", "Verbose (turns on logging).", MakeCallback (&SetVerbose));
89  cmd.AddValue ("verbose", "Verbose (turns on logging).", MakeCallback (&SetVerbose));
90  cmd.AddValue ("d", "Use Drop Controller (Learning if not specified).", MakeCallback (&SetDrop));
91  cmd.AddValue ("drop", "Use Drop Controller (Learning if not specified).", MakeCallback (&SetDrop));
92  cmd.AddValue ("t", "Learning Controller Timeout (has no effect if drop controller is specified).", MakeCallback ( &SetTimeout));
93  cmd.AddValue ("timeout", "Learning Controller Timeout (has no effect if drop controller is specified).", MakeCallback ( &SetTimeout));
94 
95  cmd.Parse (argc, argv);
96 
97  if (verbose)
98  {
99  LogComponentEnable ("OpenFlowCsmaSwitchExample", LOG_LEVEL_INFO);
100  LogComponentEnable ("OpenFlowInterface", LOG_LEVEL_INFO);
101  LogComponentEnable ("OpenFlowSwitchNetDevice", LOG_LEVEL_INFO);
102  }
103 
104  //
105  // Explicitly create the nodes required by the topology (shown above).
106  //
107  NS_LOG_INFO ("Create nodes.");
108  NodeContainer terminals;
109  terminals.Create (4);
110 
111  NodeContainer csmaSwitch;
112  csmaSwitch.Create (1);
113 
114  NS_LOG_INFO ("Build Topology");
116  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
117  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
118 
119  // Create the csma links, from each terminal to the switch
120  NetDeviceContainer terminalDevices;
121  NetDeviceContainer switchDevices;
122  for (int i = 0; i < 4; i++)
123  {
124  NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
125  terminalDevices.Add (link.Get (0));
126  switchDevices.Add (link.Get (1));
127  }
128 
129  // Create the switch netdevice, which will do the packet switching
130  Ptr<Node> switchNode = csmaSwitch.Get (0);
131  OpenFlowSwitchHelper swtch;
132 
133  if (use_drop)
134  {
135  Ptr<ns3::ofi::DropController> controller = CreateObject<ns3::ofi::DropController> ();
136  swtch.Install (switchNode, switchDevices, controller);
137  }
138  else
139  {
140  Ptr<ns3::ofi::LearningController> controller = CreateObject<ns3::ofi::LearningController> ();
141  if (!timeout.IsZero ()) controller->SetAttribute ("ExpirationTime", TimeValue (timeout));
142  swtch.Install (switchNode, switchDevices, controller);
143  }
144 
145  // Add internet stack to the terminals
146  InternetStackHelper internet;
147  internet.Install (terminals);
148 
149  // We've got the "hardware" in place. Now we need to add IP addresses.
150  NS_LOG_INFO ("Assign IP Addresses.");
151  Ipv4AddressHelper ipv4;
152  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
153  ipv4.Assign (terminalDevices);
154 
155  // Create an OnOff application to send UDP datagrams from n0 to n1.
156  NS_LOG_INFO ("Create Applications.");
157  uint16_t port = 9; // Discard port (RFC 863)
158 
159  OnOffHelper onoff ("ns3::UdpSocketFactory",
160  Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
161  onoff.SetConstantRate (DataRate ("500kb/s"));
162 
163  ApplicationContainer app = onoff.Install (terminals.Get (0));
164  // Start the application
165  app.Start (Seconds (1.0));
166  app.Stop (Seconds (10.0));
167 
168  // Create an optional packet sink to receive these packets
169  PacketSinkHelper sink ("ns3::UdpSocketFactory",
171  app = sink.Install (terminals.Get (1));
172  app.Start (Seconds (0.0));
173 
174  //
175  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
176  //
177  onoff.SetAttribute ("Remote",
178  AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.1"), port)));
179  app = onoff.Install (terminals.Get (3));
180  app.Start (Seconds (1.1));
181  app.Stop (Seconds (10.0));
182 
183  app = sink.Install (terminals.Get (0));
184  app.Start (Seconds (0.0));
185 
186  NS_LOG_INFO ("Configure Tracing.");
187 
188  //
189  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
190  // Trace output will be sent to the file "openflow-switch.tr"
191  //
192  AsciiTraceHelper ascii;
193  csma.EnableAsciiAll (ascii.CreateFileStream ("openflow-switch.tr"));
194 
195  //
196  // Also configure some tcpdump traces; each interface will be traced.
197  // The output files will be named:
198  // openflow-switch-<nodeId>-<interfaceId>.pcap
199  // and can be read by the "tcpdump -r" command (use "-tt" option to
200  // display timestamps correctly)
201  //
202  csma.EnablePcapAll ("openflow-switch", false);
203 
204  //
205  // Now, do the actual simulation.
206  //
207  NS_LOG_INFO ("Run Simulation.");
208  Simulator::Run ();
210  NS_LOG_INFO ("Done.");
211  #else
212  NS_LOG_INFO ("NS-3 OpenFlow is not enabled. Cannot run simulation.");
213  #endif // NS3_OPENFLOW
214 }
bool SetTimeout(std::string value)
bool SetVerbose(std::string value)
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
bool SetDrop(std::string value)
Add capability to switch multiple LAN segments (IEEE 802.1D bridging)
ns3::Time timeout
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c, Ptr< ns3::ofi::Controller > controller)
This method creates an ns3::OpenFlowSwitchNetDevice with the attributes configured by OpenFlowSwitchH...
bool IsZero(void) const
Definition: nstime.h:288
AttributeValue implementation for Time.
Definition: nstime.h:1076
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
bool use_drop
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
bool verbose