2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 "meta-flow.h"
32 #include "multipath.h"
35 #include "ofp-actions.h"
36 #include "ofp-errors.h"
42 #include "unaligned.h"
43 #include "type-props.h"
46 VLOG_DEFINE_THIS_MODULE(ofp_util);
48 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
49 * in the peer and so there's not much point in showing a lot of them. */
50 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
52 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
53 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
56 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
57 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
58 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
59 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
62 ofputil_wcbits_to_netmask(int wcbits)
65 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
68 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
69 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
70 * between 0 and 32 inclusive.
72 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
73 * still be in the valid range but isn't otherwise meaningful. */
75 ofputil_netmask_to_wcbits(ovs_be32 netmask)
77 return 32 - ip_count_cidr_bits(netmask);
80 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
81 * flow_wildcards in 'wc' for use in struct cls_rule. It is the caller's
82 * responsibility to handle the special case where the flow match's dl_vlan is
83 * set to OFP_VLAN_NONE. */
85 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
87 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
89 /* Initialize most of rule->wc. */
90 flow_wildcards_init_catchall(wc);
92 /* Start with wildcard fields that aren't defined by ofp10_match. */
93 wc->wildcards = FWW_NW_ECN | FWW_NW_TTL;
95 if (ofpfw & OFPFW10_IN_PORT) {
96 wc->wildcards |= FWW_IN_PORT;
98 if (ofpfw & OFPFW10_DL_TYPE) {
99 wc->wildcards |= FWW_DL_TYPE;
101 if (ofpfw & OFPFW10_NW_PROTO) {
102 wc->wildcards |= FWW_NW_PROTO;
104 if (ofpfw & OFPFW10_NW_TOS) {
105 wc->wildcards |= FWW_NW_DSCP;
108 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_SRC_SHIFT);
109 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_DST_SHIFT);
111 if (!(ofpfw & OFPFW10_TP_SRC)) {
112 wc->tp_src_mask = htons(UINT16_MAX);
114 if (!(ofpfw & OFPFW10_TP_DST)) {
115 wc->tp_dst_mask = htons(UINT16_MAX);
118 if (!(ofpfw & OFPFW10_DL_SRC)) {
119 memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN);
121 if (!(ofpfw & OFPFW10_DL_DST)) {
122 memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN);
126 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
127 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
129 if (!(ofpfw & OFPFW10_DL_VLAN)) {
130 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
134 /* Converts the ofp10_match in 'match' into a cls_rule in 'rule', with the
135 * given 'priority'. */
137 ofputil_cls_rule_from_ofp10_match(const struct ofp10_match *match,
138 unsigned int priority, struct cls_rule *rule)
140 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW10_ALL;
142 /* Initialize rule->priority, rule->wc. */
143 rule->priority = !ofpfw ? UINT16_MAX : priority;
144 ofputil_wildcard_from_ofpfw10(ofpfw, &rule->wc);
146 /* Initialize most of rule->flow. */
147 rule->flow.nw_src = match->nw_src;
148 rule->flow.nw_dst = match->nw_dst;
149 rule->flow.in_port = ntohs(match->in_port);
150 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
151 rule->flow.tp_src = match->tp_src;
152 rule->flow.tp_dst = match->tp_dst;
153 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
154 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
155 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
156 rule->flow.nw_proto = match->nw_proto;
158 /* Translate VLANs. */
159 if (!(ofpfw & OFPFW10_DL_VLAN) &&
160 match->dl_vlan == htons(OFP10_VLAN_NONE)) {
161 /* Match only packets without 802.1Q header.
163 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
165 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
166 * because we can't have a specific PCP without an 802.1Q header.
167 * However, older versions of OVS treated this as matching packets
168 * withut an 802.1Q header, so we do here too. */
169 rule->flow.vlan_tci = htons(0);
170 rule->wc.vlan_tci_mask = htons(0xffff);
172 ovs_be16 vid, pcp, tci;
174 vid = match->dl_vlan & htons(VLAN_VID_MASK);
175 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
176 tci = vid | pcp | htons(VLAN_CFI);
177 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
181 cls_rule_zero_wildcarded_fields(rule);
184 /* Convert 'rule' into the OpenFlow 1.0 match structure 'match'. */
186 ofputil_cls_rule_to_ofp10_match(const struct cls_rule *rule,
187 struct ofp10_match *match)
189 const struct flow_wildcards *wc = &rule->wc;
192 /* Figure out most OpenFlow wildcards. */
194 if (wc->wildcards & FWW_IN_PORT) {
195 ofpfw |= OFPFW10_IN_PORT;
197 if (wc->wildcards & FWW_DL_TYPE) {
198 ofpfw |= OFPFW10_DL_TYPE;
200 if (wc->wildcards & FWW_NW_PROTO) {
201 ofpfw |= OFPFW10_NW_PROTO;
203 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_src_mask)
204 << OFPFW10_NW_SRC_SHIFT);
205 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_dst_mask)
206 << OFPFW10_NW_DST_SHIFT);
207 if (wc->wildcards & FWW_NW_DSCP) {
208 ofpfw |= OFPFW10_NW_TOS;
210 if (!wc->tp_src_mask) {
211 ofpfw |= OFPFW10_TP_SRC;
213 if (!wc->tp_dst_mask) {
214 ofpfw |= OFPFW10_TP_DST;
216 if (eth_addr_is_zero(wc->dl_src_mask)) {
217 ofpfw |= OFPFW10_DL_SRC;
219 if (eth_addr_is_zero(wc->dl_dst_mask)) {
220 ofpfw |= OFPFW10_DL_DST;
223 /* Translate VLANs. */
224 match->dl_vlan = htons(0);
225 match->dl_vlan_pcp = 0;
226 if (rule->wc.vlan_tci_mask == htons(0)) {
227 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
228 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
229 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
230 match->dl_vlan = htons(OFP10_VLAN_NONE);
231 ofpfw |= OFPFW10_DL_VLAN_PCP;
233 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
234 ofpfw |= OFPFW10_DL_VLAN;
236 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
239 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
240 ofpfw |= OFPFW10_DL_VLAN_PCP;
242 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
246 /* Compose most of the match structure. */
247 match->wildcards = htonl(ofpfw);
248 match->in_port = htons(rule->flow.in_port);
249 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
250 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
251 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
252 match->nw_src = rule->flow.nw_src;
253 match->nw_dst = rule->flow.nw_dst;
254 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
255 match->nw_proto = rule->flow.nw_proto;
256 match->tp_src = rule->flow.tp_src;
257 match->tp_dst = rule->flow.tp_dst;
258 memset(match->pad1, '\0', sizeof match->pad1);
259 memset(match->pad2, '\0', sizeof match->pad2);
263 ofputil_pull_ofp11_match(struct ofpbuf *buf, unsigned int priority,
264 struct cls_rule *rule, uint16_t *padded_match_len)
266 struct ofp11_match_header *omh = buf->data;
269 if (buf->size < sizeof *omh) {
270 return OFPERR_OFPBMC_BAD_LEN;
273 match_len = ntohs(omh->length);
275 switch (ntohs(omh->type)) {
276 case OFPMT_STANDARD: {
277 struct ofp11_match *om;
279 if (match_len != sizeof *om || buf->size < sizeof *om) {
280 return OFPERR_OFPBMC_BAD_LEN;
282 om = ofpbuf_pull(buf, sizeof *om);
283 if (padded_match_len) {
284 *padded_match_len = match_len;
286 return ofputil_cls_rule_from_ofp11_match(om, priority, rule);
290 if (padded_match_len) {
291 *padded_match_len = ROUND_UP(match_len, 8);
293 return oxm_pull_match(buf, priority, rule);
296 return OFPERR_OFPBMC_BAD_TYPE;
300 /* Converts the ofp11_match in 'match' into a cls_rule in 'rule', with the
301 * given 'priority'. Returns 0 if successful, otherwise an OFPERR_* value. */
303 ofputil_cls_rule_from_ofp11_match(const struct ofp11_match *match,
304 unsigned int priority,
305 struct cls_rule *rule)
307 uint16_t wc = ntohl(match->wildcards);
308 uint8_t dl_src_mask[ETH_ADDR_LEN];
309 uint8_t dl_dst_mask[ETH_ADDR_LEN];
313 cls_rule_init_catchall(rule, priority);
315 if (!(wc & OFPFW11_IN_PORT)) {
319 error = ofputil_port_from_ofp11(match->in_port, &ofp_port);
321 return OFPERR_OFPBMC_BAD_VALUE;
323 cls_rule_set_in_port(rule, ofp_port);
326 for (i = 0; i < ETH_ADDR_LEN; i++) {
327 dl_src_mask[i] = ~match->dl_src_mask[i];
329 cls_rule_set_dl_src_masked(rule, match->dl_src, dl_src_mask);
331 for (i = 0; i < ETH_ADDR_LEN; i++) {
332 dl_dst_mask[i] = ~match->dl_dst_mask[i];
334 cls_rule_set_dl_dst_masked(rule, match->dl_dst, dl_dst_mask);
336 if (!(wc & OFPFW11_DL_VLAN)) {
337 if (match->dl_vlan == htons(OFPVID11_NONE)) {
338 /* Match only packets without a VLAN tag. */
339 rule->flow.vlan_tci = htons(0);
340 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
342 if (match->dl_vlan == htons(OFPVID11_ANY)) {
343 /* Match any packet with a VLAN tag regardless of VID. */
344 rule->flow.vlan_tci = htons(VLAN_CFI);
345 rule->wc.vlan_tci_mask = htons(VLAN_CFI);
346 } else if (ntohs(match->dl_vlan) < 4096) {
347 /* Match only packets with the specified VLAN VID. */
348 rule->flow.vlan_tci = htons(VLAN_CFI) | match->dl_vlan;
349 rule->wc.vlan_tci_mask = htons(VLAN_CFI | VLAN_VID_MASK);
352 return OFPERR_OFPBMC_BAD_VALUE;
355 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
356 if (match->dl_vlan_pcp <= 7) {
357 rule->flow.vlan_tci |= htons(match->dl_vlan_pcp
359 rule->wc.vlan_tci_mask |= htons(VLAN_PCP_MASK);
362 return OFPERR_OFPBMC_BAD_VALUE;
368 if (!(wc & OFPFW11_DL_TYPE)) {
369 cls_rule_set_dl_type(rule,
370 ofputil_dl_type_from_openflow(match->dl_type));
373 ipv4 = rule->flow.dl_type == htons(ETH_TYPE_IP);
374 arp = rule->flow.dl_type == htons(ETH_TYPE_ARP);
376 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
377 if (match->nw_tos & ~IP_DSCP_MASK) {
379 return OFPERR_OFPBMC_BAD_VALUE;
382 cls_rule_set_nw_dscp(rule, match->nw_tos);
386 if (!(wc & OFPFW11_NW_PROTO)) {
387 cls_rule_set_nw_proto(rule, match->nw_proto);
389 cls_rule_set_nw_src_masked(rule, match->nw_src, ~match->nw_src_mask);
390 cls_rule_set_nw_dst_masked(rule, match->nw_dst, ~match->nw_dst_mask);
393 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
394 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
395 switch (rule->flow.nw_proto) {
397 /* "A.2.3 Flow Match Structures" in OF1.1 says:
399 * The tp_src and tp_dst fields will be ignored unless the
400 * network protocol specified is as TCP, UDP or SCTP.
402 * but I'm pretty sure we should support ICMP too, otherwise
403 * that's a regression from OF1.0. */
404 if (!(wc & OFPFW11_TP_SRC)) {
405 uint16_t icmp_type = ntohs(match->tp_src);
406 if (icmp_type < 0x100) {
407 cls_rule_set_icmp_type(rule, icmp_type);
409 return OFPERR_OFPBMC_BAD_FIELD;
412 if (!(wc & OFPFW11_TP_DST)) {
413 uint16_t icmp_code = ntohs(match->tp_dst);
414 if (icmp_code < 0x100) {
415 cls_rule_set_icmp_code(rule, icmp_code);
417 return OFPERR_OFPBMC_BAD_FIELD;
424 if (!(wc & (OFPFW11_TP_SRC))) {
425 cls_rule_set_tp_src(rule, match->tp_src);
427 if (!(wc & (OFPFW11_TP_DST))) {
428 cls_rule_set_tp_dst(rule, match->tp_dst);
433 /* We don't support SCTP and it seems that we should tell the
434 * controller, since OF1.1 implementations are supposed to. */
435 return OFPERR_OFPBMC_BAD_FIELD;
438 /* OF1.1 says explicitly to ignore this. */
443 if (rule->flow.dl_type == htons(ETH_TYPE_MPLS) ||
444 rule->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
445 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
447 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
448 /* MPLS not supported. */
449 return OFPERR_OFPBMC_BAD_TAG;
453 if (match->metadata_mask != htonll(UINT64_MAX)) {
454 cls_rule_set_metadata_masked(rule, match->metadata,
455 ~match->metadata_mask);
461 /* Convert 'rule' into the OpenFlow 1.1 match structure 'match'. */
463 ofputil_cls_rule_to_ofp11_match(const struct cls_rule *rule,
464 struct ofp11_match *match)
469 memset(match, 0, sizeof *match);
470 match->omh.type = htons(OFPMT_STANDARD);
471 match->omh.length = htons(OFPMT11_STANDARD_LENGTH);
473 if (rule->wc.wildcards & FWW_IN_PORT) {
474 wc |= OFPFW11_IN_PORT;
476 match->in_port = ofputil_port_to_ofp11(rule->flow.in_port);
479 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
480 for (i = 0; i < ETH_ADDR_LEN; i++) {
481 match->dl_src_mask[i] = ~rule->wc.dl_src_mask[i];
484 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
485 for (i = 0; i < ETH_ADDR_LEN; i++) {
486 match->dl_dst_mask[i] = ~rule->wc.dl_dst_mask[i];
489 if (rule->wc.vlan_tci_mask == htons(0)) {
490 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
491 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
492 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
493 match->dl_vlan = htons(OFPVID11_NONE);
494 wc |= OFPFW11_DL_VLAN_PCP;
496 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
497 match->dl_vlan = htons(OFPVID11_ANY);
499 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
502 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
503 wc |= OFPFW11_DL_VLAN_PCP;
505 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
509 if (rule->wc.wildcards & FWW_DL_TYPE) {
510 wc |= OFPFW11_DL_TYPE;
512 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
515 if (rule->wc.wildcards & FWW_NW_DSCP) {
516 wc |= OFPFW11_NW_TOS;
518 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
521 if (rule->wc.wildcards & FWW_NW_PROTO) {
522 wc |= OFPFW11_NW_PROTO;
524 match->nw_proto = rule->flow.nw_proto;
527 match->nw_src = rule->flow.nw_src;
528 match->nw_src_mask = ~rule->wc.nw_src_mask;
529 match->nw_dst = rule->flow.nw_dst;
530 match->nw_dst_mask = ~rule->wc.nw_dst_mask;
532 if (!rule->wc.tp_src_mask) {
533 wc |= OFPFW11_TP_SRC;
535 match->tp_src = rule->flow.tp_src;
538 if (!rule->wc.tp_dst_mask) {
539 wc |= OFPFW11_TP_DST;
541 match->tp_dst = rule->flow.tp_dst;
544 /* MPLS not supported. */
545 wc |= OFPFW11_MPLS_LABEL;
546 wc |= OFPFW11_MPLS_TC;
548 match->metadata = rule->flow.metadata;
549 match->metadata_mask = ~rule->wc.metadata_mask;
551 match->wildcards = htonl(wc);
554 /* Given a 'dl_type' value in the format used in struct flow, returns the
555 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
558 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
560 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
561 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
565 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
566 * structure, returns the corresponding 'dl_type' value for use in struct
569 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
571 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
572 ? htons(FLOW_DL_TYPE_NONE)
578 struct proto_abbrev {
579 enum ofputil_protocol protocol;
583 /* Most users really don't care about some of the differences between
584 * protocols. These abbreviations help with that. */
585 static const struct proto_abbrev proto_abbrevs[] = {
586 { OFPUTIL_P_ANY, "any" },
587 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
588 { OFPUTIL_P_NXM_ANY, "NXM" },
590 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
592 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
596 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
598 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
599 * connection that has negotiated the given 'version'. 'version' should
600 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
601 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
602 * outside the valid range. */
603 enum ofputil_protocol
604 ofputil_protocol_from_ofp_version(enum ofp_version version)
608 return OFPUTIL_P_OF10;
610 return OFPUTIL_P_OF12;
617 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
618 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
620 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
624 case OFPUTIL_P_OF10_TID:
626 case OFPUTIL_P_NXM_TID:
627 return OFP10_VERSION;
629 return OFP12_VERSION;
635 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
638 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
640 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
643 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
644 * extension turned on or off if 'enable' is true or false, respectively.
646 * This extension is only useful for protocols whose "standard" version does
647 * not allow specific tables to be modified. In particular, this is true of
648 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
649 * specifies a table ID and so there is no need for such an extension. When
650 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
651 * extension, this function just returns its 'protocol' argument unchanged
652 * regardless of the value of 'enable'. */
653 enum ofputil_protocol
654 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
658 case OFPUTIL_P_OF10_TID:
659 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
662 case OFPUTIL_P_NXM_TID:
663 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
666 return OFPUTIL_P_OF12;
673 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
674 * some extension to a standard protocol version, the return value is the
675 * standard version of that protocol without any extension. If 'protocol' is a
676 * standard protocol version, returns 'protocol' unchanged. */
677 enum ofputil_protocol
678 ofputil_protocol_to_base(enum ofputil_protocol protocol)
680 return ofputil_protocol_set_tid(protocol, false);
683 /* Returns 'new_base' with any extensions taken from 'cur'. */
684 enum ofputil_protocol
685 ofputil_protocol_set_base(enum ofputil_protocol cur,
686 enum ofputil_protocol new_base)
688 bool tid = (cur & OFPUTIL_P_TID) != 0;
692 case OFPUTIL_P_OF10_TID:
693 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
696 case OFPUTIL_P_NXM_TID:
697 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
700 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
707 /* Returns a string form of 'protocol', if a simple form exists (that is, if
708 * 'protocol' is either a single protocol or it is a combination of protocols
709 * that have a single abbreviation). Otherwise, returns NULL. */
711 ofputil_protocol_to_string(enum ofputil_protocol protocol)
713 const struct proto_abbrev *p;
715 /* Use a "switch" statement for single-bit names so that we get a compiler
716 * warning if we forget any. */
719 return "NXM-table_id";
721 case OFPUTIL_P_NXM_TID:
722 return "NXM+table_id";
725 return "OpenFlow10-table_id";
727 case OFPUTIL_P_OF10_TID:
728 return "OpenFlow10+table_id";
734 /* Check abbreviations. */
735 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
736 if (protocol == p->protocol) {
744 /* Returns a string that represents 'protocols'. The return value might be a
745 * comma-separated list if 'protocols' doesn't have a simple name. The return
746 * value is "none" if 'protocols' is 0.
748 * The caller must free the returned string (with free()). */
750 ofputil_protocols_to_string(enum ofputil_protocol protocols)
754 assert(!(protocols & ~OFPUTIL_P_ANY));
755 if (protocols == 0) {
756 return xstrdup("none");
761 const struct proto_abbrev *p;
765 ds_put_char(&s, ',');
768 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
769 if ((protocols & p->protocol) == p->protocol) {
770 ds_put_cstr(&s, p->name);
771 protocols &= ~p->protocol;
776 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
777 enum ofputil_protocol bit = 1u << i;
779 if (protocols & bit) {
780 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
789 return ds_steal_cstr(&s);
792 static enum ofputil_protocol
793 ofputil_protocol_from_string__(const char *s, size_t n)
795 const struct proto_abbrev *p;
798 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
799 enum ofputil_protocol bit = 1u << i;
800 const char *name = ofputil_protocol_to_string(bit);
802 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
807 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
808 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
816 /* Returns the nonempty set of protocols represented by 's', which can be a
817 * single protocol name or abbreviation or a comma-separated list of them.
819 * Aborts the program with an error message if 's' is invalid. */
820 enum ofputil_protocol
821 ofputil_protocols_from_string(const char *s)
823 const char *orig_s = s;
824 enum ofputil_protocol protocols;
828 enum ofputil_protocol p;
837 p = ofputil_protocol_from_string__(s, n);
839 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
847 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
853 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
855 switch (packet_in_format) {
856 case NXPIF_OPENFLOW10:
865 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
867 switch (packet_in_format) {
868 case NXPIF_OPENFLOW10:
878 ofputil_packet_in_format_from_string(const char *s)
880 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
881 : !strcmp(s, "nxm") ? NXPIF_NXM
886 regs_fully_wildcarded(const struct flow_wildcards *wc)
890 for (i = 0; i < FLOW_N_REGS; i++) {
891 if (wc->reg_masks[i] != 0) {
898 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
899 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
900 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
901 * use OpenFlow 1.0 protocol for backward compatibility. */
902 enum ofputil_protocol
903 ofputil_usable_protocols(const struct cls_rule *rule)
905 const struct flow_wildcards *wc = &rule->wc;
907 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
909 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
910 if (!eth_mask_is_exact(wc->dl_src_mask)
911 && !eth_addr_is_zero(wc->dl_src_mask)) {
912 return OFPUTIL_P_NXM_ANY;
914 if (!eth_mask_is_exact(wc->dl_dst_mask)
915 && !eth_addr_is_zero(wc->dl_dst_mask)) {
916 return OFPUTIL_P_NXM_ANY;
919 /* NXM and OF1.1+ support matching metadata. */
920 if (wc->metadata_mask != htonll(0)) {
921 return OFPUTIL_P_NXM_ANY;
924 /* Only NXM supports matching ARP hardware addresses. */
925 if (!eth_addr_is_zero(wc->arp_sha_mask) ||
926 !eth_addr_is_zero(wc->arp_tha_mask)) {
927 return OFPUTIL_P_NXM_ANY;
930 /* Only NXM supports matching IPv6 traffic. */
931 if (!(wc->wildcards & FWW_DL_TYPE)
932 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
933 return OFPUTIL_P_NXM_ANY;
936 /* Only NXM supports matching registers. */
937 if (!regs_fully_wildcarded(wc)) {
938 return OFPUTIL_P_NXM_ANY;
941 /* Only NXM supports matching tun_id. */
942 if (wc->tun_id_mask != htonll(0)) {
943 return OFPUTIL_P_NXM_ANY;
946 /* Only NXM supports matching fragments. */
947 if (wc->nw_frag_mask) {
948 return OFPUTIL_P_NXM_ANY;
951 /* Only NXM supports matching IPv6 flow label. */
952 if (wc->ipv6_label_mask) {
953 return OFPUTIL_P_NXM_ANY;
956 /* Only NXM supports matching IP ECN bits. */
957 if (!(wc->wildcards & FWW_NW_ECN)) {
958 return OFPUTIL_P_NXM_ANY;
961 /* Only NXM supports matching IP TTL/hop limit. */
962 if (!(wc->wildcards & FWW_NW_TTL)) {
963 return OFPUTIL_P_NXM_ANY;
966 /* Only NXM supports non-CIDR IPv4 address masks. */
967 if (!ip_is_cidr(wc->nw_src_mask) || !ip_is_cidr(wc->nw_dst_mask)) {
968 return OFPUTIL_P_NXM_ANY;
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))) {
974 return OFPUTIL_P_NXM_ANY;
977 /* Other formats can express this rule. */
978 return OFPUTIL_P_ANY;
981 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
982 * protocol is 'current', at least partly transitions the protocol to 'want'.
983 * Stores in '*next' the protocol that will be in effect on the OpenFlow
984 * connection if the switch processes the returned message correctly. (If
985 * '*next != want' then the caller will have to iterate.)
987 * If 'current == want', returns NULL and stores 'current' in '*next'. */
989 ofputil_encode_set_protocol(enum ofputil_protocol current,
990 enum ofputil_protocol want,
991 enum ofputil_protocol *next)
993 enum ofputil_protocol cur_base, want_base;
994 bool cur_tid, want_tid;
996 cur_base = ofputil_protocol_to_base(current);
997 want_base = ofputil_protocol_to_base(want);
998 if (cur_base != want_base) {
999 *next = ofputil_protocol_set_base(current, want_base);
1001 switch (want_base) {
1003 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1005 case OFPUTIL_P_OF10:
1006 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1008 case OFPUTIL_P_OF12:
1009 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1011 case OFPUTIL_P_OF10_TID:
1012 case OFPUTIL_P_NXM_TID:
1017 cur_tid = (current & OFPUTIL_P_TID) != 0;
1018 want_tid = (want & OFPUTIL_P_TID) != 0;
1019 if (cur_tid != want_tid) {
1020 *next = ofputil_protocol_set_tid(current, want_tid);
1021 return ofputil_make_flow_mod_table_id(want_tid);
1024 assert(current == want);
1030 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1031 * format to 'nxff'. */
1033 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1035 struct nx_set_flow_format *sff;
1038 assert(ofputil_nx_flow_format_is_valid(nxff));
1040 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1041 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1042 sff->format = htonl(nxff);
1047 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1049 enum ofputil_protocol
1050 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1052 switch (flow_format) {
1053 case NXFF_OPENFLOW10:
1054 return OFPUTIL_P_OF10;
1057 return OFPUTIL_P_NXM;
1059 case NXFF_OPENFLOW12:
1060 return OFPUTIL_P_OF12;
1067 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1069 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1071 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1074 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1077 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1079 switch (flow_format) {
1080 case NXFF_OPENFLOW10:
1081 return "openflow10";
1084 case NXFF_OPENFLOW12:
1085 return "openflow12";
1092 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1094 struct nx_set_packet_in_format *spif;
1097 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, 0);
1098 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1099 spif->format = htonl(packet_in_format);
1104 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1105 * extension on or off (according to 'flow_mod_table_id'). */
1107 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1109 struct nx_flow_mod_table_id *nfmti;
1112 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1113 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1114 nfmti->set = flow_mod_table_id;
1118 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1119 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1122 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1123 * The caller must initialize 'ofpacts' and retains ownership of it.
1124 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1126 * Does not validate the flow_mod actions. The caller should do that, with
1127 * ofpacts_check(). */
1129 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1130 const struct ofp_header *oh,
1131 enum ofputil_protocol protocol,
1132 struct ofpbuf *ofpacts)
1138 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1139 raw = ofpraw_pull_assert(&b);
1140 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1141 /* Standard OpenFlow 1.1 flow_mod. */
1142 const struct ofp11_flow_mod *ofm;
1145 ofm = ofpbuf_pull(&b, sizeof *ofm);
1147 error = ofputil_pull_ofp11_match(&b, ntohs(ofm->priority), &fm->cr,
1153 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1158 /* Translate the message. */
1159 if (ofm->command == OFPFC_ADD) {
1160 fm->cookie = htonll(0);
1161 fm->cookie_mask = htonll(0);
1162 fm->new_cookie = ofm->cookie;
1165 fm->cookie = ofm->cookie;
1166 fm->cookie_mask = ofm->cookie_mask;
1167 fm->new_cookie = htonll(UINT64_MAX);
1169 fm->command = ofm->command;
1170 fm->table_id = ofm->table_id;
1171 fm->idle_timeout = ntohs(ofm->idle_timeout);
1172 fm->hard_timeout = ntohs(ofm->hard_timeout);
1173 fm->buffer_id = ntohl(ofm->buffer_id);
1174 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1178 if (ofm->out_group != htonl(OFPG_ANY)) {
1179 return OFPERR_NXFMFC_GROUPS_NOT_SUPPORTED;
1181 fm->flags = ntohs(ofm->flags);
1183 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1184 /* Standard OpenFlow 1.0 flow_mod. */
1185 const struct ofp10_flow_mod *ofm;
1189 /* Get the ofp10_flow_mod. */
1190 ofm = ofpbuf_pull(&b, sizeof *ofm);
1192 /* Set priority based on original wildcards. Normally we'd allow
1193 * ofputil_cls_rule_from_match() to do this for us, but
1194 * ofputil_normalize_rule() can put wildcards where the original
1195 * flow didn't have them. */
1196 priority = ntohs(ofm->priority);
1197 if (!(ofm->match.wildcards & htonl(OFPFW10_ALL))) {
1198 priority = UINT16_MAX;
1201 /* Translate the rule. */
1202 ofputil_cls_rule_from_ofp10_match(&ofm->match, priority, &fm->cr);
1203 ofputil_normalize_rule(&fm->cr);
1205 /* Now get the actions. */
1206 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1211 /* Translate the message. */
1212 command = ntohs(ofm->command);
1213 fm->cookie = htonll(0);
1214 fm->cookie_mask = htonll(0);
1215 fm->new_cookie = ofm->cookie;
1216 fm->idle_timeout = ntohs(ofm->idle_timeout);
1217 fm->hard_timeout = ntohs(ofm->hard_timeout);
1218 fm->buffer_id = ntohl(ofm->buffer_id);
1219 fm->out_port = ntohs(ofm->out_port);
1220 fm->flags = ntohs(ofm->flags);
1221 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1222 /* Nicira extended flow_mod. */
1223 const struct nx_flow_mod *nfm;
1226 /* Dissect the message. */
1227 nfm = ofpbuf_pull(&b, sizeof *nfm);
1228 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1229 &fm->cr, &fm->cookie, &fm->cookie_mask);
1233 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1238 /* Translate the message. */
1239 command = ntohs(nfm->command);
1240 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1241 /* Flow additions may only set a new cookie, not match an
1242 * existing cookie. */
1243 return OFPERR_NXBRC_NXM_INVALID;
1245 fm->new_cookie = nfm->cookie;
1246 fm->idle_timeout = ntohs(nfm->idle_timeout);
1247 fm->hard_timeout = ntohs(nfm->hard_timeout);
1248 fm->buffer_id = ntohl(nfm->buffer_id);
1249 fm->out_port = ntohs(nfm->out_port);
1250 fm->flags = ntohs(nfm->flags);
1255 if (protocol & OFPUTIL_P_TID) {
1256 fm->command = command & 0xff;
1257 fm->table_id = command >> 8;
1259 fm->command = command;
1260 fm->table_id = 0xff;
1264 fm->ofpacts = ofpacts->data;
1265 fm->ofpacts_len = ofpacts->size;
1271 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1272 enum ofputil_protocol protocol)
1274 return htons(protocol & OFPUTIL_P_TID
1275 ? (fm->command & 0xff) | (fm->table_id << 8)
1279 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1280 * 'protocol' and returns the message. */
1282 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1283 enum ofputil_protocol protocol)
1288 case OFPUTIL_P_OF12: {
1289 struct ofp11_flow_mod *ofm;
1291 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1292 NXM_TYPICAL_LEN + fm->ofpacts_len);
1293 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1294 ofm->cookie = fm->new_cookie;
1295 ofm->cookie_mask = fm->cookie_mask;
1296 ofm->table_id = fm->table_id;
1297 ofm->command = fm->command;
1298 ofm->idle_timeout = htons(fm->idle_timeout);
1299 ofm->hard_timeout = htons(fm->hard_timeout);
1300 ofm->priority = htons(fm->cr.priority);
1301 ofm->buffer_id = htonl(fm->buffer_id);
1302 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1303 ofm->out_group = htonl(OFPG11_ANY);
1304 ofm->flags = htons(fm->flags);
1305 oxm_put_match(msg, &fm->cr);
1306 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1310 case OFPUTIL_P_OF10:
1311 case OFPUTIL_P_OF10_TID: {
1312 struct ofp10_flow_mod *ofm;
1314 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1316 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1317 ofputil_cls_rule_to_ofp10_match(&fm->cr, &ofm->match);
1318 ofm->cookie = fm->new_cookie;
1319 ofm->command = ofputil_tid_command(fm, protocol);
1320 ofm->idle_timeout = htons(fm->idle_timeout);
1321 ofm->hard_timeout = htons(fm->hard_timeout);
1322 ofm->priority = htons(fm->cr.priority);
1323 ofm->buffer_id = htonl(fm->buffer_id);
1324 ofm->out_port = htons(fm->out_port);
1325 ofm->flags = htons(fm->flags);
1326 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1331 case OFPUTIL_P_NXM_TID: {
1332 struct nx_flow_mod *nfm;
1335 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1336 NXM_TYPICAL_LEN + fm->ofpacts_len);
1337 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1338 nfm->command = ofputil_tid_command(fm, protocol);
1339 nfm->cookie = fm->new_cookie;
1340 match_len = nx_put_match(msg, &fm->cr, fm->cookie, fm->cookie_mask);
1342 nfm->idle_timeout = htons(fm->idle_timeout);
1343 nfm->hard_timeout = htons(fm->hard_timeout);
1344 nfm->priority = htons(fm->cr.priority);
1345 nfm->buffer_id = htonl(fm->buffer_id);
1346 nfm->out_port = htons(fm->out_port);
1347 nfm->flags = htons(fm->flags);
1348 nfm->match_len = htons(match_len);
1349 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1357 ofpmsg_update_length(msg);
1361 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1362 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1363 * 0-bit for each protocol that is inadequate.
1365 * (The return value will have at least one 1-bit.) */
1366 enum ofputil_protocol
1367 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1370 enum ofputil_protocol usable_protocols;
1373 usable_protocols = OFPUTIL_P_ANY;
1374 for (i = 0; i < n_fms; i++) {
1375 const struct ofputil_flow_mod *fm = &fms[i];
1377 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1378 if (fm->table_id != 0xff) {
1379 usable_protocols &= OFPUTIL_P_TID;
1382 /* Matching of the cookie is only supported through NXM. */
1383 if (fm->cookie_mask != htonll(0)) {
1384 usable_protocols &= OFPUTIL_P_NXM_ANY;
1387 assert(usable_protocols);
1389 return usable_protocols;
1393 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1394 const struct ofp10_flow_stats_request *ofsr,
1397 fsr->aggregate = aggregate;
1398 ofputil_cls_rule_from_ofp10_match(&ofsr->match, 0, &fsr->match);
1399 fsr->out_port = ntohs(ofsr->out_port);
1400 fsr->table_id = ofsr->table_id;
1401 fsr->cookie = fsr->cookie_mask = htonll(0);
1407 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1408 struct ofpbuf *b, bool aggregate)
1410 const struct ofp11_flow_stats_request *ofsr;
1413 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1414 fsr->aggregate = aggregate;
1415 fsr->table_id = ofsr->table_id;
1416 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1420 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1421 return OFPERR_NXFMFC_GROUPS_NOT_SUPPORTED;
1423 fsr->cookie = ofsr->cookie;
1424 fsr->cookie_mask = ofsr->cookie_mask;
1425 error = ofputil_pull_ofp11_match(b, 0, &fsr->match, NULL);
1434 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1435 struct ofpbuf *b, bool aggregate)
1437 const struct nx_flow_stats_request *nfsr;
1440 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1441 error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &fsr->match,
1442 &fsr->cookie, &fsr->cookie_mask);
1447 return OFPERR_OFPBRC_BAD_LEN;
1450 fsr->aggregate = aggregate;
1451 fsr->out_port = ntohs(nfsr->out_port);
1452 fsr->table_id = nfsr->table_id;
1457 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1458 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1459 * successful, otherwise an OpenFlow error code. */
1461 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1462 const struct ofp_header *oh)
1467 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1468 raw = ofpraw_pull_assert(&b);
1469 switch ((int) raw) {
1470 case OFPRAW_OFPST10_FLOW_REQUEST:
1471 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1473 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1474 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1476 case OFPRAW_OFPST11_FLOW_REQUEST:
1477 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1479 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1480 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1482 case OFPRAW_NXST_FLOW_REQUEST:
1483 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1485 case OFPRAW_NXST_AGGREGATE_REQUEST:
1486 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1489 /* Hey, the caller lied. */
1494 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1495 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1496 * 'protocol', and returns the message. */
1498 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1499 enum ofputil_protocol protocol)
1505 case OFPUTIL_P_OF12: {
1506 struct ofp11_flow_stats_request *ofsr;
1508 raw = (fsr->aggregate
1509 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1510 : OFPRAW_OFPST11_FLOW_REQUEST);
1511 msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN);
1512 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1513 ofsr->table_id = fsr->table_id;
1514 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1515 ofsr->out_group = htonl(OFPG11_ANY);
1516 ofsr->cookie = fsr->cookie;
1517 ofsr->cookie_mask = fsr->cookie_mask;
1518 oxm_put_match(msg, &fsr->match);
1522 case OFPUTIL_P_OF10:
1523 case OFPUTIL_P_OF10_TID: {
1524 struct ofp10_flow_stats_request *ofsr;
1526 raw = (fsr->aggregate
1527 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1528 : OFPRAW_OFPST10_FLOW_REQUEST);
1529 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1530 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1531 ofputil_cls_rule_to_ofp10_match(&fsr->match, &ofsr->match);
1532 ofsr->table_id = fsr->table_id;
1533 ofsr->out_port = htons(fsr->out_port);
1538 case OFPUTIL_P_NXM_TID: {
1539 struct nx_flow_stats_request *nfsr;
1542 raw = (fsr->aggregate
1543 ? OFPRAW_NXST_AGGREGATE_REQUEST
1544 : OFPRAW_NXST_FLOW_REQUEST);
1545 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1546 ofpbuf_put_zeros(msg, sizeof *nfsr);
1547 match_len = nx_put_match(msg, &fsr->match,
1548 fsr->cookie, fsr->cookie_mask);
1551 nfsr->out_port = htons(fsr->out_port);
1552 nfsr->match_len = htons(match_len);
1553 nfsr->table_id = fsr->table_id;
1564 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1565 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1567 * (The return value will have at least one 1-bit.) */
1568 enum ofputil_protocol
1569 ofputil_flow_stats_request_usable_protocols(
1570 const struct ofputil_flow_stats_request *fsr)
1572 enum ofputil_protocol usable_protocols;
1574 usable_protocols = ofputil_usable_protocols(&fsr->match);
1575 if (fsr->cookie_mask != htonll(0)) {
1576 usable_protocols &= OFPUTIL_P_NXM_ANY;
1578 return usable_protocols;
1581 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1582 * ofputil_flow_stats in 'fs'.
1584 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1585 * OpenFlow message. Calling this function multiple times for a single 'msg'
1586 * iterates through the replies. The caller must initially leave 'msg''s layer
1587 * pointers null and not modify them between calls.
1589 * Most switches don't send the values needed to populate fs->idle_age and
1590 * fs->hard_age, so those members will usually be set to 0. If the switch from
1591 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1592 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1593 * 'idle_age' and 'hard_age' members in 'fs'.
1595 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1596 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1597 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1599 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1600 * otherwise a positive errno value. */
1602 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1604 bool flow_age_extension,
1605 struct ofpbuf *ofpacts)
1611 ? ofpraw_decode(&raw, msg->l2)
1612 : ofpraw_pull(&raw, msg));
1619 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1620 const struct ofp11_flow_stats *ofs;
1622 uint16_t padded_match_len;
1624 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1626 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1627 "bytes at end", msg->size);
1631 length = ntohs(ofs->length);
1632 if (length < sizeof *ofs) {
1633 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1634 "length %zu", length);
1638 if (ofputil_pull_ofp11_match(msg, ntohs(ofs->priority), &fs->rule,
1639 &padded_match_len)) {
1640 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1644 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1645 padded_match_len, ofpacts)) {
1646 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1650 fs->table_id = ofs->table_id;
1651 fs->duration_sec = ntohl(ofs->duration_sec);
1652 fs->duration_nsec = ntohl(ofs->duration_nsec);
1653 fs->idle_timeout = ntohs(ofs->idle_timeout);
1654 fs->hard_timeout = ntohs(ofs->hard_timeout);
1657 fs->cookie = ofs->cookie;
1658 fs->packet_count = ntohll(ofs->packet_count);
1659 fs->byte_count = ntohll(ofs->byte_count);
1660 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1661 const struct ofp10_flow_stats *ofs;
1664 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1666 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1667 "bytes at end", msg->size);
1671 length = ntohs(ofs->length);
1672 if (length < sizeof *ofs) {
1673 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1674 "length %zu", length);
1678 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1682 fs->cookie = get_32aligned_be64(&ofs->cookie);
1683 ofputil_cls_rule_from_ofp10_match(&ofs->match, ntohs(ofs->priority),
1685 fs->table_id = ofs->table_id;
1686 fs->duration_sec = ntohl(ofs->duration_sec);
1687 fs->duration_nsec = ntohl(ofs->duration_nsec);
1688 fs->idle_timeout = ntohs(ofs->idle_timeout);
1689 fs->hard_timeout = ntohs(ofs->hard_timeout);
1692 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1693 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1694 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1695 const struct nx_flow_stats *nfs;
1696 size_t match_len, actions_len, length;
1698 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1700 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1701 "bytes at end", msg->size);
1705 length = ntohs(nfs->length);
1706 match_len = ntohs(nfs->match_len);
1707 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1708 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1709 "claims invalid length %zu", match_len, length);
1712 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1717 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1718 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1722 fs->cookie = nfs->cookie;
1723 fs->table_id = nfs->table_id;
1724 fs->duration_sec = ntohl(nfs->duration_sec);
1725 fs->duration_nsec = ntohl(nfs->duration_nsec);
1726 fs->idle_timeout = ntohs(nfs->idle_timeout);
1727 fs->hard_timeout = ntohs(nfs->hard_timeout);
1730 if (flow_age_extension) {
1731 if (nfs->idle_age) {
1732 fs->idle_age = ntohs(nfs->idle_age) - 1;
1734 if (nfs->hard_age) {
1735 fs->hard_age = ntohs(nfs->hard_age) - 1;
1738 fs->packet_count = ntohll(nfs->packet_count);
1739 fs->byte_count = ntohll(nfs->byte_count);
1744 fs->ofpacts = ofpacts->data;
1745 fs->ofpacts_len = ofpacts->size;
1750 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1752 * We use this in situations where OVS internally uses UINT64_MAX to mean
1753 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1755 unknown_to_zero(uint64_t count)
1757 return count != UINT64_MAX ? count : 0;
1760 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1761 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1762 * have been initialized with ofputil_start_stats_reply(). */
1764 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1765 struct list *replies)
1767 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1768 size_t start_ofs = reply->size;
1771 ofpraw_decode_partial(&raw, reply->data, reply->size);
1772 if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1773 struct ofp11_flow_stats *ofs;
1775 ofpbuf_put_uninit(reply, sizeof *ofs);
1776 oxm_put_match(reply, &fs->rule);
1777 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
1780 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1781 ofs->length = htons(reply->size - start_ofs);
1782 ofs->table_id = fs->table_id;
1784 ofs->duration_sec = htonl(fs->duration_sec);
1785 ofs->duration_nsec = htonl(fs->duration_nsec);
1786 ofs->priority = htons(fs->rule.priority);
1787 ofs->idle_timeout = htons(fs->idle_timeout);
1788 ofs->hard_timeout = htons(fs->hard_timeout);
1789 memset(ofs->pad2, 0, sizeof ofs->pad2);
1790 ofs->cookie = fs->cookie;
1791 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
1792 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
1793 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1794 struct ofp10_flow_stats *ofs;
1796 ofpbuf_put_uninit(reply, sizeof *ofs);
1797 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1799 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1800 ofs->length = htons(reply->size - start_ofs);
1801 ofs->table_id = fs->table_id;
1803 ofputil_cls_rule_to_ofp10_match(&fs->rule, &ofs->match);
1804 ofs->duration_sec = htonl(fs->duration_sec);
1805 ofs->duration_nsec = htonl(fs->duration_nsec);
1806 ofs->priority = htons(fs->rule.priority);
1807 ofs->idle_timeout = htons(fs->idle_timeout);
1808 ofs->hard_timeout = htons(fs->hard_timeout);
1809 memset(ofs->pad2, 0, sizeof ofs->pad2);
1810 put_32aligned_be64(&ofs->cookie, fs->cookie);
1811 put_32aligned_be64(&ofs->packet_count,
1812 htonll(unknown_to_zero(fs->packet_count)));
1813 put_32aligned_be64(&ofs->byte_count,
1814 htonll(unknown_to_zero(fs->byte_count)));
1815 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1816 struct nx_flow_stats *nfs;
1819 ofpbuf_put_uninit(reply, sizeof *nfs);
1820 match_len = nx_put_match(reply, &fs->rule, 0, 0);
1821 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1823 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1824 nfs->length = htons(reply->size - start_ofs);
1825 nfs->table_id = fs->table_id;
1827 nfs->duration_sec = htonl(fs->duration_sec);
1828 nfs->duration_nsec = htonl(fs->duration_nsec);
1829 nfs->priority = htons(fs->rule.priority);
1830 nfs->idle_timeout = htons(fs->idle_timeout);
1831 nfs->hard_timeout = htons(fs->hard_timeout);
1832 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1833 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1835 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1836 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1838 nfs->match_len = htons(match_len);
1839 nfs->cookie = fs->cookie;
1840 nfs->packet_count = htonll(fs->packet_count);
1841 nfs->byte_count = htonll(fs->byte_count);
1846 ofpmp_postappend(replies, start_ofs);
1849 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1850 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1852 ofputil_encode_aggregate_stats_reply(
1853 const struct ofputil_aggregate_stats *stats,
1854 const struct ofp_header *request)
1856 struct ofp_aggregate_stats_reply *asr;
1857 uint64_t packet_count;
1858 uint64_t byte_count;
1862 ofpraw_decode(&raw, request);
1863 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
1864 packet_count = unknown_to_zero(stats->packet_count);
1865 byte_count = unknown_to_zero(stats->byte_count);
1867 packet_count = stats->packet_count;
1868 byte_count = stats->byte_count;
1871 msg = ofpraw_alloc_stats_reply(request, 0);
1872 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1873 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1874 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1875 asr->flow_count = htonl(stats->flow_count);
1881 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1882 const struct ofp_header *reply)
1884 struct ofp_aggregate_stats_reply *asr;
1887 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
1888 ofpraw_pull_assert(&msg);
1891 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1892 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1893 stats->flow_count = ntohl(asr->flow_count);
1898 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1899 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1900 * an OpenFlow error code. */
1902 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1903 const struct ofp_header *oh)
1908 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1909 raw = ofpraw_pull_assert(&b);
1910 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
1911 const struct ofp12_flow_removed *ofr;
1914 ofr = ofpbuf_pull(&b, sizeof *ofr);
1916 error = ofputil_pull_ofp11_match(&b, ntohs(ofr->priority),
1922 fr->cookie = ofr->cookie;
1923 fr->reason = ofr->reason;
1924 /* XXX: ofr->table_id is ignored */
1925 fr->duration_sec = ntohl(ofr->duration_sec);
1926 fr->duration_nsec = ntohl(ofr->duration_nsec);
1927 fr->idle_timeout = ntohs(ofr->idle_timeout);
1928 fr->hard_timeout = ntohs(ofr->hard_timeout);
1929 fr->packet_count = ntohll(ofr->packet_count);
1930 fr->byte_count = ntohll(ofr->byte_count);
1931 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
1932 const struct ofp_flow_removed *ofr;
1934 ofr = ofpbuf_pull(&b, sizeof *ofr);
1936 ofputil_cls_rule_from_ofp10_match(&ofr->match, ntohs(ofr->priority),
1938 fr->cookie = ofr->cookie;
1939 fr->reason = ofr->reason;
1940 fr->duration_sec = ntohl(ofr->duration_sec);
1941 fr->duration_nsec = ntohl(ofr->duration_nsec);
1942 fr->idle_timeout = ntohs(ofr->idle_timeout);
1943 fr->hard_timeout = 0;
1944 fr->packet_count = ntohll(ofr->packet_count);
1945 fr->byte_count = ntohll(ofr->byte_count);
1946 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
1947 struct nx_flow_removed *nfr;
1950 nfr = ofpbuf_pull(&b, sizeof *nfr);
1951 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1952 &fr->rule, NULL, NULL);
1957 return OFPERR_OFPBRC_BAD_LEN;
1960 fr->cookie = nfr->cookie;
1961 fr->reason = nfr->reason;
1962 fr->duration_sec = ntohl(nfr->duration_sec);
1963 fr->duration_nsec = ntohl(nfr->duration_nsec);
1964 fr->idle_timeout = ntohs(nfr->idle_timeout);
1965 fr->hard_timeout = 0;
1966 fr->packet_count = ntohll(nfr->packet_count);
1967 fr->byte_count = ntohll(nfr->byte_count);
1975 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1976 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1979 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1980 enum ofputil_protocol protocol)
1985 case OFPUTIL_P_OF12: {
1986 struct ofp12_flow_removed *ofr;
1988 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
1989 ofputil_protocol_to_ofp_version(protocol),
1990 htonl(0), NXM_TYPICAL_LEN);
1991 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
1992 ofr->cookie = fr->cookie;
1993 ofr->priority = htons(fr->rule.priority);
1994 ofr->reason = fr->reason;
1996 ofr->duration_sec = htonl(fr->duration_sec);
1997 ofr->duration_nsec = htonl(fr->duration_nsec);
1998 ofr->idle_timeout = htons(fr->idle_timeout);
1999 ofr->hard_timeout = htons(fr->hard_timeout);
2000 ofr->packet_count = htonll(fr->packet_count);
2001 ofr->byte_count = htonll(fr->byte_count);
2002 oxm_put_match(msg, &fr->rule);
2006 case OFPUTIL_P_OF10:
2007 case OFPUTIL_P_OF10_TID: {
2008 struct ofp_flow_removed *ofr;
2010 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2012 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2013 ofputil_cls_rule_to_ofp10_match(&fr->rule, &ofr->match);
2014 ofr->cookie = fr->cookie;
2015 ofr->priority = htons(fr->rule.priority);
2016 ofr->reason = fr->reason;
2017 ofr->duration_sec = htonl(fr->duration_sec);
2018 ofr->duration_nsec = htonl(fr->duration_nsec);
2019 ofr->idle_timeout = htons(fr->idle_timeout);
2020 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2021 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2026 case OFPUTIL_P_NXM_TID: {
2027 struct nx_flow_removed *nfr;
2030 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2031 htonl(0), NXM_TYPICAL_LEN);
2032 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2033 match_len = nx_put_match(msg, &fr->rule, 0, 0);
2036 nfr->cookie = fr->cookie;
2037 nfr->priority = htons(fr->rule.priority);
2038 nfr->reason = fr->reason;
2039 nfr->duration_sec = htonl(fr->duration_sec);
2040 nfr->duration_nsec = htonl(fr->duration_nsec);
2041 nfr->idle_timeout = htons(fr->idle_timeout);
2042 nfr->match_len = htons(match_len);
2043 nfr->packet_count = htonll(fr->packet_count);
2044 nfr->byte_count = htonll(fr->byte_count);
2056 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2057 struct cls_rule *rule,
2060 pin->packet = b->data;
2061 pin->packet_len = b->size;
2063 pin->fmd.in_port = rule->flow.in_port;
2064 pin->fmd.tun_id = rule->flow.tun_id;
2065 pin->fmd.metadata = rule->flow.metadata;
2066 memcpy(pin->fmd.regs, rule->flow.regs, sizeof pin->fmd.regs);
2070 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2071 const struct ofp_header *oh)
2076 memset(pin, 0, sizeof *pin);
2078 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2079 raw = ofpraw_pull_assert(&b);
2080 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2081 const struct ofp12_packet_in *opi;
2082 struct cls_rule rule;
2085 opi = ofpbuf_pull(&b, sizeof *opi);
2086 error = oxm_pull_match_loose(&b, 0, &rule);
2091 if (!ofpbuf_try_pull(&b, 2)) {
2092 return OFPERR_OFPBRC_BAD_LEN;
2095 pin->reason = opi->reason;
2096 pin->table_id = opi->table_id;
2098 pin->buffer_id = ntohl(opi->buffer_id);
2099 pin->total_len = ntohs(opi->total_len);
2101 ofputil_decode_packet_in_finish(pin, &rule, &b);
2102 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2103 const struct ofp_packet_in *opi;
2105 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2107 pin->packet = opi->data;
2108 pin->packet_len = b.size;
2110 pin->fmd.in_port = ntohs(opi->in_port);
2111 pin->reason = opi->reason;
2112 pin->buffer_id = ntohl(opi->buffer_id);
2113 pin->total_len = ntohs(opi->total_len);
2114 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2115 const struct nx_packet_in *npi;
2116 struct cls_rule rule;
2119 npi = ofpbuf_pull(&b, sizeof *npi);
2120 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
2126 if (!ofpbuf_try_pull(&b, 2)) {
2127 return OFPERR_OFPBRC_BAD_LEN;
2130 pin->reason = npi->reason;
2131 pin->table_id = npi->table_id;
2132 pin->cookie = npi->cookie;
2134 pin->buffer_id = ntohl(npi->buffer_id);
2135 pin->total_len = ntohs(npi->total_len);
2137 ofputil_decode_packet_in_finish(pin, &rule, &b);
2146 ofputil_packet_in_to_rule(const struct ofputil_packet_in *pin,
2147 struct cls_rule *rule)
2151 cls_rule_init_catchall(rule, 0);
2152 if (pin->fmd.tun_id != htonll(0)) {
2153 cls_rule_set_tun_id(rule, pin->fmd.tun_id);
2155 if (pin->fmd.metadata != htonll(0)) {
2156 cls_rule_set_metadata(rule, pin->fmd.metadata);
2159 for (i = 0; i < FLOW_N_REGS; i++) {
2160 if (pin->fmd.regs[i]) {
2161 cls_rule_set_reg(rule, i, pin->fmd.regs[i]);
2165 cls_rule_set_in_port(rule, pin->fmd.in_port);
2168 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2169 * in the format specified by 'packet_in_format'. */
2171 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2172 enum ofputil_protocol protocol,
2173 enum nx_packet_in_format packet_in_format)
2175 size_t send_len = MIN(pin->send_len, pin->packet_len);
2176 struct ofpbuf *packet;
2178 /* Add OFPT_PACKET_IN. */
2179 if (protocol == OFPUTIL_P_OF12) {
2180 struct ofp12_packet_in *opi;
2181 struct cls_rule rule;
2183 ofputil_packet_in_to_rule(pin, &rule);
2185 /* The final argument is just an estimate of the space required. */
2186 packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION,
2187 htonl(0), (sizeof(struct flow_metadata) * 2
2189 ofpbuf_put_zeros(packet, sizeof *opi);
2190 oxm_put_match(packet, &rule);
2191 ofpbuf_put_zeros(packet, 2);
2192 ofpbuf_put(packet, pin->packet, send_len);
2195 opi->buffer_id = htonl(pin->buffer_id);
2196 opi->total_len = htons(pin->total_len);
2197 opi->reason = pin->reason;
2198 opi->table_id = pin->table_id;
2199 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2200 struct ofp_packet_in *opi;
2202 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2203 htonl(0), send_len);
2204 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2205 opi->total_len = htons(pin->total_len);
2206 opi->in_port = htons(pin->fmd.in_port);
2207 opi->reason = pin->reason;
2208 opi->buffer_id = htonl(pin->buffer_id);
2210 ofpbuf_put(packet, pin->packet, send_len);
2211 } else if (packet_in_format == NXPIF_NXM) {
2212 struct nx_packet_in *npi;
2213 struct cls_rule rule;
2216 ofputil_packet_in_to_rule(pin, &rule);
2218 /* The final argument is just an estimate of the space required. */
2219 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2220 htonl(0), (sizeof(struct flow_metadata) * 2
2222 ofpbuf_put_zeros(packet, sizeof *npi);
2223 match_len = nx_put_match(packet, &rule, 0, 0);
2224 ofpbuf_put_zeros(packet, 2);
2225 ofpbuf_put(packet, pin->packet, send_len);
2228 npi->buffer_id = htonl(pin->buffer_id);
2229 npi->total_len = htons(pin->total_len);
2230 npi->reason = pin->reason;
2231 npi->table_id = pin->table_id;
2232 npi->cookie = pin->cookie;
2233 npi->match_len = htons(match_len);
2237 ofpmsg_update_length(packet);
2243 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2245 static char s[INT_STRLEN(int) + 1];
2252 case OFPR_INVALID_TTL:
2253 return "invalid_ttl";
2255 case OFPR_N_REASONS:
2257 sprintf(s, "%d", (int) reason);
2263 ofputil_packet_in_reason_from_string(const char *s,
2264 enum ofp_packet_in_reason *reason)
2268 for (i = 0; i < OFPR_N_REASONS; i++) {
2269 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2277 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2280 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2281 * message's actions. The caller must initialize 'ofpacts' and retains
2282 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2284 * Returns 0 if successful, otherwise an OFPERR_* value. */
2286 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2287 const struct ofp_header *oh,
2288 struct ofpbuf *ofpacts)
2290 enum ofperr bad_in_port_err;
2294 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2295 raw = ofpraw_pull_assert(&b);
2297 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2299 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2301 po->buffer_id = ntohl(opo->buffer_id);
2302 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2307 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2313 bad_in_port_err = OFPERR_OFPBMC_BAD_VALUE;
2314 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2316 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2318 po->buffer_id = ntohl(opo->buffer_id);
2319 po->in_port = ntohs(opo->in_port);
2321 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2326 bad_in_port_err = OFPERR_NXBRC_BAD_IN_PORT;
2331 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2332 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2333 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2335 return bad_in_port_err;
2338 po->ofpacts = ofpacts->data;
2339 po->ofpacts_len = ofpacts->size;
2341 if (po->buffer_id == UINT32_MAX) {
2342 po->packet = b.data;
2343 po->packet_len = b.size;
2352 /* ofputil_phy_port */
2354 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2355 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2356 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2357 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2358 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2359 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2360 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2361 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2363 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2364 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2365 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2366 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2367 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2368 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2370 static enum netdev_features
2371 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2373 uint32_t ofp10 = ntohl(ofp10_);
2374 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2378 netdev_port_features_to_ofp10(enum netdev_features features)
2380 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2383 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2384 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2385 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2386 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2387 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2388 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2389 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2390 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2391 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2392 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2393 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2394 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2395 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2396 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2397 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2398 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2400 static enum netdev_features
2401 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2403 return ntohl(ofp11) & 0xffff;
2407 netdev_port_features_to_ofp11(enum netdev_features features)
2409 return htonl(features & 0xffff);
2413 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2414 const struct ofp10_phy_port *opp)
2416 memset(pp, 0, sizeof *pp);
2418 pp->port_no = ntohs(opp->port_no);
2419 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2420 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2422 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2423 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2425 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2426 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2427 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2428 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2430 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2431 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2437 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2438 const struct ofp11_port *op)
2442 memset(pp, 0, sizeof *pp);
2444 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2448 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2449 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2451 pp->config = ntohl(op->config) & OFPPC11_ALL;
2452 pp->state = ntohl(op->state) & OFPPC11_ALL;
2454 pp->curr = netdev_port_features_from_ofp11(op->curr);
2455 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2456 pp->supported = netdev_port_features_from_ofp11(op->supported);
2457 pp->peer = netdev_port_features_from_ofp11(op->peer);
2459 pp->curr_speed = ntohl(op->curr_speed);
2460 pp->max_speed = ntohl(op->max_speed);
2466 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2468 switch (ofp_version) {
2470 return sizeof(struct ofp10_phy_port);
2473 return sizeof(struct ofp11_port);
2480 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2481 struct ofp10_phy_port *opp)
2483 memset(opp, 0, sizeof *opp);
2485 opp->port_no = htons(pp->port_no);
2486 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2487 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2489 opp->config = htonl(pp->config & OFPPC10_ALL);
2490 opp->state = htonl(pp->state & OFPPS10_ALL);
2492 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2493 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2494 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2495 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2499 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2500 struct ofp11_port *op)
2502 memset(op, 0, sizeof *op);
2504 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2505 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2506 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2508 op->config = htonl(pp->config & OFPPC11_ALL);
2509 op->state = htonl(pp->state & OFPPS11_ALL);
2511 op->curr = netdev_port_features_to_ofp11(pp->curr);
2512 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2513 op->supported = netdev_port_features_to_ofp11(pp->supported);
2514 op->peer = netdev_port_features_to_ofp11(pp->peer);
2516 op->curr_speed = htonl(pp->curr_speed);
2517 op->max_speed = htonl(pp->max_speed);
2521 ofputil_put_phy_port(enum ofp_version ofp_version,
2522 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2524 switch (ofp_version) {
2525 case OFP10_VERSION: {
2526 struct ofp10_phy_port *opp;
2527 if (b->size + sizeof *opp <= UINT16_MAX) {
2528 opp = ofpbuf_put_uninit(b, sizeof *opp);
2529 ofputil_encode_ofp10_phy_port(pp, opp);
2535 case OFP12_VERSION: {
2536 struct ofp11_port *op;
2537 if (b->size + sizeof *op <= UINT16_MAX) {
2538 op = ofpbuf_put_uninit(b, sizeof *op);
2539 ofputil_encode_ofp11_port(pp, op);
2550 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2551 const struct ofputil_phy_port *pp,
2552 struct list *replies)
2554 switch (ofp_version) {
2555 case OFP10_VERSION: {
2556 struct ofp10_phy_port *opp;
2558 opp = ofpmp_append(replies, sizeof *opp);
2559 ofputil_encode_ofp10_phy_port(pp, opp);
2564 case OFP12_VERSION: {
2565 struct ofp11_port *op;
2567 op = ofpmp_append(replies, sizeof *op);
2568 ofputil_encode_ofp11_port(pp, op);
2577 /* ofputil_switch_features */
2579 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2580 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2581 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2582 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2583 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2584 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2585 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2586 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2588 struct ofputil_action_bit_translation {
2589 enum ofputil_action_bitmap ofputil_bit;
2593 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2594 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2595 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2596 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2597 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2598 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2599 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2600 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2601 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2602 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2603 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2604 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2605 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2609 static enum ofputil_action_bitmap
2610 decode_action_bits(ovs_be32 of_actions,
2611 const struct ofputil_action_bit_translation *x)
2613 enum ofputil_action_bitmap ofputil_actions;
2615 ofputil_actions = 0;
2616 for (; x->ofputil_bit; x++) {
2617 if (of_actions & htonl(1u << x->of_bit)) {
2618 ofputil_actions |= x->ofputil_bit;
2621 return ofputil_actions;
2625 ofputil_capabilities_mask(enum ofp_version ofp_version)
2627 /* Handle capabilities whose bit is unique for all Open Flow versions */
2628 switch (ofp_version) {
2631 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2633 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2635 /* Caller needs to check osf->header.version itself */
2640 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2641 * abstract representation in '*features'. Initializes '*b' to iterate over
2642 * the OpenFlow port structures following 'osf' with later calls to
2643 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2644 * OFPERR_* value. */
2646 ofputil_decode_switch_features(const struct ofp_header *oh,
2647 struct ofputil_switch_features *features,
2650 const struct ofp_switch_features *osf;
2653 ofpbuf_use_const(b, oh, ntohs(oh->length));
2654 raw = ofpraw_pull_assert(b);
2656 osf = ofpbuf_pull(b, sizeof *osf);
2657 features->datapath_id = ntohll(osf->datapath_id);
2658 features->n_buffers = ntohl(osf->n_buffers);
2659 features->n_tables = osf->n_tables;
2661 features->capabilities = ntohl(osf->capabilities) &
2662 ofputil_capabilities_mask(oh->version);
2664 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2665 return OFPERR_OFPBRC_BAD_LEN;
2668 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2669 if (osf->capabilities & htonl(OFPC10_STP)) {
2670 features->capabilities |= OFPUTIL_C_STP;
2672 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2673 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2674 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2675 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2677 features->actions = 0;
2679 return OFPERR_OFPBRC_BAD_VERSION;
2685 /* Returns true if the maximum number of ports are in 'oh'. */
2687 max_ports_in_features(const struct ofp_header *oh)
2689 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2690 return ntohs(oh->length) + pp_size > UINT16_MAX;
2693 /* Given a buffer 'b' that contains a Features Reply message, checks if
2694 * it contains the maximum number of ports that will fit. If so, it
2695 * returns true and removes the ports from the message. The caller
2696 * should then send an OFPST_PORT_DESC stats request to get the ports,
2697 * since the switch may have more ports than could be represented in the
2698 * Features Reply. Otherwise, returns false.
2701 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2703 struct ofp_header *oh = b->data;
2705 if (max_ports_in_features(oh)) {
2706 /* Remove all the ports. */
2707 b->size = (sizeof(struct ofp_header)
2708 + sizeof(struct ofp_switch_features));
2709 ofpmsg_update_length(b);
2718 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2719 const struct ofputil_action_bit_translation *x)
2721 uint32_t of_actions;
2724 for (; x->ofputil_bit; x++) {
2725 if (ofputil_actions & x->ofputil_bit) {
2726 of_actions |= 1 << x->of_bit;
2729 return htonl(of_actions);
2732 /* Returns a buffer owned by the caller that encodes 'features' in the format
2733 * required by 'protocol' with the given 'xid'. The caller should append port
2734 * information to the buffer with subsequent calls to
2735 * ofputil_put_switch_features_port(). */
2737 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2738 enum ofputil_protocol protocol, ovs_be32 xid)
2740 struct ofp_switch_features *osf;
2742 enum ofp_version version;
2745 version = ofputil_protocol_to_ofp_version(protocol);
2748 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2752 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2757 b = ofpraw_alloc_xid(raw, version, xid, 0);
2758 osf = ofpbuf_put_zeros(b, sizeof *osf);
2759 osf->datapath_id = htonll(features->datapath_id);
2760 osf->n_buffers = htonl(features->n_buffers);
2761 osf->n_tables = features->n_tables;
2763 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2764 osf->capabilities = htonl(features->capabilities &
2765 ofputil_capabilities_mask(version));
2768 if (features->capabilities & OFPUTIL_C_STP) {
2769 osf->capabilities |= htonl(OFPC10_STP);
2771 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2775 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2776 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2786 /* Encodes 'pp' into the format required by the switch_features message already
2787 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2788 * and appends the encoded version to 'b'. */
2790 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2793 const struct ofp_header *oh = b->data;
2795 ofputil_put_phy_port(oh->version, pp, b);
2798 /* ofputil_port_status */
2800 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2801 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2803 ofputil_decode_port_status(const struct ofp_header *oh,
2804 struct ofputil_port_status *ps)
2806 const struct ofp_port_status *ops;
2810 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2811 ofpraw_pull_assert(&b);
2812 ops = ofpbuf_pull(&b, sizeof *ops);
2814 if (ops->reason != OFPPR_ADD &&
2815 ops->reason != OFPPR_DELETE &&
2816 ops->reason != OFPPR_MODIFY) {
2817 return OFPERR_NXBRC_BAD_REASON;
2819 ps->reason = ops->reason;
2821 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
2822 assert(retval != EOF);
2826 /* Converts the abstract form of a "port status" message in '*ps' into an
2827 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2828 * a buffer owned by the caller. */
2830 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2831 enum ofputil_protocol protocol)
2833 struct ofp_port_status *ops;
2835 enum ofp_version version;
2838 version = ofputil_protocol_to_ofp_version(protocol);
2841 raw = OFPRAW_OFPT10_PORT_STATUS;
2846 raw = OFPRAW_OFPT11_PORT_STATUS;
2853 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2854 ops = ofpbuf_put_zeros(b, sizeof *ops);
2855 ops->reason = ps->reason;
2856 ofputil_put_phy_port(version, &ps->desc, b);
2857 ofpmsg_update_length(b);
2861 /* ofputil_port_mod */
2863 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2864 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2866 ofputil_decode_port_mod(const struct ofp_header *oh,
2867 struct ofputil_port_mod *pm)
2872 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2873 raw = ofpraw_pull_assert(&b);
2875 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2876 const struct ofp10_port_mod *opm = b.data;
2878 pm->port_no = ntohs(opm->port_no);
2879 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2880 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2881 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2882 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2883 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2884 const struct ofp11_port_mod *opm = b.data;
2887 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2892 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2893 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2894 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2895 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2897 return OFPERR_OFPBRC_BAD_TYPE;
2900 pm->config &= pm->mask;
2904 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2905 * message suitable for 'protocol', and returns that encoded form in a buffer
2906 * owned by the caller. */
2908 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2909 enum ofputil_protocol protocol)
2911 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
2914 switch (ofp_version) {
2915 case OFP10_VERSION: {
2916 struct ofp10_port_mod *opm;
2918 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2919 opm = ofpbuf_put_zeros(b, sizeof *opm);
2920 opm->port_no = htons(pm->port_no);
2921 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2922 opm->config = htonl(pm->config & OFPPC10_ALL);
2923 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2924 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2929 case OFP12_VERSION: {
2930 struct ofp11_port_mod *opm;
2932 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2933 opm = ofpbuf_put_zeros(b, sizeof *opm);
2934 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
2935 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2936 opm->config = htonl(pm->config & OFPPC11_ALL);
2937 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2938 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2949 /* ofputil_flow_monitor_request */
2951 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
2952 * ofputil_flow_monitor_request in 'rq'.
2954 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
2955 * message. Calling this function multiple times for a single 'msg' iterates
2956 * through the requests. The caller must initially leave 'msg''s layer
2957 * pointers null and not modify them between calls.
2959 * Returns 0 if successful, EOF if no requests were left in this 'msg',
2960 * otherwise an OFPERR_* value. */
2962 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
2965 struct nx_flow_monitor_request *nfmr;
2969 msg->l2 = msg->data;
2970 ofpraw_pull_assert(msg);
2977 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
2979 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
2980 "leftover bytes at end", msg->size);
2981 return OFPERR_OFPBRC_BAD_LEN;
2984 flags = ntohs(nfmr->flags);
2985 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
2986 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
2987 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
2988 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
2990 return OFPERR_NXBRC_FM_BAD_FLAGS;
2993 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
2994 return OFPERR_NXBRC_MUST_BE_ZERO;
2997 rq->id = ntohl(nfmr->id);
2999 rq->out_port = ntohs(nfmr->out_port);
3000 rq->table_id = nfmr->table_id;
3002 return nx_pull_match(msg, ntohs(nfmr->match_len), OFP_DEFAULT_PRIORITY,
3003 &rq->match, NULL, NULL);
3007 ofputil_append_flow_monitor_request(
3008 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3010 struct nx_flow_monitor_request *nfmr;
3015 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3018 start_ofs = msg->size;
3019 ofpbuf_put_zeros(msg, sizeof *nfmr);
3020 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3022 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3023 nfmr->id = htonl(rq->id);
3024 nfmr->flags = htons(rq->flags);
3025 nfmr->out_port = htons(rq->out_port);
3026 nfmr->match_len = htons(match_len);
3027 nfmr->table_id = rq->table_id;
3030 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3031 * into an abstract ofputil_flow_update in 'update'. The caller must have
3032 * initialized update->match to point to space allocated for a cls_rule.
3034 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3035 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3036 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3037 * will point into the 'ofpacts' buffer.
3039 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3040 * this function multiple times for a single 'msg' iterates through the
3041 * updates. The caller must initially leave 'msg''s layer pointers null and
3042 * not modify them between calls.
3044 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3045 * otherwise an OFPERR_* value. */
3047 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3048 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3050 struct nx_flow_update_header *nfuh;
3051 unsigned int length;
3054 msg->l2 = msg->data;
3055 ofpraw_pull_assert(msg);
3062 if (msg->size < sizeof(struct nx_flow_update_header)) {
3067 update->event = ntohs(nfuh->event);
3068 length = ntohs(nfuh->length);
3069 if (length > msg->size || length % 8) {
3073 if (update->event == NXFME_ABBREV) {
3074 struct nx_flow_update_abbrev *nfua;
3076 if (length != sizeof *nfua) {
3080 nfua = ofpbuf_pull(msg, sizeof *nfua);
3081 update->xid = nfua->xid;
3083 } else if (update->event == NXFME_ADDED
3084 || update->event == NXFME_DELETED
3085 || update->event == NXFME_MODIFIED) {
3086 struct nx_flow_update_full *nfuf;
3087 unsigned int actions_len;
3088 unsigned int match_len;
3091 if (length < sizeof *nfuf) {
3095 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3096 match_len = ntohs(nfuf->match_len);
3097 if (sizeof *nfuf + match_len > length) {
3101 update->reason = ntohs(nfuf->reason);
3102 update->idle_timeout = ntohs(nfuf->idle_timeout);
3103 update->hard_timeout = ntohs(nfuf->hard_timeout);
3104 update->table_id = nfuf->table_id;
3105 update->cookie = nfuf->cookie;
3107 error = nx_pull_match(msg, match_len, ntohs(nfuf->priority),
3108 update->match, NULL, NULL);
3113 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3114 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3119 update->ofpacts = ofpacts->data;
3120 update->ofpacts_len = ofpacts->size;
3123 VLOG_WARN_RL(&bad_ofmsg_rl,
3124 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3125 ntohs(nfuh->event));
3126 return OFPERR_OFPET_BAD_REQUEST;
3130 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3131 "leftover bytes at end", msg->size);
3132 return OFPERR_OFPBRC_BAD_LEN;
3136 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3138 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3140 return ntohl(cancel->id);
3144 ofputil_encode_flow_monitor_cancel(uint32_t id)
3146 struct nx_flow_monitor_cancel *nfmc;
3149 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3150 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3151 nfmc->id = htonl(id);
3156 ofputil_start_flow_update(struct list *replies)
3160 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3164 list_push_back(replies, &msg->list_node);
3168 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3169 struct list *replies)
3171 struct nx_flow_update_header *nfuh;
3175 msg = ofpbuf_from_list(list_back(replies));
3176 start_ofs = msg->size;
3178 if (update->event == NXFME_ABBREV) {
3179 struct nx_flow_update_abbrev *nfua;
3181 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3182 nfua->xid = update->xid;
3184 struct nx_flow_update_full *nfuf;
3187 ofpbuf_put_zeros(msg, sizeof *nfuf);
3188 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3189 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3191 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3192 nfuf->reason = htons(update->reason);
3193 nfuf->priority = htons(update->match->priority);
3194 nfuf->idle_timeout = htons(update->idle_timeout);
3195 nfuf->hard_timeout = htons(update->hard_timeout);
3196 nfuf->match_len = htons(match_len);
3197 nfuf->table_id = update->table_id;
3198 nfuf->cookie = update->cookie;
3201 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3202 nfuh->length = htons(msg->size - start_ofs);
3203 nfuh->event = htons(update->event);
3205 ofpmp_postappend(replies, start_ofs);
3209 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3210 enum ofputil_protocol protocol)
3212 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3216 size = po->ofpacts_len;
3217 if (po->buffer_id == UINT32_MAX) {
3218 size += po->packet_len;
3221 switch (ofp_version) {
3222 case OFP10_VERSION: {
3223 struct ofp_packet_out *opo;
3226 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3227 ofpbuf_put_zeros(msg, sizeof *opo);
3228 actions_ofs = msg->size;
3229 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3232 opo->buffer_id = htonl(po->buffer_id);
3233 opo->in_port = htons(po->in_port);
3234 opo->actions_len = htons(msg->size - actions_ofs);
3239 case OFP12_VERSION: {
3240 struct ofp11_packet_out *opo;
3243 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3244 ofpbuf_put_zeros(msg, sizeof *opo);
3245 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3248 opo->buffer_id = htonl(po->buffer_id);
3249 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3250 opo->actions_len = htons(len);
3258 if (po->buffer_id == UINT32_MAX) {
3259 ofpbuf_put(msg, po->packet, po->packet_len);
3262 ofpmsg_update_length(msg);
3267 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3269 make_echo_request(enum ofp_version ofp_version)
3271 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3275 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3276 * OFPT_ECHO_REQUEST message in 'rq'. */
3278 make_echo_reply(const struct ofp_header *rq)
3280 struct ofpbuf rq_buf;
3281 struct ofpbuf *reply;
3283 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3284 ofpraw_pull_assert(&rq_buf);
3286 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3287 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3292 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3296 switch (ofp_version) {
3299 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3303 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3310 return ofpraw_alloc(type, ofp_version, 0);
3314 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3316 switch (flags & OFPC_FRAG_MASK) {
3317 case OFPC_FRAG_NORMAL: return "normal";
3318 case OFPC_FRAG_DROP: return "drop";
3319 case OFPC_FRAG_REASM: return "reassemble";
3320 case OFPC_FRAG_NX_MATCH: return "nx-match";
3327 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3329 if (!strcasecmp(s, "normal")) {
3330 *flags = OFPC_FRAG_NORMAL;
3331 } else if (!strcasecmp(s, "drop")) {
3332 *flags = OFPC_FRAG_DROP;
3333 } else if (!strcasecmp(s, "reassemble")) {
3334 *flags = OFPC_FRAG_REASM;
3335 } else if (!strcasecmp(s, "nx-match")) {
3336 *flags = OFPC_FRAG_NX_MATCH;
3343 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3344 * port number and stores the latter in '*ofp10_port', for the purpose of
3345 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3346 * otherwise an OFPERR_* number.
3348 * See the definition of OFP11_MAX for an explanation of the mapping. */
3350 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3352 uint32_t ofp11_port_h = ntohl(ofp11_port);
3354 if (ofp11_port_h < OFPP_MAX) {
3355 *ofp10_port = ofp11_port_h;
3357 } else if (ofp11_port_h >= OFPP11_MAX) {
3358 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3361 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3362 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3363 ofp11_port_h, OFPP_MAX - 1,
3364 (uint32_t) OFPP11_MAX, UINT32_MAX);
3365 return OFPERR_OFPBAC_BAD_OUT_PORT;
3369 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3370 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3372 * See the definition of OFP11_MAX for an explanation of the mapping. */
3374 ofputil_port_to_ofp11(uint16_t ofp10_port)
3376 return htonl(ofp10_port < OFPP_MAX
3378 : ofp10_port + OFPP11_OFFSET);
3381 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3382 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3383 * 'port' is valid, otherwise an OpenFlow return code. */
3385 ofputil_check_output_port(uint16_t port, int max_ports)
3393 case OFPP_CONTROLLER:
3399 if (port < max_ports) {
3402 return OFPERR_OFPBAC_BAD_OUT_PORT;
3406 #define OFPUTIL_NAMED_PORTS \
3407 OFPUTIL_NAMED_PORT(IN_PORT) \
3408 OFPUTIL_NAMED_PORT(TABLE) \
3409 OFPUTIL_NAMED_PORT(NORMAL) \
3410 OFPUTIL_NAMED_PORT(FLOOD) \
3411 OFPUTIL_NAMED_PORT(ALL) \
3412 OFPUTIL_NAMED_PORT(CONTROLLER) \
3413 OFPUTIL_NAMED_PORT(LOCAL) \
3414 OFPUTIL_NAMED_PORT(NONE)
3416 /* Checks whether 's' is the string representation of an OpenFlow port number,
3417 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
3418 * number in '*port' and returns true. Otherwise, returns false. */
3420 ofputil_port_from_string(const char *name, uint16_t *port)
3426 static const struct pair pairs[] = {
3427 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3429 #undef OFPUTIL_NAMED_PORT
3431 static const int n_pairs = ARRAY_SIZE(pairs);
3434 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
3439 for (i = 0; i < n_pairs; i++) {
3440 if (!strcasecmp(name, pairs[i].name)) {
3441 *port = pairs[i].value;
3448 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3449 * Most ports' string representation is just the port number, but for special
3450 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3452 ofputil_format_port(uint16_t port, struct ds *s)
3457 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3459 #undef OFPUTIL_NAMED_PORT
3462 ds_put_format(s, "%"PRIu16, port);
3465 ds_put_cstr(s, name);
3468 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3469 * 'ofp_version', tries to pull the first element from the array. If
3470 * successful, initializes '*pp' with an abstract representation of the
3471 * port and returns 0. If no ports remain to be decoded, returns EOF.
3472 * On an error, returns a positive OFPERR_* value. */
3474 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
3475 struct ofputil_phy_port *pp)
3477 switch (ofp_version) {
3478 case OFP10_VERSION: {
3479 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3480 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3483 case OFP12_VERSION: {
3484 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3485 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3492 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3493 * 'ofp_version', returns the number of elements. */
3494 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3496 return b->size / ofputil_get_phy_port_size(ofp_version);
3499 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3500 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3501 * 'name' is not the name of any action.
3503 * ofp-util.def lists the mapping from names to action. */
3505 ofputil_action_code_from_name(const char *name)
3507 static const char *names[OFPUTIL_N_ACTIONS] = {
3509 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3510 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) NAME,
3511 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3512 #include "ofp-util.def"
3517 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3518 if (*p && !strcasecmp(name, *p)) {
3525 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3526 * action. Initializes the parts of 'action' that identify it as having type
3527 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3528 * have variable length, the length used and cleared is that of struct
3531 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3534 case OFPUTIL_ACTION_INVALID:
3537 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3538 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3539 #define OFPAT11_ACTION OFPAT10_ACTION
3540 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3541 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3542 #include "ofp-util.def"
3547 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3549 ofputil_init_##ENUM(struct STRUCT *s) \
3551 memset(s, 0, sizeof *s); \
3552 s->type = htons(ENUM); \
3553 s->len = htons(sizeof *s); \
3557 ofputil_put_##ENUM(struct ofpbuf *buf) \
3559 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3560 ofputil_init_##ENUM(s); \
3563 #define OFPAT11_ACTION OFPAT10_ACTION
3564 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3566 ofputil_init_##ENUM(struct STRUCT *s) \
3568 memset(s, 0, sizeof *s); \
3569 s->type = htons(OFPAT10_VENDOR); \
3570 s->len = htons(sizeof *s); \
3571 s->vendor = htonl(NX_VENDOR_ID); \
3572 s->subtype = htons(ENUM); \
3576 ofputil_put_##ENUM(struct ofpbuf *buf) \
3578 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3579 ofputil_init_##ENUM(s); \
3582 #include "ofp-util.def"
3585 ofputil_normalize_rule__(struct cls_rule *rule, bool may_log)
3588 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3589 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3590 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3591 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3592 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3593 MAY_ARP_THA = 1 << 5, /* arp_tha */
3594 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3595 MAY_ND_TARGET = 1 << 7 /* nd_target */
3598 struct flow_wildcards wc;
3600 /* Figure out what fields may be matched. */
3601 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
3602 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3603 if (rule->flow.nw_proto == IPPROTO_TCP ||
3604 rule->flow.nw_proto == IPPROTO_UDP ||
3605 rule->flow.nw_proto == IPPROTO_ICMP) {
3606 may_match |= MAY_TP_ADDR;
3608 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3609 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3610 if (rule->flow.nw_proto == IPPROTO_TCP ||
3611 rule->flow.nw_proto == IPPROTO_UDP) {
3612 may_match |= MAY_TP_ADDR;
3613 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3614 may_match |= MAY_TP_ADDR;
3615 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3616 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3617 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3618 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3621 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
3622 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3627 /* Clear the fields that may not be matched. */
3629 if (!(may_match & MAY_NW_ADDR)) {
3630 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3632 if (!(may_match & MAY_TP_ADDR)) {
3633 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
3635 if (!(may_match & MAY_NW_PROTO)) {
3636 wc.wildcards |= FWW_NW_PROTO;
3638 if (!(may_match & MAY_IPVx)) {
3639 wc.wildcards |= FWW_NW_DSCP;
3640 wc.wildcards |= FWW_NW_ECN;
3641 wc.wildcards |= FWW_NW_TTL;
3643 if (!(may_match & MAY_ARP_SHA)) {
3644 memset(wc.arp_sha_mask, 0, ETH_ADDR_LEN);
3646 if (!(may_match & MAY_ARP_THA)) {
3647 memset(wc.arp_tha_mask, 0, ETH_ADDR_LEN);
3649 if (!(may_match & MAY_IPV6)) {
3650 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
3651 wc.ipv6_label_mask = htonl(0);
3653 if (!(may_match & MAY_ND_TARGET)) {
3654 wc.nd_target_mask = in6addr_any;
3657 /* Log any changes. */
3658 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3659 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
3660 char *pre = log ? cls_rule_to_string(rule) : NULL;
3663 cls_rule_zero_wildcarded_fields(rule);
3666 char *post = cls_rule_to_string(rule);
3667 VLOG_INFO("normalization changed ofp_match, details:");
3668 VLOG_INFO(" pre: %s", pre);
3669 VLOG_INFO("post: %s", post);
3676 /* "Normalizes" the wildcards in 'rule'. That means:
3678 * 1. If the type of level N is known, then only the valid fields for that
3679 * level may be specified. For example, ARP does not have a TOS field,
3680 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3681 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3682 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3685 * 2. If the type of level N is not known (or not understood by Open
3686 * vSwitch), then no fields at all for that level may be specified. For
3687 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3688 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3691 * If this function changes 'rule', it logs a rate-limited informational
3694 ofputil_normalize_rule(struct cls_rule *rule)
3696 ofputil_normalize_rule__(rule, true);
3699 /* Same as ofputil_normalize_rule() without the logging. Thus, this function
3700 * is suitable for a program's internal use, whereas ofputil_normalize_rule()
3701 * sense for use on flows received from elsewhere (so that a bug in the program
3702 * that sent them can be reported and corrected). */
3704 ofputil_normalize_rule_quiet(struct cls_rule *rule)
3706 ofputil_normalize_rule__(rule, false);
3709 /* Parses a key or a key-value pair from '*stringp'.
3711 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3712 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3713 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3714 * are substrings of '*stringp' created by replacing some of its bytes by null
3715 * terminators. Returns true.
3717 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3718 * NULL and returns false. */
3720 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3722 char *pos, *key, *value;
3726 pos += strspn(pos, ", \t\r\n");
3728 *keyp = *valuep = NULL;
3733 key_len = strcspn(pos, ":=(, \t\r\n");
3734 if (key[key_len] == ':' || key[key_len] == '=') {
3735 /* The value can be separated by a colon. */
3738 value = key + key_len + 1;
3739 value_len = strcspn(value, ", \t\r\n");
3740 pos = value + value_len + (value[value_len] != '\0');
3741 value[value_len] = '\0';
3742 } else if (key[key_len] == '(') {
3743 /* The value can be surrounded by balanced parentheses. The outermost
3744 * set of parentheses is removed. */
3748 value = key + key_len + 1;
3749 for (value_len = 0; level > 0; value_len++) {
3750 switch (value[value_len]) {
3764 value[value_len - 1] = '\0';
3765 pos = value + value_len;
3767 /* There might be no value at all. */
3768 value = key + key_len; /* Will become the empty string below. */
3769 pos = key + key_len + (key[key_len] != '\0');
3771 key[key_len] = '\0';