A Discrete-Event Network Simulator
API
ofswitch13-internal-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 University of Campinas (Unicamp)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
18  */
19 
20 #ifdef NS3_OFSWITCH13
21 
23 
24 #include <ns3/ofswitch13-learning-controller.h>
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("OFSwitch13InternalHelper");
30 NS_OBJECT_ENSURE_REGISTERED(OFSwitch13InternalHelper);
31 
32 class OFSwitch13Controller;
33 
35 {
36  NS_LOG_FUNCTION(this);
37 }
38 
40 {
41  NS_LOG_FUNCTION(this);
42 }
43 
44 TypeId
46 {
47  static TypeId tid = TypeId("ns3::OFSwitch13InternalHelper")
49  .SetGroupName("OFSwitch13")
50  .AddConstructor<OFSwitch13InternalHelper>();
51  return tid;
52 }
53 
54 void
56 {
57  NS_LOG_FUNCTION(this);
58 
59  NS_LOG_INFO("Creating OpenFlow channels.");
60  NS_ABORT_MSG_IF(m_blocked, "OpenFlow channels already configured.");
61 
62  // Block this helper to avoid further calls to install methods.
63  m_blocked = true;
64 
65  // Create and start the connections between switches and controllers.
66  switch (m_channelType)
67  {
69  NS_LOG_INFO("Attach all switches and controllers to the same "
70  "CSMA network.");
71 
72  // Create the common channel for all switches and controllers.
73  Ptr<CsmaChannel> csmaChannel = CreateObjectWithAttributes<CsmaChannel>(
74  "DataRate",
76 
77  // Connecting all switches and controllers to the common channel.
78  NetDeviceContainer switchDevices;
79  Ipv4InterfaceContainer controllerAddrs;
81  switchDevices = m_csmaHelper.Install(m_switchNodes, csmaChannel);
82  controllerAddrs = m_ipv4helper.Assign(m_controlDevs);
83  m_ipv4helper.Assign(switchDevices);
84 
85  // Start the connections between controllers and switches.
86  UintegerValue portValue;
87  for (uint32_t ctIdx = 0; ctIdx < controllerAddrs.GetN(); ctIdx++)
88  {
89  m_controlApps.Get(ctIdx)->GetAttribute("Port", portValue);
90  InetSocketAddress addr(controllerAddrs.GetAddress(ctIdx),
91  portValue.Get());
92 
94  for (ofDev = m_openFlowDevs.Begin(); ofDev != m_openFlowDevs.End();
95  ofDev++)
96  {
97  NS_LOG_INFO("Connect switch "
98  << (*ofDev)->GetDatapathId() << " to controller "
99  << addr.GetIpv4() << " port " << addr.GetPort());
102  *ofDev,
103  addr);
104  }
105  }
107  break;
108  }
111  // Setting channel/device data rates.
112  m_p2pHelper.SetDeviceAttribute("DataRate",
116 
117  // To avoid IP datagram fragmentation, we are configuring the OpenFlow
118  // channel devices with a very large MTU value. The TCP sockets used to
119  // send packets to theses devices are also configured to use a large
120  // segment size at OFSwitch13Controller and OFSwitch13Device.
121  m_csmaHelper.SetDeviceAttribute("Mtu", UintegerValue(9000));
122  m_p2pHelper.SetDeviceAttribute("Mtu", UintegerValue(9000));
123 
124  // Using large queues on devices to avoid losing packets.
125  m_csmaHelper.SetQueue("ns3::DropTailQueue<Packet>",
126  "MaxSize",
127  StringValue("65536p"));
128  m_p2pHelper.SetQueue("ns3::DropTailQueue<Packet>",
129  "MaxSize",
130  StringValue("65536p"));
131 
132  // Create individual channels for each pair switch/controller.
133  UintegerValue portValue;
134  for (uint32_t swIdx = 0; swIdx < m_switchNodes.GetN(); swIdx++)
135  {
136  Ptr<Node> swNode = m_switchNodes.Get(swIdx);
137  Ptr<OFSwitch13Device> ofDev = m_openFlowDevs.Get(swIdx);
138 
139  for (uint32_t ctIdx = 0; ctIdx < m_controlNodes.GetN(); ctIdx++)
140  {
141  Ptr<Node> ctNode = m_controlNodes.Get(ctIdx);
142  Ptr<Application> ctApp = m_controlApps.Get(ctIdx);
143 
144  NetDeviceContainer pairDevs = Connect(ctNode, swNode);
145  m_controlDevs.Add(pairDevs.Get(0));
146  Ipv4InterfaceContainer pairIfaces =
147  m_ipv4helper.Assign(pairDevs);
148 
149  // Start this single connection between switch and controller.
150  m_controlApps.Get(ctIdx)->GetAttribute("Port", portValue);
151  InetSocketAddress addr(pairIfaces.GetAddress(0),
152  portValue.Get());
153 
154  NS_LOG_INFO("Connect switch "
155  << ofDev->GetDatapathId() << " to controller "
156  << addr.GetIpv4() << " port " << addr.GetPort());
159  ofDev,
160  addr);
162  }
163  }
164  break;
165  }
166  default: {
167  NS_ABORT_MSG("Invalid OpenflowChannelType.");
168  }
169  }
170 }
171 
172 Ptr<OFSwitch13Controller>
174  Ptr<Node> cNode,
175  Ptr<OFSwitch13Controller> controller)
176 {
177  NS_LOG_FUNCTION(this << cNode << controller);
178 
179  NS_LOG_INFO("Installing OpenFlow controller on node " << cNode->GetId());
180  NS_ABORT_MSG_IF(m_blocked, "OpenFlow channels already configured.");
181 
182  // Install the TCP/IP stack into controller node.
183  if (!cNode->GetObject<Ipv4>())
184  {
185  m_internet.Install(cNode);
186  }
187 
188  // Configure and save controller application and node.
189  controller->SetStartTime(Seconds(0));
190  cNode->AddApplication(controller);
191  m_controlApps.Add(controller);
192  m_controlNodes.Add(cNode);
193 
194  return controller;
195 }
196 
197 void
199 {
200  NS_LOG_FUNCTION(this);
201 
203 }
204 
205 NetDeviceContainer
206 OFSwitch13InternalHelper::Connect(Ptr<Node> ctrl, Ptr<Node> swtch)
207 {
208  NS_LOG_FUNCTION(this << ctrl << swtch);
209 
210  NodeContainer pairNodes(ctrl, swtch);
211  switch (m_channelType)
212  {
214  return m_csmaHelper.Install(pairNodes);
215  }
217  return m_p2pHelper.Install(pairNodes);
218  }
220  default: {
221  NS_ABORT_MSG("Invalid OpenflowChannelType.");
222  }
223  }
224 }
225 
226 } // namespace ns3
227 #endif // NS3_OFSWITCH13
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
void SetQueue(std::string type, Ts &&... args)
Definition: csma-helper.h:269
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:50
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
AttributeValue implementation for DataRate.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
std::vector< Ptr< OFSwitch13Device > >::const_iterator Iterator
OFSwitch13Device container iterator.
Ptr< OFSwitch13Device > Get(uint32_t i) const
Get the Ptr<OFSwitch13Device> stored in this container at a given index.
Iterator Begin() const
Get an iterator which refers to the first OpenFlow device in the container.
Iterator End() const
Get an iterator which indicates past-the-last OpenFlow device in the container.
void StartControllerConnection(Address ctrlAddr)
Starts the TCP connection between this switch and the target controller indicated by the address para...
InternetStackHelper m_internet
Helper for TCP/IP stack.
DataRate m_channelDataRate
OF channel data rate.
@ DEDICATEDP2P
Uses individual P2P channels.
@ DEDICATEDCSMA
Uses individual CSMA channels.
@ SINGLECSMA
Uses a single shared CSMA channel.
void DoDispose() override
Destructor implementation.
PointToPointHelper m_p2pHelper
Helper for P2P links.
NetDeviceContainer m_controlDevs
OF channel ctrl devices.
CsmaHelper m_csmaHelper
Helper for CSMA links.
static Ipv4AddressHelper m_ipv4helper
Helper for IP address.
OFSwitch13DeviceContainer m_openFlowDevs
OF switch devices.
bool m_blocked
Block this helper.
ChannelType m_channelType
OF channel type.
OFSwitch13Helper()
Default constructor.
NodeContainer m_switchNodes
OF switch nodes.
NodeContainer m_controlNodes
OF controller nodes.
void CreateOpenFlowChannels() override
This virtual method must interconnect all switches to all controllers installed by this helper and st...
OFSwitch13InternalHelper()
Default constructor.
void DoDispose() override
Destructor implementation.
NetDeviceContainer Connect(Ptr< Node > ctrl, Ptr< Node > swtch)
Create an individual connection between the switch and the controller node, using the already configu...
ApplicationContainer m_controlApps
OF controller apps.
Ptr< OFSwitch13Controller > InstallController(Ptr< Node > cNode, Ptr< OFSwitch13Controller > controller=CreateObject< OFSwitch13LearningController >())
This method installs the given controller application into the given controller node.
~OFSwitch13InternalHelper() override
Dummy destructor, see DoDispose.
static TypeId GetTypeId()
Register this type.
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:240
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetQueue(std::string type, Ts &&... args)
Each point to point net device must have a queue to pass packets through.
NetDeviceContainer Install(NodeContainer c)
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.