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-errors.h"
29 #include "openflow/nicira-ext.h"
31 #include "unaligned.h"
35 VLOG_DEFINE_THIS_MODULE(nx_match);
37 /* Rate limit for nx_match parse errors. These always indicate a bug in the
38 * peer and so there's not much point in showing a lot of them. */
39 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41 /* Returns the width of the data for a field with the given 'header', in
44 nxm_field_bytes(uint32_t header)
46 unsigned int length = NXM_LENGTH(header);
47 return NXM_HASMASK(header) ? length / 2 : length;
50 /* Returns the width of the data for a field with the given 'header', in
53 nxm_field_bits(uint32_t header)
55 return nxm_field_bytes(header) * 8;
58 /* nx_pull_match() and helpers. */
61 nx_entry_ok(const void *p, unsigned int match_len)
63 unsigned int payload_len;
69 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
73 memcpy(&header_be, p, 4);
74 header = ntohl(header_be);
76 payload_len = NXM_LENGTH(header);
78 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
82 if (match_len < payload_len + 4) {
83 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
84 "%u bytes left in nx_match", payload_len + 4, match_len);
92 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
93 uint16_t priority, struct cls_rule *rule,
94 ovs_be64 *cookie, ovs_be64 *cookie_mask)
99 assert((cookie != NULL) == (cookie_mask != NULL));
101 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
103 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
104 "multiple of 8, is longer than space in message (max "
105 "length %zu)", match_len, b->size);
106 return OFPERR_OFPBMC_BAD_LEN;
109 cls_rule_init_catchall(rule, priority);
111 *cookie = *cookie_mask = htonll(0);
114 (header = nx_entry_ok(p, match_len)) != 0;
115 p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
116 const struct mf_field *mf;
119 mf = mf_from_nxm_header(header);
122 error = OFPERR_OFPBMC_BAD_FIELD;
126 } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
127 error = OFPERR_OFPBMC_BAD_PREREQ;
128 } else if (!mf_is_all_wild(mf, &rule->wc)) {
129 error = OFPERR_OFPBMC_DUP_FIELD;
131 unsigned int width = mf->n_bytes;
132 union mf_value value;
134 memcpy(&value, p + 4, width);
135 if (!mf_is_value_valid(mf, &value)) {
136 error = OFPERR_OFPBMC_BAD_VALUE;
137 } else if (!NXM_HASMASK(header)) {
139 mf_set_value(mf, &value, rule);
143 memcpy(&mask, p + 4 + width, width);
144 if (!mf_is_mask_valid(mf, &mask)) {
145 error = OFPERR_OFPBMC_BAD_MASK;
148 mf_set(mf, &value, &mask, rule);
153 /* Check if the match is for a cookie rather than a classifier rule. */
154 if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
156 error = OFPERR_OFPBMC_DUP_FIELD;
158 unsigned int width = sizeof *cookie;
160 memcpy(cookie, p + 4, width);
161 if (NXM_HASMASK(header)) {
162 memcpy(cookie_mask, p + 4 + width, width);
164 *cookie_mask = htonll(UINT64_MAX);
171 VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
172 "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
174 NXM_VENDOR(header), NXM_FIELD(header),
175 NXM_HASMASK(header), NXM_LENGTH(header),
176 ofperr_to_string(error));
181 return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
184 /* Parses the nx_match formatted match description in 'b' with length
185 * 'match_len'. The results are stored in 'rule', which is initialized with
186 * 'priority'. If 'cookie' and 'cookie_mask' contain valid pointers, then the
187 * cookie and mask will be stored in them if a "NXM_NX_COOKIE*" match is
188 * defined. Otherwise, 0 is stored in both.
190 * Fails with an error when encountering unknown NXM headers.
192 * Returns 0 if successful, otherwise an OpenFlow error code. */
194 nx_pull_match(struct ofpbuf *b, unsigned int match_len,
195 uint16_t priority, struct cls_rule *rule,
196 ovs_be64 *cookie, ovs_be64 *cookie_mask)
198 return nx_pull_match__(b, match_len, true, priority, rule, cookie,
202 /* Behaves the same as nx_pull_match() with one exception. Skips over unknown
203 * NXM headers instead of failing with an error when they are encountered. */
205 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
206 uint16_t priority, struct cls_rule *rule,
207 ovs_be64 *cookie, ovs_be64 *cookie_mask)
209 return nx_pull_match__(b, match_len, false, priority, rule, cookie,
213 /* nx_put_match() and helpers.
215 * 'put' functions whose names end in 'w' add a wildcarded field.
216 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
217 * Other 'put' functions add exact-match fields.
221 nxm_put_header(struct ofpbuf *b, uint32_t header)
223 ovs_be32 n_header = htonl(header);
224 ofpbuf_put(b, &n_header, sizeof n_header);
228 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
230 nxm_put_header(b, header);
231 ofpbuf_put(b, &value, sizeof value);
235 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
242 nxm_put_8(b, header, value);
246 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
247 ofpbuf_put(b, &value, sizeof value);
248 ofpbuf_put(b, &mask, sizeof mask);
253 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
255 nxm_put_header(b, header);
256 ofpbuf_put(b, &value, sizeof value);
260 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
262 nxm_put_header(b, header);
263 ofpbuf_put(b, &value, sizeof value);
264 ofpbuf_put(b, &mask, sizeof mask);
268 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
274 case CONSTANT_HTONS(UINT16_MAX):
275 nxm_put_16(b, header, value);
279 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
285 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
287 nxm_put_header(b, header);
288 ofpbuf_put(b, &value, sizeof value);
292 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
294 nxm_put_header(b, header);
295 ofpbuf_put(b, &value, sizeof value);
296 ofpbuf_put(b, &mask, sizeof mask);
300 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
306 case CONSTANT_HTONL(UINT32_MAX):
307 nxm_put_32(b, header, value);
311 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
317 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
319 nxm_put_header(b, header);
320 ofpbuf_put(b, &value, sizeof value);
324 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
326 nxm_put_header(b, header);
327 ofpbuf_put(b, &value, sizeof value);
328 ofpbuf_put(b, &mask, sizeof mask);
332 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
338 case CONSTANT_HTONLL(UINT64_MAX):
339 nxm_put_64(b, header, value);
343 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
349 nxm_put_eth(struct ofpbuf *b, uint32_t header,
350 const uint8_t value[ETH_ADDR_LEN])
352 nxm_put_header(b, header);
353 ofpbuf_put(b, value, ETH_ADDR_LEN);
357 nxm_put_eth_dst(struct ofpbuf *b,
358 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
360 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
361 case FWW_DL_DST | FWW_ETH_MCAST:
364 nxm_put_header(b, NXM_OF_ETH_DST_W);
365 ofpbuf_put(b, value, ETH_ADDR_LEN);
366 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
369 nxm_put_eth(b, NXM_OF_ETH_DST, value);
375 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
376 const struct in6_addr *value, const struct in6_addr *mask)
378 if (ipv6_mask_is_any(mask)) {
380 } else if (ipv6_mask_is_exact(mask)) {
381 nxm_put_header(b, header);
382 ofpbuf_put(b, value, sizeof *value);
384 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
385 ofpbuf_put(b, value, sizeof *value);
386 ofpbuf_put(b, mask, sizeof *mask);
391 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
393 uint8_t nw_frag = cr->flow.nw_frag;
394 uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
396 switch (nw_frag_mask) {
400 case FLOW_NW_FRAG_MASK:
401 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
405 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
406 nw_frag_mask & FLOW_NW_FRAG_MASK);
412 nxm_put_ip(struct ofpbuf *b, const struct cls_rule *cr,
413 uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code)
415 const flow_wildcards_t wc = cr->wc.wildcards;
416 const struct flow *flow = &cr->flow;
420 if (!(wc & FWW_NW_DSCP)) {
421 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
424 if (!(wc & FWW_NW_ECN)) {
425 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
428 if (!(wc & FWW_NW_TTL)) {
429 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
432 if (!(wc & FWW_NW_PROTO)) {
433 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
435 if (flow->nw_proto == IPPROTO_TCP) {
436 nxm_put_16m(b, NXM_OF_TCP_SRC, flow->tp_src, cr->wc.tp_src_mask);
437 nxm_put_16m(b, NXM_OF_TCP_DST, flow->tp_dst, cr->wc.tp_dst_mask);
438 } else if (flow->nw_proto == IPPROTO_UDP) {
439 nxm_put_16m(b, NXM_OF_UDP_SRC, flow->tp_src, cr->wc.tp_src_mask);
440 nxm_put_16m(b, NXM_OF_UDP_DST, flow->tp_dst, cr->wc.tp_dst_mask);
441 } else if (flow->nw_proto == icmp_proto) {
442 if (cr->wc.tp_src_mask) {
443 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
445 if (cr->wc.tp_dst_mask) {
446 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
452 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
453 * 'cr->priority', because priority is not part of nx_match), plus enough
454 * zero bytes to pad the nx_match out to a multiple of 8. For Flow Mod
455 * and Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be
456 * supplied. Otherwise, 'cookie_mask' should be zero.
458 * This function can cause 'b''s data to be reallocated.
460 * Returns the number of bytes appended to 'b', excluding padding.
462 * If 'cr' is a catch-all rule that matches every packet, then this function
463 * appends nothing to 'b' and returns 0. */
465 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr,
466 ovs_be64 cookie, ovs_be64 cookie_mask)
468 const flow_wildcards_t wc = cr->wc.wildcards;
469 const struct flow *flow = &cr->flow;
470 const size_t start_len = b->size;
474 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 10);
477 if (!(wc & FWW_IN_PORT)) {
478 uint16_t in_port = flow->in_port;
479 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
483 nxm_put_eth_dst(b, wc, flow->dl_dst);
484 if (!(wc & FWW_DL_SRC)) {
485 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
487 if (!(wc & FWW_DL_TYPE)) {
488 nxm_put_16(b, NXM_OF_ETH_TYPE,
489 ofputil_dl_type_to_openflow(flow->dl_type));
493 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
496 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
498 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
499 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
500 nxm_put_ip(b, cr, IPPROTO_ICMP, NXM_OF_ICMP_TYPE, NXM_OF_ICMP_CODE);
501 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
503 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
504 &cr->wc.ipv6_src_mask);
505 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
506 &cr->wc.ipv6_dst_mask);
508 IPPROTO_ICMPV6, NXM_NX_ICMPV6_TYPE, NXM_NX_ICMPV6_CODE);
510 if (!(wc & FWW_IPV6_LABEL)) {
511 nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
514 if (flow->nw_proto == IPPROTO_ICMPV6
515 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
516 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
517 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
518 &cr->wc.nd_target_mask);
519 if (!(wc & FWW_ARP_SHA)
520 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
521 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
523 if (!(wc & FWW_ARP_THA)
524 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
525 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
528 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
530 if (!(wc & FWW_NW_PROTO)) {
531 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
533 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
534 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
535 if (!(wc & FWW_ARP_SHA)) {
536 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
538 if (!(wc & FWW_ARP_THA)) {
539 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
544 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
547 for (i = 0; i < FLOW_N_REGS; i++) {
548 nxm_put_32m(b, NXM_NX_REG(i),
549 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
553 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
555 match_len = b->size - start_len;
556 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
560 /* nx_match_to_string() and helpers. */
562 static void format_nxm_field_name(struct ds *, uint32_t header);
565 nx_match_to_string(const uint8_t *p, unsigned int match_len)
571 return xstrdup("<any>");
575 while ((header = nx_entry_ok(p, match_len)) != 0) {
576 unsigned int length = NXM_LENGTH(header);
577 unsigned int value_len = nxm_field_bytes(header);
578 const uint8_t *value = p + 4;
579 const uint8_t *mask = value + value_len;
583 ds_put_cstr(&s, ", ");
586 format_nxm_field_name(&s, header);
587 ds_put_char(&s, '(');
589 for (i = 0; i < value_len; i++) {
590 ds_put_format(&s, "%02x", value[i]);
592 if (NXM_HASMASK(header)) {
593 ds_put_char(&s, '/');
594 for (i = 0; i < value_len; i++) {
595 ds_put_format(&s, "%02x", mask[i]);
598 ds_put_char(&s, ')');
601 match_len -= 4 + length;
606 ds_put_cstr(&s, ", ");
609 ds_put_format(&s, "<%u invalid bytes>", match_len);
612 return ds_steal_cstr(&s);
616 format_nxm_field_name(struct ds *s, uint32_t header)
618 const struct mf_field *mf = mf_from_nxm_header(header);
620 ds_put_cstr(s, mf->nxm_name);
621 if (NXM_HASMASK(header)) {
622 ds_put_cstr(s, "_W");
624 } else if (header == NXM_NX_COOKIE) {
625 ds_put_cstr(s, "NXM_NX_COOKIE");
626 } else if (header == NXM_NX_COOKIE_W) {
627 ds_put_cstr(s, "NXM_NX_COOKIE_W");
629 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
634 parse_nxm_field_name(const char *name, int name_len)
639 /* Check whether it's a field name. */
640 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
645 for (i = 0; i < MFF_N_IDS; i++) {
646 const struct mf_field *mf = mf_from_id(i);
649 && !strncmp(mf->nxm_name, name, name_len)
650 && mf->nxm_name[name_len] == '\0') {
652 return mf->nxm_header;
653 } else if (mf->maskable != MFM_NONE) {
654 return NXM_MAKE_WILD_HEADER(mf->nxm_header);
659 if (!strncmp("NXM_NX_COOKIE", name, name_len)
660 && (name_len == strlen("NXM_NX_COOKIE"))) {
662 return NXM_NX_COOKIE;
664 return NXM_NX_COOKIE_W;
668 /* Check whether it's a 32-bit field header value as hex.
669 * (This isn't ordinarily useful except for testing error behavior.) */
671 uint32_t header = hexits_value(name, name_len, NULL);
672 if (header != UINT_MAX) {
680 /* nx_match_from_string(). */
683 nx_match_from_string(const char *s, struct ofpbuf *b)
685 const char *full_s = s;
686 const size_t start_len = b->size;
689 if (!strcmp(s, "<any>")) {
690 /* Ensure that 'b->data' isn't actually null. */
691 ofpbuf_prealloc_tailroom(b, 1);
695 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
702 name_len = strcspn(s, "(");
703 if (s[name_len] != '(') {
704 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
707 header = parse_nxm_field_name(name, name_len);
709 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
714 nxm_put_header(b, header);
715 s = ofpbuf_put_hex(b, s, &n);
716 if (n != nxm_field_bytes(header)) {
717 ovs_fatal(0, "%.2s: hex digits expected", s);
719 if (NXM_HASMASK(header)) {
722 ovs_fatal(0, "%s: missing / in masked field %.*s",
723 full_s, name_len, name);
725 s = ofpbuf_put_hex(b, s + 1, &n);
726 if (n != nxm_field_bytes(header)) {
727 ovs_fatal(0, "%.2s: hex digits expected", s);
733 ovs_fatal(0, "%s: missing ) following field %.*s",
734 full_s, name_len, name);
739 match_len = b->size - start_len;
740 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
745 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
747 const char *full_s = s;
748 struct mf_subfield src, dst;
750 s = mf_parse_subfield(&src, s);
751 if (strncmp(s, "->", 2)) {
752 ovs_fatal(0, "%s: missing `->' following source", full_s);
755 s = mf_parse_subfield(&dst, s);
757 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
760 if (src.n_bits != dst.n_bits) {
761 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
762 "%d bits wide", full_s, src.n_bits, dst.n_bits);
765 ofputil_init_NXAST_REG_MOVE(move);
766 move->n_bits = htons(src.n_bits);
767 move->src_ofs = htons(src.ofs);
768 move->dst_ofs = htons(dst.ofs);
769 move->src = htonl(src.field->nxm_header);
770 move->dst = htonl(dst.field->nxm_header);
774 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
776 const char *full_s = s;
777 struct mf_subfield dst;
780 value = strtoull(s, (char **) &s, 0);
781 if (strncmp(s, "->", 2)) {
782 ovs_fatal(0, "%s: missing `->' following value", full_s);
785 s = mf_parse_subfield(&dst, s);
787 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
790 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
791 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %u bits",
792 full_s, value, dst.n_bits);
795 ofputil_init_NXAST_REG_LOAD(load);
796 load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
797 load->dst = htonl(dst.field->nxm_header);
798 load->value = htonll(value);
801 /* nxm_format_reg_move(), nxm_format_reg_load(). */
804 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
806 struct mf_subfield src, dst;
808 nxm_decode_discrete(&src, move->src, move->src_ofs, move->n_bits);
809 nxm_decode_discrete(&dst, move->dst, move->dst_ofs, move->n_bits);
811 ds_put_format(s, "move:");
812 mf_format_subfield(&src, s);
813 ds_put_cstr(s, "->");
814 mf_format_subfield(&dst, s);
818 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
820 struct mf_subfield dst;
822 ds_put_format(s, "load:%#"PRIx64"->", ntohll(load->value));
824 nxm_decode(&dst, load->dst, load->ofs_nbits);
825 mf_format_subfield(&dst, s);
828 /* nxm_check_reg_move(), nxm_check_reg_load(). */
831 nxm_check_reg_move(const struct nx_action_reg_move *action,
832 const struct flow *flow)
834 struct mf_subfield src;
835 struct mf_subfield dst;
838 nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
839 error = mf_check_src(&src, flow);
844 nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
845 return mf_check_dst(&dst, flow);
849 nxm_check_reg_load(const struct nx_action_reg_load *action,
850 const struct flow *flow)
852 struct mf_subfield dst;
855 nxm_decode(&dst, action->dst, action->ofs_nbits);
856 error = mf_check_dst(&dst, flow);
861 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
863 if (dst.n_bits < 64 && ntohll(action->value) >> dst.n_bits) {
864 return OFPERR_OFPBAC_BAD_ARGUMENT;
870 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
873 nxm_execute_reg_move(const struct nx_action_reg_move *action,
876 struct mf_subfield src, dst;
877 union mf_value src_value;
878 union mf_value dst_value;
880 nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
881 nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
883 mf_get_value(dst.field, flow, &dst_value);
884 mf_get_value(src.field, flow, &src_value);
885 bitwise_copy(&src_value, src.field->n_bytes, src.ofs,
886 &dst_value, dst.field->n_bytes, dst.ofs,
888 mf_set_flow_value(dst.field, &dst_value, flow);
892 nxm_execute_reg_load(const struct nx_action_reg_load *action,
895 struct mf_subfield dst;
897 nxm_decode(&dst, action->dst, action->ofs_nbits);
898 mf_set_subfield_value(&dst, ntohll(action->value), flow);
901 /* Initializes 'sf->field' with the field corresponding to the given NXM
902 * 'header' and 'sf->ofs' and 'sf->n_bits' decoded from 'ofs_nbits' with
903 * nxm_decode_ofs() and nxm_decode_n_bits(), respectively.
905 * Afterward, 'sf' might be invalid in a few different ways:
907 * - 'sf->field' will be NULL if 'header' is unknown.
909 * - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
911 * The caller should call mf_check_src() or mf_check_dst() to check for these
914 nxm_decode(struct mf_subfield *sf, ovs_be32 header, ovs_be16 ofs_nbits)
916 sf->field = mf_from_nxm_header(ntohl(header));
917 sf->ofs = nxm_decode_ofs(ofs_nbits);
918 sf->n_bits = nxm_decode_n_bits(ofs_nbits);
921 /* Initializes 'sf->field' with the field corresponding to the given NXM
922 * 'header' and 'sf->ofs' and 'sf->n_bits' from 'ofs' and 'n_bits',
925 * Afterward, 'sf' might be invalid in a few different ways:
927 * - 'sf->field' will be NULL if 'header' is unknown.
929 * - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
931 * The caller should call mf_check_src() or mf_check_dst() to check for these
934 nxm_decode_discrete(struct mf_subfield *sf, ovs_be32 header,
935 ovs_be16 ofs, ovs_be16 n_bits)
937 sf->field = mf_from_nxm_header(ntohl(header));
938 sf->ofs = ntohs(ofs);
939 sf->n_bits = ntohs(n_bits);