2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 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.
21 #include <netinet/icmp6.h>
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "ofp-actions.h"
27 #include "ofp-errors.h"
30 #include "openflow/nicira-ext.h"
32 #include "unaligned.h"
36 VLOG_DEFINE_THIS_MODULE(nx_match);
38 /* Rate limit for nx_match parse errors. These always indicate a bug in the
39 * peer and so there's not much point in showing a lot of them. */
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
42 /* Returns the width of the data for a field with the given 'header', in
45 nxm_field_bytes(uint32_t header)
47 unsigned int length = NXM_LENGTH(header);
48 return NXM_HASMASK(header) ? length / 2 : length;
51 /* Returns the width of the data for a field with the given 'header', in
54 nxm_field_bits(uint32_t header)
56 return nxm_field_bytes(header) * 8;
59 /* nx_pull_match() and helpers. */
62 nx_entry_ok(const void *p, unsigned int match_len)
64 unsigned int payload_len;
70 VLOG_DBG_RL(&rl, "nx_match ends with partial (%u-byte) nxm_header",
75 memcpy(&header_be, p, 4);
76 header = ntohl(header_be);
78 payload_len = NXM_LENGTH(header);
80 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
84 if (match_len < payload_len + 4) {
85 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
86 "%u bytes left in nx_match", payload_len + 4, match_len);
93 /* Given NXM/OXM value 'value' and mask 'mask', each 'width' bytes long,
94 * checks for any 1-bit in the value where there is a 0-bit in the mask. If it
95 * finds one, logs a warning. */
97 check_mask_consistency(const uint8_t *p, const struct mf_field *mf)
99 unsigned int width = mf->n_bytes;
100 const uint8_t *value = p + 4;
101 const uint8_t *mask = p + 4 + width;
104 for (i = 0; i < width; i++) {
105 if (value[i] & ~mask[i]) {
106 if (!VLOG_DROP_WARN(&rl)) {
107 char *s = nx_match_to_string(p, width * 2 + 4);
108 VLOG_WARN_RL(&rl, "NXM/OXM entry %s has 1-bits in value for "
109 "bits wildcarded by the mask. (Future versions "
110 "of OVS may report this as an OpenFlow error.)",
119 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
120 struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
124 ovs_assert((cookie != NULL) == (cookie_mask != NULL));
126 match_init_catchall(match);
128 *cookie = *cookie_mask = htonll(0);
135 (header = nx_entry_ok(p, match_len)) != 0;
136 p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
137 const struct mf_field *mf;
140 mf = mf_from_nxm_header(header);
143 error = OFPERR_OFPBMC_BAD_FIELD;
147 } else if (!mf_are_prereqs_ok(mf, &match->flow)) {
148 error = OFPERR_OFPBMC_BAD_PREREQ;
149 } else if (!mf_is_all_wild(mf, &match->wc)) {
150 error = OFPERR_OFPBMC_DUP_FIELD;
152 unsigned int width = mf->n_bytes;
153 union mf_value value;
155 memcpy(&value, p + 4, width);
156 if (!mf_is_value_valid(mf, &value)) {
157 error = OFPERR_OFPBMC_BAD_VALUE;
158 } else if (!NXM_HASMASK(header)) {
160 mf_set_value(mf, &value, match);
164 memcpy(&mask, p + 4 + width, width);
165 if (!mf_is_mask_valid(mf, &mask)) {
166 error = OFPERR_OFPBMC_BAD_MASK;
169 check_mask_consistency(p, mf);
170 mf_set(mf, &value, &mask, match);
175 /* Check if the match is for a cookie rather than a classifier rule. */
176 if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
178 error = OFPERR_OFPBMC_DUP_FIELD;
180 unsigned int width = sizeof *cookie;
182 memcpy(cookie, p + 4, width);
183 if (NXM_HASMASK(header)) {
184 memcpy(cookie_mask, p + 4 + width, width);
186 *cookie_mask = OVS_BE64_MAX;
193 VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
194 "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
196 NXM_VENDOR(header), NXM_FIELD(header),
197 NXM_HASMASK(header), NXM_LENGTH(header),
198 ofperr_to_string(error));
203 return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
207 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
209 ovs_be64 *cookie, ovs_be64 *cookie_mask)
214 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
216 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
217 "multiple of 8, is longer than space in message (max "
218 "length %"PRIu32")", match_len, ofpbuf_size(b));
219 return OFPERR_OFPBMC_BAD_LEN;
223 return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
226 /* Parses the nx_match formatted match description in 'b' with length
227 * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask'
228 * are valid pointers, then stores the cookie and mask in them if 'b' contains
229 * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both.
231 * Fails with an error upon encountering an unknown NXM header.
233 * Returns 0 if successful, otherwise an OpenFlow error code. */
235 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
236 ovs_be64 *cookie, ovs_be64 *cookie_mask)
238 return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
241 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
242 * instead of failing with an error. */
244 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
246 ovs_be64 *cookie, ovs_be64 *cookie_mask)
248 return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
252 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
254 struct ofp11_match_header *omh = ofpbuf_data(b);
258 if (ofpbuf_size(b) < sizeof *omh) {
259 return OFPERR_OFPBMC_BAD_LEN;
262 match_len = ntohs(omh->length);
263 if (match_len < sizeof *omh) {
264 return OFPERR_OFPBMC_BAD_LEN;
267 if (omh->type != htons(OFPMT_OXM)) {
268 return OFPERR_OFPBMC_BAD_TYPE;
271 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
273 VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
274 "multiple of 8, is longer than space in message (max "
275 "length %"PRIu32")", match_len, ofpbuf_size(b));
276 return OFPERR_OFPBMC_BAD_LEN;
279 return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
280 strict, match, NULL, NULL);
283 /* Parses the oxm formatted match description preceded by a struct
284 * ofp11_match_header in 'b'. Stores the result in 'match'.
286 * Fails with an error when encountering unknown OXM headers.
288 * Returns 0 if successful, otherwise an OpenFlow error code. */
290 oxm_pull_match(struct ofpbuf *b, struct match *match)
292 return oxm_pull_match__(b, true, match);
295 /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown
296 * OXM headers instead of failing with an error when they are encountered. */
298 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
300 return oxm_pull_match__(b, false, match);
303 /* nx_put_match() and helpers.
305 * 'put' functions whose names end in 'w' add a wildcarded field.
306 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
307 * Other 'put' functions add exact-match fields.
311 nxm_put_header(struct ofpbuf *b, uint32_t header)
313 ovs_be32 n_header = htonl(header);
314 ofpbuf_put(b, &n_header, sizeof n_header);
318 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
320 nxm_put_header(b, header);
321 ofpbuf_put(b, &value, sizeof value);
325 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
332 nxm_put_8(b, header, value);
336 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
337 ofpbuf_put(b, &value, sizeof value);
338 ofpbuf_put(b, &mask, sizeof mask);
343 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
345 nxm_put_header(b, header);
346 ofpbuf_put(b, &value, sizeof value);
350 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
352 nxm_put_header(b, header);
353 ofpbuf_put(b, &value, sizeof value);
354 ofpbuf_put(b, &mask, sizeof mask);
358 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
365 nxm_put_16(b, header, value);
369 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
375 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
377 nxm_put_header(b, header);
378 ofpbuf_put(b, &value, sizeof value);
382 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
384 nxm_put_header(b, header);
385 ofpbuf_put(b, &value, sizeof value);
386 ofpbuf_put(b, &mask, sizeof mask);
390 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
397 nxm_put_32(b, header, value);
401 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
407 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
409 nxm_put_header(b, header);
410 ofpbuf_put(b, &value, sizeof value);
414 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
416 nxm_put_header(b, header);
417 ofpbuf_put(b, &value, sizeof value);
418 ofpbuf_put(b, &mask, sizeof mask);
422 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
429 nxm_put_64(b, header, value);
433 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
439 nxm_put_eth(struct ofpbuf *b, uint32_t header,
440 const uint8_t value[ETH_ADDR_LEN])
442 nxm_put_header(b, header);
443 ofpbuf_put(b, value, ETH_ADDR_LEN);
447 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
448 const uint8_t value[ETH_ADDR_LEN],
449 const uint8_t mask[ETH_ADDR_LEN])
451 if (!eth_addr_is_zero(mask)) {
452 if (eth_mask_is_exact(mask)) {
453 nxm_put_eth(b, header, value);
455 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
456 ofpbuf_put(b, value, ETH_ADDR_LEN);
457 ofpbuf_put(b, mask, ETH_ADDR_LEN);
463 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
464 const struct in6_addr *value, const struct in6_addr *mask)
466 if (ipv6_mask_is_any(mask)) {
468 } else if (ipv6_mask_is_exact(mask)) {
469 nxm_put_header(b, header);
470 ofpbuf_put(b, value, sizeof *value);
472 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
473 ofpbuf_put(b, value, sizeof *value);
474 ofpbuf_put(b, mask, sizeof *mask);
479 nxm_put_frag(struct ofpbuf *b, const struct match *match)
481 uint8_t nw_frag = match->flow.nw_frag;
482 uint8_t nw_frag_mask = match->wc.masks.nw_frag;
484 switch (nw_frag_mask) {
488 case FLOW_NW_FRAG_MASK:
489 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
493 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
494 nw_frag_mask & FLOW_NW_FRAG_MASK);
499 /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in
502 nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
504 const struct flow *flow = &match->flow;
506 if (flow->dl_type == htons(ETH_TYPE_IP)) {
507 nxm_put_32m(b, mf_oxm_header(MFF_IPV4_SRC, oxm),
508 flow->nw_src, match->wc.masks.nw_src);
509 nxm_put_32m(b, mf_oxm_header(MFF_IPV4_DST, oxm),
510 flow->nw_dst, match->wc.masks.nw_dst);
512 nxm_put_ipv6(b, mf_oxm_header(MFF_IPV6_SRC, oxm),
513 &flow->ipv6_src, &match->wc.masks.ipv6_src);
514 nxm_put_ipv6(b, mf_oxm_header(MFF_IPV6_DST, oxm),
515 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
518 nxm_put_frag(b, match);
520 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
522 nxm_put_8(b, mf_oxm_header(MFF_IP_DSCP_SHIFTED, oxm),
525 nxm_put_8(b, mf_oxm_header(MFF_IP_DSCP, oxm),
526 flow->nw_tos & IP_DSCP_MASK);
530 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
531 nxm_put_8(b, mf_oxm_header(MFF_IP_ECN, oxm),
532 flow->nw_tos & IP_ECN_MASK);
535 if (!oxm && match->wc.masks.nw_ttl) {
536 nxm_put_8(b, mf_oxm_header(MFF_IP_TTL, oxm), flow->nw_ttl);
539 nxm_put_32m(b, mf_oxm_header(MFF_IPV6_LABEL, oxm),
540 flow->ipv6_label, match->wc.masks.ipv6_label);
542 if (match->wc.masks.nw_proto) {
543 nxm_put_8(b, mf_oxm_header(MFF_IP_PROTO, oxm), flow->nw_proto);
545 if (flow->nw_proto == IPPROTO_TCP) {
546 nxm_put_16m(b, mf_oxm_header(MFF_TCP_SRC, oxm),
547 flow->tp_src, match->wc.masks.tp_src);
548 nxm_put_16m(b, mf_oxm_header(MFF_TCP_DST, oxm),
549 flow->tp_dst, match->wc.masks.tp_dst);
550 nxm_put_16m(b, mf_oxm_header(MFF_TCP_FLAGS, oxm),
551 flow->tcp_flags, match->wc.masks.tcp_flags);
552 } else if (flow->nw_proto == IPPROTO_UDP) {
553 nxm_put_16m(b, mf_oxm_header(MFF_UDP_SRC, oxm),
554 flow->tp_src, match->wc.masks.tp_src);
555 nxm_put_16m(b, mf_oxm_header(MFF_UDP_DST, oxm),
556 flow->tp_dst, match->wc.masks.tp_dst);
557 } else if (flow->nw_proto == IPPROTO_SCTP) {
558 nxm_put_16m(b, mf_oxm_header(MFF_SCTP_SRC, oxm), flow->tp_src,
559 match->wc.masks.tp_src);
560 nxm_put_16m(b, mf_oxm_header(MFF_SCTP_DST, oxm), flow->tp_dst,
561 match->wc.masks.tp_dst);
562 } else if (is_icmpv4(flow)) {
563 if (match->wc.masks.tp_src) {
564 nxm_put_8(b, mf_oxm_header(MFF_ICMPV4_TYPE, oxm),
565 ntohs(flow->tp_src));
567 if (match->wc.masks.tp_dst) {
568 nxm_put_8(b, mf_oxm_header(MFF_ICMPV4_CODE, oxm),
569 ntohs(flow->tp_dst));
571 } else if (is_icmpv6(flow)) {
572 if (match->wc.masks.tp_src) {
573 nxm_put_8(b, mf_oxm_header(MFF_ICMPV6_TYPE, oxm),
574 ntohs(flow->tp_src));
576 if (match->wc.masks.tp_dst) {
577 nxm_put_8(b, mf_oxm_header(MFF_ICMPV6_CODE, oxm),
578 ntohs(flow->tp_dst));
580 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
581 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
582 nxm_put_ipv6(b, mf_oxm_header(MFF_ND_TARGET, oxm),
583 &flow->nd_target, &match->wc.masks.nd_target);
584 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
585 nxm_put_eth_masked(b, mf_oxm_header(MFF_ND_SLL, oxm),
586 flow->arp_sha, match->wc.masks.arp_sha);
588 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
589 nxm_put_eth_masked(b, mf_oxm_header(MFF_ND_TLL, oxm),
590 flow->arp_tha, match->wc.masks.arp_tha);
597 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
598 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
599 * Otherwise, 'cookie_mask' should be zero.
601 * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
602 * 'oxm' as the OpenFlow version number for the OXM format to use.
604 * This function can cause 'b''s data to be reallocated.
606 * Returns the number of bytes appended to 'b', excluding padding.
608 * If 'match' is a catch-all rule that matches every packet, then this function
609 * appends nothing to 'b' and returns 0. */
611 nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
612 ovs_be64 cookie, ovs_be64 cookie_mask)
614 const struct flow *flow = &match->flow;
615 const size_t start_len = ofpbuf_size(b);
619 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
622 if (match->wc.masks.dp_hash) {
624 nxm_put_32m(b, NXM_NX_DP_HASH, htonl(flow->dp_hash),
625 htonl(match->wc.masks.dp_hash));
629 if (match->wc.masks.recirc_id) {
631 nxm_put_32(b, NXM_NX_RECIRC_ID, htonl(flow->recirc_id));
635 if (match->wc.masks.in_port.ofp_port) {
636 ofp_port_t in_port = flow->in_port.ofp_port;
638 nxm_put_32(b, mf_oxm_header(MFF_IN_PORT_OXM, oxm),
639 ofputil_port_to_ofp11(in_port));
641 nxm_put_16(b, mf_oxm_header(MFF_IN_PORT, oxm),
642 htons(ofp_to_u16(in_port)));
647 nxm_put_eth_masked(b, mf_oxm_header(MFF_ETH_SRC, oxm),
648 flow->dl_src, match->wc.masks.dl_src);
649 nxm_put_eth_masked(b, mf_oxm_header(MFF_ETH_DST, oxm),
650 flow->dl_dst, match->wc.masks.dl_dst);
651 nxm_put_16m(b, mf_oxm_header(MFF_ETH_TYPE, oxm),
652 ofputil_dl_type_to_openflow(flow->dl_type),
653 match->wc.masks.dl_type);
657 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
658 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
659 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
661 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
662 nxm_put_16(b, mf_oxm_header(MFF_VLAN_VID, oxm), vid);
664 nxm_put_16m(b, mf_oxm_header(MFF_VLAN_VID, oxm), vid, mask);
667 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
668 nxm_put_8(b, mf_oxm_header(MFF_VLAN_PCP, oxm),
669 vlan_tci_to_pcp(flow->vlan_tci));
673 nxm_put_16m(b, mf_oxm_header(MFF_VLAN_TCI, oxm), flow->vlan_tci,
674 match->wc.masks.vlan_tci);
678 if (eth_type_mpls(flow->dl_type)) {
679 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
680 nxm_put_8(b, mf_oxm_header(MFF_MPLS_TC, oxm),
681 mpls_lse_to_tc(flow->mpls_lse[0]));
684 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
685 nxm_put_8(b, mf_oxm_header(MFF_MPLS_BOS, oxm),
686 mpls_lse_to_bos(flow->mpls_lse[0]));
689 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
690 nxm_put_32(b, mf_oxm_header(MFF_MPLS_LABEL, oxm),
691 htonl(mpls_lse_to_label(flow->mpls_lse[0])));
696 if (is_ip_any(flow)) {
697 nxm_put_ip(b, match, oxm);
698 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
699 flow->dl_type == htons(ETH_TYPE_RARP)) {
701 if (match->wc.masks.nw_proto) {
702 nxm_put_16(b, mf_oxm_header(MFF_ARP_OP, oxm),
703 htons(flow->nw_proto));
705 nxm_put_32m(b, mf_oxm_header(MFF_ARP_SPA, oxm),
706 flow->nw_src, match->wc.masks.nw_src);
707 nxm_put_32m(b, mf_oxm_header(MFF_ARP_TPA, oxm),
708 flow->nw_dst, match->wc.masks.nw_dst);
709 nxm_put_eth_masked(b, mf_oxm_header(MFF_ARP_SHA, oxm),
710 flow->arp_sha, match->wc.masks.arp_sha);
711 nxm_put_eth_masked(b, mf_oxm_header(MFF_ARP_THA, oxm),
712 flow->arp_tha, match->wc.masks.arp_tha);
716 nxm_put_64m(b, mf_oxm_header(MFF_TUN_ID, oxm),
717 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
719 /* Other tunnel metadata. */
720 nxm_put_32m(b, mf_oxm_header(MFF_TUN_SRC, oxm),
721 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
722 nxm_put_32m(b, mf_oxm_header(MFF_TUN_DST, oxm),
723 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
726 for (i = 0; i < FLOW_N_REGS; i++) {
727 nxm_put_32m(b, mf_oxm_header(MFF_REG0 + i, oxm),
728 htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
732 nxm_put_32m(b, mf_oxm_header(MFF_PKT_MARK, oxm), htonl(flow->pkt_mark),
733 htonl(match->wc.masks.pkt_mark));
735 /* OpenFlow 1.1+ Metadata. */
736 nxm_put_64m(b, mf_oxm_header(MFF_METADATA, oxm),
737 flow->metadata, match->wc.masks.metadata);
740 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
742 match_len = ofpbuf_size(b) - start_len;
746 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
747 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
748 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
749 * Otherwise, 'cookie_mask' should be zero.
751 * This function can cause 'b''s data to be reallocated.
753 * Returns the number of bytes appended to 'b', excluding padding. The return
754 * value can be zero if it appended nothing at all to 'b' (which happens if
755 * 'cr' is a catch-all rule that matches every packet). */
757 nx_put_match(struct ofpbuf *b, const struct match *match,
758 ovs_be64 cookie, ovs_be64 cookie_mask)
760 int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
762 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
766 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
767 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
770 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
771 * version in use as 'version'.
773 * This function can cause 'b''s data to be reallocated.
775 * Returns the number of bytes appended to 'b', excluding the padding. Never
778 oxm_put_match(struct ofpbuf *b, const struct match *match,
779 enum ofp_version version)
782 struct ofp11_match_header *omh;
783 size_t start_len = ofpbuf_size(b);
784 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
786 ofpbuf_put_uninit(b, sizeof *omh);
787 match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
789 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
791 omh = ofpbuf_at(b, start_len, sizeof *omh);
792 omh->type = htons(OFPMT_OXM);
793 omh->length = htons(match_len);
798 /* nx_match_to_string() and helpers. */
800 static void format_nxm_field_name(struct ds *, uint32_t header);
803 nx_match_to_string(const uint8_t *p, unsigned int match_len)
809 return xstrdup("<any>");
813 while ((header = nx_entry_ok(p, match_len)) != 0) {
814 unsigned int length = NXM_LENGTH(header);
815 unsigned int value_len = nxm_field_bytes(header);
816 const uint8_t *value = p + 4;
817 const uint8_t *mask = value + value_len;
821 ds_put_cstr(&s, ", ");
824 format_nxm_field_name(&s, header);
825 ds_put_char(&s, '(');
827 for (i = 0; i < value_len; i++) {
828 ds_put_format(&s, "%02x", value[i]);
830 if (NXM_HASMASK(header)) {
831 ds_put_char(&s, '/');
832 for (i = 0; i < value_len; i++) {
833 ds_put_format(&s, "%02x", mask[i]);
836 ds_put_char(&s, ')');
839 match_len -= 4 + length;
844 ds_put_cstr(&s, ", ");
847 ds_put_format(&s, "<%u invalid bytes>", match_len);
850 return ds_steal_cstr(&s);
854 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
856 const struct ofp11_match_header *omh = ofpbuf_data(p);
862 if (match_len < sizeof *omh) {
863 ds_put_format(&s, "<match too short: %u>", match_len);
867 if (omh->type != htons(OFPMT_OXM)) {
868 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
872 match_len_ = ntohs(omh->length);
873 if (match_len_ < sizeof *omh) {
874 ds_put_format(&s, "<match length field too short: %u>", match_len_);
878 if (match_len_ != match_len) {
879 ds_put_format(&s, "<match length field incorrect: %u != %u>",
880 match_len_, match_len);
884 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
885 match_len - sizeof *omh);
888 return ds_steal_cstr(&s);
892 format_nxm_field_name(struct ds *s, uint32_t header)
894 const struct mf_field *mf = mf_from_nxm_header(header);
896 ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
897 if (NXM_HASMASK(header)) {
898 ds_put_cstr(s, "_W");
900 } else if (header == NXM_NX_COOKIE) {
901 ds_put_cstr(s, "NXM_NX_COOKIE");
902 } else if (header == NXM_NX_COOKIE_W) {
903 ds_put_cstr(s, "NXM_NX_COOKIE_W");
905 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
910 parse_nxm_field_name(const char *name, int name_len)
915 /* Check whether it's a field name. */
916 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
921 for (i = 0; i < MFF_N_IDS; i++) {
922 const struct mf_field *mf = mf_from_id(i);
926 !strncmp(mf->nxm_name, name, name_len) &&
927 mf->nxm_name[name_len] == '\0') {
928 header = mf->nxm_header;
929 } else if (mf->oxm_name &&
930 !strncmp(mf->oxm_name, name, name_len) &&
931 mf->oxm_name[name_len] == '\0') {
932 header = mf->oxm_header;
939 } else if (mf->maskable != MFM_NONE) {
940 return NXM_MAKE_WILD_HEADER(header);
944 if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
945 (name_len == strlen("NXM_NX_COOKIE"))) {
947 return NXM_NX_COOKIE;
949 return NXM_NX_COOKIE_W;
953 /* Check whether it's a 32-bit field header value as hex.
954 * (This isn't ordinarily useful except for testing error behavior.) */
956 uint32_t header = hexits_value(name, name_len, NULL);
957 if (header != UINT_MAX) {
965 /* nx_match_from_string(). */
968 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
970 const char *full_s = s;
971 const size_t start_len = ofpbuf_size(b);
973 if (!strcmp(s, "<any>")) {
974 /* Ensure that 'ofpbuf_data(b)' isn't actually null. */
975 ofpbuf_prealloc_tailroom(b, 1);
979 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
986 name_len = strcspn(s, "(");
987 if (s[name_len] != '(') {
988 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
991 header = parse_nxm_field_name(name, name_len);
993 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
998 nxm_put_header(b, header);
999 s = ofpbuf_put_hex(b, s, &n);
1000 if (n != nxm_field_bytes(header)) {
1001 ovs_fatal(0, "%.2s: hex digits expected", s);
1003 if (NXM_HASMASK(header)) {
1004 s += strspn(s, " ");
1006 ovs_fatal(0, "%s: missing / in masked field %.*s",
1007 full_s, name_len, name);
1009 s = ofpbuf_put_hex(b, s + 1, &n);
1010 if (n != nxm_field_bytes(header)) {
1011 ovs_fatal(0, "%.2s: hex digits expected", s);
1015 s += strspn(s, " ");
1017 ovs_fatal(0, "%s: missing ) following field %.*s",
1018 full_s, name_len, name);
1023 return ofpbuf_size(b) - start_len;
1027 nx_match_from_string(const char *s, struct ofpbuf *b)
1029 int match_len = nx_match_from_string_raw(s, b);
1030 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1035 oxm_match_from_string(const char *s, struct ofpbuf *b)
1038 struct ofp11_match_header *omh;
1039 size_t start_len = ofpbuf_size(b);
1041 ofpbuf_put_uninit(b, sizeof *omh);
1042 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1043 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1045 omh = ofpbuf_at(b, start_len, sizeof *omh);
1046 omh->type = htons(OFPMT_OXM);
1047 omh->length = htons(match_len);
1052 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1055 * Returns NULL if successful, otherwise a malloc()'d string describing the
1056 * error. The caller is responsible for freeing the returned string. */
1057 char * WARN_UNUSED_RESULT
1058 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1060 const char *full_s = s;
1063 error = mf_parse_subfield__(&move->src, &s);
1067 if (strncmp(s, "->", 2)) {
1068 return xasprintf("%s: missing `->' following source", full_s);
1071 error = mf_parse_subfield(&move->dst, s);
1076 if (move->src.n_bits != move->dst.n_bits) {
1077 return xasprintf("%s: source field is %d bits wide but destination is "
1078 "%d bits wide", full_s,
1079 move->src.n_bits, move->dst.n_bits);
1084 /* Parses 's' as a "load" action, in the form described in ovs-ofctl(8), into
1087 * Returns NULL if successful, otherwise a malloc()'d string describing the
1088 * error. The caller is responsible for freeing the returned string. */
1089 char * WARN_UNUSED_RESULT
1090 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1092 const char *full_s = s;
1093 uint64_t value = strtoull(s, (char **) &s, 0);
1096 if (strncmp(s, "->", 2)) {
1097 return xasprintf("%s: missing `->' following value", full_s);
1100 error = mf_parse_subfield(&load->dst, s);
1105 if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1106 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
1107 full_s, value, load->dst.n_bits);
1110 load->subvalue.be64[0] = htonll(0);
1111 load->subvalue.be64[1] = htonll(value);
1115 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1118 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1120 ds_put_format(s, "move:");
1121 mf_format_subfield(&move->src, s);
1122 ds_put_cstr(s, "->");
1123 mf_format_subfield(&move->dst, s);
1127 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1129 ds_put_cstr(s, "load:");
1130 mf_format_subvalue(&load->subvalue, s);
1131 ds_put_cstr(s, "->");
1132 mf_format_subfield(&load->dst, s);
1136 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1137 struct ofpbuf *ofpacts)
1139 struct ofpact_reg_move *move;
1141 move = ofpact_put_REG_MOVE(ofpacts);
1142 move->src.field = mf_from_nxm_header(ntohl(narm->src));
1143 move->src.ofs = ntohs(narm->src_ofs);
1144 move->src.n_bits = ntohs(narm->n_bits);
1145 move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1146 move->dst.ofs = ntohs(narm->dst_ofs);
1147 move->dst.n_bits = ntohs(narm->n_bits);
1149 return nxm_reg_move_check(move, NULL);
1153 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1154 struct ofpbuf *ofpacts)
1156 struct ofpact_reg_load *load;
1158 load = ofpact_put_REG_LOAD(ofpacts);
1159 load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1160 load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1161 load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1162 load->subvalue.be64[1] = narl->value;
1164 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1166 if (load->dst.n_bits < 64 &&
1167 ntohll(narl->value) >> load->dst.n_bits) {
1168 return OFPERR_OFPBAC_BAD_ARGUMENT;
1171 return nxm_reg_load_check(load, NULL);
1175 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1179 error = mf_check_src(&move->src, flow);
1184 return mf_check_dst(&move->dst, NULL);
1188 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1190 return mf_check_dst(&load->dst, flow);
1194 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1195 struct ofpbuf *openflow)
1197 struct nx_action_reg_move *narm;
1199 narm = ofputil_put_NXAST_REG_MOVE(openflow);
1200 narm->n_bits = htons(move->dst.n_bits);
1201 narm->src_ofs = htons(move->src.ofs);
1202 narm->dst_ofs = htons(move->dst.ofs);
1203 narm->src = htonl(move->src.field->nxm_header);
1204 narm->dst = htonl(move->dst.field->nxm_header);
1208 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1209 struct ofpbuf *openflow)
1211 struct nx_action_reg_load *narl;
1213 narl = ofputil_put_NXAST_REG_LOAD(openflow);
1214 narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1215 narl->dst = htonl(load->dst.field->nxm_header);
1216 narl->value = load->subvalue.be64[1];
1219 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1222 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1223 struct flow *flow, struct flow_wildcards *wc)
1225 union mf_value src_value;
1226 union mf_value dst_value;
1228 mf_mask_field_and_prereqs(move->dst.field, &wc->masks);
1229 mf_mask_field_and_prereqs(move->src.field, &wc->masks);
1231 mf_get_value(move->dst.field, flow, &dst_value);
1232 mf_get_value(move->src.field, flow, &src_value);
1233 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1234 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1236 mf_set_flow_value(move->dst.field, &dst_value, flow);
1240 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow,
1241 struct flow_wildcards *wc)
1243 /* Since at the datapath interface we do not have set actions for
1244 * individual fields, but larger sets of fields for a given protocol
1245 * layer, the set action will in practice only ever apply to exactly
1246 * matched flows for the given protocol layer. For example, if the
1247 * reg_load changes the IP TTL, the corresponding datapath action will
1248 * rewrite also the IP addresses and TOS byte. Since these other field
1249 * values may not be explicitly set, they depend on the incoming flow field
1250 * values, and are hence all of them are set in the wildcards masks, when
1251 * the action is committed to the datapath. For the rare case, where the
1252 * reg_load action does not actually change the value, and no other flow
1253 * field values are set (or loaded), the datapath action is skipped, and
1254 * no mask bits are set. Such a datapath flow should, however, be
1255 * dependent on the specific field value, so the corresponding wildcard
1256 * mask bits must be set, lest the datapath flow be applied to packets
1257 * containing some other value in the field and the field value remain
1258 * unchanged regardless of the incoming value.
1260 * We set the masks here for the whole fields, and their prerequisities.
1261 * Even if only the lower byte of a TCP destination port is set,
1262 * we set the mask for the whole field, and also the ip_proto in the IP
1263 * header, so that the kernel flow would not be applied on, e.g., a UDP
1264 * packet, or any other IP protocol in addition to TCP packets.
1266 mf_mask_field_and_prereqs(load->dst.field, &wc->masks);
1267 mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1271 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1272 struct flow *flow, struct flow_wildcards *wc)
1274 union mf_subvalue src_subvalue;
1275 union mf_subvalue mask_value;
1276 ovs_be64 src_data_be = htonll(src_data);
1278 memset(&mask_value, 0xff, sizeof mask_value);
1279 mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1281 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1282 &src_subvalue, sizeof src_subvalue, 0,
1283 sizeof src_data_be * 8);
1284 mf_write_subfield_flow(dst, &src_subvalue, flow);
1287 /* nxm_parse_stack_action, works for both push() and pop(). */
1289 /* Parses 's' as a "push" or "pop" action, in the form described in
1290 * ovs-ofctl(8), into '*stack_action'.
1292 * Returns NULL if successful, otherwise a malloc()'d string describing the
1293 * error. The caller is responsible for freeing the returned string. */
1294 char * WARN_UNUSED_RESULT
1295 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1299 error = mf_parse_subfield__(&stack_action->subfield, &s);
1305 return xasprintf("%s: trailing garbage following push or pop", s);
1312 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1314 ds_put_cstr(s, "push:");
1315 mf_format_subfield(&push->subfield, s);
1319 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1321 ds_put_cstr(s, "pop:");
1322 mf_format_subfield(&pop->subfield, s);
1325 /* Common set for both push and pop actions. */
1327 stack_action_from_openflow__(const struct nx_action_stack *nasp,
1328 struct ofpact_stack *stack_action)
1330 stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field));
1331 stack_action->subfield.ofs = ntohs(nasp->offset);
1332 stack_action->subfield.n_bits = ntohs(nasp->n_bits);
1336 nxm_stack_to_nxast__(const struct ofpact_stack *stack_action,
1337 struct nx_action_stack *nasp)
1339 nasp->offset = htons(stack_action->subfield.ofs);
1340 nasp->n_bits = htons(stack_action->subfield.n_bits);
1341 nasp->field = htonl(stack_action->subfield.field->nxm_header);
1345 nxm_stack_push_from_openflow(const struct nx_action_stack *nasp,
1346 struct ofpbuf *ofpacts)
1348 struct ofpact_stack *push;
1350 push = ofpact_put_STACK_PUSH(ofpacts);
1351 stack_action_from_openflow__(nasp, push);
1353 return nxm_stack_push_check(push, NULL);
1357 nxm_stack_pop_from_openflow(const struct nx_action_stack *nasp,
1358 struct ofpbuf *ofpacts)
1360 struct ofpact_stack *pop;
1362 pop = ofpact_put_STACK_POP(ofpacts);
1363 stack_action_from_openflow__(nasp, pop);
1365 return nxm_stack_pop_check(pop, NULL);
1369 nxm_stack_push_check(const struct ofpact_stack *push,
1370 const struct flow *flow)
1372 return mf_check_src(&push->subfield, flow);
1376 nxm_stack_pop_check(const struct ofpact_stack *pop,
1377 const struct flow *flow)
1379 return mf_check_dst(&pop->subfield, flow);
1383 nxm_stack_push_to_nxast(const struct ofpact_stack *stack,
1384 struct ofpbuf *openflow)
1386 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_PUSH(openflow));
1390 nxm_stack_pop_to_nxast(const struct ofpact_stack *stack,
1391 struct ofpbuf *openflow)
1393 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_POP(openflow));
1396 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1398 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1400 ofpbuf_put(stack, v, sizeof *v);
1403 static union mf_subvalue *
1404 nx_stack_pop(struct ofpbuf *stack)
1406 union mf_subvalue *v = NULL;
1408 if (ofpbuf_size(stack)) {
1410 ofpbuf_set_size(stack, ofpbuf_size(stack) - sizeof *v);
1411 v = (union mf_subvalue *) ofpbuf_tail(stack);
1418 nxm_execute_stack_push(const struct ofpact_stack *push,
1419 const struct flow *flow, struct flow_wildcards *wc,
1420 struct ofpbuf *stack)
1422 union mf_subvalue mask_value;
1423 union mf_subvalue dst_value;
1425 memset(&mask_value, 0xff, sizeof mask_value);
1426 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1428 mf_read_subfield(&push->subfield, flow, &dst_value);
1429 nx_stack_push(stack, &dst_value);
1433 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1434 struct flow *flow, struct flow_wildcards *wc,
1435 struct ofpbuf *stack)
1437 union mf_subvalue *src_value;
1439 src_value = nx_stack_pop(stack);
1441 /* Only pop if stack is not empty. Otherwise, give warning. */
1443 union mf_subvalue mask_value;
1445 memset(&mask_value, 0xff, sizeof mask_value);
1446 mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1447 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1449 if (!VLOG_DROP_WARN(&rl)) {
1450 char *flow_str = flow_to_string(flow);
1451 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"