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"
27 #include "openflow/nicira-ext.h"
28 #include "openvswitch/types.h"
33 /* Basic decoding and length validation of OpenFlow messages. */
34 enum ofputil_msg_code {
37 /* OFPT_* messages. */
40 OFPUTIL_OFPT_ECHO_REQUEST,
41 OFPUTIL_OFPT_ECHO_REPLY,
42 OFPUTIL_OFPT_FEATURES_REQUEST,
43 OFPUTIL_OFPT_FEATURES_REPLY,
44 OFPUTIL_OFPT_GET_CONFIG_REQUEST,
45 OFPUTIL_OFPT_GET_CONFIG_REPLY,
46 OFPUTIL_OFPT_SET_CONFIG,
47 OFPUTIL_OFPT_PACKET_IN,
48 OFPUTIL_OFPT_FLOW_REMOVED,
49 OFPUTIL_OFPT_PORT_STATUS,
50 OFPUTIL_OFPT_PACKET_OUT,
51 OFPUTIL_OFPT_FLOW_MOD,
52 OFPUTIL_OFPT_PORT_MOD,
53 OFPUTIL_OFPT_BARRIER_REQUEST,
54 OFPUTIL_OFPT_BARRIER_REPLY,
55 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
56 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
58 /* OFPST_* stat requests. */
59 OFPUTIL_OFPST_DESC_REQUEST,
60 OFPUTIL_OFPST_FLOW_REQUEST,
61 OFPUTIL_OFPST_AGGREGATE_REQUEST,
62 OFPUTIL_OFPST_TABLE_REQUEST,
63 OFPUTIL_OFPST_PORT_REQUEST,
64 OFPUTIL_OFPST_QUEUE_REQUEST,
66 /* OFPST_* stat replies. */
67 OFPUTIL_OFPST_DESC_REPLY,
68 OFPUTIL_OFPST_FLOW_REPLY,
69 OFPUTIL_OFPST_QUEUE_REPLY,
70 OFPUTIL_OFPST_PORT_REPLY,
71 OFPUTIL_OFPST_TABLE_REPLY,
72 OFPUTIL_OFPST_AGGREGATE_REPLY,
75 OFPUTIL_NXT_ROLE_REQUEST,
76 OFPUTIL_NXT_ROLE_REPLY,
77 OFPUTIL_NXT_SET_FLOW_FORMAT,
78 OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
80 OFPUTIL_NXT_FLOW_REMOVED,
81 OFPUTIL_NXT_SET_PACKET_IN_FORMAT,
82 OFPUTIL_NXT_PACKET_IN,
84 OFPUTIL_NXT_SET_ASYNC_CONFIG,
85 OFPUTIL_NXT_SET_CONTROLLER_ID,
87 /* NXST_* stat requests. */
88 OFPUTIL_NXST_FLOW_REQUEST,
89 OFPUTIL_NXST_AGGREGATE_REQUEST,
91 /* NXST_* stat replies. */
92 OFPUTIL_NXST_FLOW_REPLY,
93 OFPUTIL_NXST_AGGREGATE_REPLY
96 struct ofputil_msg_type;
97 enum ofperr ofputil_decode_msg_type(const struct ofp_header *,
98 const struct ofputil_msg_type **);
99 enum ofperr ofputil_decode_msg_type_partial(const struct ofp_header *,
101 const struct ofputil_msg_type **);
102 enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
103 const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
106 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port);
107 ovs_be32 ofputil_port_to_ofp11(uint16_t ofp10_port);
109 enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
110 bool ofputil_port_from_string(const char *, uint16_t *port);
111 void ofputil_format_port(uint16_t port, struct ds *);
113 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
114 * and from IP bitmasks. */
115 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
116 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
120 * These are arranged from most portable to least portable, or alternatively
121 * from least powerful to most powerful. Formats earlier on the list are more
122 * likely to be understood for the purpose of making requests, but formats
123 * later on the list are more likely to accurately describe a flow within a
126 * On any given OpenFlow connection, a single protocol is in effect at any
127 * given time. These values use separate bits only because that makes it easy
128 * to test whether a particular protocol is within a given set of protocols and
129 * to implement set union and intersection.
131 enum ofputil_protocol {
132 /* OpenFlow 1.0-based protocols. */
133 OFPUTIL_P_OF10 = 1 << 0, /* OpenFlow 1.0 flow format. */
134 OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
135 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
137 /* OpenFlow 1.0 with NXM-based flow formats. */
138 OFPUTIL_P_NXM = 1 << 2, /* Nicira extended match. */
139 OFPUTIL_P_NXM_TID = 1 << 3, /* NXM + flow_mod_table_id extension. */
140 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
143 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
145 /* Protocols in which a specific table may be specified in flow_mods. */
146 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
149 /* Protocols to use for flow dumps, from most to least preferred. */
150 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
151 extern size_t ofputil_n_flow_dump_protocols;
153 enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
154 bool ofputil_protocol_is_valid(enum ofputil_protocol);
155 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
157 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
158 enum ofputil_protocol ofputil_protocol_set_base(
159 enum ofputil_protocol cur, enum ofputil_protocol new_base);
161 const char *ofputil_protocol_to_string(enum ofputil_protocol);
162 char *ofputil_protocols_to_string(enum ofputil_protocol);
163 enum ofputil_protocol ofputil_protocols_from_string(const char *);
164 enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
166 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
167 enum ofputil_protocol want,
168 enum ofputil_protocol *next);
171 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
172 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
173 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
174 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
176 /* Work with OpenFlow 1.0 ofp_match. */
177 void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
178 void ofputil_cls_rule_from_match(const struct ofp_match *,
179 unsigned int priority, struct cls_rule *);
180 void ofputil_normalize_rule(struct cls_rule *);
181 void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
183 /* dl_type translation between OpenFlow and 'struct flow' format. */
184 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
185 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
188 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
189 int ofputil_packet_in_format_from_string(const char *);
190 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
191 struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
193 /* NXT_FLOW_MOD_TABLE_ID extension. */
194 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
196 /* Protocol-independent flow_mod. */
197 struct ofputil_flow_mod {
200 ovs_be64 cookie_mask;
203 uint16_t idle_timeout;
204 uint16_t hard_timeout;
208 union ofp_action *actions;
212 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
213 const struct ofp_header *,
214 enum ofputil_protocol);
215 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
216 enum ofputil_protocol);
218 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
219 const struct ofputil_flow_mod *fms, size_t n_fms);
221 /* Flow stats or aggregate stats request, independent of protocol. */
222 struct ofputil_flow_stats_request {
223 bool aggregate; /* Aggregate results? */
224 struct cls_rule match;
226 ovs_be64 cookie_mask;
231 enum ofperr ofputil_decode_flow_stats_request(
232 struct ofputil_flow_stats_request *, const struct ofp_header *);
233 struct ofpbuf *ofputil_encode_flow_stats_request(
234 const struct ofputil_flow_stats_request *, enum ofputil_protocol);
235 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
236 const struct ofputil_flow_stats_request *);
238 /* Flow stats reply, independent of protocol. */
239 struct ofputil_flow_stats {
240 struct cls_rule rule;
243 uint32_t duration_sec;
244 uint32_t duration_nsec;
245 uint16_t idle_timeout;
246 uint16_t hard_timeout;
247 int idle_age; /* Seconds since last packet, -1 if unknown. */
248 int hard_age; /* Seconds since last change, -1 if unknown. */
249 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
250 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
251 union ofp_action *actions;
255 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
257 bool flow_age_extension);
258 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
259 struct list *replies);
261 /* Aggregate stats reply, independent of protocol. */
262 struct ofputil_aggregate_stats {
263 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
264 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
268 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
269 const struct ofputil_aggregate_stats *stats,
270 const struct ofp_stats_msg *request);
272 /* Flow removed message, independent of protocol. */
273 struct ofputil_flow_removed {
274 struct cls_rule rule;
276 uint8_t reason; /* One of OFPRR_*. */
277 uint32_t duration_sec;
278 uint32_t duration_nsec;
279 uint16_t idle_timeout;
280 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
281 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
284 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
285 const struct ofp_header *);
286 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
287 enum ofputil_protocol);
289 /* Abstract packet-in message. */
290 struct ofputil_packet_in {
294 enum ofp_packet_in_reason reason; /* One of OFPRR_*. */
295 uint16_t controller_id; /* Controller ID to send to. */
301 uint16_t total_len; /* Full length of frame. */
303 struct flow_metadata fmd; /* Metadata at creation time. */
306 int ofputil_decode_packet_in(struct ofputil_packet_in *,
307 const struct ofp_header *);
308 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
309 enum nx_packet_in_format);
311 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
312 bool ofputil_packet_in_reason_from_string(const char *,
313 enum ofp_packet_in_reason *);
315 /* Abstract packet-out message. */
316 struct ofputil_packet_out {
317 const void *packet; /* Packet data, if buffer_id == UINT32_MAX. */
318 size_t packet_len; /* Length of packet data in bytes. */
319 uint32_t buffer_id; /* Buffer id or UINT32_MAX if no buffer. */
320 uint16_t in_port; /* Packet's input port or OFPP_NONE. */
321 union ofp_action *actions; /* Actions. */
322 size_t n_actions; /* Number of elements in 'actions' array. */
325 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
326 const struct ofp_packet_out *);
327 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
330 enum netdev_features ofputil_netdev_port_features_from_ofp10(ovs_be32 ofp10);
331 ovs_be32 ofputil_netdev_port_features_to_ofp10(enum netdev_features);
333 /* OpenFlow protocol utility functions. */
334 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
335 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
337 void *make_openflow_xid(size_t openflow_len, uint8_t type,
338 ovs_be32 xid, struct ofpbuf **);
339 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
342 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
343 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
346 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
347 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
350 void update_openflow_length(struct ofpbuf *);
352 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
353 uint32_t subtype, struct ofpbuf **);
354 void *ofputil_make_stats_reply(size_t openflow_len,
355 const struct ofp_stats_msg *request,
358 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
360 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
361 void *ofputil_append_stats_reply(size_t len, struct list *);
363 const void *ofputil_stats_body(const struct ofp_header *);
364 size_t ofputil_stats_body_len(const struct ofp_header *);
366 const void *ofputil_nxstats_body(const struct ofp_header *);
367 size_t ofputil_nxstats_body_len(const struct ofp_header *);
369 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
371 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
372 uint16_t max_idle, size_t actions_len);
373 struct ofpbuf *make_del_flow(const struct cls_rule *);
374 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
375 uint32_t buffer_id, uint16_t out_port,
377 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
379 const struct ofpbuf *payload, int max_send_len);
380 struct ofpbuf *make_echo_request(void);
381 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
383 struct ofpbuf *ofputil_encode_barrier_request(void);
385 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
386 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
390 /* The type of an action.
392 * For each implemented OFPAT10_* and NXAST_* action type, there is a
393 * corresponding constant prefixed with OFPUTIL_, e.g.:
395 * OFPUTIL_OFPAT10_OUTPUT
396 * OFPUTIL_OFPAT10_SET_VLAN_VID
397 * OFPUTIL_OFPAT10_SET_VLAN_PCP
398 * OFPUTIL_OFPAT10_STRIP_VLAN
399 * OFPUTIL_OFPAT10_SET_DL_SRC
400 * OFPUTIL_OFPAT10_SET_DL_DST
401 * OFPUTIL_OFPAT10_SET_NW_SRC
402 * OFPUTIL_OFPAT10_SET_NW_DST
403 * OFPUTIL_OFPAT10_SET_NW_TOS
404 * OFPUTIL_OFPAT10_SET_TP_SRC
405 * OFPUTIL_OFPAT10_SET_TP_DST
406 * OFPUTIL_OFPAT10_ENQUEUE
407 * OFPUTIL_NXAST_RESUBMIT
408 * OFPUTIL_NXAST_SET_TUNNEL
409 * OFPUTIL_NXAST_SET_QUEUE
410 * OFPUTIL_NXAST_POP_QUEUE
411 * OFPUTIL_NXAST_REG_MOVE
412 * OFPUTIL_NXAST_REG_LOAD
414 * OFPUTIL_NXAST_SET_TUNNEL64
415 * OFPUTIL_NXAST_MULTIPATH
416 * OFPUTIL_NXAST_AUTOPATH
417 * OFPUTIL_NXAST_BUNDLE
418 * OFPUTIL_NXAST_BUNDLE_LOAD
419 * OFPUTIL_NXAST_RESUBMIT_TABLE
420 * OFPUTIL_NXAST_OUTPUT_REG
421 * OFPUTIL_NXAST_LEARN
422 * OFPUTIL_NXAST_DEC_TTL
423 * OFPUTIL_NXAST_FIN_TIMEOUT
425 * (The above list helps developers who want to "grep" for these definitions.)
427 enum ofputil_action_code {
428 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) OFPUTIL_##ENUM,
429 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
430 #include "ofp-util.def"
433 /* The number of values of "enum ofputil_action_code". */
435 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) + 1
436 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
437 OFPUTIL_N_ACTIONS = 0
438 #include "ofp-util.def"
441 int ofputil_decode_action(const union ofp_action *);
442 enum ofputil_action_code ofputil_decode_action_unsafe(
443 const union ofp_action *);
445 int ofputil_action_code_from_name(const char *);
447 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
449 /* For each OpenFlow action <ENUM> that has a corresponding action structure
450 * struct <STRUCT>, this defines two functions:
452 * void ofputil_init_<ENUM>(struct <STRUCT> *action);
454 * Initializes the parts of 'action' that identify it as having type <ENUM>
455 * and length 'sizeof *action' and zeros the rest. For actions that have
456 * variable length, the length used and cleared is that of struct <STRUCT>.
458 * struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
460 * Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
461 * initializes it with ofputil_init_<ENUM>(), and returns it.
463 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
464 void ofputil_init_##ENUM(struct STRUCT *); \
465 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
466 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
467 void ofputil_init_##ENUM(struct STRUCT *); \
468 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
469 #include "ofp-util.def"
471 #define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */
473 static inline union ofp_action *
474 ofputil_action_next(const union ofp_action *a)
476 return ((union ofp_action *) (void *)
477 ((uint8_t *) a + ntohs(a->header.len)));
481 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
483 uint16_t len = ntohs(a->header.len);
484 return (!(len % OFP_ACTION_ALIGN)
486 && len / sizeof *a <= n_actions);
489 /* This macro is careful to check for actions with bad lengths. */
490 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
491 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
492 (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT); \
493 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
494 (ITER) = ofputil_action_next(ITER)))
496 /* This macro does not check for actions with bad lengths. It should only be
497 * used with actions from trusted sources or with actions that have already
498 * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH). */
499 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS) \
500 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
502 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
503 (ITER) = ofputil_action_next(ITER)))
505 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
506 const struct flow *, int max_ports);
507 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
509 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
510 union ofp_action **, size_t *);
512 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
513 const union ofp_action *b, size_t n_b);
514 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
516 /* Handy utility for parsing flows and actions. */
517 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
519 #endif /* ofp-util.h */