ofp-util: Simplify iteration through OpenFlow actions.
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OFP_UTIL_H
18 #define OFP_UTIL_H 1
19
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "classifier.h"
25 #include "flow.h"
26 #include "openflow/nicira-ext.h"
27 #include "openvswitch/types.h"
28
29 struct cls_rule;
30 struct ofpbuf;
31
32 /* Basic decoding and length validation of OpenFlow messages. */
33 enum ofputil_msg_code {
34     OFPUTIL_MSG_INVALID,
35
36     /* OFPT_* messages. */
37     OFPUTIL_OFPT_HELLO,
38     OFPUTIL_OFPT_ERROR,
39     OFPUTIL_OFPT_ECHO_REQUEST,
40     OFPUTIL_OFPT_ECHO_REPLY,
41     OFPUTIL_OFPT_FEATURES_REQUEST,
42     OFPUTIL_OFPT_FEATURES_REPLY,
43     OFPUTIL_OFPT_GET_CONFIG_REQUEST,
44     OFPUTIL_OFPT_GET_CONFIG_REPLY,
45     OFPUTIL_OFPT_SET_CONFIG,
46     OFPUTIL_OFPT_PACKET_IN,
47     OFPUTIL_OFPT_FLOW_REMOVED,
48     OFPUTIL_OFPT_PORT_STATUS,
49     OFPUTIL_OFPT_PACKET_OUT,
50     OFPUTIL_OFPT_FLOW_MOD,
51     OFPUTIL_OFPT_PORT_MOD,
52     OFPUTIL_OFPT_BARRIER_REQUEST,
53     OFPUTIL_OFPT_BARRIER_REPLY,
54     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
55     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
56
57     /* OFPST_* stat requests. */
58     OFPUTIL_OFPST_DESC_REQUEST,
59     OFPUTIL_OFPST_FLOW_REQUEST,
60     OFPUTIL_OFPST_AGGREGATE_REQUEST,
61     OFPUTIL_OFPST_TABLE_REQUEST,
62     OFPUTIL_OFPST_PORT_REQUEST,
63     OFPUTIL_OFPST_QUEUE_REQUEST,
64
65     /* OFPST_* stat replies. */
66     OFPUTIL_OFPST_DESC_REPLY,
67     OFPUTIL_OFPST_FLOW_REPLY,
68     OFPUTIL_OFPST_QUEUE_REPLY,
69     OFPUTIL_OFPST_PORT_REPLY,
70     OFPUTIL_OFPST_TABLE_REPLY,
71     OFPUTIL_OFPST_AGGREGATE_REPLY,
72
73     /* NXT_* messages. */
74     OFPUTIL_NXT_ROLE_REQUEST,
75     OFPUTIL_NXT_ROLE_REPLY,
76     OFPUTIL_NXT_SET_FLOW_FORMAT,
77     OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
78     OFPUTIL_NXT_FLOW_MOD,
79     OFPUTIL_NXT_FLOW_REMOVED,
80
81     /* NXST_* stat requests. */
82     OFPUTIL_NXST_FLOW_REQUEST,
83     OFPUTIL_NXST_AGGREGATE_REQUEST,
84
85     /* NXST_* stat replies. */
86     OFPUTIL_NXST_FLOW_REPLY,
87     OFPUTIL_NXST_AGGREGATE_REPLY
88 };
89
90 struct ofputil_msg_type;
91 int ofputil_decode_msg_type(const struct ofp_header *,
92                             const struct ofputil_msg_type **);
93 enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
94 const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
95
96 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
97  * and from IP bitmasks. */
98 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
99 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
100
101 /* Work with OpenFlow 1.0 ofp_match. */
102 void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
103 void ofputil_cls_rule_from_match(const struct ofp_match *,
104                                  unsigned int priority, struct cls_rule *);
105 void ofputil_normalize_rule(struct cls_rule *, enum nx_flow_format);
106 void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
107
108 /* dl_type translation between OpenFlow and 'struct flow' format. */
109 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
110 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
111
112 /* Flow formats. */
113 bool ofputil_flow_format_is_valid(enum nx_flow_format);
114 const char *ofputil_flow_format_to_string(enum nx_flow_format);
115 int ofputil_flow_format_from_string(const char *);
116 enum nx_flow_format ofputil_min_flow_format(const struct cls_rule *);
117
118 struct ofpbuf *ofputil_make_set_flow_format(enum nx_flow_format);
119
120 /* NXT_FLOW_MOD_TABLE_ID extension. */
121 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
122
123 /* Flow format independent flow_mod. */
124 struct flow_mod {
125     struct cls_rule cr;
126     ovs_be64 cookie;
127     uint8_t table_id;
128     uint16_t command;
129     uint16_t idle_timeout;
130     uint16_t hard_timeout;
131     uint32_t buffer_id;
132     uint16_t out_port;
133     uint16_t flags;
134     union ofp_action *actions;
135     size_t n_actions;
136 };
137
138 int ofputil_decode_flow_mod(struct flow_mod *, const struct ofp_header *,
139                             bool flow_mod_table_id);
140 struct ofpbuf *ofputil_encode_flow_mod(const struct flow_mod *,
141                                        enum nx_flow_format,
142                                        bool flow_mod_table_id);
143
144 /* Flow stats or aggregate stats request, independent of flow format. */
145 struct flow_stats_request {
146     bool aggregate;             /* Aggregate results? */
147     struct cls_rule match;
148     uint16_t out_port;
149     uint8_t table_id;
150 };
151
152 int ofputil_decode_flow_stats_request(struct flow_stats_request *,
153                                       const struct ofp_header *);
154 struct ofpbuf *ofputil_encode_flow_stats_request(
155     const struct flow_stats_request *, enum nx_flow_format);
156
157 /* Flow stats reply, independent of flow format. */
158 struct ofputil_flow_stats {
159     struct cls_rule rule;
160     ovs_be64 cookie;
161     uint8_t table_id;
162     uint32_t duration_sec;
163     uint32_t duration_nsec;
164     uint16_t idle_timeout;
165     uint16_t hard_timeout;
166     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
167     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
168     union ofp_action *actions;
169     size_t n_actions;
170 };
171
172 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
173                                     struct ofpbuf *msg);
174 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
175                                      struct list *replies);
176
177 /* Aggregate stats reply, independent of flow format. */
178 struct ofputil_aggregate_stats {
179     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
180     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
181     uint32_t flow_count;
182 };
183
184 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
185     const struct ofputil_aggregate_stats *stats,
186     const struct ofp_stats_msg *request);
187
188 /* Flow removed message, independent of flow format. */
189 struct ofputil_flow_removed {
190     struct cls_rule rule;
191     ovs_be64 cookie;
192     uint8_t reason;             /* One of OFPRR_*. */
193     uint32_t duration_sec;
194     uint32_t duration_nsec;
195     uint16_t idle_timeout;
196     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
197     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
198 };
199
200 int ofputil_decode_flow_removed(struct ofputil_flow_removed *,
201                                 const struct ofp_header *);
202 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
203                                            enum nx_flow_format);
204
205 /* Abstract packet-in message. */
206 struct ofputil_packet_in {
207     struct ofpbuf *packet;
208     uint16_t in_port;
209     uint8_t reason;             /* One of OFPR_*. */
210
211     uint32_t buffer_id;
212     int send_len;
213 };
214
215 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
216                                         struct ofpbuf *rw_packet);
217
218 /* OpenFlow protocol utility functions. */
219 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
220 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
221
222 void *make_openflow_xid(size_t openflow_len, uint8_t type,
223                         ovs_be32 xid, struct ofpbuf **);
224 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
225                      struct ofpbuf **);
226
227 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
228 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
229                        struct ofpbuf *);
230
231 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
232 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
233                     struct ofpbuf *);
234
235 void update_openflow_length(struct ofpbuf *);
236
237 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
238                                  uint32_t subtype, struct ofpbuf **);
239 void *ofputil_make_stats_reply(size_t openflow_len,
240                                const struct ofp_stats_msg *request,
241                                struct ofpbuf **);
242
243 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
244                                struct list *);
245 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
246 void *ofputil_append_stats_reply(size_t len, struct list *);
247
248 const void *ofputil_stats_body(const struct ofp_header *);
249 size_t ofputil_stats_body_len(const struct ofp_header *);
250
251 const void *ofputil_nxstats_body(const struct ofp_header *);
252 size_t ofputil_nxstats_body_len(const struct ofp_header *);
253
254 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
255                              size_t actions_len);
256 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
257                              uint16_t max_idle, size_t actions_len);
258 struct ofpbuf *make_del_flow(const struct cls_rule *);
259 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
260                                     uint32_t buffer_id, uint16_t out_port,
261                                     uint16_t max_idle);
262 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
263                               uint8_t reason,
264                               const struct ofpbuf *payload, int max_send_len);
265 struct ofpbuf *make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
266                                uint16_t in_port,
267                                const struct ofp_action_header *,
268                                size_t n_actions);
269 struct ofpbuf *make_buffered_packet_out(uint32_t buffer_id,
270                                         uint16_t in_port, uint16_t out_port);
271 struct ofpbuf *make_unbuffered_packet_out(const struct ofpbuf *packet,
272                                           uint16_t in_port, uint16_t out_port);
273 struct ofpbuf *make_echo_request(void);
274 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
275 \f
276 /* Actions. */
277
278 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
279
280 static inline union ofp_action *
281 ofputil_action_next(const union ofp_action *a)
282 {
283     return (void *) ((uint8_t *) a + ntohs(a->header.len));
284 }
285
286 static inline bool
287 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
288 {
289     uint16_t len = ntohs(a->header.len);
290     return (!(len % OFP_ACTION_ALIGN)
291             && len >= sizeof *a
292             && len / sizeof *a <= n_actions);
293 }
294
295 /* This macro is careful to check for actions with bad lengths. */
296 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)         \
297     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
298          (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT);             \
299          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
300           (ITER) = ofputil_action_next(ITER)))
301
302 /* This macro does not check for actions with bad lengths.  It should only be
303  * used with actions from trusted sources or with actions that have already
304  * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH).  */
305 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS)  \
306     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
307          (LEFT) > 0;                                                    \
308          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
309           (ITER) = ofputil_action_next(ITER)))
310
311 int validate_actions(const union ofp_action *, size_t n_actions,
312                      const struct flow *, int max_ports);
313 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
314
315 int ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
316                          union ofp_action **, size_t *);
317
318 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
319                            const union ofp_action *b, size_t n_b);
320 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
321 \f
322 /* OpenFlow vendors.
323  *
324  * These functions map OpenFlow 32-bit vendor IDs (as used in struct
325  * ofp_vendor_header) into 4-bit values to embed in an "int".  The 4-bit values
326  * are only used internally in Open vSwitch and never appear on the wire, so
327  * particular codes used are not important.
328  */
329
330 /* Vendor error numbers currently used in Open vSwitch. */
331 #define OFPUTIL_VENDORS                                     \
332     /*             vendor name              vendor value */ \
333     OFPUTIL_VENDOR(OFPUTIL_VENDOR_OPENFLOW, 0x00000000)     \
334     OFPUTIL_VENDOR(OFPUTIL_VENDOR_NICIRA,   NX_VENDOR_ID)
335
336 /* OFPUTIL_VENDOR_* definitions. */
337 enum ofputil_vendor_codes {
338 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) NAME,
339     OFPUTIL_VENDORS
340     OFPUTIL_N_VENDORS
341 #undef OFPUTIL_VENDOR
342 };
343 \f
344 /* Error codes.
345  *
346  * We embed system errno values and OpenFlow standard and vendor extension
347  * error codes into a single 31-bit space using the following encoding.
348  * (Bit 31 is unused and assumed 0 to avoid negative "int" values.)
349  *
350  *   30                                                   0
351  *  +------------------------------------------------------+
352  *  |                           0                          |  success
353  *  +------------------------------------------------------+
354  *
355  *   30 29                                                0
356  *  +--+---------------------------------------------------+
357  *  | 0|                    errno value                    |  errno value
358  *  +--+---------------------------------------------------+
359  *
360  *   30 29   26 25            16 15                       0
361  *  +--+-------+----------------+--------------------------+
362  *  | 1|   0   |      type      |           code           |  standard OpenFlow
363  *  +--+-------+----------------+--------------------------+  error
364  *
365  *   30 29   26 25            16 15                       0
366  *  +--+-------+----------------+--------------------------+  Nicira
367  *  | 1| vendor|      type      |           code           |  NXET_VENDOR
368  *  +--+-------+----------------+--------------------------+  error extension
369  *
370  * C and POSIX say that errno values are positive.  We assume that they are
371  * less than 2**29.  They are actually less than 65536 on at least Linux,
372  * FreeBSD, OpenBSD, and Windows.
373  *
374  * The 'vendor' field holds one of the OFPUTIL_VENDOR_* codes defined above.
375  * It must be nonzero.
376  *
377  * Negative values are not defined.
378  */
379
380 /* Currently 4 bits are allocated to the "vendor" field.  Make sure that all
381  * the vendor codes can fit. */
382 BUILD_ASSERT_DECL(OFPUTIL_N_VENDORS <= 16);
383
384 /* These are macro versions of the functions defined below.  The macro versions
385  * are intended for use in contexts where function calls are not allowed,
386  * e.g. static initializers and case labels. */
387 #define OFP_MKERR(TYPE, CODE) ((1 << 30) | ((TYPE) << 16) | (CODE))
388 #define OFP_MKERR_VENDOR(VENDOR, TYPE, CODE) \
389         ((1 << 30) | ((VENDOR) << 26) | ((TYPE) << 16) | (CODE))
390 #define OFP_MKERR_NICIRA(TYPE, CODE) \
391         OFP_MKERR_VENDOR(OFPUTIL_VENDOR_NICIRA, TYPE, CODE)
392
393 /* Returns the standard OpenFlow error with the specified 'type' and 'code' as
394  * an integer. */
395 static inline int
396 ofp_mkerr(uint16_t type, uint16_t code)
397 {
398     return OFP_MKERR(type, code);
399 }
400
401 /* Returns the OpenFlow vendor error with the specified 'vendor', 'type', and
402  * 'code' as an integer.  'vendor' must be an OFPUTIL_VENDOR_* constant. */
403 static inline int
404 ofp_mkerr_vendor(uint8_t vendor, uint16_t type, uint16_t code)
405 {
406     assert(vendor < OFPUTIL_N_VENDORS);
407     return OFP_MKERR_VENDOR(vendor, type, code);
408 }
409
410 /* Returns the OpenFlow vendor error with Nicira as vendor, with the specific
411  * 'type' and 'code', as an integer. */
412 static inline int
413 ofp_mkerr_nicira(uint16_t type, uint16_t code)
414 {
415     return OFP_MKERR_NICIRA(type, code);
416 }
417
418 /* Returns true if 'error' encodes an OpenFlow standard or vendor extension
419  * error codes as documented above. */
420 static inline bool
421 is_ofp_error(int error)
422 {
423     return (error & (1 << 30)) != 0;
424 }
425
426 /* Returns true if 'error' appears to be a system errno value. */
427 static inline bool
428 is_errno(int error)
429 {
430     return !is_ofp_error(error);
431 }
432
433 /* Returns the "vendor" part of the OpenFlow error code 'error' (which must be
434  * in the format explained above).  This is normally one of the
435  * OFPUTIL_VENDOR_* constants.  Returns OFPUTIL_VENDOR_OPENFLOW (0) for a
436  * standard OpenFlow error. */
437 static inline uint8_t
438 get_ofp_err_vendor(int error)
439 {
440     return (error >> 26) & 0xf;
441 }
442
443 /* Returns the "type" part of the OpenFlow error code 'error' (which must be in
444  * the format explained above). */
445 static inline uint16_t
446 get_ofp_err_type(int error)
447 {
448     return (error >> 16) & 0x3ff;
449 }
450
451 /* Returns the "code" part of the OpenFlow error code 'error' (which must be in
452  * the format explained above). */
453 static inline uint16_t
454 get_ofp_err_code(int error)
455 {
456     return error & 0xffff;
457 }
458
459 struct ofpbuf *ofputil_encode_error_msg(int error, const struct ofp_header *);
460 int ofputil_decode_error_msg(const struct ofp_header *, size_t *payload_ofs);
461
462 /* String versions of errors. */
463 void ofputil_format_error(struct ds *, int error);
464 char *ofputil_error_to_string(int error);
465
466 #endif /* ofp-util.h */