2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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.
28 #include "meta-flow.h"
30 #include "openflow/netronome-ext.h"
31 #include "openflow/nicira-ext.h"
32 #include "openvswitch/types.h"
33 #include "type-props.h"
37 struct ofpact_set_field;
40 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port,
41 ofp_port_t *ofp10_port);
42 ovs_be32 ofputil_port_to_ofp11(ofp_port_t ofp10_port);
44 bool ofputil_port_from_string(const char *, ofp_port_t *portp);
45 void ofputil_format_port(ofp_port_t port, struct ds *);
46 void ofputil_port_to_string(ofp_port_t, char namebuf[OFP_MAX_PORT_NAME_LEN],
50 enum { MAX_GROUP_NAME_LEN = INT_STRLEN(uint32_t) };
51 bool ofputil_group_from_string(const char *, uint32_t *group_id);
52 void ofputil_format_group(uint32_t group_id, struct ds *);
53 void ofputil_group_to_string(uint32_t group_id,
54 char namebuf[MAX_GROUP_NAME_LEN + 1],
57 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
58 * to and from IP bitmasks. */
59 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
60 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
64 * A "protocol" is an OpenFlow version plus, for some OpenFlow versions,
65 * a bit extra about the flow match format in use.
67 * These are arranged from most portable to least portable, or alternatively
68 * from least powerful to most powerful. Protocols earlier on the list are
69 * more likely to be understood for the purpose of making requests, but
70 * protocol later on the list are more likely to accurately describe a flow
73 * On any given OpenFlow connection, a single protocol is in effect at any
74 * given time. These values use separate bits only because that makes it easy
75 * to test whether a particular protocol is within a given set of protocols and
76 * to implement set union and intersection.
78 enum ofputil_protocol {
79 /* OpenFlow 1.0 protocols.
81 * The "STD" protocols use the standard OpenFlow 1.0 flow format.
82 * The "NXM" protocols use the Nicira Extensible Match (NXM) flow format.
84 * The protocols with "TID" mean that the nx_flow_mod_table_id Nicira
85 * extension has been enabled. The other protocols have it disabled.
87 #define OFPUTIL_P_NONE 0
88 OFPUTIL_P_OF10_STD = 1 << 0,
89 OFPUTIL_P_OF10_STD_TID = 1 << 1,
90 OFPUTIL_P_OF10_NXM = 1 << 2,
91 OFPUTIL_P_OF10_NXM_TID = 1 << 3,
92 #define OFPUTIL_P_OF10_STD_ANY (OFPUTIL_P_OF10_STD | OFPUTIL_P_OF10_STD_TID)
93 #define OFPUTIL_P_OF10_NXM_ANY (OFPUTIL_P_OF10_NXM | OFPUTIL_P_OF10_NXM_TID)
94 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY)
96 /* OpenFlow 1.1 protocol.
98 * We only support the standard OpenFlow 1.1 flow format.
100 * OpenFlow 1.1 always operates with an equivalent of the
101 * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
103 OFPUTIL_P_OF11_STD = 1 << 4,
105 /* OpenFlow 1.2+ protocols (only one variant each).
107 * These use the standard OpenFlow Extensible Match (OXM) flow format.
109 * OpenFlow 1.2+ always operates with an equivalent of the
110 * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
112 OFPUTIL_P_OF12_OXM = 1 << 5,
113 OFPUTIL_P_OF13_OXM = 1 << 6,
114 OFPUTIL_P_OF14_OXM = 1 << 7,
115 OFPUTIL_P_OF15_OXM = 1 << 8,
116 #define OFPUTIL_P_ANY_OXM (OFPUTIL_P_OF12_OXM | \
117 OFPUTIL_P_OF13_OXM | \
118 OFPUTIL_P_OF14_OXM | \
121 #define OFPUTIL_P_NXM_OF11_UP (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF11_STD | \
124 #define OFPUTIL_P_NXM_OXM_ANY (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_ANY_OXM)
126 #define OFPUTIL_P_OF11_UP (OFPUTIL_P_OF11_STD | OFPUTIL_P_ANY_OXM)
128 #define OFPUTIL_P_OF12_UP (OFPUTIL_P_OF12_OXM | OFPUTIL_P_OF13_UP)
129 #define OFPUTIL_P_OF13_UP (OFPUTIL_P_OF13_OXM | OFPUTIL_P_OF14_UP)
130 #define OFPUTIL_P_OF14_UP (OFPUTIL_P_OF14_OXM | OFPUTIL_P_OF15_UP)
131 #define OFPUTIL_P_OF15_UP OFPUTIL_P_OF15_OXM
134 #define OFPUTIL_P_ANY ((1 << 9) - 1)
136 /* Protocols in which a specific table may be specified in flow_mods. */
137 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_STD_TID | \
138 OFPUTIL_P_OF10_NXM_TID | \
139 OFPUTIL_P_OF11_STD | \
143 /* Protocols to use for flow dumps, from most to least preferred. */
144 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
145 extern size_t ofputil_n_flow_dump_protocols;
147 enum ofputil_protocol ofputil_protocol_from_ofp_version(enum ofp_version);
148 enum ofputil_protocol ofputil_protocols_from_ofp_version(enum ofp_version);
149 enum ofp_version ofputil_protocol_to_ofp_version(enum ofputil_protocol);
151 bool ofputil_protocol_is_valid(enum ofputil_protocol);
152 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
154 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
155 enum ofputil_protocol ofputil_protocol_set_base(
156 enum ofputil_protocol cur, enum ofputil_protocol new_base);
158 const char *ofputil_protocol_to_string(enum ofputil_protocol);
159 char *ofputil_protocols_to_string(enum ofputil_protocol);
160 enum ofputil_protocol ofputil_protocols_from_string(const char *);
162 void ofputil_format_version(struct ds *, enum ofp_version);
163 void ofputil_format_version_name(struct ds *, enum ofp_version);
165 /* A bitmap of version numbers
167 * Bit offsets correspond to ofp_version numbers which in turn correspond to
168 * wire-protocol numbers for OpenFlow versions, e.g. (1u << OFP11_VERSION)
169 * is the mask for OpenFlow 1.1. If the bit for a version is set then it is
170 * allowed, otherwise it is disallowed. */
172 void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap);
173 void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap);
175 enum ofp_version ofputil_version_from_string(const char *s);
177 uint32_t ofputil_protocols_to_version_bitmap(enum ofputil_protocol);
178 enum ofputil_protocol ofputil_protocols_from_version_bitmap(uint32_t bitmap);
180 /* Bitmaps of OpenFlow versions that Open vSwitch supports, and that it enables
181 * by default. When Open vSwitch has experimental or incomplete support for
182 * newer versions of OpenFlow, those versions should not be supported by
183 * default and thus should be omitted from the latter bitmap. */
184 #define OFPUTIL_SUPPORTED_VERSIONS ((1u << OFP10_VERSION) | \
185 (1u << OFP11_VERSION) | \
186 (1u << OFP12_VERSION) | \
187 (1u << OFP13_VERSION))
188 #define OFPUTIL_DEFAULT_VERSIONS OFPUTIL_SUPPORTED_VERSIONS
190 enum ofputil_protocol ofputil_protocols_from_string(const char *s);
192 const char *ofputil_version_to_string(enum ofp_version ofp_version);
193 uint32_t ofputil_versions_from_string(const char *s);
194 uint32_t ofputil_versions_from_strings(char ** const s, size_t count);
196 bool ofputil_decode_hello(const struct ofp_header *,
197 uint32_t *allowed_versions);
198 struct ofpbuf *ofputil_encode_hello(uint32_t version_bitmap);
200 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
201 enum ofputil_protocol want,
202 enum ofputil_protocol *next);
205 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
206 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
207 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
208 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
210 /* Work with ofp10_match. */
211 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
212 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
214 void ofputil_normalize_match(struct match *);
215 void ofputil_normalize_match_quiet(struct match *);
216 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
218 /* Work with ofp11_match. */
219 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
220 uint16_t *padded_match_len);
221 enum ofperr ofputil_pull_ofp11_mask(struct ofpbuf *, struct match *,
222 struct mf_bitmap *bm);
223 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
225 int ofputil_put_ofp11_match(struct ofpbuf *, const struct match *,
226 enum ofputil_protocol);
227 void ofputil_match_to_ofp11_match(const struct match *, struct ofp11_match *);
228 int ofputil_match_typical_len(enum ofputil_protocol);
230 /* dl_type translation between OpenFlow and 'struct flow' format. */
231 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
232 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
235 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
236 int ofputil_packet_in_format_from_string(const char *);
237 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
238 struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
239 enum nx_packet_in_format);
241 /* NXT_FLOW_MOD_TABLE_ID extension. */
242 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
244 /* Protocol-independent flow_mod flags. */
245 enum ofputil_flow_mod_flags {
246 /* Flags that are maintained with a flow as part of its state.
248 * (OFPUTIL_FF_EMERG would be here too, if OVS supported it.) */
249 OFPUTIL_FF_SEND_FLOW_REM = 1 << 0, /* All versions. */
250 OFPUTIL_FF_NO_PKT_COUNTS = 1 << 1, /* OpenFlow 1.3+. */
251 OFPUTIL_FF_NO_BYT_COUNTS = 1 << 2, /* OpenFlow 1.3+. */
252 #define OFPUTIL_FF_STATE (OFPUTIL_FF_SEND_FLOW_REM \
253 | OFPUTIL_FF_NO_PKT_COUNTS \
254 | OFPUTIL_FF_NO_BYT_COUNTS)
256 /* Flags that affect flow_mod behavior but are not part of flow state. */
257 OFPUTIL_FF_CHECK_OVERLAP = 1 << 3, /* All versions. */
258 OFPUTIL_FF_EMERG = 1 << 4, /* OpenFlow 1.0 only. */
259 OFPUTIL_FF_RESET_COUNTS = 1 << 5, /* OpenFlow 1.2+. */
261 /* Flags that are only set by OVS for its internal use. Cannot be set via
263 OFPUTIL_FF_HIDDEN_FIELDS = 1 << 6, /* Allow hidden match fields to be
265 OFPUTIL_FF_NO_READONLY = 1 << 7, /* Allow rules within read only tables
269 /* Protocol-independent flow_mod.
271 * The handling of cookies across multiple versions of OpenFlow is a bit
272 * confusing. See DESIGN for the details. */
273 struct ofputil_flow_mod {
274 struct ovs_list list_node; /* For queuing flow_mods. */
279 /* Cookie matching. The flow_mod affects only flows that have cookies that
280 * bitwise match 'cookie' bits in positions where 'cookie_mask has 1-bits.
282 * 'cookie_mask' should be zero for OFPFC_ADD flow_mods. */
283 ovs_be64 cookie; /* Cookie bits to match. */
284 ovs_be64 cookie_mask; /* 1-bit in each 'cookie' bit to match. */
288 * OFPFC_ADD uses 'new_cookie' as the new flow's cookie. 'new_cookie'
289 * should not be UINT64_MAX.
291 * OFPFC_MODIFY and OFPFC_MODIFY_STRICT have two cases:
293 * - If one or more matching flows exist and 'modify_cookie' is true,
294 * then the flow_mod changes the existing flows' cookies to
295 * 'new_cookie'. 'new_cookie' should not be UINT64_MAX.
297 * - If no matching flow exists, 'new_cookie' is not UINT64_MAX, and
298 * 'cookie_mask' is 0, then the flow_mod adds a new flow with
299 * 'new_cookie' as its cookie.
301 ovs_be64 new_cookie; /* New cookie to install or UINT64_MAX. */
302 bool modify_cookie; /* Set cookie of existing flow to 'new_cookie'? */
306 uint16_t idle_timeout;
307 uint16_t hard_timeout;
311 enum ofputil_flow_mod_flags flags;
312 uint16_t importance; /* Eviction precedence. */
313 struct ofpact *ofpacts; /* Series of "struct ofpact"s. */
314 size_t ofpacts_len; /* Length of ofpacts, in bytes. */
316 /* Reason for delete; ignored for non-delete commands */
317 enum ofp_flow_removed_reason delete_reason;
320 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
321 const struct ofp_header *,
322 enum ofputil_protocol,
323 struct ofpbuf *ofpacts,
326 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
327 enum ofputil_protocol);
329 /* Flow stats or aggregate stats request, independent of protocol. */
330 struct ofputil_flow_stats_request {
331 bool aggregate; /* Aggregate results? */
334 ovs_be64 cookie_mask;
340 enum ofperr ofputil_decode_flow_stats_request(
341 struct ofputil_flow_stats_request *, const struct ofp_header *);
342 struct ofpbuf *ofputil_encode_flow_stats_request(
343 const struct ofputil_flow_stats_request *, enum ofputil_protocol);
345 /* Flow stats reply, independent of protocol. */
346 struct ofputil_flow_stats {
351 uint16_t idle_timeout;
352 uint16_t hard_timeout;
353 uint32_t duration_sec;
354 uint32_t duration_nsec;
355 int idle_age; /* Seconds since last packet, -1 if unknown. */
356 int hard_age; /* Seconds since last change, -1 if unknown. */
357 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
358 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
359 const struct ofpact *ofpacts;
361 enum ofputil_flow_mod_flags flags;
362 uint16_t importance; /* Eviction precedence. */
365 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
367 bool flow_age_extension,
368 struct ofpbuf *ofpacts);
369 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
370 struct ovs_list *replies);
372 /* Aggregate stats reply, independent of protocol. */
373 struct ofputil_aggregate_stats {
374 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
375 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
379 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
380 const struct ofputil_aggregate_stats *stats,
381 const struct ofp_header *request);
382 enum ofperr ofputil_decode_aggregate_stats_reply(
383 struct ofputil_aggregate_stats *,
384 const struct ofp_header *reply);
386 /* Flow removed message, independent of protocol. */
387 struct ofputil_flow_removed {
391 uint8_t reason; /* One of OFPRR_*. */
392 uint8_t table_id; /* 255 if message didn't include table ID. */
393 uint32_t duration_sec;
394 uint32_t duration_nsec;
395 uint16_t idle_timeout;
396 uint16_t hard_timeout;
397 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
398 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
401 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
402 const struct ofp_header *);
403 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
404 enum ofputil_protocol);
406 /* Abstract packet-in message. */
407 struct ofputil_packet_in {
408 /* Packet data and metadata.
410 * To save bandwidth, in some cases a switch may send only the first
411 * several bytes of a packet, indicated by 'packet_len < total_len'. When
412 * the full packet is included, 'packet_len == total_len'. */
414 size_t packet_len; /* Number of bytes in 'packet'. */
415 size_t total_len; /* Size of packet, pre-truncation. */
416 struct match flow_metadata;
418 /* Identifies a buffer in the switch that contains the full packet, to
419 * allow the controller to reference it later without having to send the
420 * entire packet back to the switch.
422 * UINT32_MAX indicates that the packet is not buffered in the switch. A
423 * switch should only use UINT32_MAX when it sends the entire packet. */
426 /* Reason that the packet-in is being sent. */
427 enum ofp_packet_in_reason reason; /* One of OFPR_*. */
429 /* Information about the OpenFlow flow that triggered the packet-in.
431 * A packet-in triggered by a flow table miss has no associated flow. In
432 * that case, 'cookie' is UINT64_MAX. */
433 uint8_t table_id; /* OpenFlow table ID. */
434 ovs_be64 cookie; /* Flow's cookie. */
437 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
438 const struct ofp_header *);
439 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
440 enum ofputil_protocol protocol,
441 enum nx_packet_in_format);
443 enum { OFPUTIL_PACKET_IN_REASON_BUFSIZE = INT_STRLEN(int) + 1 };
444 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason,
447 bool ofputil_packet_in_reason_from_string(const char *,
448 enum ofp_packet_in_reason *);
450 /* Abstract packet-out message.
452 * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
453 * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
454 struct ofputil_packet_out {
455 const void *packet; /* Packet data, if buffer_id == UINT32_MAX. */
456 size_t packet_len; /* Length of packet data in bytes. */
457 uint32_t buffer_id; /* Buffer id or UINT32_MAX if no buffer. */
458 ofp_port_t in_port; /* Packet's input port. */
459 struct ofpact *ofpacts; /* Actions. */
460 size_t ofpacts_len; /* Size of ofpacts in bytes. */
463 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
464 const struct ofp_header *,
465 struct ofpbuf *ofpacts);
466 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
467 enum ofputil_protocol protocol);
469 enum ofputil_port_config {
470 /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
471 OFPUTIL_PC_PORT_DOWN = 1 << 0, /* Port is administratively down. */
472 OFPUTIL_PC_NO_RECV = 1 << 2, /* Drop all packets received by port. */
473 OFPUTIL_PC_NO_FWD = 1 << 5, /* Drop packets forwarded to port. */
474 OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
475 /* OpenFlow 1.0 only. */
476 OFPUTIL_PC_NO_STP = 1 << 1, /* No 802.1D spanning tree for port. */
477 OFPUTIL_PC_NO_RECV_STP = 1 << 3, /* Drop received 802.1D STP packets. */
478 OFPUTIL_PC_NO_FLOOD = 1 << 4, /* Do not include port when flooding. */
479 /* There are no OpenFlow 1.1-only bits. */
482 enum ofputil_port_state {
483 /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
484 OFPUTIL_PS_LINK_DOWN = 1 << 0, /* No physical link present. */
485 /* OpenFlow 1.1 only. */
486 OFPUTIL_PS_BLOCKED = 1 << 1, /* Port is blocked */
487 OFPUTIL_PS_LIVE = 1 << 2, /* Live for Fast Failover Group. */
488 /* OpenFlow 1.0 only. */
489 OFPUTIL_PS_STP_LISTEN = 0 << 8, /* Not learning or relaying frames. */
490 OFPUTIL_PS_STP_LEARN = 1 << 8, /* Learning but not relaying frames. */
491 OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
492 OFPUTIL_PS_STP_BLOCK = 3 << 8, /* Not part of spanning tree. */
493 OFPUTIL_PS_STP_MASK = 3 << 8 /* Bit mask for OFPPS10_STP_* values. */
496 /* Abstract ofp10_phy_port or ofp11_port. */
497 struct ofputil_phy_port {
499 uint8_t hw_addr[OFP_ETH_ALEN];
500 char name[OFP_MAX_PORT_NAME_LEN];
501 enum ofputil_port_config config;
502 enum ofputil_port_state state;
504 /* NETDEV_F_* feature bitmasks. */
505 enum netdev_features curr; /* Current features. */
506 enum netdev_features advertised; /* Features advertised by the port. */
507 enum netdev_features supported; /* Features supported by the port. */
508 enum netdev_features peer; /* Features advertised by peer. */
511 uint32_t curr_speed; /* Current speed, in kbps. */
512 uint32_t max_speed; /* Maximum supported speed, in kbps. */
515 enum ofputil_capabilities {
516 /* OpenFlow 1.0, 1.1, 1.2, and 1.3 share these capability values. */
517 OFPUTIL_C_FLOW_STATS = 1 << 0, /* Flow statistics. */
518 OFPUTIL_C_TABLE_STATS = 1 << 1, /* Table statistics. */
519 OFPUTIL_C_PORT_STATS = 1 << 2, /* Port statistics. */
520 OFPUTIL_C_IP_REASM = 1 << 5, /* Can reassemble IP fragments. */
521 OFPUTIL_C_QUEUE_STATS = 1 << 6, /* Queue statistics. */
523 /* OpenFlow 1.0 and 1.1 share this capability. */
524 OFPUTIL_C_ARP_MATCH_IP = 1 << 7, /* Match IP addresses in ARP pkts. */
526 /* OpenFlow 1.0 only. */
527 OFPUTIL_C_STP = 1 << 3, /* 802.1d spanning tree. */
529 /* OpenFlow 1.1, 1.2, and 1.3 share this capability. */
530 OFPUTIL_C_GROUP_STATS = 1 << 4, /* Group statistics. */
532 /* OpenFlow 1.2 and 1.3 share this capability */
533 OFPUTIL_C_PORT_BLOCKED = 1 << 8, /* Switch will block looping ports */
536 /* Abstract ofp_switch_features. */
537 struct ofputil_switch_features {
538 uint64_t datapath_id; /* Datapath unique ID. */
539 uint32_t n_buffers; /* Max packets buffered at once. */
540 uint8_t n_tables; /* Number of tables supported by datapath. */
541 uint8_t auxiliary_id; /* Identify auxiliary connections */
542 enum ofputil_capabilities capabilities;
543 uint64_t ofpacts; /* Bitmap of OFPACT_* bits. */
546 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
547 struct ofputil_switch_features *,
550 struct ofpbuf *ofputil_encode_switch_features(
551 const struct ofputil_switch_features *, enum ofputil_protocol,
553 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
555 bool ofputil_switch_features_has_ports(struct ofpbuf *b);
557 /* phy_port helper functions. */
558 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
559 struct ofputil_phy_port *);
561 /* Abstract ofp_port_status. */
562 struct ofputil_port_status {
563 enum ofp_port_reason reason;
564 struct ofputil_phy_port desc;
567 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
568 struct ofputil_port_status *);
569 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
570 enum ofputil_protocol);
572 /* Abstract ofp_port_mod. */
573 struct ofputil_port_mod {
575 uint8_t hw_addr[OFP_ETH_ALEN];
576 enum ofputil_port_config config;
577 enum ofputil_port_config mask;
578 enum netdev_features advertise;
581 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
582 struct ofputil_port_mod *, bool loose);
583 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
584 enum ofputil_protocol);
586 /* Abstract version of OFPTC11_TABLE_MISS_*.
588 * OpenFlow 1.0 always sends packets that miss to the next flow table, or to
589 * the controller if they miss in the last flow table.
591 * OpenFlow 1.1 and 1.2 can configure table miss behavior via a "table-mod"
592 * that specifies "send to controller", "miss", or "drop".
594 * OpenFlow 1.3 and later never sends packets that miss to the controller.
596 enum ofputil_table_miss {
597 /* Protocol-specific default behavior. On OpenFlow 1.0 through 1.2
598 * connections, the packet is sent to the controller, and on OpenFlow 1.3
599 * and later connections, the packet is dropped.
601 * This is also used as a result of decoding OpenFlow 1.3+ "config" values
602 * in table-mods, to indicate that no table-miss was specified. */
603 OFPUTIL_TABLE_MISS_DEFAULT, /* Protocol default behavior. */
605 /* These constants have the same meanings as those in OpenFlow with the
607 OFPUTIL_TABLE_MISS_CONTROLLER, /* Send to controller. */
608 OFPUTIL_TABLE_MISS_CONTINUE, /* Go to next table. */
609 OFPUTIL_TABLE_MISS_DROP, /* Drop the packet. */
612 ovs_be32 ofputil_table_miss_to_config(enum ofputil_table_miss,
615 /* Abstract ofp_table_mod. */
616 struct ofputil_table_mod {
617 uint8_t table_id; /* ID of the table, 0xff indicates all tables. */
618 enum ofputil_table_miss miss_config;
621 enum ofperr ofputil_decode_table_mod(const struct ofp_header *,
622 struct ofputil_table_mod *);
623 struct ofpbuf *ofputil_encode_table_mod(const struct ofputil_table_mod *,
624 enum ofputil_protocol);
626 /* Abstract ofp_table_features. */
627 struct ofputil_table_features {
628 uint8_t table_id; /* Identifier of table. Lower numbered tables
629 are consulted first. */
630 char name[OFP_MAX_TABLE_NAME_LEN];
631 ovs_be64 metadata_match; /* Bits of metadata table can match. */
632 ovs_be64 metadata_write; /* Bits of metadata table can write. */
633 enum ofputil_table_miss miss_config;
634 uint32_t max_entries; /* Max number of entries supported. */
636 /* Table features related to instructions. There are two instances:
638 * - 'miss' reports features available in the table miss flow.
640 * - 'nonmiss' reports features available in other flows. */
641 struct ofputil_table_instruction_features {
642 /* Tables that "goto-table" may jump to. */
643 unsigned long int next[BITMAP_N_LONGS(255)];
645 /* Bitmap of OVSINST_* for supported instructions. */
646 uint32_t instructions;
648 /* Table features related to actions. There are two instances:
650 * - 'write' reports features available in a "write_actions"
653 * - 'apply' reports features available in an "apply_actions"
655 struct ofputil_table_action_features {
656 uint64_t ofpacts; /* Bitmap of supported OFPACT_*. */
657 struct mf_bitmap set_fields; /* Fields for "set-field". */
663 * For any given field the following combinations are valid:
665 * - match=0, wildcard=0, mask=0: Flows in this table cannot match on
668 * - match=1, wildcard=0, mask=0: Flows in this table must match on all
669 * the bits in this field.
671 * - match=1, wildcard=1, mask=0: Flows in this table must either match
672 * on all the bits in the field or wildcard the field entirely.
674 * - match=1, wildcard=1, mask=1: Flows in this table may arbitrarily
675 * mask this field (as special cases, they may match on all the bits
676 * or wildcard it entirely).
678 * Other combinations do not make sense.
680 struct mf_bitmap match; /* Fields that may be matched. */
681 struct mf_bitmap mask; /* Subset of 'match' that may have masks. */
682 struct mf_bitmap wildcard; /* Subset of 'match' that may be wildcarded. */
685 int ofputil_decode_table_features(struct ofpbuf *,
686 struct ofputil_table_features *, bool loose);
687 struct ofpbuf *ofputil_encode_table_features_request(enum ofp_version);
689 void ofputil_append_table_features_reply(
690 const struct ofputil_table_features *tf, struct ovs_list *replies);
692 /* Meter band configuration for all supported band types. */
693 struct ofputil_meter_band {
695 uint8_t prec_level; /* Non-zero if type == OFPMBT_DSCP_REMARK. */
700 struct ofputil_meter_band_stats {
701 uint64_t packet_count;
705 struct ofputil_meter_config {
709 struct ofputil_meter_band *bands;
712 /* Abstract ofp_meter_mod. */
713 struct ofputil_meter_mod {
715 struct ofputil_meter_config meter;
718 struct ofputil_meter_stats {
721 uint64_t packet_in_count;
722 uint64_t byte_in_count;
723 uint32_t duration_sec;
724 uint32_t duration_nsec;
726 struct ofputil_meter_band_stats *bands;
729 struct ofputil_meter_features {
730 uint32_t max_meters; /* Maximum number of meters. */
731 uint32_t band_types; /* Can support max 32 band types. */
732 uint32_t capabilities; /* Supported flags. */
737 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
738 struct ofputil_meter_mod *,
739 struct ofpbuf *bands);
740 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
741 const struct ofputil_meter_mod *);
743 void ofputil_decode_meter_features(const struct ofp_header *,
744 struct ofputil_meter_features *);
745 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
746 ofputil_meter_features *,
747 const struct ofp_header *
749 void ofputil_decode_meter_request(const struct ofp_header *,
752 void ofputil_append_meter_config(struct ovs_list *replies,
753 const struct ofputil_meter_config *);
755 void ofputil_append_meter_stats(struct ovs_list *replies,
756 const struct ofputil_meter_stats *);
758 enum ofputil_meter_request_type {
759 OFPUTIL_METER_FEATURES,
760 OFPUTIL_METER_CONFIG,
764 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
765 enum ofputil_meter_request_type,
768 int ofputil_decode_meter_stats(struct ofpbuf *,
769 struct ofputil_meter_stats *,
770 struct ofpbuf *bands);
772 int ofputil_decode_meter_config(struct ofpbuf *,
773 struct ofputil_meter_config *,
774 struct ofpbuf *bands);
776 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
777 typedef struct { uint32_t uint32; } ofproto_meter_id;
779 /* Abstract ofp_role_request and reply. */
780 struct ofputil_role_request {
781 enum ofp12_controller_role role;
782 bool have_generation_id;
783 uint64_t generation_id;
786 struct ofputil_role_status {
787 enum ofp12_controller_role role;
788 enum ofp14_controller_role_reason reason;
789 uint64_t generation_id;
792 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
793 struct ofputil_role_request *);
794 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
795 const struct ofputil_role_request *);
797 struct ofpbuf *ofputil_encode_role_status(
798 const struct ofputil_role_status *status,
799 enum ofputil_protocol protocol);
801 enum ofperr ofputil_decode_role_status(const struct ofp_header *oh,
802 struct ofputil_role_status *rs);
804 /* Abstract table stats.
806 * This corresponds to the OpenFlow 1.3 table statistics structure, which only
807 * includes actual statistics. In earlier versions of OpenFlow, several
808 * members describe table features, so this structure has to be paired with
809 * struct ofputil_table_features to get all information. */
810 struct ofputil_table_stats {
811 uint8_t table_id; /* Identifier of table. */
812 uint32_t active_count; /* Number of active entries. */
813 uint64_t lookup_count; /* Number of packets looked up in table. */
814 uint64_t matched_count; /* Number of packets that hit table. */
817 struct ofpbuf *ofputil_encode_table_stats_reply(const struct ofp_header *rq);
818 void ofputil_append_table_stats_reply(struct ofpbuf *reply,
819 const struct ofputil_table_stats *,
820 const struct ofputil_table_features *);
822 int ofputil_decode_table_stats_reply(struct ofpbuf *reply,
823 struct ofputil_table_stats *,
824 struct ofputil_table_features *);
826 /* Queue configuration request. */
827 struct ofpbuf *ofputil_encode_queue_get_config_request(enum ofp_version,
829 enum ofperr ofputil_decode_queue_get_config_request(const struct ofp_header *,
832 /* Queue configuration reply. */
833 struct ofputil_queue_config {
836 /* Each of these optional values is expressed in tenths of a percent.
837 * Values greater than 1000 indicate that the feature is disabled.
838 * UINT16_MAX indicates that the value is omitted. */
843 struct ofpbuf *ofputil_encode_queue_get_config_reply(
844 const struct ofp_header *request);
845 void ofputil_append_queue_get_config_reply(
846 struct ofpbuf *reply, const struct ofputil_queue_config *);
848 enum ofperr ofputil_decode_queue_get_config_reply(struct ofpbuf *reply,
850 int ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
851 struct ofputil_queue_config *);
854 /* Abstract nx_flow_monitor_request. */
855 struct ofputil_flow_monitor_request {
857 enum nx_flow_monitor_flags flags;
863 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
865 void ofputil_append_flow_monitor_request(
866 const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
868 /* Abstract nx_flow_update. */
869 struct ofputil_flow_update {
870 enum nx_flow_update_event event;
872 /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
873 enum ofp_flow_removed_reason reason;
874 uint16_t idle_timeout;
875 uint16_t hard_timeout;
880 const struct ofpact *ofpacts;
883 /* Used only for NXFME_ABBREV. */
887 int ofputil_decode_flow_update(struct ofputil_flow_update *,
888 struct ofpbuf *msg, struct ofpbuf *ofpacts);
889 void ofputil_start_flow_update(struct ovs_list *replies);
890 void ofputil_append_flow_update(const struct ofputil_flow_update *,
891 struct ovs_list *replies);
893 /* Abstract nx_flow_monitor_cancel. */
894 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
895 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
897 /* Port desc stats requests and replies. */
898 enum ofperr ofputil_decode_port_desc_stats_request(const struct ofp_header *,
900 struct ofpbuf *ofputil_encode_port_desc_stats_request(
901 enum ofp_version ofp_version, ofp_port_t);
903 void ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
904 struct ovs_list *replies);
906 /* Encoding simple OpenFlow messages. */
907 struct ofpbuf *make_echo_request(enum ofp_version);
908 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
910 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
912 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
913 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
918 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
920 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
921 union ofp_action **, size_t *);
923 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
924 const union ofp_action *b, size_t n_b);
925 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
927 /* Handy utility for parsing flows and actions. */
928 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
930 struct ofputil_port_stats {
932 struct netdev_stats stats;
933 uint32_t duration_sec; /* UINT32_MAX if unknown. */
934 uint32_t duration_nsec;
937 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
939 void ofputil_append_port_stat(struct ovs_list *replies,
940 const struct ofputil_port_stats *ops);
941 size_t ofputil_count_port_stats(const struct ofp_header *);
942 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
943 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
944 ofp_port_t *ofp10_port);
946 struct ofputil_queue_stats_request {
947 ofp_port_t port_no; /* OFPP_ANY means "all ports". */
952 ofputil_decode_queue_stats_request(const struct ofp_header *request,
953 struct ofputil_queue_stats_request *oqsr);
955 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
956 const struct ofputil_queue_stats_request *oqsr);
958 struct ofputil_queue_stats {
962 /* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
967 /* UINT32_MAX if unknown. */
968 uint32_t duration_sec;
969 uint32_t duration_nsec;
972 size_t ofputil_count_queue_stats(const struct ofp_header *);
973 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
974 void ofputil_append_queue_stat(struct ovs_list *replies,
975 const struct ofputil_queue_stats *oqs);
977 struct bucket_counter {
978 uint64_t packet_count; /* Number of packets processed by bucket. */
979 uint64_t byte_count; /* Number of bytes processed by bucket. */
982 /* Bucket for use in groups. */
983 struct ofputil_bucket {
984 struct ovs_list list_node;
985 uint16_t weight; /* Relative weight, for "select" groups. */
986 ofp_port_t watch_port; /* Port whose state affects whether this bucket
987 * is live. Only required for fast failover
989 uint32_t watch_group; /* Group whose state affects whether this
990 * bucket is live. Only required for fast
991 * failover groups. */
992 uint32_t bucket_id; /* Bucket Id used to identify bucket*/
993 struct ofpact *ofpacts; /* Series of "struct ofpact"s. */
994 size_t ofpacts_len; /* Length of ofpacts, in bytes. */
996 struct bucket_counter stats;
999 /* Protocol-independent group_mod. */
1000 struct ofputil_group_props {
1001 /* NTR selection method */
1002 char selection_method[NTR_MAX_SELECTION_METHOD_LEN];
1003 uint64_t selection_method_param;
1004 struct field_array fields;
1007 /* Protocol-independent group_mod. */
1008 struct ofputil_group_mod {
1009 uint16_t command; /* One of OFPGC15_*. */
1010 uint8_t type; /* One of OFPGT11_*. */
1011 uint32_t group_id; /* Group identifier. */
1012 uint32_t command_bucket_id; /* Bucket Id used as part of
1013 * OFPGC15_INSERT_BUCKET and
1014 * OFPGC15_REMOVE_BUCKET commands
1016 struct ovs_list buckets; /* Contains "struct ofputil_bucket"s. */
1017 struct ofputil_group_props props; /* Group properties. */
1020 /* Group stats reply, independent of protocol. */
1021 struct ofputil_group_stats {
1022 uint32_t group_id; /* Group identifier. */
1024 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
1025 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
1026 uint32_t duration_sec; /* UINT32_MAX if unknown. */
1027 uint32_t duration_nsec;
1029 struct bucket_counter *bucket_stats;
1032 /* Group features reply, independent of protocol.
1034 * Only OF1.2 and later support group features replies. */
1035 struct ofputil_group_features {
1036 uint32_t types; /* Bitmap of OFPGT_* values supported. */
1037 uint32_t capabilities; /* Bitmap of OFPGFC12_* capability supported. */
1038 uint32_t max_groups[4]; /* Maximum number of groups for each type. */
1039 uint64_t ofpacts[4]; /* Bitmaps of supported OFPACT_* */
1042 /* Group desc reply, independent of protocol. */
1043 struct ofputil_group_desc {
1044 uint8_t type; /* One of OFPGT_*. */
1045 uint32_t group_id; /* Group identifier. */
1046 struct ovs_list buckets; /* Contains "struct ofputil_bucket"s. */
1047 struct ofputil_group_props props; /* Group properties. */
1050 void ofputil_bucket_list_destroy(struct ovs_list *buckets);
1051 void ofputil_bucket_clone_list(struct ovs_list *dest,
1052 const struct ovs_list *src,
1053 const struct ofputil_bucket *);
1054 struct ofputil_bucket *ofputil_bucket_find(const struct ovs_list *,
1055 uint32_t bucket_id);
1056 bool ofputil_bucket_check_duplicate_id(const struct ovs_list *);
1057 struct ofputil_bucket *ofputil_bucket_list_front(const struct ovs_list *);
1058 struct ofputil_bucket *ofputil_bucket_list_back(const struct ovs_list *);
1061 ofputil_bucket_has_liveness(const struct ofputil_bucket *bucket)
1063 return (bucket->watch_port != OFPP_ANY ||
1064 bucket->watch_group != OFPG_ANY);
1067 struct ofpbuf *ofputil_encode_group_stats_request(enum ofp_version,
1069 enum ofperr ofputil_decode_group_stats_request(
1070 const struct ofp_header *request, uint32_t *group_id);
1071 void ofputil_append_group_stats(struct ovs_list *replies,
1072 const struct ofputil_group_stats *);
1073 struct ofpbuf *ofputil_encode_group_features_request(enum ofp_version);
1074 struct ofpbuf *ofputil_encode_group_features_reply(
1075 const struct ofputil_group_features *, const struct ofp_header *request);
1076 void ofputil_decode_group_features_reply(const struct ofp_header *,
1077 struct ofputil_group_features *);
1078 void ofputil_uninit_group_mod(struct ofputil_group_mod *gm);
1079 struct ofpbuf *ofputil_encode_group_mod(enum ofp_version ofp_version,
1080 const struct ofputil_group_mod *gm);
1082 enum ofperr ofputil_decode_group_mod(const struct ofp_header *,
1083 struct ofputil_group_mod *);
1085 int ofputil_decode_group_stats_reply(struct ofpbuf *,
1086 struct ofputil_group_stats *);
1088 void ofputil_uninit_group_desc(struct ofputil_group_desc *gd);
1089 uint32_t ofputil_decode_group_desc_request(const struct ofp_header *);
1090 struct ofpbuf *ofputil_encode_group_desc_request(enum ofp_version,
1093 int ofputil_decode_group_desc_reply(struct ofputil_group_desc *,
1094 struct ofpbuf *, enum ofp_version);
1096 void ofputil_append_group_desc_reply(const struct ofputil_group_desc *,
1097 const struct ovs_list *buckets,
1098 struct ovs_list *replies);
1100 struct ofputil_bundle_ctrl_msg {
1106 struct ofputil_bundle_add_msg {
1109 const struct ofp_header *msg;
1114 enum ofperr ofputil_decode_bundle_ctrl(const struct ofp_header *,
1115 struct ofputil_bundle_ctrl_msg *);
1117 struct ofpbuf *ofputil_encode_bundle_ctrl_request(enum ofp_version,
1118 struct ofputil_bundle_ctrl_msg *);
1119 struct ofpbuf *ofputil_encode_bundle_ctrl_reply(const struct ofp_header *,
1120 struct ofputil_bundle_ctrl_msg *);
1122 struct ofpbuf *ofputil_encode_bundle_add(enum ofp_version ofp_version,
1123 struct ofputil_bundle_add_msg *msg);
1125 enum ofperr ofputil_decode_bundle_add(const struct ofp_header *,
1126 struct ofputil_bundle_add_msg *,
1127 enum ofptype *type);
1128 #endif /* ofp-util.h */