2 * Copyright (c) 2010, 2011, 2012, 2013 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 = htonll(UINT64_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 %zu)", match_len, b->size);
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 = b->data;
258 if (b->size < 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 %zu)", match_len, b->size);
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 ofp11_match
284 * in 'b' with length 'match_len'. 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 * PXM 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)
364 case CONSTANT_HTONS(UINT16_MAX):
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)
396 case CONSTANT_HTONL(UINT32_MAX):
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)
428 case CONSTANT_HTONLL(UINT64_MAX):
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);
500 nxm_put_ip(struct ofpbuf *b, const struct match *match,
501 uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
504 const struct flow *flow = &match->flow;
506 nxm_put_frag(b, match);
508 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
510 nxm_put_8(b, OXM_OF_IP_DSCP, flow->nw_tos >> 2);
512 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
516 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
517 nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
518 flow->nw_tos & IP_ECN_MASK);
521 if (!oxm && match->wc.masks.nw_ttl) {
522 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
525 if (match->wc.masks.nw_proto) {
526 nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
528 if (flow->nw_proto == IPPROTO_TCP) {
529 nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
530 flow->tp_src, match->wc.masks.tp_src);
531 nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
532 flow->tp_dst, match->wc.masks.tp_dst);
533 } else if (flow->nw_proto == IPPROTO_UDP) {
534 nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
535 flow->tp_src, match->wc.masks.tp_src);
536 nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
537 flow->tp_dst, match->wc.masks.tp_dst);
538 } else if (flow->nw_proto == icmp_proto) {
539 if (match->wc.masks.tp_src) {
540 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
542 if (match->wc.masks.tp_dst) {
543 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
549 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
550 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
551 * Otherwise, 'cookie_mask' should be zero.
553 * This function can cause 'b''s data to be reallocated.
555 * Returns the number of bytes appended to 'b', excluding padding.
557 * If 'match' is a catch-all rule that matches every packet, then this function
558 * appends nothing to 'b' and returns 0. */
560 nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match,
561 ovs_be64 cookie, ovs_be64 cookie_mask)
563 const struct flow *flow = &match->flow;
564 const size_t start_len = b->size;
568 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
571 if (match->wc.masks.in_port.ofp_port) {
572 ofp_port_t in_port = flow->in_port.ofp_port;
574 nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
576 nxm_put_16(b, NXM_OF_IN_PORT, htons(ofp_to_u16(in_port)));
581 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
582 flow->dl_src, match->wc.masks.dl_src);
583 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
584 flow->dl_dst, match->wc.masks.dl_dst);
585 nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
586 ofputil_dl_type_to_openflow(flow->dl_type),
587 match->wc.masks.dl_type);
591 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
592 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
593 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
595 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
596 nxm_put_16(b, OXM_OF_VLAN_VID, vid);
598 nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask);
601 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
602 nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci));
606 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci,
607 match->wc.masks.vlan_tci);
611 if (eth_type_mpls(flow->dl_type)) {
612 if (match->wc.masks.mpls_lse & htonl(MPLS_TC_MASK)) {
613 nxm_put_8(b, OXM_OF_MPLS_TC, mpls_lse_to_tc(flow->mpls_lse));
616 if (match->wc.masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
617 nxm_put_8(b, OXM_OF_MPLS_BOS, mpls_lse_to_bos(flow->mpls_lse));
620 if (match->wc.masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
621 nxm_put_32(b, OXM_OF_MPLS_LABEL,
622 htonl(mpls_lse_to_label(flow->mpls_lse)));
627 if (flow->dl_type == htons(ETH_TYPE_IP)) {
629 nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
630 flow->nw_src, match->wc.masks.nw_src);
631 nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
632 flow->nw_dst, match->wc.masks.nw_dst);
633 nxm_put_ip(b, match, IPPROTO_ICMP,
634 oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
635 oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
636 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
638 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
639 &flow->ipv6_src, &match->wc.masks.ipv6_src);
640 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
641 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
642 nxm_put_ip(b, match, IPPROTO_ICMPV6,
643 oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
644 oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
646 nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
647 flow->ipv6_label, match->wc.masks.ipv6_label);
649 if (flow->nw_proto == IPPROTO_ICMPV6
650 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
651 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
652 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
653 &flow->nd_target, &match->wc.masks.nd_target);
654 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
655 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
656 flow->arp_sha, match->wc.masks.arp_sha);
658 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
659 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
660 flow->arp_tha, match->wc.masks.arp_tha);
663 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
664 flow->dl_type == htons(ETH_TYPE_RARP)) {
666 if (match->wc.masks.nw_proto) {
667 nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
668 htons(flow->nw_proto));
670 nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
671 flow->nw_src, match->wc.masks.nw_src);
672 nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
673 flow->nw_dst, match->wc.masks.nw_dst);
674 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
675 flow->arp_sha, match->wc.masks.arp_sha);
676 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
677 flow->arp_tha, match->wc.masks.arp_tha);
681 nxm_put_64m(b, oxm ? OXM_OF_TUNNEL_ID : NXM_NX_TUN_ID,
682 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
684 /* Other tunnel metadata. */
685 nxm_put_32m(b, NXM_NX_TUN_IPV4_SRC,
686 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
687 nxm_put_32m(b, NXM_NX_TUN_IPV4_DST,
688 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
691 for (i = 0; i < FLOW_N_REGS; i++) {
692 nxm_put_32m(b, NXM_NX_REG(i),
693 htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
696 /* OpenFlow 1.1+ Metadata. */
697 nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata);
700 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
702 match_len = b->size - start_len;
706 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
707 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
708 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
709 * Otherwise, 'cookie_mask' should be zero.
711 * This function can cause 'b''s data to be reallocated.
713 * Returns the number of bytes appended to 'b', excluding padding. The return
714 * value can be zero if it appended nothing at all to 'b' (which happens if
715 * 'cr' is a catch-all rule that matches every packet). */
717 nx_put_match(struct ofpbuf *b, const struct match *match,
718 ovs_be64 cookie, ovs_be64 cookie_mask)
720 int match_len = nx_put_raw(b, false, match, cookie, cookie_mask);
722 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
727 /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that
728 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
731 * This function can cause 'b''s data to be reallocated.
733 * Returns the number of bytes appended to 'b', excluding the padding. Never
736 oxm_put_match(struct ofpbuf *b, const struct match *match)
739 struct ofp11_match_header *omh;
740 size_t start_len = b->size;
741 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
743 ofpbuf_put_uninit(b, sizeof *omh);
744 match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
745 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
747 omh = ofpbuf_at(b, start_len, sizeof *omh);
748 omh->type = htons(OFPMT_OXM);
749 omh->length = htons(match_len);
754 /* nx_match_to_string() and helpers. */
756 static void format_nxm_field_name(struct ds *, uint32_t header);
759 nx_match_to_string(const uint8_t *p, unsigned int match_len)
765 return xstrdup("<any>");
769 while ((header = nx_entry_ok(p, match_len)) != 0) {
770 unsigned int length = NXM_LENGTH(header);
771 unsigned int value_len = nxm_field_bytes(header);
772 const uint8_t *value = p + 4;
773 const uint8_t *mask = value + value_len;
777 ds_put_cstr(&s, ", ");
780 format_nxm_field_name(&s, header);
781 ds_put_char(&s, '(');
783 for (i = 0; i < value_len; i++) {
784 ds_put_format(&s, "%02x", value[i]);
786 if (NXM_HASMASK(header)) {
787 ds_put_char(&s, '/');
788 for (i = 0; i < value_len; i++) {
789 ds_put_format(&s, "%02x", mask[i]);
792 ds_put_char(&s, ')');
795 match_len -= 4 + length;
800 ds_put_cstr(&s, ", ");
803 ds_put_format(&s, "<%u invalid bytes>", match_len);
806 return ds_steal_cstr(&s);
810 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
812 const struct ofp11_match_header *omh = p->data;
818 if (match_len < sizeof *omh) {
819 ds_put_format(&s, "<match too short: %u>", match_len);
823 if (omh->type != htons(OFPMT_OXM)) {
824 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
828 match_len_ = ntohs(omh->length);
829 if (match_len_ < sizeof *omh) {
830 ds_put_format(&s, "<match length field too short: %u>", match_len_);
834 if (match_len_ != match_len) {
835 ds_put_format(&s, "<match length field incorrect: %u != %u>",
836 match_len_, match_len);
840 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
841 match_len - sizeof *omh);
844 return ds_steal_cstr(&s);
848 format_nxm_field_name(struct ds *s, uint32_t header)
850 const struct mf_field *mf = mf_from_nxm_header(header);
852 ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
853 if (NXM_HASMASK(header)) {
854 ds_put_cstr(s, "_W");
856 } else if (header == NXM_NX_COOKIE) {
857 ds_put_cstr(s, "NXM_NX_COOKIE");
858 } else if (header == NXM_NX_COOKIE_W) {
859 ds_put_cstr(s, "NXM_NX_COOKIE_W");
861 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
866 parse_nxm_field_name(const char *name, int name_len)
871 /* Check whether it's a field name. */
872 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
877 for (i = 0; i < MFF_N_IDS; i++) {
878 const struct mf_field *mf = mf_from_id(i);
882 !strncmp(mf->nxm_name, name, name_len) &&
883 mf->nxm_name[name_len] == '\0') {
884 header = mf->nxm_header;
885 } else if (mf->oxm_name &&
886 !strncmp(mf->oxm_name, name, name_len) &&
887 mf->oxm_name[name_len] == '\0') {
888 header = mf->oxm_header;
895 } else if (mf->maskable != MFM_NONE) {
896 return NXM_MAKE_WILD_HEADER(header);
900 if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
901 (name_len == strlen("NXM_NX_COOKIE"))) {
903 return NXM_NX_COOKIE;
905 return NXM_NX_COOKIE_W;
909 /* Check whether it's a 32-bit field header value as hex.
910 * (This isn't ordinarily useful except for testing error behavior.) */
912 uint32_t header = hexits_value(name, name_len, NULL);
913 if (header != UINT_MAX) {
921 /* nx_match_from_string(). */
924 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
926 const char *full_s = s;
927 const size_t start_len = b->size;
929 if (!strcmp(s, "<any>")) {
930 /* Ensure that 'b->data' isn't actually null. */
931 ofpbuf_prealloc_tailroom(b, 1);
935 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
942 name_len = strcspn(s, "(");
943 if (s[name_len] != '(') {
944 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
947 header = parse_nxm_field_name(name, name_len);
949 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
954 nxm_put_header(b, header);
955 s = ofpbuf_put_hex(b, s, &n);
956 if (n != nxm_field_bytes(header)) {
957 ovs_fatal(0, "%.2s: hex digits expected", s);
959 if (NXM_HASMASK(header)) {
962 ovs_fatal(0, "%s: missing / in masked field %.*s",
963 full_s, name_len, name);
965 s = ofpbuf_put_hex(b, s + 1, &n);
966 if (n != nxm_field_bytes(header)) {
967 ovs_fatal(0, "%.2s: hex digits expected", s);
973 ovs_fatal(0, "%s: missing ) following field %.*s",
974 full_s, name_len, name);
979 return b->size - start_len;
983 nx_match_from_string(const char *s, struct ofpbuf *b)
985 int match_len = nx_match_from_string_raw(s, b);
986 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
991 oxm_match_from_string(const char *s, struct ofpbuf *b)
994 struct ofp11_match_header *omh;
995 size_t start_len = b->size;
997 ofpbuf_put_uninit(b, sizeof *omh);
998 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
999 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
1001 omh = ofpbuf_at(b, start_len, sizeof *omh);
1002 omh->type = htons(OFPMT_OXM);
1003 omh->length = htons(match_len);
1008 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1011 * Returns NULL if successful, otherwise a malloc()'d string describing the
1012 * error. The caller is responsible for freeing the returned string. */
1013 char * WARN_UNUSED_RESULT
1014 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1016 const char *full_s = s;
1019 error = mf_parse_subfield__(&move->src, &s);
1023 if (strncmp(s, "->", 2)) {
1024 return xasprintf("%s: missing `->' following source", full_s);
1027 error = mf_parse_subfield(&move->dst, s);
1032 if (move->src.n_bits != move->dst.n_bits) {
1033 return xasprintf("%s: source field is %d bits wide but destination is "
1034 "%d bits wide", full_s,
1035 move->src.n_bits, move->dst.n_bits);
1040 /* Parses 's' as a "load" action, in the form described in ovs-ofctl(8), into
1043 * Returns NULL if successful, otherwise a malloc()'d string describing the
1044 * error. The caller is responsible for freeing the returned string. */
1045 char * WARN_UNUSED_RESULT
1046 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1048 const char *full_s = s;
1049 uint64_t value = strtoull(s, (char **) &s, 0);
1052 if (strncmp(s, "->", 2)) {
1053 return xasprintf("%s: missing `->' following value", full_s);
1056 error = mf_parse_subfield(&load->dst, s);
1061 if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1062 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
1063 full_s, value, load->dst.n_bits);
1066 load->subvalue.be64[0] = htonll(0);
1067 load->subvalue.be64[1] = htonll(value);
1071 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1074 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1076 ds_put_format(s, "move:");
1077 mf_format_subfield(&move->src, s);
1078 ds_put_cstr(s, "->");
1079 mf_format_subfield(&move->dst, s);
1083 set_field_format(const struct ofpact_reg_load *load, struct ds *s)
1085 const struct mf_field *mf = load->dst.field;
1086 union mf_value value;
1088 ovs_assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD);
1089 ds_put_format(s, "set_field:");
1090 memset(&value, 0, sizeof value);
1091 bitwise_copy(&load->subvalue, sizeof load->subvalue, 0,
1092 &value, mf->n_bytes, 0, load->dst.n_bits);
1093 mf_format(mf, &value, NULL, s);
1094 ds_put_format(s, "->%s", mf->name);
1098 load_format(const struct ofpact_reg_load *load, struct ds *s)
1100 ds_put_cstr(s, "load:");
1101 mf_format_subvalue(&load->subvalue, s);
1102 ds_put_cstr(s, "->");
1103 mf_format_subfield(&load->dst, s);
1107 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1109 if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1110 set_field_format(load, s);
1112 load_format(load, s);
1117 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1118 struct ofpbuf *ofpacts)
1120 struct ofpact_reg_move *move;
1122 move = ofpact_put_REG_MOVE(ofpacts);
1123 move->src.field = mf_from_nxm_header(ntohl(narm->src));
1124 move->src.ofs = ntohs(narm->src_ofs);
1125 move->src.n_bits = ntohs(narm->n_bits);
1126 move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1127 move->dst.ofs = ntohs(narm->dst_ofs);
1128 move->dst.n_bits = ntohs(narm->n_bits);
1130 return nxm_reg_move_check(move, NULL);
1134 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1135 struct ofpbuf *ofpacts)
1137 struct ofpact_reg_load *load;
1139 load = ofpact_put_REG_LOAD(ofpacts);
1140 load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1141 load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1142 load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1143 load->subvalue.be64[1] = narl->value;
1145 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1147 if (load->dst.n_bits < 64 &&
1148 ntohll(narl->value) >> load->dst.n_bits) {
1149 return OFPERR_OFPBAC_BAD_ARGUMENT;
1152 return nxm_reg_load_check(load, NULL);
1156 nxm_reg_load_from_openflow12_set_field(
1157 const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts)
1159 uint16_t oasf_len = ntohs(oasf->len);
1160 uint32_t oxm_header = ntohl(oasf->dst);
1161 uint8_t oxm_length = NXM_LENGTH(oxm_header);
1162 struct ofpact_reg_load *load;
1163 const struct mf_field *mf;
1165 /* ofp12_action_set_field is padded to 64 bits by zero */
1166 if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) {
1167 return OFPERR_OFPBAC_BAD_ARGUMENT;
1169 if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length,
1170 oasf_len - oxm_length - sizeof *oasf)) {
1171 return OFPERR_OFPBAC_BAD_ARGUMENT;
1174 if (NXM_HASMASK(oxm_header)) {
1175 return OFPERR_OFPBAC_BAD_ARGUMENT;
1177 mf = mf_from_nxm_header(oxm_header);
1179 return OFPERR_OFPBAC_BAD_ARGUMENT;
1181 load = ofpact_put_REG_LOAD(ofpacts);
1182 ofpact_set_field_init(load, mf, oasf + 1);
1184 return nxm_reg_load_check(load, NULL);
1188 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1192 error = mf_check_src(&move->src, flow);
1197 return mf_check_dst(&move->dst, NULL);
1201 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1203 return mf_check_dst(&load->dst, flow);
1207 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1208 struct ofpbuf *openflow)
1210 struct nx_action_reg_move *narm;
1212 narm = ofputil_put_NXAST_REG_MOVE(openflow);
1213 narm->n_bits = htons(move->dst.n_bits);
1214 narm->src_ofs = htons(move->src.ofs);
1215 narm->dst_ofs = htons(move->dst.ofs);
1216 narm->src = htonl(move->src.field->nxm_header);
1217 narm->dst = htonl(move->dst.field->nxm_header);
1221 reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow)
1223 struct nx_action_reg_load *narl;
1225 narl = ofputil_put_NXAST_REG_LOAD(openflow);
1226 narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1227 narl->dst = htonl(load->dst.field->nxm_header);
1228 narl->value = load->subvalue.be64[1];
1232 set_field_to_ofast(const struct ofpact_reg_load *load,
1233 struct ofpbuf *openflow)
1235 const struct mf_field *mf = load->dst.field;
1236 uint16_t padded_value_len = ROUND_UP(mf->n_bytes, 8);
1237 struct ofp12_action_set_field *oasf;
1240 /* Set field is the only action of variable length (so far),
1241 * so handling the variable length portion is open-coded here */
1242 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
1243 oasf->dst = htonl(mf->oxm_header);
1244 oasf->len = htons(ntohs(oasf->len) + padded_value_len);
1246 value = ofpbuf_put_zeros(openflow, padded_value_len);
1247 bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs,
1248 value, mf->n_bytes, load->dst.ofs, load->dst.n_bits);
1252 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1253 struct ofpbuf *openflow)
1256 if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1257 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1259 switch(oh->version) {
1262 set_field_to_ofast(load, openflow);
1267 if (load->dst.n_bits < 64) {
1268 reg_load_to_nxast(load, openflow);
1270 /* Split into 64bit chunks */
1272 for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) {
1273 struct ofpact_reg_load subload = *load;
1275 chunk = MIN(load->dst.n_bits - ofs, 64);
1277 subload.dst.field = load->dst.field;
1278 subload.dst.ofs = load->dst.ofs + ofs;
1279 subload.dst.n_bits = chunk;
1280 bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs,
1281 &subload.subvalue, sizeof subload.subvalue, 0,
1283 reg_load_to_nxast(&subload, openflow);
1292 reg_load_to_nxast(load, openflow);
1296 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1299 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1300 struct flow *flow, struct flow_wildcards *wc)
1302 union mf_subvalue mask_value;
1303 union mf_value src_value;
1304 union mf_value dst_value;
1306 memset(&mask_value, 0xff, sizeof mask_value);
1307 mf_write_subfield_flow(&move->src, &mask_value, &wc->masks);
1309 mf_get_value(move->dst.field, flow, &dst_value);
1310 mf_get_value(move->src.field, flow, &src_value);
1311 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1312 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1314 mf_set_flow_value(move->dst.field, &dst_value, flow);
1318 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
1320 mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1324 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1327 union mf_subvalue src_subvalue;
1328 ovs_be64 src_data_be = htonll(src_data);
1330 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1331 &src_subvalue, sizeof src_subvalue, 0,
1332 sizeof src_data_be * 8);
1333 mf_write_subfield_flow(dst, &src_subvalue, flow);
1336 /* nxm_parse_stack_action, works for both push() and pop(). */
1338 /* Parses 's' as a "push" or "pop" action, in the form described in
1339 * ovs-ofctl(8), into '*stack_action'.
1341 * Returns NULL if successful, otherwise a malloc()'d string describing the
1342 * error. The caller is responsible for freeing the returned string. */
1343 char * WARN_UNUSED_RESULT
1344 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1348 error = mf_parse_subfield__(&stack_action->subfield, &s);
1354 return xasprintf("%s: trailing garbage following push or pop", s);
1361 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1363 ds_put_cstr(s, "push:");
1364 mf_format_subfield(&push->subfield, s);
1368 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1370 ds_put_cstr(s, "pop:");
1371 mf_format_subfield(&pop->subfield, s);
1374 /* Common set for both push and pop actions. */
1376 stack_action_from_openflow__(const struct nx_action_stack *nasp,
1377 struct ofpact_stack *stack_action)
1379 stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field));
1380 stack_action->subfield.ofs = ntohs(nasp->offset);
1381 stack_action->subfield.n_bits = ntohs(nasp->n_bits);
1385 nxm_stack_to_nxast__(const struct ofpact_stack *stack_action,
1386 struct nx_action_stack *nasp)
1388 nasp->offset = htons(stack_action->subfield.ofs);
1389 nasp->n_bits = htons(stack_action->subfield.n_bits);
1390 nasp->field = htonl(stack_action->subfield.field->nxm_header);
1394 nxm_stack_push_from_openflow(const struct nx_action_stack *nasp,
1395 struct ofpbuf *ofpacts)
1397 struct ofpact_stack *push;
1399 push = ofpact_put_STACK_PUSH(ofpacts);
1400 stack_action_from_openflow__(nasp, push);
1402 return nxm_stack_push_check(push, NULL);
1406 nxm_stack_pop_from_openflow(const struct nx_action_stack *nasp,
1407 struct ofpbuf *ofpacts)
1409 struct ofpact_stack *pop;
1411 pop = ofpact_put_STACK_POP(ofpacts);
1412 stack_action_from_openflow__(nasp, pop);
1414 return nxm_stack_pop_check(pop, NULL);
1418 nxm_stack_push_check(const struct ofpact_stack *push,
1419 const struct flow *flow)
1421 return mf_check_src(&push->subfield, flow);
1425 nxm_stack_pop_check(const struct ofpact_stack *pop,
1426 const struct flow *flow)
1428 return mf_check_dst(&pop->subfield, flow);
1432 nxm_stack_push_to_nxast(const struct ofpact_stack *stack,
1433 struct ofpbuf *openflow)
1435 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_PUSH(openflow));
1439 nxm_stack_pop_to_nxast(const struct ofpact_stack *stack,
1440 struct ofpbuf *openflow)
1442 nxm_stack_to_nxast__(stack, ofputil_put_NXAST_STACK_POP(openflow));
1445 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1447 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1449 ofpbuf_put(stack, v, sizeof *v);
1452 static union mf_subvalue *
1453 nx_stack_pop(struct ofpbuf *stack)
1455 union mf_subvalue *v = NULL;
1458 stack->size -= sizeof *v;
1459 v = (union mf_subvalue *) ofpbuf_tail(stack);
1466 nxm_execute_stack_push(const struct ofpact_stack *push,
1467 const struct flow *flow, struct flow_wildcards *wc,
1468 struct ofpbuf *stack)
1470 union mf_subvalue mask_value;
1471 union mf_subvalue dst_value;
1473 memset(&mask_value, 0xff, sizeof mask_value);
1474 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1476 mf_read_subfield(&push->subfield, flow, &dst_value);
1477 nx_stack_push(stack, &dst_value);
1481 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1482 struct flow *flow, struct ofpbuf *stack)
1484 union mf_subvalue *src_value;
1486 src_value = nx_stack_pop(stack);
1488 /* Only pop if stack is not empty. Otherwise, give warning. */
1490 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1492 if (!VLOG_DROP_WARN(&rl)) {
1493 char *flow_str = flow_to_string(flow);
1494 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"