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 *);
49 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
50 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
51 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
52 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
53 for ((RULE) = (HEAD); \
54 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
57 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
58 static struct cls_rule *next_rule_in_list(struct cls_rule *);
60 static struct cls_table *
61 cls_table_from_hmap_node(const struct hmap_node *node)
63 return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
66 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
67 * 'wildcards' and 'priority'. */
69 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
70 unsigned int priority, struct cls_rule *rule)
73 rule->wc = *wildcards;
74 rule->priority = priority;
75 cls_rule_zero_wildcarded_fields(rule);
78 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
79 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
80 * priority, so 'priority' should be at least 65535.) */
82 cls_rule_init_exact(const struct flow *flow,
83 unsigned int priority, struct cls_rule *rule)
86 flow_wildcards_init_exact(&rule->wc);
87 rule->priority = priority;
90 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
91 * priority 'priority'. */
93 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
95 memset(&rule->flow, 0, sizeof rule->flow);
96 flow_wildcards_init_catchall(&rule->wc);
97 rule->priority = priority;
100 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
101 * field in 'flow' to all-0-bits. It is important to maintain this invariant
102 * in a clr_rule that might be inserted into a classifier.
104 * It is never necessary to call this function directly for a cls_rule that is
105 * initialized or modified only by cls_rule_*() functions. It is useful to
106 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
109 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
111 flow_zero_wildcards(&rule->flow, &rule->wc);
115 cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value)
117 cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX);
121 cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx,
122 uint32_t value, uint32_t mask)
124 assert(reg_idx < FLOW_N_REGS);
125 flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask);
126 rule->flow.regs[reg_idx] = value & mask;
130 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
132 cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX));
136 cls_rule_set_tun_id_masked(struct cls_rule *rule,
137 ovs_be64 tun_id, ovs_be64 mask)
139 rule->wc.tun_id_mask = mask;
140 rule->flow.tun_id = tun_id & mask;
144 cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port)
146 rule->wc.wildcards &= ~FWW_IN_PORT;
147 rule->flow.in_port = ofp_port;
151 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
153 rule->wc.wildcards &= ~FWW_DL_TYPE;
154 rule->flow.dl_type = dl_type;
158 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
160 rule->wc.wildcards &= ~FWW_DL_SRC;
161 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
164 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' exactly. */
166 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
168 rule->wc.wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
169 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
172 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' after each
173 * byte is ANDed with the appropriate byte in 'mask'.
175 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
176 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
178 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
179 const uint8_t dl_dst[ETH_ADDR_LEN],
180 const uint8_t mask[ETH_ADDR_LEN])
182 flow_wildcards_t *wc = &rule->wc.wildcards;
185 *wc = flow_wildcards_set_dl_dst_mask(*wc, mask);
186 for (i = 0; i < ETH_ADDR_LEN; i++) {
187 rule->flow.dl_dst[i] = dl_dst[i] & mask[i];
192 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
194 cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
198 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
200 rule->flow.vlan_tci = tci & mask;
201 rule->wc.vlan_tci_mask = mask;
204 /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already
205 * wildcarded, then 'rule' will match a packet regardless of whether it has an
206 * 802.1Q header or not. */
208 cls_rule_set_any_vid(struct cls_rule *rule)
210 if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
211 rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
212 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
214 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
218 /* Modifies 'rule' depending on 'dl_vlan':
220 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
221 * without an 802.1Q header.
223 * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
224 * VID equals the low 12 bits of 'dl_vlan'.
227 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
229 if (dl_vlan == htons(OFP_VLAN_NONE)) {
230 cls_rule_set_dl_tci(rule, htons(0));
232 dl_vlan &= htons(VLAN_VID_MASK);
233 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
234 rule->flow.vlan_tci |= htons(VLAN_CFI) | dl_vlan;
235 rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
239 /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
240 * wildcarded, then 'rule' will match a packet regardless of whether it has an
241 * 802.1Q header or not. */
243 cls_rule_set_any_pcp(struct cls_rule *rule)
245 if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
246 rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
247 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
249 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
253 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
254 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
256 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
259 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
260 rule->flow.vlan_tci |= htons((dl_vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
261 rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
265 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
267 rule->wc.wildcards &= ~FWW_TP_SRC;
268 rule->flow.tp_src = tp_src;
272 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
274 rule->wc.wildcards &= ~FWW_TP_DST;
275 rule->flow.tp_dst = tp_dst;
279 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
281 rule->wc.wildcards &= ~FWW_NW_PROTO;
282 rule->flow.nw_proto = nw_proto;
286 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
288 cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
292 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
294 if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
295 rule->flow.nw_src = ip & mask;
303 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
305 cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
309 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
311 if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
312 rule->flow.nw_dst = ip & mask;
320 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
322 rule->wc.tos_frag_mask |= IP_DSCP_MASK;
323 rule->flow.tos_frag &= ~IP_DSCP_MASK;
324 rule->flow.tos_frag |= nw_tos & IP_DSCP_MASK;
328 cls_rule_set_frag(struct cls_rule *rule, uint8_t frag)
330 rule->wc.tos_frag_mask |= FLOW_FRAG_MASK;
331 rule->flow.tos_frag &= ~FLOW_FRAG_MASK;
332 rule->flow.tos_frag |= frag & FLOW_FRAG_MASK;
336 cls_rule_set_frag_masked(struct cls_rule *rule, uint8_t frag, uint8_t mask)
338 mask &= FLOW_FRAG_MASK;
340 rule->wc.tos_frag_mask = (rule->wc.tos_frag_mask & ~FLOW_FRAG_MASK) | mask;
341 rule->flow.tos_frag = (rule->flow.tos_frag & ~FLOW_FRAG_MASK) | frag;
345 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
347 rule->wc.wildcards &= ~FWW_TP_SRC;
348 rule->flow.tp_src = htons(icmp_type);
353 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
355 rule->wc.wildcards &= ~FWW_TP_DST;
356 rule->flow.tp_dst = htons(icmp_code);
360 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
362 rule->wc.wildcards &= ~FWW_ARP_SHA;
363 memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN);
367 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
369 rule->wc.wildcards &= ~FWW_ARP_THA;
370 memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN);
374 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
376 cls_rule_set_ipv6_src_masked(rule, src, &in6addr_exact);
380 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
381 const struct in6_addr *mask)
383 if (flow_wildcards_set_ipv6_src_mask(&rule->wc, mask)) {
384 rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
392 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
394 cls_rule_set_ipv6_dst_masked(rule, dst, &in6addr_exact);
398 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
399 const struct in6_addr *mask)
401 if (flow_wildcards_set_ipv6_dst_mask(&rule->wc, mask)) {
402 rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
410 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
412 rule->wc.wildcards &= ~FWW_ND_TARGET;
413 rule->flow.nd_target = *target;
416 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
417 * fields, and have the same values for fixed fields, otherwise false. */
419 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
421 return (a->priority == b->priority
422 && flow_wildcards_equal(&a->wc, &b->wc)
423 && flow_equal(&a->flow, &b->flow));
426 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
427 * starting from 'basis'. */
429 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
431 uint32_t h0 = flow_hash(&rule->flow, basis);
432 uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
433 return hash_int(rule->priority, h1);
437 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
441 ds_put_format(s, "%s=", name);
442 ip_format_masked(ip, netmask, s);
448 format_ipv6_netmask(struct ds *s, const char *name,
449 const struct in6_addr *addr,
450 const struct in6_addr *netmask)
452 if (!ipv6_mask_is_any(netmask)) {
453 ds_put_format(s, "%s=", name);
454 print_ipv6_masked(s, addr, netmask);
460 cls_rule_format(const struct cls_rule *rule, struct ds *s)
462 const struct flow_wildcards *wc = &rule->wc;
463 size_t start_len = s->length;
464 flow_wildcards_t w = wc->wildcards;
465 const struct flow *f = &rule->flow;
466 bool skip_type = false;
467 bool skip_proto = false;
471 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 3);
473 if (rule->priority != OFP_DEFAULT_PRIORITY) {
474 ds_put_format(s, "priority=%d,", rule->priority);
477 if (!(w & FWW_DL_TYPE)) {
479 if (f->dl_type == htons(ETH_TYPE_IP)) {
480 if (!(w & FWW_NW_PROTO)) {
482 if (f->nw_proto == IPPROTO_ICMP) {
483 ds_put_cstr(s, "icmp,");
484 } else if (f->nw_proto == IPPROTO_TCP) {
485 ds_put_cstr(s, "tcp,");
486 } else if (f->nw_proto == IPPROTO_UDP) {
487 ds_put_cstr(s, "udp,");
489 ds_put_cstr(s, "ip,");
493 ds_put_cstr(s, "ip,");
495 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
496 if (!(w & FWW_NW_PROTO)) {
498 if (f->nw_proto == IPPROTO_ICMPV6) {
499 ds_put_cstr(s, "icmp6,");
500 } else if (f->nw_proto == IPPROTO_TCP) {
501 ds_put_cstr(s, "tcp6,");
502 } else if (f->nw_proto == IPPROTO_UDP) {
503 ds_put_cstr(s, "udp6,");
505 ds_put_cstr(s, "ipv6,");
509 ds_put_cstr(s, "ipv6,");
511 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
512 ds_put_cstr(s, "arp,");
517 for (i = 0; i < FLOW_N_REGS; i++) {
518 switch (wc->reg_masks[i]) {
522 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
525 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
526 i, f->regs[i], wc->reg_masks[i]);
530 switch (wc->tun_id_mask) {
533 case CONSTANT_HTONLL(UINT64_MAX):
534 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
537 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
538 ntohll(f->tun_id), ntohll(wc->tun_id_mask));
541 if (!(w & FWW_IN_PORT)) {
542 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
544 if (wc->vlan_tci_mask) {
545 ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
546 ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
547 ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
549 if (cfi && f->vlan_tci & htons(VLAN_CFI)
550 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
551 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
552 && (vid_mask || pcp_mask)) {
554 ds_put_format(s, "dl_vlan=%"PRIu16",",
555 vlan_tci_to_vid(f->vlan_tci));
558 ds_put_format(s, "dl_vlan_pcp=%d,",
559 vlan_tci_to_pcp(f->vlan_tci));
561 } else if (wc->vlan_tci_mask == htons(0xffff)) {
562 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
564 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
565 ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
568 if (!(w & FWW_DL_SRC)) {
569 ds_put_format(s, "dl_src="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_src));
571 switch (w & (FWW_DL_DST | FWW_ETH_MCAST)) {
573 ds_put_format(s, "dl_dst="ETH_ADDR_FMT",", ETH_ADDR_ARGS(f->dl_dst));
576 ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/01:00:00:00:00:00,",
577 ETH_ADDR_ARGS(f->dl_dst));
580 ds_put_format(s, "dl_dst="ETH_ADDR_FMT"/fe:ff:ff:ff:ff:ff,",
581 ETH_ADDR_ARGS(f->dl_dst));
583 case FWW_DL_DST | FWW_ETH_MCAST:
586 if (!skip_type && !(w & FWW_DL_TYPE)) {
587 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
589 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
590 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask);
591 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
593 format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
594 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
596 if (!skip_proto && !(w & FWW_NW_PROTO)) {
597 if (f->dl_type == htons(ETH_TYPE_ARP)) {
598 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
600 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
603 if (f->dl_type == htons(ETH_TYPE_ARP)) {
604 if (!(w & FWW_ARP_SHA)) {
605 ds_put_format(s, "arp_sha="ETH_ADDR_FMT",",
606 ETH_ADDR_ARGS(f->arp_sha));
608 if (!(w & FWW_ARP_THA)) {
609 ds_put_format(s, "arp_tha="ETH_ADDR_FMT",",
610 ETH_ADDR_ARGS(f->arp_tha));
613 if (wc->tos_frag_mask & IP_DSCP_MASK) {
614 ds_put_format(s, "nw_tos=%"PRIu8",", f->tos_frag & IP_DSCP_MASK);
616 switch (wc->tos_frag_mask & FLOW_FRAG_MASK) {
617 case FLOW_FRAG_ANY | FLOW_FRAG_LATER:
618 ds_put_format(s, "frag=%s,",
619 f->tos_frag & FLOW_FRAG_ANY
620 ? (f->tos_frag & FLOW_FRAG_LATER ? "later" : "first")
621 : (f->tos_frag & FLOW_FRAG_LATER ? "<error>" : "no"));
625 ds_put_format(s, "frag=%s,",
626 f->tos_frag & FLOW_FRAG_ANY ? "yes" : "no");
629 case FLOW_FRAG_LATER:
630 ds_put_format(s, "frag=%s,",
631 f->tos_frag & FLOW_FRAG_LATER ? "later" : "not_later");
634 if (f->nw_proto == IPPROTO_ICMP) {
635 if (!(w & FWW_TP_SRC)) {
636 ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src));
638 if (!(w & FWW_TP_DST)) {
639 ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst));
641 } else if (f->nw_proto == IPPROTO_ICMPV6) {
642 if (!(w & FWW_TP_SRC)) {
643 ds_put_format(s, "icmp_type=%"PRIu16",", ntohs(f->tp_src));
645 if (!(w & FWW_TP_DST)) {
646 ds_put_format(s, "icmp_code=%"PRIu16",", ntohs(f->tp_dst));
648 if (!(w & FWW_ND_TARGET)) {
649 ds_put_cstr(s, "nd_target=");
650 print_ipv6_addr(s, &f->nd_target);
653 if (!(w & FWW_ARP_SHA)) {
654 ds_put_format(s, "nd_sll="ETH_ADDR_FMT",",
655 ETH_ADDR_ARGS(f->arp_sha));
657 if (!(w & FWW_ARP_THA)) {
658 ds_put_format(s, "nd_tll="ETH_ADDR_FMT",",
659 ETH_ADDR_ARGS(f->arp_tha));
662 if (!(w & FWW_TP_SRC)) {
663 ds_put_format(s, "tp_src=%"PRIu16",", ntohs(f->tp_src));
665 if (!(w & FWW_TP_DST)) {
666 ds_put_format(s, "tp_dst=%"PRIu16",", ntohs(f->tp_dst));
670 if (s->length > start_len && ds_last(s) == ',') {
675 /* Converts 'rule' to a string and returns the string. The caller must free
676 * the string (with free()). */
678 cls_rule_to_string(const struct cls_rule *rule)
680 struct ds s = DS_EMPTY_INITIALIZER;
681 cls_rule_format(rule, &s);
682 return ds_steal_cstr(&s);
686 cls_rule_print(const struct cls_rule *rule)
688 char *s = cls_rule_to_string(rule);
693 /* Initializes 'cls' as a classifier that initially contains no classification
696 classifier_init(struct classifier *cls)
699 hmap_init(&cls->tables);
702 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
703 * caller's responsibility. */
705 classifier_destroy(struct classifier *cls)
708 struct cls_table *table, *next_table;
710 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
711 hmap_destroy(&table->rules);
712 hmap_remove(&cls->tables, &table->hmap_node);
715 hmap_destroy(&cls->tables);
719 /* Returns true if 'cls' contains no classification rules, false otherwise. */
721 classifier_is_empty(const struct classifier *cls)
723 return cls->n_rules == 0;
726 /* Returns the number of rules in 'classifier'. */
728 classifier_count(const struct classifier *cls)
733 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
734 * must not modify or free it.
736 * If 'cls' already contains an identical rule (including wildcards, values of
737 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
738 * rule that was replaced. The caller takes ownership of the returned rule and
739 * is thus responsible for freeing it, etc., as necessary.
741 * Returns NULL if 'cls' does not contain a rule with an identical key, after
742 * inserting the new rule. In this case, no rules are displaced by the new
743 * rule, even rules that cannot have any effect because the new rule matches a
744 * superset of their flows and has higher priority. */
746 classifier_replace(struct classifier *cls, struct cls_rule *rule)
748 struct cls_rule *old_rule;
749 struct cls_table *table;
751 table = find_table(cls, &rule->wc);
753 table = insert_table(cls, &rule->wc);
756 old_rule = insert_rule(table, rule);
758 table->n_table_rules++;
764 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
765 * must not modify or free it.
767 * 'cls' must not contain an identical rule (including wildcards, values of
768 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
771 classifier_insert(struct classifier *cls, struct cls_rule *rule)
773 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
774 assert(!displaced_rule);
777 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
778 * 'rule', if this is desirable. */
780 classifier_remove(struct classifier *cls, struct cls_rule *rule)
782 struct cls_rule *head;
783 struct cls_table *table;
785 table = find_table(cls, &rule->wc);
786 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
788 list_remove(&rule->list);
789 } else if (list_is_empty(&rule->list)) {
790 hmap_remove(&table->rules, &rule->hmap_node);
792 struct cls_rule *next = CONTAINER_OF(rule->list.next,
793 struct cls_rule, list);
795 list_remove(&rule->list);
796 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
799 if (--table->n_table_rules == 0) {
800 destroy_table(cls, table);
806 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
807 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
808 * of equal priority match 'flow', returns one arbitrarily. */
810 classifier_lookup(const struct classifier *cls, const struct flow *flow)
812 struct cls_table *table;
813 struct cls_rule *best;
816 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
817 struct cls_rule *rule = find_match(table, flow);
818 if (rule && (!best || rule->priority > best->priority)) {
825 /* Finds and returns a rule in 'cls' with exactly the same priority and
826 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
827 * contain an exact match. */
829 classifier_find_rule_exactly(const struct classifier *cls,
830 const struct cls_rule *target)
832 struct cls_rule *head, *rule;
833 struct cls_table *table;
835 table = find_table(cls, &target->wc);
840 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
841 FOR_EACH_RULE_IN_LIST (rule, head) {
842 if (target->priority >= rule->priority) {
843 return target->priority == rule->priority ? rule : NULL;
849 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
850 * considered to overlap if both rules have the same priority and a packet
851 * could match both. */
853 classifier_rule_overlaps(const struct classifier *cls,
854 const struct cls_rule *target)
856 struct cls_table *table;
858 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
859 struct flow_wildcards wc;
860 struct cls_rule *head;
862 flow_wildcards_combine(&wc, &target->wc, &table->wc);
863 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
864 struct cls_rule *rule;
866 FOR_EACH_RULE_IN_LIST (rule, head) {
867 if (rule->priority == target->priority
868 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
881 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
884 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
887 static struct cls_rule *
888 search_table(const struct cls_table *table, const struct cls_rule *target)
890 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
891 struct cls_rule *rule;
893 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
894 if (rule_matches(rule, target)) {
902 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
903 * 'target' or are more specific than 'target'. That is, a given 'rule'
904 * matches 'target' if, for every field:
906 * - 'target' and 'rule' specify the same (non-wildcarded) value for the
909 * - 'target' wildcards the field,
913 * - 'target' and 'rule' specify different values for the field, or
915 * - 'target' specifies a value for the field but 'rule' wildcards it.
917 * Equivalently, the truth table for whether a field matches is:
922 * +---------+---------+
923 * t wild | yes | yes |
925 * r +---------+---------+
926 * g exact | no |if values|
928 * t +---------+---------+
930 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
931 * commands and by OpenFlow 1.0 aggregate and flow stats.
933 * Ignores target->priority.
935 * 'target' may be NULL to iterate over every rule in 'cls'. */
937 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
938 const struct cls_rule *target)
941 cursor->target = target;
944 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
945 * pointer if there are no matches. */
947 cls_cursor_first(struct cls_cursor *cursor)
949 struct cls_table *table;
951 for (table = classifier_first_table(cursor->cls); table;
952 table = classifier_next_table(cursor->cls, table)) {
953 struct cls_rule *rule = search_table(table, cursor->target);
955 cursor->table = table;
963 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
964 * pointer if there are no more matches. */
966 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
968 const struct cls_table *table;
969 struct cls_rule *next;
971 next = next_rule_in_list__(rule);
972 if (next->priority < rule->priority) {
976 /* 'next' is the head of the list, that is, the rule that is included in
977 * the table's hmap. (This is important when the classifier contains rules
978 * that differ only in priority.) */
980 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
981 if (rule_matches(rule, cursor->target)) {
986 for (table = classifier_next_table(cursor->cls, cursor->table); table;
987 table = classifier_next_table(cursor->cls, table)) {
988 rule = search_table(table, cursor->target);
990 cursor->table = table;
998 static struct cls_table *
999 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
1001 struct cls_table *table;
1003 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
1005 if (flow_wildcards_equal(wc, &table->wc)) {
1012 static struct cls_table *
1013 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
1015 struct cls_table *table;
1017 table = xzalloc(sizeof *table);
1018 hmap_init(&table->rules);
1020 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1025 static struct cls_table *
1026 classifier_first_table(const struct classifier *cls)
1028 return cls_table_from_hmap_node(hmap_first(&cls->tables));
1031 static struct cls_table *
1032 classifier_next_table(const struct classifier *cls,
1033 const struct cls_table *table)
1035 return cls_table_from_hmap_node(hmap_next(&cls->tables,
1036 &table->hmap_node));
1040 destroy_table(struct classifier *cls, struct cls_table *table)
1042 hmap_remove(&cls->tables, &table->hmap_node);
1043 hmap_destroy(&table->rules);
1047 static struct cls_rule *
1048 find_match(const struct cls_table *table, const struct flow *flow)
1050 struct cls_rule *rule;
1054 flow_zero_wildcards(&f, &table->wc);
1055 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1057 if (flow_equal(&f, &rule->flow)) {
1064 static struct cls_rule *
1065 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1067 struct cls_rule *head;
1069 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1070 if (flow_equal(&head->flow, flow)) {
1077 static struct cls_rule *
1078 insert_rule(struct cls_table *table, struct cls_rule *new)
1080 struct cls_rule *head;
1082 new->hmap_node.hash = flow_hash(&new->flow, 0);
1084 head = find_equal(table, &new->flow, new->hmap_node.hash);
1086 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1087 list_init(&new->list);
1090 /* Scan the list for the insertion point that will keep the list in
1091 * order of decreasing priority. */
1092 struct cls_rule *rule;
1093 FOR_EACH_RULE_IN_LIST (rule, head) {
1094 if (new->priority >= rule->priority) {
1096 /* 'new' is the new highest-priority flow in the list. */
1097 hmap_replace(&table->rules,
1098 &rule->hmap_node, &new->hmap_node);
1101 if (new->priority == rule->priority) {
1102 list_replace(&new->list, &rule->list);
1105 list_insert(&rule->list, &new->list);
1111 /* Insert 'new' at the end of the list. */
1112 list_push_back(&head->list, &new->list);
1117 static struct cls_rule *
1118 next_rule_in_list__(struct cls_rule *rule)
1120 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1124 static struct cls_rule *
1125 next_rule_in_list(struct cls_rule *rule)
1127 struct cls_rule *next = next_rule_in_list__(rule);
1128 return next->priority < rule->priority ? next : NULL;
1132 ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b,
1133 const struct in6_addr *mask)
1138 for (i=0; i<4; i++) {
1139 if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) {
1144 for (i=0; i<16; i++) {
1145 if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) {
1156 flow_equal_except(const struct flow *a, const struct flow *b,
1157 const struct flow_wildcards *wildcards)
1159 const flow_wildcards_t wc = wildcards->wildcards;
1162 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 3);
1164 for (i = 0; i < FLOW_N_REGS; i++) {
1165 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
1170 return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask)
1171 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
1172 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
1173 && (wc & FWW_IN_PORT || a->in_port == b->in_port)
1174 && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
1175 && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
1176 && (wc & FWW_TP_SRC || a->tp_src == b->tp_src)
1177 && (wc & FWW_TP_DST || a->tp_dst == b->tp_dst)
1178 && (wc & FWW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
1180 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
1181 && a->dl_dst[1] == b->dl_dst[1]
1182 && a->dl_dst[2] == b->dl_dst[2]
1183 && a->dl_dst[3] == b->dl_dst[3]
1184 && a->dl_dst[4] == b->dl_dst[4]
1185 && a->dl_dst[5] == b->dl_dst[5]))
1186 && (wc & FWW_ETH_MCAST
1187 || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
1188 && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
1189 && !((a->tos_frag ^ b->tos_frag) & wildcards->tos_frag_mask)
1190 && (wc & FWW_ARP_SHA || eth_addr_equals(a->arp_sha, b->arp_sha))
1191 && (wc & FWW_ARP_THA || eth_addr_equals(a->arp_tha, b->arp_tha))
1192 && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src,
1193 &wildcards->ipv6_src_mask)
1194 && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst,
1195 &wildcards->ipv6_dst_mask)
1196 && (wc & FWW_ND_TARGET
1197 || ipv6_addr_equals(&a->nd_target, &b->nd_target)));