2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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 void destroy_table(struct classifier *, struct cls_table *);
37 static struct cls_rule *find_match(const struct cls_table *,
39 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
41 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
43 static bool flow_equal_except(const struct flow *, const struct flow *,
44 const struct flow_wildcards *);
46 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
47 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
48 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
49 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
50 for ((RULE) = (HEAD); \
51 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
54 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
55 static struct cls_rule *next_rule_in_list(struct cls_rule *);
57 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
58 * 'wildcards' and 'priority'. */
60 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
61 unsigned int priority, struct cls_rule *rule)
64 rule->wc = *wildcards;
65 rule->priority = priority;
66 cls_rule_zero_wildcarded_fields(rule);
69 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
70 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
71 * priority, so 'priority' should be at least 65535.) */
73 cls_rule_init_exact(const struct flow *flow,
74 unsigned int priority, struct cls_rule *rule)
77 rule->flow.skb_priority = 0;
78 flow_wildcards_init_exact(&rule->wc);
79 rule->priority = priority;
82 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
83 * priority 'priority'. */
85 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
87 memset(&rule->flow, 0, sizeof rule->flow);
88 flow_wildcards_init_catchall(&rule->wc);
89 rule->priority = priority;
92 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
93 * field in 'flow' to all-0-bits. It is important to maintain this invariant
94 * in a clr_rule that might be inserted into a classifier.
96 * It is never necessary to call this function directly for a cls_rule that is
97 * initialized or modified only by cls_rule_*() functions. It is useful to
98 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
101 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
103 flow_zero_wildcards(&rule->flow, &rule->wc);
107 cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value)
109 cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX);
113 cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx,
114 uint32_t value, uint32_t mask)
116 assert(reg_idx < FLOW_N_REGS);
117 flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask);
118 rule->flow.regs[reg_idx] = value & mask;
122 cls_rule_set_metadata(struct cls_rule *rule, ovs_be64 metadata)
124 cls_rule_set_metadata_masked(rule, metadata, htonll(UINT64_MAX));
128 cls_rule_set_metadata_masked(struct cls_rule *rule, ovs_be64 metadata,
131 rule->wc.metadata_mask = mask;
132 rule->flow.metadata = metadata & mask;
136 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
138 cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX));
142 cls_rule_set_tun_id_masked(struct cls_rule *rule,
143 ovs_be64 tun_id, ovs_be64 mask)
145 rule->wc.tun_id_mask = mask;
146 rule->flow.tun_id = tun_id & mask;
150 cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port)
152 rule->wc.wildcards &= ~FWW_IN_PORT;
153 rule->flow.in_port = ofp_port;
157 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
159 rule->wc.wildcards &= ~FWW_DL_TYPE;
160 rule->flow.dl_type = dl_type;
164 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
166 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
167 memset(rule->wc.dl_src_mask, 0xff, ETH_ADDR_LEN);
170 /* Modifies 'rule' so that the Ethernet address must match 'dl_src' after each
171 * byte is ANDed with the appropriate byte in 'mask'. */
173 cls_rule_set_dl_src_masked(struct cls_rule *rule,
174 const uint8_t dl_src[ETH_ADDR_LEN],
175 const uint8_t mask[ETH_ADDR_LEN])
179 for (i = 0; i < ETH_ADDR_LEN; i++) {
180 rule->flow.dl_src[i] = dl_src[i] & mask[i];
181 rule->wc.dl_src_mask[i] = mask[i];
185 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' exactly. */
187 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
189 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
190 memset(rule->wc.dl_dst_mask, 0xff, ETH_ADDR_LEN);
193 /* Modifies 'rule' so that the Ethernet address must match 'dl_dst' after each
194 * byte is ANDed with the appropriate byte in 'mask'.
196 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
197 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
199 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
200 const uint8_t dl_dst[ETH_ADDR_LEN],
201 const uint8_t mask[ETH_ADDR_LEN])
205 for (i = 0; i < ETH_ADDR_LEN; i++) {
206 rule->flow.dl_dst[i] = dl_dst[i] & mask[i];
207 rule->wc.dl_dst_mask[i] = mask[i];
212 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
214 cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
218 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
220 rule->flow.vlan_tci = tci & mask;
221 rule->wc.vlan_tci_mask = mask;
224 /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already
225 * wildcarded, then 'rule' will match a packet regardless of whether it has an
226 * 802.1Q header or not. */
228 cls_rule_set_any_vid(struct cls_rule *rule)
230 if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
231 rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
232 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
234 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
238 /* Modifies 'rule' depending on 'dl_vlan':
240 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
241 * without an 802.1Q header.
243 * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
244 * VID equals the low 12 bits of 'dl_vlan'.
247 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
249 flow_set_vlan_vid(&rule->flow, dl_vlan);
250 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
251 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
253 rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
257 /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
258 * wildcarded, then 'rule' will match a packet regardless of whether it has an
259 * 802.1Q header or not. */
261 cls_rule_set_any_pcp(struct cls_rule *rule)
263 if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
264 rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
265 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
267 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
271 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
272 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
274 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
276 flow_set_vlan_pcp(&rule->flow, dl_vlan_pcp);
277 rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
281 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
283 cls_rule_set_tp_src_masked(rule, tp_src, htons(UINT16_MAX));
287 cls_rule_set_tp_src_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
289 rule->flow.tp_src = port & mask;
290 rule->wc.tp_src_mask = mask;
294 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
296 cls_rule_set_tp_dst_masked(rule, tp_dst, htons(UINT16_MAX));
300 cls_rule_set_tp_dst_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
302 rule->flow.tp_dst = port & mask;
303 rule->wc.tp_dst_mask = mask;
307 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
309 rule->wc.wildcards &= ~FWW_NW_PROTO;
310 rule->flow.nw_proto = nw_proto;
314 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
316 rule->flow.nw_src = nw_src;
317 rule->wc.nw_src_mask = htonl(UINT32_MAX);
321 cls_rule_set_nw_src_masked(struct cls_rule *rule,
322 ovs_be32 nw_src, ovs_be32 mask)
324 rule->flow.nw_src = nw_src & mask;
325 rule->wc.nw_src_mask = mask;
329 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
331 rule->flow.nw_dst = nw_dst;
332 rule->wc.nw_dst_mask = htonl(UINT32_MAX);
336 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
338 rule->flow.nw_dst = ip & mask;
339 rule->wc.nw_dst_mask = mask;
343 cls_rule_set_nw_dscp(struct cls_rule *rule, uint8_t nw_dscp)
345 rule->wc.wildcards &= ~FWW_NW_DSCP;
346 rule->flow.nw_tos &= ~IP_DSCP_MASK;
347 rule->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
351 cls_rule_set_nw_ecn(struct cls_rule *rule, uint8_t nw_ecn)
353 rule->wc.wildcards &= ~FWW_NW_ECN;
354 rule->flow.nw_tos &= ~IP_ECN_MASK;
355 rule->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
359 cls_rule_set_nw_ttl(struct cls_rule *rule, uint8_t nw_ttl)
361 rule->wc.wildcards &= ~FWW_NW_TTL;
362 rule->flow.nw_ttl = nw_ttl;
366 cls_rule_set_nw_frag(struct cls_rule *rule, uint8_t nw_frag)
368 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
369 rule->flow.nw_frag = nw_frag;
373 cls_rule_set_nw_frag_masked(struct cls_rule *rule,
374 uint8_t nw_frag, uint8_t mask)
376 rule->flow.nw_frag = nw_frag & mask;
377 rule->wc.nw_frag_mask = mask;
381 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
383 cls_rule_set_tp_src(rule, htons(icmp_type));
387 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
389 cls_rule_set_tp_dst(rule, htons(icmp_code));
393 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
395 rule->wc.wildcards &= ~FWW_ARP_SHA;
396 memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN);
400 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
402 rule->wc.wildcards &= ~FWW_ARP_THA;
403 memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN);
407 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
409 rule->flow.ipv6_src = *src;
410 rule->wc.ipv6_src_mask = in6addr_exact;
414 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
415 const struct in6_addr *mask)
417 rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
418 rule->wc.ipv6_src_mask = *mask;
422 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
424 rule->flow.ipv6_dst = *dst;
425 rule->wc.ipv6_dst_mask = in6addr_exact;
429 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
430 const struct in6_addr *mask)
432 rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
433 rule->wc.ipv6_dst_mask = *mask;
437 cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label)
439 rule->wc.wildcards &= ~FWW_IPV6_LABEL;
440 rule->flow.ipv6_label = ipv6_label;
444 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
446 rule->flow.nd_target = *target;
447 rule->wc.nd_target_mask = in6addr_exact;
451 cls_rule_set_nd_target_masked(struct cls_rule *rule,
452 const struct in6_addr *target,
453 const struct in6_addr *mask)
455 rule->flow.nd_target = ipv6_addr_bitand(target, mask);
456 rule->wc.nd_target_mask = *mask;
459 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
460 * fields, and have the same values for fixed fields, otherwise false. */
462 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
464 return (a->priority == b->priority
465 && flow_wildcards_equal(&a->wc, &b->wc)
466 && flow_equal(&a->flow, &b->flow));
469 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
470 * starting from 'basis'. */
472 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
474 uint32_t h0 = flow_hash(&rule->flow, basis);
475 uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
476 return hash_int(rule->priority, h1);
480 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
481 const uint8_t mask[6])
483 if (!eth_addr_is_zero(mask)) {
484 ds_put_format(s, "%s=", name);
485 eth_format_masked(eth, mask, s);
491 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
495 ds_put_format(s, "%s=", name);
496 ip_format_masked(ip, netmask, s);
502 format_ipv6_netmask(struct ds *s, const char *name,
503 const struct in6_addr *addr,
504 const struct in6_addr *netmask)
506 if (!ipv6_mask_is_any(netmask)) {
507 ds_put_format(s, "%s=", name);
508 print_ipv6_masked(s, addr, netmask);
515 format_be16_masked(struct ds *s, const char *name,
516 ovs_be16 value, ovs_be16 mask)
518 if (mask != htons(0)) {
519 ds_put_format(s, "%s=", name);
520 if (mask == htons(UINT16_MAX)) {
521 ds_put_format(s, "%"PRIu16, ntohs(value));
523 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
524 ntohs(value), ntohs(mask));
531 cls_rule_format(const struct cls_rule *rule, struct ds *s)
533 const struct flow_wildcards *wc = &rule->wc;
534 size_t start_len = s->length;
535 flow_wildcards_t w = wc->wildcards;
536 const struct flow *f = &rule->flow;
537 bool skip_type = false;
538 bool skip_proto = false;
542 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 12);
544 if (rule->priority != OFP_DEFAULT_PRIORITY) {
545 ds_put_format(s, "priority=%d,", rule->priority);
548 if (!(w & FWW_DL_TYPE)) {
550 if (f->dl_type == htons(ETH_TYPE_IP)) {
551 if (!(w & FWW_NW_PROTO)) {
553 if (f->nw_proto == IPPROTO_ICMP) {
554 ds_put_cstr(s, "icmp,");
555 } else if (f->nw_proto == IPPROTO_TCP) {
556 ds_put_cstr(s, "tcp,");
557 } else if (f->nw_proto == IPPROTO_UDP) {
558 ds_put_cstr(s, "udp,");
560 ds_put_cstr(s, "ip,");
564 ds_put_cstr(s, "ip,");
566 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
567 if (!(w & FWW_NW_PROTO)) {
569 if (f->nw_proto == IPPROTO_ICMPV6) {
570 ds_put_cstr(s, "icmp6,");
571 } else if (f->nw_proto == IPPROTO_TCP) {
572 ds_put_cstr(s, "tcp6,");
573 } else if (f->nw_proto == IPPROTO_UDP) {
574 ds_put_cstr(s, "udp6,");
576 ds_put_cstr(s, "ipv6,");
580 ds_put_cstr(s, "ipv6,");
582 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
583 ds_put_cstr(s, "arp,");
588 for (i = 0; i < FLOW_N_REGS; i++) {
589 switch (wc->reg_masks[i]) {
593 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
596 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
597 i, f->regs[i], wc->reg_masks[i]);
601 switch (wc->tun_id_mask) {
604 case CONSTANT_HTONLL(UINT64_MAX):
605 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
608 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
609 ntohll(f->tun_id), ntohll(wc->tun_id_mask));
612 switch (wc->metadata_mask) {
615 case CONSTANT_HTONLL(UINT64_MAX):
616 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
619 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
620 ntohll(f->metadata), ntohll(wc->metadata_mask));
623 if (!(w & FWW_IN_PORT)) {
624 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
626 if (wc->vlan_tci_mask) {
627 ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
628 ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
629 ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
631 if (cfi && f->vlan_tci & htons(VLAN_CFI)
632 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
633 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
634 && (vid_mask || pcp_mask)) {
636 ds_put_format(s, "dl_vlan=%"PRIu16",",
637 vlan_tci_to_vid(f->vlan_tci));
640 ds_put_format(s, "dl_vlan_pcp=%d,",
641 vlan_tci_to_pcp(f->vlan_tci));
643 } else if (wc->vlan_tci_mask == htons(0xffff)) {
644 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
646 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
647 ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
650 format_eth_masked(s, "dl_src", f->dl_src, wc->dl_src_mask);
651 format_eth_masked(s, "dl_dst", f->dl_dst, wc->dl_dst_mask);
652 if (!skip_type && !(w & FWW_DL_TYPE)) {
653 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
655 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
656 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask);
657 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
658 if (!(w & FWW_IPV6_LABEL)) {
659 ds_put_format(s, "ipv6_label=0x%05"PRIx32",", ntohl(f->ipv6_label));
662 format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
663 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
665 if (!skip_proto && !(w & FWW_NW_PROTO)) {
666 if (f->dl_type == htons(ETH_TYPE_ARP)) {
667 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
669 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
672 if (f->dl_type == htons(ETH_TYPE_ARP)) {
673 if (!(w & FWW_ARP_SHA)) {
674 ds_put_format(s, "arp_sha="ETH_ADDR_FMT",",
675 ETH_ADDR_ARGS(f->arp_sha));
677 if (!(w & FWW_ARP_THA)) {
678 ds_put_format(s, "arp_tha="ETH_ADDR_FMT",",
679 ETH_ADDR_ARGS(f->arp_tha));
682 if (!(w & FWW_NW_DSCP)) {
683 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
685 if (!(w & FWW_NW_ECN)) {
686 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
688 if (!(w & FWW_NW_TTL)) {
689 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
691 switch (wc->nw_frag_mask) {
692 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
693 ds_put_format(s, "nw_frag=%s,",
694 f->nw_frag & FLOW_NW_FRAG_ANY
695 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
696 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
699 case FLOW_NW_FRAG_ANY:
700 ds_put_format(s, "nw_frag=%s,",
701 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
704 case FLOW_NW_FRAG_LATER:
705 ds_put_format(s, "nw_frag=%s,",
706 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
709 if (f->nw_proto == IPPROTO_ICMP) {
710 format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
711 format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
712 } else if (f->nw_proto == IPPROTO_ICMPV6) {
713 format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
714 format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
715 format_ipv6_netmask(s, "nd_target", &f->nd_target,
716 &wc->nd_target_mask);
717 if (!(w & FWW_ARP_SHA)) {
718 ds_put_format(s, "nd_sll="ETH_ADDR_FMT",",
719 ETH_ADDR_ARGS(f->arp_sha));
721 if (!(w & FWW_ARP_THA)) {
722 ds_put_format(s, "nd_tll="ETH_ADDR_FMT",",
723 ETH_ADDR_ARGS(f->arp_tha));
726 format_be16_masked(s, "tp_src", f->tp_src, wc->tp_src_mask);
727 format_be16_masked(s, "tp_dst", f->tp_dst, wc->tp_dst_mask);
730 if (s->length > start_len && ds_last(s) == ',') {
735 /* Converts 'rule' to a string and returns the string. The caller must free
736 * the string (with free()). */
738 cls_rule_to_string(const struct cls_rule *rule)
740 struct ds s = DS_EMPTY_INITIALIZER;
741 cls_rule_format(rule, &s);
742 return ds_steal_cstr(&s);
746 cls_rule_print(const struct cls_rule *rule)
748 char *s = cls_rule_to_string(rule);
753 /* Initializes 'cls' as a classifier that initially contains no classification
756 classifier_init(struct classifier *cls)
759 hmap_init(&cls->tables);
762 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
763 * caller's responsibility. */
765 classifier_destroy(struct classifier *cls)
768 struct cls_table *table, *next_table;
770 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
771 hmap_destroy(&table->rules);
772 hmap_remove(&cls->tables, &table->hmap_node);
775 hmap_destroy(&cls->tables);
779 /* Returns true if 'cls' contains no classification rules, false otherwise. */
781 classifier_is_empty(const struct classifier *cls)
783 return cls->n_rules == 0;
786 /* Returns the number of rules in 'classifier'. */
788 classifier_count(const struct classifier *cls)
793 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
794 * must not modify or free it.
796 * If 'cls' already contains an identical rule (including wildcards, values of
797 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
798 * rule that was replaced. The caller takes ownership of the returned rule and
799 * is thus responsible for freeing it, etc., as necessary.
801 * Returns NULL if 'cls' does not contain a rule with an identical key, after
802 * inserting the new rule. In this case, no rules are displaced by the new
803 * rule, even rules that cannot have any effect because the new rule matches a
804 * superset of their flows and has higher priority. */
806 classifier_replace(struct classifier *cls, struct cls_rule *rule)
808 struct cls_rule *old_rule;
809 struct cls_table *table;
811 table = find_table(cls, &rule->wc);
813 table = insert_table(cls, &rule->wc);
816 old_rule = insert_rule(table, rule);
818 table->n_table_rules++;
824 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
825 * must not modify or free it.
827 * 'cls' must not contain an identical rule (including wildcards, values of
828 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
831 classifier_insert(struct classifier *cls, struct cls_rule *rule)
833 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
834 assert(!displaced_rule);
837 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
838 * 'rule', if this is desirable. */
840 classifier_remove(struct classifier *cls, struct cls_rule *rule)
842 struct cls_rule *head;
843 struct cls_table *table;
845 table = find_table(cls, &rule->wc);
846 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
848 list_remove(&rule->list);
849 } else if (list_is_empty(&rule->list)) {
850 hmap_remove(&table->rules, &rule->hmap_node);
852 struct cls_rule *next = CONTAINER_OF(rule->list.next,
853 struct cls_rule, list);
855 list_remove(&rule->list);
856 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
859 if (--table->n_table_rules == 0) {
860 destroy_table(cls, table);
866 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
867 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
868 * of equal priority match 'flow', returns one arbitrarily. */
870 classifier_lookup(const struct classifier *cls, const struct flow *flow)
872 struct cls_table *table;
873 struct cls_rule *best;
876 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
877 struct cls_rule *rule = find_match(table, flow);
878 if (rule && (!best || rule->priority > best->priority)) {
885 /* Finds and returns a rule in 'cls' with exactly the same priority and
886 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
887 * contain an exact match. */
889 classifier_find_rule_exactly(const struct classifier *cls,
890 const struct cls_rule *target)
892 struct cls_rule *head, *rule;
893 struct cls_table *table;
895 table = find_table(cls, &target->wc);
900 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
901 FOR_EACH_RULE_IN_LIST (rule, head) {
902 if (target->priority >= rule->priority) {
903 return target->priority == rule->priority ? rule : NULL;
909 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
910 * considered to overlap if both rules have the same priority and a packet
911 * could match both. */
913 classifier_rule_overlaps(const struct classifier *cls,
914 const struct cls_rule *target)
916 struct cls_table *table;
918 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
919 struct flow_wildcards wc;
920 struct cls_rule *head;
922 flow_wildcards_combine(&wc, &target->wc, &table->wc);
923 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
924 struct cls_rule *rule;
926 FOR_EACH_RULE_IN_LIST (rule, head) {
927 if (rule->priority == target->priority
928 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
938 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
939 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
940 * function returns true if, for every field:
942 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
945 * - 'criteria' wildcards the field,
947 * Conversely, 'rule' does not match 'criteria' and this function returns false
948 * if, for at least one field:
950 * - 'criteria' and 'rule' specify different values for the field, or
952 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
954 * Equivalently, the truth table for whether a field matches is:
959 * r +---------+---------+
960 * i wild | yes | yes |
962 * e +---------+---------+
963 * r exact | no |if values|
965 * a +---------+---------+
967 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
968 * commands and by OpenFlow 1.0 aggregate and flow stats.
970 * Ignores rule->priority and criteria->priority. */
972 cls_rule_is_loose_match(const struct cls_rule *rule,
973 const struct cls_rule *criteria)
975 return (!flow_wildcards_has_extra(&rule->wc, &criteria->wc)
976 && flow_equal_except(&rule->flow, &criteria->flow, &criteria->wc));
982 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
985 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
988 static struct cls_rule *
989 search_table(const struct cls_table *table, const struct cls_rule *target)
991 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
992 struct cls_rule *rule;
994 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
995 if (rule_matches(rule, target)) {
1003 /* Initializes 'cursor' for iterating through rules in 'cls':
1005 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1007 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1008 * such that cls_rule_is_loose_match(rule, target) returns true.
1010 * Ignores target->priority. */
1012 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
1013 const struct cls_rule *target)
1016 cursor->target = target;
1019 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
1020 * pointer if there are no matches. */
1022 cls_cursor_first(struct cls_cursor *cursor)
1024 struct cls_table *table;
1026 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
1027 struct cls_rule *rule = search_table(table, cursor->target);
1029 cursor->table = table;
1037 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1038 * pointer if there are no more matches. */
1040 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
1042 const struct cls_table *table;
1043 struct cls_rule *next;
1045 next = next_rule_in_list__(rule);
1046 if (next->priority < rule->priority) {
1050 /* 'next' is the head of the list, that is, the rule that is included in
1051 * the table's hmap. (This is important when the classifier contains rules
1052 * that differ only in priority.) */
1054 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
1055 if (rule_matches(rule, cursor->target)) {
1060 table = cursor->table;
1061 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
1062 rule = search_table(table, cursor->target);
1064 cursor->table = table;
1072 static struct cls_table *
1073 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
1075 struct cls_table *table;
1077 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
1079 if (flow_wildcards_equal(wc, &table->wc)) {
1086 static struct cls_table *
1087 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
1089 struct cls_table *table;
1091 table = xzalloc(sizeof *table);
1092 hmap_init(&table->rules);
1094 table->is_catchall = flow_wildcards_is_catchall(&table->wc);
1095 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1101 destroy_table(struct classifier *cls, struct cls_table *table)
1103 hmap_remove(&cls->tables, &table->hmap_node);
1104 hmap_destroy(&table->rules);
1108 static struct cls_rule *
1109 find_match(const struct cls_table *table, const struct flow *flow)
1111 struct cls_rule *rule;
1113 if (table->is_catchall) {
1114 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1121 flow_zero_wildcards(&f, &table->wc);
1122 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1124 if (flow_equal(&f, &rule->flow)) {
1133 static struct cls_rule *
1134 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1136 struct cls_rule *head;
1138 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1139 if (flow_equal(&head->flow, flow)) {
1146 static struct cls_rule *
1147 insert_rule(struct cls_table *table, struct cls_rule *new)
1149 struct cls_rule *head;
1151 new->hmap_node.hash = flow_hash(&new->flow, 0);
1153 head = find_equal(table, &new->flow, new->hmap_node.hash);
1155 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1156 list_init(&new->list);
1159 /* Scan the list for the insertion point that will keep the list in
1160 * order of decreasing priority. */
1161 struct cls_rule *rule;
1162 FOR_EACH_RULE_IN_LIST (rule, head) {
1163 if (new->priority >= rule->priority) {
1165 /* 'new' is the new highest-priority flow in the list. */
1166 hmap_replace(&table->rules,
1167 &rule->hmap_node, &new->hmap_node);
1170 if (new->priority == rule->priority) {
1171 list_replace(&new->list, &rule->list);
1174 list_insert(&rule->list, &new->list);
1180 /* Insert 'new' at the end of the list. */
1181 list_push_back(&head->list, &new->list);
1186 static struct cls_rule *
1187 next_rule_in_list__(struct cls_rule *rule)
1189 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1193 static struct cls_rule *
1194 next_rule_in_list(struct cls_rule *rule)
1196 struct cls_rule *next = next_rule_in_list__(rule);
1197 return next->priority < rule->priority ? next : NULL;
1201 ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b,
1202 const struct in6_addr *mask)
1207 for (i=0; i<4; i++) {
1208 if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) {
1213 for (i=0; i<16; i++) {
1214 if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) {
1225 flow_equal_except(const struct flow *a, const struct flow *b,
1226 const struct flow_wildcards *wildcards)
1228 const flow_wildcards_t wc = wildcards->wildcards;
1231 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 12);
1233 for (i = 0; i < FLOW_N_REGS; i++) {
1234 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
1239 return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask)
1240 && !((a->metadata ^ b->metadata) & wildcards->metadata_mask)
1241 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
1242 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
1243 && (wc & FWW_IN_PORT || a->in_port == b->in_port)
1244 && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
1245 && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
1246 && !((a->tp_src ^ b->tp_src) & wildcards->tp_src_mask)
1247 && !((a->tp_dst ^ b->tp_dst) & wildcards->tp_dst_mask)
1248 && eth_addr_equal_except(a->dl_src, b->dl_src,
1249 wildcards->dl_src_mask)
1250 && eth_addr_equal_except(a->dl_dst, b->dl_dst,
1251 wildcards->dl_dst_mask)
1252 && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
1253 && (wc & FWW_NW_TTL || a->nw_ttl == b->nw_ttl)
1254 && (wc & FWW_NW_DSCP || !((a->nw_tos ^ b->nw_tos) & IP_DSCP_MASK))
1255 && (wc & FWW_NW_ECN || !((a->nw_tos ^ b->nw_tos) & IP_ECN_MASK))
1256 && !((a->nw_frag ^ b->nw_frag) & wildcards->nw_frag_mask)
1257 && (wc & FWW_ARP_SHA || eth_addr_equals(a->arp_sha, b->arp_sha))
1258 && (wc & FWW_ARP_THA || eth_addr_equals(a->arp_tha, b->arp_tha))
1259 && (wc & FWW_IPV6_LABEL || a->ipv6_label == b->ipv6_label)
1260 && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src,
1261 &wildcards->ipv6_src_mask)
1262 && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst,
1263 &wildcards->ipv6_dst_mask)
1264 && ipv6_equal_except(&a->nd_target, &b->nd_target,
1265 &wildcards->nd_target_mask));