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