A Discrete-Event Network Simulator
API
ofswitch13-controller.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 #ifndef OFSWITCH13_CONTROLLER_H
21 #define OFSWITCH13_CONTROLLER_H
22 
23 #include "ofswitch13-interface.h"
25 
26 #include <ns3/application.h>
27 #include <ns3/socket.h>
28 
29 #include <string>
30 
31 namespace ns3
32 {
33 
45 {
46  protected:
52  class RemoteSwitch : public SimpleRefCount<RemoteSwitch>
53  {
54  friend class OFSwitch13Controller;
55 
56  public:
58  RemoteSwitch();
59 
64  Ipv4Address GetIpv4() const;
65 
70  uint16_t GetPort() const;
71 
76  uint64_t GetDpId() const;
77 
78  private:
82  uint64_t m_dpId;
83  enum ofp_controller_role m_role;
84 
89  //\{
90  uint32_t m_nBuffers;
91  uint8_t m_nTables;
92  uint8_t m_auxiliaryId;
93  uint32_t m_capabilities;
94  //\}
95 
96  }; // class RemoteSwitch
97 
102  struct EchoInfo
103  {
104  friend class OFSwitch13Controller;
105 
106  public:
112 
117  Time GetRtt() const;
118 
119  private:
120  bool m_waiting;
124  };
125 
130  struct BarrierInfo
131  {
132  friend class OFSwitch13Controller;
133 
134  public:
140 
141  private:
142  bool m_waiting;
144  };
145 
146  private:
151  class PendingCommands : public SimpleRefCount<PendingCommands>
152  {
153  friend class OFSwitch13Controller;
154 
155  public:
157  PendingCommands();
158 
159  private:
160  std::queue<std::string> m_queue;
161  };
162 
163  public:
165  ~OFSwitch13Controller() override;
166 
171  static TypeId GetTypeId();
172 
176  void DoDispose() override;
177 
184  int DpctlExecute(uint64_t dpId, const std::string textCmd);
185 
195  static void DpctlSendAndPrint(struct vconn* vconn,
196  struct ofl_msg_header* msg);
197 
198  protected:
199  // inherited from Application
200  void StartApplication() override;
201  void StopApplication() override;
202 
206  uint32_t GetNextXid();
207 
214  virtual void HandshakeSuccessful(Ptr<const RemoteSwitch> swtch);
215 
221  Ptr<const RemoteSwitch> GetRemoteSwitch(uint64_t dpId) const;
222 
231  struct ofl_msg_header* msg,
232  uint32_t xid = 0);
233 
239  void SendEchoRequest(Ptr<const RemoteSwitch> swtch, size_t payloadSize = 0);
240 
249 
268  //\{
269  ofl_err HandleEchoRequest(struct ofl_msg_echo* msg,
271  uint32_t xid);
272 
273  ofl_err HandleEchoReply(struct ofl_msg_echo* msg,
275  uint32_t xid);
276 
277  ofl_err HandleBarrierReply(struct ofl_msg_header* msg,
279  uint32_t xid);
280 
281  ofl_err HandleHello(struct ofl_msg_header* msg,
283  uint32_t xid);
284 
285  ofl_err HandleFeaturesReply(struct ofl_msg_features_reply* msg,
286  Ptr<RemoteSwitch> swtch,
287  uint32_t xid);
288 
289  virtual ofl_err HandlePacketIn(struct ofl_msg_packet_in* msg,
291  uint32_t xid);
292 
293  virtual ofl_err HandleError(struct ofl_msg_error* msg,
295  uint32_t xid);
296 
297  virtual ofl_err HandleGetConfigReply(struct ofl_msg_get_config_reply* msg,
299  uint32_t xid);
300 
301  virtual ofl_err HandleFlowRemoved(struct ofl_msg_flow_removed* msg,
303  uint32_t xid);
304 
305  virtual ofl_err HandlePortStatus(struct ofl_msg_port_status* msg,
307  uint32_t xid);
308 
309  virtual ofl_err HandleAsyncReply(struct ofl_msg_async_config* msg,
311  uint32_t xid);
312 
313  virtual ofl_err HandleMultipartReply(
314  struct ofl_msg_multipart_reply_header* msg,
316  uint32_t xid);
317 
318  virtual ofl_err HandleRoleReply(struct ofl_msg_role_request* msg,
320  uint32_t xid);
321 
322  virtual ofl_err HandleQueueGetConfigReply(
323  struct ofl_msg_queue_get_config_reply* msg,
325  uint32_t xid);
326  //\}
327 
328  private:
337  ofl_err HandleSwitchMsg(struct ofl_msg_header* msg,
338  Ptr<RemoteSwitch> swtch,
339  uint32_t xid);
340 
346  void ReceiveFromSwitch(Ptr<Packet> packet, Address from);
347 
354 
362  //\{
364  bool SocketRequest(Ptr<Socket> socket, const Address& from);
365 
367  void SocketAccept(Ptr<Socket> socket, const Address& from);
368 
370  void SocketPeerClose(Ptr<Socket> socket);
371 
373  void SocketPeerError(Ptr<Socket> socket);
374  //\}
375 
377  typedef std::map<uint32_t, EchoInfo> EchoMsgMap_t;
378 
380  typedef std::map<uint32_t, BarrierInfo> BarrierMsgMap_t;
381 
383  typedef std::map<uint64_t, Ptr<PendingCommands>> DpIdCmdMap_t;
384 
386  typedef std::map<Address, Ptr<RemoteSwitch>> AddrSwMap_t;
387 
389  typedef std::map<uint64_t, Ptr<RemoteSwitch>> DpIdSwMap_t;
390 
391  uint32_t m_xid;
392  uint16_t m_port;
394 
400 };
401 
402 } // namespace ns3
403 #endif /* OFSWITCH13_CONTROLLER_H */
a polymophic address class
Definition: address.h:100
The base class for all ns3 applications.
Definition: application.h:61
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Inner class to save pending commands waiting for handshake procedure.
std::queue< std::string > m_queue
Queue of pending dpctl commands.
Inner class to save information of a remote active OpenFlow switch connected to this controller.
uint32_t m_capabilities
Bitmap of support ofp_capabilities.
RemoteSwitch()
Default (empty) constructor.
Ipv4Address GetIpv4() const
Get the IP from socket connection address.
Ptr< OFSwitch13Controller > m_ctrlApp
Controller application.
uint8_t m_auxiliaryId
Identify auxiliary connections.
Address m_address
Switch connection address.
enum ofp_controller_role m_role
Controller role over switch.
Ptr< OFSwitch13SocketHandler > m_handler
Socket handler.
uint16_t GetPort() const
Get the port from socket connection address.
uint32_t m_nBuffers
Switch features informed to the controller during handshake procedure.
uint64_t GetDpId() const
Get the datapath ID.
uint8_t m_nTables
Number of tables supported by datapath.
OpenFlow 1.3 controller base class that can handle a collection of OpenFlow switches and provides the...
std::map< uint32_t, EchoInfo > EchoMsgMap_t
Map to store echo information by transaction id.
void StopApplication() override
Application specific shutdown code.
OFSwitch13Controller()
Default constructor.
ofl_err HandleEchoRequest(struct ofl_msg_echo *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
std::map< Address, Ptr< RemoteSwitch > > AddrSwMap_t
Map to store switch info by ip address.
ofl_err HandleEchoReply(struct ofl_msg_echo *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
virtual ofl_err HandlePortStatus(struct ofl_msg_port_status *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
virtual void HandshakeSuccessful(Ptr< const RemoteSwitch > swtch)
Function invoked after a successfully handshake procedure between this controller and a remote switch...
virtual ofl_err HandlePacketIn(struct ofl_msg_packet_in *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
void SocketPeerError(Ptr< Socket > socket)
TCP connection error.
AddrSwMap_t m_addrSwMap
Registered switches by address.
void SocketAccept(Ptr< Socket > socket, const Address &from)
TCP handshake succeeded.
virtual ofl_err HandleGetConfigReply(struct ofl_msg_get_config_reply *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
virtual ofl_err HandleMultipartReply(struct ofl_msg_multipart_reply_header *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
void StartApplication() override
Application specific startup code.
static void DpctlSendAndPrint(struct vconn *vconn, struct ofl_msg_header *msg)
Overriding BOFUSS dpctl_send_and_print and dpctl_transact_and_print weak functions from utilities/dpc...
void SocketPeerClose(Ptr< Socket > socket)
TCP connection closed.
uint32_t m_xid
Global transaction idx.
~OFSwitch13Controller() override
Dummy destructor, see DoDispose.
void ReceiveFromSwitch(Ptr< Packet > packet, Address from)
Receive an OpenFlow packet from switch.
std::map< uint64_t, Ptr< PendingCommands > > DpIdCmdMap_t
Map saving pair <datapath id / pending commands>
void SendEchoRequest(Ptr< const RemoteSwitch > swtch, size_t payloadSize=0)
Send an echo request message to switch, and wait for a non-blocking reply.
virtual ofl_err HandleFlowRemoved(struct ofl_msg_flow_removed *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
ofl_err HandleHello(struct ofl_msg_header *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
Ptr< Socket > m_serverSocket
Listening server socket.
ofl_err HandleBarrierReply(struct ofl_msg_header *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
bool SocketRequest(Ptr< Socket > socket, const Address &from)
TCP request from switch.
uint16_t m_port
Local controller tcp port.
virtual ofl_err HandleQueueGetConfigReply(struct ofl_msg_queue_get_config_reply *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
ofl_err HandleSwitchMsg(struct ofl_msg_header *msg, Ptr< RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
ofl_err HandleFeaturesReply(struct ofl_msg_features_reply *msg, Ptr< RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
static TypeId GetTypeId()
Register this type.
int SendToSwitch(Ptr< const RemoteSwitch > swtch, struct ofl_msg_header *msg, uint32_t xid=0)
Send a OFLib message to a registered switch.
void SendBarrierRequest(Ptr< const RemoteSwitch > swtch)
Send a barrier request message to switch, and wait for a non-blocking reply.
EchoMsgMap_t m_echoMap
Metadata for echo requests.
virtual ofl_err HandleError(struct ofl_msg_error *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
Ptr< const RemoteSwitch > GetRemoteSwitch(uint64_t dpId) const
Get the remote switch for this OpenFlow datapath ID.
std::map< uint32_t, BarrierInfo > BarrierMsgMap_t
Map to store barrier information by transaction id.
BarrierMsgMap_t m_barrierMap
Metadata for barrier requests.
DpIdCmdMap_t m_commandsMap
Commands scheduled for execution.
virtual ofl_err HandleAsyncReply(struct ofl_msg_async_config *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
virtual ofl_err HandleRoleReply(struct ofl_msg_role_request *msg, Ptr< const RemoteSwitch > swtch, uint32_t xid)
Called when an OpenFlow message is received from a switch.
int DpctlExecute(uint64_t dpId, const std::string textCmd)
Execute a dpctl command to interact with the remote switch.
std::map< uint64_t, Ptr< RemoteSwitch > > DpIdSwMap_t
Map to store switch info by datapath id.
void DoDispose() override
Destructor implementation.
DpIdSwMap_t m_dpIdSwMap
Registered switches by datapath id.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
A template-based reference counting class.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:60
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Structure to save barrier metadata used by the controller interface.
Ptr< const RemoteSwitch > m_swtch
Remote switch.
bool m_waiting
True when waiting for reply.
BarrierInfo(Ptr< const RemoteSwitch > swtch)
Complete constructor, with remote switch.
Structure to save echo request metadata used by the controller interface.
bool m_waiting
True when waiting for reply.
Ptr< const RemoteSwitch > m_swtch
Remote switch.
Time GetRtt() const
Compute the echo RTT time.
EchoInfo(Ptr< const RemoteSwitch > swtch)
Complete constructor, with remote switch.