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 uint32_t wildcard; /* Wildcard 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);
78 if (hmap_is_empty(&all_nxm_fields)) {
81 for (i = 0; i < N_NXM_FIELDS; i++) {
82 struct nxm_field *f = &nxm_fields[i];
83 hmap_insert(&all_nxm_fields, &f->hmap_node,
84 hash_int(f->header, 0));
87 /* Verify that the header values are unique (duplicate "case" values
88 * cause a compile error). */
90 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
91 case NXM_##HEADER: break;
92 #include "nx-match.def"
97 static const struct nxm_field *
98 nxm_field_lookup(uint32_t header)
104 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
106 if (f->header == header) {
114 /* Returns the width of the data for a field with the given 'header', in
117 nxm_field_bytes(uint32_t header)
119 unsigned int length = NXM_LENGTH(header);
120 return NXM_HASMASK(header) ? length / 2 : length;
123 /* nx_pull_match() and helpers. */
126 parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
128 enum { OFPFW_DL_TCI = OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP };
129 if ((rule->wc.wildcards & OFPFW_DL_TCI) != OFPFW_DL_TCI) {
132 return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
137 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
138 const void *value, const void *mask)
140 struct flow_wildcards *wc = &rule->wc;
141 struct flow *flow = &rule->flow;
145 case NFI_NXM_OF_IN_PORT:
146 flow->in_port = ntohs(get_unaligned_u16(value));
147 if (flow->in_port == OFPP_LOCAL) {
148 flow->in_port = ODPP_LOCAL;
152 /* Ethernet header. */
153 case NFI_NXM_OF_ETH_DST:
154 memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
156 case NFI_NXM_OF_ETH_SRC:
157 memcpy(flow->dl_src, value, ETH_ADDR_LEN);
159 case NFI_NXM_OF_ETH_TYPE:
160 flow->dl_type = get_unaligned_u16(value);
164 case NFI_NXM_OF_VLAN_TCI:
165 return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
167 case NFI_NXM_OF_VLAN_TCI_W:
168 return parse_tci(rule, get_unaligned_u16(value),
169 get_unaligned_u16(mask));
172 case NFI_NXM_OF_IP_TOS:
173 if (*(uint8_t *) value & 0x03) {
174 return NXM_BAD_VALUE;
176 flow->nw_tos = *(uint8_t *) value;
179 case NFI_NXM_OF_IP_PROTO:
180 flow->nw_proto = *(uint8_t *) value;
183 /* IP addresses in IP and ARP headers. */
184 case NFI_NXM_OF_IP_SRC:
185 case NFI_NXM_OF_ARP_SPA:
186 if (wc->nw_src_mask) {
189 cls_rule_set_nw_src(rule, get_unaligned_u32(value));
192 case NFI_NXM_OF_IP_SRC_W:
193 case NFI_NXM_OF_ARP_SPA_W:
194 if (wc->nw_src_mask) {
197 ovs_be32 ip = get_unaligned_u32(value);
198 ovs_be32 netmask = get_unaligned_u32(mask);
199 if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
204 case NFI_NXM_OF_IP_DST:
205 case NFI_NXM_OF_ARP_TPA:
206 if (wc->nw_dst_mask) {
209 cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
212 case NFI_NXM_OF_IP_DST_W:
213 case NFI_NXM_OF_ARP_TPA_W:
214 if (wc->nw_dst_mask) {
217 ovs_be32 ip = get_unaligned_u32(value);
218 ovs_be32 netmask = get_unaligned_u32(mask);
219 if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
226 case NFI_NXM_OF_TCP_SRC:
227 flow->tp_src = get_unaligned_u16(value);
229 case NFI_NXM_OF_TCP_DST:
230 flow->tp_dst = get_unaligned_u16(value);
234 case NFI_NXM_OF_UDP_SRC:
235 flow->tp_src = get_unaligned_u16(value);
237 case NFI_NXM_OF_UDP_DST:
238 flow->tp_dst = get_unaligned_u16(value);
242 case NFI_NXM_OF_ICMP_TYPE:
243 flow->tp_src = htons(*(uint8_t *) value);
245 case NFI_NXM_OF_ICMP_CODE:
246 flow->tp_dst = htons(*(uint8_t *) value);
250 case NFI_NXM_OF_ARP_OP:
251 if (ntohs(get_unaligned_u16(value)) > 255) {
252 return NXM_BAD_VALUE;
254 flow->nw_proto = ntohs(get_unaligned_u16(value));
259 case NFI_NXM_NX_TUN_ID:
260 flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
270 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
272 return (!field->dl_type
273 || (field->dl_type == flow->dl_type
274 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
278 nx_entry_ok(const void *p, unsigned int match_len)
280 unsigned int payload_len;
286 VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
290 memcpy(&header_be, p, 4);
291 header = ntohl(header_be);
293 payload_len = NXM_LENGTH(header);
295 VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
299 if (match_len < payload_len + 4) {
300 VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
301 "%u bytes left in nx_match", payload_len + 4, match_len);
309 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
310 struct cls_rule *rule)
315 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
317 VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
318 "multiple of 8, is longer than space in message (max "
319 "length %zu)", match_len, b->size);
320 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
323 cls_rule_init_catchall(rule, priority);
324 while ((header = nx_entry_ok(p, match_len)) != 0) {
325 unsigned length = NXM_LENGTH(header);
326 const struct nxm_field *f;
329 f = nxm_field_lookup(header);
331 error = NXM_BAD_TYPE;
332 } else if (!nxm_prereqs_ok(f, &rule->flow)) {
333 error = NXM_BAD_PREREQ;
334 } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
335 error = NXM_DUP_TYPE;
337 /* 'hasmask' and 'length' are known to be correct at this point
338 * because they are included in 'header' and nxm_field_lookup()
339 * checked them already. */
340 rule->wc.wildcards &= ~f->wildcard;
341 error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
344 VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
345 "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
347 NXM_VENDOR(header), NXM_FIELD(header),
348 NXM_HASMASK(header), NXM_TYPE(header),
355 match_len -= 4 + length;
358 return match_len ? NXM_INVALID : 0;
361 /* nx_put_match() and helpers.
363 * 'put' functions whose names end in 'w' add a wildcarded field.
364 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
365 * Other 'put' functions add exact-match fields.
369 nxm_put_header(struct ofpbuf *b, uint32_t header)
371 ovs_be32 n_header = htonl(header);
372 ofpbuf_put(b, &n_header, sizeof n_header);
376 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
378 nxm_put_header(b, header);
379 ofpbuf_put(b, &value, sizeof value);
383 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
385 nxm_put_header(b, header);
386 ofpbuf_put(b, &value, sizeof value);
390 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
392 nxm_put_header(b, header);
393 ofpbuf_put(b, &value, sizeof value);
394 ofpbuf_put(b, &mask, sizeof mask);
398 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
400 nxm_put_header(b, header);
401 ofpbuf_put(b, &value, sizeof value);
405 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
407 nxm_put_header(b, header);
408 ofpbuf_put(b, &value, sizeof value);
409 ofpbuf_put(b, &mask, sizeof mask);
413 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
420 nxm_put_32(b, header, value);
424 nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
430 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
432 nxm_put_header(b, header);
433 ofpbuf_put(b, &value, sizeof value);
438 nxm_put_eth(struct ofpbuf *b, uint32_t header,
439 const uint8_t value[ETH_ADDR_LEN])
441 nxm_put_header(b, header);
442 ofpbuf_put(b, value, ETH_ADDR_LEN);
446 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
448 const uint32_t wc = cr->wc.wildcards;
449 const struct flow *flow = &cr->flow;
450 const size_t start_len = b->size;
455 if (!(wc & OFPFW_IN_PORT)) {
456 uint16_t in_port = flow->in_port;
457 if (in_port == ODPP_LOCAL) {
458 in_port = OFPP_LOCAL;
460 nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
464 if (!(wc & OFPFW_DL_DST)) {
465 nxm_put_eth(b, NXM_OF_ETH_DST, flow->dl_dst);
467 if (!(wc & OFPFW_DL_SRC)) {
468 nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
470 if (!(wc & OFPFW_DL_TYPE)) {
471 nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
475 vid = flow->dl_vlan & htons(VLAN_VID_MASK);
476 pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
477 switch (wc & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
478 case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
481 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
482 htons(VLAN_PCP_MASK | VLAN_CFI));
484 case OFPFW_DL_VLAN_PCP:
485 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
486 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
488 nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
489 htons(VLAN_VID_MASK | VLAN_CFI));
493 if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
494 nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
496 nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
501 if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
503 if (!(wc & OFPFW_NW_TOS)) {
504 nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
506 nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
507 nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
509 if (!(wc & OFPFW_NW_PROTO)) {
510 nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
511 switch (flow->nw_proto) {
514 if (!(wc & OFPFW_TP_SRC)) {
515 nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
517 if (!(wc & OFPFW_TP_DST)) {
518 nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
524 if (!(wc & OFPFW_TP_SRC)) {
525 nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
527 if (!(wc & OFPFW_TP_DST)) {
528 nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
534 if (!(wc & OFPFW_TP_SRC)) {
535 nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
537 if (!(wc & OFPFW_TP_DST)) {
538 nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
543 } else if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
545 if (!(wc & OFPFW_NW_PROTO)) {
546 nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
548 nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
549 nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
553 if (!(wc & NXFW_TUN_ID)) {
554 nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
557 match_len = b->size - start_len;
558 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
562 /* nx_match_to_string() and helpers. */
565 nx_match_to_string(const uint8_t *p, unsigned int match_len)
571 return xstrdup("<any>");
575 while ((header = nx_entry_ok(p, match_len)) != 0) {
576 unsigned int length = NXM_LENGTH(header);
577 unsigned int value_len = nxm_field_bytes(header);
578 const uint8_t *value = p + 4;
579 const uint8_t *mask = value + value_len;
580 const struct nxm_field *f;
584 ds_put_cstr(&s, ", ");
587 f = nxm_field_lookup(header);
589 ds_put_cstr(&s, f->name);
591 ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
594 ds_put_char(&s, '(');
596 for (i = 0; i < value_len; i++) {
597 ds_put_format(&s, "%02x", value[i]);
599 if (NXM_HASMASK(header)) {
600 ds_put_char(&s, '/');
601 for (i = 0; i < value_len; i++) {
602 ds_put_format(&s, "%02x", mask[i]);
605 ds_put_char(&s, ')');
608 match_len -= 4 + length;
613 ds_put_cstr(&s, ", ");
616 ds_put_format(&s, "<%u invalid bytes>", match_len);
619 return ds_steal_cstr(&s);
622 static const struct nxm_field *
623 lookup_nxm_field(const char *name, int name_len)
625 const struct nxm_field *f;
627 for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
628 if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
637 parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
644 low = hexit_value(*s);
645 high = low < 0 ? low : hexit_value(s[1]);
646 if (low < 0 || high < 0) {
647 ovs_fatal(0, "%.2s: hex digits expected", s);
650 byte = 16 * low + high;
651 ofpbuf_put(b, &byte, 1);
657 /* nx_match_from_string(). */
660 nx_match_from_string(const char *s, struct ofpbuf *b)
662 const char *full_s = s;
663 const size_t start_len = b->size;
666 if (!strcmp(s, "<any>")) {
667 /* Ensure that 'b->data' isn't actually null. */
668 ofpbuf_prealloc_tailroom(b, 1);
672 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
673 const struct nxm_field *f;
676 name_len = strcspn(s, "(");
677 if (s[name_len] != '(') {
678 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
681 f = lookup_nxm_field(s, name_len);
683 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
688 nxm_put_header(b, f->header);
689 s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
690 if (NXM_HASMASK(f->header)) {
693 ovs_fatal(0, "%s: missing / in masked field %s",
696 s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
701 ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
706 match_len = b->size - start_len;
707 ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);