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