2 * Copyright (c) 2010, 2011 Nicira Networks.
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"
28 #include "openflow/nicira-ext.h"
30 #include "unaligned.h"
33 VLOG_DEFINE_THIS_MODULE(nx_match);
35 /* Rate limit for nx_match parse errors. These always indicate a bug in the
36 * peer and so there's not much point in showing a lot of them. */
37 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40 NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
41 NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
42 NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
43 NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
44 NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
45 NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
46 BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
49 /* Returns the width of the data for a field with the given 'header', in
52 nxm_field_bytes(uint32_t header)
54 unsigned int length = NXM_LENGTH(header);
55 return NXM_HASMASK(header) ? length / 2 : length;
58 /* Returns the width of the data for a field with the given 'header', in
61 nxm_field_bits(uint32_t header)
63 return nxm_field_bytes(header) * 8;
66 /* nx_pull_match() and helpers. */
69 nx_entry_ok(const void *p, unsigned int match_len)
71 unsigned int payload_len;
77 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
81 memcpy(&header_be, p, 4);
82 header = ntohl(header_be);
84 payload_len = NXM_LENGTH(header);
86 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
90 if (match_len < payload_len + 4) {
91 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
92 "%u bytes left in nx_match", payload_len + 4, match_len);
100 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
101 struct cls_rule *rule)
106 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
108 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
109 "multiple of 8, is longer than space in message (max "
110 "length %zu)", match_len, b->size);
111 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
114 cls_rule_init_catchall(rule, priority);
115 while ((header = nx_entry_ok(p, match_len)) != 0) {
116 unsigned length = NXM_LENGTH(header);
117 const struct mf_field *mf;
120 mf = mf_from_nxm_header(header);
122 error = NXM_BAD_TYPE;
123 } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
124 error = NXM_BAD_PREREQ;
125 } else if (!mf_is_all_wild(mf, &rule->wc)) {
126 error = NXM_DUP_TYPE;
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 = NXM_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 = NXM_BAD_MASK;
145 mf_set(mf, &value, &mask, rule);
151 char *msg = ofputil_error_to_string(error);
152 VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
153 "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
155 NXM_VENDOR(header), NXM_FIELD(header),
156 NXM_HASMASK(header), NXM_LENGTH(header),
164 match_len -= 4 + length;
167 return match_len ? NXM_INVALID : 0;
170 /* nx_put_match() and helpers.
172 * 'put' functions whose names end in 'w' add a wildcarded field.
173 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
174 * Other 'put' functions add exact-match fields.
178 nxm_put_header(struct ofpbuf *b, uint32_t header)
180 ovs_be32 n_header = htonl(header);
181 ofpbuf_put(b, &n_header, sizeof n_header);
185 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
187 nxm_put_header(b, header);
188 ofpbuf_put(b, &value, sizeof value);
192 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
199 nxm_put_8(b, header, value);
203 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
204 ofpbuf_put(b, &value, sizeof value);
205 ofpbuf_put(b, &mask, sizeof mask);
210 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
212 nxm_put_header(b, header);
213 ofpbuf_put(b, &value, sizeof value);
217 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
219 nxm_put_header(b, header);
220 ofpbuf_put(b, &value, sizeof value);
221 ofpbuf_put(b, &mask, sizeof mask);
225 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
231 case CONSTANT_HTONS(UINT16_MAX):
232 nxm_put_16(b, header, value);
236 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
242 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
244 nxm_put_header(b, header);
245 ofpbuf_put(b, &value, sizeof value);
249 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
251 nxm_put_header(b, header);
252 ofpbuf_put(b, &value, sizeof value);
253 ofpbuf_put(b, &mask, sizeof mask);
257 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
263 case CONSTANT_HTONL(UINT32_MAX):
264 nxm_put_32(b, header, value);
268 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
274 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
276 nxm_put_header(b, header);
277 ofpbuf_put(b, &value, sizeof value);
281 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
283 nxm_put_header(b, header);
284 ofpbuf_put(b, &value, sizeof value);
285 ofpbuf_put(b, &mask, sizeof mask);
289 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
295 case CONSTANT_HTONLL(UINT64_MAX):
296 nxm_put_64(b, header, value);
300 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
306 nxm_put_eth(struct ofpbuf *b, uint32_t header,
307 const uint8_t value[ETH_ADDR_LEN])
309 nxm_put_header(b, header);
310 ofpbuf_put(b, value, ETH_ADDR_LEN);
314 nxm_put_eth_dst(struct ofpbuf *b,
315 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
317 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
318 case FWW_DL_DST | FWW_ETH_MCAST:
321 nxm_put_header(b, NXM_OF_ETH_DST_W);
322 ofpbuf_put(b, value, ETH_ADDR_LEN);
323 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
326 nxm_put_eth(b, NXM_OF_ETH_DST, value);
332 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
333 const struct in6_addr *value, const struct in6_addr *mask)
335 if (ipv6_mask_is_any(mask)) {
337 } else if (ipv6_mask_is_exact(mask)) {
338 nxm_put_header(b, header);
339 ofpbuf_put(b, value, sizeof *value);
341 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
342 ofpbuf_put(b, value, sizeof *value);
343 ofpbuf_put(b, mask, sizeof *mask);
348 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
350 uint8_t nw_frag = cr->flow.nw_frag;
351 uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
353 switch (nw_frag_mask) {
357 case FLOW_NW_FRAG_MASK:
358 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
362 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
363 nw_frag_mask & FLOW_NW_FRAG_MASK);
368 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
369 * 'cr->priority', because priority is not part of nx_match), plus enough
370 * zero bytes to pad the nx_match out to a multiple of 8.
372 * This function can cause 'b''s data to be reallocated.
374 * Returns the number of bytes appended to 'b', excluding padding.
376 * If 'cr' is a catch-all rule that matches every packet, then this function
377 * appends nothing to 'b' and returns 0. */
379 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
381 const flow_wildcards_t wc = cr->wc.wildcards;
382 const struct flow *flow = &cr->flow;
383 const size_t start_len = b->size;
387 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
390 if (!(wc & FWW_IN_PORT)) {
391 uint16_t in_port = flow->in_port;
392 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
396 nxm_put_eth_dst(b, wc, flow->dl_dst);
397 if (!(wc & FWW_DL_SRC)) {
398 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
400 if (!(wc & FWW_DL_TYPE)) {
401 nxm_put_16(b, NXM_OF_ETH_TYPE,
402 ofputil_dl_type_to_openflow(flow->dl_type));
406 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
409 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
411 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
412 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
415 if (!(wc & FWW_NW_DSCP)) {
416 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
419 if (!(wc & FWW_NW_ECN)) {
420 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
423 if (!(wc & FWW_NW_TTL)) {
424 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
427 if (!(wc & FWW_NW_PROTO)) {
428 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
429 switch (flow->nw_proto) {
432 if (!(wc & FWW_TP_SRC)) {
433 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
435 if (!(wc & FWW_TP_DST)) {
436 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
442 if (!(wc & FWW_TP_SRC)) {
443 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
445 if (!(wc & FWW_TP_DST)) {
446 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
452 if (!(wc & FWW_TP_SRC)) {
453 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
455 if (!(wc & FWW_TP_DST)) {
456 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
461 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
463 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
464 &cr->wc.ipv6_src_mask);
465 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
466 &cr->wc.ipv6_dst_mask);
469 if (!(wc & FWW_IPV6_LABEL)) {
470 nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
473 if (!(wc & FWW_NW_DSCP)) {
474 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
477 if (!(wc & FWW_NW_ECN)) {
478 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
481 if (!(wc & FWW_NW_TTL)) {
482 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
485 if (!(wc & FWW_NW_PROTO)) {
486 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
487 switch (flow->nw_proto) {
490 if (!(wc & FWW_TP_SRC)) {
491 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
493 if (!(wc & FWW_TP_DST)) {
494 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
500 if (!(wc & FWW_TP_SRC)) {
501 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
503 if (!(wc & FWW_TP_DST)) {
504 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
510 if (!(wc & FWW_TP_SRC)) {
511 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
513 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
514 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
515 if (!(wc & FWW_ND_TARGET)) {
516 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
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);
529 if (!(wc & FWW_TP_DST)) {
530 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
535 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
537 if (!(wc & FWW_NW_PROTO)) {
538 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
540 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
541 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
542 if (!(wc & FWW_ARP_SHA)) {
543 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
545 if (!(wc & FWW_ARP_THA)) {
546 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
551 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
554 for (i = 0; i < FLOW_N_REGS; i++) {
555 nxm_put_32m(b, NXM_NX_REG(i),
556 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
559 match_len = b->size - start_len;
560 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
564 /* nx_match_to_string() and helpers. */
566 static void format_nxm_field_name(struct ds *, uint32_t header);
569 nx_match_to_string(const uint8_t *p, unsigned int match_len)
575 return xstrdup("<any>");
579 while ((header = nx_entry_ok(p, match_len)) != 0) {
580 unsigned int length = NXM_LENGTH(header);
581 unsigned int value_len = nxm_field_bytes(header);
582 const uint8_t *value = p + 4;
583 const uint8_t *mask = value + value_len;
587 ds_put_cstr(&s, ", ");
590 format_nxm_field_name(&s, header);
591 ds_put_char(&s, '(');
593 for (i = 0; i < value_len; i++) {
594 ds_put_format(&s, "%02x", value[i]);
596 if (NXM_HASMASK(header)) {
597 ds_put_char(&s, '/');
598 for (i = 0; i < value_len; i++) {
599 ds_put_format(&s, "%02x", mask[i]);
602 ds_put_char(&s, ')');
605 match_len -= 4 + length;
610 ds_put_cstr(&s, ", ");
613 ds_put_format(&s, "<%u invalid bytes>", match_len);
616 return ds_steal_cstr(&s);
620 format_nxm_field_name(struct ds *s, uint32_t header)
622 const struct mf_field *mf = mf_from_nxm_header(header);
624 ds_put_cstr(s, mf->nxm_name);
625 if (NXM_HASMASK(header)) {
626 ds_put_cstr(s, "_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);
644 for (i = 0; i < MFF_N_IDS; i++) {
645 const struct mf_field *mf = mf_from_id(i);
648 && !strncmp(mf->nxm_name, name, name_len)
649 && mf->nxm_name[name_len] == '\0') {
651 return mf->nxm_header;
652 } else if (mf->maskable != MFM_NONE) {
653 return NXM_MAKE_WILD_HEADER(mf->nxm_header);
658 /* Check whether it's a 32-bit field header value as hex.
659 * (This isn't ordinarily useful except for testing error behavior.) */
661 uint32_t header = hexits_value(name, name_len, NULL);
662 if (header != UINT_MAX) {
670 /* nx_match_from_string(). */
673 nx_match_from_string(const char *s, struct ofpbuf *b)
675 const char *full_s = s;
676 const size_t start_len = b->size;
679 if (!strcmp(s, "<any>")) {
680 /* Ensure that 'b->data' isn't actually null. */
681 ofpbuf_prealloc_tailroom(b, 1);
685 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
692 name_len = strcspn(s, "(");
693 if (s[name_len] != '(') {
694 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
697 header = parse_nxm_field_name(name, name_len);
699 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
704 nxm_put_header(b, header);
705 s = ofpbuf_put_hex(b, s, &n);
706 if (n != nxm_field_bytes(header)) {
707 ovs_fatal(0, "%.2s: hex digits expected", s);
709 if (NXM_HASMASK(header)) {
712 ovs_fatal(0, "%s: missing / in masked field %.*s",
713 full_s, name_len, name);
715 s = ofpbuf_put_hex(b, s + 1, &n);
716 if (n != nxm_field_bytes(header)) {
717 ovs_fatal(0, "%.2s: hex digits expected", s);
723 ovs_fatal(0, "%s: missing ) following field %.*s",
724 full_s, name_len, name);
729 match_len = b->size - start_len;
730 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
735 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
737 const char *full_s = s;
745 name_len = strcspn(s, "[");
746 if (s[name_len] != '[') {
747 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
750 header = parse_nxm_field_name(name, name_len);
752 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
754 width = nxm_field_bits(header);
757 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
759 } else if (sscanf(s, "[%d]", &start) == 1) {
761 } else if (!strncmp(s, "[]", 2)) {
765 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
766 "[<start>..<end>]", full_s);
768 s = strchr(s, ']') + 1;
771 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
773 } else if (start >= width) {
774 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
775 "%d bits wide", full_s, start, width);
776 } else if (end >= width){
777 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
778 "%d bits wide", full_s, end, width);
783 *n_bitsp = end - start + 1;
789 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
791 const char *full_s = s;
793 int src_ofs, dst_ofs;
794 int src_n_bits, dst_n_bits;
796 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
797 if (strncmp(s, "->", 2)) {
798 ovs_fatal(0, "%s: missing `->' following source", full_s);
801 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
803 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
806 if (src_n_bits != dst_n_bits) {
807 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
808 "%d bits wide", full_s, src_n_bits, dst_n_bits);
811 ofputil_init_NXAST_REG_MOVE(move);
812 move->n_bits = htons(src_n_bits);
813 move->src_ofs = htons(src_ofs);
814 move->dst_ofs = htons(dst_ofs);
815 move->src = htonl(src);
816 move->dst = htonl(dst);
820 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
822 const char *full_s = s;
827 value = strtoull(s, (char **) &s, 0);
828 if (strncmp(s, "->", 2)) {
829 ovs_fatal(0, "%s: missing `->' following value", full_s);
832 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
834 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
837 if (n_bits < 64 && (value >> n_bits) != 0) {
838 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
839 full_s, value, n_bits);
842 ofputil_init_NXAST_REG_LOAD(load);
843 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
844 load->dst = htonl(dst);
845 load->value = htonll(value);
848 /* nxm_format_reg_move(), nxm_format_reg_load(). */
851 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
853 format_nxm_field_name(s, header);
854 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
855 ds_put_cstr(s, "[]");
856 } else if (n_bits == 1) {
857 ds_put_format(s, "[%d]", ofs);
859 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
864 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
866 int n_bits = ntohs(move->n_bits);
867 int src_ofs = ntohs(move->src_ofs);
868 int dst_ofs = ntohs(move->dst_ofs);
869 uint32_t src = ntohl(move->src);
870 uint32_t dst = ntohl(move->dst);
872 ds_put_format(s, "move:");
873 nxm_format_field_bits(s, src, src_ofs, n_bits);
874 ds_put_cstr(s, "->");
875 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
879 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
881 int ofs = nxm_decode_ofs(load->ofs_nbits);
882 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
883 uint32_t dst = ntohl(load->dst);
884 uint64_t value = ntohll(load->value);
886 ds_put_format(s, "load:%#"PRIx64"->", value);
887 nxm_format_field_bits(s, dst, ofs, n_bits);
890 /* nxm_check_reg_move(), nxm_check_reg_load(). */
893 field_ok(const struct mf_field *mf, const struct flow *flow, int size)
896 && mf_are_prereqs_ok(mf, flow)
897 && size <= nxm_field_bits(mf->nxm_header));
901 nxm_check_reg_move(const struct nx_action_reg_move *action,
902 const struct flow *flow)
904 int src_ofs, dst_ofs, n_bits;
907 n_bits = ntohs(action->n_bits);
908 src_ofs = ntohs(action->src_ofs);
909 dst_ofs = ntohs(action->dst_ofs);
911 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
916 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
919 /* Given a flow, checks that the source field represented by 'src_header'
920 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
922 nxm_src_check(ovs_be32 src_header_, unsigned int ofs, unsigned int n_bits,
923 const struct flow *flow)
925 uint32_t src_header = ntohl(src_header_);
926 const struct mf_field *src = mf_from_nxm_header(src_header);
929 VLOG_WARN_RL(&rl, "zero bit source field");
930 } else if (NXM_HASMASK(src_header) || !field_ok(src, flow, ofs + n_bits)) {
931 VLOG_WARN_RL(&rl, "invalid source field");
939 /* Given a flow, checks that the destination field represented by 'dst_header'
940 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
942 nxm_dst_check(ovs_be32 dst_header_, unsigned int ofs, unsigned int n_bits,
943 const struct flow *flow)
945 uint32_t dst_header = ntohl(dst_header_);
946 const struct mf_field *dst = mf_from_nxm_header(dst_header);
949 VLOG_WARN_RL(&rl, "zero bit destination field");
950 } else if (NXM_HASMASK(dst_header) || !field_ok(dst, flow, ofs + n_bits)) {
951 VLOG_WARN_RL(&rl, "invalid destination field");
952 } else if (!dst->writable) {
953 VLOG_WARN_RL(&rl, "destination field is not writable");
962 nxm_check_reg_load(const struct nx_action_reg_load *action,
963 const struct flow *flow)
965 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
966 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
969 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
974 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
976 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
983 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
986 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
987 void *dst_, unsigned int dst_len, unsigned int dst_ofs,
990 const uint8_t *src = src_;
993 src += src_len - (src_ofs / 8 + 1);
996 dst += dst_len - (dst_ofs / 8 + 1);
999 if (src_ofs == 0 && dst_ofs == 0) {
1000 unsigned int n_bytes = n_bits / 8;
1004 memcpy(dst, src, n_bytes);
1011 uint8_t mask = (1 << n_bits) - 1;
1012 *dst = (*dst & ~mask) | (*src & mask);
1015 while (n_bits > 0) {
1016 unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
1017 unsigned int chunk = MIN(n_bits, max_copy);
1018 uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
1021 *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
1038 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1041 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1042 const struct flow *flow)
1044 const struct mf_field *field = mf_from_nxm_header(ntohl(header));
1045 union mf_value value;
1046 union mf_value bits;
1048 mf_get_value(field, flow, &value);
1049 bits.be64 = htonll(0);
1050 bitwise_copy(&value, field->n_bytes, nxm_decode_ofs(ofs_nbits),
1051 &bits, sizeof bits.be64, 0,
1052 nxm_decode_n_bits(ofs_nbits));
1053 return ntohll(bits.be64);
1057 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1060 const struct mf_field *src = mf_from_nxm_header(ntohl(action->src));
1061 const struct mf_field *dst = mf_from_nxm_header(ntohl(action->dst));
1062 union mf_value src_value;
1063 union mf_value dst_value;
1065 mf_get_value(dst, flow, &dst_value);
1066 mf_get_value(src, flow, &src_value);
1067 bitwise_copy(&src_value, src->n_bytes, ntohs(action->src_ofs),
1068 &dst_value, dst->n_bytes, ntohs(action->dst_ofs),
1069 ntohs(action->n_bits));
1070 mf_set_flow_value(dst, &dst_value, flow);
1074 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1077 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1080 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1081 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1083 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1086 const struct mf_field *dst = mf_from_nxm_header(ntohl(dst_header));
1087 int n_bits = nxm_decode_n_bits(ofs_nbits);
1088 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1089 union mf_value dst_value;
1090 union mf_value src_value;
1092 mf_get_value(dst, flow, &dst_value);
1093 src_value.be64 = htonll(src_data);
1094 bitwise_copy(&src_value, sizeof src_value.be64, 0,
1095 &dst_value, dst->n_bytes, dst_ofs,
1097 mf_set_flow_value(dst, &dst_value, flow);