2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "classifier.h"
26 #include "openflow/nicira-ext.h"
27 #include "openvswitch/types.h"
32 /* Basic decoding and length validation of OpenFlow messages. */
33 enum ofputil_msg_code {
36 /* OFPT_* messages. */
39 OFPUTIL_OFPT_ECHO_REQUEST,
40 OFPUTIL_OFPT_ECHO_REPLY,
41 OFPUTIL_OFPT_FEATURES_REQUEST,
42 OFPUTIL_OFPT_FEATURES_REPLY,
43 OFPUTIL_OFPT_GET_CONFIG_REQUEST,
44 OFPUTIL_OFPT_GET_CONFIG_REPLY,
45 OFPUTIL_OFPT_SET_CONFIG,
46 OFPUTIL_OFPT_PACKET_IN,
47 OFPUTIL_OFPT_FLOW_REMOVED,
48 OFPUTIL_OFPT_PORT_STATUS,
49 OFPUTIL_OFPT_PACKET_OUT,
50 OFPUTIL_OFPT_FLOW_MOD,
51 OFPUTIL_OFPT_PORT_MOD,
52 OFPUTIL_OFPT_BARRIER_REQUEST,
53 OFPUTIL_OFPT_BARRIER_REPLY,
54 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
55 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
57 /* OFPST_* stat requests. */
58 OFPUTIL_OFPST_DESC_REQUEST,
59 OFPUTIL_OFPST_FLOW_REQUEST,
60 OFPUTIL_OFPST_AGGREGATE_REQUEST,
61 OFPUTIL_OFPST_TABLE_REQUEST,
62 OFPUTIL_OFPST_PORT_REQUEST,
63 OFPUTIL_OFPST_QUEUE_REQUEST,
65 /* OFPST_* stat replies. */
66 OFPUTIL_OFPST_DESC_REPLY,
67 OFPUTIL_OFPST_FLOW_REPLY,
68 OFPUTIL_OFPST_QUEUE_REPLY,
69 OFPUTIL_OFPST_PORT_REPLY,
70 OFPUTIL_OFPST_TABLE_REPLY,
71 OFPUTIL_OFPST_AGGREGATE_REPLY,
74 OFPUTIL_NXT_ROLE_REQUEST,
75 OFPUTIL_NXT_ROLE_REPLY,
76 OFPUTIL_NXT_SET_FLOW_FORMAT,
77 OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
79 OFPUTIL_NXT_FLOW_REMOVED,
80 OFPUTIL_NXT_SET_PACKET_IN_FORMAT,
81 OFPUTIL_NXT_PACKET_IN,
83 OFPUTIL_NXT_SET_ASYNC_CONFIG,
84 OFPUTIL_NXT_SET_CONTROLLER_ID,
86 /* NXST_* stat requests. */
87 OFPUTIL_NXST_FLOW_REQUEST,
88 OFPUTIL_NXST_AGGREGATE_REQUEST,
90 /* NXST_* stat replies. */
91 OFPUTIL_NXST_FLOW_REPLY,
92 OFPUTIL_NXST_AGGREGATE_REPLY
95 struct ofputil_msg_type;
96 enum ofperr ofputil_decode_msg_type(const struct ofp_header *,
97 const struct ofputil_msg_type **);
98 enum ofperr ofputil_decode_msg_type_partial(const struct ofp_header *,
100 const struct ofputil_msg_type **);
101 enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
102 const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
105 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port);
106 ovs_be32 ofputil_port_to_ofp11(uint16_t ofp10_port);
108 enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
109 bool ofputil_port_from_string(const char *, uint16_t *port);
110 void ofputil_format_port(uint16_t port, struct ds *);
112 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
113 * and from IP bitmasks. */
114 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
115 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
119 * These are arranged from most portable to least portable, or alternatively
120 * from least powerful to most powerful. Formats earlier on the list are more
121 * likely to be understood for the purpose of making requests, but formats
122 * later on the list are more likely to accurately describe a flow within a
125 * On any given OpenFlow connection, a single protocol is in effect at any
126 * given time. These values use separate bits only because that makes it easy
127 * to test whether a particular protocol is within a given set of protocols and
128 * to implement set union and intersection.
130 enum ofputil_protocol {
131 /* OpenFlow 1.0-based protocols. */
132 OFPUTIL_P_OF10 = 1 << 0, /* OpenFlow 1.0 flow format. */
133 OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
134 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
136 /* OpenFlow 1.0 with NXM-based flow formats. */
137 OFPUTIL_P_NXM = 1 << 2, /* Nicira extended match. */
138 OFPUTIL_P_NXM_TID = 1 << 3, /* NXM + flow_mod_table_id extension. */
139 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
142 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
144 /* Protocols in which a specific table may be specified in flow_mods. */
145 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
148 /* Protocols to use for flow dumps, from most to least preferred. */
149 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
150 extern size_t ofputil_n_flow_dump_protocols;
152 enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
153 bool ofputil_protocol_is_valid(enum ofputil_protocol);
154 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
156 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
157 enum ofputil_protocol ofputil_protocol_set_base(
158 enum ofputil_protocol cur, enum ofputil_protocol new_base);
160 const char *ofputil_protocol_to_string(enum ofputil_protocol);
161 char *ofputil_protocols_to_string(enum ofputil_protocol);
162 enum ofputil_protocol ofputil_protocols_from_string(const char *);
163 enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
165 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
166 enum ofputil_protocol want,
167 enum ofputil_protocol *next);
170 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
171 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
172 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
173 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
175 /* Work with OpenFlow 1.0 ofp_match. */
176 void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
177 void ofputil_cls_rule_from_match(const struct ofp_match *,
178 unsigned int priority, struct cls_rule *);
179 void ofputil_normalize_rule(struct cls_rule *);
180 void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
182 /* dl_type translation between OpenFlow and 'struct flow' format. */
183 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
184 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
187 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
188 int ofputil_packet_in_format_from_string(const char *);
189 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
190 struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
192 /* NXT_FLOW_MOD_TABLE_ID extension. */
193 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
195 /* Protocol-independent flow_mod. */
196 struct ofputil_flow_mod {
199 ovs_be64 cookie_mask;
202 uint16_t idle_timeout;
203 uint16_t hard_timeout;
207 union ofp_action *actions;
211 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
212 const struct ofp_header *,
213 enum ofputil_protocol);
214 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
215 enum ofputil_protocol);
217 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
218 const struct ofputil_flow_mod *fms, size_t n_fms);
220 /* Flow stats or aggregate stats request, independent of protocol. */
221 struct ofputil_flow_stats_request {
222 bool aggregate; /* Aggregate results? */
223 struct cls_rule match;
225 ovs_be64 cookie_mask;
230 enum ofperr ofputil_decode_flow_stats_request(
231 struct ofputil_flow_stats_request *, const struct ofp_header *);
232 struct ofpbuf *ofputil_encode_flow_stats_request(
233 const struct ofputil_flow_stats_request *, enum ofputil_protocol);
234 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
235 const struct ofputil_flow_stats_request *);
237 /* Flow stats reply, independent of protocol. */
238 struct ofputil_flow_stats {
239 struct cls_rule rule;
242 uint32_t duration_sec;
243 uint32_t duration_nsec;
244 uint16_t idle_timeout;
245 uint16_t hard_timeout;
246 int idle_age; /* Seconds since last packet, -1 if unknown. */
247 int hard_age; /* Seconds since last change, -1 if unknown. */
248 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
249 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
250 union ofp_action *actions;
254 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
256 bool flow_age_extension);
257 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
258 struct list *replies);
260 /* Aggregate stats reply, independent of protocol. */
261 struct ofputil_aggregate_stats {
262 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
263 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
267 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
268 const struct ofputil_aggregate_stats *stats,
269 const struct ofp_stats_msg *request);
271 /* Flow removed message, independent of protocol. */
272 struct ofputil_flow_removed {
273 struct cls_rule rule;
275 uint8_t reason; /* One of OFPRR_*. */
276 uint32_t duration_sec;
277 uint32_t duration_nsec;
278 uint16_t idle_timeout;
279 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
280 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
283 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
284 const struct ofp_header *);
285 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
286 enum ofputil_protocol);
288 /* Abstract packet-in message. */
289 struct ofputil_packet_in {
293 enum ofp_packet_in_reason reason; /* One of OFPRR_*. */
294 uint16_t controller_id; /* Controller ID to send to. */
300 uint16_t total_len; /* Full length of frame. */
302 struct flow_metadata fmd; /* Metadata at creation time. */
305 int ofputil_decode_packet_in(struct ofputil_packet_in *,
306 const struct ofp_header *);
307 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
308 enum nx_packet_in_format);
310 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
311 bool ofputil_packet_in_reason_from_string(const char *,
312 enum ofp_packet_in_reason *);
314 /* Abstract packet-out message. */
315 struct ofputil_packet_out {
316 const void *packet; /* Packet data, if buffer_id == UINT32_MAX. */
317 size_t packet_len; /* Length of packet data in bytes. */
318 uint32_t buffer_id; /* Buffer id or UINT32_MAX if no buffer. */
319 uint16_t in_port; /* Packet's input port or OFPP_NONE. */
320 union ofp_action *actions; /* Actions. */
321 size_t n_actions; /* Number of elements in 'actions' array. */
324 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
325 const struct ofp_packet_out *);
326 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
328 /* OpenFlow protocol utility functions. */
329 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
330 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
332 void *make_openflow_xid(size_t openflow_len, uint8_t type,
333 ovs_be32 xid, struct ofpbuf **);
334 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
337 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
338 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
341 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
342 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
345 void update_openflow_length(struct ofpbuf *);
347 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
348 uint32_t subtype, struct ofpbuf **);
349 void *ofputil_make_stats_reply(size_t openflow_len,
350 const struct ofp_stats_msg *request,
353 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
355 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
356 void *ofputil_append_stats_reply(size_t len, struct list *);
358 const void *ofputil_stats_body(const struct ofp_header *);
359 size_t ofputil_stats_body_len(const struct ofp_header *);
361 const void *ofputil_nxstats_body(const struct ofp_header *);
362 size_t ofputil_nxstats_body_len(const struct ofp_header *);
364 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
366 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
367 uint16_t max_idle, size_t actions_len);
368 struct ofpbuf *make_del_flow(const struct cls_rule *);
369 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
370 uint32_t buffer_id, uint16_t out_port,
372 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
374 const struct ofpbuf *payload, int max_send_len);
375 struct ofpbuf *make_echo_request(void);
376 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
378 struct ofpbuf *ofputil_encode_barrier_request(void);
380 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
381 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
385 /* The type of an action.
387 * For each implemented OFPAT_* and NXAST_* action type, there is a
388 * corresponding constant prefixed with OFPUTIL_, e.g.:
390 * OFPUTIL_OFPAT_OUTPUT
391 * OFPUTIL_OFPAT_SET_VLAN_VID
392 * OFPUTIL_OFPAT_SET_VLAN_PCP
393 * OFPUTIL_OFPAT_STRIP_VLAN
394 * OFPUTIL_OFPAT_SET_DL_SRC
395 * OFPUTIL_OFPAT_SET_DL_DST
396 * OFPUTIL_OFPAT_SET_NW_SRC
397 * OFPUTIL_OFPAT_SET_NW_DST
398 * OFPUTIL_OFPAT_SET_NW_TOS
399 * OFPUTIL_OFPAT_SET_TP_SRC
400 * OFPUTIL_OFPAT_SET_TP_DST
401 * OFPUTIL_OFPAT_ENQUEUE
402 * OFPUTIL_NXAST_RESUBMIT
403 * OFPUTIL_NXAST_SET_TUNNEL
404 * OFPUTIL_NXAST_SET_QUEUE
405 * OFPUTIL_NXAST_POP_QUEUE
406 * OFPUTIL_NXAST_REG_MOVE
407 * OFPUTIL_NXAST_REG_LOAD
409 * OFPUTIL_NXAST_SET_TUNNEL64
410 * OFPUTIL_NXAST_MULTIPATH
411 * OFPUTIL_NXAST_AUTOPATH
412 * OFPUTIL_NXAST_BUNDLE
413 * OFPUTIL_NXAST_BUNDLE_LOAD
414 * OFPUTIL_NXAST_RESUBMIT_TABLE
415 * OFPUTIL_NXAST_OUTPUT_REG
416 * OFPUTIL_NXAST_LEARN
417 * OFPUTIL_NXAST_DEC_TTL
418 * OFPUTIL_NXAST_FIN_TIMEOUT
420 * (The above list helps developers who want to "grep" for these definitions.)
422 enum ofputil_action_code {
423 #define OFPAT_ACTION(ENUM, STRUCT, NAME) OFPUTIL_##ENUM,
424 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
425 #include "ofp-util.def"
428 /* The number of values of "enum ofputil_action_code". */
430 #define OFPAT_ACTION(ENUM, STRUCT, NAME) + 1
431 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
432 OFPUTIL_N_ACTIONS = 0
433 #include "ofp-util.def"
436 int ofputil_decode_action(const union ofp_action *);
437 enum ofputil_action_code ofputil_decode_action_unsafe(
438 const union ofp_action *);
440 int ofputil_action_code_from_name(const char *);
442 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
444 /* For each OpenFlow action <ENUM> that has a corresponding action structure
445 * struct <STRUCT>, this defines two functions:
447 * void ofputil_init_<ENUM>(struct <STRUCT> *action);
449 * Initializes the parts of 'action' that identify it as having type <ENUM>
450 * and length 'sizeof *action' and zeros the rest. For actions that have
451 * variable length, the length used and cleared is that of struct <STRUCT>.
453 * struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
455 * Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
456 * initializes it with ofputil_init_<ENUM>(), and returns it.
458 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
459 void ofputil_init_##ENUM(struct STRUCT *); \
460 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
461 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
462 void ofputil_init_##ENUM(struct STRUCT *); \
463 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
464 #include "ofp-util.def"
466 #define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */
468 static inline union ofp_action *
469 ofputil_action_next(const union ofp_action *a)
471 return ((union ofp_action *) (void *)
472 ((uint8_t *) a + ntohs(a->header.len)));
476 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
478 uint16_t len = ntohs(a->header.len);
479 return (!(len % OFP_ACTION_ALIGN)
481 && len / sizeof *a <= n_actions);
484 /* This macro is careful to check for actions with bad lengths. */
485 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
486 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
487 (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT); \
488 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
489 (ITER) = ofputil_action_next(ITER)))
491 /* This macro does not check for actions with bad lengths. It should only be
492 * used with actions from trusted sources or with actions that have already
493 * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH). */
494 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS) \
495 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
497 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
498 (ITER) = ofputil_action_next(ITER)))
500 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
501 const struct flow *, int max_ports);
502 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
504 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
505 union ofp_action **, size_t *);
507 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
508 const union ofp_action *b, size_t n_b);
509 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
511 /* Handy utility for parsing flows and actions. */
512 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
514 #endif /* ofp-util.h */