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_check_output_port(uint16_t ofp_port, int max_ports);
106 bool ofputil_port_from_string(const char *, uint16_t *port);
107 void ofputil_format_port(uint16_t port, struct ds *);
109 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
110 * and from IP bitmasks. */
111 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
112 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
116 * These are arranged from most portable to least portable, or alternatively
117 * from least powerful to most powerful. Formats earlier on the list are more
118 * likely to be understood for the purpose of making requests, but formats
119 * later on the list are more likely to accurately describe a flow within a
122 * On any given OpenFlow connection, a single protocol is in effect at any
123 * given time. These values use separate bits only because that makes it easy
124 * to test whether a particular protocol is within a given set of protocols and
125 * to implement set union and intersection.
127 enum ofputil_protocol {
128 /* OpenFlow 1.0-based protocols. */
129 OFPUTIL_P_OF10 = 1 << 0, /* OpenFlow 1.0 flow format. */
130 OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
131 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
133 /* OpenFlow 1.0 with NXM-based flow formats. */
134 OFPUTIL_P_NXM = 1 << 2, /* Nicira extended match. */
135 OFPUTIL_P_NXM_TID = 1 << 3, /* NXM + flow_mod_table_id extension. */
136 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
139 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
141 /* Protocols in which a specific table may be specified in flow_mods. */
142 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
145 /* Protocols to use for flow dumps, from most to least preferred. */
146 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
147 extern size_t ofputil_n_flow_dump_protocols;
149 enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
150 bool ofputil_protocol_is_valid(enum ofputil_protocol);
151 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
153 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
154 enum ofputil_protocol ofputil_protocol_set_base(
155 enum ofputil_protocol cur, enum ofputil_protocol new_base);
157 const char *ofputil_protocol_to_string(enum ofputil_protocol);
158 char *ofputil_protocols_to_string(enum ofputil_protocol);
159 enum ofputil_protocol ofputil_protocols_from_string(const char *);
160 enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
162 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
163 enum ofputil_protocol want,
164 enum ofputil_protocol *next);
167 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
168 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
169 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
170 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
172 /* Work with OpenFlow 1.0 ofp_match. */
173 void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
174 void ofputil_cls_rule_from_match(const struct ofp_match *,
175 unsigned int priority, struct cls_rule *);
176 void ofputil_normalize_rule(struct cls_rule *);
177 void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
179 /* dl_type translation between OpenFlow and 'struct flow' format. */
180 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
181 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
184 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
185 int ofputil_packet_in_format_from_string(const char *);
186 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
187 struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
189 /* NXT_FLOW_MOD_TABLE_ID extension. */
190 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
192 /* Protocol-independent flow_mod. */
193 struct ofputil_flow_mod {
196 ovs_be64 cookie_mask;
199 uint16_t idle_timeout;
200 uint16_t hard_timeout;
204 union ofp_action *actions;
208 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
209 const struct ofp_header *,
210 enum ofputil_protocol);
211 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
212 enum ofputil_protocol);
214 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
215 const struct ofputil_flow_mod *fms, size_t n_fms);
217 /* Flow stats or aggregate stats request, independent of protocol. */
218 struct ofputil_flow_stats_request {
219 bool aggregate; /* Aggregate results? */
220 struct cls_rule match;
222 ovs_be64 cookie_mask;
227 enum ofperr ofputil_decode_flow_stats_request(
228 struct ofputil_flow_stats_request *, const struct ofp_header *);
229 struct ofpbuf *ofputil_encode_flow_stats_request(
230 const struct ofputil_flow_stats_request *, enum ofputil_protocol);
231 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
232 const struct ofputil_flow_stats_request *);
234 /* Flow stats reply, independent of protocol. */
235 struct ofputil_flow_stats {
236 struct cls_rule rule;
239 uint32_t duration_sec;
240 uint32_t duration_nsec;
241 uint16_t idle_timeout;
242 uint16_t hard_timeout;
243 int idle_age; /* Seconds since last packet, -1 if unknown. */
244 int hard_age; /* Seconds since last change, -1 if unknown. */
245 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
246 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
247 union ofp_action *actions;
251 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
253 bool flow_age_extension);
254 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
255 struct list *replies);
257 /* Aggregate stats reply, independent of protocol. */
258 struct ofputil_aggregate_stats {
259 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
260 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
264 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
265 const struct ofputil_aggregate_stats *stats,
266 const struct ofp_stats_msg *request);
268 /* Flow removed message, independent of protocol. */
269 struct ofputil_flow_removed {
270 struct cls_rule rule;
272 uint8_t reason; /* One of OFPRR_*. */
273 uint32_t duration_sec;
274 uint32_t duration_nsec;
275 uint16_t idle_timeout;
276 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
277 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
280 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
281 const struct ofp_header *);
282 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
283 enum ofputil_protocol);
285 /* Abstract packet-in message. */
286 struct ofputil_packet_in {
290 enum ofp_packet_in_reason reason; /* One of OFPRR_*. */
291 uint16_t controller_id; /* Controller ID to send to. */
297 uint16_t total_len; /* Full length of frame. */
299 struct flow_metadata fmd; /* Metadata at creation time. */
302 int ofputil_decode_packet_in(struct ofputil_packet_in *,
303 const struct ofp_header *);
304 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
305 enum nx_packet_in_format);
307 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
308 bool ofputil_packet_in_reason_from_string(const char *,
309 enum ofp_packet_in_reason *);
311 /* Abstract packet-out message. */
312 struct ofputil_packet_out {
313 const void *packet; /* Packet data, if buffer_id == UINT32_MAX. */
314 size_t packet_len; /* Length of packet data in bytes. */
315 uint32_t buffer_id; /* Buffer id or UINT32_MAX if no buffer. */
316 uint16_t in_port; /* Packet's input port or OFPP_NONE. */
317 union ofp_action *actions; /* Actions. */
318 size_t n_actions; /* Number of elements in 'actions' array. */
321 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
322 const struct ofp_packet_out *);
323 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
325 /* OpenFlow protocol utility functions. */
326 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
327 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
329 void *make_openflow_xid(size_t openflow_len, uint8_t type,
330 ovs_be32 xid, struct ofpbuf **);
331 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
334 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
335 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
338 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
339 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
342 void update_openflow_length(struct ofpbuf *);
344 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
345 uint32_t subtype, struct ofpbuf **);
346 void *ofputil_make_stats_reply(size_t openflow_len,
347 const struct ofp_stats_msg *request,
350 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
352 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
353 void *ofputil_append_stats_reply(size_t len, struct list *);
355 const void *ofputil_stats_body(const struct ofp_header *);
356 size_t ofputil_stats_body_len(const struct ofp_header *);
358 const void *ofputil_nxstats_body(const struct ofp_header *);
359 size_t ofputil_nxstats_body_len(const struct ofp_header *);
361 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
363 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
364 uint16_t max_idle, size_t actions_len);
365 struct ofpbuf *make_del_flow(const struct cls_rule *);
366 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
367 uint32_t buffer_id, uint16_t out_port,
369 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
371 const struct ofpbuf *payload, int max_send_len);
372 struct ofpbuf *make_echo_request(void);
373 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
375 struct ofpbuf *ofputil_encode_barrier_request(void);
377 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
378 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
382 /* The type of an action.
384 * For each implemented OFPAT_* and NXAST_* action type, there is a
385 * corresponding constant prefixed with OFPUTIL_, e.g.:
387 * OFPUTIL_OFPAT_OUTPUT
388 * OFPUTIL_OFPAT_SET_VLAN_VID
389 * OFPUTIL_OFPAT_SET_VLAN_PCP
390 * OFPUTIL_OFPAT_STRIP_VLAN
391 * OFPUTIL_OFPAT_SET_DL_SRC
392 * OFPUTIL_OFPAT_SET_DL_DST
393 * OFPUTIL_OFPAT_SET_NW_SRC
394 * OFPUTIL_OFPAT_SET_NW_DST
395 * OFPUTIL_OFPAT_SET_NW_TOS
396 * OFPUTIL_OFPAT_SET_TP_SRC
397 * OFPUTIL_OFPAT_SET_TP_DST
398 * OFPUTIL_OFPAT_ENQUEUE
399 * OFPUTIL_NXAST_RESUBMIT
400 * OFPUTIL_NXAST_SET_TUNNEL
401 * OFPUTIL_NXAST_SET_QUEUE
402 * OFPUTIL_NXAST_POP_QUEUE
403 * OFPUTIL_NXAST_REG_MOVE
404 * OFPUTIL_NXAST_REG_LOAD
406 * OFPUTIL_NXAST_SET_TUNNEL64
407 * OFPUTIL_NXAST_MULTIPATH
408 * OFPUTIL_NXAST_AUTOPATH
409 * OFPUTIL_NXAST_BUNDLE
410 * OFPUTIL_NXAST_BUNDLE_LOAD
411 * OFPUTIL_NXAST_RESUBMIT_TABLE
412 * OFPUTIL_NXAST_OUTPUT_REG
413 * OFPUTIL_NXAST_LEARN
414 * OFPUTIL_NXAST_DEC_TTL
415 * OFPUTIL_NXAST_FIN_TIMEOUT
417 * (The above list helps developers who want to "grep" for these definitions.)
419 enum ofputil_action_code {
420 #define OFPAT_ACTION(ENUM, STRUCT, NAME) OFPUTIL_##ENUM,
421 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
422 #include "ofp-util.def"
425 /* The number of values of "enum ofputil_action_code". */
427 #define OFPAT_ACTION(ENUM, STRUCT, NAME) + 1
428 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
429 OFPUTIL_N_ACTIONS = 0
430 #include "ofp-util.def"
433 int ofputil_decode_action(const union ofp_action *);
434 enum ofputil_action_code ofputil_decode_action_unsafe(
435 const union ofp_action *);
437 int ofputil_action_code_from_name(const char *);
439 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
441 /* For each OpenFlow action <ENUM> that has a corresponding action structure
442 * struct <STRUCT>, this defines two functions:
444 * void ofputil_init_<ENUM>(struct <STRUCT> *action);
446 * Initializes the parts of 'action' that identify it as having type <ENUM>
447 * and length 'sizeof *action' and zeros the rest. For actions that have
448 * variable length, the length used and cleared is that of struct <STRUCT>.
450 * struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
452 * Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
453 * initializes it with ofputil_init_<ENUM>(), and returns it.
455 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
456 void ofputil_init_##ENUM(struct STRUCT *); \
457 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
458 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
459 void ofputil_init_##ENUM(struct STRUCT *); \
460 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
461 #include "ofp-util.def"
463 #define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */
465 static inline union ofp_action *
466 ofputil_action_next(const union ofp_action *a)
468 return ((union ofp_action *) (void *)
469 ((uint8_t *) a + ntohs(a->header.len)));
473 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
475 uint16_t len = ntohs(a->header.len);
476 return (!(len % OFP_ACTION_ALIGN)
478 && len / sizeof *a <= n_actions);
481 /* This macro is careful to check for actions with bad lengths. */
482 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
483 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
484 (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT); \
485 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
486 (ITER) = ofputil_action_next(ITER)))
488 /* This macro does not check for actions with bad lengths. It should only be
489 * used with actions from trusted sources or with actions that have already
490 * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH). */
491 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS) \
492 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
494 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
495 (ITER) = ofputil_action_next(ITER)))
497 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
498 const struct flow *, int max_ports);
499 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
501 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
502 union ofp_action **, size_t *);
504 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
505 const union ofp_action *b, size_t n_b);
506 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
508 /* Handy utility for parsing flows and actions. */
509 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
511 #endif /* ofp-util.h */