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 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
51 enum nxm_field_index {
52 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE) \
54 #include "nx-match.def"
59 struct hmap_node hmap_node;
60 enum nxm_field_index index; /* NFI_* value. */
61 uint32_t header; /* NXM_* value. */
62 enum mf_field_id mf_id; /* MFF_* value. */
63 const struct mf_field *mf;
64 const char *name; /* "NXM_*" string. */
65 bool writable; /* Writable with NXAST_REG_{MOVE,LOAD}? */
68 /* All the known fields. */
69 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
70 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE) \
71 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, \
72 MFF_ID, NULL, "NXM_" #HEADER, WRITABLE },
73 #include "nx-match.def"
76 /* Hash table of 'nxm_fields'. */
77 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
82 if (hmap_is_empty(&all_nxm_fields)) {
85 for (i = 0; i < N_NXM_FIELDS; i++) {
86 struct nxm_field *f = &nxm_fields[i];
87 hmap_insert(&all_nxm_fields, &f->hmap_node,
88 hash_int(f->header, 0));
89 f->mf = mf_from_id(f->mf_id);
92 /* Verify that the header values are unique (duplicate "case" values
93 * cause a compile error). */
95 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE) \
96 case NXM_##HEADER: break;
97 #include "nx-match.def"
102 static const struct nxm_field *
103 nxm_field_lookup(uint32_t header)
109 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
111 if (f->header == header) {
119 /* Returns the width of the data for a field with the given 'header', in
122 nxm_field_bytes(uint32_t header)
124 unsigned int length = NXM_LENGTH(header);
125 return NXM_HASMASK(header) ? length / 2 : length;
128 /* Returns the width of the data for a field with the given 'header', in
131 nxm_field_bits(uint32_t header)
133 return nxm_field_bytes(header) * 8;
136 const struct mf_field *
137 nxm_field_to_mf_field(uint32_t header)
139 const struct nxm_field *f = nxm_field_lookup(header);
140 return f ? f->mf : NULL;
143 /* nx_pull_match() and helpers. */
146 nx_entry_ok(const void *p, unsigned int match_len)
148 unsigned int payload_len;
154 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
158 memcpy(&header_be, p, 4);
159 header = ntohl(header_be);
161 payload_len = NXM_LENGTH(header);
163 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
167 if (match_len < payload_len + 4) {
168 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
169 "%u bytes left in nx_match", payload_len + 4, match_len);
177 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
178 struct cls_rule *rule)
183 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
185 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
186 "multiple of 8, is longer than space in message (max "
187 "length %zu)", match_len, b->size);
188 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
191 cls_rule_init_catchall(rule, priority);
192 while ((header = nx_entry_ok(p, match_len)) != 0) {
193 unsigned length = NXM_LENGTH(header);
194 const struct nxm_field *f;
197 f = nxm_field_lookup(header);
199 error = NXM_BAD_TYPE;
200 } else if (!mf_are_prereqs_ok(f->mf, &rule->flow)) {
201 error = NXM_BAD_PREREQ;
202 } else if (!mf_is_all_wild(f->mf, &rule->wc)) {
203 error = NXM_DUP_TYPE;
205 unsigned int width = f->mf->n_bytes;
206 union mf_value value;
208 memcpy(&value, p + 4, width);
209 if (!mf_is_value_valid(f->mf, &value)) {
210 error = NXM_BAD_VALUE;
211 } else if (!NXM_HASMASK(header)) {
213 mf_set_value(f->mf, &value, rule);
217 memcpy(&mask, p + 4 + width, width);
218 if (!mf_is_mask_valid(f->mf, &mask)) {
219 error = NXM_BAD_MASK;
222 mf_set(f->mf, &value, &mask, rule);
228 char *msg = ofputil_error_to_string(error);
229 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
230 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
232 NXM_VENDOR(header), NXM_FIELD(header),
233 NXM_HASMASK(header), NXM_TYPE(header),
241 match_len -= 4 + length;
244 return match_len ? NXM_INVALID : 0;
247 /* nx_put_match() and helpers.
249 * 'put' functions whose names end in 'w' add a wildcarded field.
250 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
251 * Other 'put' functions add exact-match fields.
255 nxm_put_header(struct ofpbuf *b, uint32_t header)
257 ovs_be32 n_header = htonl(header);
258 ofpbuf_put(b, &n_header, sizeof n_header);
262 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
264 nxm_put_header(b, header);
265 ofpbuf_put(b, &value, sizeof value);
269 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
276 nxm_put_8(b, header, value);
280 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
281 ofpbuf_put(b, &value, sizeof value);
282 ofpbuf_put(b, &mask, sizeof mask);
287 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
289 nxm_put_header(b, header);
290 ofpbuf_put(b, &value, sizeof value);
294 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
296 nxm_put_header(b, header);
297 ofpbuf_put(b, &value, sizeof value);
298 ofpbuf_put(b, &mask, sizeof mask);
302 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
308 case CONSTANT_HTONS(UINT16_MAX):
309 nxm_put_16(b, header, value);
313 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
319 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
321 nxm_put_header(b, header);
322 ofpbuf_put(b, &value, sizeof value);
326 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
328 nxm_put_header(b, header);
329 ofpbuf_put(b, &value, sizeof value);
330 ofpbuf_put(b, &mask, sizeof mask);
334 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
340 case CONSTANT_HTONL(UINT32_MAX):
341 nxm_put_32(b, header, value);
345 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
351 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
353 nxm_put_header(b, header);
354 ofpbuf_put(b, &value, sizeof value);
358 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
360 nxm_put_header(b, header);
361 ofpbuf_put(b, &value, sizeof value);
362 ofpbuf_put(b, &mask, sizeof mask);
366 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
372 case CONSTANT_HTONLL(UINT64_MAX):
373 nxm_put_64(b, header, value);
377 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
383 nxm_put_eth(struct ofpbuf *b, uint32_t header,
384 const uint8_t value[ETH_ADDR_LEN])
386 nxm_put_header(b, header);
387 ofpbuf_put(b, value, ETH_ADDR_LEN);
391 nxm_put_eth_dst(struct ofpbuf *b,
392 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
394 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
395 case FWW_DL_DST | FWW_ETH_MCAST:
398 nxm_put_header(b, NXM_OF_ETH_DST_W);
399 ofpbuf_put(b, value, ETH_ADDR_LEN);
400 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
403 nxm_put_eth(b, NXM_OF_ETH_DST, value);
409 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
410 const struct in6_addr *value, const struct in6_addr *mask)
412 if (ipv6_mask_is_any(mask)) {
414 } else if (ipv6_mask_is_exact(mask)) {
415 nxm_put_header(b, header);
416 ofpbuf_put(b, value, sizeof *value);
418 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
419 ofpbuf_put(b, value, sizeof *value);
420 ofpbuf_put(b, mask, sizeof *mask);
425 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
427 uint8_t nw_frag = cr->flow.nw_frag;
428 uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
430 switch (nw_frag_mask) {
434 case FLOW_NW_FRAG_MASK:
435 nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
439 nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
440 nw_frag_mask & FLOW_NW_FRAG_MASK);
445 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
446 * 'cr->priority', because priority is not part of nx_match), plus enough
447 * zero bytes to pad the nx_match out to a multiple of 8.
449 * This function can cause 'b''s data to be reallocated.
451 * Returns the number of bytes appended to 'b', excluding padding.
453 * If 'cr' is a catch-all rule that matches every packet, then this function
454 * appends nothing to 'b' and returns 0. */
456 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
458 const flow_wildcards_t wc = cr->wc.wildcards;
459 const struct flow *flow = &cr->flow;
460 const size_t start_len = b->size;
464 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
467 if (!(wc & FWW_IN_PORT)) {
468 uint16_t in_port = flow->in_port;
469 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
473 nxm_put_eth_dst(b, wc, flow->dl_dst);
474 if (!(wc & FWW_DL_SRC)) {
475 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
477 if (!(wc & FWW_DL_TYPE)) {
478 nxm_put_16(b, NXM_OF_ETH_TYPE,
479 ofputil_dl_type_to_openflow(flow->dl_type));
483 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
486 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
488 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
489 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
492 if (!(wc & FWW_NW_DSCP)) {
493 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
496 if (!(wc & FWW_NW_ECN)) {
497 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
500 if (!(wc & FWW_NW_TTL)) {
501 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
504 if (!(wc & FWW_NW_PROTO)) {
505 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
506 switch (flow->nw_proto) {
509 if (!(wc & FWW_TP_SRC)) {
510 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
512 if (!(wc & FWW_TP_DST)) {
513 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
519 if (!(wc & FWW_TP_SRC)) {
520 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
522 if (!(wc & FWW_TP_DST)) {
523 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
529 if (!(wc & FWW_TP_SRC)) {
530 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
532 if (!(wc & FWW_TP_DST)) {
533 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
538 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
540 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
541 &cr->wc.ipv6_src_mask);
542 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
543 &cr->wc.ipv6_dst_mask);
546 if (!(wc & FWW_IPV6_LABEL)) {
547 nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
550 if (!(wc & FWW_NW_DSCP)) {
551 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & IP_DSCP_MASK);
554 if (!(wc & FWW_NW_ECN)) {
555 nxm_put_8(b, NXM_NX_IP_ECN, flow->nw_tos & IP_ECN_MASK);
558 if (!(wc & FWW_NW_TTL)) {
559 nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
562 if (!(wc & FWW_NW_PROTO)) {
563 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
564 switch (flow->nw_proto) {
567 if (!(wc & FWW_TP_SRC)) {
568 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
570 if (!(wc & FWW_TP_DST)) {
571 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
577 if (!(wc & FWW_TP_SRC)) {
578 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
580 if (!(wc & FWW_TP_DST)) {
581 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
587 if (!(wc & FWW_TP_SRC)) {
588 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
590 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
591 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
592 if (!(wc & FWW_ND_TARGET)) {
593 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
596 if (!(wc & FWW_ARP_SHA)
597 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
598 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
600 if (!(wc & FWW_ARP_THA)
601 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
602 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
606 if (!(wc & FWW_TP_DST)) {
607 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
612 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
614 if (!(wc & FWW_NW_PROTO)) {
615 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
617 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
618 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
619 if (!(wc & FWW_ARP_SHA)) {
620 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
622 if (!(wc & FWW_ARP_THA)) {
623 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
628 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
631 for (i = 0; i < FLOW_N_REGS; i++) {
632 nxm_put_32m(b, NXM_NX_REG(i),
633 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
636 match_len = b->size - start_len;
637 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
641 /* nx_match_to_string() and helpers. */
643 static void format_nxm_field_name(struct ds *, uint32_t header);
646 nx_match_to_string(const uint8_t *p, unsigned int match_len)
652 return xstrdup("<any>");
656 while ((header = nx_entry_ok(p, match_len)) != 0) {
657 unsigned int length = NXM_LENGTH(header);
658 unsigned int value_len = nxm_field_bytes(header);
659 const uint8_t *value = p + 4;
660 const uint8_t *mask = value + value_len;
664 ds_put_cstr(&s, ", ");
667 format_nxm_field_name(&s, header);
668 ds_put_char(&s, '(');
670 for (i = 0; i < value_len; i++) {
671 ds_put_format(&s, "%02x", value[i]);
673 if (NXM_HASMASK(header)) {
674 ds_put_char(&s, '/');
675 for (i = 0; i < value_len; i++) {
676 ds_put_format(&s, "%02x", mask[i]);
679 ds_put_char(&s, ')');
682 match_len -= 4 + length;
687 ds_put_cstr(&s, ", ");
690 ds_put_format(&s, "<%u invalid bytes>", match_len);
693 return ds_steal_cstr(&s);
697 format_nxm_field_name(struct ds *s, uint32_t header)
699 const struct nxm_field *f = nxm_field_lookup(header);
701 ds_put_cstr(s, f->name);
703 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
708 parse_nxm_field_name(const char *name, int name_len)
710 const struct nxm_field *f;
712 /* Check whether it's a field name. */
713 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
714 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
719 /* Check whether it's a 32-bit field header value as hex.
720 * (This isn't ordinarily useful except for testing error behavior.) */
722 uint32_t header = hexits_value(name, name_len, NULL);
723 if (header != UINT_MAX) {
731 /* nx_match_from_string(). */
734 nx_match_from_string(const char *s, struct ofpbuf *b)
736 const char *full_s = s;
737 const size_t start_len = b->size;
740 if (!strcmp(s, "<any>")) {
741 /* Ensure that 'b->data' isn't actually null. */
742 ofpbuf_prealloc_tailroom(b, 1);
746 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
753 name_len = strcspn(s, "(");
754 if (s[name_len] != '(') {
755 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
758 header = parse_nxm_field_name(name, name_len);
760 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
765 nxm_put_header(b, header);
766 s = ofpbuf_put_hex(b, s, &n);
767 if (n != nxm_field_bytes(header)) {
768 ovs_fatal(0, "%.2s: hex digits expected", s);
770 if (NXM_HASMASK(header)) {
773 ovs_fatal(0, "%s: missing / in masked field %.*s",
774 full_s, name_len, name);
776 s = ofpbuf_put_hex(b, s + 1, &n);
777 if (n != nxm_field_bytes(header)) {
778 ovs_fatal(0, "%.2s: hex digits expected", s);
784 ovs_fatal(0, "%s: missing ) following field %.*s",
785 full_s, name_len, name);
790 match_len = b->size - start_len;
791 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
796 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
798 const char *full_s = s;
806 name_len = strcspn(s, "[");
807 if (s[name_len] != '[') {
808 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
811 header = parse_nxm_field_name(name, name_len);
813 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
815 width = nxm_field_bits(header);
818 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
820 } else if (sscanf(s, "[%d]", &start) == 1) {
822 } else if (!strncmp(s, "[]", 2)) {
826 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
827 "[<start>..<end>]", full_s);
829 s = strchr(s, ']') + 1;
832 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
834 } else if (start >= width) {
835 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
836 "%d bits wide", full_s, start, width);
837 } else if (end >= width){
838 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
839 "%d bits wide", full_s, end, width);
844 *n_bitsp = end - start + 1;
850 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
852 const char *full_s = s;
854 int src_ofs, dst_ofs;
855 int src_n_bits, dst_n_bits;
857 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
858 if (strncmp(s, "->", 2)) {
859 ovs_fatal(0, "%s: missing `->' following source", full_s);
862 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
864 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
867 if (src_n_bits != dst_n_bits) {
868 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
869 "%d bits wide", full_s, src_n_bits, dst_n_bits);
872 ofputil_init_NXAST_REG_MOVE(move);
873 move->n_bits = htons(src_n_bits);
874 move->src_ofs = htons(src_ofs);
875 move->dst_ofs = htons(dst_ofs);
876 move->src = htonl(src);
877 move->dst = htonl(dst);
881 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
883 const char *full_s = s;
888 value = strtoull(s, (char **) &s, 0);
889 if (strncmp(s, "->", 2)) {
890 ovs_fatal(0, "%s: missing `->' following value", full_s);
893 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
895 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
898 if (n_bits < 64 && (value >> n_bits) != 0) {
899 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
900 full_s, value, n_bits);
903 ofputil_init_NXAST_REG_LOAD(load);
904 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
905 load->dst = htonl(dst);
906 load->value = htonll(value);
909 /* nxm_format_reg_move(), nxm_format_reg_load(). */
912 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
914 format_nxm_field_name(s, header);
915 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
916 ds_put_cstr(s, "[]");
917 } else if (n_bits == 1) {
918 ds_put_format(s, "[%d]", ofs);
920 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
925 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
927 int n_bits = ntohs(move->n_bits);
928 int src_ofs = ntohs(move->src_ofs);
929 int dst_ofs = ntohs(move->dst_ofs);
930 uint32_t src = ntohl(move->src);
931 uint32_t dst = ntohl(move->dst);
933 ds_put_format(s, "move:");
934 nxm_format_field_bits(s, src, src_ofs, n_bits);
935 ds_put_cstr(s, "->");
936 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
940 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
942 int ofs = nxm_decode_ofs(load->ofs_nbits);
943 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
944 uint32_t dst = ntohl(load->dst);
945 uint64_t value = ntohll(load->value);
947 ds_put_format(s, "load:%#"PRIx64"->", value);
948 nxm_format_field_bits(s, dst, ofs, n_bits);
951 /* nxm_check_reg_move(), nxm_check_reg_load(). */
954 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
957 && !NXM_HASMASK(f->header)
958 && mf_are_prereqs_ok(f->mf, flow)
959 && size <= nxm_field_bits(f->header));
963 nxm_check_reg_move(const struct nx_action_reg_move *action,
964 const struct flow *flow)
966 int src_ofs, dst_ofs, n_bits;
969 n_bits = ntohs(action->n_bits);
970 src_ofs = ntohs(action->src_ofs);
971 dst_ofs = ntohs(action->dst_ofs);
973 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
978 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
981 /* Given a flow, checks that the source field represented by 'src_header'
982 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
984 nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits,
985 const struct flow *flow)
987 const struct nxm_field *src = nxm_field_lookup(ntohl(src_header));
990 VLOG_WARN_RL(&rl, "zero bit source field");
991 } else if (!field_ok(src, flow, ofs + n_bits)) {
992 VLOG_WARN_RL(&rl, "invalid source field");
1000 /* Given a flow, checks that the destination field represented by 'dst_header'
1001 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
1003 nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits,
1004 const struct flow *flow)
1006 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1009 VLOG_WARN_RL(&rl, "zero bit destination field");
1010 } else if (!field_ok(dst, flow, ofs + n_bits)) {
1011 VLOG_WARN_RL(&rl, "invalid destination field");
1012 } else if (!dst->writable) {
1013 VLOG_WARN_RL(&rl, "destination field is not writable");
1018 return BAD_ARGUMENT;
1022 nxm_check_reg_load(const struct nx_action_reg_load *action,
1023 const struct flow *flow)
1025 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
1026 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1029 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
1034 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1036 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1037 return BAD_ARGUMENT;
1043 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1046 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1048 switch (src->index) {
1049 case NFI_NXM_OF_IN_PORT:
1050 return flow->in_port;
1052 case NFI_NXM_OF_ETH_DST:
1053 return eth_addr_to_uint64(flow->dl_dst);
1055 case NFI_NXM_OF_ETH_SRC:
1056 return eth_addr_to_uint64(flow->dl_src);
1058 case NFI_NXM_OF_ETH_TYPE:
1059 return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
1061 case NFI_NXM_OF_VLAN_TCI:
1062 return ntohs(flow->vlan_tci);
1064 case NFI_NXM_OF_IP_TOS:
1065 return flow->nw_tos & IP_DSCP_MASK;
1067 case NFI_NXM_NX_IP_ECN:
1068 return flow->nw_tos & IP_ECN_MASK;
1070 case NFI_NXM_NX_IP_TTL:
1071 return flow->nw_ttl;
1073 case NFI_NXM_NX_IP_FRAG:
1074 return flow->nw_frag;
1076 case NFI_NXM_OF_IP_PROTO:
1077 case NFI_NXM_OF_ARP_OP:
1078 return flow->nw_proto;
1080 case NFI_NXM_OF_IP_SRC:
1081 case NFI_NXM_OF_ARP_SPA:
1082 return ntohl(flow->nw_src);
1084 case NFI_NXM_OF_IP_DST:
1085 case NFI_NXM_OF_ARP_TPA:
1086 return ntohl(flow->nw_dst);
1088 case NFI_NXM_OF_TCP_SRC:
1089 case NFI_NXM_OF_UDP_SRC:
1090 return ntohs(flow->tp_src);
1092 case NFI_NXM_OF_TCP_DST:
1093 case NFI_NXM_OF_UDP_DST:
1094 return ntohs(flow->tp_dst);
1096 case NFI_NXM_OF_ICMP_TYPE:
1097 case NFI_NXM_NX_ICMPV6_TYPE:
1098 return ntohs(flow->tp_src) & 0xff;
1100 case NFI_NXM_OF_ICMP_CODE:
1101 case NFI_NXM_NX_ICMPV6_CODE:
1102 return ntohs(flow->tp_dst) & 0xff;
1104 case NFI_NXM_NX_TUN_ID:
1105 return ntohll(flow->tun_id);
1107 case NFI_NXM_NX_IPV6_LABEL:
1108 return ntohl(flow->ipv6_label);
1110 #define NXM_READ_REGISTER(IDX) \
1111 case NFI_NXM_NX_REG##IDX: \
1112 return flow->regs[IDX]; \
1113 case NFI_NXM_NX_REG##IDX##_W: \
1116 NXM_READ_REGISTER(0);
1117 #if FLOW_N_REGS >= 2
1118 NXM_READ_REGISTER(1);
1120 #if FLOW_N_REGS >= 3
1121 NXM_READ_REGISTER(2);
1123 #if FLOW_N_REGS >= 4
1124 NXM_READ_REGISTER(3);
1126 #if FLOW_N_REGS >= 5
1127 NXM_READ_REGISTER(4);
1133 case NFI_NXM_NX_ARP_SHA:
1134 case NFI_NXM_NX_ND_SLL:
1135 return eth_addr_to_uint64(flow->arp_sha);
1137 case NFI_NXM_NX_ARP_THA:
1138 case NFI_NXM_NX_ND_TLL:
1139 return eth_addr_to_uint64(flow->arp_tha);
1141 case NFI_NXM_NX_TUN_ID_W:
1142 case NFI_NXM_OF_ETH_DST_W:
1143 case NFI_NXM_OF_VLAN_TCI_W:
1144 case NFI_NXM_OF_IP_SRC_W:
1145 case NFI_NXM_OF_IP_DST_W:
1146 case NFI_NXM_OF_ARP_SPA_W:
1147 case NFI_NXM_OF_ARP_TPA_W:
1148 case NFI_NXM_NX_IPV6_SRC:
1149 case NFI_NXM_NX_IPV6_SRC_W:
1150 case NFI_NXM_NX_IPV6_DST:
1151 case NFI_NXM_NX_IPV6_DST_W:
1152 case NFI_NXM_NX_IP_FRAG_W:
1153 case NFI_NXM_NX_ND_TARGET:
1161 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1164 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1165 const struct flow *flow)
1167 int n_bits = nxm_decode_n_bits(ofs_nbits);
1168 int ofs = nxm_decode_ofs(ofs_nbits);
1169 uint64_t mask, data;
1171 mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1172 data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow);
1173 data = (data >> ofs) & mask;
1179 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1182 switch (dst->index) {
1183 case NFI_NXM_OF_ETH_DST:
1184 eth_addr_from_uint64(new_value, flow->dl_dst);
1187 case NFI_NXM_OF_ETH_SRC:
1188 eth_addr_from_uint64(new_value, flow->dl_src);
1191 case NFI_NXM_OF_VLAN_TCI:
1192 flow->vlan_tci = htons(new_value);
1195 case NFI_NXM_NX_TUN_ID:
1196 flow->tun_id = htonll(new_value);
1199 #define NXM_WRITE_REGISTER(IDX) \
1200 case NFI_NXM_NX_REG##IDX: \
1201 flow->regs[IDX] = new_value; \
1203 case NFI_NXM_NX_REG##IDX##_W: \
1206 NXM_WRITE_REGISTER(0);
1207 #if FLOW_N_REGS >= 2
1208 NXM_WRITE_REGISTER(1);
1210 #if FLOW_N_REGS >= 3
1211 NXM_WRITE_REGISTER(2);
1213 #if FLOW_N_REGS >= 4
1214 NXM_WRITE_REGISTER(3);
1216 #if FLOW_N_REGS >= 5
1217 NXM_WRITE_REGISTER(4);
1223 case NFI_NXM_OF_IP_TOS:
1224 flow->nw_tos &= ~IP_DSCP_MASK;
1225 flow->nw_tos |= new_value & IP_DSCP_MASK;
1228 case NFI_NXM_NX_IP_ECN:
1229 flow->nw_tos &= ~IP_ECN_MASK;
1230 flow->nw_tos |= new_value & IP_ECN_MASK;
1233 case NFI_NXM_NX_IP_TTL:
1234 flow->nw_ttl = new_value;
1237 case NFI_NXM_NX_IP_FRAG:
1238 flow->nw_frag = new_value;
1241 case NFI_NXM_OF_IP_SRC:
1242 flow->nw_src = htonl(new_value);
1245 case NFI_NXM_OF_IP_DST:
1246 flow->nw_dst = htonl(new_value);
1249 case NFI_NXM_NX_IPV6_LABEL:
1250 flow->ipv6_label = htonl(new_value);
1253 case NFI_NXM_OF_TCP_SRC:
1254 case NFI_NXM_OF_UDP_SRC:
1255 flow->tp_src = htons(new_value);
1258 case NFI_NXM_OF_TCP_DST:
1259 case NFI_NXM_OF_UDP_DST:
1260 flow->tp_dst = htons(new_value);
1263 case NFI_NXM_OF_IN_PORT:
1264 case NFI_NXM_OF_ETH_TYPE:
1265 case NFI_NXM_OF_IP_PROTO:
1266 case NFI_NXM_OF_ARP_OP:
1267 case NFI_NXM_OF_ARP_SPA:
1268 case NFI_NXM_OF_ARP_TPA:
1269 case NFI_NXM_OF_ICMP_TYPE:
1270 case NFI_NXM_OF_ICMP_CODE:
1271 case NFI_NXM_NX_TUN_ID_W:
1272 case NFI_NXM_OF_ETH_DST_W:
1273 case NFI_NXM_OF_VLAN_TCI_W:
1274 case NFI_NXM_OF_IP_SRC_W:
1275 case NFI_NXM_OF_IP_DST_W:
1276 case NFI_NXM_OF_ARP_SPA_W:
1277 case NFI_NXM_OF_ARP_TPA_W:
1278 case NFI_NXM_NX_ARP_SHA:
1279 case NFI_NXM_NX_ARP_THA:
1280 case NFI_NXM_NX_IPV6_SRC:
1281 case NFI_NXM_NX_IPV6_SRC_W:
1282 case NFI_NXM_NX_IPV6_DST:
1283 case NFI_NXM_NX_IPV6_DST_W:
1284 case NFI_NXM_NX_IP_FRAG_W:
1285 case NFI_NXM_NX_ICMPV6_TYPE:
1286 case NFI_NXM_NX_ICMPV6_CODE:
1287 case NFI_NXM_NX_ND_TARGET:
1288 case NFI_NXM_NX_ND_SLL:
1289 case NFI_NXM_NX_ND_TLL:
1296 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1299 ovs_be16 src_ofs_nbits, dst_ofs_nbits;
1303 n_bits = ntohs(action->n_bits);
1304 src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
1305 dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
1307 src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow);
1308 nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
1312 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1315 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1318 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1319 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1321 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1324 int n_bits = nxm_decode_n_bits(ofs_nbits);
1325 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1326 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1328 /* Get remaining bits of the destination field. */
1329 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1330 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1332 /* Get the final value. */
1333 uint64_t new_data = dst_data | (src_data << dst_ofs);
1335 nxm_write_field(dst, flow, new_data);