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"
27 #include "openflow/nicira-ext.h"
29 #include "unaligned.h"
32 VLOG_DEFINE_THIS_MODULE(nx_match);
34 /* Rate limit for nx_match parse errors. These always indicate a bug in the
35 * peer and so there's not much point in showing a lot of them. */
36 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39 NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
40 NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
41 NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
42 NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
43 NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
44 NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
45 BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
48 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
50 enum nxm_field_index {
51 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPES, NW_PROTO, WRITABLE) \
53 #include "nx-match.def"
58 struct hmap_node hmap_node;
59 enum nxm_field_index index; /* NFI_* value. */
60 uint32_t header; /* NXM_* value. */
61 flow_wildcards_t wildcard; /* FWW_* bit, if exactly one. */
62 ovs_be16 dl_type[N_NXM_DL_TYPES]; /* dl_type prerequisites. */
63 uint8_t nw_proto; /* nw_proto prerequisite, if nonzero. */
64 const char *name; /* "NXM_*" string. */
65 bool writable; /* Writable with NXAST_REG_{MOVE,LOAD}? */
69 /* All the known fields. */
70 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
71 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPES, NW_PROTO, WRITABLE) \
72 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
73 DL_CONVERT DL_TYPES, NW_PROTO, "NXM_" #HEADER, WRITABLE },
74 #define DL_CONVERT(T1, T2) { CONSTANT_HTONS(T1), CONSTANT_HTONS(T2) }
75 #include "nx-match.def"
78 /* Hash table of 'nxm_fields'. */
79 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
81 /* Possible masks for NXM_OF_ETH_DST_W. */
82 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
83 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
84 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
85 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
86 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
87 = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
88 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
89 = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
94 if (hmap_is_empty(&all_nxm_fields)) {
97 for (i = 0; i < N_NXM_FIELDS; i++) {
98 struct nxm_field *f = &nxm_fields[i];
99 hmap_insert(&all_nxm_fields, &f->hmap_node,
100 hash_int(f->header, 0));
103 /* Verify that the header values are unique (duplicate "case" values
104 * cause a compile error). */
106 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO, WRITABLE) \
107 case NXM_##HEADER: break;
108 #include "nx-match.def"
113 static const struct nxm_field *
114 nxm_field_lookup(uint32_t header)
120 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
122 if (f->header == header) {
130 /* Returns the width of the data for a field with the given 'header', in
133 nxm_field_bytes(uint32_t header)
135 unsigned int length = NXM_LENGTH(header);
136 return NXM_HASMASK(header) ? length / 2 : length;
139 /* Returns the width of the data for a field with the given 'header', in
142 nxm_field_bits(uint32_t header)
144 return nxm_field_bytes(header) * 8;
147 /* nx_pull_match() and helpers. */
150 parse_nx_reg(const struct nxm_field *f,
151 struct flow *flow, struct flow_wildcards *wc,
152 const void *value, const void *maskp)
154 int idx = NXM_NX_REG_IDX(f->header);
155 if (wc->reg_masks[idx]) {
158 flow_wildcards_set_reg_mask(wc, idx,
159 (NXM_HASMASK(f->header)
160 ? ntohl(get_unaligned_be32(maskp))
162 flow->regs[idx] = ntohl(get_unaligned_be32(value));
163 flow->regs[idx] &= wc->reg_masks[idx];
169 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
170 const void *value, const void *mask)
172 struct flow_wildcards *wc = &rule->wc;
173 struct flow *flow = &rule->flow;
177 case NFI_NXM_OF_IN_PORT:
178 flow->in_port = ntohs(get_unaligned_be16(value));
179 if (flow->in_port == OFPP_LOCAL) {
180 flow->in_port = ODPP_LOCAL;
184 /* Ethernet header. */
185 case NFI_NXM_OF_ETH_DST:
186 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
187 != (FWW_DL_DST | FWW_ETH_MCAST)) {
190 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
191 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
194 case NFI_NXM_OF_ETH_DST_W:
195 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
196 != (FWW_DL_DST | FWW_ETH_MCAST)) {
198 } else if (eth_addr_equals(mask, eth_mcast_1)) {
199 wc->wildcards &= ~FWW_ETH_MCAST;
200 flow->dl_dst[0] = *(uint8_t *) value & 0x01;
201 } else if (eth_addr_equals(mask, eth_mcast_0)) {
202 wc->wildcards &= ~FWW_DL_DST;
203 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
204 flow->dl_dst[0] &= 0xfe;
205 } else if (eth_addr_equals(mask, eth_all_0s)) {
207 } else if (eth_addr_equals(mask, eth_all_1s)) {
208 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
209 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
214 case NFI_NXM_OF_ETH_SRC:
215 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
217 case NFI_NXM_OF_ETH_TYPE:
218 flow->dl_type = ofputil_dl_type_from_openflow(get_unaligned_be16(value));
222 case NFI_NXM_OF_VLAN_TCI:
223 if (wc->vlan_tci_mask) {
226 cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
229 case NFI_NXM_OF_VLAN_TCI_W:
230 if (wc->vlan_tci_mask) {
233 cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
234 get_unaligned_be16(mask));
239 case NFI_NXM_OF_IP_TOS:
240 if (*(uint8_t *) value & 0x03) {
241 return NXM_BAD_VALUE;
243 flow->nw_tos = *(uint8_t *) value;
246 case NFI_NXM_OF_IP_PROTO:
247 flow->nw_proto = *(uint8_t *) value;
250 /* IP addresses in IP and ARP headers. */
251 case NFI_NXM_OF_IP_SRC:
252 case NFI_NXM_OF_ARP_SPA:
253 if (wc->nw_src_mask) {
256 cls_rule_set_nw_src(rule, get_unaligned_be32(value));
259 case NFI_NXM_OF_IP_SRC_W:
260 case NFI_NXM_OF_ARP_SPA_W:
261 if (wc->nw_src_mask) {
264 ovs_be32 ip = get_unaligned_be32(value);
265 ovs_be32 netmask = get_unaligned_be32(mask);
266 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
271 case NFI_NXM_OF_IP_DST:
272 case NFI_NXM_OF_ARP_TPA:
273 if (wc->nw_dst_mask) {
276 cls_rule_set_nw_dst(rule, get_unaligned_be32(value));
279 case NFI_NXM_OF_IP_DST_W:
280 case NFI_NXM_OF_ARP_TPA_W:
281 if (wc->nw_dst_mask) {
284 ovs_be32 ip = get_unaligned_be32(value);
285 ovs_be32 netmask = get_unaligned_be32(mask);
286 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
292 /* IPv6 addresses. */
293 case NFI_NXM_NX_IPV6_SRC:
294 if (!ipv6_mask_is_any(&wc->ipv6_src_mask)) {
297 struct in6_addr ipv6;
298 memcpy(&ipv6, value, sizeof ipv6);
299 cls_rule_set_ipv6_src(rule, &ipv6);
302 case NFI_NXM_NX_IPV6_SRC_W:
303 if (!ipv6_mask_is_any(&wc->ipv6_src_mask)) {
306 struct in6_addr ipv6, netmask;
307 memcpy(&ipv6, value, sizeof ipv6);
308 memcpy(&netmask, mask, sizeof netmask);
309 if (!cls_rule_set_ipv6_src_masked(rule, &ipv6, &netmask)) {
314 case NFI_NXM_NX_IPV6_DST:
315 if (!ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
318 struct in6_addr ipv6;
319 memcpy(&ipv6, value, sizeof ipv6);
320 cls_rule_set_ipv6_dst(rule, &ipv6);
323 case NFI_NXM_NX_IPV6_DST_W:
324 if (!ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
327 struct in6_addr ipv6, netmask;
328 memcpy(&ipv6, value, sizeof ipv6);
329 memcpy(&netmask, mask, sizeof netmask);
330 if (!cls_rule_set_ipv6_dst_masked(rule, &ipv6, &netmask)) {
337 case NFI_NXM_OF_TCP_SRC:
338 flow->tp_src = get_unaligned_be16(value);
340 case NFI_NXM_OF_TCP_DST:
341 flow->tp_dst = get_unaligned_be16(value);
345 case NFI_NXM_OF_UDP_SRC:
346 flow->tp_src = get_unaligned_be16(value);
348 case NFI_NXM_OF_UDP_DST:
349 flow->tp_dst = get_unaligned_be16(value);
353 case NFI_NXM_OF_ICMP_TYPE:
354 flow->tp_src = htons(*(uint8_t *) value);
356 case NFI_NXM_OF_ICMP_CODE:
357 flow->tp_dst = htons(*(uint8_t *) value);
361 case NFI_NXM_NX_ICMPV6_TYPE:
362 flow->tp_src = htons(*(uint8_t *) value);
364 case NFI_NXM_NX_ICMPV6_CODE:
365 flow->tp_dst = htons(*(uint8_t *) value);
368 /* IPv6 Neighbor Discovery. */
369 case NFI_NXM_NX_ND_TARGET:
370 /* We've already verified that it's an ICMPv6 message. */
371 if ((flow->tp_src != htons(ND_NEIGHBOR_SOLICIT))
372 && (flow->tp_src != htons(ND_NEIGHBOR_ADVERT))) {
373 return NXM_BAD_PREREQ;
375 memcpy(&flow->nd_target, value, sizeof flow->nd_target);
377 case NFI_NXM_NX_ND_SLL:
378 /* We've already verified that it's an ICMPv6 message. */
379 if (flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)) {
380 return NXM_BAD_PREREQ;
382 memcpy(flow->arp_sha, value, ETH_ADDR_LEN);
384 case NFI_NXM_NX_ND_TLL:
385 /* We've already verified that it's an ICMPv6 message. */
386 if (flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
387 return NXM_BAD_PREREQ;
389 memcpy(flow->arp_tha, value, ETH_ADDR_LEN);
393 case NFI_NXM_OF_ARP_OP:
394 if (ntohs(get_unaligned_be16(value)) > 255) {
395 return NXM_BAD_VALUE;
397 flow->nw_proto = ntohs(get_unaligned_be16(value));
401 case NFI_NXM_NX_ARP_SHA:
402 memcpy(flow->arp_sha, value, ETH_ADDR_LEN);
404 case NFI_NXM_NX_ARP_THA:
405 memcpy(flow->arp_tha, value, ETH_ADDR_LEN);
409 case NFI_NXM_NX_TUN_ID:
410 if (wc->tun_id_mask) {
413 cls_rule_set_tun_id(rule, get_unaligned_be64(value));
416 case NFI_NXM_NX_TUN_ID_W:
417 if (wc->tun_id_mask) {
420 ovs_be64 tun_id = get_unaligned_be64(value);
421 ovs_be64 tun_mask = get_unaligned_be64(mask);
422 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
427 case NFI_NXM_NX_REG0:
428 case NFI_NXM_NX_REG0_W:
430 case NFI_NXM_NX_REG1:
431 case NFI_NXM_NX_REG1_W:
434 case NFI_NXM_NX_REG2:
435 case NFI_NXM_NX_REG2_W:
438 case NFI_NXM_NX_REG3:
439 case NFI_NXM_NX_REG3_W:
444 return parse_nx_reg(f, flow, wc, value, mask);
453 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
455 if (field->nw_proto && field->nw_proto != flow->nw_proto) {
459 if (!field->dl_type[0]) {
461 } else if (field->dl_type[0] == flow->dl_type) {
463 } else if (field->dl_type[1] && field->dl_type[1] == flow->dl_type) {
471 nx_entry_ok(const void *p, unsigned int match_len)
473 unsigned int payload_len;
479 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
483 memcpy(&header_be, p, 4);
484 header = ntohl(header_be);
486 payload_len = NXM_LENGTH(header);
488 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
492 if (match_len < payload_len + 4) {
493 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
494 "%u bytes left in nx_match", payload_len + 4, match_len);
502 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
503 struct cls_rule *rule)
508 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
510 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
511 "multiple of 8, is longer than space in message (max "
512 "length %zu)", match_len, b->size);
513 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
516 cls_rule_init_catchall(rule, priority);
517 while ((header = nx_entry_ok(p, match_len)) != 0) {
518 unsigned length = NXM_LENGTH(header);
519 const struct nxm_field *f;
522 f = nxm_field_lookup(header);
524 error = NXM_BAD_TYPE;
525 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
526 error = NXM_BAD_PREREQ;
527 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
528 error = NXM_DUP_TYPE;
530 /* 'hasmask' and 'length' are known to be correct at this point
531 * because they are included in 'header' and nxm_field_lookup()
532 * checked them already. */
533 rule->wc.wildcards &= ~f->wildcard;
534 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
537 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
538 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
540 NXM_VENDOR(header), NXM_FIELD(header),
541 NXM_HASMASK(header), NXM_TYPE(header),
548 match_len -= 4 + length;
551 return match_len ? NXM_INVALID : 0;
554 /* nx_put_match() and helpers.
556 * 'put' functions whose names end in 'w' add a wildcarded field.
557 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
558 * Other 'put' functions add exact-match fields.
562 nxm_put_header(struct ofpbuf *b, uint32_t header)
564 ovs_be32 n_header = htonl(header);
565 ofpbuf_put(b, &n_header, sizeof n_header);
569 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
571 nxm_put_header(b, header);
572 ofpbuf_put(b, &value, sizeof value);
576 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
578 nxm_put_header(b, header);
579 ofpbuf_put(b, &value, sizeof value);
583 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
585 nxm_put_header(b, header);
586 ofpbuf_put(b, &value, sizeof value);
587 ofpbuf_put(b, &mask, sizeof mask);
591 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
597 case CONSTANT_HTONS(UINT16_MAX):
598 nxm_put_16(b, header, value);
602 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
608 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
610 nxm_put_header(b, header);
611 ofpbuf_put(b, &value, sizeof value);
615 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
617 nxm_put_header(b, header);
618 ofpbuf_put(b, &value, sizeof value);
619 ofpbuf_put(b, &mask, sizeof mask);
623 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
629 case CONSTANT_HTONL(UINT32_MAX):
630 nxm_put_32(b, header, value);
634 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
640 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
642 nxm_put_header(b, header);
643 ofpbuf_put(b, &value, sizeof value);
647 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
649 nxm_put_header(b, header);
650 ofpbuf_put(b, &value, sizeof value);
651 ofpbuf_put(b, &mask, sizeof mask);
655 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
661 case CONSTANT_HTONLL(UINT64_MAX):
662 nxm_put_64(b, header, value);
666 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
672 nxm_put_eth(struct ofpbuf *b, uint32_t header,
673 const uint8_t value[ETH_ADDR_LEN])
675 nxm_put_header(b, header);
676 ofpbuf_put(b, value, ETH_ADDR_LEN);
680 nxm_put_eth_dst(struct ofpbuf *b,
681 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
683 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
684 case FWW_DL_DST | FWW_ETH_MCAST:
687 nxm_put_header(b, NXM_OF_ETH_DST_W);
688 ofpbuf_put(b, value, ETH_ADDR_LEN);
689 ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
692 nxm_put_header(b, NXM_OF_ETH_DST_W);
693 ofpbuf_put(b, value, ETH_ADDR_LEN);
694 ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
697 nxm_put_eth(b, NXM_OF_ETH_DST, value);
703 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
704 const struct in6_addr *value, const struct in6_addr *mask)
706 if (ipv6_mask_is_any(mask)) {
708 } else if (ipv6_mask_is_exact(mask)) {
709 nxm_put_header(b, header);
710 ofpbuf_put(b, value, sizeof *value);
712 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
713 ofpbuf_put(b, value, sizeof *value);
714 ofpbuf_put(b, mask, sizeof *mask);
718 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
719 * 'cr->priority', because priority is not part of nx_match), plus enough
720 * zero bytes to pad the nx_match out to a multiple of 8.
722 * This function can cause 'b''s data to be reallocated.
724 * Returns the number of bytes appended to 'b', excluding padding.
726 * If 'cr' is a catch-all rule that matches every packet, then this function
727 * appends nothing to 'b' and returns 0. */
729 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
731 const flow_wildcards_t wc = cr->wc.wildcards;
732 const struct flow *flow = &cr->flow;
733 const size_t start_len = b->size;
738 if (!(wc & FWW_IN_PORT)) {
739 uint16_t in_port = flow->in_port;
740 if (in_port == ODPP_LOCAL) {
741 in_port = OFPP_LOCAL;
743 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
747 nxm_put_eth_dst(b, wc, flow->dl_dst);
748 if (!(wc & FWW_DL_SRC)) {
749 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
751 if (!(wc & FWW_DL_TYPE)) {
752 nxm_put_16(b, NXM_OF_ETH_TYPE,
753 ofputil_dl_type_to_openflow(flow->dl_type));
757 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
760 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
762 if (!(wc & FWW_NW_TOS)) {
763 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
765 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
766 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
768 if (!(wc & FWW_NW_PROTO)) {
769 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
770 switch (flow->nw_proto) {
773 if (!(wc & FWW_TP_SRC)) {
774 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
776 if (!(wc & FWW_TP_DST)) {
777 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
783 if (!(wc & FWW_TP_SRC)) {
784 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
786 if (!(wc & FWW_TP_DST)) {
787 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
793 if (!(wc & FWW_TP_SRC)) {
794 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
796 if (!(wc & FWW_TP_DST)) {
797 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
802 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
805 if (!(wc & FWW_NW_TOS)) {
806 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
808 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
809 &cr->wc.ipv6_src_mask);
810 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
811 &cr->wc.ipv6_dst_mask);
813 if (!(wc & FWW_NW_PROTO)) {
814 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
815 switch (flow->nw_proto) {
818 if (!(wc & FWW_TP_SRC)) {
819 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
821 if (!(wc & FWW_TP_DST)) {
822 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
828 if (!(wc & FWW_TP_SRC)) {
829 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
831 if (!(wc & FWW_TP_DST)) {
832 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
838 if (!(wc & FWW_TP_SRC)) {
839 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
841 if (!(wc & FWW_TP_DST)) {
842 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
844 if (!(wc & FWW_ND_TARGET)) {
845 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
848 if (!(wc & FWW_ARP_SHA)) {
849 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
851 if (!(wc & FWW_ARP_THA)) {
852 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
857 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
859 if (!(wc & FWW_NW_PROTO)) {
860 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
862 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
863 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
864 if (!(wc & FWW_ARP_SHA)) {
865 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
867 if (!(wc & FWW_ARP_THA)) {
868 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
873 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
876 for (i = 0; i < FLOW_N_REGS; i++) {
877 nxm_put_32m(b, NXM_NX_REG(i),
878 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
881 match_len = b->size - start_len;
882 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
886 /* nx_match_to_string() and helpers. */
888 static void format_nxm_field_name(struct ds *, uint32_t header);
891 nx_match_to_string(const uint8_t *p, unsigned int match_len)
897 return xstrdup("<any>");
901 while ((header = nx_entry_ok(p, match_len)) != 0) {
902 unsigned int length = NXM_LENGTH(header);
903 unsigned int value_len = nxm_field_bytes(header);
904 const uint8_t *value = p + 4;
905 const uint8_t *mask = value + value_len;
909 ds_put_cstr(&s, ", ");
912 format_nxm_field_name(&s, header);
913 ds_put_char(&s, '(');
915 for (i = 0; i < value_len; i++) {
916 ds_put_format(&s, "%02x", value[i]);
918 if (NXM_HASMASK(header)) {
919 ds_put_char(&s, '/');
920 for (i = 0; i < value_len; i++) {
921 ds_put_format(&s, "%02x", mask[i]);
924 ds_put_char(&s, ')');
927 match_len -= 4 + length;
932 ds_put_cstr(&s, ", ");
935 ds_put_format(&s, "<%u invalid bytes>", match_len);
938 return ds_steal_cstr(&s);
942 format_nxm_field_name(struct ds *s, uint32_t header)
944 const struct nxm_field *f = nxm_field_lookup(header);
946 ds_put_cstr(s, f->name);
948 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
953 parse_nxm_field_name(const char *name, int name_len)
955 const struct nxm_field *f;
957 /* Check whether it's a field name. */
958 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
959 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
964 /* Check whether it's a 32-bit field header value as hex.
965 * (This isn't ordinarily useful except for testing error behavior.) */
967 uint32_t header = hexits_value(name, name_len, NULL);
968 if (header != UINT_MAX) {
976 /* nx_match_from_string(). */
979 nx_match_from_string(const char *s, struct ofpbuf *b)
981 const char *full_s = s;
982 const size_t start_len = b->size;
985 if (!strcmp(s, "<any>")) {
986 /* Ensure that 'b->data' isn't actually null. */
987 ofpbuf_prealloc_tailroom(b, 1);
991 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
998 name_len = strcspn(s, "(");
999 if (s[name_len] != '(') {
1000 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
1003 header = parse_nxm_field_name(name, name_len);
1005 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1010 nxm_put_header(b, header);
1011 s = ofpbuf_put_hex(b, s, &n);
1012 if (n != nxm_field_bytes(header)) {
1013 ovs_fatal(0, "%.2s: hex digits expected", s);
1015 if (NXM_HASMASK(header)) {
1016 s += strspn(s, " ");
1018 ovs_fatal(0, "%s: missing / in masked field %.*s",
1019 full_s, name_len, name);
1021 s = ofpbuf_put_hex(b, s + 1, &n);
1022 if (n != nxm_field_bytes(header)) {
1023 ovs_fatal(0, "%.2s: hex digits expected", s);
1027 s += strspn(s, " ");
1029 ovs_fatal(0, "%s: missing ) following field %.*s",
1030 full_s, name_len, name);
1035 match_len = b->size - start_len;
1036 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
1041 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
1043 const char *full_s = s;
1051 name_len = strcspn(s, "[");
1052 if (s[name_len] != '[') {
1053 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
1056 header = parse_nxm_field_name(name, name_len);
1058 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1060 width = nxm_field_bits(header);
1063 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
1064 /* Nothing to do. */
1065 } else if (sscanf(s, "[%d]", &start) == 1) {
1067 } else if (!strncmp(s, "[]", 2)) {
1071 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
1072 "[<start>..<end>]", full_s);
1074 s = strchr(s, ']') + 1;
1077 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
1078 full_s, start, end);
1079 } else if (start >= width) {
1080 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
1081 "%d bits wide", full_s, start, width);
1082 } else if (end >= width){
1083 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
1084 "%d bits wide", full_s, end, width);
1089 *n_bitsp = end - start + 1;
1095 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
1097 const char *full_s = s;
1099 int src_ofs, dst_ofs;
1100 int src_n_bits, dst_n_bits;
1102 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
1103 if (strncmp(s, "->", 2)) {
1104 ovs_fatal(0, "%s: missing `->' following source", full_s);
1107 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
1109 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1112 if (src_n_bits != dst_n_bits) {
1113 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
1114 "%d bits wide", full_s, src_n_bits, dst_n_bits);
1117 move->type = htons(OFPAT_VENDOR);
1118 move->len = htons(sizeof *move);
1119 move->vendor = htonl(NX_VENDOR_ID);
1120 move->subtype = htons(NXAST_REG_MOVE);
1121 move->n_bits = htons(src_n_bits);
1122 move->src_ofs = htons(src_ofs);
1123 move->dst_ofs = htons(dst_ofs);
1124 move->src = htonl(src);
1125 move->dst = htonl(dst);
1129 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
1131 const char *full_s = s;
1136 value = strtoull(s, (char **) &s, 0);
1137 if (strncmp(s, "->", 2)) {
1138 ovs_fatal(0, "%s: missing `->' following value", full_s);
1141 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
1143 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1146 if (n_bits < 64 && (value >> n_bits) != 0) {
1147 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
1148 full_s, value, n_bits);
1151 load->type = htons(OFPAT_VENDOR);
1152 load->len = htons(sizeof *load);
1153 load->vendor = htonl(NX_VENDOR_ID);
1154 load->subtype = htons(NXAST_REG_LOAD);
1155 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
1156 load->dst = htonl(dst);
1157 load->value = htonll(value);
1160 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1163 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
1165 format_nxm_field_name(s, header);
1166 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
1167 ds_put_cstr(s, "[]");
1168 } else if (n_bits == 1) {
1169 ds_put_format(s, "[%d]", ofs);
1171 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
1176 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
1178 int n_bits = ntohs(move->n_bits);
1179 int src_ofs = ntohs(move->src_ofs);
1180 int dst_ofs = ntohs(move->dst_ofs);
1181 uint32_t src = ntohl(move->src);
1182 uint32_t dst = ntohl(move->dst);
1184 ds_put_format(s, "move:");
1185 nxm_format_field_bits(s, src, src_ofs, n_bits);
1186 ds_put_cstr(s, "->");
1187 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
1191 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
1193 int ofs = nxm_decode_ofs(load->ofs_nbits);
1194 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
1195 uint32_t dst = ntohl(load->dst);
1196 uint64_t value = ntohll(load->value);
1198 ds_put_format(s, "load:%#"PRIx64"->", value);
1199 nxm_format_field_bits(s, dst, ofs, n_bits);
1202 /* nxm_check_reg_move(), nxm_check_reg_load(). */
1205 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
1207 return (f && !NXM_HASMASK(f->header)
1208 && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
1212 nxm_check_reg_move(const struct nx_action_reg_move *action,
1213 const struct flow *flow)
1215 const struct nxm_field *src;
1216 const struct nxm_field *dst;
1218 if (action->n_bits == htons(0)) {
1219 return BAD_ARGUMENT;
1222 src = nxm_field_lookup(ntohl(action->src));
1223 if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
1224 return BAD_ARGUMENT;
1227 dst = nxm_field_lookup(ntohl(action->dst));
1228 if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
1229 return BAD_ARGUMENT;
1232 if (!dst->writable) {
1233 return BAD_ARGUMENT;
1240 nxm_check_reg_load(const struct nx_action_reg_load *action,
1241 const struct flow *flow)
1243 const struct nxm_field *dst;
1246 ofs = nxm_decode_ofs(action->ofs_nbits);
1247 n_bits = nxm_decode_n_bits(action->ofs_nbits);
1248 dst = nxm_field_lookup(ntohl(action->dst));
1249 if (!field_ok(dst, flow, ofs + n_bits)) {
1250 return BAD_ARGUMENT;
1253 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1255 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1256 return BAD_ARGUMENT;
1259 if (!dst->writable) {
1260 return BAD_ARGUMENT;
1266 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1269 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1271 switch (src->index) {
1272 case NFI_NXM_OF_IN_PORT:
1273 return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
1275 case NFI_NXM_OF_ETH_DST:
1276 return eth_addr_to_uint64(flow->dl_dst);
1278 case NFI_NXM_OF_ETH_SRC:
1279 return eth_addr_to_uint64(flow->dl_src);
1281 case NFI_NXM_OF_ETH_TYPE:
1282 return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
1284 case NFI_NXM_OF_VLAN_TCI:
1285 return ntohs(flow->vlan_tci);
1287 case NFI_NXM_OF_IP_TOS:
1288 return flow->nw_tos;
1290 case NFI_NXM_OF_IP_PROTO:
1291 case NFI_NXM_OF_ARP_OP:
1292 return flow->nw_proto;
1294 case NFI_NXM_OF_IP_SRC:
1295 case NFI_NXM_OF_ARP_SPA:
1296 return ntohl(flow->nw_src);
1298 case NFI_NXM_OF_IP_DST:
1299 case NFI_NXM_OF_ARP_TPA:
1300 return ntohl(flow->nw_dst);
1302 case NFI_NXM_OF_TCP_SRC:
1303 case NFI_NXM_OF_UDP_SRC:
1304 return ntohs(flow->tp_src);
1306 case NFI_NXM_OF_TCP_DST:
1307 case NFI_NXM_OF_UDP_DST:
1308 return ntohs(flow->tp_dst);
1310 case NFI_NXM_OF_ICMP_TYPE:
1311 case NFI_NXM_NX_ICMPV6_TYPE:
1312 return ntohs(flow->tp_src) & 0xff;
1314 case NFI_NXM_OF_ICMP_CODE:
1315 case NFI_NXM_NX_ICMPV6_CODE:
1316 return ntohs(flow->tp_dst) & 0xff;
1318 case NFI_NXM_NX_TUN_ID:
1319 return ntohll(flow->tun_id);
1321 #define NXM_READ_REGISTER(IDX) \
1322 case NFI_NXM_NX_REG##IDX: \
1323 return flow->regs[IDX]; \
1324 case NFI_NXM_NX_REG##IDX##_W: \
1327 NXM_READ_REGISTER(0);
1328 #if FLOW_N_REGS >= 2
1329 NXM_READ_REGISTER(1);
1331 #if FLOW_N_REGS >= 3
1332 NXM_READ_REGISTER(2);
1334 #if FLOW_N_REGS >= 4
1335 NXM_READ_REGISTER(3);
1341 case NFI_NXM_NX_ARP_SHA:
1342 case NFI_NXM_NX_ND_SLL:
1343 return eth_addr_to_uint64(flow->arp_sha);
1345 case NFI_NXM_NX_ARP_THA:
1346 case NFI_NXM_NX_ND_TLL:
1347 return eth_addr_to_uint64(flow->arp_tha);
1349 case NFI_NXM_NX_TUN_ID_W:
1350 case NFI_NXM_OF_ETH_DST_W:
1351 case NFI_NXM_OF_VLAN_TCI_W:
1352 case NFI_NXM_OF_IP_SRC_W:
1353 case NFI_NXM_OF_IP_DST_W:
1354 case NFI_NXM_OF_ARP_SPA_W:
1355 case NFI_NXM_OF_ARP_TPA_W:
1356 case NFI_NXM_NX_IPV6_SRC:
1357 case NFI_NXM_NX_IPV6_SRC_W:
1358 case NFI_NXM_NX_IPV6_DST:
1359 case NFI_NXM_NX_IPV6_DST_W:
1360 case NFI_NXM_NX_ND_TARGET:
1369 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1372 switch (dst->index) {
1373 case NFI_NXM_OF_VLAN_TCI:
1374 flow->vlan_tci = htons(new_value);
1377 case NFI_NXM_NX_TUN_ID:
1378 flow->tun_id = htonll(new_value);
1381 #define NXM_WRITE_REGISTER(IDX) \
1382 case NFI_NXM_NX_REG##IDX: \
1383 flow->regs[IDX] = new_value; \
1385 case NFI_NXM_NX_REG##IDX##_W: \
1388 NXM_WRITE_REGISTER(0);
1389 #if FLOW_N_REGS >= 2
1390 NXM_WRITE_REGISTER(1);
1392 #if FLOW_N_REGS >= 3
1393 NXM_WRITE_REGISTER(2);
1395 #if FLOW_N_REGS >= 4
1396 NXM_WRITE_REGISTER(3);
1402 case NFI_NXM_OF_IN_PORT:
1403 case NFI_NXM_OF_ETH_DST:
1404 case NFI_NXM_OF_ETH_SRC:
1405 case NFI_NXM_OF_ETH_TYPE:
1406 case NFI_NXM_OF_IP_TOS:
1407 case NFI_NXM_OF_IP_PROTO:
1408 case NFI_NXM_OF_ARP_OP:
1409 case NFI_NXM_OF_IP_SRC:
1410 case NFI_NXM_OF_ARP_SPA:
1411 case NFI_NXM_OF_IP_DST:
1412 case NFI_NXM_OF_ARP_TPA:
1413 case NFI_NXM_OF_TCP_SRC:
1414 case NFI_NXM_OF_UDP_SRC:
1415 case NFI_NXM_OF_TCP_DST:
1416 case NFI_NXM_OF_UDP_DST:
1417 case NFI_NXM_OF_ICMP_TYPE:
1418 case NFI_NXM_OF_ICMP_CODE:
1419 case NFI_NXM_NX_TUN_ID_W:
1420 case NFI_NXM_OF_ETH_DST_W:
1421 case NFI_NXM_OF_VLAN_TCI_W:
1422 case NFI_NXM_OF_IP_SRC_W:
1423 case NFI_NXM_OF_IP_DST_W:
1424 case NFI_NXM_OF_ARP_SPA_W:
1425 case NFI_NXM_OF_ARP_TPA_W:
1426 case NFI_NXM_NX_ARP_SHA:
1427 case NFI_NXM_NX_ARP_THA:
1428 case NFI_NXM_NX_IPV6_SRC:
1429 case NFI_NXM_NX_IPV6_SRC_W:
1430 case NFI_NXM_NX_IPV6_DST:
1431 case NFI_NXM_NX_IPV6_DST_W:
1432 case NFI_NXM_NX_ICMPV6_TYPE:
1433 case NFI_NXM_NX_ICMPV6_CODE:
1434 case NFI_NXM_NX_ND_TARGET:
1435 case NFI_NXM_NX_ND_SLL:
1436 case NFI_NXM_NX_ND_TLL:
1443 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1447 int n_bits = ntohs(action->n_bits);
1448 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1450 /* Get the interesting bits of the source field. */
1451 const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
1452 int src_ofs = ntohs(action->src_ofs);
1453 uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
1455 /* Get the remaining bits of the destination field. */
1456 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1457 int dst_ofs = ntohs(action->dst_ofs);
1458 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1460 /* Get the final value. */
1461 uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
1463 nxm_write_field(dst, flow, new_data);
1467 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1471 int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1472 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1474 /* Get source data. */
1475 uint64_t src_data = ntohll(action->value);
1477 /* Get remaining bits of the destination field. */
1478 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1479 int dst_ofs = nxm_decode_ofs(action->ofs_nbits);
1480 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1482 /* Get the final value. */
1483 uint64_t new_data = dst_data | (src_data << dst_ofs);
1485 nxm_write_field(dst, flow, new_data);