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