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_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
271 nxm_put_header(b, header);
272 ofpbuf_put(b, &value, sizeof value);
276 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
278 nxm_put_header(b, header);
279 ofpbuf_put(b, &value, sizeof value);
280 ofpbuf_put(b, &mask, sizeof mask);
284 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
290 case CONSTANT_HTONS(UINT16_MAX):
291 nxm_put_16(b, header, value);
295 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
301 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
303 nxm_put_header(b, header);
304 ofpbuf_put(b, &value, sizeof value);
308 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
310 nxm_put_header(b, header);
311 ofpbuf_put(b, &value, sizeof value);
312 ofpbuf_put(b, &mask, sizeof mask);
316 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
322 case CONSTANT_HTONL(UINT32_MAX):
323 nxm_put_32(b, header, value);
327 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
333 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
335 nxm_put_header(b, header);
336 ofpbuf_put(b, &value, sizeof value);
340 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
342 nxm_put_header(b, header);
343 ofpbuf_put(b, &value, sizeof value);
344 ofpbuf_put(b, &mask, sizeof mask);
348 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
354 case CONSTANT_HTONLL(UINT64_MAX):
355 nxm_put_64(b, header, value);
359 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
365 nxm_put_eth(struct ofpbuf *b, uint32_t header,
366 const uint8_t value[ETH_ADDR_LEN])
368 nxm_put_header(b, header);
369 ofpbuf_put(b, value, ETH_ADDR_LEN);
373 nxm_put_eth_dst(struct ofpbuf *b,
374 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
376 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
377 case FWW_DL_DST | FWW_ETH_MCAST:
380 nxm_put_header(b, NXM_OF_ETH_DST_W);
381 ofpbuf_put(b, value, ETH_ADDR_LEN);
382 ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
385 nxm_put_eth(b, NXM_OF_ETH_DST, value);
391 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
392 const struct in6_addr *value, const struct in6_addr *mask)
394 if (ipv6_mask_is_any(mask)) {
396 } else if (ipv6_mask_is_exact(mask)) {
397 nxm_put_header(b, header);
398 ofpbuf_put(b, value, sizeof *value);
400 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
401 ofpbuf_put(b, value, sizeof *value);
402 ofpbuf_put(b, mask, sizeof *mask);
406 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
407 * 'cr->priority', because priority is not part of nx_match), plus enough
408 * zero bytes to pad the nx_match out to a multiple of 8.
410 * This function can cause 'b''s data to be reallocated.
412 * Returns the number of bytes appended to 'b', excluding padding.
414 * If 'cr' is a catch-all rule that matches every packet, then this function
415 * appends nothing to 'b' and returns 0. */
417 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
419 const flow_wildcards_t wc = cr->wc.wildcards;
420 const struct flow *flow = &cr->flow;
421 const size_t start_len = b->size;
425 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 2);
428 if (!(wc & FWW_IN_PORT)) {
429 uint16_t in_port = flow->in_port;
430 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
434 nxm_put_eth_dst(b, wc, flow->dl_dst);
435 if (!(wc & FWW_DL_SRC)) {
436 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
438 if (!(wc & FWW_DL_TYPE)) {
439 nxm_put_16(b, NXM_OF_ETH_TYPE,
440 ofputil_dl_type_to_openflow(flow->dl_type));
444 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
447 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
449 if (!(wc & FWW_NW_TOS)) {
450 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
452 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
453 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
455 if (!(wc & FWW_NW_PROTO)) {
456 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
457 switch (flow->nw_proto) {
460 if (!(wc & FWW_TP_SRC)) {
461 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
463 if (!(wc & FWW_TP_DST)) {
464 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
470 if (!(wc & FWW_TP_SRC)) {
471 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
473 if (!(wc & FWW_TP_DST)) {
474 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
480 if (!(wc & FWW_TP_SRC)) {
481 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
483 if (!(wc & FWW_TP_DST)) {
484 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
489 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
492 if (!(wc & FWW_NW_TOS)) {
493 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
495 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
496 &cr->wc.ipv6_src_mask);
497 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
498 &cr->wc.ipv6_dst_mask);
500 if (!(wc & FWW_NW_PROTO)) {
501 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
502 switch (flow->nw_proto) {
505 if (!(wc & FWW_TP_SRC)) {
506 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
508 if (!(wc & FWW_TP_DST)) {
509 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
515 if (!(wc & FWW_TP_SRC)) {
516 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
518 if (!(wc & FWW_TP_DST)) {
519 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
525 if (!(wc & FWW_TP_SRC)) {
526 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
528 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
529 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
530 if (!(wc & FWW_ND_TARGET)) {
531 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
534 if (!(wc & FWW_ARP_SHA)
535 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
536 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
538 if (!(wc & FWW_ARP_THA)
539 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
540 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
544 if (!(wc & FWW_TP_DST)) {
545 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
550 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
552 if (!(wc & FWW_NW_PROTO)) {
553 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
555 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
556 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
557 if (!(wc & FWW_ARP_SHA)) {
558 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
560 if (!(wc & FWW_ARP_THA)) {
561 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
566 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
569 for (i = 0; i < FLOW_N_REGS; i++) {
570 nxm_put_32m(b, NXM_NX_REG(i),
571 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
574 match_len = b->size - start_len;
575 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
579 /* nx_match_to_string() and helpers. */
581 static void format_nxm_field_name(struct ds *, uint32_t header);
584 nx_match_to_string(const uint8_t *p, unsigned int match_len)
590 return xstrdup("<any>");
594 while ((header = nx_entry_ok(p, match_len)) != 0) {
595 unsigned int length = NXM_LENGTH(header);
596 unsigned int value_len = nxm_field_bytes(header);
597 const uint8_t *value = p + 4;
598 const uint8_t *mask = value + value_len;
602 ds_put_cstr(&s, ", ");
605 format_nxm_field_name(&s, header);
606 ds_put_char(&s, '(');
608 for (i = 0; i < value_len; i++) {
609 ds_put_format(&s, "%02x", value[i]);
611 if (NXM_HASMASK(header)) {
612 ds_put_char(&s, '/');
613 for (i = 0; i < value_len; i++) {
614 ds_put_format(&s, "%02x", mask[i]);
617 ds_put_char(&s, ')');
620 match_len -= 4 + length;
625 ds_put_cstr(&s, ", ");
628 ds_put_format(&s, "<%u invalid bytes>", match_len);
631 return ds_steal_cstr(&s);
635 format_nxm_field_name(struct ds *s, uint32_t header)
637 const struct nxm_field *f = nxm_field_lookup(header);
639 ds_put_cstr(s, f->name);
641 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
646 parse_nxm_field_name(const char *name, int name_len)
648 const struct nxm_field *f;
650 /* Check whether it's a field name. */
651 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
652 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
657 /* Check whether it's a 32-bit field header value as hex.
658 * (This isn't ordinarily useful except for testing error behavior.) */
660 uint32_t header = hexits_value(name, name_len, NULL);
661 if (header != UINT_MAX) {
669 /* nx_match_from_string(). */
672 nx_match_from_string(const char *s, struct ofpbuf *b)
674 const char *full_s = s;
675 const size_t start_len = b->size;
678 if (!strcmp(s, "<any>")) {
679 /* Ensure that 'b->data' isn't actually null. */
680 ofpbuf_prealloc_tailroom(b, 1);
684 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
691 name_len = strcspn(s, "(");
692 if (s[name_len] != '(') {
693 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
696 header = parse_nxm_field_name(name, name_len);
698 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
703 nxm_put_header(b, header);
704 s = ofpbuf_put_hex(b, s, &n);
705 if (n != nxm_field_bytes(header)) {
706 ovs_fatal(0, "%.2s: hex digits expected", s);
708 if (NXM_HASMASK(header)) {
711 ovs_fatal(0, "%s: missing / in masked field %.*s",
712 full_s, name_len, name);
714 s = ofpbuf_put_hex(b, s + 1, &n);
715 if (n != nxm_field_bytes(header)) {
716 ovs_fatal(0, "%.2s: hex digits expected", s);
722 ovs_fatal(0, "%s: missing ) following field %.*s",
723 full_s, name_len, name);
728 match_len = b->size - start_len;
729 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
734 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
736 const char *full_s = s;
744 name_len = strcspn(s, "[");
745 if (s[name_len] != '[') {
746 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
749 header = parse_nxm_field_name(name, name_len);
751 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
753 width = nxm_field_bits(header);
756 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
758 } else if (sscanf(s, "[%d]", &start) == 1) {
760 } else if (!strncmp(s, "[]", 2)) {
764 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
765 "[<start>..<end>]", full_s);
767 s = strchr(s, ']') + 1;
770 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
772 } else if (start >= width) {
773 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
774 "%d bits wide", full_s, start, width);
775 } else if (end >= width){
776 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
777 "%d bits wide", full_s, end, width);
782 *n_bitsp = end - start + 1;
788 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
790 const char *full_s = s;
792 int src_ofs, dst_ofs;
793 int src_n_bits, dst_n_bits;
795 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
796 if (strncmp(s, "->", 2)) {
797 ovs_fatal(0, "%s: missing `->' following source", full_s);
800 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
802 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
805 if (src_n_bits != dst_n_bits) {
806 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
807 "%d bits wide", full_s, src_n_bits, dst_n_bits);
810 ofputil_init_NXAST_REG_MOVE(move);
811 move->n_bits = htons(src_n_bits);
812 move->src_ofs = htons(src_ofs);
813 move->dst_ofs = htons(dst_ofs);
814 move->src = htonl(src);
815 move->dst = htonl(dst);
819 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
821 const char *full_s = s;
826 value = strtoull(s, (char **) &s, 0);
827 if (strncmp(s, "->", 2)) {
828 ovs_fatal(0, "%s: missing `->' following value", full_s);
831 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
833 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
836 if (n_bits < 64 && (value >> n_bits) != 0) {
837 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
838 full_s, value, n_bits);
841 ofputil_init_NXAST_REG_LOAD(load);
842 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
843 load->dst = htonl(dst);
844 load->value = htonll(value);
847 /* nxm_format_reg_move(), nxm_format_reg_load(). */
850 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
852 format_nxm_field_name(s, header);
853 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
854 ds_put_cstr(s, "[]");
855 } else if (n_bits == 1) {
856 ds_put_format(s, "[%d]", ofs);
858 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
863 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
865 int n_bits = ntohs(move->n_bits);
866 int src_ofs = ntohs(move->src_ofs);
867 int dst_ofs = ntohs(move->dst_ofs);
868 uint32_t src = ntohl(move->src);
869 uint32_t dst = ntohl(move->dst);
871 ds_put_format(s, "move:");
872 nxm_format_field_bits(s, src, src_ofs, n_bits);
873 ds_put_cstr(s, "->");
874 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
878 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
880 int ofs = nxm_decode_ofs(load->ofs_nbits);
881 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
882 uint32_t dst = ntohl(load->dst);
883 uint64_t value = ntohll(load->value);
885 ds_put_format(s, "load:%#"PRIx64"->", value);
886 nxm_format_field_bits(s, dst, ofs, n_bits);
889 /* nxm_check_reg_move(), nxm_check_reg_load(). */
892 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
895 && !NXM_HASMASK(f->header)
896 && mf_are_prereqs_ok(f->mf, flow)
897 && size <= nxm_field_bits(f->header));
901 nxm_check_reg_move(const struct nx_action_reg_move *action,
902 const struct flow *flow)
904 int src_ofs, dst_ofs, n_bits;
907 n_bits = ntohs(action->n_bits);
908 src_ofs = ntohs(action->src_ofs);
909 dst_ofs = ntohs(action->dst_ofs);
911 error = nxm_src_check(action->src, src_ofs, n_bits, flow);
916 return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
919 /* Given a flow, checks that the source field represented by 'src_header'
920 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
922 nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits,
923 const struct flow *flow)
925 const struct nxm_field *src = nxm_field_lookup(ntohl(src_header));
928 VLOG_WARN_RL(&rl, "zero bit source field");
929 } else if (!field_ok(src, flow, ofs + n_bits)) {
930 VLOG_WARN_RL(&rl, "invalid source field");
938 /* Given a flow, checks that the destination field represented by 'dst_header'
939 * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
941 nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits,
942 const struct flow *flow)
944 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
947 VLOG_WARN_RL(&rl, "zero bit destination field");
948 } else if (!field_ok(dst, flow, ofs + n_bits)) {
949 VLOG_WARN_RL(&rl, "invalid destination field");
950 } else if (!dst->writable) {
951 VLOG_WARN_RL(&rl, "destination field is not writable");
960 nxm_check_reg_load(const struct nx_action_reg_load *action,
961 const struct flow *flow)
963 unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
964 unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
967 error = nxm_dst_check(action->dst, ofs, n_bits, flow);
972 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
974 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
981 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
984 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
986 switch (src->index) {
987 case NFI_NXM_OF_IN_PORT:
988 return flow->in_port;
990 case NFI_NXM_OF_ETH_DST:
991 return eth_addr_to_uint64(flow->dl_dst);
993 case NFI_NXM_OF_ETH_SRC:
994 return eth_addr_to_uint64(flow->dl_src);
996 case NFI_NXM_OF_ETH_TYPE:
997 return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
999 case NFI_NXM_OF_VLAN_TCI:
1000 return ntohs(flow->vlan_tci);
1002 case NFI_NXM_OF_IP_TOS:
1003 return flow->nw_tos;
1005 case NFI_NXM_OF_IP_PROTO:
1006 case NFI_NXM_OF_ARP_OP:
1007 return flow->nw_proto;
1009 case NFI_NXM_OF_IP_SRC:
1010 case NFI_NXM_OF_ARP_SPA:
1011 return ntohl(flow->nw_src);
1013 case NFI_NXM_OF_IP_DST:
1014 case NFI_NXM_OF_ARP_TPA:
1015 return ntohl(flow->nw_dst);
1017 case NFI_NXM_OF_TCP_SRC:
1018 case NFI_NXM_OF_UDP_SRC:
1019 return ntohs(flow->tp_src);
1021 case NFI_NXM_OF_TCP_DST:
1022 case NFI_NXM_OF_UDP_DST:
1023 return ntohs(flow->tp_dst);
1025 case NFI_NXM_OF_ICMP_TYPE:
1026 case NFI_NXM_NX_ICMPV6_TYPE:
1027 return ntohs(flow->tp_src) & 0xff;
1029 case NFI_NXM_OF_ICMP_CODE:
1030 case NFI_NXM_NX_ICMPV6_CODE:
1031 return ntohs(flow->tp_dst) & 0xff;
1033 case NFI_NXM_NX_TUN_ID:
1034 return ntohll(flow->tun_id);
1036 #define NXM_READ_REGISTER(IDX) \
1037 case NFI_NXM_NX_REG##IDX: \
1038 return flow->regs[IDX]; \
1039 case NFI_NXM_NX_REG##IDX##_W: \
1042 NXM_READ_REGISTER(0);
1043 #if FLOW_N_REGS >= 2
1044 NXM_READ_REGISTER(1);
1046 #if FLOW_N_REGS >= 3
1047 NXM_READ_REGISTER(2);
1049 #if FLOW_N_REGS >= 4
1050 NXM_READ_REGISTER(3);
1052 #if FLOW_N_REGS >= 5
1053 NXM_READ_REGISTER(4);
1059 case NFI_NXM_NX_ARP_SHA:
1060 case NFI_NXM_NX_ND_SLL:
1061 return eth_addr_to_uint64(flow->arp_sha);
1063 case NFI_NXM_NX_ARP_THA:
1064 case NFI_NXM_NX_ND_TLL:
1065 return eth_addr_to_uint64(flow->arp_tha);
1067 case NFI_NXM_NX_TUN_ID_W:
1068 case NFI_NXM_OF_ETH_DST_W:
1069 case NFI_NXM_OF_VLAN_TCI_W:
1070 case NFI_NXM_OF_IP_SRC_W:
1071 case NFI_NXM_OF_IP_DST_W:
1072 case NFI_NXM_OF_ARP_SPA_W:
1073 case NFI_NXM_OF_ARP_TPA_W:
1074 case NFI_NXM_NX_IPV6_SRC:
1075 case NFI_NXM_NX_IPV6_SRC_W:
1076 case NFI_NXM_NX_IPV6_DST:
1077 case NFI_NXM_NX_IPV6_DST_W:
1078 case NFI_NXM_NX_ND_TARGET:
1086 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1089 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1090 const struct flow *flow)
1092 int n_bits = nxm_decode_n_bits(ofs_nbits);
1093 int ofs = nxm_decode_ofs(ofs_nbits);
1094 uint64_t mask, data;
1096 mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1097 data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow);
1098 data = (data >> ofs) & mask;
1104 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1107 switch (dst->index) {
1108 case NFI_NXM_OF_ETH_DST:
1109 eth_addr_from_uint64(new_value, flow->dl_dst);
1112 case NFI_NXM_OF_ETH_SRC:
1113 eth_addr_from_uint64(new_value, flow->dl_src);
1116 case NFI_NXM_OF_VLAN_TCI:
1117 flow->vlan_tci = htons(new_value);
1120 case NFI_NXM_NX_TUN_ID:
1121 flow->tun_id = htonll(new_value);
1124 #define NXM_WRITE_REGISTER(IDX) \
1125 case NFI_NXM_NX_REG##IDX: \
1126 flow->regs[IDX] = new_value; \
1128 case NFI_NXM_NX_REG##IDX##_W: \
1131 NXM_WRITE_REGISTER(0);
1132 #if FLOW_N_REGS >= 2
1133 NXM_WRITE_REGISTER(1);
1135 #if FLOW_N_REGS >= 3
1136 NXM_WRITE_REGISTER(2);
1138 #if FLOW_N_REGS >= 4
1139 NXM_WRITE_REGISTER(3);
1141 #if FLOW_N_REGS >= 5
1142 NXM_WRITE_REGISTER(4);
1148 case NFI_NXM_OF_IP_TOS:
1149 flow->nw_tos = new_value & IP_DSCP_MASK;
1152 case NFI_NXM_OF_IP_SRC:
1153 flow->nw_src = htonl(new_value);
1156 case NFI_NXM_OF_IP_DST:
1157 flow->nw_dst = htonl(new_value);
1160 case NFI_NXM_OF_TCP_SRC:
1161 case NFI_NXM_OF_UDP_SRC:
1162 flow->tp_src = htons(new_value);
1165 case NFI_NXM_OF_TCP_DST:
1166 case NFI_NXM_OF_UDP_DST:
1167 flow->tp_dst = htons(new_value);
1170 case NFI_NXM_OF_IN_PORT:
1171 case NFI_NXM_OF_ETH_TYPE:
1172 case NFI_NXM_OF_IP_PROTO:
1173 case NFI_NXM_OF_ARP_OP:
1174 case NFI_NXM_OF_ARP_SPA:
1175 case NFI_NXM_OF_ARP_TPA:
1176 case NFI_NXM_OF_ICMP_TYPE:
1177 case NFI_NXM_OF_ICMP_CODE:
1178 case NFI_NXM_NX_TUN_ID_W:
1179 case NFI_NXM_OF_ETH_DST_W:
1180 case NFI_NXM_OF_VLAN_TCI_W:
1181 case NFI_NXM_OF_IP_SRC_W:
1182 case NFI_NXM_OF_IP_DST_W:
1183 case NFI_NXM_OF_ARP_SPA_W:
1184 case NFI_NXM_OF_ARP_TPA_W:
1185 case NFI_NXM_NX_ARP_SHA:
1186 case NFI_NXM_NX_ARP_THA:
1187 case NFI_NXM_NX_IPV6_SRC:
1188 case NFI_NXM_NX_IPV6_SRC_W:
1189 case NFI_NXM_NX_IPV6_DST:
1190 case NFI_NXM_NX_IPV6_DST_W:
1191 case NFI_NXM_NX_ICMPV6_TYPE:
1192 case NFI_NXM_NX_ICMPV6_CODE:
1193 case NFI_NXM_NX_ND_TARGET:
1194 case NFI_NXM_NX_ND_SLL:
1195 case NFI_NXM_NX_ND_TLL:
1202 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1205 ovs_be16 src_ofs_nbits, dst_ofs_nbits;
1209 n_bits = ntohs(action->n_bits);
1210 src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
1211 dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
1213 src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow);
1214 nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
1218 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1221 nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1224 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1225 * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1227 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1230 int n_bits = nxm_decode_n_bits(ofs_nbits);
1231 int dst_ofs = nxm_decode_ofs(ofs_nbits);
1232 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1234 /* Get remaining bits of the destination field. */
1235 const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1236 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1238 /* Get the final value. */
1239 uint64_t new_data = dst_data | (src_data << dst_ofs);
1241 nxm_write_field(dst, flow, new_data);