079f13f8dcc0708d9d24525fc3232a4c6fa53bf4
[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 struct flow *,
38                              size_t actions_len);
39 struct ofpbuf *make_add_flow(const struct flow *, uint32_t buffer_id,
40                              uint16_t max_idle, size_t actions_len);
41 struct ofpbuf *make_del_flow(const struct flow *);
42 struct ofpbuf *make_add_simple_flow(const struct flow *,
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 flow_stats_iterator {
66     const uint8_t *pos, *end;
67 };
68 const struct ofp_flow_stats *flow_stats_first(struct flow_stats_iterator *,
69                                               const struct ofp_stats_reply *);
70 const struct ofp_flow_stats *flow_stats_next(struct flow_stats_iterator *);
71
72 struct actions_iterator {
73     const union ofp_action *pos, *end;
74 };
75 const union ofp_action *actions_first(struct actions_iterator *,
76                                       const union ofp_action *,
77                                       size_t n_actions);
78 const union ofp_action *actions_next(struct actions_iterator *);
79 int validate_actions(const union ofp_action *, size_t n_actions,
80                      int max_ports);
81 bool action_outputs_to_port(const union ofp_action *, uint16_t port);
82
83 void normalize_match(struct ofp_match *);
84 char *ofp_match_to_literal_string(const struct ofp_match *match);
85 \f
86 /* OpenFlow errors.
87  *
88  * OpenFlow errors have two 16-bit parts: a "type" and a "code".  A "type" has
89  * a unique meaning.  The "code" values are different for each "type".
90  *
91  * We embed OpenFlow errors in the same space as errno values by shifting
92  * 'type' left 16 bits and adding the 'code'.  An "int" value is thus broken
93  * into a few different ranges:
94  *
95  *      - 0: success.
96  *
97  *      - 1...65535: system errno values.
98  *
99  *        The assumption that system errno values are less than 65536 is true
100  *        on at least Linux, FreeBSD, OpenBSD, and Windows.  RFC 1813 defines
101  *        NFSv3-specific errno codes starting at 10000, another hint that this
102  *        is a reasonable assumption.
103  *
104  *        C and POSIX say that errno values are positive.
105  *
106  *      - 65536...INT_MAX: OpenFlow errors.
107  *
108  *        In OpenFlow, a "type" of 0 is valid, but it corresponds to
109  *        OFPET_HELLO_FAILED.  That's not a general-purpose error: only the
110  *        vconn library would ever care to send it.  So we ignore it.
111  *
112  *      - negative values: not used.
113  */
114
115 /* Returns the OpenFlow error with the specified 'type' and 'code' as an
116  * integer. */
117 static inline int
118 ofp_mkerr(uint16_t type, uint16_t code)
119 {
120     assert(type > 0 && type <= 0x7fff);
121     return (type << 16) | code;
122 }
123
124 /* Returns true if 'error' is in the range of values used as OpenFlow error
125  * codes as explained above. */
126 static inline bool
127 is_ofp_error(int error)
128 {
129     return error >= 0x10000;
130 }
131
132 /* Returns true if 'error' appears to be a system errno value. */
133 static inline bool
134 is_errno(int error)
135 {
136     return error < 0x10000;
137 }
138
139 /* Returns the "type" part of the OpenFlow error code 'error' (which must be in
140  * the format explained above). */
141 static inline uint16_t
142 get_ofp_err_type(int error)
143 {
144     return error >> 16;
145 }
146
147 /* Returns the "code" part of the OpenFlow error code 'error' (which must be in
148  * the format explained above). */
149 static inline uint16_t
150 get_ofp_err_code(int error)
151 {
152     return error & 0xffff;
153 }
154
155 #endif /* ofp-util.h */