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 },
404 { OFPUTIL_NXT_SET_ASYNC_CONFIG, OFP10_VERSION,
405 NXT_SET_ASYNC_CONFIG, "NXT_SET_ASYNC_CONFIG",
406 sizeof(struct nx_async_config), 0 },
409 static const struct ofputil_msg_category nxt_category = {
410 "Nicira extension message",
411 nxt_messages, ARRAY_SIZE(nxt_messages),
412 OFPERR_OFPBRC_BAD_SUBTYPE
415 const struct ofp_vendor_header *ovh;
416 const struct nicira_header *nh;
418 if (length < sizeof(struct ofp_vendor_header)) {
419 if (length == ntohs(oh->length)) {
420 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
422 return OFPERR_OFPBRC_BAD_LEN;
425 ovh = (const struct ofp_vendor_header *) oh;
426 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
427 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
428 "vendor %"PRIx32, ntohl(ovh->vendor));
429 return OFPERR_OFPBRC_BAD_VENDOR;
432 if (length < sizeof(struct nicira_header)) {
433 if (length == ntohs(oh->length)) {
434 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
435 "length %u (expected at least %zu)",
436 ntohs(ovh->header.length),
437 sizeof(struct nicira_header));
439 return OFPERR_OFPBRC_BAD_LEN;
442 nh = (const struct nicira_header *) oh;
443 return ofputil_lookup_openflow_message(&nxt_category, oh->version,
444 ntohl(nh->subtype), typep);
448 check_nxstats_msg(const struct ofp_header *oh, size_t length)
450 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
453 if (length < sizeof(struct ofp_vendor_stats_msg)) {
454 if (length == ntohs(oh->length)) {
455 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
457 return OFPERR_OFPBRC_BAD_LEN;
460 memcpy(&vendor, osm + 1, sizeof vendor);
461 if (vendor != htonl(NX_VENDOR_ID)) {
462 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
463 "unknown vendor %"PRIx32, ntohl(vendor));
464 return OFPERR_OFPBRC_BAD_VENDOR;
467 if (length < sizeof(struct nicira_stats_msg)) {
468 if (length == ntohs(osm->header.length)) {
469 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
471 return OFPERR_OFPBRC_BAD_LEN;
478 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
479 const struct ofputil_msg_type **typep)
481 static const struct ofputil_msg_type nxst_requests[] = {
482 { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION,
483 NXST_FLOW, "NXST_FLOW request",
484 sizeof(struct nx_flow_stats_request), 8 },
486 { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION,
487 NXST_AGGREGATE, "NXST_AGGREGATE request",
488 sizeof(struct nx_aggregate_stats_request), 8 },
491 static const struct ofputil_msg_category nxst_request_category = {
492 "Nicira extension statistics request",
493 nxst_requests, ARRAY_SIZE(nxst_requests),
494 OFPERR_OFPBRC_BAD_SUBTYPE
497 const struct nicira_stats_msg *nsm;
500 error = check_nxstats_msg(oh, length);
505 nsm = (struct nicira_stats_msg *) oh;
506 return ofputil_lookup_openflow_message(&nxst_request_category, oh->version,
507 ntohl(nsm->subtype), typep);
511 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
512 const struct ofputil_msg_type **typep)
514 static const struct ofputil_msg_type nxst_replies[] = {
515 { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION,
516 NXST_FLOW, "NXST_FLOW reply",
517 sizeof(struct nicira_stats_msg), 8 },
519 { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION,
520 NXST_AGGREGATE, "NXST_AGGREGATE reply",
521 sizeof(struct nx_aggregate_stats_reply), 0 },
524 static const struct ofputil_msg_category nxst_reply_category = {
525 "Nicira extension statistics reply",
526 nxst_replies, ARRAY_SIZE(nxst_replies),
527 OFPERR_OFPBRC_BAD_SUBTYPE
530 const struct nicira_stats_msg *nsm;
533 error = check_nxstats_msg(oh, length);
538 nsm = (struct nicira_stats_msg *) oh;
539 return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version,
540 ntohl(nsm->subtype), typep);
544 check_stats_msg(const struct ofp_header *oh, size_t length)
546 if (length < sizeof(struct ofp_stats_msg)) {
547 if (length == ntohs(oh->length)) {
548 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
550 return OFPERR_OFPBRC_BAD_LEN;
557 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
558 const struct ofputil_msg_type **typep)
560 static const struct ofputil_msg_type ofpst_requests[] = {
561 { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION,
562 OFPST_DESC, "OFPST_DESC request",
563 sizeof(struct ofp_stats_msg), 0 },
565 { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION,
566 OFPST_FLOW, "OFPST_FLOW request",
567 sizeof(struct ofp_flow_stats_request), 0 },
569 { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION,
570 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
571 sizeof(struct ofp_flow_stats_request), 0 },
573 { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION,
574 OFPST_TABLE, "OFPST_TABLE request",
575 sizeof(struct ofp_stats_msg), 0 },
577 { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION,
578 OFPST_PORT, "OFPST_PORT request",
579 sizeof(struct ofp_port_stats_request), 0 },
581 { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION,
582 OFPST_QUEUE, "OFPST_QUEUE request",
583 sizeof(struct ofp_queue_stats_request), 0 },
586 OFPST_VENDOR, "OFPST_VENDOR request",
587 sizeof(struct ofp_vendor_stats_msg), 1 },
590 static const struct ofputil_msg_category ofpst_request_category = {
591 "OpenFlow statistics",
592 ofpst_requests, ARRAY_SIZE(ofpst_requests),
593 OFPERR_OFPBRC_BAD_STAT
596 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
599 error = check_stats_msg(oh, length);
604 error = ofputil_lookup_openflow_message(&ofpst_request_category,
605 oh->version, ntohs(request->type),
607 if (!error && request->type == htons(OFPST_VENDOR)) {
608 error = ofputil_decode_nxst_request(oh, length, typep);
614 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
615 const struct ofputil_msg_type **typep)
617 static const struct ofputil_msg_type ofpst_replies[] = {
618 { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION,
619 OFPST_DESC, "OFPST_DESC reply",
620 sizeof(struct ofp_desc_stats), 0 },
622 { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION,
623 OFPST_FLOW, "OFPST_FLOW reply",
624 sizeof(struct ofp_stats_msg), 1 },
626 { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION,
627 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
628 sizeof(struct ofp_aggregate_stats_reply), 0 },
630 { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION,
631 OFPST_TABLE, "OFPST_TABLE reply",
632 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
634 { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION,
635 OFPST_PORT, "OFPST_PORT reply",
636 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
638 { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION,
639 OFPST_QUEUE, "OFPST_QUEUE reply",
640 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
643 OFPST_VENDOR, "OFPST_VENDOR reply",
644 sizeof(struct ofp_vendor_stats_msg), 1 },
647 static const struct ofputil_msg_category ofpst_reply_category = {
648 "OpenFlow statistics",
649 ofpst_replies, ARRAY_SIZE(ofpst_replies),
650 OFPERR_OFPBRC_BAD_STAT
653 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
656 error = check_stats_msg(oh, length);
661 error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version,
662 ntohs(reply->type), typep);
663 if (!error && reply->type == htons(OFPST_VENDOR)) {
664 error = ofputil_decode_nxst_reply(oh, length, typep);
670 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
671 const struct ofputil_msg_type **typep)
673 static const struct ofputil_msg_type ofpt_messages[] = {
674 { OFPUTIL_OFPT_HELLO, OFP10_VERSION,
675 OFPT_HELLO, "OFPT_HELLO",
676 sizeof(struct ofp_hello), 1 },
678 { OFPUTIL_OFPT_ERROR, 0,
679 OFPT_ERROR, "OFPT_ERROR",
680 sizeof(struct ofp_error_msg), 1 },
682 { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION,
683 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
684 sizeof(struct ofp_header), 1 },
686 { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION,
687 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
688 sizeof(struct ofp_header), 1 },
690 { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION,
691 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
692 sizeof(struct ofp_header), 0 },
694 { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION,
695 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
696 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
698 { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION,
699 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
700 sizeof(struct ofp_header), 0 },
702 { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION,
703 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
704 sizeof(struct ofp_switch_config), 0 },
706 { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION,
707 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
708 sizeof(struct ofp_switch_config), 0 },
710 { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION,
711 OFPT_PACKET_IN, "OFPT_PACKET_IN",
712 offsetof(struct ofp_packet_in, data), 1 },
714 { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION,
715 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
716 sizeof(struct ofp_flow_removed), 0 },
718 { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION,
719 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
720 sizeof(struct ofp_port_status), 0 },
722 { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION,
723 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
724 sizeof(struct ofp_packet_out), 1 },
726 { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION,
727 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
728 sizeof(struct ofp_flow_mod), 1 },
730 { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION,
731 OFPT_PORT_MOD, "OFPT_PORT_MOD",
732 sizeof(struct ofp_port_mod), 0 },
735 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
736 sizeof(struct ofp_stats_msg), 1 },
739 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
740 sizeof(struct ofp_stats_msg), 1 },
742 { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION,
743 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
744 sizeof(struct ofp_header), 0 },
746 { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION,
747 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
748 sizeof(struct ofp_header), 0 },
751 OFPT_VENDOR, "OFPT_VENDOR",
752 sizeof(struct ofp_vendor_header), 1 },
755 static const struct ofputil_msg_category ofpt_category = {
757 ofpt_messages, ARRAY_SIZE(ofpt_messages),
758 OFPERR_OFPBRC_BAD_TYPE
763 error = ofputil_lookup_openflow_message(&ofpt_category, oh->version,
768 error = ofputil_decode_vendor(oh, length, typep);
771 case OFPT_STATS_REQUEST:
772 error = ofputil_decode_ofpst_request(oh, length, typep);
775 case OFPT_STATS_REPLY:
776 error = ofputil_decode_ofpst_reply(oh, length, typep);
785 /* Decodes the message type represented by 'oh'. Returns 0 if successful or an
786 * OpenFlow error code on failure. Either way, stores in '*typep' a type
787 * structure that can be inspected with the ofputil_msg_type_*() functions.
789 * oh->length must indicate the correct length of the message (and must be at
790 * least sizeof(struct ofp_header)).
792 * Success indicates that 'oh' is at least as long as the minimum-length
793 * message of its type. */
795 ofputil_decode_msg_type(const struct ofp_header *oh,
796 const struct ofputil_msg_type **typep)
798 size_t length = ntohs(oh->length);
801 error = ofputil_decode_msg_type__(oh, length, typep);
803 error = ofputil_check_length(*typep, length);
806 *typep = &ofputil_invalid_type;
811 /* Decodes the message type represented by 'oh', of which only the first
812 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
813 * code on failure. Either way, stores in '*typep' a type structure that can
814 * be inspected with the ofputil_msg_type_*() functions. */
816 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
817 const struct ofputil_msg_type **typep)
821 error = (length >= sizeof *oh
822 ? ofputil_decode_msg_type__(oh, length, typep)
823 : OFPERR_OFPBRC_BAD_LEN);
825 *typep = &ofputil_invalid_type;
830 /* Returns an OFPUTIL_* message type code for 'type'. */
831 enum ofputil_msg_code
832 ofputil_msg_type_code(const struct ofputil_msg_type *type)
840 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
842 switch (flow_format) {
843 case NXFF_OPENFLOW10:
852 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
854 switch (flow_format) {
855 case NXFF_OPENFLOW10:
865 ofputil_flow_format_from_string(const char *s)
867 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
868 : !strcmp(s, "nxm") ? NXFF_NXM
873 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
875 switch (packet_in_format) {
876 case NXPIF_OPENFLOW10:
885 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
887 switch (packet_in_format) {
888 case NXPIF_OPENFLOW10:
898 ofputil_packet_in_format_from_string(const char *s)
900 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
901 : !strcmp(s, "nxm") ? NXPIF_NXM
906 regs_fully_wildcarded(const struct flow_wildcards *wc)
910 for (i = 0; i < FLOW_N_REGS; i++) {
911 if (wc->reg_masks[i] != 0) {
918 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
919 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
920 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
921 * NXFF_OPENFLOW10 for backward compatibility. */
923 ofputil_min_flow_format(const struct cls_rule *rule)
925 const struct flow_wildcards *wc = &rule->wc;
927 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
929 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
930 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
934 /* Only NXM supports matching ARP hardware addresses. */
935 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
939 /* Only NXM supports matching IPv6 traffic. */
940 if (!(wc->wildcards & FWW_DL_TYPE)
941 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
945 /* Only NXM supports matching registers. */
946 if (!regs_fully_wildcarded(wc)) {
950 /* Only NXM supports matching tun_id. */
951 if (wc->tun_id_mask != htonll(0)) {
955 /* Only NXM supports matching fragments. */
956 if (wc->nw_frag_mask) {
960 /* Only NXM supports matching IPv6 flow label. */
961 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
965 /* Only NXM supports matching IP ECN bits. */
966 if (!(wc->wildcards & FWW_NW_ECN)) {
970 /* Only NXM supports matching IP TTL/hop limit. */
971 if (!(wc->wildcards & FWW_NW_TTL)) {
975 /* Only NXM supports bitwise matching on transport port. */
976 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
977 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
981 /* Other formats can express this rule. */
982 return NXFF_OPENFLOW10;
985 /* Returns an OpenFlow message that can be used to set the flow format to
988 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
990 struct nx_set_flow_format *sff;
993 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
994 sff->format = htonl(flow_format);
1000 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1002 struct nx_set_packet_in_format *spif;
1005 spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg);
1006 spif->format = htonl(packet_in_format);
1011 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1012 * extension on or off (according to 'flow_mod_table_id'). */
1014 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1016 struct nx_flow_mod_table_id *nfmti;
1019 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
1020 nfmti->set = flow_mod_table_id;
1024 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1025 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1028 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1029 * enabled, false otherwise.
1031 * Does not validate the flow_mod actions. */
1033 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1034 const struct ofp_header *oh, bool flow_mod_table_id)
1036 const struct ofputil_msg_type *type;
1040 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1042 ofputil_decode_msg_type(oh, &type);
1043 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
1044 /* Standard OpenFlow flow_mod. */
1045 const struct ofp_flow_mod *ofm;
1049 /* Dissect the message. */
1050 ofm = ofpbuf_pull(&b, sizeof *ofm);
1051 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1056 /* Set priority based on original wildcards. Normally we'd allow
1057 * ofputil_cls_rule_from_match() to do this for us, but
1058 * ofputil_normalize_rule() can put wildcards where the original flow
1059 * didn't have them. */
1060 priority = ntohs(ofm->priority);
1061 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
1062 priority = UINT16_MAX;
1065 /* Translate the rule. */
1066 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
1067 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
1069 /* Translate the message. */
1070 fm->cookie = ofm->cookie;
1071 fm->cookie_mask = htonll(UINT64_MAX);
1072 command = ntohs(ofm->command);
1073 fm->idle_timeout = ntohs(ofm->idle_timeout);
1074 fm->hard_timeout = ntohs(ofm->hard_timeout);
1075 fm->buffer_id = ntohl(ofm->buffer_id);
1076 fm->out_port = ntohs(ofm->out_port);
1077 fm->flags = ntohs(ofm->flags);
1078 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1079 /* Nicira extended flow_mod. */
1080 const struct nx_flow_mod *nfm;
1083 /* Dissect the message. */
1084 nfm = ofpbuf_pull(&b, sizeof *nfm);
1085 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1086 &fm->cr, &fm->cookie, &fm->cookie_mask);
1090 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1095 /* Translate the message. */
1096 command = ntohs(nfm->command);
1097 if (command == OFPFC_ADD) {
1098 if (fm->cookie_mask) {
1099 /* The "NXM_NX_COOKIE*" matches are not valid for flow
1100 * additions. Additions must use the "cookie" field of
1101 * the "nx_flow_mod" structure. */
1102 return OFPERR_NXBRC_NXM_INVALID;
1104 fm->cookie = nfm->cookie;
1105 fm->cookie_mask = htonll(UINT64_MAX);
1108 fm->idle_timeout = ntohs(nfm->idle_timeout);
1109 fm->hard_timeout = ntohs(nfm->hard_timeout);
1110 fm->buffer_id = ntohl(nfm->buffer_id);
1111 fm->out_port = ntohs(nfm->out_port);
1112 fm->flags = ntohs(nfm->flags);
1117 if (flow_mod_table_id) {
1118 fm->command = command & 0xff;
1119 fm->table_id = command >> 8;
1121 fm->command = command;
1122 fm->table_id = 0xff;
1128 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1129 * 'flow_format' and returns the message.
1131 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1132 * enabled, false otherwise. */
1134 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1135 enum nx_flow_format flow_format,
1136 bool flow_mod_table_id)
1138 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1142 command = (flow_mod_table_id
1143 ? (fm->command & 0xff) | (fm->table_id << 8)
1146 if (flow_format == NXFF_OPENFLOW10) {
1147 struct ofp_flow_mod *ofm;
1149 msg = ofpbuf_new(sizeof *ofm + actions_len);
1150 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1151 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1152 ofm->cookie = fm->cookie;
1153 ofm->command = htons(command);
1154 ofm->idle_timeout = htons(fm->idle_timeout);
1155 ofm->hard_timeout = htons(fm->hard_timeout);
1156 ofm->priority = htons(fm->cr.priority);
1157 ofm->buffer_id = htonl(fm->buffer_id);
1158 ofm->out_port = htons(fm->out_port);
1159 ofm->flags = htons(fm->flags);
1160 } else if (flow_format == NXFF_NXM) {
1161 struct nx_flow_mod *nfm;
1164 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1165 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1167 nfm->command = htons(command);
1168 if (command == OFPFC_ADD) {
1169 nfm->cookie = fm->cookie;
1170 match_len = nx_put_match(msg, &fm->cr, 0, 0);
1173 match_len = nx_put_match(msg, &fm->cr,
1174 fm->cookie, fm->cookie_mask);
1176 nfm->idle_timeout = htons(fm->idle_timeout);
1177 nfm->hard_timeout = htons(fm->hard_timeout);
1178 nfm->priority = htons(fm->cr.priority);
1179 nfm->buffer_id = htonl(fm->buffer_id);
1180 nfm->out_port = htons(fm->out_port);
1181 nfm->flags = htons(fm->flags);
1182 nfm->match_len = htons(match_len);
1187 ofpbuf_put(msg, fm->actions, actions_len);
1188 update_openflow_length(msg);
1193 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1194 const struct ofp_header *oh,
1197 const struct ofp_flow_stats_request *ofsr =
1198 (const struct ofp_flow_stats_request *) oh;
1200 fsr->aggregate = aggregate;
1201 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1202 fsr->out_port = ntohs(ofsr->out_port);
1203 fsr->table_id = ofsr->table_id;
1204 fsr->cookie = fsr->cookie_mask = htonll(0);
1210 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1211 const struct ofp_header *oh,
1214 const struct nx_flow_stats_request *nfsr;
1218 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1220 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1221 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match,
1222 &fsr->cookie, &fsr->cookie_mask);
1227 return OFPERR_OFPBRC_BAD_LEN;
1230 fsr->aggregate = aggregate;
1231 fsr->out_port = ntohs(nfsr->out_port);
1232 fsr->table_id = nfsr->table_id;
1237 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1238 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1239 * successful, otherwise an OpenFlow error code. */
1241 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1242 const struct ofp_header *oh)
1244 const struct ofputil_msg_type *type;
1248 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1250 ofputil_decode_msg_type(oh, &type);
1251 code = ofputil_msg_type_code(type);
1253 case OFPUTIL_OFPST_FLOW_REQUEST:
1254 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1256 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1257 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1259 case OFPUTIL_NXST_FLOW_REQUEST:
1260 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1262 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1263 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1266 /* Hey, the caller lied. */
1271 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1272 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1273 * 'flow_format', and returns the message. */
1275 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1276 enum nx_flow_format flow_format)
1280 if (flow_format == NXFF_OPENFLOW10) {
1281 struct ofp_flow_stats_request *ofsr;
1284 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1285 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1286 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1287 ofsr->table_id = fsr->table_id;
1288 ofsr->out_port = htons(fsr->out_port);
1289 } else if (flow_format == NXFF_NXM) {
1290 struct nx_flow_stats_request *nfsr;
1294 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1295 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1296 match_len = nx_put_match(msg, &fsr->match,
1297 fsr->cookie, fsr->cookie_mask);
1300 nfsr->out_port = htons(fsr->out_port);
1301 nfsr->match_len = htons(match_len);
1302 nfsr->table_id = fsr->table_id;
1310 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1311 * ofputil_flow_stats in 'fs'.
1313 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1314 * OpenFlow message. Calling this function multiple times for a single 'msg'
1315 * iterates through the replies. The caller must initially leave 'msg''s layer
1316 * pointers null and not modify them between calls.
1318 * Most switches don't send the values needed to populate fs->idle_age and
1319 * fs->hard_age, so those members will usually be set to 0. If the switch from
1320 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1321 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1322 * 'idle_age' and 'hard_age' members in 'fs'.
1324 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1325 * otherwise a positive errno value. */
1327 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1329 bool flow_age_extension)
1331 const struct ofputil_msg_type *type;
1334 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1335 code = ofputil_msg_type_code(type);
1337 msg->l2 = msg->data;
1338 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1339 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1340 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1341 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1349 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1350 const struct ofp_flow_stats *ofs;
1353 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1355 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1356 "bytes at end", msg->size);
1360 length = ntohs(ofs->length);
1361 if (length < sizeof *ofs) {
1362 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1363 "length %zu", length);
1367 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1368 &fs->actions, &fs->n_actions)) {
1372 fs->cookie = get_32aligned_be64(&ofs->cookie);
1373 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1375 fs->table_id = ofs->table_id;
1376 fs->duration_sec = ntohl(ofs->duration_sec);
1377 fs->duration_nsec = ntohl(ofs->duration_nsec);
1378 fs->idle_timeout = ntohs(ofs->idle_timeout);
1379 fs->hard_timeout = ntohs(ofs->hard_timeout);
1382 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1383 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1384 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1385 const struct nx_flow_stats *nfs;
1386 size_t match_len, length;
1388 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1390 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1391 "bytes at end", msg->size);
1395 length = ntohs(nfs->length);
1396 match_len = ntohs(nfs->match_len);
1397 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1398 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1399 "claims invalid length %zu", match_len, length);
1402 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1407 if (ofputil_pull_actions(msg,
1408 length - sizeof *nfs - ROUND_UP(match_len, 8),
1409 &fs->actions, &fs->n_actions)) {
1413 fs->cookie = nfs->cookie;
1414 fs->table_id = nfs->table_id;
1415 fs->duration_sec = ntohl(nfs->duration_sec);
1416 fs->duration_nsec = ntohl(nfs->duration_nsec);
1417 fs->idle_timeout = ntohs(nfs->idle_timeout);
1418 fs->hard_timeout = ntohs(nfs->hard_timeout);
1421 if (flow_age_extension) {
1422 if (nfs->idle_age) {
1423 fs->idle_age = ntohs(nfs->idle_age) - 1;
1425 if (nfs->hard_age) {
1426 fs->hard_age = ntohs(nfs->hard_age) - 1;
1429 fs->packet_count = ntohll(nfs->packet_count);
1430 fs->byte_count = ntohll(nfs->byte_count);
1438 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1440 * We use this in situations where OVS internally uses UINT64_MAX to mean
1441 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1443 unknown_to_zero(uint64_t count)
1445 return count != UINT64_MAX ? count : 0;
1448 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1449 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1450 * have been initialized with ofputil_start_stats_reply(). */
1452 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1453 struct list *replies)
1455 size_t act_len = fs->n_actions * sizeof *fs->actions;
1456 const struct ofp_stats_msg *osm;
1458 osm = ofpbuf_from_list(list_back(replies))->data;
1459 if (osm->type == htons(OFPST_FLOW)) {
1460 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1461 struct ofp_flow_stats *ofs;
1463 ofs = ofputil_append_stats_reply(len, replies);
1464 ofs->length = htons(len);
1465 ofs->table_id = fs->table_id;
1467 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1468 ofs->duration_sec = htonl(fs->duration_sec);
1469 ofs->duration_nsec = htonl(fs->duration_nsec);
1470 ofs->priority = htons(fs->rule.priority);
1471 ofs->idle_timeout = htons(fs->idle_timeout);
1472 ofs->hard_timeout = htons(fs->hard_timeout);
1473 memset(ofs->pad2, 0, sizeof ofs->pad2);
1474 put_32aligned_be64(&ofs->cookie, fs->cookie);
1475 put_32aligned_be64(&ofs->packet_count,
1476 htonll(unknown_to_zero(fs->packet_count)));
1477 put_32aligned_be64(&ofs->byte_count,
1478 htonll(unknown_to_zero(fs->byte_count)));
1479 memcpy(ofs->actions, fs->actions, act_len);
1480 } else if (osm->type == htons(OFPST_VENDOR)) {
1481 struct nx_flow_stats *nfs;
1485 msg = ofputil_reserve_stats_reply(
1486 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1487 start_len = msg->size;
1489 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1490 nfs->table_id = fs->table_id;
1492 nfs->duration_sec = htonl(fs->duration_sec);
1493 nfs->duration_nsec = htonl(fs->duration_nsec);
1494 nfs->priority = htons(fs->rule.priority);
1495 nfs->idle_timeout = htons(fs->idle_timeout);
1496 nfs->hard_timeout = htons(fs->hard_timeout);
1497 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1498 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1500 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1501 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1503 nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0));
1504 nfs->cookie = fs->cookie;
1505 nfs->packet_count = htonll(fs->packet_count);
1506 nfs->byte_count = htonll(fs->byte_count);
1507 ofpbuf_put(msg, fs->actions, act_len);
1508 nfs->length = htons(msg->size - start_len);
1514 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1515 * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */
1517 ofputil_encode_aggregate_stats_reply(
1518 const struct ofputil_aggregate_stats *stats,
1519 const struct ofp_stats_msg *request)
1523 if (request->type == htons(OFPST_AGGREGATE)) {
1524 struct ofp_aggregate_stats_reply *asr;
1526 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
1527 put_32aligned_be64(&asr->packet_count,
1528 htonll(unknown_to_zero(stats->packet_count)));
1529 put_32aligned_be64(&asr->byte_count,
1530 htonll(unknown_to_zero(stats->byte_count)));
1531 asr->flow_count = htonl(stats->flow_count);
1532 } else if (request->type == htons(OFPST_VENDOR)) {
1533 struct nx_aggregate_stats_reply *nasr;
1535 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1536 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1537 nasr->packet_count = htonll(stats->packet_count);
1538 nasr->byte_count = htonll(stats->byte_count);
1539 nasr->flow_count = htonl(stats->flow_count);
1547 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1548 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1549 * an OpenFlow error code. */
1551 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1552 const struct ofp_header *oh)
1554 const struct ofputil_msg_type *type;
1555 enum ofputil_msg_code code;
1557 ofputil_decode_msg_type(oh, &type);
1558 code = ofputil_msg_type_code(type);
1559 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1560 const struct ofp_flow_removed *ofr;
1562 ofr = (const struct ofp_flow_removed *) oh;
1563 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1565 fr->cookie = ofr->cookie;
1566 fr->reason = ofr->reason;
1567 fr->duration_sec = ntohl(ofr->duration_sec);
1568 fr->duration_nsec = ntohl(ofr->duration_nsec);
1569 fr->idle_timeout = ntohs(ofr->idle_timeout);
1570 fr->packet_count = ntohll(ofr->packet_count);
1571 fr->byte_count = ntohll(ofr->byte_count);
1572 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1573 struct nx_flow_removed *nfr;
1577 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1579 nfr = ofpbuf_pull(&b, sizeof *nfr);
1580 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1581 &fr->rule, NULL, NULL);
1586 return OFPERR_OFPBRC_BAD_LEN;
1589 fr->cookie = nfr->cookie;
1590 fr->reason = nfr->reason;
1591 fr->duration_sec = ntohl(nfr->duration_sec);
1592 fr->duration_nsec = ntohl(nfr->duration_nsec);
1593 fr->idle_timeout = ntohs(nfr->idle_timeout);
1594 fr->packet_count = ntohll(nfr->packet_count);
1595 fr->byte_count = ntohll(nfr->byte_count);
1603 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1604 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1607 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1608 enum nx_flow_format flow_format)
1612 if (flow_format == NXFF_OPENFLOW10) {
1613 struct ofp_flow_removed *ofr;
1615 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1617 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1618 ofr->cookie = fr->cookie;
1619 ofr->priority = htons(fr->rule.priority);
1620 ofr->reason = fr->reason;
1621 ofr->duration_sec = htonl(fr->duration_sec);
1622 ofr->duration_nsec = htonl(fr->duration_nsec);
1623 ofr->idle_timeout = htons(fr->idle_timeout);
1624 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1625 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1626 } else if (flow_format == NXFF_NXM) {
1627 struct nx_flow_removed *nfr;
1630 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1631 match_len = nx_put_match(msg, &fr->rule, 0, 0);
1634 nfr->cookie = fr->cookie;
1635 nfr->priority = htons(fr->rule.priority);
1636 nfr->reason = fr->reason;
1637 nfr->duration_sec = htonl(fr->duration_sec);
1638 nfr->duration_nsec = htonl(fr->duration_nsec);
1639 nfr->idle_timeout = htons(fr->idle_timeout);
1640 nfr->match_len = htons(match_len);
1641 nfr->packet_count = htonll(fr->packet_count);
1642 nfr->byte_count = htonll(fr->byte_count);
1651 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
1652 const struct ofp_header *oh)
1654 const struct ofputil_msg_type *type;
1655 enum ofputil_msg_code code;
1657 ofputil_decode_msg_type(oh, &type);
1658 code = ofputil_msg_type_code(type);
1659 memset(pin, 0, sizeof *pin);
1661 if (code == OFPUTIL_OFPT_PACKET_IN) {
1662 const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh;
1664 pin->packet = opi->data;
1665 pin->packet_len = ntohs(opi->header.length)
1666 - offsetof(struct ofp_packet_in, data);
1668 pin->fmd.in_port = ntohs(opi->in_port);
1669 pin->reason = opi->reason;
1670 pin->buffer_id = ntohl(opi->buffer_id);
1671 pin->total_len = ntohs(opi->total_len);
1672 } else if (code == OFPUTIL_NXT_PACKET_IN) {
1673 const struct nx_packet_in *npi;
1674 struct cls_rule rule;
1678 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1680 npi = ofpbuf_pull(&b, sizeof *npi);
1681 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
1687 if (!ofpbuf_try_pull(&b, 2)) {
1688 return OFPERR_OFPBRC_BAD_LEN;
1691 pin->packet = b.data;
1692 pin->packet_len = b.size;
1693 pin->reason = npi->reason;
1694 pin->table_id = npi->table_id;
1695 pin->cookie = npi->cookie;
1697 pin->fmd.in_port = rule.flow.in_port;
1699 pin->fmd.tun_id = rule.flow.tun_id;
1700 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
1702 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
1703 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
1704 sizeof pin->fmd.reg_masks);
1706 pin->buffer_id = ntohl(npi->buffer_id);
1707 pin->total_len = ntohs(npi->total_len);
1715 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
1716 * in the format specified by 'packet_in_format'. */
1718 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1719 enum nx_packet_in_format packet_in_format)
1721 size_t send_len = MIN(pin->send_len, pin->packet_len);
1722 struct ofpbuf *packet;
1724 /* Add OFPT_PACKET_IN. */
1725 if (packet_in_format == NXPIF_OPENFLOW10) {
1726 size_t header_len = offsetof(struct ofp_packet_in, data);
1727 struct ofp_packet_in *opi;
1729 packet = ofpbuf_new(send_len + header_len);
1730 opi = ofpbuf_put_zeros(packet, header_len);
1731 opi->header.version = OFP_VERSION;
1732 opi->header.type = OFPT_PACKET_IN;
1733 opi->total_len = htons(pin->total_len);
1734 opi->in_port = htons(pin->fmd.in_port);
1735 opi->reason = pin->reason;
1736 opi->buffer_id = htonl(pin->buffer_id);
1738 ofpbuf_put(packet, pin->packet, send_len);
1739 } else if (packet_in_format == NXPIF_NXM) {
1740 struct nx_packet_in *npi;
1741 struct cls_rule rule;
1745 /* Estimate of required PACKET_IN length includes the NPI header, space
1746 * for the match (2 times sizeof the metadata seems like enough), 2
1747 * bytes for padding, and the packet length. */
1748 packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2
1751 cls_rule_init_catchall(&rule, 0);
1752 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
1753 pin->fmd.tun_id_mask);
1755 for (i = 0; i < FLOW_N_REGS; i++) {
1756 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
1757 pin->fmd.reg_masks[i]);
1760 cls_rule_set_in_port(&rule, pin->fmd.in_port);
1762 ofpbuf_put_zeros(packet, sizeof *npi);
1763 match_len = nx_put_match(packet, &rule, 0, 0);
1764 ofpbuf_put_zeros(packet, 2);
1765 ofpbuf_put(packet, pin->packet, send_len);
1768 npi->nxh.header.version = OFP_VERSION;
1769 npi->nxh.header.type = OFPT_VENDOR;
1770 npi->nxh.vendor = htonl(NX_VENDOR_ID);
1771 npi->nxh.subtype = htonl(NXT_PACKET_IN);
1773 npi->buffer_id = htonl(pin->buffer_id);
1774 npi->total_len = htons(pin->total_len);
1775 npi->reason = pin->reason;
1776 npi->table_id = pin->table_id;
1777 npi->cookie = pin->cookie;
1778 npi->match_len = htons(match_len);
1782 update_openflow_length(packet);
1788 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
1790 static char s[INT_STRLEN(int) + 1];
1797 case OFPR_INVALID_TTL:
1798 return "invalid_ttl";
1800 case OFPR_N_REASONS:
1802 sprintf(s, "%d", (int) reason);
1808 ofputil_packet_in_reason_from_string(const char *s,
1809 enum ofp_packet_in_reason *reason)
1813 for (i = 0; i < OFPR_N_REASONS; i++) {
1814 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
1823 ofputil_decode_packet_out(struct ofputil_packet_out *po,
1824 const struct ofp_packet_out *opo)
1829 po->buffer_id = ntohl(opo->buffer_id);
1830 po->in_port = ntohs(opo->in_port);
1831 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
1832 && po->in_port != OFPP_NONE) {
1833 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
1835 return OFPERR_NXBRC_BAD_IN_PORT;
1838 ofpbuf_use_const(&b, opo, ntohs(opo->header.length));
1839 ofpbuf_pull(&b, sizeof *opo);
1841 error = ofputil_pull_actions(&b, ntohs(opo->actions_len),
1842 &po->actions, &po->n_actions);
1847 if (po->buffer_id == UINT32_MAX) {
1848 po->packet = b.data;
1849 po->packet_len = b.size;
1859 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
1861 struct ofp_packet_out *opo;
1866 actions_len = po->n_actions * sizeof *po->actions;
1867 size = sizeof *opo + actions_len;
1868 if (po->buffer_id == UINT32_MAX) {
1869 size += po->packet_len;
1872 msg = ofpbuf_new(size);
1873 opo = put_openflow(sizeof *opo, OFPT_PACKET_OUT, msg);
1874 opo->buffer_id = htonl(po->buffer_id);
1875 opo->in_port = htons(po->in_port);
1876 opo->actions_len = htons(actions_len);
1877 ofpbuf_put(msg, po->actions, actions_len);
1878 if (po->buffer_id == UINT32_MAX) {
1879 ofpbuf_put(msg, po->packet, po->packet_len);
1881 update_openflow_length(msg);
1886 /* Returns a string representing the message type of 'type'. The string is the
1887 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1888 * messages, the constant is followed by "request" or "reply",
1889 * e.g. "OFPST_AGGREGATE reply". */
1891 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1896 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1897 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1898 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1901 * The caller is responsible for freeing '*bufferp' when it is no longer
1904 * The OpenFlow header length is initially set to 'openflow_len'; if the
1905 * message is later extended, the length should be updated with
1906 * update_openflow_length() before sending.
1908 * Returns the header. */
1910 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1912 *bufferp = ofpbuf_new(openflow_len);
1913 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1916 /* Similar to make_openflow() but creates a Nicira vendor extension message
1917 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1919 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1921 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1924 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1925 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1926 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1929 * The caller is responsible for freeing '*bufferp' when it is no longer
1932 * The OpenFlow header length is initially set to 'openflow_len'; if the
1933 * message is later extended, the length should be updated with
1934 * update_openflow_length() before sending.
1936 * Returns the header. */
1938 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1939 struct ofpbuf **bufferp)
1941 *bufferp = ofpbuf_new(openflow_len);
1942 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1945 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1946 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1948 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1949 struct ofpbuf **bufferp)
1951 *bufferp = ofpbuf_new(openflow_len);
1952 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1955 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1956 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1957 * beyond the header, if any, are zeroed.
1959 * The OpenFlow header length is initially set to 'openflow_len'; if the
1960 * message is later extended, the length should be updated with
1961 * update_openflow_length() before sending.
1963 * Returns the header. */
1965 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1967 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1970 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1971 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1972 * the header, if any, are zeroed.
1974 * The OpenFlow header length is initially set to 'openflow_len'; if the
1975 * message is later extended, the length should be updated with
1976 * update_openflow_length() before sending.
1978 * Returns the header. */
1980 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1981 struct ofpbuf *buffer)
1983 struct ofp_header *oh;
1985 assert(openflow_len >= sizeof *oh);
1986 assert(openflow_len <= UINT16_MAX);
1988 oh = ofpbuf_put_uninit(buffer, openflow_len);
1989 oh->version = OFP_VERSION;
1991 oh->length = htons(openflow_len);
1993 memset(oh + 1, 0, openflow_len - sizeof *oh);
1997 /* Similar to put_openflow() but append a Nicira vendor extension message with
1998 * the specific 'subtype'. 'subtype' should be in host byte order. */
2000 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
2002 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
2005 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
2006 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2008 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2009 struct ofpbuf *buffer)
2011 struct nicira_header *nxh;
2013 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
2014 nxh->vendor = htonl(NX_VENDOR_ID);
2015 nxh->subtype = htonl(subtype);
2019 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
2020 * 'buffer->size'. */
2022 update_openflow_length(struct ofpbuf *buffer)
2024 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
2025 oh->length = htons(buffer->size);
2029 put_stats__(ovs_be32 xid, uint8_t ofp_type,
2030 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
2033 if (ofpst_type == htons(OFPST_VENDOR)) {
2034 struct nicira_stats_msg *nsm;
2036 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
2037 nsm->vsm.osm.type = ofpst_type;
2038 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
2039 nsm->subtype = nxst_subtype;
2041 struct ofp_stats_msg *osm;
2043 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
2044 osm->type = ofpst_type;
2048 /* Creates a statistics request message with total length 'openflow_len'
2049 * (including all headers) and the given 'ofpst_type', and stores the buffer
2050 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
2051 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
2052 * subtype (otherwise 'nxst_subtype' is ignored).
2054 * Initializes bytes following the headers to all-bits-zero.
2056 * Returns the first byte of the new message. */
2058 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
2059 uint32_t nxst_subtype, struct ofpbuf **bufferp)
2063 msg = *bufferp = ofpbuf_new(openflow_len);
2064 put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
2065 htons(ofpst_type), htonl(nxst_subtype), msg);
2066 ofpbuf_padto(msg, openflow_len);
2072 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
2074 assert(request->header.type == OFPT_STATS_REQUEST ||
2075 request->header.type == OFPT_STATS_REPLY);
2076 put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
2077 (request->type != htons(OFPST_VENDOR)
2079 : ((const struct nicira_stats_msg *) request)->subtype),
2083 /* Creates a statistics reply message with total length 'openflow_len'
2084 * (including all headers) and the same type (either a standard OpenFlow
2085 * statistics type or a Nicira extension type and subtype) as 'request', and
2086 * stores the buffer containing the new message in '*bufferp'.
2088 * Initializes bytes following the headers to all-bits-zero.
2090 * Returns the first byte of the new message. */
2092 ofputil_make_stats_reply(size_t openflow_len,
2093 const struct ofp_stats_msg *request,
2094 struct ofpbuf **bufferp)
2098 msg = *bufferp = ofpbuf_new(openflow_len);
2099 put_stats_reply__(request, msg);
2100 ofpbuf_padto(msg, openflow_len);
2105 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
2106 * replies to 'request', which should be an OpenFlow or Nicira extension
2107 * statistics request. Initially 'replies' will have a single reply message
2108 * that has only a header. The functions ofputil_reserve_stats_reply() and
2109 * ofputil_append_stats_reply() may be used to add to the reply. */
2111 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
2112 struct list *replies)
2116 msg = ofpbuf_new(1024);
2117 put_stats_reply__(request, msg);
2120 list_push_back(replies, &msg->list_node);
2123 /* Prepares to append up to 'len' bytes to the series of statistics replies in
2124 * 'replies', which should have been initialized with
2125 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
2126 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
2127 * do so with e.g. ofpbuf_put_uninit().) */
2129 ofputil_reserve_stats_reply(size_t len, struct list *replies)
2131 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
2132 struct ofp_stats_msg *osm = msg->data;
2134 if (msg->size + len <= UINT16_MAX) {
2135 ofpbuf_prealloc_tailroom(msg, len);
2137 osm->flags |= htons(OFPSF_REPLY_MORE);
2139 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
2140 put_stats_reply__(osm, msg);
2141 list_push_back(replies, &msg->list_node);
2146 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
2147 * returns the first byte. */
2149 ofputil_append_stats_reply(size_t len, struct list *replies)
2151 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
2154 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
2156 ofputil_stats_body(const struct ofp_header *oh)
2158 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2159 return (const struct ofp_stats_msg *) oh + 1;
2162 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
2164 ofputil_stats_body_len(const struct ofp_header *oh)
2166 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2167 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
2170 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
2172 ofputil_nxstats_body(const struct ofp_header *oh)
2174 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2175 return ((const struct nicira_stats_msg *) oh) + 1;
2178 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
2180 ofputil_nxstats_body_len(const struct ofp_header *oh)
2182 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
2183 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
2187 make_flow_mod(uint16_t command, const struct cls_rule *rule,
2190 struct ofp_flow_mod *ofm;
2191 size_t size = sizeof *ofm + actions_len;
2192 struct ofpbuf *out = ofpbuf_new(size);
2193 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
2194 ofm->header.version = OFP_VERSION;
2195 ofm->header.type = OFPT_FLOW_MOD;
2196 ofm->header.length = htons(size);
2198 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
2199 ofputil_cls_rule_to_match(rule, &ofm->match);
2200 ofm->command = htons(command);
2205 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
2206 uint16_t idle_timeout, size_t actions_len)
2208 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
2209 struct ofp_flow_mod *ofm = out->data;
2210 ofm->idle_timeout = htons(idle_timeout);
2211 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
2212 ofm->buffer_id = htonl(buffer_id);
2217 make_del_flow(const struct cls_rule *rule)
2219 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
2220 struct ofp_flow_mod *ofm = out->data;
2221 ofm->out_port = htons(OFPP_NONE);
2226 make_add_simple_flow(const struct cls_rule *rule,
2227 uint32_t buffer_id, uint16_t out_port,
2228 uint16_t idle_timeout)
2230 if (out_port != OFPP_NONE) {
2231 struct ofp_action_output *oao;
2232 struct ofpbuf *buffer;
2234 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
2235 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
2238 return make_add_flow(rule, buffer_id, idle_timeout, 0);
2243 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
2244 const struct ofpbuf *payload, int max_send_len)
2246 struct ofp_packet_in *opi;
2250 send_len = MIN(max_send_len, payload->size);
2251 buf = ofpbuf_new(sizeof *opi + send_len);
2252 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
2253 OFPT_PACKET_IN, 0, buf);
2254 opi->buffer_id = htonl(buffer_id);
2255 opi->total_len = htons(payload->size);
2256 opi->in_port = htons(in_port);
2257 opi->reason = reason;
2258 ofpbuf_put(buf, payload->data, send_len);
2259 update_openflow_length(buf);
2264 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
2266 make_echo_request(void)
2268 struct ofp_header *rq;
2269 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
2270 rq = ofpbuf_put_uninit(out, sizeof *rq);
2271 rq->version = OFP_VERSION;
2272 rq->type = OFPT_ECHO_REQUEST;
2273 rq->length = htons(sizeof *rq);
2278 /* Creates and returns an OFPT_ECHO_REPLY message matching the
2279 * OFPT_ECHO_REQUEST message in 'rq'. */
2281 make_echo_reply(const struct ofp_header *rq)
2283 size_t size = ntohs(rq->length);
2284 struct ofpbuf *out = ofpbuf_new(size);
2285 struct ofp_header *reply = ofpbuf_put(out, rq, size);
2286 reply->type = OFPT_ECHO_REPLY;
2291 ofputil_encode_barrier_request(void)
2295 make_openflow(sizeof(struct ofp_header), OFPT_BARRIER_REQUEST, &msg);
2300 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
2302 switch (flags & OFPC_FRAG_MASK) {
2303 case OFPC_FRAG_NORMAL: return "normal";
2304 case OFPC_FRAG_DROP: return "drop";
2305 case OFPC_FRAG_REASM: return "reassemble";
2306 case OFPC_FRAG_NX_MATCH: return "nx-match";
2313 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
2315 if (!strcasecmp(s, "normal")) {
2316 *flags = OFPC_FRAG_NORMAL;
2317 } else if (!strcasecmp(s, "drop")) {
2318 *flags = OFPC_FRAG_DROP;
2319 } else if (!strcasecmp(s, "reassemble")) {
2320 *flags = OFPC_FRAG_REASM;
2321 } else if (!strcasecmp(s, "nx-match")) {
2322 *flags = OFPC_FRAG_NX_MATCH;
2329 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
2330 * that the switch will never have more than 'max_ports' ports. Returns 0 if
2331 * 'port' is valid, otherwise an OpenFlow return code. */
2333 ofputil_check_output_port(uint16_t port, int max_ports)
2341 case OFPP_CONTROLLER:
2346 if (port < max_ports) {
2349 return OFPERR_OFPBAC_BAD_OUT_PORT;
2353 #define OFPUTIL_NAMED_PORTS \
2354 OFPUTIL_NAMED_PORT(IN_PORT) \
2355 OFPUTIL_NAMED_PORT(TABLE) \
2356 OFPUTIL_NAMED_PORT(NORMAL) \
2357 OFPUTIL_NAMED_PORT(FLOOD) \
2358 OFPUTIL_NAMED_PORT(ALL) \
2359 OFPUTIL_NAMED_PORT(CONTROLLER) \
2360 OFPUTIL_NAMED_PORT(LOCAL) \
2361 OFPUTIL_NAMED_PORT(NONE)
2363 /* Checks whether 's' is the string representation of an OpenFlow port number,
2364 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
2365 * number in '*port' and returns true. Otherwise, returns false. */
2367 ofputil_port_from_string(const char *name, uint16_t *port)
2373 static const struct pair pairs[] = {
2374 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2376 #undef OFPUTIL_NAMED_PORT
2378 static const int n_pairs = ARRAY_SIZE(pairs);
2381 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2386 for (i = 0; i < n_pairs; i++) {
2387 if (!strcasecmp(name, pairs[i].name)) {
2388 *port = pairs[i].value;
2395 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
2396 * Most ports' string representation is just the port number, but for special
2397 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
2399 ofputil_format_port(uint16_t port, struct ds *s)
2404 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2406 #undef OFPUTIL_NAMED_PORT
2409 ds_put_format(s, "%"PRIu16, port);
2412 ds_put_cstr(s, name);
2416 check_resubmit_table(const struct nx_action_resubmit *nar)
2418 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2419 return OFPERR_OFPBAC_BAD_ARGUMENT;
2425 check_output_reg(const struct nx_action_output_reg *naor,
2426 const struct flow *flow)
2428 struct mf_subfield src;
2431 for (i = 0; i < sizeof naor->zero; i++) {
2432 if (naor->zero[i]) {
2433 return OFPERR_OFPBAC_BAD_ARGUMENT;
2437 nxm_decode(&src, naor->src, naor->ofs_nbits);
2438 return mf_check_src(&src, flow);
2442 validate_actions(const union ofp_action *actions, size_t n_actions,
2443 const struct flow *flow, int max_ports)
2445 const union ofp_action *a;
2448 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2453 code = ofputil_decode_action(a);
2456 VLOG_WARN_RL(&bad_ofmsg_rl,
2457 "action decoding error at offset %td (%s)",
2458 (a - actions) * sizeof *a, ofperr_get_name(error));
2464 switch ((enum ofputil_action_code) code) {
2465 case OFPUTIL_OFPAT_OUTPUT:
2466 error = ofputil_check_output_port(ntohs(a->output.port),
2470 case OFPUTIL_OFPAT_SET_VLAN_VID:
2471 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2472 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2476 case OFPUTIL_OFPAT_SET_VLAN_PCP:
2477 if (a->vlan_pcp.vlan_pcp & ~7) {
2478 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2482 case OFPUTIL_OFPAT_ENQUEUE:
2483 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
2484 if (port >= max_ports && port != OFPP_IN_PORT
2485 && port != OFPP_LOCAL) {
2486 error = OFPERR_OFPBAC_BAD_OUT_PORT;
2490 case OFPUTIL_NXAST_REG_MOVE:
2491 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2495 case OFPUTIL_NXAST_REG_LOAD:
2496 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2500 case OFPUTIL_NXAST_MULTIPATH:
2501 error = multipath_check((const struct nx_action_multipath *) a,
2505 case OFPUTIL_NXAST_AUTOPATH:
2506 error = autopath_check((const struct nx_action_autopath *) a,
2510 case OFPUTIL_NXAST_BUNDLE:
2511 case OFPUTIL_NXAST_BUNDLE_LOAD:
2512 error = bundle_check((const struct nx_action_bundle *) a,
2516 case OFPUTIL_NXAST_OUTPUT_REG:
2517 error = check_output_reg((const struct nx_action_output_reg *) a,
2521 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2522 error = check_resubmit_table(
2523 (const struct nx_action_resubmit *) a);
2526 case OFPUTIL_NXAST_LEARN:
2527 error = learn_check((const struct nx_action_learn *) a, flow);
2530 case OFPUTIL_OFPAT_STRIP_VLAN:
2531 case OFPUTIL_OFPAT_SET_NW_SRC:
2532 case OFPUTIL_OFPAT_SET_NW_DST:
2533 case OFPUTIL_OFPAT_SET_NW_TOS:
2534 case OFPUTIL_OFPAT_SET_TP_SRC:
2535 case OFPUTIL_OFPAT_SET_TP_DST:
2536 case OFPUTIL_OFPAT_SET_DL_SRC:
2537 case OFPUTIL_OFPAT_SET_DL_DST:
2538 case OFPUTIL_NXAST_RESUBMIT:
2539 case OFPUTIL_NXAST_SET_TUNNEL:
2540 case OFPUTIL_NXAST_SET_QUEUE:
2541 case OFPUTIL_NXAST_POP_QUEUE:
2542 case OFPUTIL_NXAST_NOTE:
2543 case OFPUTIL_NXAST_SET_TUNNEL64:
2544 case OFPUTIL_NXAST_EXIT:
2545 case OFPUTIL_NXAST_DEC_TTL:
2546 case OFPUTIL_NXAST_FIN_TIMEOUT:
2551 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
2552 (a - actions) * sizeof *a, ofperr_get_name(error));
2557 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
2558 (n_actions - left) * sizeof *a);
2559 return OFPERR_OFPBAC_BAD_LEN;
2564 struct ofputil_action {
2566 unsigned int min_len;
2567 unsigned int max_len;
2570 static const struct ofputil_action action_bad_type
2571 = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX };
2572 static const struct ofputil_action action_bad_len
2573 = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX };
2574 static const struct ofputil_action action_bad_vendor
2575 = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX };
2577 static const struct ofputil_action *
2578 ofputil_decode_ofpat_action(const union ofp_action *a)
2580 enum ofp_action_type type = ntohs(a->type);
2583 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2585 static const struct ofputil_action action = { \
2587 sizeof(struct STRUCT), \
2588 sizeof(struct STRUCT) \
2592 #include "ofp-util.def"
2596 return &action_bad_type;
2600 static const struct ofputil_action *
2601 ofputil_decode_nxast_action(const union ofp_action *a)
2603 const struct nx_action_header *nah = (const struct nx_action_header *) a;
2604 enum nx_action_subtype subtype = ntohs(nah->subtype);
2607 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2609 static const struct ofputil_action action = { \
2611 sizeof(struct STRUCT), \
2612 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
2616 #include "ofp-util.def"
2618 case NXAST_SNAT__OBSOLETE:
2619 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
2621 return &action_bad_type;
2625 /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or
2626 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error
2629 * The caller must have already verified that 'a''s length is correct (that is,
2630 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
2631 * longer than the amount of space allocated to 'a').
2633 * This function verifies that 'a''s length is correct for the type of action
2634 * that it represents. */
2636 ofputil_decode_action(const union ofp_action *a)
2638 const struct ofputil_action *action;
2639 uint16_t len = ntohs(a->header.len);
2641 if (a->type != htons(OFPAT_VENDOR)) {
2642 action = ofputil_decode_ofpat_action(a);
2644 switch (ntohl(a->vendor.vendor)) {
2646 if (len < sizeof(struct nx_action_header)) {
2647 return -OFPERR_OFPBAC_BAD_LEN;
2649 action = ofputil_decode_nxast_action(a);
2652 action = &action_bad_vendor;
2657 return (len >= action->min_len && len <= action->max_len
2659 : -OFPERR_OFPBAC_BAD_LEN);
2662 /* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_*
2663 * constant. The caller must have already validated that 'a' is a valid action
2664 * understood by Open vSwitch (e.g. by a previous successful call to
2665 * ofputil_decode_action()). */
2666 enum ofputil_action_code
2667 ofputil_decode_action_unsafe(const union ofp_action *a)
2669 const struct ofputil_action *action;
2671 if (a->type != htons(OFPAT_VENDOR)) {
2672 action = ofputil_decode_ofpat_action(a);
2674 action = ofputil_decode_nxast_action(a);
2677 return action->code;
2680 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
2681 * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if
2682 * 'name' is not the name of any action.
2684 * ofp-util.def lists the mapping from names to action. */
2686 ofputil_action_code_from_name(const char *name)
2688 static const char *names[OFPUTIL_N_ACTIONS] = {
2689 #define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME,
2690 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
2691 #include "ofp-util.def"
2696 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
2697 if (*p && !strcasecmp(name, *p)) {
2704 /* Appends an action of the type specified by 'code' to 'buf' and returns the
2705 * action. Initializes the parts of 'action' that identify it as having type
2706 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
2707 * have variable length, the length used and cleared is that of struct
2710 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
2713 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2714 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2715 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2716 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2717 #include "ofp-util.def"
2722 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2724 ofputil_init_##ENUM(struct STRUCT *s) \
2726 memset(s, 0, sizeof *s); \
2727 s->type = htons(ENUM); \
2728 s->len = htons(sizeof *s); \
2732 ofputil_put_##ENUM(struct ofpbuf *buf) \
2734 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2735 ofputil_init_##ENUM(s); \
2738 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2740 ofputil_init_##ENUM(struct STRUCT *s) \
2742 memset(s, 0, sizeof *s); \
2743 s->type = htons(OFPAT_VENDOR); \
2744 s->len = htons(sizeof *s); \
2745 s->vendor = htonl(NX_VENDOR_ID); \
2746 s->subtype = htons(ENUM); \
2750 ofputil_put_##ENUM(struct ofpbuf *buf) \
2752 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2753 ofputil_init_##ENUM(s); \
2756 #include "ofp-util.def"
2758 /* Returns true if 'action' outputs to 'port', false otherwise. */
2760 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2762 switch (ntohs(action->type)) {
2764 return action->output.port == port;
2766 return ((const struct ofp_action_enqueue *) action)->port == port;
2772 /* "Normalizes" the wildcards in 'rule'. That means:
2774 * 1. If the type of level N is known, then only the valid fields for that
2775 * level may be specified. For example, ARP does not have a TOS field,
2776 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2777 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2778 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2781 * 2. If the type of level N is not known (or not understood by Open
2782 * vSwitch), then no fields at all for that level may be specified. For
2783 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2784 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2787 * 'flow_format' specifies the format of the flow as received or as intended to
2788 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2789 * detailed matching. */
2791 ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2794 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2795 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2796 MAY_NW_PROTO = 1 << 2, /* nw_proto */
2797 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
2798 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2799 MAY_ARP_THA = 1 << 5, /* arp_tha */
2800 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
2801 MAY_ND_TARGET = 1 << 7 /* nd_target */
2804 struct flow_wildcards wc;
2806 /* Figure out what fields may be matched. */
2807 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2808 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
2809 if (rule->flow.nw_proto == IPPROTO_TCP ||
2810 rule->flow.nw_proto == IPPROTO_UDP ||
2811 rule->flow.nw_proto == IPPROTO_ICMP) {
2812 may_match |= MAY_TP_ADDR;
2814 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2815 && flow_format == NXFF_NXM) {
2816 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
2817 if (rule->flow.nw_proto == IPPROTO_TCP ||
2818 rule->flow.nw_proto == IPPROTO_UDP) {
2819 may_match |= MAY_TP_ADDR;
2820 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2821 may_match |= MAY_TP_ADDR;
2822 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2823 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2824 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2825 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2828 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2829 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2830 if (flow_format == NXFF_NXM) {
2831 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
2837 /* Clear the fields that may not be matched. */
2839 if (!(may_match & MAY_NW_ADDR)) {
2840 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2842 if (!(may_match & MAY_TP_ADDR)) {
2843 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
2845 if (!(may_match & MAY_NW_PROTO)) {
2846 wc.wildcards |= FWW_NW_PROTO;
2848 if (!(may_match & MAY_IPVx)) {
2849 wc.wildcards |= FWW_NW_DSCP;
2850 wc.wildcards |= FWW_NW_ECN;
2851 wc.wildcards |= FWW_NW_TTL;
2853 if (!(may_match & MAY_ARP_SHA)) {
2854 wc.wildcards |= FWW_ARP_SHA;
2856 if (!(may_match & MAY_ARP_THA)) {
2857 wc.wildcards |= FWW_ARP_THA;
2859 if (!(may_match & MAY_IPV6)) {
2860 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2861 wc.wildcards |= FWW_IPV6_LABEL;
2863 if (!(may_match & MAY_ND_TARGET)) {
2864 wc.wildcards |= FWW_ND_TARGET;
2867 /* Log any changes. */
2868 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2869 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2870 char *pre = log ? cls_rule_to_string(rule) : NULL;
2873 cls_rule_zero_wildcarded_fields(rule);
2876 char *post = cls_rule_to_string(rule);
2877 VLOG_INFO("normalization changed ofp_match, details:");
2878 VLOG_INFO(" pre: %s", pre);
2879 VLOG_INFO("post: %s", post);
2886 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2887 * successful, otherwise an OpenFlow error.
2889 * If successful, the first action is stored in '*actionsp' and the number of
2890 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2891 * are stored, respectively.
2893 * This function does not check that the actions are valid (the caller should
2894 * do so, with validate_actions()). The caller is also responsible for making
2895 * sure that 'b->data' is initially aligned appropriately for "union
2898 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2899 union ofp_action **actionsp, size_t *n_actionsp)
2901 if (actions_len % OFP_ACTION_ALIGN != 0) {
2902 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2903 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2907 *actionsp = ofpbuf_try_pull(b, actions_len);
2908 if (*actionsp == NULL) {
2909 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2910 "exceeds remaining message length (%zu)",
2911 actions_len, b->size);
2915 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2921 return OFPERR_OFPBRC_BAD_LEN;
2925 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
2926 const union ofp_action *b, size_t n_b)
2928 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
2932 ofputil_actions_clone(const union ofp_action *actions, size_t n)
2934 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
2937 /* Parses a key or a key-value pair from '*stringp'.
2939 * On success: Stores the key into '*keyp'. Stores the value, if present, into
2940 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
2941 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
2942 * are substrings of '*stringp' created by replacing some of its bytes by null
2943 * terminators. Returns true.
2945 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
2946 * NULL and returns false. */
2948 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
2950 char *pos, *key, *value;
2954 pos += strspn(pos, ", \t\r\n");
2956 *keyp = *valuep = NULL;
2961 key_len = strcspn(pos, ":=(, \t\r\n");
2962 if (key[key_len] == ':' || key[key_len] == '=') {
2963 /* The value can be separated by a colon. */
2966 value = key + key_len + 1;
2967 value_len = strcspn(value, ", \t\r\n");
2968 pos = value + value_len + (value[value_len] != '\0');
2969 value[value_len] = '\0';
2970 } else if (key[key_len] == '(') {
2971 /* The value can be surrounded by balanced parentheses. The outermost
2972 * set of parentheses is removed. */
2976 value = key + key_len + 1;
2977 for (value_len = 0; level > 0; value_len++) {
2978 switch (value[value_len]) {
2980 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
2991 value[value_len - 1] = '\0';
2992 pos = value + value_len;
2994 /* There might be no value at all. */
2995 value = key + key_len; /* Will become the empty string below. */
2996 pos = key + key_len + (key[key_len] != '\0');
2998 key[key_len] = '\0';