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 "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, WRITABLE) \
51 #include "nx-match.def"
56 struct hmap_node hmap_node;
57 enum nxm_field_index index; /* NFI_* value. */
58 uint32_t header; /* NXM_* value. */
59 flow_wildcards_t wildcard; /* FWW_* bit, if exactly one. */
60 ovs_be16 dl_type; /* dl_type prerequisite, if nonzero. */
61 uint8_t nw_proto; /* nw_proto prerequisite, if nonzero. */
62 const char *name; /* "NXM_*" string. */
63 bool writable; /* Writable with NXAST_REG_{MOVE,LOAD}? */
66 /* All the known fields. */
67 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
68 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO, WRITABLE) \
69 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
70 CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER, WRITABLE },
71 #include "nx-match.def"
74 /* Hash table of 'nxm_fields'. */
75 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
77 /* Possible masks for NXM_OF_ETH_DST_W. */
78 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
79 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
80 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
81 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
82 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
83 = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
84 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
85 = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
90 if (hmap_is_empty(&all_nxm_fields)) {
93 for (i = 0; i < N_NXM_FIELDS; i++) {
94 struct nxm_field *f = &nxm_fields[i];
95 hmap_insert(&all_nxm_fields, &f->hmap_node,
96 hash_int(f->header, 0));
99 /* Verify that the header values are unique (duplicate "case" values
100 * cause a compile error). */
102 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO, WRITABLE) \
103 case NXM_##HEADER: break;
104 #include "nx-match.def"
109 static const struct nxm_field *
110 nxm_field_lookup(uint32_t header)
116 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
118 if (f->header == header) {
126 /* Returns the width of the data for a field with the given 'header', in
129 nxm_field_bytes(uint32_t header)
131 unsigned int length = NXM_LENGTH(header);
132 return NXM_HASMASK(header) ? length / 2 : length;
135 /* Returns the width of the data for a field with the given 'header', in
138 nxm_field_bits(uint32_t header)
140 return nxm_field_bytes(header) * 8;
143 /* nx_pull_match() and helpers. */
146 parse_nx_reg(const struct nxm_field *f,
147 struct flow *flow, struct flow_wildcards *wc,
148 const void *value, const void *maskp)
150 int idx = NXM_NX_REG_IDX(f->header);
151 if (wc->reg_masks[idx]) {
154 flow_wildcards_set_reg_mask(wc, idx,
155 (NXM_HASMASK(f->header)
156 ? ntohl(get_unaligned_be32(maskp))
158 flow->regs[idx] = ntohl(get_unaligned_be32(value));
159 flow->regs[idx] &= wc->reg_masks[idx];
165 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
166 const void *value, const void *mask)
168 struct flow_wildcards *wc = &rule->wc;
169 struct flow *flow = &rule->flow;
173 case NFI_NXM_OF_IN_PORT:
174 flow->in_port = ntohs(get_unaligned_be16(value));
175 if (flow->in_port == OFPP_LOCAL) {
176 flow->in_port = ODPP_LOCAL;
180 /* Ethernet header. */
181 case NFI_NXM_OF_ETH_DST:
182 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
183 != (FWW_DL_DST | FWW_ETH_MCAST)) {
186 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
187 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
190 case NFI_NXM_OF_ETH_DST_W:
191 if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
192 != (FWW_DL_DST | FWW_ETH_MCAST)) {
194 } else if (eth_addr_equals(mask, eth_mcast_1)) {
195 wc->wildcards &= ~FWW_ETH_MCAST;
196 flow->dl_dst[0] = *(uint8_t *) value & 0x01;
197 } else if (eth_addr_equals(mask, eth_mcast_0)) {
198 wc->wildcards &= ~FWW_DL_DST;
199 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
200 flow->dl_dst[0] &= 0xfe;
201 } else if (eth_addr_equals(mask, eth_all_0s)) {
203 } else if (eth_addr_equals(mask, eth_all_1s)) {
204 wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
205 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
210 case NFI_NXM_OF_ETH_SRC:
211 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
213 case NFI_NXM_OF_ETH_TYPE:
214 flow->dl_type = get_unaligned_be16(value);
218 case NFI_NXM_OF_VLAN_TCI:
219 if (wc->vlan_tci_mask) {
222 cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
225 case NFI_NXM_OF_VLAN_TCI_W:
226 if (wc->vlan_tci_mask) {
229 cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
230 get_unaligned_be16(mask));
235 case NFI_NXM_OF_IP_TOS:
236 if (*(uint8_t *) value & 0x03) {
237 return NXM_BAD_VALUE;
239 flow->nw_tos = *(uint8_t *) value;
242 case NFI_NXM_OF_IP_PROTO:
243 flow->nw_proto = *(uint8_t *) value;
246 /* IP addresses in IP and ARP headers. */
247 case NFI_NXM_OF_IP_SRC:
248 case NFI_NXM_OF_ARP_SPA:
249 if (wc->nw_src_mask) {
252 cls_rule_set_nw_src(rule, get_unaligned_be32(value));
255 case NFI_NXM_OF_IP_SRC_W:
256 case NFI_NXM_OF_ARP_SPA_W:
257 if (wc->nw_src_mask) {
260 ovs_be32 ip = get_unaligned_be32(value);
261 ovs_be32 netmask = get_unaligned_be32(mask);
262 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
267 case NFI_NXM_OF_IP_DST:
268 case NFI_NXM_OF_ARP_TPA:
269 if (wc->nw_dst_mask) {
272 cls_rule_set_nw_dst(rule, get_unaligned_be32(value));
275 case NFI_NXM_OF_IP_DST_W:
276 case NFI_NXM_OF_ARP_TPA_W:
277 if (wc->nw_dst_mask) {
280 ovs_be32 ip = get_unaligned_be32(value);
281 ovs_be32 netmask = get_unaligned_be32(mask);
282 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
289 case NFI_NXM_OF_TCP_SRC:
290 flow->tp_src = get_unaligned_be16(value);
292 case NFI_NXM_OF_TCP_DST:
293 flow->tp_dst = get_unaligned_be16(value);
297 case NFI_NXM_OF_UDP_SRC:
298 flow->tp_src = get_unaligned_be16(value);
300 case NFI_NXM_OF_UDP_DST:
301 flow->tp_dst = get_unaligned_be16(value);
305 case NFI_NXM_OF_ICMP_TYPE:
306 flow->tp_src = htons(*(uint8_t *) value);
308 case NFI_NXM_OF_ICMP_CODE:
309 flow->tp_dst = htons(*(uint8_t *) value);
313 case NFI_NXM_OF_ARP_OP:
314 if (ntohs(get_unaligned_be16(value)) > 255) {
315 return NXM_BAD_VALUE;
317 flow->nw_proto = ntohs(get_unaligned_be16(value));
322 case NFI_NXM_NX_TUN_ID:
323 flow->tun_id = get_unaligned_be64(value);
327 case NFI_NXM_NX_REG0:
328 case NFI_NXM_NX_REG0_W:
330 case NFI_NXM_NX_REG1:
331 case NFI_NXM_NX_REG1_W:
334 case NFI_NXM_NX_REG2:
335 case NFI_NXM_NX_REG2_W:
338 case NFI_NXM_NX_REG3:
339 case NFI_NXM_NX_REG3_W:
344 return parse_nx_reg(f, flow, wc, value, mask);
353 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
355 return (!field->dl_type
356 || (field->dl_type == flow->dl_type
357 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
361 nx_entry_ok(const void *p, unsigned int match_len)
363 unsigned int payload_len;
369 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
373 memcpy(&header_be, p, 4);
374 header = ntohl(header_be);
376 payload_len = NXM_LENGTH(header);
378 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
382 if (match_len < payload_len + 4) {
383 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
384 "%u bytes left in nx_match", payload_len + 4, match_len);
392 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
393 struct cls_rule *rule)
398 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
400 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
401 "multiple of 8, is longer than space in message (max "
402 "length %zu)", match_len, b->size);
403 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
406 cls_rule_init_catchall(rule, priority);
407 while ((header = nx_entry_ok(p, match_len)) != 0) {
408 unsigned length = NXM_LENGTH(header);
409 const struct nxm_field *f;
412 f = nxm_field_lookup(header);
414 error = NXM_BAD_TYPE;
415 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
416 error = NXM_BAD_PREREQ;
417 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
418 error = NXM_DUP_TYPE;
420 /* 'hasmask' and 'length' are known to be correct at this point
421 * because they are included in 'header' and nxm_field_lookup()
422 * checked them already. */
423 rule->wc.wildcards &= ~f->wildcard;
424 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
427 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
428 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
430 NXM_VENDOR(header), NXM_FIELD(header),
431 NXM_HASMASK(header), NXM_TYPE(header),
438 match_len -= 4 + length;
441 return match_len ? NXM_INVALID : 0;
444 /* nx_put_match() and helpers.
446 * 'put' functions whose names end in 'w' add a wildcarded field.
447 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
448 * Other 'put' functions add exact-match fields.
452 nxm_put_header(struct ofpbuf *b, uint32_t header)
454 ovs_be32 n_header = htonl(header);
455 ofpbuf_put(b, &n_header, sizeof n_header);
459 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
461 nxm_put_header(b, header);
462 ofpbuf_put(b, &value, sizeof value);
466 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
468 nxm_put_header(b, header);
469 ofpbuf_put(b, &value, sizeof value);
473 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
475 nxm_put_header(b, header);
476 ofpbuf_put(b, &value, sizeof value);
477 ofpbuf_put(b, &mask, sizeof mask);
481 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
487 case CONSTANT_HTONS(UINT16_MAX):
488 nxm_put_16(b, header, value);
492 nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
498 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
500 nxm_put_header(b, header);
501 ofpbuf_put(b, &value, sizeof value);
505 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
507 nxm_put_header(b, header);
508 ofpbuf_put(b, &value, sizeof value);
509 ofpbuf_put(b, &mask, sizeof mask);
513 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
519 case CONSTANT_HTONL(UINT32_MAX):
520 nxm_put_32(b, header, value);
524 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
530 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
532 nxm_put_header(b, header);
533 ofpbuf_put(b, &value, sizeof value);
537 nxm_put_eth(struct ofpbuf *b, uint32_t header,
538 const uint8_t value[ETH_ADDR_LEN])
540 nxm_put_header(b, header);
541 ofpbuf_put(b, value, ETH_ADDR_LEN);
545 nxm_put_eth_dst(struct ofpbuf *b,
546 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
548 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
549 case FWW_DL_DST | FWW_ETH_MCAST:
552 nxm_put_header(b, NXM_OF_ETH_DST_W);
553 ofpbuf_put(b, value, ETH_ADDR_LEN);
554 ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
557 nxm_put_header(b, NXM_OF_ETH_DST_W);
558 ofpbuf_put(b, value, ETH_ADDR_LEN);
559 ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
562 nxm_put_eth(b, NXM_OF_ETH_DST, value);
567 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
568 * 'cr->priority', because priority is not part of nx_match), plus enough
569 * zero bytes to pad the nx_match out to a multiple of 8.
571 * This function can cause 'b''s data to be reallocated.
573 * Returns the number of bytes appended to 'b', excluding padding.
575 * If 'cr' is a catch-all rule that matches every packet, then this function
576 * appends nothing to 'b' and returns 0. */
578 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
580 const flow_wildcards_t wc = cr->wc.wildcards;
581 const struct flow *flow = &cr->flow;
582 const size_t start_len = b->size;
587 if (!(wc & FWW_IN_PORT)) {
588 uint16_t in_port = flow->in_port;
589 if (in_port == ODPP_LOCAL) {
590 in_port = OFPP_LOCAL;
592 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
596 nxm_put_eth_dst(b, wc, flow->dl_dst);
597 if (!(wc & FWW_DL_SRC)) {
598 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
600 if (!(wc & FWW_DL_TYPE)) {
601 nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
605 nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
608 if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
610 if (!(wc & FWW_NW_TOS)) {
611 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
613 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
614 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
616 if (!(wc & FWW_NW_PROTO)) {
617 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
618 switch (flow->nw_proto) {
621 if (!(wc & FWW_TP_SRC)) {
622 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
624 if (!(wc & FWW_TP_DST)) {
625 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
631 if (!(wc & FWW_TP_SRC)) {
632 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
634 if (!(wc & FWW_TP_DST)) {
635 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
641 if (!(wc & FWW_TP_SRC)) {
642 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
644 if (!(wc & FWW_TP_DST)) {
645 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
650 } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
652 if (!(wc & FWW_NW_PROTO)) {
653 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
655 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
656 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
660 if (!(wc & FWW_TUN_ID)) {
661 nxm_put_64(b, NXM_NX_TUN_ID, flow->tun_id);
665 for (i = 0; i < FLOW_N_REGS; i++) {
666 nxm_put_32m(b, NXM_NX_REG(i),
667 htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
670 match_len = b->size - start_len;
671 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
675 /* nx_match_to_string() and helpers. */
677 static void format_nxm_field_name(struct ds *, uint32_t header);
680 nx_match_to_string(const uint8_t *p, unsigned int match_len)
686 return xstrdup("<any>");
690 while ((header = nx_entry_ok(p, match_len)) != 0) {
691 unsigned int length = NXM_LENGTH(header);
692 unsigned int value_len = nxm_field_bytes(header);
693 const uint8_t *value = p + 4;
694 const uint8_t *mask = value + value_len;
698 ds_put_cstr(&s, ", ");
701 format_nxm_field_name(&s, header);
702 ds_put_char(&s, '(');
704 for (i = 0; i < value_len; i++) {
705 ds_put_format(&s, "%02x", value[i]);
707 if (NXM_HASMASK(header)) {
708 ds_put_char(&s, '/');
709 for (i = 0; i < value_len; i++) {
710 ds_put_format(&s, "%02x", mask[i]);
713 ds_put_char(&s, ')');
716 match_len -= 4 + length;
721 ds_put_cstr(&s, ", ");
724 ds_put_format(&s, "<%u invalid bytes>", match_len);
727 return ds_steal_cstr(&s);
731 format_nxm_field_name(struct ds *s, uint32_t header)
733 const struct nxm_field *f = nxm_field_lookup(header);
735 ds_put_cstr(s, f->name);
737 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
742 parse_nxm_field_name(const char *name, int name_len)
744 const struct nxm_field *f;
746 /* Check whether it's a field name. */
747 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
748 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
753 /* Check whether it's a 32-bit field header value as hex.
754 * (This isn't ordinarily useful except for testing error behavior.) */
756 uint32_t header = hexits_value(name, name_len, NULL);
757 if (header != UINT_MAX) {
765 /* nx_match_from_string(). */
768 nx_match_from_string(const char *s, struct ofpbuf *b)
770 const char *full_s = s;
771 const size_t start_len = b->size;
774 if (!strcmp(s, "<any>")) {
775 /* Ensure that 'b->data' isn't actually null. */
776 ofpbuf_prealloc_tailroom(b, 1);
780 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
787 name_len = strcspn(s, "(");
788 if (s[name_len] != '(') {
789 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
792 header = parse_nxm_field_name(name, name_len);
794 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
799 nxm_put_header(b, header);
800 s = ofpbuf_put_hex(b, s, &n);
801 if (n != nxm_field_bytes(header)) {
802 ovs_fatal(0, "%.2s: hex digits expected", s);
804 if (NXM_HASMASK(header)) {
807 ovs_fatal(0, "%s: missing / in masked field %.*s",
808 full_s, name_len, name);
810 s = ofpbuf_put_hex(b, s + 1, &n);
811 if (n != nxm_field_bytes(header)) {
812 ovs_fatal(0, "%.2s: hex digits expected", s);
818 ovs_fatal(0, "%s: missing ) following field %.*s",
819 full_s, name_len, name);
824 match_len = b->size - start_len;
825 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
830 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
832 const char *full_s = s;
840 name_len = strcspn(s, "[");
841 if (s[name_len] != '[') {
842 ovs_fatal(0, "%s: missing [ looking for field name", full_s);
845 header = parse_nxm_field_name(name, name_len);
847 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
849 width = nxm_field_bits(header);
852 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
854 } else if (sscanf(s, "[%d]", &start) == 1) {
856 } else if (!strncmp(s, "[]", 2)) {
860 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
861 "[<start>..<end>]", full_s);
863 s = strchr(s, ']') + 1;
866 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
868 } else if (start >= width) {
869 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
870 "%d bits wide", full_s, start, width);
871 } else if (end >= width){
872 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
873 "%d bits wide", full_s, end, width);
878 *n_bitsp = end - start + 1;
884 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
886 const char *full_s = s;
888 int src_ofs, dst_ofs;
889 int src_n_bits, dst_n_bits;
891 s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
892 if (strncmp(s, "->", 2)) {
893 ovs_fatal(0, "%s: missing `->' following source", full_s);
896 s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
898 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
901 if (src_n_bits != dst_n_bits) {
902 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
903 "%d bits wide", full_s, src_n_bits, dst_n_bits);
906 move->type = htons(OFPAT_VENDOR);
907 move->len = htons(sizeof *move);
908 move->vendor = htonl(NX_VENDOR_ID);
909 move->subtype = htons(NXAST_REG_MOVE);
910 move->n_bits = htons(src_n_bits);
911 move->src_ofs = htons(src_ofs);
912 move->dst_ofs = htons(dst_ofs);
913 move->src = htonl(src);
914 move->dst = htonl(dst);
918 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
920 const char *full_s = s;
925 value = strtoull(s, (char **) &s, 0);
926 if (strncmp(s, "->", 2)) {
927 ovs_fatal(0, "%s: missing `->' following value", full_s);
930 s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
932 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
935 if (n_bits < 64 && (value >> n_bits) != 0) {
936 ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
937 full_s, value, n_bits);
940 load->type = htons(OFPAT_VENDOR);
941 load->len = htons(sizeof *load);
942 load->vendor = htonl(NX_VENDOR_ID);
943 load->subtype = htons(NXAST_REG_LOAD);
944 load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
945 load->dst = htonl(dst);
946 load->value = htonll(value);
949 /* nxm_format_reg_move(), nxm_format_reg_load(). */
952 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
954 format_nxm_field_name(s, header);
955 if (ofs == 0 && n_bits == nxm_field_bits(header)) {
956 ds_put_cstr(s, "[]");
957 } else if (n_bits == 1) {
958 ds_put_format(s, "[%d]", ofs);
960 ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
965 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
967 int n_bits = ntohs(move->n_bits);
968 int src_ofs = ntohs(move->src_ofs);
969 int dst_ofs = ntohs(move->dst_ofs);
970 uint32_t src = ntohl(move->src);
971 uint32_t dst = ntohl(move->dst);
973 ds_put_format(s, "move:");
974 nxm_format_field_bits(s, src, src_ofs, n_bits);
975 ds_put_cstr(s, "->");
976 nxm_format_field_bits(s, dst, dst_ofs, n_bits);
980 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
982 int ofs = nxm_decode_ofs(load->ofs_nbits);
983 int n_bits = nxm_decode_n_bits(load->ofs_nbits);
984 uint32_t dst = ntohl(load->dst);
985 uint64_t value = ntohll(load->value);
987 ds_put_format(s, "load:%#"PRIx64"->", value);
988 nxm_format_field_bits(s, dst, ofs, n_bits);
991 /* nxm_check_reg_move(), nxm_check_reg_load(). */
994 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
996 return (f && !NXM_HASMASK(f->header)
997 && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
1001 nxm_check_reg_move(const struct nx_action_reg_move *action,
1002 const struct flow *flow)
1004 const struct nxm_field *src;
1005 const struct nxm_field *dst;
1007 if (action->n_bits == htons(0)) {
1008 return BAD_ARGUMENT;
1011 src = nxm_field_lookup(ntohl(action->src));
1012 if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
1013 return BAD_ARGUMENT;
1016 dst = nxm_field_lookup(ntohl(action->dst));
1017 if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
1018 return BAD_ARGUMENT;
1021 if (!dst->writable) {
1022 return BAD_ARGUMENT;
1029 nxm_check_reg_load(const struct nx_action_reg_load *action,
1030 const struct flow *flow)
1032 const struct nxm_field *dst;
1035 ofs = nxm_decode_ofs(action->ofs_nbits);
1036 n_bits = nxm_decode_n_bits(action->ofs_nbits);
1037 dst = nxm_field_lookup(ntohl(action->dst));
1038 if (!field_ok(dst, flow, ofs + n_bits)) {
1039 return BAD_ARGUMENT;
1042 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1044 if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1045 return BAD_ARGUMENT;
1048 if (!dst->writable) {
1049 return BAD_ARGUMENT;
1055 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1058 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1060 switch (src->index) {
1061 case NFI_NXM_OF_IN_PORT:
1062 return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
1064 case NFI_NXM_OF_ETH_DST:
1065 return eth_addr_to_uint64(flow->dl_dst);
1067 case NFI_NXM_OF_ETH_SRC:
1068 return eth_addr_to_uint64(flow->dl_src);
1070 case NFI_NXM_OF_ETH_TYPE:
1071 return ntohs(flow->dl_type);
1073 case NFI_NXM_OF_VLAN_TCI:
1074 return ntohs(flow->vlan_tci);
1076 case NFI_NXM_OF_IP_TOS:
1077 return flow->nw_tos;
1079 case NFI_NXM_OF_IP_PROTO:
1080 case NFI_NXM_OF_ARP_OP:
1081 return flow->nw_proto;
1083 case NFI_NXM_OF_IP_SRC:
1084 case NFI_NXM_OF_ARP_SPA:
1085 return ntohl(flow->nw_src);
1087 case NFI_NXM_OF_IP_DST:
1088 case NFI_NXM_OF_ARP_TPA:
1089 return ntohl(flow->nw_dst);
1091 case NFI_NXM_OF_TCP_SRC:
1092 case NFI_NXM_OF_UDP_SRC:
1093 return ntohs(flow->tp_src);
1095 case NFI_NXM_OF_TCP_DST:
1096 case NFI_NXM_OF_UDP_DST:
1097 return ntohs(flow->tp_dst);
1099 case NFI_NXM_OF_ICMP_TYPE:
1100 return ntohs(flow->tp_src) & 0xff;
1102 case NFI_NXM_OF_ICMP_CODE:
1103 return ntohs(flow->tp_dst) & 0xff;
1105 case NFI_NXM_NX_TUN_ID:
1106 return ntohll(flow->tun_id);
1108 #define NXM_READ_REGISTER(IDX) \
1109 case NFI_NXM_NX_REG##IDX: \
1110 return flow->regs[IDX]; \
1111 case NFI_NXM_NX_REG##IDX##_W: \
1114 NXM_READ_REGISTER(0);
1115 #if FLOW_N_REGS >= 2
1116 NXM_READ_REGISTER(1);
1118 #if FLOW_N_REGS >= 3
1119 NXM_READ_REGISTER(2);
1121 #if FLOW_N_REGS >= 4
1122 NXM_READ_REGISTER(3);
1128 case NFI_NXM_OF_ETH_DST_W:
1129 case NFI_NXM_OF_VLAN_TCI_W:
1130 case NFI_NXM_OF_IP_SRC_W:
1131 case NFI_NXM_OF_IP_DST_W:
1132 case NFI_NXM_OF_ARP_SPA_W:
1133 case NFI_NXM_OF_ARP_TPA_W:
1142 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1145 switch (dst->index) {
1146 case NFI_NXM_OF_VLAN_TCI:
1147 flow->vlan_tci = htons(new_value);
1150 case NFI_NXM_NX_TUN_ID:
1151 flow->tun_id = htonll(new_value);
1154 #define NXM_WRITE_REGISTER(IDX) \
1155 case NFI_NXM_NX_REG##IDX: \
1156 flow->regs[IDX] = new_value; \
1158 case NFI_NXM_NX_REG##IDX##_W: \
1161 NXM_WRITE_REGISTER(0);
1162 #if FLOW_N_REGS >= 2
1163 NXM_WRITE_REGISTER(1);
1165 #if FLOW_N_REGS >= 3
1166 NXM_WRITE_REGISTER(2);
1168 #if FLOW_N_REGS >= 4
1169 NXM_WRITE_REGISTER(3);
1175 case NFI_NXM_OF_IN_PORT:
1176 case NFI_NXM_OF_ETH_DST:
1177 case NFI_NXM_OF_ETH_SRC:
1178 case NFI_NXM_OF_ETH_TYPE:
1179 case NFI_NXM_OF_IP_TOS:
1180 case NFI_NXM_OF_IP_PROTO:
1181 case NFI_NXM_OF_ARP_OP:
1182 case NFI_NXM_OF_IP_SRC:
1183 case NFI_NXM_OF_ARP_SPA:
1184 case NFI_NXM_OF_IP_DST:
1185 case NFI_NXM_OF_ARP_TPA:
1186 case NFI_NXM_OF_TCP_SRC:
1187 case NFI_NXM_OF_UDP_SRC:
1188 case NFI_NXM_OF_TCP_DST:
1189 case NFI_NXM_OF_UDP_DST:
1190 case NFI_NXM_OF_ICMP_TYPE:
1191 case NFI_NXM_OF_ICMP_CODE:
1192 case NFI_NXM_OF_ETH_DST_W:
1193 case NFI_NXM_OF_VLAN_TCI_W:
1194 case NFI_NXM_OF_IP_SRC_W:
1195 case NFI_NXM_OF_IP_DST_W:
1196 case NFI_NXM_OF_ARP_SPA_W:
1197 case NFI_NXM_OF_ARP_TPA_W:
1204 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1208 int n_bits = ntohs(action->n_bits);
1209 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1211 /* Get the interesting bits of the source field. */
1212 const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
1213 int src_ofs = ntohs(action->src_ofs);
1214 uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
1216 /* Get the remaining bits of the destination field. */
1217 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1218 int dst_ofs = ntohs(action->dst_ofs);
1219 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1221 /* Get the final value. */
1222 uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
1224 nxm_write_field(dst, flow, new_data);
1228 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1232 int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1233 uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1235 /* Get source data. */
1236 uint64_t src_data = ntohll(action->value);
1238 /* Get remaining bits of the destination field. */
1239 const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1240 int dst_ofs = nxm_decode_ofs(action->ofs_nbits);
1241 uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1243 /* Get the final value. */
1244 uint64_t new_data = dst_data | (src_data << dst_ofs);
1246 nxm_write_field(dst, flow, new_data);