2 * Copyright (c) 2009, 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.
18 #include "classifier.h"
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
30 static struct cls_table *find_table(const struct classifier *,
31 const struct flow_wildcards *);
32 static struct cls_table *insert_table(struct classifier *,
33 const struct flow_wildcards *);
35 static struct cls_table *classifier_first_table(const struct classifier *);
36 static struct cls_table *classifier_next_table(const struct classifier *,
37 const struct cls_table *);
38 static void destroy_table(struct classifier *, struct cls_table *);
40 static struct cls_rule *find_match(const struct cls_table *,
42 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
44 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
46 static bool flow_equal_except(const struct flow *, const struct flow *,
47 const struct flow_wildcards *);
48 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
50 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
51 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
52 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
53 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
54 for ((RULE) = (HEAD); \
55 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
58 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
59 static struct cls_rule *next_rule_in_list(struct cls_rule *);
61 static struct cls_table *
62 cls_table_from_hmap_node(const struct hmap_node *node)
64 return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
67 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
68 * 'wildcards' and 'priority'. */
70 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
71 unsigned int priority, struct cls_rule *rule)
74 rule->wc = *wildcards;
75 rule->priority = priority;
76 cls_rule_zero_wildcarded_fields(rule);
79 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
80 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
81 * priority, so 'priority' should be at least 65535.) */
83 cls_rule_init_exact(const struct flow *flow,
84 unsigned int priority, struct cls_rule *rule)
87 flow_wildcards_init_exact(&rule->wc);
88 rule->priority = priority;
91 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
92 * priority 'priority'. */
94 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
96 memset(&rule->flow, 0, sizeof rule->flow);
97 flow_wildcards_init_catchall(&rule->wc);
98 rule->priority = priority;
101 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
102 * field in 'flow' to all-0-bits. It is important to maintain this invariant
103 * in a clr_rule that might be inserted into a classifier.
105 * It is never necessary to call this function directly for a cls_rule that is
106 * initialized or modified only by cls_rule_*() functions. It is useful to
107 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
110 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
112 zero_wildcards(&rule->flow, &rule->wc);
116 cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value)
118 cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX);
122 cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx,
123 uint32_t value, uint32_t mask)
125 assert(reg_idx < FLOW_N_REGS);
126 flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask);
127 rule->flow.regs[reg_idx] = value & mask;
131 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
133 cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX));
137 cls_rule_set_tun_id_masked(struct cls_rule *rule,
138 ovs_be64 tun_id, ovs_be64 mask)
140 rule->wc.tun_id_mask = mask;
141 rule->flow.tun_id = tun_id & mask;
145 cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port)
147 rule->wc.wildcards &= ~FWW_IN_PORT;
148 rule->flow.in_port = ofp_port;
152 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
154 rule->wc.wildcards &= ~FWW_DL_TYPE;
155 rule->flow.dl_type = dl_type;
159 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
161 rule->wc.wildcards &= ~FWW_DL_SRC;
162 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
165 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' exactly. */
167 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
169 rule->wc.wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
170 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
173 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' after each
174 * byte is ANDed with the appropriate byte in 'mask'.
176 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
177 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
179 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
180 const uint8_t dl_dst[ETH_ADDR_LEN],
181 const uint8_t mask[ETH_ADDR_LEN])
183 flow_wildcards_t *wc = &rule->wc.wildcards;
186 *wc = flow_wildcards_set_dl_dst_mask(*wc, mask);
187 for (i = 0; i < ETH_ADDR_LEN; i++) {
188 rule->flow.dl_dst[i] = dl_dst[i] & mask[i];
193 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
195 cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
199 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
201 rule->flow.vlan_tci = tci & mask;
202 rule->wc.vlan_tci_mask = mask;
205 /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already
206 * wildcarded, then 'rule' will match a packet regardless of whether it has an
207 * 802.1Q header or not. */
209 cls_rule_set_any_vid(struct cls_rule *rule)
211 if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
212 rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
213 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
215 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
219 /* Modifies 'rule' depending on 'dl_vlan':
221 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
222 * without an 802.1Q header.
224 * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
225 * VID equals the low 12 bits of 'dl_vlan'.
228 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
230 if (dl_vlan == htons(OFP_VLAN_NONE)) {
231 cls_rule_set_dl_tci(rule, htons(0));
233 dl_vlan &= htons(VLAN_VID_MASK);
234 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
235 rule->flow.vlan_tci |= htons(VLAN_CFI) | dl_vlan;
236 rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
240 /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
241 * wildcarded, then 'rule' will match a packet regardless of whether it has an
242 * 802.1Q header or not. */
244 cls_rule_set_any_pcp(struct cls_rule *rule)
246 if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
247 rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
248 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
250 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
254 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
255 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
257 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
260 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
261 rule->flow.vlan_tci |= htons((dl_vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
262 rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
266 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
268 rule->wc.wildcards &= ~FWW_TP_SRC;
269 rule->flow.tp_src = tp_src;
273 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
275 rule->wc.wildcards &= ~FWW_TP_DST;
276 rule->flow.tp_dst = tp_dst;
280 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
282 rule->wc.wildcards &= ~FWW_NW_PROTO;
283 rule->flow.nw_proto = nw_proto;
287 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
289 cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
293 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
295 if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
296 rule->flow.nw_src = ip & mask;
304 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
306 cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
310 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
312 if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
313 rule->flow.nw_dst = ip & mask;
321 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
323 rule->wc.wildcards &= ~FWW_NW_TOS;
324 rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
328 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
330 rule->wc.wildcards &= ~FWW_TP_SRC;
331 rule->flow.icmp_type = htons(icmp_type);
336 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
338 rule->wc.wildcards &= ~FWW_TP_DST;
339 rule->flow.icmp_code = htons(icmp_code);
343 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
345 rule->wc.wildcards &= ~FWW_ARP_SHA;
346 memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN);
350 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
352 rule->wc.wildcards &= ~FWW_ARP_THA;
353 memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN);
357 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
359 cls_rule_set_ipv6_src_masked(rule, src, &in6addr_exact);
363 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
364 const struct in6_addr *mask)
366 if (flow_wildcards_set_ipv6_src_mask(&rule->wc, mask)) {
367 rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
375 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
377 cls_rule_set_ipv6_dst_masked(rule, dst, &in6addr_exact);
381 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
382 const struct in6_addr *mask)
384 if (flow_wildcards_set_ipv6_dst_mask(&rule->wc, mask)) {
385 rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
393 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr target)
395 rule->wc.wildcards &= ~FWW_ND_TARGET;
396 rule->flow.nd_target = target;
399 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
400 * fields, and have the same values for fixed fields, otherwise false. */
402 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
404 return (a->priority == b->priority
405 && flow_wildcards_equal(&a->wc, &b->wc)
406 && flow_equal(&a->flow, &b->flow));
409 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
410 * starting from 'basis'. */
412 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
414 uint32_t h0 = flow_hash(&rule->flow, basis);
415 uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
416 return hash_int(rule->priority, h1);
420 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
424 ds_put_format(s, "%s="IP_FMT, name, IP_ARGS(&ip));
425 if (netmask != htonl(UINT32_MAX)) {
426 if (ip_is_cidr(netmask)) {
427 int wcbits = ofputil_netmask_to_wcbits(netmask);
428 ds_put_format(s, "/%d", 32 - wcbits);
430 ds_put_format(s, "/"IP_FMT, IP_ARGS(&netmask));
438 format_ipv6_netmask(struct ds *s, const char *name,
439 const struct in6_addr *addr,
440 const struct in6_addr *netmask)
442 if (!ipv6_mask_is_any(netmask)) {
443 ds_put_format(s, "%s=", name);
444 print_ipv6_addr(s, addr);
445 if (!ipv6_mask_is_exact(netmask)) {
446 if (ipv6_is_cidr(netmask)) {
447 int cidr_bits = ipv6_count_cidr_bits(netmask);
448 ds_put_format(s, "/%d", cidr_bits);
451 print_ipv6_addr(s, netmask);
459 cls_rule_format(const struct cls_rule *rule, struct ds *s)
461 const struct flow_wildcards *wc = &rule->wc;
462 size_t start_len = s->length;
463 flow_wildcards_t w = wc->wildcards;
464 const struct flow *f = &rule->flow;
465 bool skip_type = false;
466 bool skip_proto = false;
470 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
472 if (rule->priority != OFP_DEFAULT_PRIORITY) {
473 ds_put_format(s, "priority=%d,", rule->priority);
476 if (!(w & FWW_DL_TYPE)) {
478 if (f->dl_type == htons(ETH_TYPE_IP)) {
479 if (!(w & FWW_NW_PROTO)) {
481 if (f->nw_proto == IPPROTO_ICMP) {
482 ds_put_cstr(s, "icmp,");
483 } else if (f->nw_proto == IPPROTO_TCP) {
484 ds_put_cstr(s, "tcp,");
485 } else if (f->nw_proto == IPPROTO_UDP) {
486 ds_put_cstr(s, "udp,");
488 ds_put_cstr(s, "ip,");
492 ds_put_cstr(s, "ip,");
494 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
495 if (!(w & FWW_NW_PROTO)) {
497 if (f->nw_proto == IPPROTO_ICMPV6) {
498 ds_put_cstr(s, "icmp6,");
499 } else if (f->nw_proto == IPPROTO_TCP) {
500 ds_put_cstr(s, "tcp6,");
501 } else if (f->nw_proto == IPPROTO_UDP) {
502 ds_put_cstr(s, "udp6,");
504 ds_put_cstr(s, "ipv6,");
508 ds_put_cstr(s, "ipv6,");
510 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
511 ds_put_cstr(s, "arp,");
516 for (i = 0; i < FLOW_N_REGS; i++) {
517 switch (wc->reg_masks[i]) {
521 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
524 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
525 i, f->regs[i], wc->reg_masks[i]);
529 switch (wc->tun_id_mask) {
532 case CONSTANT_HTONLL(UINT64_MAX):
533 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
536 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
537 ntohll(f->tun_id), ntohll(wc->tun_id_mask));
540 if (!(w & FWW_IN_PORT)) {
541 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
543 if (wc->vlan_tci_mask) {
544 ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
545 ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
546 ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
548 if (cfi && f->vlan_tci & htons(VLAN_CFI)
549 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
550 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
551 && (vid_mask || pcp_mask)) {
553 ds_put_format(s, "dl_vlan=%"PRIu16",",
554 vlan_tci_to_vid(f->vlan_tci));
557 ds_put_format(s, "dl_vlan_pcp=%d,",
558 vlan_tci_to_pcp(f->vlan_tci));
560 } else if (wc->vlan_tci_mask == htons(0xffff)) {
561 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
563 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
564 ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
567 if (!(w & FWW_DL_SRC)) {
568 ds_put_format(s, "dl_src="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_src));
570 switch (w & (FWW_DL_DST | FWW_ETH_MCAST)) {
572 ds_put_format(s, "dl_dst="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_dst));
575 ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/01:00:00:00:00:00,",
576 ETH_ADDR_ARGS(f->dl_dst));
579 ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/fe:ff:ff:ff:ff:ff,",
580 ETH_ADDR_ARGS(f->dl_dst));
582 case FWW_DL_DST | FWW_ETH_MCAST:
585 if (!skip_type && !(w & FWW_DL_TYPE)) {
586 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
588 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
589 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask);
590 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
592 format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
593 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
595 if (!skip_proto && !(w & FWW_NW_PROTO)) {
596 if (f->dl_type == htons(ETH_TYPE_ARP)) {
597 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
599 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
602 if (f->dl_type == htons(ETH_TYPE_ARP)) {
603 if (!(w & FWW_ARP_SHA)) {
604 ds_put_format(s, "arp_sha="ETH_ADDR_FMT",",
605 ETH_ADDR_ARGS(f->arp_sha));
607 if (!(w & FWW_ARP_THA)) {
608 ds_put_format(s, "arp_tha="ETH_ADDR_FMT",",
609 ETH_ADDR_ARGS(f->arp_tha));
612 if (!(w & FWW_NW_TOS)) {
613 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos);
615 if (f->nw_proto == IPPROTO_ICMP) {
616 if (!(w & FWW_TP_SRC)) {
617 ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src));
619 if (!(w & FWW_TP_DST)) {
620 ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst));
622 } else if (f->nw_proto == IPPROTO_ICMPV6) {
623 if (!(w & FWW_TP_SRC)) {
624 ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src));
626 if (!(w & FWW_TP_DST)) {
627 ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst));
629 if (!(w & FWW_ND_TARGET)) {
630 ds_put_cstr(s, "nd_target=");
631 print_ipv6_addr(s, &f->nd_target);
634 if (!(w & FWW_ARP_SHA)) {
635 ds_put_format(s, "nd_sll="ETH_ADDR_FMT",",
636 ETH_ADDR_ARGS(f->arp_sha));
638 if (!(w & FWW_ARP_THA)) {
639 ds_put_format(s, "nd_tll="ETH_ADDR_FMT",",
640 ETH_ADDR_ARGS(f->arp_tha));
643 if (!(w & FWW_TP_SRC)) {
644 ds_put_format(s, "tp_src=%"PRIu16",", ntohs(f->tp_src));
646 if (!(w & FWW_TP_DST)) {
647 ds_put_format(s, "tp_dst=%"PRIu16",", ntohs(f->tp_dst));
651 if (s->length > start_len && ds_last(s) == ',') {
656 /* Converts 'rule' to a string and returns the string. The caller must free
657 * the string (with free()). */
659 cls_rule_to_string(const struct cls_rule *rule)
661 struct ds s = DS_EMPTY_INITIALIZER;
662 cls_rule_format(rule, &s);
663 return ds_steal_cstr(&s);
667 cls_rule_print(const struct cls_rule *rule)
669 char *s = cls_rule_to_string(rule);
674 /* Initializes 'cls' as a classifier that initially contains no classification
677 classifier_init(struct classifier *cls)
680 hmap_init(&cls->tables);
683 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
684 * caller's responsibility. */
686 classifier_destroy(struct classifier *cls)
689 struct cls_table *table, *next_table;
691 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
692 hmap_destroy(&table->rules);
693 hmap_remove(&cls->tables, &table->hmap_node);
696 hmap_destroy(&cls->tables);
700 /* Returns true if 'cls' contains no classification rules, false otherwise. */
702 classifier_is_empty(const struct classifier *cls)
704 return cls->n_rules == 0;
707 /* Returns the number of rules in 'classifier'. */
709 classifier_count(const struct classifier *cls)
714 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
715 * must not modify or free it.
717 * If 'cls' already contains an identical rule (including wildcards, values of
718 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
719 * rule that was replaced. The caller takes ownership of the returned rule and
720 * is thus responsible for freeing it, etc., as necessary.
722 * Returns NULL if 'cls' does not contain a rule with an identical key, after
723 * inserting the new rule. In this case, no rules are displaced by the new
724 * rule, even rules that cannot have any effect because the new rule matches a
725 * superset of their flows and has higher priority. */
727 classifier_replace(struct classifier *cls, struct cls_rule *rule)
729 struct cls_rule *old_rule;
730 struct cls_table *table;
732 table = find_table(cls, &rule->wc);
734 table = insert_table(cls, &rule->wc);
737 old_rule = insert_rule(table, rule);
739 table->n_table_rules++;
745 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
746 * must not modify or free it.
748 * 'cls' must not contain an identical rule (including wildcards, values of
749 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
752 classifier_insert(struct classifier *cls, struct cls_rule *rule)
754 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
755 assert(!displaced_rule);
758 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
759 * 'rule', if this is desirable. */
761 classifier_remove(struct classifier *cls, struct cls_rule *rule)
763 struct cls_rule *head;
764 struct cls_table *table;
766 table = find_table(cls, &rule->wc);
767 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
769 list_remove(&rule->list);
770 } else if (list_is_empty(&rule->list)) {
771 hmap_remove(&table->rules, &rule->hmap_node);
773 struct cls_rule *next = CONTAINER_OF(rule->list.next,
774 struct cls_rule, list);
776 list_remove(&rule->list);
777 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
780 if (--table->n_table_rules == 0) {
781 destroy_table(cls, table);
787 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
788 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
789 * of equal priority match 'flow', returns one arbitrarily. */
791 classifier_lookup(const struct classifier *cls, const struct flow *flow)
793 struct cls_table *table;
794 struct cls_rule *best;
797 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
798 struct cls_rule *rule = find_match(table, flow);
799 if (rule && (!best || rule->priority > best->priority)) {
806 /* Finds and returns a rule in 'cls' with exactly the same priority and
807 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
808 * contain an exact match. */
810 classifier_find_rule_exactly(const struct classifier *cls,
811 const struct cls_rule *target)
813 struct cls_rule *head, *rule;
814 struct cls_table *table;
816 table = find_table(cls, &target->wc);
821 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
822 FOR_EACH_RULE_IN_LIST (rule, head) {
823 if (target->priority >= rule->priority) {
824 return target->priority == rule->priority ? rule : NULL;
830 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
831 * considered to overlap if both rules have the same priority and a packet
832 * could match both. */
834 classifier_rule_overlaps(const struct classifier *cls,
835 const struct cls_rule *target)
837 struct cls_table *table;
839 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
840 struct flow_wildcards wc;
841 struct cls_rule *head;
843 flow_wildcards_combine(&wc, &target->wc, &table->wc);
844 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
845 struct cls_rule *rule;
847 FOR_EACH_RULE_IN_LIST (rule, head) {
848 if (rule->priority == target->priority
849 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
862 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
865 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
868 static struct cls_rule *
869 search_table(const struct cls_table *table, const struct cls_rule *target)
871 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
872 struct cls_rule *rule;
874 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
875 if (rule_matches(rule, target)) {
883 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
884 * 'target' or are more specific than 'target'. That is, a given 'rule'
885 * matches 'target' if, for every field:
887 * - 'target' and 'rule' specify the same (non-wildcarded) value for the
890 * - 'target' wildcards the field,
894 * - 'target' and 'rule' specify different values for the field, or
896 * - 'target' specifies a value for the field but 'rule' wildcards it.
898 * Equivalently, the truth table for whether a field matches is:
903 * +---------+---------+
904 * t wild | yes | yes |
906 * r +---------+---------+
907 * g exact | no |if values|
909 * t +---------+---------+
911 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
912 * commands and by OpenFlow 1.0 aggregate and flow stats.
914 * Ignores target->priority.
916 * 'target' may be NULL to iterate over every rule in 'cls'. */
918 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
919 const struct cls_rule *target)
922 cursor->target = target;
925 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
926 * pointer if there are no matches. */
928 cls_cursor_first(struct cls_cursor *cursor)
930 struct cls_table *table;
932 for (table = classifier_first_table(cursor->cls); table;
933 table = classifier_next_table(cursor->cls, table)) {
934 struct cls_rule *rule = search_table(table, cursor->target);
936 cursor->table = table;
944 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
945 * pointer if there are no more matches. */
947 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
949 const struct cls_table *table;
950 struct cls_rule *next;
952 next = next_rule_in_list__(rule);
953 if (next->priority < rule->priority) {
957 /* 'next' is the head of the list, that is, the rule that is included in
958 * the table's hmap. (This is important when the classifier contains rules
959 * that differ only in priority.) */
961 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
962 if (rule_matches(rule, cursor->target)) {
967 for (table = classifier_next_table(cursor->cls, cursor->table); table;
968 table = classifier_next_table(cursor->cls, table)) {
969 rule = search_table(table, cursor->target);
971 cursor->table = table;
979 static struct cls_table *
980 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
982 struct cls_table *table;
984 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
986 if (flow_wildcards_equal(wc, &table->wc)) {
993 static struct cls_table *
994 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
996 struct cls_table *table;
998 table = xzalloc(sizeof *table);
999 hmap_init(&table->rules);
1001 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1006 static struct cls_table *
1007 classifier_first_table(const struct classifier *cls)
1009 return cls_table_from_hmap_node(hmap_first(&cls->tables));
1012 static struct cls_table *
1013 classifier_next_table(const struct classifier *cls,
1014 const struct cls_table *table)
1016 return cls_table_from_hmap_node(hmap_next(&cls->tables,
1017 &table->hmap_node));
1021 destroy_table(struct classifier *cls, struct cls_table *table)
1023 hmap_remove(&cls->tables, &table->hmap_node);
1024 hmap_destroy(&table->rules);
1028 static struct cls_rule *
1029 find_match(const struct cls_table *table, const struct flow *flow)
1031 struct cls_rule *rule;
1035 zero_wildcards(&f, &table->wc);
1036 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1038 if (flow_equal(&f, &rule->flow)) {
1045 static struct cls_rule *
1046 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1048 struct cls_rule *head;
1050 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1051 if (flow_equal(&head->flow, flow)) {
1058 static struct cls_rule *
1059 insert_rule(struct cls_table *table, struct cls_rule *new)
1061 struct cls_rule *head;
1063 new->hmap_node.hash = flow_hash(&new->flow, 0);
1065 head = find_equal(table, &new->flow, new->hmap_node.hash);
1067 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1068 list_init(&new->list);
1071 /* Scan the list for the insertion point that will keep the list in
1072 * order of decreasing priority. */
1073 struct cls_rule *rule;
1074 FOR_EACH_RULE_IN_LIST (rule, head) {
1075 if (new->priority >= rule->priority) {
1077 /* 'new' is the new highest-priority flow in the list. */
1078 hmap_replace(&table->rules,
1079 &rule->hmap_node, &new->hmap_node);
1082 if (new->priority == rule->priority) {
1083 list_replace(&new->list, &rule->list);
1086 list_insert(&rule->list, &new->list);
1092 /* Insert 'new' at the end of the list. */
1093 list_push_back(&head->list, &new->list);
1098 static struct cls_rule *
1099 next_rule_in_list__(struct cls_rule *rule)
1101 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1105 static struct cls_rule *
1106 next_rule_in_list(struct cls_rule *rule)
1108 struct cls_rule *next = next_rule_in_list__(rule);
1109 return next->priority < rule->priority ? next : NULL;
1113 ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b,
1114 const struct in6_addr *mask)
1119 for (i=0; i<4; i++) {
1120 if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) {
1125 for (i=0; i<16; i++) {
1126 if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) {
1137 flow_equal_except(const struct flow *a, const struct flow *b,
1138 const struct flow_wildcards *wildcards)
1140 const flow_wildcards_t wc = wildcards->wildcards;
1143 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
1145 for (i = 0; i < FLOW_N_REGS; i++) {
1146 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
1151 return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask)
1152 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
1153 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
1154 && (wc & FWW_IN_PORT || a->in_port == b->in_port)
1155 && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
1156 && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
1157 && (wc & FWW_TP_SRC || a->tp_src == b->tp_src)
1158 && (wc & FWW_TP_DST || a->tp_dst == b->tp_dst)
1159 && (wc & FWW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
1161 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
1162 && a->dl_dst[1] == b->dl_dst[1]
1163 && a->dl_dst[2] == b->dl_dst[2]
1164 && a->dl_dst[3] == b->dl_dst[3]
1165 && a->dl_dst[4] == b->dl_dst[4]
1166 && a->dl_dst[5] == b->dl_dst[5]))
1167 && (wc & FWW_ETH_MCAST
1168 || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
1169 && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
1170 && (wc & FWW_NW_TOS || a->nw_tos == b->nw_tos)
1171 && (wc & FWW_ARP_SHA || eth_addr_equals(a->arp_sha, b->arp_sha))
1172 && (wc & FWW_ARP_THA || eth_addr_equals(a->arp_tha, b->arp_tha))
1173 && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src,
1174 &wildcards->ipv6_src_mask)
1175 && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst,
1176 &wildcards->ipv6_dst_mask)
1177 && (wc & FWW_ND_TARGET
1178 || ipv6_addr_equals(&a->nd_target, &b->nd_target)));
1182 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
1184 const flow_wildcards_t wc = wildcards->wildcards;
1187 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
1189 for (i = 0; i < FLOW_N_REGS; i++) {
1190 flow->regs[i] &= wildcards->reg_masks[i];
1192 flow->tun_id &= wildcards->tun_id_mask;
1193 flow->nw_src &= wildcards->nw_src_mask;
1194 flow->nw_dst &= wildcards->nw_dst_mask;
1195 if (wc & FWW_IN_PORT) {
1198 flow->vlan_tci &= wildcards->vlan_tci_mask;
1199 if (wc & FWW_DL_TYPE) {
1202 if (wc & FWW_TP_SRC) {
1205 if (wc & FWW_TP_DST) {
1208 if (wc & FWW_DL_SRC) {
1209 memset(flow->dl_src, 0, sizeof flow->dl_src);
1211 if (wc & FWW_DL_DST) {
1212 flow->dl_dst[0] &= 0x01;
1213 memset(&flow->dl_dst[1], 0, 5);
1215 if (wc & FWW_ETH_MCAST) {
1216 flow->dl_dst[0] &= 0xfe;
1218 if (wc & FWW_NW_PROTO) {
1221 if (wc & FWW_NW_TOS) {
1224 if (wc & FWW_ARP_SHA) {
1225 memset(flow->arp_sha, 0, sizeof flow->arp_sha);
1227 if (wc & FWW_ARP_THA) {
1228 memset(flow->arp_tha, 0, sizeof flow->arp_tha);
1230 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
1231 &wildcards->ipv6_src_mask);
1232 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
1233 &wildcards->ipv6_dst_mask);
1234 if (wc & FWW_ND_TARGET) {
1235 memset(&flow->nd_target, 0, sizeof flow->nd_target);