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