2 * Copyright (c) 2010 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 "classifier.h"
22 #include "dynamic-string.h"
25 #include "openflow/nicira-ext.h"
27 #include "unaligned.h"
30 VLOG_DEFINE_THIS_MODULE(nx_match);
32 /* Rate limit for nx_match parse errors. These always indicate a bug in the
33 * peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
37 NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
38 NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
39 NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
40 NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
41 NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
42 NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
43 BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
46 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
48 enum nxm_field_index {
49 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50 #include "nx-match.def"
55 struct hmap_node hmap_node;
56 enum nxm_field_index index; /* NFI_* value. */
57 uint32_t header; /* NXM_* value. */
58 flow_wildcards_t wildcard; /* FWW_* bit, if exactly one. */
59 ovs_be16 dl_type; /* dl_type prerequisite, if nonzero. */
60 uint8_t nw_proto; /* nw_proto prerequisite, if nonzero. */
61 const char *name; /* "NXM_*" string. */
64 /* All the known fields. */
65 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
66 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68 CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69 #include "nx-match.def"
72 /* Hash table of 'nxm_fields'. */
73 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
75 /* Possible masks for NXM_OF_ETH_DST_W. */
76 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
77 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
78 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
79 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
80 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
81 = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
82 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
83 = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
88 if (hmap_is_empty(&all_nxm_fields)) {
91 for (i = 0; i < N_NXM_FIELDS; i++) {
92 struct nxm_field *f = &nxm_fields[i];
93 hmap_insert(&all_nxm_fields, &f->hmap_node,
94 hash_int(f->header, 0));
97 /* Verify that the header values are unique (duplicate "case" values
98 * cause a compile error). */
100 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
101 case NXM_##HEADER: break;
102 #include "nx-match.def"
107 static const struct nxm_field *
108 nxm_field_lookup(uint32_t header)
114 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
116 if (f->header == header) {
124 /* Returns the width of the data for a field with the given 'header', in
127 nxm_field_bytes(uint32_t header)
129 unsigned int length = NXM_LENGTH(header);
130 return NXM_HASMASK(header) ? length / 2 : length;
133 /* Returns the width of the data for a field with the given 'header', in
136 nxm_field_bits(uint32_t header)
138 return nxm_field_bytes(header) * 8;
141 /* nx_pull_match() and helpers. */
144 parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
146 const flow_wildcards_t FWW_DL_TCI = FWW_DL_VLAN | FWW_DL_VLAN_PCP;
148 if ((rule->wc.wildcards & FWW_DL_TCI) != FWW_DL_TCI) {
151 return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
156 parse_nx_reg(const struct nxm_field *f,
157 struct flow *flow, struct flow_wildcards *wc,
158 const void *value, const void *maskp)
160 int idx = NXM_NX_REG_IDX(f->header);
161 if (wc->reg_masks[idx]) {
164 flow_wildcards_set_reg_mask(wc, idx,
165 (NXM_HASMASK(f->header)
166 ? ntohl(get_unaligned_u32(maskp))
168 flow->regs[idx] = ntohl(get_unaligned_u32(value));
169 flow->regs[idx] &= wc->reg_masks[idx];
175 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
176 const void *value, const void *mask)
178 struct flow_wildcards *wc = &rule->wc;
179 struct flow *flow = &rule->flow;
183 case NFI_NXM_OF_IN_PORT:
184 flow->in_port = ntohs(get_unaligned_u16(value));
185 if (flow->in_port == OFPP_LOCAL) {
186 flow->in_port = ODPP_LOCAL;
190 /* Ethernet header. */
191 case NFI_NXM_OF_ETH_DST:
192 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
193 != (FWW_DL_DST | FWW_ETH_MCAST)) {
196 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
197 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
200 case NFI_NXM_OF_ETH_DST_W:
201 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
202 != (FWW_DL_DST | FWW_ETH_MCAST)) {
204 } else if (eth_addr_equals(mask, eth_mcast_1)) {
205 wc->wildcards &= ~FWW_ETH_MCAST;
206 flow->dl_dst[0] = *(uint8_t *) value & 0x01;
207 } else if (eth_addr_equals(mask, eth_mcast_0)) {
208 wc->wildcards &= ~FWW_DL_DST;
209 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
210 flow->dl_dst[0] &= 0xfe;
211 } else if (eth_addr_equals(mask, eth_all_0s)) {
213 } else if (eth_addr_equals(mask, eth_all_1s)) {
214 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
215 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
220 case NFI_NXM_OF_ETH_SRC:
221 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
223 case NFI_NXM_OF_ETH_TYPE:
224 flow->dl_type = get_unaligned_u16(value);
228 case NFI_NXM_OF_VLAN_TCI:
229 return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
231 case NFI_NXM_OF_VLAN_TCI_W:
232 return parse_tci(rule, get_unaligned_u16(value),
233 get_unaligned_u16(mask));
236 case NFI_NXM_OF_IP_TOS:
237 if (*(uint8_t *) value & 0x03) {
238 return NXM_BAD_VALUE;
240 flow->nw_tos = *(uint8_t *) value;
243 case NFI_NXM_OF_IP_PROTO:
244 flow->nw_proto = *(uint8_t *) value;
247 /* IP addresses in IP and ARP headers. */
248 case NFI_NXM_OF_IP_SRC:
249 case NFI_NXM_OF_ARP_SPA:
250 if (wc->nw_src_mask) {
253 cls_rule_set_nw_src(rule, get_unaligned_u32(value));
256 case NFI_NXM_OF_IP_SRC_W:
257 case NFI_NXM_OF_ARP_SPA_W:
258 if (wc->nw_src_mask) {
261 ovs_be32 ip = get_unaligned_u32(value);
262 ovs_be32 netmask = get_unaligned_u32(mask);
263 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
268 case NFI_NXM_OF_IP_DST:
269 case NFI_NXM_OF_ARP_TPA:
270 if (wc->nw_dst_mask) {
273 cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
276 case NFI_NXM_OF_IP_DST_W:
277 case NFI_NXM_OF_ARP_TPA_W:
278 if (wc->nw_dst_mask) {
281 ovs_be32 ip = get_unaligned_u32(value);
282 ovs_be32 netmask = get_unaligned_u32(mask);
283 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
290 case NFI_NXM_OF_TCP_SRC:
291 flow->tp_src = get_unaligned_u16(value);
293 case NFI_NXM_OF_TCP_DST:
294 flow->tp_dst = get_unaligned_u16(value);
298 case NFI_NXM_OF_UDP_SRC:
299 flow->tp_src = get_unaligned_u16(value);
301 case NFI_NXM_OF_UDP_DST:
302 flow->tp_dst = get_unaligned_u16(value);
306 case NFI_NXM_OF_ICMP_TYPE:
307 flow->tp_src = htons(*(uint8_t *) value);
309 case NFI_NXM_OF_ICMP_CODE:
310 flow->tp_dst = htons(*(uint8_t *) value);
314 case NFI_NXM_OF_ARP_OP:
315 if (ntohs(get_unaligned_u16(value)) > 255) {
316 return NXM_BAD_VALUE;
318 flow->nw_proto = ntohs(get_unaligned_u16(value));
323 case NFI_NXM_NX_TUN_ID:
324 flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
328 case NFI_NXM_NX_REG0:
329 case NFI_NXM_NX_REG0_W:
331 case NFI_NXM_NX_REG1:
332 case NFI_NXM_NX_REG1_W:
335 case NFI_NXM_NX_REG2:
336 case NFI_NXM_NX_REG2_W:
339 case NFI_NXM_NX_REG3:
340 case NFI_NXM_NX_REG3_W:
345 return parse_nx_reg(f, flow, wc, value, mask);
354 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
356 return (!field->dl_type
357 || (field->dl_type == flow->dl_type
358 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
362 nx_entry_ok(const void *p, unsigned int match_len)
364 unsigned int payload_len;
370 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
374 memcpy(&header_be, p, 4);
375 header = ntohl(header_be);
377 payload_len = NXM_LENGTH(header);
379 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
383 if (match_len < payload_len + 4) {
384 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
385 "%u bytes left in nx_match", payload_len + 4, match_len);
393 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
394 struct cls_rule *rule)
399 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
401 VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
402 "multiple of 8, is longer than space in message (max "
403 "length %zu)", match_len, b->size);
404 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
407 cls_rule_init_catchall(rule, priority);
408 while ((header = nx_entry_ok(p, match_len)) != 0) {
409 unsigned length = NXM_LENGTH(header);
410 const struct nxm_field *f;
413 f = nxm_field_lookup(header);
415 error = NXM_BAD_TYPE;
416 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
417 error = NXM_BAD_PREREQ;
418 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
419 error = NXM_DUP_TYPE;
421 /* 'hasmask' and 'length' are known to be correct at this point
422 * because they are included in 'header' and nxm_field_lookup()
423 * checked them already. */
424 rule->wc.wildcards &= ~f->wildcard;
425 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
428 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
429 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
431 NXM_VENDOR(header), NXM_FIELD(header),
432 NXM_HASMASK(header), NXM_TYPE(header),
439 match_len -= 4 + length;
442 return match_len ? NXM_INVALID : 0;
445 /* nx_put_match() and helpers.
447 * 'put' functions whose names end in 'w' add a wildcarded field.
448 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
449 * Other 'put' functions add exact-match fields.
453 nxm_put_header(struct ofpbuf *b, uint32_t header)
455 ovs_be32 n_header = htonl(header);
456 ofpbuf_put(b, &n_header, sizeof n_header);
460 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
462 nxm_put_header(b, header);
463 ofpbuf_put(b, &value, sizeof value);
467 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
469 nxm_put_header(b, header);
470 ofpbuf_put(b, &value, sizeof value);
474 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
476 nxm_put_header(b, header);
477 ofpbuf_put(b, &value, sizeof value);
478 ofpbuf_put(b, &mask, sizeof mask);
482 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
484 nxm_put_header(b, header);
485 ofpbuf_put(b, &value, sizeof value);
489 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
491 nxm_put_header(b, header);
492 ofpbuf_put(b, &value, sizeof value);
493 ofpbuf_put(b, &mask, sizeof mask);
497 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
504 nxm_put_32(b, header, value);
508 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
514 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
516 nxm_put_header(b, header);
517 ofpbuf_put(b, &value, sizeof value);
521 nxm_put_eth(struct ofpbuf *b, uint32_t header,
522 const uint8_t value[ETH_ADDR_LEN])
524 nxm_put_header(b, header);
525 ofpbuf_put(b, value, ETH_ADDR_LEN);
529 nxm_put_eth_dst(struct ofpbuf *b,
530 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
532 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
533 case FWW_DL_DST | FWW_ETH_MCAST:
536 nxm_put_header(b, NXM_OF_ETH_DST_W);
537 ofpbuf_put(b, value, ETH_ADDR_LEN);
538 ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
541 nxm_put_header(b, NXM_OF_ETH_DST_W);
542 ofpbuf_put(b, value, ETH_ADDR_LEN);
543 ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
546 nxm_put_eth(b, NXM_OF_ETH_DST, value);
552 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
554 const flow_wildcards_t wc = cr->wc.wildcards;
555 const struct flow *flow = &cr->flow;
556 const size_t start_len = b->size;
562 if (!(wc & FWW_IN_PORT)) {
563 uint16_t in_port = flow->in_port;
564 if (in_port == ODPP_LOCAL) {
565 in_port = OFPP_LOCAL;
567 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
571 nxm_put_eth_dst(b, wc, flow->dl_dst);
572 if (!(wc & FWW_DL_SRC)) {
573 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
575 if (!(wc & FWW_DL_TYPE)) {
576 nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
580 vid = flow->dl_vlan & htons(VLAN_VID_MASK);
581 pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
582 switch (wc & (FWW_DL_VLAN | FWW_DL_VLAN_PCP)) {
583 case FWW_DL_VLAN | FWW_DL_VLAN_PCP:
586 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
587 htons(VLAN_PCP_MASK | VLAN_CFI));
589 case FWW_DL_VLAN_PCP:
590 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
591 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
593 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
594 htons(VLAN_VID_MASK | VLAN_CFI));
598 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
599 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
601 nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
606 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
608 if (!(wc & FWW_NW_TOS)) {
609 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
611 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
612 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
614 if (!(wc & FWW_NW_PROTO)) {
615 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
616 switch (flow->nw_proto) {
619 if (!(wc & FWW_TP_SRC)) {
620 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
622 if (!(wc & FWW_TP_DST)) {
623 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
629 if (!(wc & FWW_TP_SRC)) {
630 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
632 if (!(wc & FWW_TP_DST)) {
633 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
639 if (!(wc & FWW_TP_SRC)) {
640 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
642 if (!(wc & FWW_TP_DST)) {
643 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
648 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
650 if (!(wc & FWW_NW_PROTO)) {
651 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
653 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
654 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
658 if (!(wc & FWW_TUN_ID)) {
659 nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
663 for (i = 0; i < FLOW_N_REGS; i++) {
664 nxm_put_32m(b, NXM_NX_REG(i),
665 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
668 match_len = b->size - start_len;
669 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
673 /* nx_match_to_string() and helpers. */
676 nx_match_to_string(const uint8_t *p, unsigned int match_len)
682 return xstrdup("<any>");
686 while ((header = nx_entry_ok(p, match_len)) != 0) {
687 unsigned int length = NXM_LENGTH(header);
688 unsigned int value_len = nxm_field_bytes(header);
689 const uint8_t *value = p + 4;
690 const uint8_t *mask = value + value_len;
691 const struct nxm_field *f;
695 ds_put_cstr(&s, ", ");
698 f = nxm_field_lookup(header);
700 ds_put_cstr(&s, f->name);
702 ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
705 ds_put_char(&s, '(');
707 for (i = 0; i < value_len; i++) {
708 ds_put_format(&s, "%02x", value[i]);
710 if (NXM_HASMASK(header)) {
711 ds_put_char(&s, '/');
712 for (i = 0; i < value_len; i++) {
713 ds_put_format(&s, "%02x", mask[i]);
716 ds_put_char(&s, ')');
719 match_len -= 4 + length;
724 ds_put_cstr(&s, ", ");
727 ds_put_format(&s, "<%u invalid bytes>", match_len);
730 return ds_steal_cstr(&s);
733 static const struct nxm_field *
734 lookup_nxm_field(const char *name, int name_len)
736 const struct nxm_field *f;
738 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
739 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
748 parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
755 byte = hexits_value(s, 2, &ok);
757 ovs_fatal(0, "%.2s: hex digits expected", s);
760 ofpbuf_put(b, &byte, 1);
766 /* nx_match_from_string(). */
769 nx_match_from_string(const char *s, struct ofpbuf *b)
771 const char *full_s = s;
772 const size_t start_len = b->size;
775 if (!strcmp(s, "<any>")) {
776 /* Ensure that 'b->data' isn't actually null. */
777 ofpbuf_prealloc_tailroom(b, 1);
781 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
782 const struct nxm_field *f;
785 name_len = strcspn(s, "(");
786 if (s[name_len] != '(') {
787 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
790 f = lookup_nxm_field(s, name_len);
792 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
797 nxm_put_header(b, f->header);
798 s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
799 if (NXM_HASMASK(f->header)) {
802 ovs_fatal(0, "%s: missing / in masked field %s",
805 s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
810 ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
815 match_len = b->size - start_len;
816 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
820 /* nxm_check_reg_move(), nxm_check_reg_load(). */
823 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
825 return (f && !NXM_HASMASK(f->header)
826 && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
830 nxm_check_reg_move(const struct nx_action_reg_move *action,
831 const struct flow *flow)
833 const struct nxm_field *src;
834 const struct nxm_field *dst;
836 if (action->n_bits == htons(0)) {
840 src = nxm_field_lookup(ntohl(action->src));
841 if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
845 dst = nxm_field_lookup(ntohl(action->dst));
846 if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
850 if (!NXM_IS_NX_REG(dst->header)
851 && dst->header != NXM_OF_VLAN_TCI
852 && dst->header != NXM_NX_TUN_ID) {
860 nxm_check_reg_load(const struct nx_action_reg_load *action,
861 const struct flow *flow)
863 const struct nxm_field *dst;
866 ofs = ntohs(action->ofs_nbits) >> 6;
867 n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
868 dst = nxm_field_lookup(ntohl(action->dst));
869 if (!field_ok(dst, flow, ofs + n_bits)) {
873 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
875 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
879 if (!NXM_IS_NX_REG(dst->header)) {
886 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
889 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
891 switch (src->index) {
892 case NFI_NXM_OF_IN_PORT:
893 return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
895 case NFI_NXM_OF_ETH_DST:
896 return eth_addr_to_uint64(flow->dl_dst);
898 case NFI_NXM_OF_ETH_SRC:
899 return eth_addr_to_uint64(flow->dl_src);
901 case NFI_NXM_OF_ETH_TYPE:
902 return ntohs(flow->dl_type);
904 case NFI_NXM_OF_VLAN_TCI:
905 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
908 return (ntohs(flow->dl_vlan & htons(VLAN_VID_MASK))
909 | ((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK)
913 case NFI_NXM_OF_IP_TOS:
916 case NFI_NXM_OF_IP_PROTO:
917 case NFI_NXM_OF_ARP_OP:
918 return flow->nw_proto;
920 case NFI_NXM_OF_IP_SRC:
921 case NFI_NXM_OF_ARP_SPA:
922 return ntohl(flow->nw_src);
924 case NFI_NXM_OF_IP_DST:
925 case NFI_NXM_OF_ARP_TPA:
926 return ntohl(flow->nw_dst);
928 case NFI_NXM_OF_TCP_SRC:
929 case NFI_NXM_OF_UDP_SRC:
930 return ntohs(flow->tp_src);
932 case NFI_NXM_OF_TCP_DST:
933 case NFI_NXM_OF_UDP_DST:
934 return ntohs(flow->tp_dst);
936 case NFI_NXM_OF_ICMP_TYPE:
937 return ntohs(flow->tp_src) & 0xff;
939 case NFI_NXM_OF_ICMP_CODE:
940 return ntohs(flow->tp_dst) & 0xff;
942 case NFI_NXM_NX_TUN_ID:
943 return ntohl(flow->tun_id);
945 #define NXM_READ_REGISTER(IDX) \
946 case NFI_NXM_NX_REG##IDX: \
947 return flow->regs[IDX]; \
948 case NFI_NXM_NX_REG##IDX##_W: \
951 NXM_READ_REGISTER(0);
953 NXM_READ_REGISTER(1);
956 NXM_READ_REGISTER(2);
959 NXM_READ_REGISTER(3);
965 case NFI_NXM_OF_ETH_DST_W:
966 case NFI_NXM_OF_VLAN_TCI_W:
967 case NFI_NXM_OF_IP_SRC_W:
968 case NFI_NXM_OF_IP_DST_W:
969 case NFI_NXM_OF_ARP_SPA_W:
970 case NFI_NXM_OF_ARP_TPA_W:
979 nxm_execute_reg_move(const struct nx_action_reg_move *action,
983 int n_bits = ntohs(action->n_bits);
984 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
986 /* Get the interesting bits of the source field. */
987 const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
988 int src_ofs = ntohs(action->src_ofs);
989 uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
991 /* Get the remaining bits of the destination field. */
992 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
993 int dst_ofs = ntohs(action->dst_ofs);
994 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
996 /* Get the final value. */
997 uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
999 /* Store the result. */
1000 if (NXM_IS_NX_REG(dst->header)) {
1001 flow->regs[NXM_NX_REG_IDX(dst->header)] = new_data;
1002 } else if (dst->header == NXM_OF_VLAN_TCI) {
1003 ovs_be16 vlan_tci = htons(new_data & VLAN_CFI ? new_data : 0);
1004 flow->dl_vlan = htons(vlan_tci_to_vid(vlan_tci));
1005 flow->dl_vlan_pcp = vlan_tci_to_pcp(vlan_tci);
1006 } else if (dst->header == NXM_NX_TUN_ID) {
1007 flow->tun_id = htonl(new_data);
1014 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1018 int n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
1019 uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
1020 uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(action->dst))];
1022 /* Get source data. */
1023 uint32_t src_data = ntohll(action->value);
1025 /* Get remaining bits of the destination field. */
1026 int dst_ofs = ntohs(action->ofs_nbits) >> 6;
1027 uint32_t dst_data = *reg & ~(mask << dst_ofs);
1029 *reg = dst_data | (src_data << dst_ofs);