A Discrete-Event Network Simulator
API
ofswitch13-external-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 namespace ns3
25 {
26 
27 NS_LOG_COMPONENT_DEFINE("OFSwitch13ExternalHelper");
28 NS_OBJECT_ENSURE_REGISTERED(OFSwitch13ExternalHelper);
29 
30 class OFSwitch13Controller;
31 
33 {
34  NS_LOG_FUNCTION(this);
35 
36  // Create the common channel for all switches and controllers.
37  m_csmaChannel = CreateObject<CsmaChannel>();
38 }
39 
41 {
42  NS_LOG_FUNCTION(this);
43 }
44 
45 TypeId
47 {
48  static TypeId tid =
49  TypeId("ns3::OFSwitch13ExternalHelper")
51  .SetGroupName("OFSwitch13")
52  .AddConstructor<OFSwitch13ExternalHelper>()
53  .AddAttribute(
54  "Port",
55  "The port number where controller will be available.",
56  UintegerValue(6653),
58  MakeUintegerChecker<uint16_t>());
59  return tid;
60 }
61 
62 void
64 {
65  NS_LOG_FUNCTION(this << type);
66 
67  // Check for valid channel type for this helper.
69  "Invalid channel "
70  "type for OFSwitch13ExternalHelper (use SingleCsma).");
72 }
73 
74 void
76 {
77  NS_LOG_FUNCTION(this << rate);
78 
80  m_csmaChannel->SetAttribute("DataRate", DataRateValue(rate));
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION(this);
87 
88  NS_LOG_INFO("Creating OpenFlow channels.");
89  NS_ABORT_MSG_IF(m_blocked, "OpenFlow channels already configured.");
90 
91  // Block this helper to avoid further calls to install methods.
92  m_blocked = true;
93 
94  // Create and start the connections between switches and controllers.
95  switch (m_channelType)
96  {
98  NS_LOG_INFO("Attach all switches and controllers to the same "
99  "CSMA network.");
100 
101  // Connecting all switches to the common channel.
102  NetDeviceContainer switchDevices;
104  m_ipv4helper.Assign(switchDevices);
105  InetSocketAddress addr(m_controlAddr, m_controlPort);
106 
107  // Start the connections between controller and switches.
109  for (ofDev = m_openFlowDevs.Begin(); ofDev != m_openFlowDevs.End();
110  ofDev++)
111  {
112  NS_LOG_INFO("Connect switch " << (*ofDev)->GetDatapathId()
113  << " to controller " << addr.GetIpv4()
114  << " port " << addr.GetPort());
116  *ofDev,
117  addr);
118  }
120  break;
121  }
124  default: {
125  NS_ABORT_MSG("Invalid OpenflowChannelType.");
126  }
127  }
128 }
129 
130 Ptr<NetDevice>
132 {
133  NS_LOG_FUNCTION(this << cNode);
134 
135  NS_LOG_INFO("Installing OpenFlow controller on node " << cNode->GetId());
137  "OpenFlow controller/channels already configured.");
138 
139  // Install the TCP/IP stack into controller node.
140  if (!cNode->GetObject<Ipv4>())
141  {
142  m_internet.Install(cNode);
143  }
144 
145  // Connect the controller node to the common channel and configure IP addrs.
147  Ipv4InterfaceContainer ctrlIface = m_ipv4helper.Assign(m_controlDevs);
148  m_controlAddr = ctrlIface.GetAddress(0);
149  m_controlNode = cNode;
150 
151  return m_controlDevs.Get(0);
152 }
153 
154 void
156 {
157  NS_LOG_FUNCTION(this);
158 
159  m_csmaChannel = nullptr;
161 }
162 
163 } // namespace ns3
164 #endif // NS3_OFSWITCH13
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...
uint32_t GetN() const
Get the number of Ptr<NetDevice> stored in this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
std::vector< Ptr< OFSwitch13Device > >::const_iterator Iterator
OFSwitch13Device container iterator.
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...
void CreateOpenFlowChannels() override
This virtual method must interconnect all switches to all controllers installed by this helper and st...
Ptr< NetDevice > InstallExternalController(Ptr< Node > cNode)
This method prepares the controller node so it can be used to connect internal simulated switches to ...
uint16_t m_controlPort
OF controller TCP port.
~OFSwitch13ExternalHelper() override
Dummy destructor, see DoDispose.
Ptr< CsmaChannel > m_csmaChannel
Common CSMA channel.
static TypeId GetTypeId()
Register this type.
void SetChannelType(ChannelType type) override
Set the OpenFlow channel type used to create the connections between switches and controllers.
Ipv4Address m_controlAddr
OF IP controller addr.
Ptr< Node > m_controlNode
OF controller node.
void SetChannelDataRate(DataRate rate) override
Set the OpenFlow channel data rate used to create the connections between switches and controllers.
void DoDispose() override
Destructor implementation.
OFSwitch13ExternalHelper()
Default constructor.
InternetStackHelper m_internet
Helper for TCP/IP stack.
@ DEDICATEDP2P
Uses individual P2P channels.
@ DEDICATEDCSMA
Uses individual CSMA channels.
@ SINGLECSMA
Uses a single shared CSMA channel.
void DoDispose() override
Destructor implementation.
virtual void SetChannelType(ChannelType type)
Set the OpenFlow channel type used to create the connections between switches and controllers.
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.
virtual void SetChannelDataRate(DataRate rate)
Set the OpenFlow channel data rate used to create the connections between switches and controllers.
NodeContainer m_switchNodes
OF switch nodes.
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
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:46
#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
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.