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.
18 #include "ofp-print.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "multipath.h"
32 #include "meta-flow.h"
34 #include "ofp-errors.h"
39 #include "unaligned.h"
40 #include "type-props.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_util);
45 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
46 * in the peer and so there's not much point in showing a lot of them. */
47 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
49 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
50 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
53 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
54 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
55 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
56 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
59 ofputil_wcbits_to_netmask(int wcbits)
62 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
65 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
66 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
67 * must be a CIDR netmask (see ip_is_cidr()). */
69 ofputil_netmask_to_wcbits(ovs_be32 netmask)
71 return 32 - ip_count_cidr_bits(netmask);
74 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
76 #define WC_INVARIANT_LIST \
77 WC_INVARIANT_BIT(IN_PORT) \
78 WC_INVARIANT_BIT(DL_SRC) \
79 WC_INVARIANT_BIT(DL_DST) \
80 WC_INVARIANT_BIT(DL_TYPE) \
81 WC_INVARIANT_BIT(NW_PROTO)
83 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
84 * actually have the same names and values. */
85 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
87 #undef WC_INVARIANT_BIT
89 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
91 static const flow_wildcards_t WC_INVARIANTS = 0
92 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
94 #undef WC_INVARIANT_BIT
97 /* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
98 * struct cls_rule. It is the caller's responsibility to handle the special
99 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
101 ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
103 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
105 /* Initialize most of rule->wc. */
106 flow_wildcards_init_catchall(wc);
107 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
109 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
110 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL
111 | FWW_ND_TARGET | FWW_IPV6_LABEL);
113 if (ofpfw & OFPFW_NW_TOS) {
114 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
115 * the enum than we can use. */
116 wc->wildcards |= FWW_NW_DSCP;
119 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
120 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
122 if (!(ofpfw & OFPFW_TP_SRC)) {
123 wc->tp_src_mask = htons(UINT16_MAX);
125 if (!(ofpfw & OFPFW_TP_DST)) {
126 wc->tp_dst_mask = htons(UINT16_MAX);
129 if (ofpfw & OFPFW_DL_DST) {
130 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
131 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
132 * and FWW_ETH_MCAST. */
133 wc->wildcards |= FWW_ETH_MCAST;
137 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
138 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
140 if (!(ofpfw & OFPFW_DL_VLAN)) {
141 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
145 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
148 ofputil_cls_rule_from_match(const struct ofp_match *match,
149 unsigned int priority, struct cls_rule *rule)
151 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
153 /* Initialize rule->priority, rule->wc. */
154 rule->priority = !ofpfw ? UINT16_MAX : priority;
155 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
157 /* Initialize most of rule->flow. */
158 rule->flow.nw_src = match->nw_src;
159 rule->flow.nw_dst = match->nw_dst;
160 rule->flow.in_port = ntohs(match->in_port);
161 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
162 rule->flow.tp_src = match->tp_src;
163 rule->flow.tp_dst = match->tp_dst;
164 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
165 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
166 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
167 rule->flow.nw_proto = match->nw_proto;
169 /* Translate VLANs. */
170 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
171 /* Match only packets without 802.1Q header.
173 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
175 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
176 * because we can't have a specific PCP without an 802.1Q header.
177 * However, older versions of OVS treated this as matching packets
178 * withut an 802.1Q header, so we do here too. */
179 rule->flow.vlan_tci = htons(0);
180 rule->wc.vlan_tci_mask = htons(0xffff);
182 ovs_be16 vid, pcp, tci;
184 vid = match->dl_vlan & htons(VLAN_VID_MASK);
185 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
186 tci = vid | pcp | htons(VLAN_CFI);
187 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
191 cls_rule_zero_wildcarded_fields(rule);
194 /* Convert 'rule' into the OpenFlow match structure 'match'. */
196 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
198 const struct flow_wildcards *wc = &rule->wc;
201 /* Figure out most OpenFlow wildcards. */
202 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
203 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
204 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
205 if (wc->wildcards & FWW_NW_DSCP) {
206 ofpfw |= OFPFW_NW_TOS;
208 if (!wc->tp_src_mask) {
209 ofpfw |= OFPFW_TP_SRC;
211 if (!wc->tp_dst_mask) {
212 ofpfw |= OFPFW_TP_DST;
215 /* Translate VLANs. */
216 match->dl_vlan = htons(0);
217 match->dl_vlan_pcp = 0;
218 if (rule->wc.vlan_tci_mask == htons(0)) {
219 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
220 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
221 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
222 match->dl_vlan = htons(OFP_VLAN_NONE);
224 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
225 ofpfw |= OFPFW_DL_VLAN;
227 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
230 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
231 ofpfw |= OFPFW_DL_VLAN_PCP;
233 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
237 /* Compose most of the match structure. */
238 match->wildcards = htonl(ofpfw);
239 match->in_port = htons(rule->flow.in_port);
240 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
241 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
242 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
243 match->nw_src = rule->flow.nw_src;
244 match->nw_dst = rule->flow.nw_dst;
245 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
246 match->nw_proto = rule->flow.nw_proto;
247 match->tp_src = rule->flow.tp_src;
248 match->tp_dst = rule->flow.tp_dst;
249 memset(match->pad1, '\0', sizeof match->pad1);
250 memset(match->pad2, '\0', sizeof match->pad2);
253 /* Given a 'dl_type' value in the format used in struct flow, returns the
254 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
256 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
258 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
259 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
263 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
264 * structure, returns the corresponding 'dl_type' value for use in struct
267 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
269 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
270 ? htons(FLOW_DL_TYPE_NONE)
274 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
278 static uint32_t next_xid = 1;
279 return htonl(next_xid++);
282 /* Basic parsing of OpenFlow messages. */
284 struct ofputil_msg_type {
285 enum ofputil_msg_code code; /* OFPUTIL_*. */
286 uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */
287 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
288 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
289 unsigned int min_size; /* Minimum total message size in bytes. */
290 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
291 * the message may exceed 'min_size' by an even multiple of this value. */
292 unsigned int extra_multiple;
295 /* Represents a malformed OpenFlow message. */
296 static const struct ofputil_msg_type ofputil_invalid_type = {
297 OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0
300 struct ofputil_msg_category {
301 const char *name; /* e.g. "OpenFlow message" */
302 const struct ofputil_msg_type *types;
304 enum ofperr missing_error; /* Error value for missing type. */
308 ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
310 switch (type->extra_multiple) {
312 if (size != type->min_size) {
313 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
314 "length %u (expected length %u)",
315 type->name, size, type->min_size);
316 return OFPERR_OFPBRC_BAD_LEN;
321 if (size < type->min_size) {
322 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
323 "length %u (expected length at least %u bytes)",
324 type->name, size, type->min_size);
325 return OFPERR_OFPBRC_BAD_LEN;
330 if (size < type->min_size
331 || (size - type->min_size) % type->extra_multiple) {
332 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
333 "length %u (must be exactly %u bytes or longer "
334 "by an integer multiple of %u bytes)",
336 type->min_size, type->extra_multiple);
337 return OFPERR_OFPBRC_BAD_LEN;
344 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
345 uint8_t version, uint32_t value,
346 const struct ofputil_msg_type **typep)
348 const struct ofputil_msg_type *type;
350 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
351 if (type->value == value
352 && (!type->ofp_version || version == type->ofp_version)) {
358 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
360 return cat->missing_error;
364 ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
365 const struct ofputil_msg_type **typep)
367 static const struct ofputil_msg_type nxt_messages[] = {
368 { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION,
369 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
370 sizeof(struct nx_role_request), 0 },
372 { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION,
373 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
374 sizeof(struct nx_role_request), 0 },
376 { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION,
377 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
378 sizeof(struct nx_set_flow_format), 0 },
380 { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION,
381 NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT",
382 sizeof(struct nx_set_packet_in_format), 0 },
384 { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION,
385 NXT_PACKET_IN, "NXT_PACKET_IN",
386 sizeof(struct nx_packet_in), 1 },
388 { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION,
389 NXT_FLOW_MOD, "NXT_FLOW_MOD",
390 sizeof(struct nx_flow_mod), 8 },
392 { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION,
393 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
394 sizeof(struct nx_flow_removed), 8 },
396 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION,
397 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
398 sizeof(struct nx_flow_mod_table_id), 0 },
400 { OFPUTIL_NXT_FLOW_AGE, OFP10_VERSION,
401 NXT_FLOW_AGE, "NXT_FLOW_AGE",
402 sizeof(struct nicira_header), 0 },
405 static const struct ofputil_msg_category nxt_category = {
406 "Nicira extension message",
407 nxt_messages, ARRAY_SIZE(nxt_messages),
408 OFPERR_OFPBRC_BAD_SUBTYPE
411 const struct ofp_vendor_header *ovh;
412 const struct nicira_header *nh;
414 if (length < sizeof(struct ofp_vendor_header)) {
415 if (length == ntohs(oh->length)) {
416 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
418 return OFPERR_OFPBRC_BAD_LEN;
421 ovh = (const struct ofp_vendor_header *) oh;
422 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
423 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
424 "vendor %"PRIx32, ntohl(ovh->vendor));
425 return OFPERR_OFPBRC_BAD_VENDOR;
428 if (length < sizeof(struct nicira_header)) {
429 if (length == ntohs(oh->length)) {
430 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
431 "length %u (expected at least %zu)",
432 ntohs(ovh->header.length),
433 sizeof(struct nicira_header));
435 return OFPERR_OFPBRC_BAD_LEN;
438 nh = (const struct nicira_header *) oh;
439 return ofputil_lookup_openflow_message(&nxt_category, oh->version,
440 ntohl(nh->subtype), typep);
444 check_nxstats_msg(const struct ofp_header *oh, size_t length)
446 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
449 if (length < sizeof(struct ofp_vendor_stats_msg)) {
450 if (length == ntohs(oh->length)) {
451 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
453 return OFPERR_OFPBRC_BAD_LEN;
456 memcpy(&vendor, osm + 1, sizeof vendor);
457 if (vendor != htonl(NX_VENDOR_ID)) {
458 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
459 "unknown vendor %"PRIx32, ntohl(vendor));
460 return OFPERR_OFPBRC_BAD_VENDOR;
463 if (length < sizeof(struct nicira_stats_msg)) {
464 if (length == ntohs(osm->header.length)) {
465 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
467 return OFPERR_OFPBRC_BAD_LEN;
474 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
475 const struct ofputil_msg_type **typep)
477 static const struct ofputil_msg_type nxst_requests[] = {
478 { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION,
479 NXST_FLOW, "NXST_FLOW request",
480 sizeof(struct nx_flow_stats_request), 8 },
482 { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION,
483 NXST_AGGREGATE, "NXST_AGGREGATE request",
484 sizeof(struct nx_aggregate_stats_request), 8 },
487 static const struct ofputil_msg_category nxst_request_category = {
488 "Nicira extension statistics request",
489 nxst_requests, ARRAY_SIZE(nxst_requests),
490 OFPERR_OFPBRC_BAD_SUBTYPE
493 const struct nicira_stats_msg *nsm;
496 error = check_nxstats_msg(oh, length);
501 nsm = (struct nicira_stats_msg *) oh;
502 return ofputil_lookup_openflow_message(&nxst_request_category, oh->version,
503 ntohl(nsm->subtype), typep);
507 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
508 const struct ofputil_msg_type **typep)
510 static const struct ofputil_msg_type nxst_replies[] = {
511 { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION,
512 NXST_FLOW, "NXST_FLOW reply",
513 sizeof(struct nicira_stats_msg), 8 },
515 { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION,
516 NXST_AGGREGATE, "NXST_AGGREGATE reply",
517 sizeof(struct nx_aggregate_stats_reply), 0 },
520 static const struct ofputil_msg_category nxst_reply_category = {
521 "Nicira extension statistics reply",
522 nxst_replies, ARRAY_SIZE(nxst_replies),
523 OFPERR_OFPBRC_BAD_SUBTYPE
526 const struct nicira_stats_msg *nsm;
529 error = check_nxstats_msg(oh, length);
534 nsm = (struct nicira_stats_msg *) oh;
535 return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version,
536 ntohl(nsm->subtype), typep);
540 check_stats_msg(const struct ofp_header *oh, size_t length)
542 if (length < sizeof(struct ofp_stats_msg)) {
543 if (length == ntohs(oh->length)) {
544 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
546 return OFPERR_OFPBRC_BAD_LEN;
553 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
554 const struct ofputil_msg_type **typep)
556 static const struct ofputil_msg_type ofpst_requests[] = {
557 { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION,
558 OFPST_DESC, "OFPST_DESC request",
559 sizeof(struct ofp_stats_msg), 0 },
561 { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION,
562 OFPST_FLOW, "OFPST_FLOW request",
563 sizeof(struct ofp_flow_stats_request), 0 },
565 { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION,
566 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
567 sizeof(struct ofp_flow_stats_request), 0 },
569 { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION,
570 OFPST_TABLE, "OFPST_TABLE request",
571 sizeof(struct ofp_stats_msg), 0 },
573 { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION,
574 OFPST_PORT, "OFPST_PORT request",
575 sizeof(struct ofp_port_stats_request), 0 },
577 { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION,
578 OFPST_QUEUE, "OFPST_QUEUE request",
579 sizeof(struct ofp_queue_stats_request), 0 },
582 OFPST_VENDOR, "OFPST_VENDOR request",
583 sizeof(struct ofp_vendor_stats_msg), 1 },
586 static const struct ofputil_msg_category ofpst_request_category = {
587 "OpenFlow statistics",
588 ofpst_requests, ARRAY_SIZE(ofpst_requests),
589 OFPERR_OFPBRC_BAD_STAT
592 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
595 error = check_stats_msg(oh, length);
600 error = ofputil_lookup_openflow_message(&ofpst_request_category,
601 oh->version, ntohs(request->type),
603 if (!error && request->type == htons(OFPST_VENDOR)) {
604 error = ofputil_decode_nxst_request(oh, length, typep);
610 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
611 const struct ofputil_msg_type **typep)
613 static const struct ofputil_msg_type ofpst_replies[] = {
614 { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION,
615 OFPST_DESC, "OFPST_DESC reply",
616 sizeof(struct ofp_desc_stats), 0 },
618 { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION,
619 OFPST_FLOW, "OFPST_FLOW reply",
620 sizeof(struct ofp_stats_msg), 1 },
622 { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION,
623 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
624 sizeof(struct ofp_aggregate_stats_reply), 0 },
626 { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION,
627 OFPST_TABLE, "OFPST_TABLE reply",
628 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
630 { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION,
631 OFPST_PORT, "OFPST_PORT reply",
632 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
634 { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION,
635 OFPST_QUEUE, "OFPST_QUEUE reply",
636 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
639 OFPST_VENDOR, "OFPST_VENDOR reply",
640 sizeof(struct ofp_vendor_stats_msg), 1 },
643 static const struct ofputil_msg_category ofpst_reply_category = {
644 "OpenFlow statistics",
645 ofpst_replies, ARRAY_SIZE(ofpst_replies),
646 OFPERR_OFPBRC_BAD_STAT
649 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
652 error = check_stats_msg(oh, length);
657 error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version,
658 ntohs(reply->type), typep);
659 if (!error && reply->type == htons(OFPST_VENDOR)) {
660 error = ofputil_decode_nxst_reply(oh, length, typep);
666 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
667 const struct ofputil_msg_type **typep)
669 static const struct ofputil_msg_type ofpt_messages[] = {
670 { OFPUTIL_OFPT_HELLO, OFP10_VERSION,
671 OFPT_HELLO, "OFPT_HELLO",
672 sizeof(struct ofp_hello), 1 },
674 { OFPUTIL_OFPT_ERROR, 0,
675 OFPT_ERROR, "OFPT_ERROR",
676 sizeof(struct ofp_error_msg), 1 },
678 { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION,
679 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
680 sizeof(struct ofp_header), 1 },
682 { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION,
683 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
684 sizeof(struct ofp_header), 1 },
686 { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION,
687 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
688 sizeof(struct ofp_header), 0 },
690 { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION,
691 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
692 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
694 { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION,
695 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
696 sizeof(struct ofp_header), 0 },
698 { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION,
699 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
700 sizeof(struct ofp_switch_config), 0 },
702 { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION,
703 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
704 sizeof(struct ofp_switch_config), 0 },
706 { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION,
707 OFPT_PACKET_IN, "OFPT_PACKET_IN",
708 offsetof(struct ofp_packet_in, data), 1 },
710 { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION,
711 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
712 sizeof(struct ofp_flow_removed), 0 },
714 { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION,
715 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
716 sizeof(struct ofp_port_status), 0 },
718 { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION,
719 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
720 sizeof(struct ofp_packet_out), 1 },
722 { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION,
723 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
724 sizeof(struct ofp_flow_mod), 1 },
726 { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION,
727 OFPT_PORT_MOD, "OFPT_PORT_MOD",
728 sizeof(struct ofp_port_mod), 0 },
731 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
732 sizeof(struct ofp_stats_msg), 1 },
735 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
736 sizeof(struct ofp_stats_msg), 1 },
738 { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION,
739 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
740 sizeof(struct ofp_header), 0 },
742 { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION,
743 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
744 sizeof(struct ofp_header), 0 },
747 OFPT_VENDOR, "OFPT_VENDOR",
748 sizeof(struct ofp_vendor_header), 1 },
751 static const struct ofputil_msg_category ofpt_category = {
753 ofpt_messages, ARRAY_SIZE(ofpt_messages),
754 OFPERR_OFPBRC_BAD_TYPE
759 error = ofputil_lookup_openflow_message(&ofpt_category, oh->version,
764 error = ofputil_decode_vendor(oh, length, typep);
767 case OFPT_STATS_REQUEST:
768 error = ofputil_decode_ofpst_request(oh, length, typep);
771 case OFPT_STATS_REPLY:
772 error = ofputil_decode_ofpst_reply(oh, length, typep);
781 /* Decodes the message type represented by 'oh'. Returns 0 if successful or an
782 * OpenFlow error code on failure. Either way, stores in '*typep' a type
783 * structure that can be inspected with the ofputil_msg_type_*() functions.
785 * oh->length must indicate the correct length of the message (and must be at
786 * least sizeof(struct ofp_header)).
788 * Success indicates that 'oh' is at least as long as the minimum-length
789 * message of its type. */
791 ofputil_decode_msg_type(const struct ofp_header *oh,
792 const struct ofputil_msg_type **typep)
794 size_t length = ntohs(oh->length);
797 error = ofputil_decode_msg_type__(oh, length, typep);
799 error = ofputil_check_length(*typep, length);
802 *typep = &ofputil_invalid_type;
807 /* Decodes the message type represented by 'oh', of which only the first
808 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
809 * code on failure. Either way, stores in '*typep' a type structure that can
810 * be inspected with the ofputil_msg_type_*() functions. */
812 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
813 const struct ofputil_msg_type **typep)
817 error = (length >= sizeof *oh
818 ? ofputil_decode_msg_type__(oh, length, typep)
819 : OFPERR_OFPBRC_BAD_LEN);
821 *typep = &ofputil_invalid_type;
826 /* Returns an OFPUTIL_* message type code for 'type'. */
827 enum ofputil_msg_code
828 ofputil_msg_type_code(const struct ofputil_msg_type *type)
836 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
838 switch (flow_format) {
839 case NXFF_OPENFLOW10:
848 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
850 switch (flow_format) {
851 case NXFF_OPENFLOW10:
861 ofputil_flow_format_from_string(const char *s)
863 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
864 : !strcmp(s, "nxm") ? NXFF_NXM
869 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
871 switch (packet_in_format) {
872 case NXPIF_OPENFLOW10:
881 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
883 switch (packet_in_format) {
884 case NXPIF_OPENFLOW10:
894 ofputil_packet_in_format_from_string(const char *s)
896 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
897 : !strcmp(s, "nxm") ? NXPIF_NXM
902 regs_fully_wildcarded(const struct flow_wildcards *wc)
906 for (i = 0; i < FLOW_N_REGS; i++) {
907 if (wc->reg_masks[i] != 0) {
914 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
915 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
916 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
917 * NXFF_OPENFLOW10 for backward compatibility. */
919 ofputil_min_flow_format(const struct cls_rule *rule)
921 const struct flow_wildcards *wc = &rule->wc;
923 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
925 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
926 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
930 /* Only NXM supports matching ARP hardware addresses. */
931 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
935 /* Only NXM supports matching IPv6 traffic. */
936 if (!(wc->wildcards & FWW_DL_TYPE)
937 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
941 /* Only NXM supports matching registers. */
942 if (!regs_fully_wildcarded(wc)) {
946 /* Only NXM supports matching tun_id. */
947 if (wc->tun_id_mask != htonll(0)) {
951 /* Only NXM supports matching fragments. */
952 if (wc->nw_frag_mask) {
956 /* Only NXM supports matching IPv6 flow label. */
957 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
961 /* Only NXM supports matching IP ECN bits. */
962 if (!(wc->wildcards & FWW_NW_ECN)) {
966 /* Only NXM supports matching IP TTL/hop limit. */
967 if (!(wc->wildcards & FWW_NW_TTL)) {
971 /* Only NXM supports bitwise matching on transport port. */
972 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
973 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
977 /* Other formats can express this rule. */
978 return NXFF_OPENFLOW10;
981 /* Returns an OpenFlow message that can be used to set the flow format to
984 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
986 struct nx_set_flow_format *sff;
989 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
990 sff->format = htonl(flow_format);
996 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
998 struct nx_set_packet_in_format *spif;
1001 spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg);
1002 spif->format = htonl(packet_in_format);
1007 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1008 * extension on or off (according to 'flow_mod_table_id'). */
1010 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1012 struct nx_flow_mod_table_id *nfmti;
1015 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
1016 nfmti->set = flow_mod_table_id;
1020 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1021 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1024 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1025 * enabled, false otherwise.
1027 * Does not validate the flow_mod actions. */
1029 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1030 const struct ofp_header *oh, bool flow_mod_table_id)
1032 const struct ofputil_msg_type *type;
1036 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1038 ofputil_decode_msg_type(oh, &type);
1039 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
1040 /* Standard OpenFlow flow_mod. */
1041 const struct ofp_flow_mod *ofm;
1045 /* Dissect the message. */
1046 ofm = ofpbuf_pull(&b, sizeof *ofm);
1047 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1052 /* Set priority based on original wildcards. Normally we'd allow
1053 * ofputil_cls_rule_from_match() to do this for us, but
1054 * ofputil_normalize_rule() can put wildcards where the original flow
1055 * didn't have them. */
1056 priority = ntohs(ofm->priority);
1057 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
1058 priority = UINT16_MAX;
1061 /* Translate the rule. */
1062 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
1063 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
1065 /* Translate the message. */
1066 fm->cookie = ofm->cookie;
1067 fm->cookie_mask = htonll(UINT64_MAX);
1068 command = ntohs(ofm->command);
1069 fm->idle_timeout = ntohs(ofm->idle_timeout);
1070 fm->hard_timeout = ntohs(ofm->hard_timeout);
1071 fm->buffer_id = ntohl(ofm->buffer_id);
1072 fm->out_port = ntohs(ofm->out_port);
1073 fm->flags = ntohs(ofm->flags);
1074 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1075 /* Nicira extended flow_mod. */
1076 const struct nx_flow_mod *nfm;
1079 /* Dissect the message. */
1080 nfm = ofpbuf_pull(&b, sizeof *nfm);
1081 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1082 &fm->cr, &fm->cookie, &fm->cookie_mask);
1086 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1091 /* Translate the message. */
1092 command = ntohs(nfm->command);
1093 if (command == OFPFC_ADD) {
1094 if (fm->cookie_mask) {
1095 /* The "NXM_NX_COOKIE*" matches are not valid for flow
1096 * additions. Additions must use the "cookie" field of
1097 * the "nx_flow_mod" structure. */
1098 return OFPERR_NXBRC_NXM_INVALID;
1100 fm->cookie = nfm->cookie;
1101 fm->cookie_mask = htonll(UINT64_MAX);
1104 fm->idle_timeout = ntohs(nfm->idle_timeout);
1105 fm->hard_timeout = ntohs(nfm->hard_timeout);
1106 fm->buffer_id = ntohl(nfm->buffer_id);
1107 fm->out_port = ntohs(nfm->out_port);
1108 fm->flags = ntohs(nfm->flags);
1113 if (flow_mod_table_id) {
1114 fm->command = command & 0xff;
1115 fm->table_id = command >> 8;
1117 fm->command = command;
1118 fm->table_id = 0xff;
1124 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1125 * 'flow_format' and returns the message.
1127 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1128 * enabled, false otherwise. */
1130 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1131 enum nx_flow_format flow_format,
1132 bool flow_mod_table_id)
1134 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1138 command = (flow_mod_table_id
1139 ? (fm->command & 0xff) | (fm->table_id << 8)
1142 if (flow_format == NXFF_OPENFLOW10) {
1143 struct ofp_flow_mod *ofm;
1145 msg = ofpbuf_new(sizeof *ofm + actions_len);
1146 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1147 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1148 ofm->cookie = fm->cookie;
1149 ofm->command = htons(command);
1150 ofm->idle_timeout = htons(fm->idle_timeout);
1151 ofm->hard_timeout = htons(fm->hard_timeout);
1152 ofm->priority = htons(fm->cr.priority);
1153 ofm->buffer_id = htonl(fm->buffer_id);
1154 ofm->out_port = htons(fm->out_port);
1155 ofm->flags = htons(fm->flags);
1156 } else if (flow_format == NXFF_NXM) {
1157 struct nx_flow_mod *nfm;
1160 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1161 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1163 nfm->command = htons(command);
1164 if (command == OFPFC_ADD) {
1165 nfm->cookie = fm->cookie;
1166 match_len = nx_put_match(msg, &fm->cr, 0, 0);
1169 match_len = nx_put_match(msg, &fm->cr,
1170 fm->cookie, fm->cookie_mask);
1172 nfm->idle_timeout = htons(fm->idle_timeout);
1173 nfm->hard_timeout = htons(fm->hard_timeout);
1174 nfm->priority = htons(fm->cr.priority);
1175 nfm->buffer_id = htonl(fm->buffer_id);
1176 nfm->out_port = htons(fm->out_port);
1177 nfm->flags = htons(fm->flags);
1178 nfm->match_len = htons(match_len);
1183 ofpbuf_put(msg, fm->actions, actions_len);
1184 update_openflow_length(msg);
1189 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1190 const struct ofp_header *oh,
1193 const struct ofp_flow_stats_request *ofsr =
1194 (const struct ofp_flow_stats_request *) oh;
1196 fsr->aggregate = aggregate;
1197 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1198 fsr->out_port = ntohs(ofsr->out_port);
1199 fsr->table_id = ofsr->table_id;
1200 fsr->cookie = fsr->cookie_mask = htonll(0);
1206 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1207 const struct ofp_header *oh,
1210 const struct nx_flow_stats_request *nfsr;
1214 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1216 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1217 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match,
1218 &fsr->cookie, &fsr->cookie_mask);
1223 return OFPERR_OFPBRC_BAD_LEN;
1226 fsr->aggregate = aggregate;
1227 fsr->out_port = ntohs(nfsr->out_port);
1228 fsr->table_id = nfsr->table_id;
1233 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1234 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1235 * successful, otherwise an OpenFlow error code. */
1237 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1238 const struct ofp_header *oh)
1240 const struct ofputil_msg_type *type;
1244 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1246 ofputil_decode_msg_type(oh, &type);
1247 code = ofputil_msg_type_code(type);
1249 case OFPUTIL_OFPST_FLOW_REQUEST:
1250 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1252 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1253 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1255 case OFPUTIL_NXST_FLOW_REQUEST:
1256 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1258 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1259 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1262 /* Hey, the caller lied. */
1267 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1268 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1269 * 'flow_format', and returns the message. */
1271 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1272 enum nx_flow_format flow_format)
1276 if (flow_format == NXFF_OPENFLOW10) {
1277 struct ofp_flow_stats_request *ofsr;
1280 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1281 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1282 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1283 ofsr->table_id = fsr->table_id;
1284 ofsr->out_port = htons(fsr->out_port);
1285 } else if (flow_format == NXFF_NXM) {
1286 struct nx_flow_stats_request *nfsr;
1290 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1291 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1292 match_len = nx_put_match(msg, &fsr->match,
1293 fsr->cookie, fsr->cookie_mask);
1296 nfsr->out_port = htons(fsr->out_port);
1297 nfsr->match_len = htons(match_len);
1298 nfsr->table_id = fsr->table_id;
1306 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1307 * ofputil_flow_stats in 'fs'.
1309 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1310 * OpenFlow message. Calling this function multiple times for a single 'msg'
1311 * iterates through the replies. The caller must initially leave 'msg''s layer
1312 * pointers null and not modify them between calls.
1314 * Most switches don't send the values needed to populate fs->idle_age and
1315 * fs->hard_age, so those members will usually be set to 0. If the switch from
1316 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1317 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1318 * 'idle_age' and 'hard_age' members in 'fs'.
1320 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1321 * otherwise a positive errno value. */
1323 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1325 bool flow_age_extension)
1327 const struct ofputil_msg_type *type;
1330 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1331 code = ofputil_msg_type_code(type);
1333 msg->l2 = msg->data;
1334 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1335 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1336 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1337 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1345 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1346 const struct ofp_flow_stats *ofs;
1349 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1351 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1352 "bytes at end", msg->size);
1356 length = ntohs(ofs->length);
1357 if (length < sizeof *ofs) {
1358 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1359 "length %zu", length);
1363 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1364 &fs->actions, &fs->n_actions)) {
1368 fs->cookie = get_32aligned_be64(&ofs->cookie);
1369 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1371 fs->table_id = ofs->table_id;
1372 fs->duration_sec = ntohl(ofs->duration_sec);
1373 fs->duration_nsec = ntohl(ofs->duration_nsec);
1374 fs->idle_timeout = ntohs(ofs->idle_timeout);
1375 fs->hard_timeout = ntohs(ofs->hard_timeout);
1378 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1379 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1380 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1381 const struct nx_flow_stats *nfs;
1382 size_t match_len, length;
1384 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1386 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1387 "bytes at end", msg->size);
1391 length = ntohs(nfs->length);
1392 match_len = ntohs(nfs->match_len);
1393 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1394 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1395 "claims invalid length %zu", match_len, length);
1398 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1403 if (ofputil_pull_actions(msg,
1404 length - sizeof *nfs - ROUND_UP(match_len, 8),
1405 &fs->actions, &fs->n_actions)) {
1409 fs->cookie = nfs->cookie;
1410 fs->table_id = nfs->table_id;
1411 fs->duration_sec = ntohl(nfs->duration_sec);
1412 fs->duration_nsec = ntohl(nfs->duration_nsec);
1413 fs->idle_timeout = ntohs(nfs->idle_timeout);
1414 fs->hard_timeout = ntohs(nfs->hard_timeout);
1417 if (flow_age_extension) {
1418 if (nfs->idle_age) {
1419 fs->idle_age = ntohs(nfs->idle_age) - 1;
1421 if (nfs->hard_age) {
1422 fs->hard_age = ntohs(nfs->hard_age) - 1;
1425 fs->packet_count = ntohll(nfs->packet_count);
1426 fs->byte_count = ntohll(nfs->byte_count);
1434 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1436 * We use this in situations where OVS internally uses UINT64_MAX to mean
1437 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1439 unknown_to_zero(uint64_t count)
1441 return count != UINT64_MAX ? count : 0;
1444 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1445 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1446 * have been initialized with ofputil_start_stats_reply(). */
1448 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1449 struct list *replies)
1451 size_t act_len = fs->n_actions * sizeof *fs->actions;
1452 const struct ofp_stats_msg *osm;
1454 osm = ofpbuf_from_list(list_back(replies))->data;
1455 if (osm->type == htons(OFPST_FLOW)) {
1456 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1457 struct ofp_flow_stats *ofs;
1459 ofs = ofputil_append_stats_reply(len, replies);
1460 ofs->length = htons(len);
1461 ofs->table_id = fs->table_id;
1463 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1464 ofs->duration_sec = htonl(fs->duration_sec);
1465 ofs->duration_nsec = htonl(fs->duration_nsec);
1466 ofs->priority = htons(fs->rule.priority);
1467 ofs->idle_timeout = htons(fs->idle_timeout);
1468 ofs->hard_timeout = htons(fs->hard_timeout);
1469 memset(ofs->pad2, 0, sizeof ofs->pad2);
1470 put_32aligned_be64(&ofs->cookie, fs->cookie);
1471 put_32aligned_be64(&ofs->packet_count,
1472 htonll(unknown_to_zero(fs->packet_count)));
1473 put_32aligned_be64(&ofs->byte_count,
1474 htonll(unknown_to_zero(fs->byte_count)));
1475 memcpy(ofs->actions, fs->actions, act_len);
1476 } else if (osm->type == htons(OFPST_VENDOR)) {
1477 struct nx_flow_stats *nfs;
1481 msg = ofputil_reserve_stats_reply(
1482 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1483 start_len = msg->size;
1485 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1486 nfs->table_id = fs->table_id;
1488 nfs->duration_sec = htonl(fs->duration_sec);
1489 nfs->duration_nsec = htonl(fs->duration_nsec);
1490 nfs->priority = htons(fs->rule.priority);
1491 nfs->idle_timeout = htons(fs->idle_timeout);
1492 nfs->hard_timeout = htons(fs->hard_timeout);
1493 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1494 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1496 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1497 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1499 nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0));
1500 nfs->cookie = fs->cookie;
1501 nfs->packet_count = htonll(fs->packet_count);
1502 nfs->byte_count = htonll(fs->byte_count);
1503 ofpbuf_put(msg, fs->actions, act_len);
1504 nfs->length = htons(msg->size - start_len);
1510 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1511 * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */
1513 ofputil_encode_aggregate_stats_reply(
1514 const struct ofputil_aggregate_stats *stats,
1515 const struct ofp_stats_msg *request)
1519 if (request->type == htons(OFPST_AGGREGATE)) {
1520 struct ofp_aggregate_stats_reply *asr;
1522 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
1523 put_32aligned_be64(&asr->packet_count,
1524 htonll(unknown_to_zero(stats->packet_count)));
1525 put_32aligned_be64(&asr->byte_count,
1526 htonll(unknown_to_zero(stats->byte_count)));
1527 asr->flow_count = htonl(stats->flow_count);
1528 } else if (request->type == htons(OFPST_VENDOR)) {
1529 struct nx_aggregate_stats_reply *nasr;
1531 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1532 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1533 nasr->packet_count = htonll(stats->packet_count);
1534 nasr->byte_count = htonll(stats->byte_count);
1535 nasr->flow_count = htonl(stats->flow_count);
1543 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1544 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1545 * an OpenFlow error code. */
1547 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1548 const struct ofp_header *oh)
1550 const struct ofputil_msg_type *type;
1551 enum ofputil_msg_code code;
1553 ofputil_decode_msg_type(oh, &type);
1554 code = ofputil_msg_type_code(type);
1555 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1556 const struct ofp_flow_removed *ofr;
1558 ofr = (const struct ofp_flow_removed *) oh;
1559 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1561 fr->cookie = ofr->cookie;
1562 fr->reason = ofr->reason;
1563 fr->duration_sec = ntohl(ofr->duration_sec);
1564 fr->duration_nsec = ntohl(ofr->duration_nsec);
1565 fr->idle_timeout = ntohs(ofr->idle_timeout);
1566 fr->packet_count = ntohll(ofr->packet_count);
1567 fr->byte_count = ntohll(ofr->byte_count);
1568 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1569 struct nx_flow_removed *nfr;
1573 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1575 nfr = ofpbuf_pull(&b, sizeof *nfr);
1576 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1577 &fr->rule, NULL, NULL);
1582 return OFPERR_OFPBRC_BAD_LEN;
1585 fr->cookie = nfr->cookie;
1586 fr->reason = nfr->reason;
1587 fr->duration_sec = ntohl(nfr->duration_sec);
1588 fr->duration_nsec = ntohl(nfr->duration_nsec);
1589 fr->idle_timeout = ntohs(nfr->idle_timeout);
1590 fr->packet_count = ntohll(nfr->packet_count);
1591 fr->byte_count = ntohll(nfr->byte_count);
1599 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1600 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1603 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1604 enum nx_flow_format flow_format)
1608 if (flow_format == NXFF_OPENFLOW10) {
1609 struct ofp_flow_removed *ofr;
1611 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1613 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1614 ofr->cookie = fr->cookie;
1615 ofr->priority = htons(fr->rule.priority);
1616 ofr->reason = fr->reason;
1617 ofr->duration_sec = htonl(fr->duration_sec);
1618 ofr->duration_nsec = htonl(fr->duration_nsec);
1619 ofr->idle_timeout = htons(fr->idle_timeout);
1620 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1621 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1622 } else if (flow_format == NXFF_NXM) {
1623 struct nx_flow_removed *nfr;
1626 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1627 match_len = nx_put_match(msg, &fr->rule, 0, 0);
1630 nfr->cookie = fr->cookie;
1631 nfr->priority = htons(fr->rule.priority);
1632 nfr->reason = fr->reason;
1633 nfr->duration_sec = htonl(fr->duration_sec);
1634 nfr->duration_nsec = htonl(fr->duration_nsec);
1635 nfr->idle_timeout = htons(fr->idle_timeout);
1636 nfr->match_len = htons(match_len);
1637 nfr->packet_count = htonll(fr->packet_count);
1638 nfr->byte_count = htonll(fr->byte_count);
1647 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
1648 const struct ofp_header *oh)
1650 const struct ofputil_msg_type *type;
1651 enum ofputil_msg_code code;
1653 ofputil_decode_msg_type(oh, &type);
1654 code = ofputil_msg_type_code(type);
1655 memset(pin, 0, sizeof *pin);
1657 if (code == OFPUTIL_OFPT_PACKET_IN) {
1658 const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh;
1660 pin->packet = opi->data;
1661 pin->packet_len = ntohs(opi->header.length)
1662 - offsetof(struct ofp_packet_in, data);
1664 pin->fmd.in_port = ntohs(opi->in_port);
1665 pin->reason = opi->reason;
1666 pin->buffer_id = ntohl(opi->buffer_id);
1667 pin->total_len = ntohs(opi->total_len);
1668 } else if (code == OFPUTIL_NXT_PACKET_IN) {
1669 const struct nx_packet_in *npi;
1670 struct cls_rule rule;
1674 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1676 npi = ofpbuf_pull(&b, sizeof *npi);
1677 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
1683 if (!ofpbuf_try_pull(&b, 2)) {
1684 return OFPERR_OFPBRC_BAD_LEN;
1687 pin->packet = b.data;
1688 pin->packet_len = b.size;
1689 pin->reason = npi->reason;
1690 pin->table_id = npi->table_id;
1691 pin->cookie = npi->cookie;
1693 pin->fmd.in_port = rule.flow.in_port;
1695 pin->fmd.tun_id = rule.flow.tun_id;
1696 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
1698 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
1699 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
1700 sizeof pin->fmd.reg_masks);
1702 pin->buffer_id = ntohl(npi->buffer_id);
1703 pin->total_len = ntohs(npi->total_len);
1711 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
1712 * in the format specified by 'packet_in_format'. */
1714 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1715 enum nx_packet_in_format packet_in_format)
1717 size_t send_len = MIN(pin->send_len, pin->packet_len);
1718 struct ofpbuf *packet;
1720 /* Add OFPT_PACKET_IN. */
1721 if (packet_in_format == NXPIF_OPENFLOW10) {
1722 size_t header_len = offsetof(struct ofp_packet_in, data);
1723 struct ofp_packet_in *opi;
1725 packet = ofpbuf_new(send_len + header_len);
1726 opi = ofpbuf_put_zeros(packet, header_len);
1727 opi->header.version = OFP_VERSION;
1728 opi->header.type = OFPT_PACKET_IN;
1729 opi->total_len = htons(pin->total_len);
1730 opi->in_port = htons(pin->fmd.in_port);
1731 opi->reason = pin->reason;
1732 opi->buffer_id = htonl(pin->buffer_id);
1734 ofpbuf_put(packet, pin->packet, send_len);
1735 } else if (packet_in_format == NXPIF_NXM) {
1736 struct nx_packet_in *npi;
1737 struct cls_rule rule;
1741 /* Estimate of required PACKET_IN length includes the NPI header, space
1742 * for the match (2 times sizeof the metadata seems like enough), 2
1743 * bytes for padding, and the packet length. */
1744 packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2
1747 cls_rule_init_catchall(&rule, 0);
1748 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
1749 pin->fmd.tun_id_mask);
1751 for (i = 0; i < FLOW_N_REGS; i++) {
1752 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
1753 pin->fmd.reg_masks[i]);
1756 cls_rule_set_in_port(&rule, pin->fmd.in_port);
1758 ofpbuf_put_zeros(packet, sizeof *npi);
1759 match_len = nx_put_match(packet, &rule, 0, 0);
1760 ofpbuf_put_zeros(packet, 2);
1761 ofpbuf_put(packet, pin->packet, send_len);
1764 npi->nxh.header.version = OFP_VERSION;
1765 npi->nxh.header.type = OFPT_VENDOR;
1766 npi->nxh.vendor = htonl(NX_VENDOR_ID);
1767 npi->nxh.subtype = htonl(NXT_PACKET_IN);
1769 npi->buffer_id = htonl(pin->buffer_id);
1770 npi->total_len = htons(pin->total_len);
1771 npi->reason = pin->reason;
1772 npi->table_id = pin->table_id;
1773 npi->cookie = pin->cookie;
1774 npi->match_len = htons(match_len);
1778 update_openflow_length(packet);
1784 ofputil_decode_packet_out(struct ofputil_packet_out *po,
1785 const struct ofp_packet_out *opo)
1790 po->buffer_id = ntohl(opo->buffer_id);
1791 po->in_port = ntohs(opo->in_port);
1792 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
1793 && po->in_port != OFPP_NONE) {
1794 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
1796 return OFPERR_NXBRC_BAD_IN_PORT;
1799 ofpbuf_use_const(&b, opo, ntohs(opo->header.length));
1800 ofpbuf_pull(&b, sizeof *opo);
1802 error = ofputil_pull_actions(&b, ntohs(opo->actions_len),
1803 &po->actions, &po->n_actions);
1808 if (po->buffer_id == UINT32_MAX) {
1809 po->packet = b.data;
1810 po->packet_len = b.size;
1820 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
1822 struct ofp_packet_out *opo;
1827 actions_len = po->n_actions * sizeof *po->actions;
1828 size = sizeof *opo + actions_len;
1829 if (po->buffer_id == UINT32_MAX) {
1830 size += po->packet_len;
1833 msg = ofpbuf_new(size);
1834 opo = put_openflow(sizeof *opo, OFPT_PACKET_OUT, msg);
1835 opo->buffer_id = htonl(po->buffer_id);
1836 opo->in_port = htons(po->in_port);
1837 opo->actions_len = htons(actions_len);
1838 ofpbuf_put(msg, po->actions, actions_len);
1839 if (po->buffer_id == UINT32_MAX) {
1840 ofpbuf_put(msg, po->packet, po->packet_len);
1842 update_openflow_length(msg);
1847 /* Returns a string representing the message type of 'type'. The string is the
1848 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1849 * messages, the constant is followed by "request" or "reply",
1850 * e.g. "OFPST_AGGREGATE reply". */
1852 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1857 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1858 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1859 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1862 * The caller is responsible for freeing '*bufferp' when it is no longer
1865 * The OpenFlow header length is initially set to 'openflow_len'; if the
1866 * message is later extended, the length should be updated with
1867 * update_openflow_length() before sending.
1869 * Returns the header. */
1871 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1873 *bufferp = ofpbuf_new(openflow_len);
1874 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1877 /* Similar to make_openflow() but creates a Nicira vendor extension message
1878 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1880 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1882 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1885 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1886 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1887 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1890 * The caller is responsible for freeing '*bufferp' when it is no longer
1893 * The OpenFlow header length is initially set to 'openflow_len'; if the
1894 * message is later extended, the length should be updated with
1895 * update_openflow_length() before sending.
1897 * Returns the header. */
1899 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1900 struct ofpbuf **bufferp)
1902 *bufferp = ofpbuf_new(openflow_len);
1903 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1906 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1907 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1909 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1910 struct ofpbuf **bufferp)
1912 *bufferp = ofpbuf_new(openflow_len);
1913 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1916 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1917 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1918 * beyond the header, if any, are zeroed.
1920 * The OpenFlow header length is initially set to 'openflow_len'; if the
1921 * message is later extended, the length should be updated with
1922 * update_openflow_length() before sending.
1924 * Returns the header. */
1926 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1928 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1931 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1932 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1933 * the header, if any, are zeroed.
1935 * The OpenFlow header length is initially set to 'openflow_len'; if the
1936 * message is later extended, the length should be updated with
1937 * update_openflow_length() before sending.
1939 * Returns the header. */
1941 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1942 struct ofpbuf *buffer)
1944 struct ofp_header *oh;
1946 assert(openflow_len >= sizeof *oh);
1947 assert(openflow_len <= UINT16_MAX);
1949 oh = ofpbuf_put_uninit(buffer, openflow_len);
1950 oh->version = OFP_VERSION;
1952 oh->length = htons(openflow_len);
1954 memset(oh + 1, 0, openflow_len - sizeof *oh);
1958 /* Similar to put_openflow() but append a Nicira vendor extension message with
1959 * the specific 'subtype'. 'subtype' should be in host byte order. */
1961 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1963 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1966 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1967 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1969 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1970 struct ofpbuf *buffer)
1972 struct nicira_header *nxh;
1974 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1975 nxh->vendor = htonl(NX_VENDOR_ID);
1976 nxh->subtype = htonl(subtype);
1980 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1981 * 'buffer->size'. */
1983 update_openflow_length(struct ofpbuf *buffer)
1985 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1986 oh->length = htons(buffer->size);
1990 put_stats__(ovs_be32 xid, uint8_t ofp_type,
1991 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
1994 if (ofpst_type == htons(OFPST_VENDOR)) {
1995 struct nicira_stats_msg *nsm;
1997 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
1998 nsm->vsm.osm.type = ofpst_type;
1999 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
2000 nsm->subtype = nxst_subtype;
2002 struct ofp_stats_msg *osm;
2004 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
2005 osm->type = ofpst_type;
2009 /* Creates a statistics request message with total length 'openflow_len'
2010 * (including all headers) and the given 'ofpst_type', and stores the buffer
2011 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
2012 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
2013 * subtype (otherwise 'nxst_subtype' is ignored).
2015 * Initializes bytes following the headers to all-bits-zero.
2017 * Returns the first byte of the new message. */
2019 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
2020 uint32_t nxst_subtype, struct ofpbuf **bufferp)
2024 msg = *bufferp = ofpbuf_new(openflow_len);
2025 put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
2026 htons(ofpst_type), htonl(nxst_subtype), msg);
2027 ofpbuf_padto(msg, openflow_len);
2033 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
2035 assert(request->header.type == OFPT_STATS_REQUEST ||
2036 request->header.type == OFPT_STATS_REPLY);
2037 put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
2038 (request->type != htons(OFPST_VENDOR)
2040 : ((const struct nicira_stats_msg *) request)->subtype),
2044 /* Creates a statistics reply message with total length 'openflow_len'
2045 * (including all headers) and the same type (either a standard OpenFlow
2046 * statistics type or a Nicira extension type and subtype) as 'request', and
2047 * stores the buffer containing the new message in '*bufferp'.
2049 * Initializes bytes following the headers to all-bits-zero.
2051 * Returns the first byte of the new message. */
2053 ofputil_make_stats_reply(size_t openflow_len,
2054 const struct ofp_stats_msg *request,
2055 struct ofpbuf **bufferp)
2059 msg = *bufferp = ofpbuf_new(openflow_len);
2060 put_stats_reply__(request, msg);
2061 ofpbuf_padto(msg, openflow_len);
2066 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
2067 * replies to 'request', which should be an OpenFlow or Nicira extension
2068 * statistics request. Initially 'replies' will have a single reply message
2069 * that has only a header. The functions ofputil_reserve_stats_reply() and
2070 * ofputil_append_stats_reply() may be used to add to the reply. */
2072 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
2073 struct list *replies)
2077 msg = ofpbuf_new(1024);
2078 put_stats_reply__(request, msg);
2081 list_push_back(replies, &msg->list_node);
2084 /* Prepares to append up to 'len' bytes to the series of statistics replies in
2085 * 'replies', which should have been initialized with
2086 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
2087 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
2088 * do so with e.g. ofpbuf_put_uninit().) */
2090 ofputil_reserve_stats_reply(size_t len, struct list *replies)
2092 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
2093 struct ofp_stats_msg *osm = msg->data;
2095 if (msg->size + len <= UINT16_MAX) {
2096 ofpbuf_prealloc_tailroom(msg, len);
2098 osm->flags |= htons(OFPSF_REPLY_MORE);
2100 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
2101 put_stats_reply__(osm, msg);
2102 list_push_back(replies, &msg->list_node);
2107 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
2108 * returns the first byte. */
2110 ofputil_append_stats_reply(size_t len, struct list *replies)
2112 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
2115 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
2117 ofputil_stats_body(const struct ofp_header *oh)
2119 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2120 return (const struct ofp_stats_msg *) oh + 1;
2123 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
2125 ofputil_stats_body_len(const struct ofp_header *oh)
2127 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2128 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
2131 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
2133 ofputil_nxstats_body(const struct ofp_header *oh)
2135 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2136 return ((const struct nicira_stats_msg *) oh) + 1;
2139 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
2141 ofputil_nxstats_body_len(const struct ofp_header *oh)
2143 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2144 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
2148 make_flow_mod(uint16_t command, const struct cls_rule *rule,
2151 struct ofp_flow_mod *ofm;
2152 size_t size = sizeof *ofm + actions_len;
2153 struct ofpbuf *out = ofpbuf_new(size);
2154 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
2155 ofm->header.version = OFP_VERSION;
2156 ofm->header.type = OFPT_FLOW_MOD;
2157 ofm->header.length = htons(size);
2159 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
2160 ofputil_cls_rule_to_match(rule, &ofm->match);
2161 ofm->command = htons(command);
2166 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
2167 uint16_t idle_timeout, size_t actions_len)
2169 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
2170 struct ofp_flow_mod *ofm = out->data;
2171 ofm->idle_timeout = htons(idle_timeout);
2172 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
2173 ofm->buffer_id = htonl(buffer_id);
2178 make_del_flow(const struct cls_rule *rule)
2180 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
2181 struct ofp_flow_mod *ofm = out->data;
2182 ofm->out_port = htons(OFPP_NONE);
2187 make_add_simple_flow(const struct cls_rule *rule,
2188 uint32_t buffer_id, uint16_t out_port,
2189 uint16_t idle_timeout)
2191 if (out_port != OFPP_NONE) {
2192 struct ofp_action_output *oao;
2193 struct ofpbuf *buffer;
2195 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
2196 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
2199 return make_add_flow(rule, buffer_id, idle_timeout, 0);
2204 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
2205 const struct ofpbuf *payload, int max_send_len)
2207 struct ofp_packet_in *opi;
2211 send_len = MIN(max_send_len, payload->size);
2212 buf = ofpbuf_new(sizeof *opi + send_len);
2213 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
2214 OFPT_PACKET_IN, 0, buf);
2215 opi->buffer_id = htonl(buffer_id);
2216 opi->total_len = htons(payload->size);
2217 opi->in_port = htons(in_port);
2218 opi->reason = reason;
2219 ofpbuf_put(buf, payload->data, send_len);
2220 update_openflow_length(buf);
2225 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
2227 make_echo_request(void)
2229 struct ofp_header *rq;
2230 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
2231 rq = ofpbuf_put_uninit(out, sizeof *rq);
2232 rq->version = OFP_VERSION;
2233 rq->type = OFPT_ECHO_REQUEST;
2234 rq->length = htons(sizeof *rq);
2239 /* Creates and returns an OFPT_ECHO_REPLY message matching the
2240 * OFPT_ECHO_REQUEST message in 'rq'. */
2242 make_echo_reply(const struct ofp_header *rq)
2244 size_t size = ntohs(rq->length);
2245 struct ofpbuf *out = ofpbuf_new(size);
2246 struct ofp_header *reply = ofpbuf_put(out, rq, size);
2247 reply->type = OFPT_ECHO_REPLY;
2252 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
2254 switch (flags & OFPC_FRAG_MASK) {
2255 case OFPC_FRAG_NORMAL: return "normal";
2256 case OFPC_FRAG_DROP: return "drop";
2257 case OFPC_FRAG_REASM: return "reassemble";
2258 case OFPC_FRAG_NX_MATCH: return "nx-match";
2265 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
2267 if (!strcasecmp(s, "normal")) {
2268 *flags = OFPC_FRAG_NORMAL;
2269 } else if (!strcasecmp(s, "drop")) {
2270 *flags = OFPC_FRAG_DROP;
2271 } else if (!strcasecmp(s, "reassemble")) {
2272 *flags = OFPC_FRAG_REASM;
2273 } else if (!strcasecmp(s, "nx-match")) {
2274 *flags = OFPC_FRAG_NX_MATCH;
2281 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
2282 * that the switch will never have more than 'max_ports' ports. Returns 0 if
2283 * 'port' is valid, otherwise an OpenFlow return code. */
2285 ofputil_check_output_port(uint16_t port, int max_ports)
2293 case OFPP_CONTROLLER:
2298 if (port < max_ports) {
2301 return OFPERR_OFPBAC_BAD_OUT_PORT;
2305 #define OFPUTIL_NAMED_PORTS \
2306 OFPUTIL_NAMED_PORT(IN_PORT) \
2307 OFPUTIL_NAMED_PORT(TABLE) \
2308 OFPUTIL_NAMED_PORT(NORMAL) \
2309 OFPUTIL_NAMED_PORT(FLOOD) \
2310 OFPUTIL_NAMED_PORT(ALL) \
2311 OFPUTIL_NAMED_PORT(CONTROLLER) \
2312 OFPUTIL_NAMED_PORT(LOCAL) \
2313 OFPUTIL_NAMED_PORT(NONE)
2315 /* Checks whether 's' is the string representation of an OpenFlow port number,
2316 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
2317 * number in '*port' and returns true. Otherwise, returns false. */
2319 ofputil_port_from_string(const char *name, uint16_t *port)
2325 static const struct pair pairs[] = {
2326 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2328 #undef OFPUTIL_NAMED_PORT
2330 static const int n_pairs = ARRAY_SIZE(pairs);
2333 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2338 for (i = 0; i < n_pairs; i++) {
2339 if (!strcasecmp(name, pairs[i].name)) {
2340 *port = pairs[i].value;
2347 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
2348 * Most ports' string representation is just the port number, but for special
2349 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
2351 ofputil_format_port(uint16_t port, struct ds *s)
2356 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2358 #undef OFPUTIL_NAMED_PORT
2361 ds_put_format(s, "%"PRIu16, port);
2364 ds_put_cstr(s, name);
2368 check_resubmit_table(const struct nx_action_resubmit *nar)
2370 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2371 return OFPERR_OFPBAC_BAD_ARGUMENT;
2377 check_output_reg(const struct nx_action_output_reg *naor,
2378 const struct flow *flow)
2380 struct mf_subfield src;
2383 for (i = 0; i < sizeof naor->zero; i++) {
2384 if (naor->zero[i]) {
2385 return OFPERR_OFPBAC_BAD_ARGUMENT;
2389 nxm_decode(&src, naor->src, naor->ofs_nbits);
2390 return mf_check_src(&src, flow);
2394 validate_actions(const union ofp_action *actions, size_t n_actions,
2395 const struct flow *flow, int max_ports)
2397 const union ofp_action *a;
2400 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2405 code = ofputil_decode_action(a);
2408 VLOG_WARN_RL(&bad_ofmsg_rl,
2409 "action decoding error at offset %td (%s)",
2410 (a - actions) * sizeof *a, ofperr_get_name(error));
2416 switch ((enum ofputil_action_code) code) {
2417 case OFPUTIL_OFPAT_OUTPUT:
2418 error = ofputil_check_output_port(ntohs(a->output.port),
2422 case OFPUTIL_OFPAT_SET_VLAN_VID:
2423 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2424 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2428 case OFPUTIL_OFPAT_SET_VLAN_PCP:
2429 if (a->vlan_pcp.vlan_pcp & ~7) {
2430 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2434 case OFPUTIL_OFPAT_ENQUEUE:
2435 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
2436 if (port >= max_ports && port != OFPP_IN_PORT
2437 && port != OFPP_LOCAL) {
2438 error = OFPERR_OFPBAC_BAD_OUT_PORT;
2442 case OFPUTIL_NXAST_REG_MOVE:
2443 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2447 case OFPUTIL_NXAST_REG_LOAD:
2448 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2452 case OFPUTIL_NXAST_MULTIPATH:
2453 error = multipath_check((const struct nx_action_multipath *) a,
2457 case OFPUTIL_NXAST_AUTOPATH:
2458 error = autopath_check((const struct nx_action_autopath *) a,
2462 case OFPUTIL_NXAST_BUNDLE:
2463 case OFPUTIL_NXAST_BUNDLE_LOAD:
2464 error = bundle_check((const struct nx_action_bundle *) a,
2468 case OFPUTIL_NXAST_OUTPUT_REG:
2469 error = check_output_reg((const struct nx_action_output_reg *) a,
2473 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2474 error = check_resubmit_table(
2475 (const struct nx_action_resubmit *) a);
2478 case OFPUTIL_NXAST_LEARN:
2479 error = learn_check((const struct nx_action_learn *) a, flow);
2482 case OFPUTIL_OFPAT_STRIP_VLAN:
2483 case OFPUTIL_OFPAT_SET_NW_SRC:
2484 case OFPUTIL_OFPAT_SET_NW_DST:
2485 case OFPUTIL_OFPAT_SET_NW_TOS:
2486 case OFPUTIL_OFPAT_SET_TP_SRC:
2487 case OFPUTIL_OFPAT_SET_TP_DST:
2488 case OFPUTIL_OFPAT_SET_DL_SRC:
2489 case OFPUTIL_OFPAT_SET_DL_DST:
2490 case OFPUTIL_NXAST_RESUBMIT:
2491 case OFPUTIL_NXAST_SET_TUNNEL:
2492 case OFPUTIL_NXAST_SET_QUEUE:
2493 case OFPUTIL_NXAST_POP_QUEUE:
2494 case OFPUTIL_NXAST_NOTE:
2495 case OFPUTIL_NXAST_SET_TUNNEL64:
2496 case OFPUTIL_NXAST_EXIT:
2497 case OFPUTIL_NXAST_DEC_TTL:
2502 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
2503 (a - actions) * sizeof *a, ofperr_get_name(error));
2508 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
2509 (n_actions - left) * sizeof *a);
2510 return OFPERR_OFPBAC_BAD_LEN;
2515 struct ofputil_action {
2517 unsigned int min_len;
2518 unsigned int max_len;
2521 static const struct ofputil_action action_bad_type
2522 = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX };
2523 static const struct ofputil_action action_bad_len
2524 = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX };
2525 static const struct ofputil_action action_bad_vendor
2526 = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX };
2528 static const struct ofputil_action *
2529 ofputil_decode_ofpat_action(const union ofp_action *a)
2531 enum ofp_action_type type = ntohs(a->type);
2534 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2536 static const struct ofputil_action action = { \
2538 sizeof(struct STRUCT), \
2539 sizeof(struct STRUCT) \
2543 #include "ofp-util.def"
2547 return &action_bad_type;
2551 static const struct ofputil_action *
2552 ofputil_decode_nxast_action(const union ofp_action *a)
2554 const struct nx_action_header *nah = (const struct nx_action_header *) a;
2555 enum nx_action_subtype subtype = ntohs(nah->subtype);
2558 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2560 static const struct ofputil_action action = { \
2562 sizeof(struct STRUCT), \
2563 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
2567 #include "ofp-util.def"
2569 case NXAST_SNAT__OBSOLETE:
2570 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
2572 return &action_bad_type;
2576 /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or
2577 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error
2580 * The caller must have already verified that 'a''s length is correct (that is,
2581 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
2582 * longer than the amount of space allocated to 'a').
2584 * This function verifies that 'a''s length is correct for the type of action
2585 * that it represents. */
2587 ofputil_decode_action(const union ofp_action *a)
2589 const struct ofputil_action *action;
2590 uint16_t len = ntohs(a->header.len);
2592 if (a->type != htons(OFPAT_VENDOR)) {
2593 action = ofputil_decode_ofpat_action(a);
2595 switch (ntohl(a->vendor.vendor)) {
2597 if (len < sizeof(struct nx_action_header)) {
2598 return -OFPERR_OFPBAC_BAD_LEN;
2600 action = ofputil_decode_nxast_action(a);
2603 action = &action_bad_vendor;
2608 return (len >= action->min_len && len <= action->max_len
2610 : -OFPERR_OFPBAC_BAD_LEN);
2613 /* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_*
2614 * constant. The caller must have already validated that 'a' is a valid action
2615 * understood by Open vSwitch (e.g. by a previous successful call to
2616 * ofputil_decode_action()). */
2617 enum ofputil_action_code
2618 ofputil_decode_action_unsafe(const union ofp_action *a)
2620 const struct ofputil_action *action;
2622 if (a->type != htons(OFPAT_VENDOR)) {
2623 action = ofputil_decode_ofpat_action(a);
2625 action = ofputil_decode_nxast_action(a);
2628 return action->code;
2631 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
2632 * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if
2633 * 'name' is not the name of any action.
2635 * ofp-util.def lists the mapping from names to action. */
2637 ofputil_action_code_from_name(const char *name)
2639 static const char *names[OFPUTIL_N_ACTIONS] = {
2640 #define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME,
2641 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
2642 #include "ofp-util.def"
2647 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
2648 if (*p && !strcasecmp(name, *p)) {
2655 /* Appends an action of the type specified by 'code' to 'buf' and returns the
2656 * action. Initializes the parts of 'action' that identify it as having type
2657 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
2658 * have variable length, the length used and cleared is that of struct
2661 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
2664 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2665 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2666 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2667 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2668 #include "ofp-util.def"
2673 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2675 ofputil_init_##ENUM(struct STRUCT *s) \
2677 memset(s, 0, sizeof *s); \
2678 s->type = htons(ENUM); \
2679 s->len = htons(sizeof *s); \
2683 ofputil_put_##ENUM(struct ofpbuf *buf) \
2685 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2686 ofputil_init_##ENUM(s); \
2689 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2691 ofputil_init_##ENUM(struct STRUCT *s) \
2693 memset(s, 0, sizeof *s); \
2694 s->type = htons(OFPAT_VENDOR); \
2695 s->len = htons(sizeof *s); \
2696 s->vendor = htonl(NX_VENDOR_ID); \
2697 s->subtype = htons(ENUM); \
2701 ofputil_put_##ENUM(struct ofpbuf *buf) \
2703 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2704 ofputil_init_##ENUM(s); \
2707 #include "ofp-util.def"
2709 /* Returns true if 'action' outputs to 'port', false otherwise. */
2711 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2713 switch (ntohs(action->type)) {
2715 return action->output.port == port;
2717 return ((const struct ofp_action_enqueue *) action)->port == port;
2723 /* "Normalizes" the wildcards in 'rule'. That means:
2725 * 1. If the type of level N is known, then only the valid fields for that
2726 * level may be specified. For example, ARP does not have a TOS field,
2727 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2728 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2729 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2732 * 2. If the type of level N is not known (or not understood by Open
2733 * vSwitch), then no fields at all for that level may be specified. For
2734 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2735 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2738 * 'flow_format' specifies the format of the flow as received or as intended to
2739 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2740 * detailed matching. */
2742 ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2745 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2746 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2747 MAY_NW_PROTO = 1 << 2, /* nw_proto */
2748 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
2749 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2750 MAY_ARP_THA = 1 << 5, /* arp_tha */
2751 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
2752 MAY_ND_TARGET = 1 << 7 /* nd_target */
2755 struct flow_wildcards wc;
2757 /* Figure out what fields may be matched. */
2758 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2759 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
2760 if (rule->flow.nw_proto == IPPROTO_TCP ||
2761 rule->flow.nw_proto == IPPROTO_UDP ||
2762 rule->flow.nw_proto == IPPROTO_ICMP) {
2763 may_match |= MAY_TP_ADDR;
2765 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2766 && flow_format == NXFF_NXM) {
2767 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
2768 if (rule->flow.nw_proto == IPPROTO_TCP ||
2769 rule->flow.nw_proto == IPPROTO_UDP) {
2770 may_match |= MAY_TP_ADDR;
2771 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2772 may_match |= MAY_TP_ADDR;
2773 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2774 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2775 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2776 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2779 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2780 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2781 if (flow_format == NXFF_NXM) {
2782 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
2788 /* Clear the fields that may not be matched. */
2790 if (!(may_match & MAY_NW_ADDR)) {
2791 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2793 if (!(may_match & MAY_TP_ADDR)) {
2794 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
2796 if (!(may_match & MAY_NW_PROTO)) {
2797 wc.wildcards |= FWW_NW_PROTO;
2799 if (!(may_match & MAY_IPVx)) {
2800 wc.wildcards |= FWW_NW_DSCP;
2801 wc.wildcards |= FWW_NW_ECN;
2802 wc.wildcards |= FWW_NW_TTL;
2804 if (!(may_match & MAY_ARP_SHA)) {
2805 wc.wildcards |= FWW_ARP_SHA;
2807 if (!(may_match & MAY_ARP_THA)) {
2808 wc.wildcards |= FWW_ARP_THA;
2810 if (!(may_match & MAY_IPV6)) {
2811 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2812 wc.wildcards |= FWW_IPV6_LABEL;
2814 if (!(may_match & MAY_ND_TARGET)) {
2815 wc.wildcards |= FWW_ND_TARGET;
2818 /* Log any changes. */
2819 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2820 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2821 char *pre = log ? cls_rule_to_string(rule) : NULL;
2824 cls_rule_zero_wildcarded_fields(rule);
2827 char *post = cls_rule_to_string(rule);
2828 VLOG_INFO("normalization changed ofp_match, details:");
2829 VLOG_INFO(" pre: %s", pre);
2830 VLOG_INFO("post: %s", post);
2837 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2838 * successful, otherwise an OpenFlow error.
2840 * If successful, the first action is stored in '*actionsp' and the number of
2841 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2842 * are stored, respectively.
2844 * This function does not check that the actions are valid (the caller should
2845 * do so, with validate_actions()). The caller is also responsible for making
2846 * sure that 'b->data' is initially aligned appropriately for "union
2849 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2850 union ofp_action **actionsp, size_t *n_actionsp)
2852 if (actions_len % OFP_ACTION_ALIGN != 0) {
2853 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2854 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2858 *actionsp = ofpbuf_try_pull(b, actions_len);
2859 if (*actionsp == NULL) {
2860 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2861 "exceeds remaining message length (%zu)",
2862 actions_len, b->size);
2866 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2872 return OFPERR_OFPBRC_BAD_LEN;
2876 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
2877 const union ofp_action *b, size_t n_b)
2879 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
2883 ofputil_actions_clone(const union ofp_action *actions, size_t n)
2885 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
2888 /* Parses a key or a key-value pair from '*stringp'.
2890 * On success: Stores the key into '*keyp'. Stores the value, if present, into
2891 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
2892 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
2893 * are substrings of '*stringp' created by replacing some of its bytes by null
2894 * terminators. Returns true.
2896 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
2897 * NULL and returns false. */
2899 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
2901 char *pos, *key, *value;
2905 pos += strspn(pos, ", \t\r\n");
2907 *keyp = *valuep = NULL;
2912 key_len = strcspn(pos, ":=(, \t\r\n");
2913 if (key[key_len] == ':' || key[key_len] == '=') {
2914 /* The value can be separated by a colon. */
2917 value = key + key_len + 1;
2918 value_len = strcspn(value, ", \t\r\n");
2919 pos = value + value_len + (value[value_len] != '\0');
2920 value[value_len] = '\0';
2921 } else if (key[key_len] == '(') {
2922 /* The value can be surrounded by balanced parentheses. The outermost
2923 * set of parentheses is removed. */
2927 value = key + key_len + 1;
2928 for (value_len = 0; level > 0; value_len++) {
2929 switch (value[value_len]) {
2931 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
2942 value[value_len - 1] = '\0';
2943 pos = value + value_len;
2945 /* There might be no value at all. */
2946 value = key + key_len; /* Will become the empty string below. */
2947 pos = key + key_len + (key[key_len] != '\0');
2949 key[key_len] = '\0';