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);
99 /* Parses the nx_match formatted match description in 'b' with length
100 * 'match_len'. The results are stored in 'rule', which is initialized
101 * with 'priority'. If 'cookie' and 'cookie_mask' contain valid
102 * pointers, then the cookie and mask will be stored in them if a
103 * "NXM_NX_COOKIE*" match is defined. Otherwise, 0 is stored in both.
105 * Returns 0 if successful, otherwise an OpenFlow error code.
108 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
109 struct cls_rule *rule,
110 ovs_be64 *cookie, ovs_be64 *cookie_mask)
115 assert((cookie != NULL) == (cookie_mask != NULL));
117 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
119 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
120 "multiple of 8, is longer than space in message (max "
121 "length %zu)", match_len, b->size);
122 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
125 cls_rule_init_catchall(rule, priority);
127 *cookie = *cookie_mask = htonll(0);
129 while ((header = nx_entry_ok(p, match_len)) != 0) {
130 unsigned length = NXM_LENGTH(header);
131 const struct mf_field *mf;
134 mf = mf_from_nxm_header(header);
136 error = NXM_BAD_TYPE;
137 } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
138 error = NXM_BAD_PREREQ;
139 } else if (!mf_is_all_wild(mf, &rule->wc)) {
140 error = NXM_DUP_TYPE;
142 unsigned int width = mf->n_bytes;
143 union mf_value value;
145 memcpy(&value, p + 4, width);
146 if (!mf_is_value_valid(mf, &value)) {
147 error = NXM_BAD_VALUE;
148 } else if (!NXM_HASMASK(header)) {
150 mf_set_value(mf, &value, rule);
154 memcpy(&mask, p + 4 + width, width);
155 if (!mf_is_mask_valid(mf, &mask)) {
156 error = NXM_BAD_MASK;
159 mf_set(mf, &value, &mask, rule);
164 /* Check if the match is for a cookie rather than a classifier rule. */
165 if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
167 error = NXM_DUP_TYPE;
169 unsigned int width = sizeof *cookie;
171 memcpy(cookie, p + 4, width);
172 if (NXM_HASMASK(header)) {
173 memcpy(cookie_mask, p + 4 + width, width);
175 *cookie_mask = htonll(UINT64_MAX);
182 char *msg = ofputil_error_to_string(error);
183 VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
184 "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
186 NXM_VENDOR(header), NXM_FIELD(header),
187 NXM_HASMASK(header), NXM_LENGTH(header),
195 match_len -= 4 + length;
198 return match_len ? NXM_INVALID : 0;
201 /* nx_put_match() and helpers.
203 * 'put' functions whose names end in 'w' add a wildcarded field.
204 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
205 * Other 'put' functions add exact-match fields.
209 nxm_put_header(struct ofpbuf *b, uint32_t header)
211 ovs_be32 n_header = htonl(header);
212 ofpbuf_put(b, &n_header, sizeof n_header);
216 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
218 nxm_put_header(b, header);
219 ofpbuf_put(b, &value, sizeof value);
223 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
230 nxm_put_8(b, header, value);
234 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
235 ofpbuf_put(b, &value, sizeof value);
236 ofpbuf_put(b, &mask, sizeof mask);
241 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
243 nxm_put_header(b, header);
244 ofpbuf_put(b, &value, sizeof value);
248 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
250 nxm_put_header(b, header);
251 ofpbuf_put(b, &value, sizeof value);
252 ofpbuf_put(b, &mask, sizeof mask);
256 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
262 case CONSTANT_HTONS(UINT16_MAX):
263 nxm_put_16(b, header, value);
267 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
273 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
275 nxm_put_header(b, header);
276 ofpbuf_put(b, &value, sizeof value);
280 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
282 nxm_put_header(b, header);
283 ofpbuf_put(b, &value, sizeof value);
284 ofpbuf_put(b, &mask, sizeof mask);
288 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
294 case CONSTANT_HTONL(UINT32_MAX):
295 nxm_put_32(b, header, value);
299 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
305 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
307 nxm_put_header(b, header);
308 ofpbuf_put(b, &value, sizeof value);
312 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
314 nxm_put_header(b, header);
315 ofpbuf_put(b, &value, sizeof value);
316 ofpbuf_put(b, &mask, sizeof mask);
320 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
326 case CONSTANT_HTONLL(UINT64_MAX):
327 nxm_put_64(b, header, value);
331 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
337 nxm_put_eth(struct ofpbuf *b, uint32_t header,
338 const uint8_t value[ETH_ADDR_LEN])
340 nxm_put_header(b, header);
341 ofpbuf_put(b, value, ETH_ADDR_LEN);
345 nxm_put_eth_dst(struct ofpbuf *b,
346 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
348 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
349 case FWW_DL_DST | FWW_ETH_MCAST:
352 nxm_put_header(b, NXM_OF_ETH_DST_W);
353 ofpbuf_put(b, value, ETH_ADDR_LEN);
354 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
357 nxm_put_eth(b, NXM_OF_ETH_DST, value);
363 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
364 const struct in6_addr *value, const struct in6_addr *mask)
366 if (ipv6_mask_is_any(mask)) {
368 } else if (ipv6_mask_is_exact(mask)) {
369 nxm_put_header(b, header);
370 ofpbuf_put(b, value, sizeof *value);
372 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
373 ofpbuf_put(b, value, sizeof *value);
374 ofpbuf_put(b, mask, sizeof *mask);
379 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
381 uint8_t nw_frag = cr->flow.nw_frag;
382 uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
384 switch (nw_frag_mask) {
388 case FLOW_NW_FRAG_MASK:
389 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
393 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
394 nw_frag_mask & FLOW_NW_FRAG_MASK);
399 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
400 * 'cr->priority', because priority is not part of nx_match), plus enough
401 * zero bytes to pad the nx_match out to a multiple of 8. For Flow Mod
402 * and Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be
403 * supplied. Otherwise, 'cookie_mask' should be zero.
405 * This function can cause 'b''s data to be reallocated.
407 * Returns the number of bytes appended to 'b', excluding padding.
409 * If 'cr' is a catch-all rule that matches every packet, then this function
410 * appends nothing to 'b' and returns 0. */
412 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr,
413 ovs_be64 cookie, ovs_be64 cookie_mask)
415 const flow_wildcards_t wc = cr->wc.wildcards;
416 const struct flow *flow = &cr->flow;
417 const size_t start_len = b->size;
421 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
424 if (!(wc & FWW_IN_PORT)) {
425 uint16_t in_port = flow->in_port;
426 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
430 nxm_put_eth_dst(b, wc, flow->dl_dst);
431 if (!(wc & FWW_DL_SRC)) {
432 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
434 if (!(wc & FWW_DL_TYPE)) {
435 nxm_put_16(b, NXM_OF_ETH_TYPE,
436 ofputil_dl_type_to_openflow(flow->dl_type));
440 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
443 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
445 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
446 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
449 if (!(wc & FWW_NW_DSCP)) {
450 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
453 if (!(wc & FWW_NW_ECN)) {
454 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
457 if (!(wc & FWW_NW_TTL)) {
458 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
461 if (!(wc & FWW_NW_PROTO)) {
462 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
463 switch (flow->nw_proto) {
466 if (!(wc & FWW_TP_SRC)) {
467 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
469 if (!(wc & FWW_TP_DST)) {
470 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
476 if (!(wc & FWW_TP_SRC)) {
477 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
479 if (!(wc & FWW_TP_DST)) {
480 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
486 if (!(wc & FWW_TP_SRC)) {
487 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
489 if (!(wc & FWW_TP_DST)) {
490 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
495 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
497 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
498 &cr->wc.ipv6_src_mask);
499 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
500 &cr->wc.ipv6_dst_mask);
503 if (!(wc & FWW_IPV6_LABEL)) {
504 nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
507 if (!(wc & FWW_NW_DSCP)) {
508 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
511 if (!(wc & FWW_NW_ECN)) {
512 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
515 if (!(wc & FWW_NW_TTL)) {
516 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
519 if (!(wc & FWW_NW_PROTO)) {
520 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
521 switch (flow->nw_proto) {
524 if (!(wc & FWW_TP_SRC)) {
525 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
527 if (!(wc & FWW_TP_DST)) {
528 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
534 if (!(wc & FWW_TP_SRC)) {
535 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
537 if (!(wc & FWW_TP_DST)) {
538 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
544 if (!(wc & FWW_TP_SRC)) {
545 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
547 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
548 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
549 if (!(wc & FWW_ND_TARGET)) {
550 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
553 if (!(wc & FWW_ARP_SHA)
554 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
555 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
557 if (!(wc & FWW_ARP_THA)
558 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
559 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
563 if (!(wc & FWW_TP_DST)) {
564 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
569 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
571 if (!(wc & FWW_NW_PROTO)) {
572 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
574 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
575 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
576 if (!(wc & FWW_ARP_SHA)) {
577 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
579 if (!(wc & FWW_ARP_THA)) {
580 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
585 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
588 for (i = 0; i < FLOW_N_REGS; i++) {
589 nxm_put_32m(b, NXM_NX_REG(i),
590 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
594 nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
596 match_len = b->size - start_len;
597 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
601 /* nx_match_to_string() and helpers. */
603 static void format_nxm_field_name(struct ds *, uint32_t header);
606 nx_match_to_string(const uint8_t *p, unsigned int match_len)
612 return xstrdup("<any>");
616 while ((header = nx_entry_ok(p, match_len)) != 0) {
617 unsigned int length = NXM_LENGTH(header);
618 unsigned int value_len = nxm_field_bytes(header);
619 const uint8_t *value = p + 4;
620 const uint8_t *mask = value + value_len;
624 ds_put_cstr(&s, ", ");
627 format_nxm_field_name(&s, header);
628 ds_put_char(&s, '(');
630 for (i = 0; i < value_len; i++) {
631 ds_put_format(&s, "%02x", value[i]);
633 if (NXM_HASMASK(header)) {
634 ds_put_char(&s, '/');
635 for (i = 0; i < value_len; i++) {
636 ds_put_format(&s, "%02x", mask[i]);
639 ds_put_char(&s, ')');
642 match_len -= 4 + length;
647 ds_put_cstr(&s, ", ");
650 ds_put_format(&s, "<%u invalid bytes>", match_len);
653 return ds_steal_cstr(&s);
657 format_nxm_field_name(struct ds *s, uint32_t header)
659 const struct mf_field *mf = mf_from_nxm_header(header);
661 ds_put_cstr(s, mf->nxm_name);
662 if (NXM_HASMASK(header)) {
663 ds_put_cstr(s, "_W");
665 } else if (header == NXM_NX_COOKIE) {
666 ds_put_cstr(s, "NXM_NX_COOKIE");
667 } else if (header == NXM_NX_COOKIE_W) {
668 ds_put_cstr(s, "NXM_NX_COOKIE_W");
670 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
675 parse_nxm_field_name(const char *name, int name_len)
680 /* Check whether it's a field name. */
681 wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
686 for (i = 0; i < MFF_N_IDS; i++) {
687 const struct mf_field *mf = mf_from_id(i);
690 && !strncmp(mf->nxm_name, name, name_len)
691 && mf->nxm_name[name_len] == '\0') {
693 return mf->nxm_header;
694 } else if (mf->maskable != MFM_NONE) {
695 return NXM_MAKE_WILD_HEADER(mf->nxm_header);
700 if (!strncmp("NXM_NX_COOKIE", name, name_len)
701 && (name_len == strlen("NXM_NX_COOKIE"))) {
703 return NXM_NX_COOKIE;
705 return NXM_NX_COOKIE_W;
709 /* Check whether it's a 32-bit field header value as hex.
710 * (This isn't ordinarily useful except for testing error behavior.) */
712 uint32_t header = hexits_value(name, name_len, NULL);
713 if (header != UINT_MAX) {
721 /* nx_match_from_string(). */
724 nx_match_from_string(const char *s, struct ofpbuf *b)
726 const char *full_s = s;
727 const size_t start_len = b->size;
730 if (!strcmp(s, "<any>")) {
731 /* Ensure that 'b->data' isn't actually null. */
732 ofpbuf_prealloc_tailroom(b, 1);
736 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
743 name_len = strcspn(s, "(");
744 if (s[name_len] != '(') {
745 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
748 header = parse_nxm_field_name(name, name_len);
750 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
755 nxm_put_header(b, header);
756 s = ofpbuf_put_hex(b, s, &n);
757 if (n != nxm_field_bytes(header)) {
758 ovs_fatal(0, "%.2s: hex digits expected", s);
760 if (NXM_HASMASK(header)) {
763 ovs_fatal(0, "%s: missing / in masked field %.*s",
764 full_s, name_len, name);
766 s = ofpbuf_put_hex(b, s + 1, &n);
767 if (n != nxm_field_bytes(header)) {
768 ovs_fatal(0, "%.2s: hex digits expected", s);
774 ovs_fatal(0, "%s: missing ) following field %.*s",
775 full_s, name_len, name);
780 match_len = b->size - start_len;
781 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
786 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
788 const char *full_s = s;
796 name_len = strcspn(s, "[");
797 if (s[name_len] != '[') {
798 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
801 header = parse_nxm_field_name(name, name_len);
803 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
805 width = nxm_field_bits(header);
808 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
810 } else if (sscanf(s, "[%d]", &start) == 1) {
812 } else if (!strncmp(s, "[]", 2)) {
816 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
817 "[<start>..<end>]", full_s);
819 s = strchr(s, ']') + 1;
822 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
824 } else if (start >= width) {
825 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
826 "%d bits wide", full_s, start, width);
827 } else if (end >= width){
828 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
829 "%d bits wide", full_s, end, width);
834 *n_bitsp = end - start + 1;
840 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
842 const char *full_s = s;
844 int src_ofs, dst_ofs;
845 int src_n_bits, dst_n_bits;
847 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
848 if (strncmp(s, "->", 2)) {
849 ovs_fatal(0, "%s: missing `->' following source", full_s);
852 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
854 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
857 if (src_n_bits != dst_n_bits) {
858 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
859 "%d bits wide", full_s, src_n_bits, dst_n_bits);
862 ofputil_init_NXAST_REG_MOVE(move);
863 move->n_bits = htons(src_n_bits);
864 move->src_ofs = htons(src_ofs);
865 move->dst_ofs = htons(dst_ofs);
866 move->src = htonl(src);
867 move->dst = htonl(dst);
871 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
873 const char *full_s = s;
878 value = strtoull(s, (char **) &s, 0);
879 if (strncmp(s, "->", 2)) {
880 ovs_fatal(0, "%s: missing `->' following value", full_s);
883 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
885 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
888 if (n_bits < 64 && (value >> n_bits) != 0) {
889 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
890 full_s, value, n_bits);
893 ofputil_init_NXAST_REG_LOAD(load);
894 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
895 load->dst = htonl(dst);
896 load->value = htonll(value);
899 /* nxm_format_reg_move(), nxm_format_reg_load(). */
902 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
904 format_nxm_field_name(s, header);
905 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
906 ds_put_cstr(s, "[]");
907 } else if (n_bits == 1) {
908 ds_put_format(s, "[%d]", ofs);
910 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
915 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
917 int n_bits = ntohs(move->n_bits);
918 int src_ofs = ntohs(move->src_ofs);
919 int dst_ofs = ntohs(move->dst_ofs);
920 uint32_t src = ntohl(move->src);
921 uint32_t dst = ntohl(move->dst);
923 ds_put_format(s, "move:");
924 nxm_format_field_bits(s, src, src_ofs, n_bits);
925 ds_put_cstr(s, "->");
926 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
930 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
932 int ofs = nxm_decode_ofs(load->ofs_nbits);
933 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
934 uint32_t dst = ntohl(load->dst);
935 uint64_t value = ntohll(load->value);
937 ds_put_format(s, "load:%#"PRIx64"->", value);
938 nxm_format_field_bits(s, dst, ofs, n_bits);
941 /* nxm_check_reg_move(), nxm_check_reg_load(). */
944 field_ok(const struct mf_field *mf, const struct flow *flow, int size)
947 && mf_are_prereqs_ok(mf, flow)
948 && size <= nxm_field_bits(mf->nxm_header));
952 nxm_check_reg_move(const struct nx_action_reg_move *action,
953 const struct flow *flow)
955 int src_ofs, dst_ofs, n_bits;
958 n_bits = ntohs(action->n_bits);
959 src_ofs = ntohs(action->src_ofs);
960 dst_ofs = ntohs(action->dst_ofs);
962 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
967 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
970 /* Given a flow, checks that the source field represented by 'src_header'
971 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
973 nxm_src_check(ovs_be32 src_header_, unsigned int ofs, unsigned int n_bits,
974 const struct flow *flow)
976 uint32_t src_header = ntohl(src_header_);
977 const struct mf_field *src = mf_from_nxm_header(src_header);
980 VLOG_WARN_RL(&rl, "zero bit source field");
981 } else if (NXM_HASMASK(src_header) || !field_ok(src, flow, ofs + n_bits)) {
982 VLOG_WARN_RL(&rl, "invalid source field");
990 /* Given a flow, checks that the destination field represented by 'dst_header'
991 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
993 nxm_dst_check(ovs_be32 dst_header_, unsigned int ofs, unsigned int n_bits,
994 const struct flow *flow)
996 uint32_t dst_header = ntohl(dst_header_);
997 const struct mf_field *dst = mf_from_nxm_header(dst_header);
1000 VLOG_WARN_RL(&rl, "zero bit destination field");
1001 } else if (NXM_HASMASK(dst_header) || !field_ok(dst, flow, ofs + n_bits)) {
1002 VLOG_WARN_RL(&rl, "invalid destination field");
1003 } else if (!dst->writable) {
1004 VLOG_WARN_RL(&rl, "destination field is not writable");
1009 return BAD_ARGUMENT;
1013 nxm_check_reg_load(const struct nx_action_reg_load *action,
1014 const struct flow *flow)
1016 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
1017 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1020 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
1025 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1027 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1028 return BAD_ARGUMENT;
1034 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1037 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
1038 void *dst_, unsigned int dst_len, unsigned int dst_ofs,
1039 unsigned int n_bits)
1041 const uint8_t *src = src_;
1042 uint8_t *dst = dst_;
1044 src += src_len - (src_ofs / 8 + 1);
1047 dst += dst_len - (dst_ofs / 8 + 1);
1050 if (src_ofs == 0 && dst_ofs == 0) {
1051 unsigned int n_bytes = n_bits / 8;
1055 memcpy(dst, src, n_bytes);
1062 uint8_t mask = (1 << n_bits) - 1;
1063 *dst = (*dst & ~mask) | (*src & mask);
1066 while (n_bits > 0) {
1067 unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
1068 unsigned int chunk = MIN(n_bits, max_copy);
1069 uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
1072 *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
1089 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1092 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1093 const struct flow *flow)
1095 const struct mf_field *field = mf_from_nxm_header(ntohl(header));
1096 union mf_value value;
1097 union mf_value bits;
1099 mf_get_value(field, flow, &value);
1100 bits.be64 = htonll(0);
1101 bitwise_copy(&value, field->n_bytes, nxm_decode_ofs(ofs_nbits),
1102 &bits, sizeof bits.be64, 0,
1103 nxm_decode_n_bits(ofs_nbits));
1104 return ntohll(bits.be64);
1108 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1111 const struct mf_field *src = mf_from_nxm_header(ntohl(action->src));
1112 const struct mf_field *dst = mf_from_nxm_header(ntohl(action->dst));
1113 union mf_value src_value;
1114 union mf_value dst_value;
1116 mf_get_value(dst, flow, &dst_value);
1117 mf_get_value(src, flow, &src_value);
1118 bitwise_copy(&src_value, src->n_bytes, ntohs(action->src_ofs),
1119 &dst_value, dst->n_bytes, ntohs(action->dst_ofs),
1120 ntohs(action->n_bits));
1121 mf_set_flow_value(dst, &dst_value, flow);
1125 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1128 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1131 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1132 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1134 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1137 const struct mf_field *dst = mf_from_nxm_header(ntohl(dst_header));
1138 int n_bits = nxm_decode_n_bits(ofs_nbits);
1139 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1140 union mf_value dst_value;
1141 union mf_value src_value;
1143 mf_get_value(dst, flow, &dst_value);
1144 src_value.be64 = htonll(src_data);
1145 bitwise_copy(&src_value, sizeof src_value.be64, 0,
1146 &dst_value, dst->n_bytes, dst_ofs,
1148 mf_set_flow_value(dst, &dst_value, flow);