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