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));
181 /* Ethernet header. */
182 case NFI_NXM_OF_ETH_DST:
183 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
184 != (FWW_DL_DST | FWW_ETH_MCAST)) {
187 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
188 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
191 case NFI_NXM_OF_ETH_DST_W:
192 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
193 != (FWW_DL_DST | FWW_ETH_MCAST)) {
195 } else if (eth_addr_equals(mask, eth_mcast_1)) {
196 wc->wildcards &= ~FWW_ETH_MCAST;
197 flow->dl_dst[0] = *(uint8_t *) value & 0x01;
199 } else if (eth_addr_equals(mask, eth_mcast_0)) {
200 wc->wildcards &= ~FWW_DL_DST;
201 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
202 flow->dl_dst[0] &= 0xfe;
204 } else if (eth_addr_equals(mask, eth_all_0s)) {
206 } else if (eth_addr_equals(mask, eth_all_1s)) {
207 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
208 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
213 case NFI_NXM_OF_ETH_SRC:
214 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
216 case NFI_NXM_OF_ETH_TYPE:
217 flow->dl_type = ofputil_dl_type_from_openflow(get_unaligned_be16(value));
221 case NFI_NXM_OF_VLAN_TCI:
222 if (wc->vlan_tci_mask) {
225 cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
228 case NFI_NXM_OF_VLAN_TCI_W:
229 if (wc->vlan_tci_mask) {
232 cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
233 get_unaligned_be16(mask));
238 case NFI_NXM_OF_IP_TOS:
239 if (*(uint8_t *) value & 0x03) {
240 return NXM_BAD_VALUE;
242 flow->nw_tos = *(uint8_t *) value;
245 case NFI_NXM_OF_IP_PROTO:
246 flow->nw_proto = *(uint8_t *) value;
249 /* IP addresses in IP and ARP headers. */
250 case NFI_NXM_OF_IP_SRC:
251 case NFI_NXM_OF_ARP_SPA:
252 if (wc->nw_src_mask) {
255 cls_rule_set_nw_src(rule, get_unaligned_be32(value));
258 case NFI_NXM_OF_IP_SRC_W:
259 case NFI_NXM_OF_ARP_SPA_W:
260 if (wc->nw_src_mask) {
263 ovs_be32 ip = get_unaligned_be32(value);
264 ovs_be32 netmask = get_unaligned_be32(mask);
265 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
270 case NFI_NXM_OF_IP_DST:
271 case NFI_NXM_OF_ARP_TPA:
272 if (wc->nw_dst_mask) {
275 cls_rule_set_nw_dst(rule, get_unaligned_be32(value));
278 case NFI_NXM_OF_IP_DST_W:
279 case NFI_NXM_OF_ARP_TPA_W:
280 if (wc->nw_dst_mask) {
283 ovs_be32 ip = get_unaligned_be32(value);
284 ovs_be32 netmask = get_unaligned_be32(mask);
285 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
291 /* IPv6 addresses. */
292 case NFI_NXM_NX_IPV6_SRC:
293 if (!ipv6_mask_is_any(&wc->ipv6_src_mask)) {
296 struct in6_addr ipv6;
297 memcpy(&ipv6, value, sizeof ipv6);
298 cls_rule_set_ipv6_src(rule, &ipv6);
301 case NFI_NXM_NX_IPV6_SRC_W:
302 if (!ipv6_mask_is_any(&wc->ipv6_src_mask)) {
305 struct in6_addr ipv6, netmask;
306 memcpy(&ipv6, value, sizeof ipv6);
307 memcpy(&netmask, mask, sizeof netmask);
308 if (!cls_rule_set_ipv6_src_masked(rule, &ipv6, &netmask)) {
313 case NFI_NXM_NX_IPV6_DST:
314 if (!ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
317 struct in6_addr ipv6;
318 memcpy(&ipv6, value, sizeof ipv6);
319 cls_rule_set_ipv6_dst(rule, &ipv6);
322 case NFI_NXM_NX_IPV6_DST_W:
323 if (!ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
326 struct in6_addr ipv6, netmask;
327 memcpy(&ipv6, value, sizeof ipv6);
328 memcpy(&netmask, mask, sizeof netmask);
329 if (!cls_rule_set_ipv6_dst_masked(rule, &ipv6, &netmask)) {
336 case NFI_NXM_OF_TCP_SRC:
337 flow->tp_src = get_unaligned_be16(value);
339 case NFI_NXM_OF_TCP_DST:
340 flow->tp_dst = get_unaligned_be16(value);
344 case NFI_NXM_OF_UDP_SRC:
345 flow->tp_src = get_unaligned_be16(value);
347 case NFI_NXM_OF_UDP_DST:
348 flow->tp_dst = get_unaligned_be16(value);
352 case NFI_NXM_OF_ICMP_TYPE:
353 flow->tp_src = htons(*(uint8_t *) value);
355 case NFI_NXM_OF_ICMP_CODE:
356 flow->tp_dst = htons(*(uint8_t *) value);
360 case NFI_NXM_NX_ICMPV6_TYPE:
361 flow->tp_src = htons(*(uint8_t *) value);
363 case NFI_NXM_NX_ICMPV6_CODE:
364 flow->tp_dst = htons(*(uint8_t *) value);
367 /* IPv6 Neighbor Discovery. */
368 case NFI_NXM_NX_ND_TARGET:
369 /* We've already verified that it's an ICMPv6 message. */
370 if ((flow->tp_src != htons(ND_NEIGHBOR_SOLICIT))
371 && (flow->tp_src != htons(ND_NEIGHBOR_ADVERT))) {
372 return NXM_BAD_PREREQ;
374 memcpy(&flow->nd_target, value, sizeof flow->nd_target);
376 case NFI_NXM_NX_ND_SLL:
377 /* We've already verified that it's an ICMPv6 message. */
378 if (flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)) {
379 return NXM_BAD_PREREQ;
381 memcpy(flow->arp_sha, value, ETH_ADDR_LEN);
383 case NFI_NXM_NX_ND_TLL:
384 /* We've already verified that it's an ICMPv6 message. */
385 if (flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
386 return NXM_BAD_PREREQ;
388 memcpy(flow->arp_tha, value, ETH_ADDR_LEN);
392 case NFI_NXM_OF_ARP_OP:
393 if (ntohs(get_unaligned_be16(value)) > 255) {
394 return NXM_BAD_VALUE;
396 flow->nw_proto = ntohs(get_unaligned_be16(value));
400 case NFI_NXM_NX_ARP_SHA:
401 memcpy(flow->arp_sha, value, ETH_ADDR_LEN);
403 case NFI_NXM_NX_ARP_THA:
404 memcpy(flow->arp_tha, value, ETH_ADDR_LEN);
408 case NFI_NXM_NX_TUN_ID:
409 if (wc->tun_id_mask) {
412 cls_rule_set_tun_id(rule, get_unaligned_be64(value));
415 case NFI_NXM_NX_TUN_ID_W:
416 if (wc->tun_id_mask) {
419 ovs_be64 tun_id = get_unaligned_be64(value);
420 ovs_be64 tun_mask = get_unaligned_be64(mask);
421 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
426 case NFI_NXM_NX_REG0:
427 case NFI_NXM_NX_REG0_W:
429 case NFI_NXM_NX_REG1:
430 case NFI_NXM_NX_REG1_W:
433 case NFI_NXM_NX_REG2:
434 case NFI_NXM_NX_REG2_W:
437 case NFI_NXM_NX_REG3:
438 case NFI_NXM_NX_REG3_W:
443 return parse_nx_reg(f, flow, wc, value, mask);
452 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
454 if (field->nw_proto && field->nw_proto != flow->nw_proto) {
458 if (!field->dl_type[0]) {
460 } else if (field->dl_type[0] == flow->dl_type) {
462 } else if (field->dl_type[1] && field->dl_type[1] == flow->dl_type) {
470 nx_entry_ok(const void *p, unsigned int match_len)
472 unsigned int payload_len;
478 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
482 memcpy(&header_be, p, 4);
483 header = ntohl(header_be);
485 payload_len = NXM_LENGTH(header);
487 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
491 if (match_len < payload_len + 4) {
492 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
493 "%u bytes left in nx_match", payload_len + 4, match_len);
501 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
502 struct cls_rule *rule)
507 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
509 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
510 "multiple of 8, is longer than space in message (max "
511 "length %zu)", match_len, b->size);
512 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
515 cls_rule_init_catchall(rule, priority);
516 while ((header = nx_entry_ok(p, match_len)) != 0) {
517 unsigned length = NXM_LENGTH(header);
518 const struct nxm_field *f;
521 f = nxm_field_lookup(header);
523 error = NXM_BAD_TYPE;
524 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
525 error = NXM_BAD_PREREQ;
526 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
527 error = NXM_DUP_TYPE;
529 /* 'hasmask' and 'length' are known to be correct at this point
530 * because they are included in 'header' and nxm_field_lookup()
531 * checked them already. */
532 rule->wc.wildcards &= ~f->wildcard;
533 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
536 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
537 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
539 NXM_VENDOR(header), NXM_FIELD(header),
540 NXM_HASMASK(header), NXM_TYPE(header),
547 match_len -= 4 + length;
550 return match_len ? NXM_INVALID : 0;
553 /* nx_put_match() and helpers.
555 * 'put' functions whose names end in 'w' add a wildcarded field.
556 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
557 * Other 'put' functions add exact-match fields.
561 nxm_put_header(struct ofpbuf *b, uint32_t header)
563 ovs_be32 n_header = htonl(header);
564 ofpbuf_put(b, &n_header, sizeof n_header);
568 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
570 nxm_put_header(b, header);
571 ofpbuf_put(b, &value, sizeof value);
575 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
577 nxm_put_header(b, header);
578 ofpbuf_put(b, &value, sizeof value);
582 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
584 nxm_put_header(b, header);
585 ofpbuf_put(b, &value, sizeof value);
586 ofpbuf_put(b, &mask, sizeof mask);
590 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
596 case CONSTANT_HTONS(UINT16_MAX):
597 nxm_put_16(b, header, value);
601 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
607 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
609 nxm_put_header(b, header);
610 ofpbuf_put(b, &value, sizeof value);
614 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
616 nxm_put_header(b, header);
617 ofpbuf_put(b, &value, sizeof value);
618 ofpbuf_put(b, &mask, sizeof mask);
622 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
628 case CONSTANT_HTONL(UINT32_MAX):
629 nxm_put_32(b, header, value);
633 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
639 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
641 nxm_put_header(b, header);
642 ofpbuf_put(b, &value, sizeof value);
646 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
648 nxm_put_header(b, header);
649 ofpbuf_put(b, &value, sizeof value);
650 ofpbuf_put(b, &mask, sizeof mask);
654 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
660 case CONSTANT_HTONLL(UINT64_MAX):
661 nxm_put_64(b, header, value);
665 nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
671 nxm_put_eth(struct ofpbuf *b, uint32_t header,
672 const uint8_t value[ETH_ADDR_LEN])
674 nxm_put_header(b, header);
675 ofpbuf_put(b, value, ETH_ADDR_LEN);
679 nxm_put_eth_dst(struct ofpbuf *b,
680 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
682 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
683 case FWW_DL_DST | FWW_ETH_MCAST:
686 nxm_put_header(b, NXM_OF_ETH_DST_W);
687 ofpbuf_put(b, value, ETH_ADDR_LEN);
688 ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
691 nxm_put_header(b, NXM_OF_ETH_DST_W);
692 ofpbuf_put(b, value, ETH_ADDR_LEN);
693 ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
696 nxm_put_eth(b, NXM_OF_ETH_DST, value);
702 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
703 const struct in6_addr *value, const struct in6_addr *mask)
705 if (ipv6_mask_is_any(mask)) {
707 } else if (ipv6_mask_is_exact(mask)) {
708 nxm_put_header(b, header);
709 ofpbuf_put(b, value, sizeof *value);
711 nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
712 ofpbuf_put(b, value, sizeof *value);
713 ofpbuf_put(b, mask, sizeof *mask);
717 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
718 * 'cr->priority', because priority is not part of nx_match), plus enough
719 * zero bytes to pad the nx_match out to a multiple of 8.
721 * This function can cause 'b''s data to be reallocated.
723 * Returns the number of bytes appended to 'b', excluding padding.
725 * If 'cr' is a catch-all rule that matches every packet, then this function
726 * appends nothing to 'b' and returns 0. */
728 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
730 const flow_wildcards_t wc = cr->wc.wildcards;
731 const struct flow *flow = &cr->flow;
732 const size_t start_len = b->size;
737 if (!(wc & FWW_IN_PORT)) {
738 uint16_t in_port = flow->in_port;
739 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
743 nxm_put_eth_dst(b, wc, flow->dl_dst);
744 if (!(wc & FWW_DL_SRC)) {
745 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
747 if (!(wc & FWW_DL_TYPE)) {
748 nxm_put_16(b, NXM_OF_ETH_TYPE,
749 ofputil_dl_type_to_openflow(flow->dl_type));
753 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
756 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
758 if (!(wc & FWW_NW_TOS)) {
759 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
761 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
762 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
764 if (!(wc & FWW_NW_PROTO)) {
765 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
766 switch (flow->nw_proto) {
769 if (!(wc & FWW_TP_SRC)) {
770 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
772 if (!(wc & FWW_TP_DST)) {
773 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
779 if (!(wc & FWW_TP_SRC)) {
780 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
782 if (!(wc & FWW_TP_DST)) {
783 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
789 if (!(wc & FWW_TP_SRC)) {
790 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
792 if (!(wc & FWW_TP_DST)) {
793 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
798 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
801 if (!(wc & FWW_NW_TOS)) {
802 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
804 nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
805 &cr->wc.ipv6_src_mask);
806 nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
807 &cr->wc.ipv6_dst_mask);
809 if (!(wc & FWW_NW_PROTO)) {
810 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
811 switch (flow->nw_proto) {
814 if (!(wc & FWW_TP_SRC)) {
815 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
817 if (!(wc & FWW_TP_DST)) {
818 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
824 if (!(wc & FWW_TP_SRC)) {
825 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
827 if (!(wc & FWW_TP_DST)) {
828 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
834 if (!(wc & FWW_TP_SRC)) {
835 nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
837 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
838 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
839 if (!(wc & FWW_ND_TARGET)) {
840 nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
843 if (!(wc & FWW_ARP_SHA)
844 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
845 nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
847 if (!(wc & FWW_ARP_THA)
848 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
849 nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
853 if (!(wc & FWW_TP_DST)) {
854 nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
859 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
861 if (!(wc & FWW_NW_PROTO)) {
862 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
864 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
865 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
866 if (!(wc & FWW_ARP_SHA)) {
867 nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
869 if (!(wc & FWW_ARP_THA)) {
870 nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
875 nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
878 for (i = 0; i < FLOW_N_REGS; i++) {
879 nxm_put_32m(b, NXM_NX_REG(i),
880 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
883 match_len = b->size - start_len;
884 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
888 /* nx_match_to_string() and helpers. */
890 static void format_nxm_field_name(struct ds *, uint32_t header);
893 nx_match_to_string(const uint8_t *p, unsigned int match_len)
899 return xstrdup("<any>");
903 while ((header = nx_entry_ok(p, match_len)) != 0) {
904 unsigned int length = NXM_LENGTH(header);
905 unsigned int value_len = nxm_field_bytes(header);
906 const uint8_t *value = p + 4;
907 const uint8_t *mask = value + value_len;
911 ds_put_cstr(&s, ", ");
914 format_nxm_field_name(&s, header);
915 ds_put_char(&s, '(');
917 for (i = 0; i < value_len; i++) {
918 ds_put_format(&s, "%02x", value[i]);
920 if (NXM_HASMASK(header)) {
921 ds_put_char(&s, '/');
922 for (i = 0; i < value_len; i++) {
923 ds_put_format(&s, "%02x", mask[i]);
926 ds_put_char(&s, ')');
929 match_len -= 4 + length;
934 ds_put_cstr(&s, ", ");
937 ds_put_format(&s, "<%u invalid bytes>", match_len);
940 return ds_steal_cstr(&s);
944 format_nxm_field_name(struct ds *s, uint32_t header)
946 const struct nxm_field *f = nxm_field_lookup(header);
948 ds_put_cstr(s, f->name);
950 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
955 parse_nxm_field_name(const char *name, int name_len)
957 const struct nxm_field *f;
959 /* Check whether it's a field name. */
960 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
961 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
966 /* Check whether it's a 32-bit field header value as hex.
967 * (This isn't ordinarily useful except for testing error behavior.) */
969 uint32_t header = hexits_value(name, name_len, NULL);
970 if (header != UINT_MAX) {
978 /* nx_match_from_string(). */
981 nx_match_from_string(const char *s, struct ofpbuf *b)
983 const char *full_s = s;
984 const size_t start_len = b->size;
987 if (!strcmp(s, "<any>")) {
988 /* Ensure that 'b->data' isn't actually null. */
989 ofpbuf_prealloc_tailroom(b, 1);
993 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
1000 name_len = strcspn(s, "(");
1001 if (s[name_len] != '(') {
1002 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
1005 header = parse_nxm_field_name(name, name_len);
1007 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1012 nxm_put_header(b, header);
1013 s = ofpbuf_put_hex(b, s, &n);
1014 if (n != nxm_field_bytes(header)) {
1015 ovs_fatal(0, "%.2s: hex digits expected", s);
1017 if (NXM_HASMASK(header)) {
1018 s += strspn(s, " ");
1020 ovs_fatal(0, "%s: missing / in masked field %.*s",
1021 full_s, name_len, name);
1023 s = ofpbuf_put_hex(b, s + 1, &n);
1024 if (n != nxm_field_bytes(header)) {
1025 ovs_fatal(0, "%.2s: hex digits expected", s);
1029 s += strspn(s, " ");
1031 ovs_fatal(0, "%s: missing ) following field %.*s",
1032 full_s, name_len, name);
1037 match_len = b->size - start_len;
1038 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
1043 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
1045 const char *full_s = s;
1053 name_len = strcspn(s, "[");
1054 if (s[name_len] != '[') {
1055 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
1058 header = parse_nxm_field_name(name, name_len);
1060 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1062 width = nxm_field_bits(header);
1065 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
1066 /* Nothing to do. */
1067 } else if (sscanf(s, "[%d]", &start) == 1) {
1069 } else if (!strncmp(s, "[]", 2)) {
1073 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
1074 "[<start>..<end>]", full_s);
1076 s = strchr(s, ']') + 1;
1079 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
1080 full_s, start, end);
1081 } else if (start >= width) {
1082 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
1083 "%d bits wide", full_s, start, width);
1084 } else if (end >= width){
1085 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
1086 "%d bits wide", full_s, end, width);
1091 *n_bitsp = end - start + 1;
1097 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
1099 const char *full_s = s;
1101 int src_ofs, dst_ofs;
1102 int src_n_bits, dst_n_bits;
1104 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
1105 if (strncmp(s, "->", 2)) {
1106 ovs_fatal(0, "%s: missing `->' following source", full_s);
1109 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
1111 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1114 if (src_n_bits != dst_n_bits) {
1115 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
1116 "%d bits wide", full_s, src_n_bits, dst_n_bits);
1119 move->type = htons(OFPAT_VENDOR);
1120 move->len = htons(sizeof *move);
1121 move->vendor = htonl(NX_VENDOR_ID);
1122 move->subtype = htons(NXAST_REG_MOVE);
1123 move->n_bits = htons(src_n_bits);
1124 move->src_ofs = htons(src_ofs);
1125 move->dst_ofs = htons(dst_ofs);
1126 move->src = htonl(src);
1127 move->dst = htonl(dst);
1131 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
1133 const char *full_s = s;
1138 value = strtoull(s, (char **) &s, 0);
1139 if (strncmp(s, "->", 2)) {
1140 ovs_fatal(0, "%s: missing `->' following value", full_s);
1143 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
1145 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1148 if (n_bits < 64 && (value >> n_bits) != 0) {
1149 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
1150 full_s, value, n_bits);
1153 load->type = htons(OFPAT_VENDOR);
1154 load->len = htons(sizeof *load);
1155 load->vendor = htonl(NX_VENDOR_ID);
1156 load->subtype = htons(NXAST_REG_LOAD);
1157 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
1158 load->dst = htonl(dst);
1159 load->value = htonll(value);
1162 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1165 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
1167 format_nxm_field_name(s, header);
1168 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
1169 ds_put_cstr(s, "[]");
1170 } else if (n_bits == 1) {
1171 ds_put_format(s, "[%d]", ofs);
1173 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
1178 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
1180 int n_bits = ntohs(move->n_bits);
1181 int src_ofs = ntohs(move->src_ofs);
1182 int dst_ofs = ntohs(move->dst_ofs);
1183 uint32_t src = ntohl(move->src);
1184 uint32_t dst = ntohl(move->dst);
1186 ds_put_format(s, "move:");
1187 nxm_format_field_bits(s, src, src_ofs, n_bits);
1188 ds_put_cstr(s, "->");
1189 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
1193 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
1195 int ofs = nxm_decode_ofs(load->ofs_nbits);
1196 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
1197 uint32_t dst = ntohl(load->dst);
1198 uint64_t value = ntohll(load->value);
1200 ds_put_format(s, "load:%#"PRIx64"->", value);
1201 nxm_format_field_bits(s, dst, ofs, n_bits);
1204 /* nxm_check_reg_move(), nxm_check_reg_load(). */
1207 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
1209 return (f && !NXM_HASMASK(f->header)
1210 && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
1214 nxm_check_reg_move(const struct nx_action_reg_move *action,
1215 const struct flow *flow)
1217 const struct nxm_field *src;
1218 const struct nxm_field *dst;
1220 if (action->n_bits == htons(0)) {
1221 return BAD_ARGUMENT;
1224 src = nxm_field_lookup(ntohl(action->src));
1225 if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
1226 return BAD_ARGUMENT;
1229 dst = nxm_field_lookup(ntohl(action->dst));
1230 if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
1231 return BAD_ARGUMENT;
1234 if (!dst->writable) {
1235 return BAD_ARGUMENT;
1242 nxm_check_reg_load(const struct nx_action_reg_load *action,
1243 const struct flow *flow)
1245 const struct nxm_field *dst;
1248 ofs = nxm_decode_ofs(action->ofs_nbits);
1249 n_bits = nxm_decode_n_bits(action->ofs_nbits);
1250 dst = nxm_field_lookup(ntohl(action->dst));
1251 if (!field_ok(dst, flow, ofs + n_bits)) {
1252 return BAD_ARGUMENT;
1255 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1257 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1258 return BAD_ARGUMENT;
1261 if (!dst->writable) {
1262 return BAD_ARGUMENT;
1268 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1271 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1273 switch (src->index) {
1274 case NFI_NXM_OF_IN_PORT:
1275 return flow->in_port;
1277 case NFI_NXM_OF_ETH_DST:
1278 return eth_addr_to_uint64(flow->dl_dst);
1280 case NFI_NXM_OF_ETH_SRC:
1281 return eth_addr_to_uint64(flow->dl_src);
1283 case NFI_NXM_OF_ETH_TYPE:
1284 return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
1286 case NFI_NXM_OF_VLAN_TCI:
1287 return ntohs(flow->vlan_tci);
1289 case NFI_NXM_OF_IP_TOS:
1290 return flow->nw_tos;
1292 case NFI_NXM_OF_IP_PROTO:
1293 case NFI_NXM_OF_ARP_OP:
1294 return flow->nw_proto;
1296 case NFI_NXM_OF_IP_SRC:
1297 case NFI_NXM_OF_ARP_SPA:
1298 return ntohl(flow->nw_src);
1300 case NFI_NXM_OF_IP_DST:
1301 case NFI_NXM_OF_ARP_TPA:
1302 return ntohl(flow->nw_dst);
1304 case NFI_NXM_OF_TCP_SRC:
1305 case NFI_NXM_OF_UDP_SRC:
1306 return ntohs(flow->tp_src);
1308 case NFI_NXM_OF_TCP_DST:
1309 case NFI_NXM_OF_UDP_DST:
1310 return ntohs(flow->tp_dst);
1312 case NFI_NXM_OF_ICMP_TYPE:
1313 case NFI_NXM_NX_ICMPV6_TYPE:
1314 return ntohs(flow->tp_src) & 0xff;
1316 case NFI_NXM_OF_ICMP_CODE:
1317 case NFI_NXM_NX_ICMPV6_CODE:
1318 return ntohs(flow->tp_dst) & 0xff;
1320 case NFI_NXM_NX_TUN_ID:
1321 return ntohll(flow->tun_id);
1323 #define NXM_READ_REGISTER(IDX) \
1324 case NFI_NXM_NX_REG##IDX: \
1325 return flow->regs[IDX]; \
1326 case NFI_NXM_NX_REG##IDX##_W: \
1329 NXM_READ_REGISTER(0);
1330 #if FLOW_N_REGS >= 2
1331 NXM_READ_REGISTER(1);
1333 #if FLOW_N_REGS >= 3
1334 NXM_READ_REGISTER(2);
1336 #if FLOW_N_REGS >= 4
1337 NXM_READ_REGISTER(3);
1343 case NFI_NXM_NX_ARP_SHA:
1344 case NFI_NXM_NX_ND_SLL:
1345 return eth_addr_to_uint64(flow->arp_sha);
1347 case NFI_NXM_NX_ARP_THA:
1348 case NFI_NXM_NX_ND_TLL:
1349 return eth_addr_to_uint64(flow->arp_tha);
1351 case NFI_NXM_NX_TUN_ID_W:
1352 case NFI_NXM_OF_ETH_DST_W:
1353 case NFI_NXM_OF_VLAN_TCI_W:
1354 case NFI_NXM_OF_IP_SRC_W:
1355 case NFI_NXM_OF_IP_DST_W:
1356 case NFI_NXM_OF_ARP_SPA_W:
1357 case NFI_NXM_OF_ARP_TPA_W:
1358 case NFI_NXM_NX_IPV6_SRC:
1359 case NFI_NXM_NX_IPV6_SRC_W:
1360 case NFI_NXM_NX_IPV6_DST:
1361 case NFI_NXM_NX_IPV6_DST_W:
1362 case NFI_NXM_NX_ND_TARGET:
1371 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1374 switch (dst->index) {
1375 case NFI_NXM_OF_VLAN_TCI:
1376 flow->vlan_tci = htons(new_value);
1379 case NFI_NXM_NX_TUN_ID:
1380 flow->tun_id = htonll(new_value);
1383 #define NXM_WRITE_REGISTER(IDX) \
1384 case NFI_NXM_NX_REG##IDX: \
1385 flow->regs[IDX] = new_value; \
1387 case NFI_NXM_NX_REG##IDX##_W: \
1390 NXM_WRITE_REGISTER(0);
1391 #if FLOW_N_REGS >= 2
1392 NXM_WRITE_REGISTER(1);
1394 #if FLOW_N_REGS >= 3
1395 NXM_WRITE_REGISTER(2);
1397 #if FLOW_N_REGS >= 4
1398 NXM_WRITE_REGISTER(3);
1404 case NFI_NXM_OF_IN_PORT:
1405 case NFI_NXM_OF_ETH_DST:
1406 case NFI_NXM_OF_ETH_SRC:
1407 case NFI_NXM_OF_ETH_TYPE:
1408 case NFI_NXM_OF_IP_TOS:
1409 case NFI_NXM_OF_IP_PROTO:
1410 case NFI_NXM_OF_ARP_OP:
1411 case NFI_NXM_OF_IP_SRC:
1412 case NFI_NXM_OF_ARP_SPA:
1413 case NFI_NXM_OF_IP_DST:
1414 case NFI_NXM_OF_ARP_TPA:
1415 case NFI_NXM_OF_TCP_SRC:
1416 case NFI_NXM_OF_UDP_SRC:
1417 case NFI_NXM_OF_TCP_DST:
1418 case NFI_NXM_OF_UDP_DST:
1419 case NFI_NXM_OF_ICMP_TYPE:
1420 case NFI_NXM_OF_ICMP_CODE:
1421 case NFI_NXM_NX_TUN_ID_W:
1422 case NFI_NXM_OF_ETH_DST_W:
1423 case NFI_NXM_OF_VLAN_TCI_W:
1424 case NFI_NXM_OF_IP_SRC_W:
1425 case NFI_NXM_OF_IP_DST_W:
1426 case NFI_NXM_OF_ARP_SPA_W:
1427 case NFI_NXM_OF_ARP_TPA_W:
1428 case NFI_NXM_NX_ARP_SHA:
1429 case NFI_NXM_NX_ARP_THA:
1430 case NFI_NXM_NX_IPV6_SRC:
1431 case NFI_NXM_NX_IPV6_SRC_W:
1432 case NFI_NXM_NX_IPV6_DST:
1433 case NFI_NXM_NX_IPV6_DST_W:
1434 case NFI_NXM_NX_ICMPV6_TYPE:
1435 case NFI_NXM_NX_ICMPV6_CODE:
1436 case NFI_NXM_NX_ND_TARGET:
1437 case NFI_NXM_NX_ND_SLL:
1438 case NFI_NXM_NX_ND_TLL:
1445 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1449 int n_bits = ntohs(action->n_bits);
1450 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1452 /* Get the interesting bits of the source field. */
1453 const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
1454 int src_ofs = ntohs(action->src_ofs);
1455 uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
1457 /* Get the remaining bits of the destination field. */
1458 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1459 int dst_ofs = ntohs(action->dst_ofs);
1460 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1462 /* Get the final value. */
1463 uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
1465 nxm_write_field(dst, flow, new_data);
1469 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1473 int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1474 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1476 /* Get source data. */
1477 uint64_t src_data = ntohll(action->value);
1479 /* Get remaining bits of the destination field. */
1480 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1481 int dst_ofs = nxm_decode_ofs(action->ofs_nbits);
1482 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1484 /* Get the final value. */
1485 uint64_t new_data = dst_data | (src_data << dst_ofs);
1487 nxm_write_field(dst, flow, new_data);