nicira-ext: Name the enum used for flow formats, to clarify code.
[cascardo/ovs.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 "flow.h"
25 #include "openflow/nicira-ext.h"
26 #include "openvswitch/types.h"
27
28 struct cls_rule;
29 struct ofpbuf;
30 struct ofp_action_header;
31
32 /* Alignment of ofp_actions. */
33 #define OFP_ACTION_ALIGN 8
34
35 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
36  * and from IP bitmasks. */
37 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
38 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
39
40 /* Work with OpenFlow 1.0 ofp_match. */
41 void ofputil_cls_rule_from_match(const struct ofp_match *,
42                                  unsigned int priority, enum nx_flow_format,
43                                  uint64_t cookie, struct cls_rule *);
44 void ofputil_cls_rule_to_match(const struct cls_rule *, enum nx_flow_format,
45                                struct ofp_match *);
46 void normalize_match(struct ofp_match *);
47 char *ofp_match_to_literal_string(const struct ofp_match *match);
48
49 /* OpenFlow protocol utility functions. */
50 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
51 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
52 void *make_openflow_xid(size_t openflow_len, uint8_t type,
53                         ovs_be32 xid, struct ofpbuf **);
54 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
55                      struct ofpbuf **);
56 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
57 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
58                        struct ofpbuf *);
59 void update_openflow_length(struct ofpbuf *);
60 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
61                              size_t actions_len);
62 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
63                              uint16_t max_idle, size_t actions_len);
64 struct ofpbuf *make_del_flow(const struct cls_rule *);
65 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
66                                     uint32_t buffer_id, uint16_t out_port,
67                                     uint16_t max_idle);
68 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
69                               uint8_t reason,
70                               const struct ofpbuf *payload, int max_send_len);
71 struct ofpbuf *make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
72                                uint16_t in_port,
73                                const struct ofp_action_header *,
74                                size_t n_actions);
75 struct ofpbuf *make_buffered_packet_out(uint32_t buffer_id,
76                                         uint16_t in_port, uint16_t out_port);
77 struct ofpbuf *make_unbuffered_packet_out(const struct ofpbuf *packet,
78                                           uint16_t in_port, uint16_t out_port);
79 struct ofpbuf *make_echo_request(void);
80 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
81 int check_ofp_message(const struct ofp_header *, uint8_t type, size_t size);
82 int check_ofp_message_array(const struct ofp_header *, uint8_t type,
83                             size_t size, size_t array_elt_size,
84                             size_t *n_array_elts);
85
86 struct flow_stats_iterator {
87     const uint8_t *pos, *end;
88 };
89 const struct ofp_flow_stats *flow_stats_first(struct flow_stats_iterator *,
90                                               const struct ofp_stats_reply *);
91 const struct ofp_flow_stats *flow_stats_next(struct flow_stats_iterator *);
92
93 struct actions_iterator {
94     const union ofp_action *pos, *end;
95 };
96 const union ofp_action *actions_first(struct actions_iterator *,
97                                       const union ofp_action *,
98                                       size_t n_actions);
99 const union ofp_action *actions_next(struct actions_iterator *);
100 int validate_actions(const union ofp_action *, size_t n_actions,
101                      const struct flow *, int max_ports);
102 bool action_outputs_to_port(const union ofp_action *, uint16_t port);
103
104 int ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
105                          union ofp_action **, size_t *);
106 \f
107 /* OpenFlow vendors.
108  *
109  * These functions map OpenFlow 32-bit vendor IDs (as used in struct
110  * ofp_vendor_header) into 4-bit values to embed in an "int".  The 4-bit values
111  * are only used internally in Open vSwitch and never appear on the wire, so
112  * particular codes used are not important.
113  */
114
115 /* Vendor error numbers currently used in Open vSwitch. */
116 #define OFPUTIL_VENDORS                                     \
117     /*             vendor name              vendor value */ \
118     OFPUTIL_VENDOR(OFPUTIL_VENDOR_OPENFLOW, 0x00000000)     \
119     OFPUTIL_VENDOR(OFPUTIL_VENDOR_NICIRA,   NX_VENDOR_ID)
120
121 /* OFPUTIL_VENDOR_* definitions. */
122 enum ofputil_vendor_codes {
123 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) NAME,
124     OFPUTIL_VENDORS
125     OFPUTIL_N_VENDORS
126 #undef OFPUTIL_VENDOR
127 };
128 \f
129 /* Error codes.
130  *
131  * We embed system errno values and OpenFlow standard and vendor extension
132  * error codes into a single 31-bit space using the following encoding.
133  * (Bit 31 is unused and assumed 0 to avoid negative "int" values.)
134  *
135  *   30                                                   0
136  *  +------------------------------------------------------+
137  *  |                           0                          |  success
138  *  +------------------------------------------------------+
139  *
140  *   30 29                                                0
141  *  +--+---------------------------------------------------+
142  *  | 0|                    errno value                    |  errno value
143  *  +--+---------------------------------------------------+
144  *
145  *   30 29   26 25            16 15                       0
146  *  +--+-------+----------------+--------------------------+
147  *  | 1|   0   |      type      |           code           |  standard OpenFlow
148  *  +--+-------+----------------+--------------------------+  error
149  *
150  *   30 29   26 25            16 15                       0
151  *  +--+-------+----------------+--------------------------+  Nicira
152  *  | 1| vendor|      type      |           code           |  NXET_VENDOR
153  *  +--+-------+----------------+--------------------------+  error extension
154  *
155  * C and POSIX say that errno values are positive.  We assume that they are
156  * less than 2**29.  They are actually less than 65536 on at least Linux,
157  * FreeBSD, OpenBSD, and Windows.
158  *
159  * The 'vendor' field holds one of the OFPUTIL_VENDOR_* codes defined above.
160  * It must be nonzero.
161  *
162  * Negative values are not defined.
163  */
164
165 /* Currently 4 bits are allocated to the "vendor" field.  Make sure that all
166  * the vendor codes can fit. */
167 BUILD_ASSERT_DECL(OFPUTIL_N_VENDORS <= 16);
168
169 /* These are macro versions of the functions defined below.  The macro versions
170  * are intended for use in contexts where function calls are not allowed,
171  * e.g. static initializers and case labels. */
172 #define OFP_MKERR(TYPE, CODE) ((1 << 30) | ((TYPE) << 16) | (CODE))
173 #define OFP_MKERR_VENDOR(VENDOR, TYPE, CODE) \
174         ((1 << 30) | ((VENDOR) << 26) | ((TYPE) << 16) | (CODE))
175 #define OFP_MKERR_NICIRA(TYPE, CODE) \
176         OFP_MKERR_VENDOR(OFPUTIL_VENDOR_NICIRA, TYPE, CODE)
177
178 /* Returns the standard OpenFlow error with the specified 'type' and 'code' as
179  * an integer. */
180 static inline int
181 ofp_mkerr(uint16_t type, uint16_t code)
182 {
183     return OFP_MKERR(type, code);
184 }
185
186 /* Returns the OpenFlow vendor error with the specified 'vendor', 'type', and
187  * 'code' as an integer.  'vendor' must be an OFPUTIL_VENDOR_* constant. */
188 static inline int
189 ofp_mkerr_vendor(uint8_t vendor, uint16_t type, uint16_t code)
190 {
191     assert(vendor < OFPUTIL_N_VENDORS);
192     return OFP_MKERR_VENDOR(vendor, type, code);
193 }
194
195 /* Returns the OpenFlow vendor error with Nicira as vendor, with the specific
196  * 'type' and 'code', as an integer. */
197 static inline int
198 ofp_mkerr_nicira(uint16_t type, uint16_t code)
199 {
200     return OFP_MKERR_NICIRA(type, code);
201 }
202
203 /* Returns true if 'error' encodes an OpenFlow standard or vendor extension
204  * error codes as documented above. */
205 static inline bool
206 is_ofp_error(int error)
207 {
208     return (error & (1 << 30)) != 0;
209 }
210
211 /* Returns true if 'error' appears to be a system errno value. */
212 static inline bool
213 is_errno(int error)
214 {
215     return !is_ofp_error(error);
216 }
217
218 /* Returns the "vendor" part of the OpenFlow error code 'error' (which must be
219  * in the format explained above).  This is normally one of the
220  * OFPUTIL_VENDOR_* constants.  Returns OFPUTIL_VENDOR_OPENFLOW (0) for a
221  * standard OpenFlow error. */
222 static inline uint8_t
223 get_ofp_err_vendor(int error)
224 {
225     return (error >> 26) & 0xf;
226 }
227
228 /* Returns the "type" part of the OpenFlow error code 'error' (which must be in
229  * the format explained above). */
230 static inline uint16_t
231 get_ofp_err_type(int error)
232 {
233     return (error >> 16) & 0x3ff;
234 }
235
236 /* Returns the "code" part of the OpenFlow error code 'error' (which must be in
237  * the format explained above). */
238 static inline uint16_t
239 get_ofp_err_code(int error)
240 {
241     return error & 0xffff;
242 }
243
244 struct ofpbuf *make_ofp_error_msg(int error, const struct ofp_header *);
245
246 #endif /* ofp-util.h */