2 * Copyright (c) 2010, 2011, 2012 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"
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_OFPBRC_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_NXBRC_NXM_BAD_TYPE;
126 } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
127 error = OFPERR_NXBRC_NXM_BAD_PREREQ;
128 } else if (!mf_is_all_wild(mf, &rule->wc)) {
129 error = OFPERR_NXBRC_NXM_DUP_TYPE;
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_NXBRC_NXM_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_NXBRC_NXM_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_NXBRC_NXM_DUP_TYPE;
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_NXBRC_NXM_INVALID : 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);
411 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
412 * 'cr->priority', because priority is not part of nx_match), plus enough
413 * zero bytes to pad the nx_match out to a multiple of 8. For Flow Mod
414 * and Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be
415 * supplied. Otherwise, 'cookie_mask' should be zero.
417 * This function can cause 'b''s data to be reallocated.
419 * Returns the number of bytes appended to 'b', excluding padding.
421 * If 'cr' is a catch-all rule that matches every packet, then this function
422 * appends nothing to 'b' and returns 0. */
424 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr,
425 ovs_be64 cookie, ovs_be64 cookie_mask)
427 const flow_wildcards_t wc = cr->wc.wildcards;
428 const struct flow *flow = &cr->flow;
429 const size_t start_len = b->size;
433 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
436 if (!(wc & FWW_IN_PORT)) {
437 uint16_t in_port = flow->in_port;
438 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
442 nxm_put_eth_dst(b, wc, flow->dl_dst);
443 if (!(wc & FWW_DL_SRC)) {
444 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
446 if (!(wc & FWW_DL_TYPE)) {
447 nxm_put_16(b, NXM_OF_ETH_TYPE,
448 ofputil_dl_type_to_openflow(flow->dl_type));
452 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
455 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
457 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
458 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
461 if (!(wc & FWW_NW_DSCP)) {
462 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
465 if (!(wc & FWW_NW_ECN)) {
466 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
469 if (!(wc & FWW_NW_TTL)) {
470 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
473 if (!(wc & FWW_NW_PROTO)) {
474 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
475 switch (flow->nw_proto) {
478 if (!(wc & FWW_TP_SRC)) {
479 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
481 if (!(wc & FWW_TP_DST)) {
482 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
488 if (!(wc & FWW_TP_SRC)) {
489 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
491 if (!(wc & FWW_TP_DST)) {
492 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
498 if (!(wc & FWW_TP_SRC)) {
499 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
501 if (!(wc & FWW_TP_DST)) {
502 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
507 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
509 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
510 &cr->wc.ipv6_src_mask);
511 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
512 &cr->wc.ipv6_dst_mask);
515 if (!(wc & FWW_IPV6_LABEL)) {
516 nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
519 if (!(wc & FWW_NW_DSCP)) {
520 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
523 if (!(wc & FWW_NW_ECN)) {
524 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
527 if (!(wc & FWW_NW_TTL)) {
528 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
531 if (!(wc & FWW_NW_PROTO)) {
532 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
533 switch (flow->nw_proto) {
536 if (!(wc & FWW_TP_SRC)) {
537 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
539 if (!(wc & FWW_TP_DST)) {
540 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
546 if (!(wc & FWW_TP_SRC)) {
547 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
549 if (!(wc & FWW_TP_DST)) {
550 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
556 if (!(wc & FWW_TP_SRC)) {
557 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
559 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
560 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
561 if (!(wc & FWW_ND_TARGET)) {
562 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
565 if (!(wc & FWW_ARP_SHA)
566 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
567 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
569 if (!(wc & FWW_ARP_THA)
570 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
571 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
575 if (!(wc & FWW_TP_DST)) {
576 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
581 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
583 if (!(wc & FWW_NW_PROTO)) {
584 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
586 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
587 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
588 if (!(wc & FWW_ARP_SHA)) {
589 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
591 if (!(wc & FWW_ARP_THA)) {
592 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
597 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
600 for (i = 0; i < FLOW_N_REGS; i++) {
601 nxm_put_32m(b, NXM_NX_REG(i),
602 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
606 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
608 match_len = b->size - start_len;
609 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
613 /* nx_match_to_string() and helpers. */
615 static void format_nxm_field_name(struct ds *, uint32_t header);
618 nx_match_to_string(const uint8_t *p, unsigned int match_len)
624 return xstrdup("<any>");
628 while ((header = nx_entry_ok(p, match_len)) != 0) {
629 unsigned int length = NXM_LENGTH(header);
630 unsigned int value_len = nxm_field_bytes(header);
631 const uint8_t *value = p + 4;
632 const uint8_t *mask = value + value_len;
636 ds_put_cstr(&s, ", ");
639 format_nxm_field_name(&s, header);
640 ds_put_char(&s, '(');
642 for (i = 0; i < value_len; i++) {
643 ds_put_format(&s, "%02x", value[i]);
645 if (NXM_HASMASK(header)) {
646 ds_put_char(&s, '/');
647 for (i = 0; i < value_len; i++) {
648 ds_put_format(&s, "%02x", mask[i]);
651 ds_put_char(&s, ')');
654 match_len -= 4 + length;
659 ds_put_cstr(&s, ", ");
662 ds_put_format(&s, "<%u invalid bytes>", match_len);
665 return ds_steal_cstr(&s);
669 format_nxm_field_name(struct ds *s, uint32_t header)
671 const struct mf_field *mf = mf_from_nxm_header(header);
673 ds_put_cstr(s, mf->nxm_name);
674 if (NXM_HASMASK(header)) {
675 ds_put_cstr(s, "_W");
677 } else if (header == NXM_NX_COOKIE) {
678 ds_put_cstr(s, "NXM_NX_COOKIE");
679 } else if (header == NXM_NX_COOKIE_W) {
680 ds_put_cstr(s, "NXM_NX_COOKIE_W");
682 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
687 parse_nxm_field_name(const char *name, int name_len)
692 /* Check whether it's a field name. */
693 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
698 for (i = 0; i < MFF_N_IDS; i++) {
699 const struct mf_field *mf = mf_from_id(i);
702 && !strncmp(mf->nxm_name, name, name_len)
703 && mf->nxm_name[name_len] == '\0') {
705 return mf->nxm_header;
706 } else if (mf->maskable != MFM_NONE) {
707 return NXM_MAKE_WILD_HEADER(mf->nxm_header);
712 if (!strncmp("NXM_NX_COOKIE", name, name_len)
713 && (name_len == strlen("NXM_NX_COOKIE"))) {
715 return NXM_NX_COOKIE;
717 return NXM_NX_COOKIE_W;
721 /* Check whether it's a 32-bit field header value as hex.
722 * (This isn't ordinarily useful except for testing error behavior.) */
724 uint32_t header = hexits_value(name, name_len, NULL);
725 if (header != UINT_MAX) {
733 /* nx_match_from_string(). */
736 nx_match_from_string(const char *s, struct ofpbuf *b)
738 const char *full_s = s;
739 const size_t start_len = b->size;
742 if (!strcmp(s, "<any>")) {
743 /* Ensure that 'b->data' isn't actually null. */
744 ofpbuf_prealloc_tailroom(b, 1);
748 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
755 name_len = strcspn(s, "(");
756 if (s[name_len] != '(') {
757 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
760 header = parse_nxm_field_name(name, name_len);
762 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
767 nxm_put_header(b, header);
768 s = ofpbuf_put_hex(b, s, &n);
769 if (n != nxm_field_bytes(header)) {
770 ovs_fatal(0, "%.2s: hex digits expected", s);
772 if (NXM_HASMASK(header)) {
775 ovs_fatal(0, "%s: missing / in masked field %.*s",
776 full_s, name_len, name);
778 s = ofpbuf_put_hex(b, s + 1, &n);
779 if (n != nxm_field_bytes(header)) {
780 ovs_fatal(0, "%.2s: hex digits expected", s);
786 ovs_fatal(0, "%s: missing ) following field %.*s",
787 full_s, name_len, name);
792 match_len = b->size - start_len;
793 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
798 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
800 const char *full_s = s;
808 name_len = strcspn(s, "[");
809 if (s[name_len] != '[') {
810 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
813 header = parse_nxm_field_name(name, name_len);
815 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
817 width = nxm_field_bits(header);
820 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
822 } else if (sscanf(s, "[%d]", &start) == 1) {
824 } else if (!strncmp(s, "[]", 2)) {
828 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
829 "[<start>..<end>]", full_s);
831 s = strchr(s, ']') + 1;
834 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
836 } else if (start >= width) {
837 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
838 "%d bits wide", full_s, start, width);
839 } else if (end >= width){
840 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
841 "%d bits wide", full_s, end, width);
846 *n_bitsp = end - start + 1;
852 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
854 const char *full_s = s;
856 int src_ofs, dst_ofs;
857 int src_n_bits, dst_n_bits;
859 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
860 if (strncmp(s, "->", 2)) {
861 ovs_fatal(0, "%s: missing `->' following source", full_s);
864 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
866 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
869 if (src_n_bits != dst_n_bits) {
870 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
871 "%d bits wide", full_s, src_n_bits, dst_n_bits);
874 ofputil_init_NXAST_REG_MOVE(move);
875 move->n_bits = htons(src_n_bits);
876 move->src_ofs = htons(src_ofs);
877 move->dst_ofs = htons(dst_ofs);
878 move->src = htonl(src);
879 move->dst = htonl(dst);
883 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
885 const char *full_s = s;
890 value = strtoull(s, (char **) &s, 0);
891 if (strncmp(s, "->", 2)) {
892 ovs_fatal(0, "%s: missing `->' following value", full_s);
895 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
897 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
900 if (n_bits < 64 && (value >> n_bits) != 0) {
901 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
902 full_s, value, n_bits);
905 ofputil_init_NXAST_REG_LOAD(load);
906 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
907 load->dst = htonl(dst);
908 load->value = htonll(value);
911 /* nxm_format_reg_move(), nxm_format_reg_load(). */
914 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
916 format_nxm_field_name(s, header);
917 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
918 ds_put_cstr(s, "[]");
919 } else if (n_bits == 1) {
920 ds_put_format(s, "[%d]", ofs);
922 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
927 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
929 int n_bits = ntohs(move->n_bits);
930 int src_ofs = ntohs(move->src_ofs);
931 int dst_ofs = ntohs(move->dst_ofs);
932 uint32_t src = ntohl(move->src);
933 uint32_t dst = ntohl(move->dst);
935 ds_put_format(s, "move:");
936 nxm_format_field_bits(s, src, src_ofs, n_bits);
937 ds_put_cstr(s, "->");
938 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
942 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
944 int ofs = nxm_decode_ofs(load->ofs_nbits);
945 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
946 uint32_t dst = ntohl(load->dst);
947 uint64_t value = ntohll(load->value);
949 ds_put_format(s, "load:%#"PRIx64"->", value);
950 nxm_format_field_bits(s, dst, ofs, n_bits);
953 /* nxm_check_reg_move(), nxm_check_reg_load(). */
956 field_ok(const struct mf_field *mf, const struct flow *flow, int size)
959 && mf_are_prereqs_ok(mf, flow)
960 && size <= nxm_field_bits(mf->nxm_header));
964 nxm_check_reg_move(const struct nx_action_reg_move *action,
965 const struct flow *flow)
967 int src_ofs, dst_ofs, n_bits;
970 n_bits = ntohs(action->n_bits);
971 src_ofs = ntohs(action->src_ofs);
972 dst_ofs = ntohs(action->dst_ofs);
974 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
979 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
982 /* Given a flow, checks that the source field represented by 'src_header'
983 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
985 nxm_src_check(ovs_be32 src_header_, unsigned int ofs, unsigned int n_bits,
986 const struct flow *flow)
988 uint32_t src_header = ntohl(src_header_);
989 const struct mf_field *src = mf_from_nxm_header(src_header);
992 VLOG_WARN_RL(&rl, "zero bit source field");
993 } else if (NXM_HASMASK(src_header) || !field_ok(src, flow, ofs + n_bits)) {
994 VLOG_WARN_RL(&rl, "invalid source field");
999 return OFPERR_OFPBAC_BAD_ARGUMENT;
1002 /* Given a flow, checks that the destination field represented by 'dst_header'
1003 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
1005 nxm_dst_check(ovs_be32 dst_header_, unsigned int ofs, unsigned int n_bits,
1006 const struct flow *flow)
1008 uint32_t dst_header = ntohl(dst_header_);
1009 const struct mf_field *dst = mf_from_nxm_header(dst_header);
1012 VLOG_WARN_RL(&rl, "zero bit destination field");
1013 } else if (NXM_HASMASK(dst_header) || !field_ok(dst, flow, ofs + n_bits)) {
1014 VLOG_WARN_RL(&rl, "invalid destination field");
1015 } else if (!dst->writable) {
1016 VLOG_WARN_RL(&rl, "destination field is not writable");
1021 return OFPERR_OFPBAC_BAD_ARGUMENT;
1025 nxm_check_reg_load(const struct nx_action_reg_load *action,
1026 const struct flow *flow)
1028 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
1029 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1032 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
1037 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1039 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1040 return OFPERR_OFPBAC_BAD_ARGUMENT;
1046 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1048 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1051 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1052 const struct flow *flow)
1054 const struct mf_field *field = mf_from_nxm_header(ntohl(header));
1055 union mf_value value;
1056 union mf_value bits;
1058 mf_get_value(field, flow, &value);
1059 bits.be64 = htonll(0);
1060 bitwise_copy(&value, field->n_bytes, nxm_decode_ofs(ofs_nbits),
1061 &bits, sizeof bits.be64, 0,
1062 nxm_decode_n_bits(ofs_nbits));
1063 return ntohll(bits.be64);
1067 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1070 const struct mf_field *src = mf_from_nxm_header(ntohl(action->src));
1071 const struct mf_field *dst = mf_from_nxm_header(ntohl(action->dst));
1072 union mf_value src_value;
1073 union mf_value dst_value;
1075 mf_get_value(dst, flow, &dst_value);
1076 mf_get_value(src, flow, &src_value);
1077 bitwise_copy(&src_value, src->n_bytes, ntohs(action->src_ofs),
1078 &dst_value, dst->n_bytes, ntohs(action->dst_ofs),
1079 ntohs(action->n_bits));
1080 mf_set_flow_value(dst, &dst_value, flow);
1084 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1087 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1090 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1091 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1093 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1096 const struct mf_field *dst = mf_from_nxm_header(ntohl(dst_header));
1097 int n_bits = nxm_decode_n_bits(ofs_nbits);
1098 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1099 union mf_value dst_value;
1100 union mf_value src_value;
1102 mf_get_value(dst, flow, &dst_value);
1103 src_value.be64 = htonll(src_data);
1104 bitwise_copy(&src_value, sizeof src_value.be64, 0,
1105 &dst_value, dst->n_bytes, dst_ofs,
1107 mf_set_flow_value(dst, &dst_value, flow);