Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
cofmsg.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef COFMSG_H
6 #define COFMSG_H 1
7 
8 #include <set>
9 #include <map>
10 #include <string>
11 #include <vector>
12 #include <strings.h>
13 #include <arpa/inet.h>
14 #include <endian.h>
15 #ifndef htobe16
16  #include "../../endian_conversion.h"
17 #endif
18 
19 #include "rofl/common/croflexception.h"
20 #include "rofl/common/openflow/openflow_rofl_exceptions.h"
21 #include "rofl/common/fframe.h"
22 #include "rofl/common/cpacket.h"
23 
24 #include "rofl/common/openflow/openflow.h"
25 #if 0
26 #include "rofl/common/openflow/cofmatch.h"
27 #include "rofl/common/openflow/cofinstructions.h"
28 #include "rofl/common/openflow/cofactions.h"
29 #include "rofl/common/openflow/cofbuckets.h"
30 #include "rofl/common/openflow/cofports.h"
31 #include "rofl/common/openflow/cofport.h"
32 #include "rofl/common/openflow/cofdescstats.h"
33 #include "rofl/common/openflow/cofflowstats.h"
34 #include "rofl/common/openflow/cofaggrstats.h"
35 #include "rofl/common/openflow/coftablestats.h"
36 #include "rofl/common/openflow/cofportstats.h"
37 #include "rofl/common/openflow/cofqueuestats.h"
38 #include "rofl/common/openflow/cofgroupstats.h"
39 #include "rofl/common/openflow/cofgroupdescstats.h"
40 #endif
41 
42 // forward declarations
43 class cofbase;
44 
45 namespace rofl {
46 namespace openflow {
47 
48 class eOFpacketBase : public RoflException {};
49 class eOFpacketInval : public eOFpacketBase {};
50 class eOFpacketNoData : public eOFpacketBase {};
51 class eOFpacketHeaderInval : public eOFpacketBase {}; // invalid header
52 
53 
62 class cofmsg
63 {
64 public: // static
65 
66  static std::set<cofmsg*> cofpacket_list; //< list of allocated cofpacket instances
67 
68 private: // static
69 
70  static std::string pinfo; //< information string for cofpacket::cofpacket_list
71 
72 public:
73 
76  static
77  const char*
78  packet_info(uint8_t ofp_version);
79 
82  static const char*
83  type2desc(uint8_t ofp_version, uint8_t type);
84 
85  typedef struct {
86  uint8_t type;
87  char desc[64];
88  } typedesc_t;
89 
90 
91 protected: // data structures
92 
93  cmemory *memarea; // OpenFlow packet received from socket
94 
95  union {
96  uint8_t* ofhu_generic;
97  struct rofl::openflow::ofp_header* ofhu_header;
98  } ofh_ofhu;
99 
100 #define ofh_generic ofh_ofhu.ofhu_generic
101 #define ofh_header ofh_ofhu.ofhu_header
102 
103 public:
104 
108  cofmsg(
109  uint8_t ofp_version, uint32_t xid, uint8_t type);
110 
114  cofmsg(
115  size_t size = sizeof(struct openflow::ofp_header));
116 
117 
121  cofmsg(
122  cmemory *memarea);
123 
124 
128  cofmsg(
129  cofmsg const& p);
130 
131 
135  virtual
136  ~cofmsg();
137 
138 
142  cofmsg&
143  operator=(
144  cofmsg const& p);
145 
146 
150  virtual void
151  reset();
152 
153 
157  virtual size_t
158  length() const;
159 
160 
164  virtual void
165  pack(uint8_t *buf = (uint8_t*)0, size_t buflen = 0);
166 
167 
171  virtual void
172  unpack(uint8_t *buf, size_t buflen);
173 
174 
177  virtual void
178  validate();
179 
180 
183  virtual uint8_t*
184  resize(size_t len);
185 
186 
190  uint8_t*
191  soframe() const { return memarea->somem(); };
192 
193 
197  size_t
198  framelen() const { return memarea->memlen(); };
199 
200 
204  uint8_t*
205  sobody() const { return (soframe() + sizeof(struct openflow::ofp_header)); };
206 
207 
211  size_t
212  bodylen() const { return (framelen() - sizeof(struct openflow::ofp_header)); };
213 
214 
215 public:
216 
217 
218 
219 
223  uint8_t
224  get_version() const;
225 
226 
230  virtual void
231  set_version(uint8_t ofp_version);
232 
233 
237  uint16_t
238  get_length() const;
239 
240 
244  void
245  set_length(uint16_t len);
246 
247 
251  uint8_t
252  get_type() const;
253 
254 
258  void
259  set_type(uint8_t type);
260 
261 
265  uint32_t
266  get_xid() const;
267 
268 
272  void
273  set_xid(uint32_t xid);
274 
275 public:
276 
277  friend std::ostream&
278  operator<< (std::ostream& os, cofmsg const& msg) {
279  os << indent(0) << "<cofmsg ";
280  os << "version: " << (int)msg.get_version() << " ";
281  os << "type: " << std::dec << (int)msg.get_type() << " ";
282  os << "length: " << (int)msg.get_length() << " ";
283  os << "xid: 0x" << std::hex << (unsigned int)msg.get_xid() << std::dec << " ";
284  os << ">" << std::endl;
285  return os;
286  };
287 
288  std::string
289  str() const {
290  std::stringstream ss;
291  ss << "OFP:{";
292  switch (get_version()) {
293  case rofl::openflow::OFP_VERSION_UNKNOWN: {
294  ss << "version: unknown, ";
295  } break;
296  case rofl::openflow10::OFP_VERSION: {
297  ss << "version: 1.0, ";
298  } break;
299  case rofl::openflow12::OFP_VERSION: {
300  ss << "version: 1.2, ";
301  } break;
302  case rofl::openflow13::OFP_VERSION: {
303  ss << "version: 1.3, ";
304  } break;
305  }
306  ss << "type: " << (int)get_type() << ", ";
307  ss << "len: " << (int)get_length() << ", ";
308  ss << "xid: 0x" << std::hex << (unsigned int)get_xid() << std::dec;
309  ss << "} ";
310  return ss.str();
311  };
312 };
313 
314 }; // end of namespace openflow
315 }; // end of namespace rofl
316 
317 #endif
size_t bodylen() const
Definition: cofmsg.h:212
static const char * type2desc(uint8_t ofp_version, uint8_t type)
Definition: cofmsg.cc:123
Definition: cofmsg.h:48
size_t memlen() const
Returns length of allocated memory area.
Definition: cmemory.cc:109
size_t framelen() const
Definition: cofmsg.h:198
virtual void pack(uint8_t *buf=(uint8_t *) 0, size_t buflen=0)
Definition: cofmsg.cc:236
cofmsg & operator=(cofmsg const &p)
Definition: cofmsg.cc:201
uint8_t * somem() const
Returns pointer to start of allocated memory area.
Definition: cmemory.cc:101
static const char * packet_info(uint8_t ofp_version)
Definition: cofmsg.cc:85
uint8_t * sobody() const
Definition: cofmsg.h:205
uint8_t * soframe() const
Definition: cofmsg.h:191
uint32_t get_xid() const
Definition: cofmsg.cc:291
virtual void unpack(uint8_t *buf, size_t buflen)
Definition: cofmsg.cc:250
Definition: openflow_common.h:58
virtual void reset()
Definition: cofmsg.cc:218
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual size_t length() const
Definition: cofmsg.cc:228
virtual ~cofmsg()
Definition: cofmsg.cc:191
Definition: cofmsg.h:49
Definition: logging.h:76
Definition: cofmsg.h:50
virtual void validate()
Definition: cofmsg.cc:268
void set_xid(uint32_t xid)
Definition: cofmsg.cc:301
Definition: cofmsg.h:85
Definition: croflexception.h:27
Definition: cofmsg.h:62