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 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
229 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
231 NXM_VENDOR(header), NXM_FIELD(header),
232 NXM_HASMASK(header), NXM_TYPE(header),
238 match_len -= 4 + length;
241 return match_len ? NXM_INVALID : 0;
244 /* nx_put_match() and helpers.
246 * 'put' functions whose names end in 'w' add a wildcarded field.
247 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
248 * Other 'put' functions add exact-match fields.
252 nxm_put_header(struct ofpbuf *b, uint32_t header)
254 ovs_be32 n_header = htonl(header);
255 ofpbuf_put(b, &n_header, sizeof n_header);
259 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
261 nxm_put_header(b, header);
262 ofpbuf_put(b, &value, sizeof value);
266 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
268 nxm_put_header(b, header);
269 ofpbuf_put(b, &value, sizeof value);
273 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
275 nxm_put_header(b, header);
276 ofpbuf_put(b, &value, sizeof value);
277 ofpbuf_put(b, &mask, sizeof mask);
281 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
287 case CONSTANT_HTONS(UINT16_MAX):
288 nxm_put_16(b, header, value);
292 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
298 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
300 nxm_put_header(b, header);
301 ofpbuf_put(b, &value, sizeof value);
305 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
307 nxm_put_header(b, header);
308 ofpbuf_put(b, &value, sizeof value);
309 ofpbuf_put(b, &mask, sizeof mask);
313 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
319 case CONSTANT_HTONL(UINT32_MAX):
320 nxm_put_32(b, header, value);
324 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
330 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
332 nxm_put_header(b, header);
333 ofpbuf_put(b, &value, sizeof value);
337 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
339 nxm_put_header(b, header);
340 ofpbuf_put(b, &value, sizeof value);
341 ofpbuf_put(b, &mask, sizeof mask);
345 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
351 case CONSTANT_HTONLL(UINT64_MAX):
352 nxm_put_64(b, header, value);
356 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
362 nxm_put_eth(struct ofpbuf *b, uint32_t header,
363 const uint8_t value[ETH_ADDR_LEN])
365 nxm_put_header(b, header);
366 ofpbuf_put(b, value, ETH_ADDR_LEN);
370 nxm_put_eth_dst(struct ofpbuf *b,
371 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
373 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
374 case FWW_DL_DST | FWW_ETH_MCAST:
377 nxm_put_header(b, NXM_OF_ETH_DST_W);
378 ofpbuf_put(b, value, ETH_ADDR_LEN);
379 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
382 nxm_put_eth(b, NXM_OF_ETH_DST, value);
388 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
389 const struct in6_addr *value, const struct in6_addr *mask)
391 if (ipv6_mask_is_any(mask)) {
393 } else if (ipv6_mask_is_exact(mask)) {
394 nxm_put_header(b, header);
395 ofpbuf_put(b, value, sizeof *value);
397 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
398 ofpbuf_put(b, value, sizeof *value);
399 ofpbuf_put(b, mask, sizeof *mask);
403 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
404 * 'cr->priority', because priority is not part of nx_match), plus enough
405 * zero bytes to pad the nx_match out to a multiple of 8.
407 * This function can cause 'b''s data to be reallocated.
409 * Returns the number of bytes appended to 'b', excluding padding.
411 * If 'cr' is a catch-all rule that matches every packet, then this function
412 * appends nothing to 'b' and returns 0. */
414 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
416 const flow_wildcards_t wc = cr->wc.wildcards;
417 const struct flow *flow = &cr->flow;
418 const size_t start_len = b->size;
422 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
425 if (!(wc & FWW_IN_PORT)) {
426 uint16_t in_port = flow->in_port;
427 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
431 nxm_put_eth_dst(b, wc, flow->dl_dst);
432 if (!(wc & FWW_DL_SRC)) {
433 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
435 if (!(wc & FWW_DL_TYPE)) {
436 nxm_put_16(b, NXM_OF_ETH_TYPE,
437 ofputil_dl_type_to_openflow(flow->dl_type));
441 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
444 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
446 if (!(wc & FWW_NW_TOS)) {
447 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
449 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
450 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
452 if (!(wc & FWW_NW_PROTO)) {
453 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
454 switch (flow->nw_proto) {
457 if (!(wc & FWW_TP_SRC)) {
458 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
460 if (!(wc & FWW_TP_DST)) {
461 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
467 if (!(wc & FWW_TP_SRC)) {
468 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
470 if (!(wc & FWW_TP_DST)) {
471 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
477 if (!(wc & FWW_TP_SRC)) {
478 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
480 if (!(wc & FWW_TP_DST)) {
481 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
486 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
489 if (!(wc & FWW_NW_TOS)) {
490 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
492 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
493 &cr->wc.ipv6_src_mask);
494 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
495 &cr->wc.ipv6_dst_mask);
497 if (!(wc & FWW_NW_PROTO)) {
498 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
499 switch (flow->nw_proto) {
502 if (!(wc & FWW_TP_SRC)) {
503 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
505 if (!(wc & FWW_TP_DST)) {
506 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
512 if (!(wc & FWW_TP_SRC)) {
513 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
515 if (!(wc & FWW_TP_DST)) {
516 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
522 if (!(wc & FWW_TP_SRC)) {
523 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
525 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
526 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
527 if (!(wc & FWW_ND_TARGET)) {
528 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
531 if (!(wc & FWW_ARP_SHA)
532 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
533 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
535 if (!(wc & FWW_ARP_THA)
536 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
537 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
541 if (!(wc & FWW_TP_DST)) {
542 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
547 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
549 if (!(wc & FWW_NW_PROTO)) {
550 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
552 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
553 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
554 if (!(wc & FWW_ARP_SHA)) {
555 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
557 if (!(wc & FWW_ARP_THA)) {
558 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
563 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
566 for (i = 0; i < FLOW_N_REGS; i++) {
567 nxm_put_32m(b, NXM_NX_REG(i),
568 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
571 match_len = b->size - start_len;
572 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
576 /* nx_match_to_string() and helpers. */
578 static void format_nxm_field_name(struct ds *, uint32_t header);
581 nx_match_to_string(const uint8_t *p, unsigned int match_len)
587 return xstrdup("<any>");
591 while ((header = nx_entry_ok(p, match_len)) != 0) {
592 unsigned int length = NXM_LENGTH(header);
593 unsigned int value_len = nxm_field_bytes(header);
594 const uint8_t *value = p + 4;
595 const uint8_t *mask = value + value_len;
599 ds_put_cstr(&s, ", ");
602 format_nxm_field_name(&s, header);
603 ds_put_char(&s, '(');
605 for (i = 0; i < value_len; i++) {
606 ds_put_format(&s, "%02x", value[i]);
608 if (NXM_HASMASK(header)) {
609 ds_put_char(&s, '/');
610 for (i = 0; i < value_len; i++) {
611 ds_put_format(&s, "%02x", mask[i]);
614 ds_put_char(&s, ')');
617 match_len -= 4 + length;
622 ds_put_cstr(&s, ", ");
625 ds_put_format(&s, "<%u invalid bytes>", match_len);
628 return ds_steal_cstr(&s);
632 format_nxm_field_name(struct ds *s, uint32_t header)
634 const struct nxm_field *f = nxm_field_lookup(header);
636 ds_put_cstr(s, f->name);
638 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
643 parse_nxm_field_name(const char *name, int name_len)
645 const struct nxm_field *f;
647 /* Check whether it's a field name. */
648 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
649 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
654 /* Check whether it's a 32-bit field header value as hex.
655 * (This isn't ordinarily useful except for testing error behavior.) */
657 uint32_t header = hexits_value(name, name_len, NULL);
658 if (header != UINT_MAX) {
666 /* nx_match_from_string(). */
669 nx_match_from_string(const char *s, struct ofpbuf *b)
671 const char *full_s = s;
672 const size_t start_len = b->size;
675 if (!strcmp(s, "<any>")) {
676 /* Ensure that 'b->data' isn't actually null. */
677 ofpbuf_prealloc_tailroom(b, 1);
681 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
688 name_len = strcspn(s, "(");
689 if (s[name_len] != '(') {
690 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
693 header = parse_nxm_field_name(name, name_len);
695 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
700 nxm_put_header(b, header);
701 s = ofpbuf_put_hex(b, s, &n);
702 if (n != nxm_field_bytes(header)) {
703 ovs_fatal(0, "%.2s: hex digits expected", s);
705 if (NXM_HASMASK(header)) {
708 ovs_fatal(0, "%s: missing / in masked field %.*s",
709 full_s, name_len, name);
711 s = ofpbuf_put_hex(b, s + 1, &n);
712 if (n != nxm_field_bytes(header)) {
713 ovs_fatal(0, "%.2s: hex digits expected", s);
719 ovs_fatal(0, "%s: missing ) following field %.*s",
720 full_s, name_len, name);
725 match_len = b->size - start_len;
726 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
731 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
733 const char *full_s = s;
741 name_len = strcspn(s, "[");
742 if (s[name_len] != '[') {
743 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
746 header = parse_nxm_field_name(name, name_len);
748 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
750 width = nxm_field_bits(header);
753 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
755 } else if (sscanf(s, "[%d]", &start) == 1) {
757 } else if (!strncmp(s, "[]", 2)) {
761 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
762 "[<start>..<end>]", full_s);
764 s = strchr(s, ']') + 1;
767 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
769 } else if (start >= width) {
770 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
771 "%d bits wide", full_s, start, width);
772 } else if (end >= width){
773 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
774 "%d bits wide", full_s, end, width);
779 *n_bitsp = end - start + 1;
785 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
787 const char *full_s = s;
789 int src_ofs, dst_ofs;
790 int src_n_bits, dst_n_bits;
792 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
793 if (strncmp(s, "->", 2)) {
794 ovs_fatal(0, "%s: missing `->' following source", full_s);
797 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
799 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
802 if (src_n_bits != dst_n_bits) {
803 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
804 "%d bits wide", full_s, src_n_bits, dst_n_bits);
807 ofputil_init_NXAST_REG_MOVE(move);
808 move->n_bits = htons(src_n_bits);
809 move->src_ofs = htons(src_ofs);
810 move->dst_ofs = htons(dst_ofs);
811 move->src = htonl(src);
812 move->dst = htonl(dst);
816 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
818 const char *full_s = s;
823 value = strtoull(s, (char **) &s, 0);
824 if (strncmp(s, "->", 2)) {
825 ovs_fatal(0, "%s: missing `->' following value", full_s);
828 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
830 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
833 if (n_bits < 64 && (value >> n_bits) != 0) {
834 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
835 full_s, value, n_bits);
838 ofputil_init_NXAST_REG_LOAD(load);
839 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
840 load->dst = htonl(dst);
841 load->value = htonll(value);
844 /* nxm_format_reg_move(), nxm_format_reg_load(). */
847 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
849 format_nxm_field_name(s, header);
850 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
851 ds_put_cstr(s, "[]");
852 } else if (n_bits == 1) {
853 ds_put_format(s, "[%d]", ofs);
855 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
860 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
862 int n_bits = ntohs(move->n_bits);
863 int src_ofs = ntohs(move->src_ofs);
864 int dst_ofs = ntohs(move->dst_ofs);
865 uint32_t src = ntohl(move->src);
866 uint32_t dst = ntohl(move->dst);
868 ds_put_format(s, "move:");
869 nxm_format_field_bits(s, src, src_ofs, n_bits);
870 ds_put_cstr(s, "->");
871 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
875 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
877 int ofs = nxm_decode_ofs(load->ofs_nbits);
878 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
879 uint32_t dst = ntohl(load->dst);
880 uint64_t value = ntohll(load->value);
882 ds_put_format(s, "load:%#"PRIx64"->", value);
883 nxm_format_field_bits(s, dst, ofs, n_bits);
886 /* nxm_check_reg_move(), nxm_check_reg_load(). */
889 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
892 && !NXM_HASMASK(f->header)
893 && mf_are_prereqs_ok(f->mf, flow)
894 && size <= nxm_field_bits(f->header));
898 nxm_check_reg_move(const struct nx_action_reg_move *action,
899 const struct flow *flow)
901 int src_ofs, dst_ofs, n_bits;
904 n_bits = ntohs(action->n_bits);
905 src_ofs = ntohs(action->src_ofs);
906 dst_ofs = ntohs(action->dst_ofs);
908 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
913 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
916 /* Given a flow, checks that the source field represented by 'src_header'
917 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
919 nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits,
920 const struct flow *flow)
922 const struct nxm_field *src = nxm_field_lookup(ntohl(src_header));
925 VLOG_WARN_RL(&rl, "zero bit source field");
926 } else if (!field_ok(src, flow, ofs + n_bits)) {
927 VLOG_WARN_RL(&rl, "invalid source field");
935 /* Given a flow, checks that the destination field represented by 'dst_header'
936 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
938 nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits,
939 const struct flow *flow)
941 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
944 VLOG_WARN_RL(&rl, "zero bit destination field");
945 } else if (!field_ok(dst, flow, ofs + n_bits)) {
946 VLOG_WARN_RL(&rl, "invalid destination field");
947 } else if (!dst->writable) {
948 VLOG_WARN_RL(&rl, "destination field is not writable");
957 nxm_check_reg_load(const struct nx_action_reg_load *action,
958 const struct flow *flow)
960 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
961 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
964 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
969 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
971 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
978 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
981 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
983 switch (src->index) {
984 case NFI_NXM_OF_IN_PORT:
985 return flow->in_port;
987 case NFI_NXM_OF_ETH_DST:
988 return eth_addr_to_uint64(flow->dl_dst);
990 case NFI_NXM_OF_ETH_SRC:
991 return eth_addr_to_uint64(flow->dl_src);
993 case NFI_NXM_OF_ETH_TYPE:
994 return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
996 case NFI_NXM_OF_VLAN_TCI:
997 return ntohs(flow->vlan_tci);
999 case NFI_NXM_OF_IP_TOS:
1000 return flow->nw_tos;
1002 case NFI_NXM_OF_IP_PROTO:
1003 case NFI_NXM_OF_ARP_OP:
1004 return flow->nw_proto;
1006 case NFI_NXM_OF_IP_SRC:
1007 case NFI_NXM_OF_ARP_SPA:
1008 return ntohl(flow->nw_src);
1010 case NFI_NXM_OF_IP_DST:
1011 case NFI_NXM_OF_ARP_TPA:
1012 return ntohl(flow->nw_dst);
1014 case NFI_NXM_OF_TCP_SRC:
1015 case NFI_NXM_OF_UDP_SRC:
1016 return ntohs(flow->tp_src);
1018 case NFI_NXM_OF_TCP_DST:
1019 case NFI_NXM_OF_UDP_DST:
1020 return ntohs(flow->tp_dst);
1022 case NFI_NXM_OF_ICMP_TYPE:
1023 case NFI_NXM_NX_ICMPV6_TYPE:
1024 return ntohs(flow->tp_src) & 0xff;
1026 case NFI_NXM_OF_ICMP_CODE:
1027 case NFI_NXM_NX_ICMPV6_CODE:
1028 return ntohs(flow->tp_dst) & 0xff;
1030 case NFI_NXM_NX_TUN_ID:
1031 return ntohll(flow->tun_id);
1033 #define NXM_READ_REGISTER(IDX) \
1034 case NFI_NXM_NX_REG##IDX: \
1035 return flow->regs[IDX]; \
1036 case NFI_NXM_NX_REG##IDX##_W: \
1039 NXM_READ_REGISTER(0);
1040 #if FLOW_N_REGS >= 2
1041 NXM_READ_REGISTER(1);
1043 #if FLOW_N_REGS >= 3
1044 NXM_READ_REGISTER(2);
1046 #if FLOW_N_REGS >= 4
1047 NXM_READ_REGISTER(3);
1053 case NFI_NXM_NX_ARP_SHA:
1054 case NFI_NXM_NX_ND_SLL:
1055 return eth_addr_to_uint64(flow->arp_sha);
1057 case NFI_NXM_NX_ARP_THA:
1058 case NFI_NXM_NX_ND_TLL:
1059 return eth_addr_to_uint64(flow->arp_tha);
1061 case NFI_NXM_NX_TUN_ID_W:
1062 case NFI_NXM_OF_ETH_DST_W:
1063 case NFI_NXM_OF_VLAN_TCI_W:
1064 case NFI_NXM_OF_IP_SRC_W:
1065 case NFI_NXM_OF_IP_DST_W:
1066 case NFI_NXM_OF_ARP_SPA_W:
1067 case NFI_NXM_OF_ARP_TPA_W:
1068 case NFI_NXM_NX_IPV6_SRC:
1069 case NFI_NXM_NX_IPV6_SRC_W:
1070 case NFI_NXM_NX_IPV6_DST:
1071 case NFI_NXM_NX_IPV6_DST_W:
1072 case NFI_NXM_NX_ND_TARGET:
1080 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1083 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1084 const struct flow *flow)
1086 int n_bits = nxm_decode_n_bits(ofs_nbits);
1087 int ofs = nxm_decode_ofs(ofs_nbits);
1088 uint64_t mask, data;
1090 mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1091 data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow);
1092 data = (data >> ofs) & mask;
1098 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1101 switch (dst->index) {
1102 case NFI_NXM_OF_ETH_DST:
1103 eth_addr_from_uint64(new_value, flow->dl_dst);
1106 case NFI_NXM_OF_ETH_SRC:
1107 eth_addr_from_uint64(new_value, flow->dl_src);
1110 case NFI_NXM_OF_VLAN_TCI:
1111 flow->vlan_tci = htons(new_value);
1114 case NFI_NXM_NX_TUN_ID:
1115 flow->tun_id = htonll(new_value);
1118 #define NXM_WRITE_REGISTER(IDX) \
1119 case NFI_NXM_NX_REG##IDX: \
1120 flow->regs[IDX] = new_value; \
1122 case NFI_NXM_NX_REG##IDX##_W: \
1125 NXM_WRITE_REGISTER(0);
1126 #if FLOW_N_REGS >= 2
1127 NXM_WRITE_REGISTER(1);
1129 #if FLOW_N_REGS >= 3
1130 NXM_WRITE_REGISTER(2);
1132 #if FLOW_N_REGS >= 4
1133 NXM_WRITE_REGISTER(3);
1139 case NFI_NXM_OF_IP_TOS:
1140 flow->nw_tos = new_value & IP_DSCP_MASK;
1143 case NFI_NXM_OF_IP_SRC:
1144 flow->nw_src = htonl(new_value);
1147 case NFI_NXM_OF_IP_DST:
1148 flow->nw_dst = htonl(new_value);
1151 case NFI_NXM_OF_TCP_SRC:
1152 case NFI_NXM_OF_UDP_SRC:
1153 flow->tp_src = htons(new_value);
1156 case NFI_NXM_OF_TCP_DST:
1157 case NFI_NXM_OF_UDP_DST:
1158 flow->tp_dst = htons(new_value);
1161 case NFI_NXM_OF_IN_PORT:
1162 case NFI_NXM_OF_ETH_TYPE:
1163 case NFI_NXM_OF_IP_PROTO:
1164 case NFI_NXM_OF_ARP_OP:
1165 case NFI_NXM_OF_ARP_SPA:
1166 case NFI_NXM_OF_ARP_TPA:
1167 case NFI_NXM_OF_ICMP_TYPE:
1168 case NFI_NXM_OF_ICMP_CODE:
1169 case NFI_NXM_NX_TUN_ID_W:
1170 case NFI_NXM_OF_ETH_DST_W:
1171 case NFI_NXM_OF_VLAN_TCI_W:
1172 case NFI_NXM_OF_IP_SRC_W:
1173 case NFI_NXM_OF_IP_DST_W:
1174 case NFI_NXM_OF_ARP_SPA_W:
1175 case NFI_NXM_OF_ARP_TPA_W:
1176 case NFI_NXM_NX_ARP_SHA:
1177 case NFI_NXM_NX_ARP_THA:
1178 case NFI_NXM_NX_IPV6_SRC:
1179 case NFI_NXM_NX_IPV6_SRC_W:
1180 case NFI_NXM_NX_IPV6_DST:
1181 case NFI_NXM_NX_IPV6_DST_W:
1182 case NFI_NXM_NX_ICMPV6_TYPE:
1183 case NFI_NXM_NX_ICMPV6_CODE:
1184 case NFI_NXM_NX_ND_TARGET:
1185 case NFI_NXM_NX_ND_SLL:
1186 case NFI_NXM_NX_ND_TLL:
1193 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1196 ovs_be16 src_ofs_nbits, dst_ofs_nbits;
1200 n_bits = ntohs(action->n_bits);
1201 src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
1202 dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
1204 src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow);
1205 nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
1209 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1212 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1215 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1216 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1218 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1221 int n_bits = nxm_decode_n_bits(ofs_nbits);
1222 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1223 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1225 /* Get remaining bits of the destination field. */
1226 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1227 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1229 /* Get the final value. */
1230 uint64_t new_data = dst_data | (src_data << dst_ofs);
1232 nxm_write_field(dst, flow, new_data);