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