]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-util.h
ofp-print: Implement printing for OFPUTIL_NXT_FLOW_MOD.
[mirror_ovs.git] / lib / ofp-util.h
CommitLineData
fa37b408
BP
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>
c1c9c9c4 21#include <stdbool.h>
fa37b408
BP
22#include <stddef.h>
23#include <stdint.h>
d1e2cf21 24#include "classifier.h"
fa37b408 25#include "flow.h"
492f7572 26#include "openflow/nicira-ext.h"
44381c1b 27#include "openvswitch/types.h"
fa37b408 28
d8ae4d67 29struct cls_rule;
fa37b408 30struct ofpbuf;
fa37b408 31
69b6be19
BP
32/* Alignment of ofp_actions. */
33#define OFP_ACTION_ALIGN 8
34
d1e2cf21
BP
35/* Basic decoding and length validation of OpenFlow messages. */
36enum ofputil_msg_code {
37 OFPUTIL_INVALID,
38
39 /* OFPT_* messages. */
40 OFPUTIL_OFPT_HELLO,
41 OFPUTIL_OFPT_ERROR,
42 OFPUTIL_OFPT_ECHO_REQUEST,
43 OFPUTIL_OFPT_ECHO_REPLY,
44 OFPUTIL_OFPT_FEATURES_REQUEST,
45 OFPUTIL_OFPT_FEATURES_REPLY,
46 OFPUTIL_OFPT_GET_CONFIG_REQUEST,
47 OFPUTIL_OFPT_GET_CONFIG_REPLY,
48 OFPUTIL_OFPT_SET_CONFIG,
49 OFPUTIL_OFPT_PACKET_IN,
50 OFPUTIL_OFPT_FLOW_REMOVED,
51 OFPUTIL_OFPT_PORT_STATUS,
52 OFPUTIL_OFPT_PACKET_OUT,
53 OFPUTIL_OFPT_FLOW_MOD,
54 OFPUTIL_OFPT_PORT_MOD,
55 OFPUTIL_OFPT_BARRIER_REQUEST,
56 OFPUTIL_OFPT_BARRIER_REPLY,
57 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
58 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
59
60 /* OFPST_* stat requests. */
61 OFPUTIL_OFPST_DESC_REQUEST,
62 OFPUTIL_OFPST_FLOW_REQUEST,
63 OFPUTIL_OFPST_AGGREGATE_REQUEST,
64 OFPUTIL_OFPST_TABLE_REQUEST,
65 OFPUTIL_OFPST_PORT_REQUEST,
66 OFPUTIL_OFPST_QUEUE_REQUEST,
67
68 /* OFPST_* stat replies. */
69 OFPUTIL_OFPST_DESC_REPLY,
70 OFPUTIL_OFPST_FLOW_REPLY,
71 OFPUTIL_OFPST_QUEUE_REPLY,
72 OFPUTIL_OFPST_PORT_REPLY,
73 OFPUTIL_OFPST_TABLE_REPLY,
74 OFPUTIL_OFPST_AGGREGATE_REPLY,
75
76 /* NXT_* messages. */
77 OFPUTIL_NXT_STATUS_REQUEST,
78 OFPUTIL_NXT_STATUS_REPLY,
79 OFPUTIL_NXT_TUN_ID_FROM_COOKIE,
80 OFPUTIL_NXT_ROLE_REQUEST,
81 OFPUTIL_NXT_ROLE_REPLY,
82 OFPUTIL_NXT_SET_FLOW_FORMAT,
83 OFPUTIL_NXT_FLOW_MOD,
84 OFPUTIL_NXT_FLOW_REMOVED,
85
86 /* NXST_* stat requests. */
87 OFPUTIL_NXST_FLOW_REQUEST,
88 OFPUTIL_NXST_AGGREGATE_REQUEST,
89
90 /* NXST_* stat replies. */
91 OFPUTIL_NXST_FLOW_REPLY,
92 OFPUTIL_NXST_AGGREGATE_REPLY
93};
94
95struct ofputil_msg_type;
96int ofputil_decode_msg_type(const struct ofp_header *,
97 const struct ofputil_msg_type **);
98enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
99const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
100
0596e897
BP
101/* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
102 * and from IP bitmasks. */
103ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
104int ofputil_netmask_to_wcbits(ovs_be32 netmask);
105
d8ae4d67
BP
106/* Work with OpenFlow 1.0 ofp_match. */
107void ofputil_cls_rule_from_match(const struct ofp_match *,
492f7572 108 unsigned int priority, enum nx_flow_format,
d8ae4d67 109 uint64_t cookie, struct cls_rule *);
492f7572 110void ofputil_cls_rule_to_match(const struct cls_rule *, enum nx_flow_format,
d8ae4d67
BP
111 struct ofp_match *);
112void normalize_match(struct ofp_match *);
113char *ofp_match_to_literal_string(const struct ofp_match *match);
114
7fa91113
BP
115/* Flow formats. */
116bool ofputil_flow_format_is_valid(enum nx_flow_format);
117const char *ofputil_flow_format_to_string(enum nx_flow_format);
118
2e4f5fcf
BP
119/* Flow format independent flow_mod. */
120struct flow_mod {
121 struct cls_rule cr;
122 ovs_be64 cookie;
123 uint16_t command;
124 uint16_t idle_timeout;
125 uint16_t hard_timeout;
126 uint32_t buffer_id;
127 uint16_t out_port;
128 uint16_t flags;
129 union ofp_action *actions;
130 size_t n_actions;
131};
132
133int ofputil_decode_flow_mod(struct flow_mod *, const struct ofp_header *,
134 enum nx_flow_format);
135struct ofpbuf *ofputil_encode_flow_mod(const struct flow_mod *,
136 enum nx_flow_format);
137
138/* Flow stats or aggregate stats request, independent of flow format. */
139struct flow_stats_request {
140 bool aggregate; /* Aggregate results? */
141 struct cls_rule match;
142 uint16_t out_port;
143 uint8_t table_id;
144};
145
146int ofputil_decode_flow_stats_request(struct flow_stats_request *,
147 const struct ofp_header *,
148 enum nx_flow_format);
149struct ofpbuf *ofputil_encode_flow_stats_request(
150 const struct flow_stats_request *, enum nx_flow_format);
151
fa37b408
BP
152/* OpenFlow protocol utility functions. */
153void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
0bd0c660 154void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
dfdfc8d4 155
fa37b408 156void *make_openflow_xid(size_t openflow_len, uint8_t type,
44381c1b
BP
157 ovs_be32 xid, struct ofpbuf **);
158void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
0bd0c660 159 struct ofpbuf **);
dfdfc8d4 160
fa37b408 161void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
44381c1b 162void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408 163 struct ofpbuf *);
dfdfc8d4
BP
164
165void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
166void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
167 struct ofpbuf *);
168
fa37b408 169void update_openflow_length(struct ofpbuf *);
d1e2cf21 170
dfdfc8d4
BP
171void *ofputil_make_stats_request(size_t body_len, uint16_t type,
172 struct ofpbuf **);
173void *ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype,
174 struct ofpbuf **);
175
d1e2cf21
BP
176const void *ofputil_stats_body(const struct ofp_header *);
177size_t ofputil_stats_body_len(const struct ofp_header *oh);
178
daa68e9f 179struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
fa37b408 180 size_t actions_len);
daa68e9f 181struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
fa37b408 182 uint16_t max_idle, size_t actions_len);
daa68e9f
BP
183struct ofpbuf *make_del_flow(const struct cls_rule *);
184struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
fa37b408
BP
185 uint32_t buffer_id, uint16_t out_port,
186 uint16_t max_idle);
187struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
188 uint8_t reason,
189 const struct ofpbuf *payload, int max_send_len);
190struct ofpbuf *make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
191 uint16_t in_port,
192 const struct ofp_action_header *,
193 size_t n_actions);
194struct ofpbuf *make_buffered_packet_out(uint32_t buffer_id,
195 uint16_t in_port, uint16_t out_port);
196struct ofpbuf *make_unbuffered_packet_out(const struct ofpbuf *packet,
197 uint16_t in_port, uint16_t out_port);
198struct ofpbuf *make_echo_request(void);
199struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
fa37b408
BP
200
201struct flow_stats_iterator {
202 const uint8_t *pos, *end;
203};
204const struct ofp_flow_stats *flow_stats_first(struct flow_stats_iterator *,
205 const struct ofp_stats_reply *);
206const struct ofp_flow_stats *flow_stats_next(struct flow_stats_iterator *);
207
208struct actions_iterator {
209 const union ofp_action *pos, *end;
210};
211const union ofp_action *actions_first(struct actions_iterator *,
212 const union ofp_action *,
213 size_t n_actions);
214const union ofp_action *actions_next(struct actions_iterator *);
215int validate_actions(const union ofp_action *, size_t n_actions,
f1defbf9 216 const struct flow *, int max_ports);
c1c9c9c4 217bool action_outputs_to_port(const union ofp_action *, uint16_t port);
3052b0c5
BP
218
219int ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
220 union ofp_action **, size_t *);
71ce9235 221\f
26c112c2 222/* OpenFlow vendors.
71ce9235 223 *
a23aab1f
BP
224 * These functions map OpenFlow 32-bit vendor IDs (as used in struct
225 * ofp_vendor_header) into 4-bit values to embed in an "int". The 4-bit values
226 * are only used internally in Open vSwitch and never appear on the wire, so
227 * particular codes used are not important.
228 */
229
26c112c2
BP
230/* Vendor error numbers currently used in Open vSwitch. */
231#define OFPUTIL_VENDORS \
232 /* vendor name vendor value */ \
233 OFPUTIL_VENDOR(OFPUTIL_VENDOR_OPENFLOW, 0x00000000) \
61d8d583 234 OFPUTIL_VENDOR(OFPUTIL_VENDOR_NICIRA, NX_VENDOR_ID)
26c112c2
BP
235
236/* OFPUTIL_VENDOR_* definitions. */
237enum ofputil_vendor_codes {
238#define OFPUTIL_VENDOR(NAME, VENDOR_ID) NAME,
239 OFPUTIL_VENDORS
240 OFPUTIL_N_VENDORS
241#undef OFPUTIL_VENDOR
242};
243\f
244/* Error codes.
71ce9235 245 *
26c112c2
BP
246 * We embed system errno values and OpenFlow standard and vendor extension
247 * error codes into a single 31-bit space using the following encoding.
248 * (Bit 31 is unused and assumed 0 to avoid negative "int" values.)
71ce9235 249 *
a23aab1f 250 * 30 0
26c112c2
BP
251 * +------------------------------------------------------+
252 * | 0 | success
253 * +------------------------------------------------------+
71ce9235 254 *
26c112c2
BP
255 * 30 29 0
256 * +--+---------------------------------------------------+
a23aab1f 257 * | 0| errno value | errno value
26c112c2 258 * +--+---------------------------------------------------+
71ce9235 259 *
26c112c2
BP
260 * 30 29 26 25 16 15 0
261 * +--+-------+----------------+--------------------------+
a23aab1f 262 * | 1| 0 | type | code | standard OpenFlow
26c112c2 263 * +--+-------+----------------+--------------------------+ error
71ce9235 264 *
26c112c2
BP
265 * 30 29 26 25 16 15 0
266 * +--+-------+----------------+--------------------------+ Nicira
267 * | 1| vendor| type | code | NXET_VENDOR
268 * +--+-------+----------------+--------------------------+ error extension
71ce9235 269 *
26c112c2
BP
270 * C and POSIX say that errno values are positive. We assume that they are
271 * less than 2**29. They are actually less than 65536 on at least Linux,
272 * FreeBSD, OpenBSD, and Windows.
71ce9235 273 *
26c112c2
BP
274 * The 'vendor' field holds one of the OFPUTIL_VENDOR_* codes defined above.
275 * It must be nonzero.
71ce9235 276 *
26c112c2 277 * Negative values are not defined.
71ce9235 278 */
fa37b408 279
26c112c2
BP
280/* Currently 4 bits are allocated to the "vendor" field. Make sure that all
281 * the vendor codes can fit. */
282BUILD_ASSERT_DECL(OFPUTIL_N_VENDORS <= 16);
283
d48a5910
BP
284/* These are macro versions of the functions defined below. The macro versions
285 * are intended for use in contexts where function calls are not allowed,
286 * e.g. static initializers and case labels. */
287#define OFP_MKERR(TYPE, CODE) ((1 << 30) | ((TYPE) << 16) | (CODE))
288#define OFP_MKERR_VENDOR(VENDOR, TYPE, CODE) \
289 ((1 << 30) | ((VENDOR) << 26) | ((TYPE) << 16) | (CODE))
290#define OFP_MKERR_NICIRA(TYPE, CODE) \
291 OFP_MKERR_VENDOR(OFPUTIL_VENDOR_NICIRA, TYPE, CODE)
292
26c112c2
BP
293/* Returns the standard OpenFlow error with the specified 'type' and 'code' as
294 * an integer. */
fa37b408
BP
295static inline int
296ofp_mkerr(uint16_t type, uint16_t code)
297{
d48a5910 298 return OFP_MKERR(type, code);
26c112c2
BP
299}
300
301/* Returns the OpenFlow vendor error with the specified 'vendor', 'type', and
302 * 'code' as an integer. 'vendor' must be an OFPUTIL_VENDOR_* constant. */
303static inline int
304ofp_mkerr_vendor(uint8_t vendor, uint16_t type, uint16_t code)
305{
306 assert(vendor < OFPUTIL_N_VENDORS);
d48a5910 307 return OFP_MKERR_VENDOR(vendor, type, code);
fa37b408
BP
308}
309
26c112c2
BP
310/* Returns the OpenFlow vendor error with Nicira as vendor, with the specific
311 * 'type' and 'code', as an integer. */
312static inline int
313ofp_mkerr_nicira(uint16_t type, uint16_t code)
314{
d48a5910 315 return OFP_MKERR_NICIRA(type, code);
26c112c2
BP
316}
317
318/* Returns true if 'error' encodes an OpenFlow standard or vendor extension
319 * error codes as documented above. */
71ce9235
BP
320static inline bool
321is_ofp_error(int error)
322{
26c112c2 323 return (error & (1 << 30)) != 0;
71ce9235
BP
324}
325
326/* Returns true if 'error' appears to be a system errno value. */
327static inline bool
328is_errno(int error)
329{
26c112c2
BP
330 return !is_ofp_error(error);
331}
332
333/* Returns the "vendor" part of the OpenFlow error code 'error' (which must be
334 * in the format explained above). This is normally one of the
335 * OFPUTIL_VENDOR_* constants. Returns OFPUTIL_VENDOR_OPENFLOW (0) for a
336 * standard OpenFlow error. */
337static inline uint8_t
338get_ofp_err_vendor(int error)
339{
340 return (error >> 26) & 0xf;
71ce9235
BP
341}
342
343/* Returns the "type" part of the OpenFlow error code 'error' (which must be in
344 * the format explained above). */
345static inline uint16_t
346get_ofp_err_type(int error)
347{
26c112c2 348 return (error >> 16) & 0x3ff;
71ce9235
BP
349}
350
351/* Returns the "code" part of the OpenFlow error code 'error' (which must be in
352 * the format explained above). */
353static inline uint16_t
354get_ofp_err_code(int error)
355{
356 return error & 0xffff;
357}
358
26c112c2
BP
359struct ofpbuf *make_ofp_error_msg(int error, const struct ofp_header *);
360
fa37b408 361#endif /* ofp-util.h */