2 * Copyright (c) 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.
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);
94 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
95 uint16_t priority, struct cls_rule *rule,
96 ovs_be64 *cookie, ovs_be64 *cookie_mask)
100 assert((cookie != NULL) == (cookie_mask != NULL));
102 cls_rule_init_catchall(rule, priority);
104 *cookie = *cookie_mask = htonll(0);
111 (header = nx_entry_ok(p, match_len)) != 0;
112 p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
113 const struct mf_field *mf;
116 mf = mf_from_nxm_header(header);
119 error = OFPERR_OFPBMC_BAD_FIELD;
123 } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
124 error = OFPERR_OFPBMC_BAD_PREREQ;
125 } else if (!mf_is_all_wild(mf, &rule->wc)) {
126 error = OFPERR_OFPBMC_DUP_FIELD;
127 } else if (header != OXM_OF_IN_PORT) {
128 unsigned int width = mf->n_bytes;
129 union mf_value value;
131 memcpy(&value, p + 4, width);
132 if (!mf_is_value_valid(mf, &value)) {
133 error = OFPERR_OFPBMC_BAD_VALUE;
134 } else if (!NXM_HASMASK(header)) {
136 mf_set_value(mf, &value, rule);
140 memcpy(&mask, p + 4 + width, width);
141 if (!mf_is_mask_valid(mf, &mask)) {
142 error = OFPERR_OFPBMC_BAD_MASK;
145 mf_set(mf, &value, &mask, rule);
149 /* Special case for 32bit ports when using OXM,
150 * ports are 16 bits wide otherwise. */
154 memcpy(&port_of11, p + 4, sizeof port_of11);
155 error = ofputil_port_from_ofp11(port_of11, &port);
157 cls_rule_set_in_port(rule, port);
161 /* Check if the match is for a cookie rather than a classifier rule. */
162 if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
164 error = OFPERR_OFPBMC_DUP_FIELD;
166 unsigned int width = sizeof *cookie;
168 memcpy(cookie, p + 4, width);
169 if (NXM_HASMASK(header)) {
170 memcpy(cookie_mask, p + 4 + width, width);
172 *cookie_mask = htonll(UINT64_MAX);
179 VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
180 "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
182 NXM_VENDOR(header), NXM_FIELD(header),
183 NXM_HASMASK(header), NXM_LENGTH(header),
184 ofperr_to_string(error));
189 return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
193 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
194 uint16_t priority, struct cls_rule *rule,
195 ovs_be64 *cookie, ovs_be64 *cookie_mask)
200 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
202 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
203 "multiple of 8, is longer than space in message (max "
204 "length %zu)", match_len, b->size);
205 return OFPERR_OFPBMC_BAD_LEN;
209 return nx_pull_raw(p, match_len, strict, priority, rule,
210 cookie, cookie_mask);
213 /* Parses the nx_match formatted match description in 'b' with length
214 * 'match_len'. The results are stored in 'rule', which is initialized with
215 * 'priority'. If 'cookie' and 'cookie_mask' contain valid pointers, then the
216 * cookie and mask will be stored in them if a "NXM_NX_COOKIE*" match is
217 * defined. Otherwise, 0 is stored in both.
219 * Fails with an error when encountering unknown NXM headers.
221 * Returns 0 if successful, otherwise an OpenFlow error code. */
223 nx_pull_match(struct ofpbuf *b, unsigned int match_len,
224 uint16_t priority, struct cls_rule *rule,
225 ovs_be64 *cookie, ovs_be64 *cookie_mask)
227 return nx_pull_match__(b, match_len, true, priority, rule, cookie,
231 /* Behaves the same as nx_pull_match() with one exception. Skips over unknown
232 * NXM headers instead of failing with an error when they are encountered. */
234 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
235 uint16_t priority, struct cls_rule *rule,
236 ovs_be64 *cookie, ovs_be64 *cookie_mask)
238 return nx_pull_match__(b, match_len, false, priority, rule, cookie,
243 oxm_pull_match__(struct ofpbuf *b, bool strict,
244 uint16_t priority, struct cls_rule *rule)
246 struct ofp11_match_header *omh = b->data;
250 if (b->size < sizeof *omh) {
251 return OFPERR_OFPBMC_BAD_LEN;
254 match_len = ntohs(omh->length);
255 if (match_len < sizeof *omh) {
256 return OFPERR_OFPBMC_BAD_LEN;
259 if (omh->type != htons(OFPMT_OXM)) {
260 return OFPERR_OFPBMC_BAD_TYPE;
263 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
265 VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
266 "multiple of 8, is longer than space in message (max "
267 "length %zu)", match_len, b->size);
268 return OFPERR_OFPBMC_BAD_LEN;
271 return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
272 strict, priority, rule, NULL, NULL);
275 /* Parses the oxm formatted match description preceeded by a struct
276 * ofp11_match in 'b' with length 'match_len'. The results are stored in
277 * 'rule', which is initialized with 'priority'.
279 * Fails with an error when encountering unknown OXM headers.
281 * Returns 0 if successful, otherwise an OpenFlow error code. */
283 oxm_pull_match(struct ofpbuf *b, uint16_t priority, struct cls_rule *rule)
285 return oxm_pull_match__(b, true, priority, rule);
288 /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown
289 * PXM headers instead of failing with an error when they are encountered. */
291 oxm_pull_match_loose(struct ofpbuf *b, uint16_t priority,
292 struct cls_rule *rule)
294 return oxm_pull_match__(b, false, priority, rule);
297 /* nx_put_match() and helpers.
299 * 'put' functions whose names end in 'w' add a wildcarded field.
300 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
301 * Other 'put' functions add exact-match fields.
305 nxm_put_header(struct ofpbuf *b, uint32_t header)
307 ovs_be32 n_header = htonl(header);
308 ofpbuf_put(b, &n_header, sizeof n_header);
312 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
314 nxm_put_header(b, header);
315 ofpbuf_put(b, &value, sizeof value);
319 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
326 nxm_put_8(b, header, value);
330 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
331 ofpbuf_put(b, &value, sizeof value);
332 ofpbuf_put(b, &mask, sizeof mask);
337 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
339 nxm_put_header(b, header);
340 ofpbuf_put(b, &value, sizeof value);
344 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
346 nxm_put_header(b, header);
347 ofpbuf_put(b, &value, sizeof value);
348 ofpbuf_put(b, &mask, sizeof mask);
352 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
358 case CONSTANT_HTONS(UINT16_MAX):
359 nxm_put_16(b, header, value);
363 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
369 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
371 nxm_put_header(b, header);
372 ofpbuf_put(b, &value, sizeof value);
376 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
378 nxm_put_header(b, header);
379 ofpbuf_put(b, &value, sizeof value);
380 ofpbuf_put(b, &mask, sizeof mask);
384 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
390 case CONSTANT_HTONL(UINT32_MAX):
391 nxm_put_32(b, header, value);
395 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
401 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
403 nxm_put_header(b, header);
404 ofpbuf_put(b, &value, sizeof value);
408 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
410 nxm_put_header(b, header);
411 ofpbuf_put(b, &value, sizeof value);
412 ofpbuf_put(b, &mask, sizeof mask);
416 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
422 case CONSTANT_HTONLL(UINT64_MAX):
423 nxm_put_64(b, header, value);
427 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
433 nxm_put_eth(struct ofpbuf *b, uint32_t header,
434 const uint8_t value[ETH_ADDR_LEN])
436 nxm_put_header(b, header);
437 ofpbuf_put(b, value, ETH_ADDR_LEN);
441 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
442 const uint8_t value[ETH_ADDR_LEN],
443 const uint8_t mask[ETH_ADDR_LEN])
445 if (!eth_addr_is_zero(mask)) {
446 if (eth_mask_is_exact(mask)) {
447 nxm_put_eth(b, header, value);
449 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
450 ofpbuf_put(b, value, ETH_ADDR_LEN);
451 ofpbuf_put(b, mask, ETH_ADDR_LEN);
457 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
458 const struct in6_addr *value, const struct in6_addr *mask)
460 if (ipv6_mask_is_any(mask)) {
462 } else if (ipv6_mask_is_exact(mask)) {
463 nxm_put_header(b, header);
464 ofpbuf_put(b, value, sizeof *value);
466 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
467 ofpbuf_put(b, value, sizeof *value);
468 ofpbuf_put(b, mask, sizeof *mask);
473 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
475 uint8_t nw_frag = cr->flow.nw_frag;
476 uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
478 switch (nw_frag_mask) {
482 case FLOW_NW_FRAG_MASK:
483 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
487 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
488 nw_frag_mask & FLOW_NW_FRAG_MASK);
494 nxm_put_ip(struct ofpbuf *b, const struct cls_rule *cr,
495 uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
498 const flow_wildcards_t wc = cr->wc.wildcards;
499 const struct flow *flow = &cr->flow;
503 if (!(wc & FWW_NW_DSCP)) {
504 nxm_put_8(b, oxm ? OXM_OF_IP_DSCP : NXM_OF_IP_TOS,
505 flow->nw_tos & IP_DSCP_MASK);
508 if (!(wc & FWW_NW_ECN)) {
509 nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
510 flow->nw_tos & IP_ECN_MASK);
513 if (!oxm && !(wc & FWW_NW_TTL)) {
514 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
517 if (!(wc & FWW_NW_PROTO)) {
518 nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
520 if (flow->nw_proto == IPPROTO_TCP) {
521 nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
522 flow->tp_src, cr->wc.tp_src_mask);
523 nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
524 flow->tp_dst, cr->wc.tp_dst_mask);
525 } else if (flow->nw_proto == IPPROTO_UDP) {
526 nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
527 flow->tp_src, cr->wc.tp_src_mask);
528 nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
529 flow->tp_dst, cr->wc.tp_dst_mask);
530 } else if (flow->nw_proto == icmp_proto) {
531 if (cr->wc.tp_src_mask) {
532 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
534 if (cr->wc.tp_dst_mask) {
535 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
541 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
542 * 'cr->priority', because priority is not part of nx_match). For Flow Mod and
543 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
544 * Otherwise, 'cookie_mask' should be zero.
546 * This function can cause 'b''s data to be reallocated.
548 * Returns the number of bytes appended to 'b', excluding padding.
550 * If 'cr' is a catch-all rule that matches every packet, then this function
551 * appends nothing to 'b' and returns 0. */
553 nx_put_raw(struct ofpbuf *b, bool oxm, const struct cls_rule *cr,
554 ovs_be64 cookie, ovs_be64 cookie_mask)
556 const flow_wildcards_t wc = cr->wc.wildcards;
557 const struct flow *flow = &cr->flow;
558 const size_t start_len = b->size;
562 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
565 if (!(wc & FWW_IN_PORT)) {
566 uint16_t in_port = flow->in_port;
568 nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
570 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
575 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
576 flow->dl_src, cr->wc.dl_src_mask);
577 nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
578 flow->dl_dst, cr->wc.dl_dst_mask);
579 if (!(wc & FWW_DL_TYPE)) {
580 nxm_put_16(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
581 ofputil_dl_type_to_openflow(flow->dl_type));
586 ovs_be16 vid = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
587 ovs_be16 mask = cr->wc.vlan_tci_mask & htons(VLAN_VID_MASK | VLAN_CFI);
589 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
590 nxm_put_16(b, OXM_OF_VLAN_VID, vid);
592 nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask);
595 if (vid && vlan_tci_to_pcp(cr->wc.vlan_tci_mask)) {
596 nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci));
600 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
604 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
606 nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
607 flow->nw_src, cr->wc.nw_src_mask);
608 nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
609 flow->nw_dst, cr->wc.nw_dst_mask);
610 nxm_put_ip(b, cr, IPPROTO_ICMP,
611 oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
612 oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
613 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
615 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
616 &flow->ipv6_src, &cr->wc.ipv6_src_mask);
617 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
618 &flow->ipv6_dst, &cr->wc.ipv6_dst_mask);
619 nxm_put_ip(b, cr, IPPROTO_ICMPV6,
620 oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
621 oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
623 nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
624 flow->ipv6_label, cr->wc.ipv6_label_mask);
626 if (flow->nw_proto == IPPROTO_ICMPV6
627 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
628 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
629 nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
630 &flow->nd_target, &cr->wc.nd_target_mask);
631 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
632 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
633 flow->arp_sha, cr->wc.arp_sha_mask);
635 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
636 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
637 flow->arp_tha, cr->wc.arp_tha_mask);
640 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
642 if (!(wc & FWW_NW_PROTO)) {
643 nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
644 htons(flow->nw_proto));
646 nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
647 flow->nw_src, cr->wc.nw_src_mask);
648 nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
649 flow->nw_dst, cr->wc.nw_dst_mask);
650 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
651 flow->arp_sha, cr->wc.arp_sha_mask);
652 nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
653 flow->arp_tha, cr->wc.arp_tha_mask);
657 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
660 for (i = 0; i < FLOW_N_REGS; i++) {
661 nxm_put_32m(b, NXM_NX_REG(i),
662 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
665 /* OpenFlow 1.1+ Metadata. */
666 nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, cr->wc.metadata_mask);
669 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
671 match_len = b->size - start_len;
675 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
676 * 'cr->priority', because priority is not part of nx_match), plus enough zero
677 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
678 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
679 * Otherwise, 'cookie_mask' should be zero.
681 * This function can cause 'b''s data to be reallocated.
683 * Returns the number of bytes appended to 'b', excluding padding. The return
684 * value can be zero if it appended nothing at all to 'b' (which happens if
685 * 'cr' is a catch-all rule that matches every packet). */
687 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr,
688 ovs_be64 cookie, ovs_be64 cookie_mask)
690 int match_len = nx_put_raw(b, false, cr, cookie, cookie_mask);
692 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
697 /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that
698 * expresses 'cr' (except for 'cr->priority', because priority is not part of
699 * nx_match), plus enough zero bytes to pad the data appended out to a multiple
702 * This function can cause 'b''s data to be reallocated.
704 * Returns the number of bytes appended to 'b', excluding the padding. Never
707 oxm_put_match(struct ofpbuf *b, const struct cls_rule *cr)
710 struct ofp11_match_header *omh;
711 size_t start_len = b->size;
712 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
714 ofpbuf_put_uninit(b, sizeof *omh);
715 match_len = nx_put_raw(b, true, cr, cookie, cookie_mask) + sizeof *omh;
716 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
718 omh = (struct ofp11_match_header *)((char *)b->data + start_len);
719 omh->type = htons(OFPMT_OXM);
720 omh->length = htons(match_len);
725 /* nx_match_to_string() and helpers. */
727 static void format_nxm_field_name(struct ds *, uint32_t header);
730 nx_match_to_string(const uint8_t *p, unsigned int match_len)
736 return xstrdup("<any>");
740 while ((header = nx_entry_ok(p, match_len)) != 0) {
741 unsigned int length = NXM_LENGTH(header);
742 unsigned int value_len = nxm_field_bytes(header);
743 const uint8_t *value = p + 4;
744 const uint8_t *mask = value + value_len;
748 ds_put_cstr(&s, ", ");
751 format_nxm_field_name(&s, header);
752 ds_put_char(&s, '(');
754 for (i = 0; i < value_len; i++) {
755 ds_put_format(&s, "%02x", value[i]);
757 if (NXM_HASMASK(header)) {
758 ds_put_char(&s, '/');
759 for (i = 0; i < value_len; i++) {
760 ds_put_format(&s, "%02x", mask[i]);
763 ds_put_char(&s, ')');
766 match_len -= 4 + length;
771 ds_put_cstr(&s, ", ");
774 ds_put_format(&s, "<%u invalid bytes>", match_len);
777 return ds_steal_cstr(&s);
781 oxm_match_to_string(const uint8_t *p, unsigned int match_len)
783 const struct ofp11_match_header *omh = (struct ofp11_match_header *)p;
789 if (match_len < sizeof *omh) {
790 ds_put_format(&s, "<match too short: %u>", match_len);
794 if (omh->type != htons(OFPMT_OXM)) {
795 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
799 match_len_ = ntohs(omh->length);
800 if (match_len_ < sizeof *omh) {
801 ds_put_format(&s, "<match length field too short: %u>", match_len_);
805 if (match_len_ != match_len) {
806 ds_put_format(&s, "<match length field incorrect: %u != %u>",
807 match_len_, match_len);
811 return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh);
814 return ds_steal_cstr(&s);
818 format_nxm_field_name(struct ds *s, uint32_t header)
820 const struct mf_field *mf = mf_from_nxm_header(header);
822 ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
823 if (NXM_HASMASK(header)) {
824 ds_put_cstr(s, "_W");
826 } else if (header == NXM_NX_COOKIE) {
827 ds_put_cstr(s, "NXM_NX_COOKIE");
828 } else if (header == NXM_NX_COOKIE_W) {
829 ds_put_cstr(s, "NXM_NX_COOKIE_W");
831 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
836 parse_nxm_field_name(const char *name, int name_len)
841 /* Check whether it's a field name. */
842 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
847 for (i = 0; i < MFF_N_IDS; i++) {
848 const struct mf_field *mf = mf_from_id(i);
852 !strncmp(mf->nxm_name, name, name_len) &&
853 mf->nxm_name[name_len] == '\0') {
854 header = mf->nxm_header;
855 } else if (mf->oxm_name &&
856 !strncmp(mf->oxm_name, name, name_len) &&
857 mf->oxm_name[name_len] == '\0') {
858 header = mf->oxm_header;
865 } else if (mf->maskable != MFM_NONE) {
866 return NXM_MAKE_WILD_HEADER(header);
870 if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
871 (name_len == strlen("NXM_NX_COOKIE"))) {
873 return NXM_NX_COOKIE;
875 return NXM_NX_COOKIE_W;
879 /* Check whether it's a 32-bit field header value as hex.
880 * (This isn't ordinarily useful except for testing error behavior.) */
882 uint32_t header = hexits_value(name, name_len, NULL);
883 if (header != UINT_MAX) {
891 /* nx_match_from_string(). */
894 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
896 const char *full_s = s;
897 const size_t start_len = b->size;
899 if (!strcmp(s, "<any>")) {
900 /* Ensure that 'b->data' isn't actually null. */
901 ofpbuf_prealloc_tailroom(b, 1);
905 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
912 name_len = strcspn(s, "(");
913 if (s[name_len] != '(') {
914 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
917 header = parse_nxm_field_name(name, name_len);
919 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
924 nxm_put_header(b, header);
925 s = ofpbuf_put_hex(b, s, &n);
926 if (n != nxm_field_bytes(header)) {
927 ovs_fatal(0, "%.2s: hex digits expected", s);
929 if (NXM_HASMASK(header)) {
932 ovs_fatal(0, "%s: missing / in masked field %.*s",
933 full_s, name_len, name);
935 s = ofpbuf_put_hex(b, s + 1, &n);
936 if (n != nxm_field_bytes(header)) {
937 ovs_fatal(0, "%.2s: hex digits expected", s);
943 ovs_fatal(0, "%s: missing ) following field %.*s",
944 full_s, name_len, name);
949 return b->size - start_len;
953 nx_match_from_string(const char *s, struct ofpbuf *b)
955 int match_len = nx_match_from_string_raw(s, b);
956 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
961 oxm_match_from_string(const char *s, struct ofpbuf *b)
964 struct ofp11_match_header *omh;
965 size_t start_len = b->size;
967 ofpbuf_put_uninit(b, sizeof *omh);
968 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
969 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
971 omh = (struct ofp11_match_header *)((char *)b->data + start_len);
972 omh->type = htons(OFPMT_OXM);
973 omh->length = htons(match_len);
979 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
981 const char *full_s = s;
983 s = mf_parse_subfield(&move->src, s);
984 if (strncmp(s, "->", 2)) {
985 ovs_fatal(0, "%s: missing `->' following source", full_s);
988 s = mf_parse_subfield(&move->dst, s);
990 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
993 if (move->src.n_bits != move->dst.n_bits) {
994 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
995 "%d bits wide", full_s,
996 move->src.n_bits, move->dst.n_bits);
1001 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
1003 const char *full_s = s;
1005 load->value = strtoull(s, (char **) &s, 0);
1006 if (strncmp(s, "->", 2)) {
1007 ovs_fatal(0, "%s: missing `->' following value", full_s);
1010 s = mf_parse_subfield(&load->dst, s);
1012 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1015 if (load->dst.n_bits < 64 && (load->value >> load->dst.n_bits) != 0) {
1016 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
1017 full_s, load->value, load->dst.n_bits);
1021 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1024 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1026 ds_put_format(s, "move:");
1027 mf_format_subfield(&move->src, s);
1028 ds_put_cstr(s, "->");
1029 mf_format_subfield(&move->dst, s);
1033 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1035 ds_put_format(s, "load:%#"PRIx64"->", load->value);
1036 mf_format_subfield(&load->dst, s);
1040 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1041 struct ofpbuf *ofpacts)
1043 struct ofpact_reg_move *move;
1045 move = ofpact_put_REG_MOVE(ofpacts);
1046 move->src.field = mf_from_nxm_header(ntohl(narm->src));
1047 move->src.ofs = ntohs(narm->src_ofs);
1048 move->src.n_bits = ntohs(narm->n_bits);
1049 move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1050 move->dst.ofs = ntohs(narm->dst_ofs);
1051 move->dst.n_bits = ntohs(narm->n_bits);
1053 return nxm_reg_move_check(move, NULL);
1057 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1058 struct ofpbuf *ofpacts)
1060 struct ofpact_reg_load *load;
1062 load = ofpact_put_REG_LOAD(ofpacts);
1063 load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1064 load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1065 load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1066 load->value = ntohll(narl->value);
1068 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1070 if (load->dst.n_bits < 64 && load->value >> load->dst.n_bits) {
1071 return OFPERR_OFPBAC_BAD_ARGUMENT;
1074 return nxm_reg_load_check(load, NULL);
1078 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1082 error = mf_check_src(&move->src, flow);
1087 return mf_check_dst(&move->dst, NULL);
1091 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1093 return mf_check_dst(&load->dst, flow);
1097 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1098 struct ofpbuf *openflow)
1100 struct nx_action_reg_move *narm;
1102 narm = ofputil_put_NXAST_REG_MOVE(openflow);
1103 narm->n_bits = htons(move->dst.n_bits);
1104 narm->src_ofs = htons(move->src.ofs);
1105 narm->dst_ofs = htons(move->dst.ofs);
1106 narm->src = htonl(move->src.field->nxm_header);
1107 narm->dst = htonl(move->dst.field->nxm_header);
1111 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1112 struct ofpbuf *openflow)
1114 struct nx_action_reg_load *narl;
1116 narl = ofputil_put_NXAST_REG_LOAD(openflow);
1117 narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1118 narl->dst = htonl(load->dst.field->nxm_header);
1119 narl->value = htonll(load->value);
1122 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1125 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1128 union mf_value src_value;
1129 union mf_value dst_value;
1131 mf_get_value(move->dst.field, flow, &dst_value);
1132 mf_get_value(move->src.field, flow, &src_value);
1133 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1134 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1136 mf_set_flow_value(move->dst.field, &dst_value, flow);
1140 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
1142 nxm_reg_load(&load->dst, load->value, flow);
1146 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1149 union mf_value dst_value;
1150 union mf_value src_value;
1152 mf_get_value(dst->field, flow, &dst_value);
1153 src_value.be64 = htonll(src_data);
1154 bitwise_copy(&src_value, sizeof src_value.be64, 0,
1155 &dst_value, dst->field->n_bytes, dst->ofs,
1157 mf_set_flow_value(dst->field, &dst_value, flow);