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;
163 /* Modifies 'value_src' so that the Ethernet address must match
164 * 'value_dst' exactly. 'mask_dst' is set to all 1s */
166 cls_rule_set_eth(const uint8_t value_src[ETH_ADDR_LEN],
167 uint8_t value_dst[ETH_ADDR_LEN],
168 uint8_t mask_dst[ETH_ADDR_LEN])
170 memcpy(value_dst, value_src, ETH_ADDR_LEN);
171 memset(mask_dst, 0xff, ETH_ADDR_LEN);
174 /* Modifies 'value_src' so that the Ethernet address must match
175 * 'value_src' after each byte is ANDed with the appropriate byte in
176 * 'mask_src'. 'mask_dst' is set to 'mask_src' */
178 cls_rule_set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
179 const uint8_t mask_src[ETH_ADDR_LEN],
180 uint8_t value_dst[ETH_ADDR_LEN],
181 uint8_t mask_dst[ETH_ADDR_LEN])
185 for (i = 0; i < ETH_ADDR_LEN; i++) {
186 value_dst[i] = value_src[i] & mask_src[i];
187 mask_dst[i] = mask_src[i];
191 /* Modifies 'rule' so that the source Ethernet address
192 * must match 'dl_src' exactly. */
194 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
196 cls_rule_set_eth(dl_src, rule->flow.dl_src, rule->wc.dl_src_mask);
199 /* Modifies 'rule' so that the source Ethernet address
200 * must match 'dl_src' after each byte is ANDed with
201 * the appropriate byte in 'mask'. */
203 cls_rule_set_dl_src_masked(struct cls_rule *rule,
204 const uint8_t dl_src[ETH_ADDR_LEN],
205 const uint8_t mask[ETH_ADDR_LEN])
207 cls_rule_set_eth_masked(dl_src, mask,
208 rule->flow.dl_src, rule->wc.dl_src_mask);
211 /* Modifies 'rule' so that the destination Ethernet address
212 * must match 'dl_dst' exactly. */
214 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
216 cls_rule_set_eth(dl_dst, rule->flow.dl_dst, rule->wc.dl_dst_mask);
219 /* Modifies 'rule' so that the destination Ethernet address
220 * must match 'dl_src' after each byte is ANDed with
221 * the appropriate byte in 'mask'. */
223 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
224 const uint8_t dl_dst[ETH_ADDR_LEN],
225 const uint8_t mask[ETH_ADDR_LEN])
227 cls_rule_set_eth_masked(dl_dst, mask,
228 rule->flow.dl_dst, rule->wc.dl_dst_mask);
232 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
234 cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
238 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
240 rule->flow.vlan_tci = tci & mask;
241 rule->wc.vlan_tci_mask = mask;
244 /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already
245 * wildcarded, then 'rule' will match a packet regardless of whether it has an
246 * 802.1Q header or not. */
248 cls_rule_set_any_vid(struct cls_rule *rule)
250 if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
251 rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
252 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
254 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
258 /* Modifies 'rule' depending on 'dl_vlan':
260 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
261 * without an 802.1Q header.
263 * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
264 * VID equals the low 12 bits of 'dl_vlan'.
267 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
269 flow_set_vlan_vid(&rule->flow, dl_vlan);
270 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
271 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
273 rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
277 /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
278 * wildcarded, then 'rule' will match a packet regardless of whether it has an
279 * 802.1Q header or not. */
281 cls_rule_set_any_pcp(struct cls_rule *rule)
283 if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
284 rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
285 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
287 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
291 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
292 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
294 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
296 flow_set_vlan_pcp(&rule->flow, dl_vlan_pcp);
297 rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
301 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
303 cls_rule_set_tp_src_masked(rule, tp_src, htons(UINT16_MAX));
307 cls_rule_set_tp_src_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
309 rule->flow.tp_src = port & mask;
310 rule->wc.tp_src_mask = mask;
314 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
316 cls_rule_set_tp_dst_masked(rule, tp_dst, htons(UINT16_MAX));
320 cls_rule_set_tp_dst_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
322 rule->flow.tp_dst = port & mask;
323 rule->wc.tp_dst_mask = mask;
327 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
329 rule->wc.wildcards &= ~FWW_NW_PROTO;
330 rule->flow.nw_proto = nw_proto;
334 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
336 rule->flow.nw_src = nw_src;
337 rule->wc.nw_src_mask = htonl(UINT32_MAX);
341 cls_rule_set_nw_src_masked(struct cls_rule *rule,
342 ovs_be32 nw_src, ovs_be32 mask)
344 rule->flow.nw_src = nw_src & mask;
345 rule->wc.nw_src_mask = mask;
349 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
351 rule->flow.nw_dst = nw_dst;
352 rule->wc.nw_dst_mask = htonl(UINT32_MAX);
356 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
358 rule->flow.nw_dst = ip & mask;
359 rule->wc.nw_dst_mask = mask;
363 cls_rule_set_nw_dscp(struct cls_rule *rule, uint8_t nw_dscp)
365 rule->wc.wildcards &= ~FWW_NW_DSCP;
366 rule->flow.nw_tos &= ~IP_DSCP_MASK;
367 rule->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
371 cls_rule_set_nw_ecn(struct cls_rule *rule, uint8_t nw_ecn)
373 rule->wc.wildcards &= ~FWW_NW_ECN;
374 rule->flow.nw_tos &= ~IP_ECN_MASK;
375 rule->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
379 cls_rule_set_nw_ttl(struct cls_rule *rule, uint8_t nw_ttl)
381 rule->wc.wildcards &= ~FWW_NW_TTL;
382 rule->flow.nw_ttl = nw_ttl;
386 cls_rule_set_nw_frag(struct cls_rule *rule, uint8_t nw_frag)
388 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
389 rule->flow.nw_frag = nw_frag;
393 cls_rule_set_nw_frag_masked(struct cls_rule *rule,
394 uint8_t nw_frag, uint8_t mask)
396 rule->flow.nw_frag = nw_frag & mask;
397 rule->wc.nw_frag_mask = mask;
401 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
403 cls_rule_set_tp_src(rule, htons(icmp_type));
407 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
409 cls_rule_set_tp_dst(rule, htons(icmp_code));
413 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
415 cls_rule_set_eth(sha, rule->flow.arp_sha, rule->wc.arp_sha_mask);
419 cls_rule_set_arp_sha_masked(struct cls_rule *rule,
420 const uint8_t arp_sha[ETH_ADDR_LEN],
421 const uint8_t mask[ETH_ADDR_LEN])
423 cls_rule_set_eth_masked(arp_sha, mask,
424 rule->flow.arp_sha, rule->wc.arp_sha_mask);
428 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
430 cls_rule_set_eth(tha, rule->flow.arp_tha, rule->wc.arp_tha_mask);
434 cls_rule_set_arp_tha_masked(struct cls_rule *rule,
435 const uint8_t arp_tha[ETH_ADDR_LEN],
436 const uint8_t mask[ETH_ADDR_LEN])
438 cls_rule_set_eth_masked(arp_tha, mask,
439 rule->flow.arp_tha, rule->wc.arp_tha_mask);
443 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
445 rule->flow.ipv6_src = *src;
446 rule->wc.ipv6_src_mask = in6addr_exact;
450 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
451 const struct in6_addr *mask)
453 rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
454 rule->wc.ipv6_src_mask = *mask;
458 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
460 rule->flow.ipv6_dst = *dst;
461 rule->wc.ipv6_dst_mask = in6addr_exact;
465 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
466 const struct in6_addr *mask)
468 rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
469 rule->wc.ipv6_dst_mask = *mask;
473 cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label)
475 cls_rule_set_ipv6_label_masked(rule, ipv6_label, htonl(UINT32_MAX));
479 cls_rule_set_ipv6_label_masked(struct cls_rule *rule, ovs_be32 ipv6_label,
482 rule->flow.ipv6_label = ipv6_label & mask;
483 rule->wc.ipv6_label_mask = mask;
487 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
489 rule->flow.nd_target = *target;
490 rule->wc.nd_target_mask = in6addr_exact;
494 cls_rule_set_nd_target_masked(struct cls_rule *rule,
495 const struct in6_addr *target,
496 const struct in6_addr *mask)
498 rule->flow.nd_target = ipv6_addr_bitand(target, mask);
499 rule->wc.nd_target_mask = *mask;
502 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
503 * fields, and have the same values for fixed fields, otherwise false. */
505 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
507 return (a->priority == b->priority
508 && flow_wildcards_equal(&a->wc, &b->wc)
509 && flow_equal(&a->flow, &b->flow));
512 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
513 * starting from 'basis'. */
515 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
517 uint32_t h0 = flow_hash(&rule->flow, basis);
518 uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
519 return hash_int(rule->priority, h1);
523 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
524 const uint8_t mask[6])
526 if (!eth_addr_is_zero(mask)) {
527 ds_put_format(s, "%s=", name);
528 eth_format_masked(eth, mask, s);
534 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
538 ds_put_format(s, "%s=", name);
539 ip_format_masked(ip, netmask, s);
545 format_ipv6_netmask(struct ds *s, const char *name,
546 const struct in6_addr *addr,
547 const struct in6_addr *netmask)
549 if (!ipv6_mask_is_any(netmask)) {
550 ds_put_format(s, "%s=", name);
551 print_ipv6_masked(s, addr, netmask);
558 format_be16_masked(struct ds *s, const char *name,
559 ovs_be16 value, ovs_be16 mask)
561 if (mask != htons(0)) {
562 ds_put_format(s, "%s=", name);
563 if (mask == htons(UINT16_MAX)) {
564 ds_put_format(s, "%"PRIu16, ntohs(value));
566 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
567 ntohs(value), ntohs(mask));
574 cls_rule_format(const struct cls_rule *rule, struct ds *s)
576 const struct flow_wildcards *wc = &rule->wc;
577 size_t start_len = s->length;
578 flow_wildcards_t w = wc->wildcards;
579 const struct flow *f = &rule->flow;
580 bool skip_type = false;
581 bool skip_proto = false;
585 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
587 if (rule->priority != OFP_DEFAULT_PRIORITY) {
588 ds_put_format(s, "priority=%d,", rule->priority);
591 if (!(w & FWW_DL_TYPE)) {
593 if (f->dl_type == htons(ETH_TYPE_IP)) {
594 if (!(w & FWW_NW_PROTO)) {
596 if (f->nw_proto == IPPROTO_ICMP) {
597 ds_put_cstr(s, "icmp,");
598 } else if (f->nw_proto == IPPROTO_TCP) {
599 ds_put_cstr(s, "tcp,");
600 } else if (f->nw_proto == IPPROTO_UDP) {
601 ds_put_cstr(s, "udp,");
603 ds_put_cstr(s, "ip,");
607 ds_put_cstr(s, "ip,");
609 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
610 if (!(w & FWW_NW_PROTO)) {
612 if (f->nw_proto == IPPROTO_ICMPV6) {
613 ds_put_cstr(s, "icmp6,");
614 } else if (f->nw_proto == IPPROTO_TCP) {
615 ds_put_cstr(s, "tcp6,");
616 } else if (f->nw_proto == IPPROTO_UDP) {
617 ds_put_cstr(s, "udp6,");
619 ds_put_cstr(s, "ipv6,");
623 ds_put_cstr(s, "ipv6,");
625 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
626 ds_put_cstr(s, "arp,");
631 for (i = 0; i < FLOW_N_REGS; i++) {
632 switch (wc->reg_masks[i]) {
636 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
639 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
640 i, f->regs[i], wc->reg_masks[i]);
644 switch (wc->tun_id_mask) {
647 case CONSTANT_HTONLL(UINT64_MAX):
648 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
651 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
652 ntohll(f->tun_id), ntohll(wc->tun_id_mask));
655 switch (wc->metadata_mask) {
658 case CONSTANT_HTONLL(UINT64_MAX):
659 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
662 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
663 ntohll(f->metadata), ntohll(wc->metadata_mask));
666 if (!(w & FWW_IN_PORT)) {
667 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
669 if (wc->vlan_tci_mask) {
670 ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
671 ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
672 ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
674 if (cfi && f->vlan_tci & htons(VLAN_CFI)
675 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
676 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
677 && (vid_mask || pcp_mask)) {
679 ds_put_format(s, "dl_vlan=%"PRIu16",",
680 vlan_tci_to_vid(f->vlan_tci));
683 ds_put_format(s, "dl_vlan_pcp=%d,",
684 vlan_tci_to_pcp(f->vlan_tci));
686 } else if (wc->vlan_tci_mask == htons(0xffff)) {
687 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
689 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
690 ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
693 format_eth_masked(s, "dl_src", f->dl_src, wc->dl_src_mask);
694 format_eth_masked(s, "dl_dst", f->dl_dst, wc->dl_dst_mask);
695 if (!skip_type && !(w & FWW_DL_TYPE)) {
696 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
698 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
699 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask);
700 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
701 if (wc->ipv6_label_mask) {
702 if (wc->ipv6_label_mask == htonl(UINT32_MAX)) {
703 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
704 ntohl(f->ipv6_label));
706 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
707 ntohl(f->ipv6_label),
708 ntohl(wc->ipv6_label_mask));
712 format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
713 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
715 if (!skip_proto && !(w & FWW_NW_PROTO)) {
716 if (f->dl_type == htons(ETH_TYPE_ARP)) {
717 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
719 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
722 if (f->dl_type == htons(ETH_TYPE_ARP)) {
723 format_eth_masked(s, "arp_sha", f->arp_sha, wc->arp_sha_mask);
724 format_eth_masked(s, "arp_tha", f->arp_tha, wc->arp_tha_mask);
726 if (!(w & FWW_NW_DSCP)) {
727 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
729 if (!(w & FWW_NW_ECN)) {
730 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
732 if (!(w & FWW_NW_TTL)) {
733 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
735 switch (wc->nw_frag_mask) {
736 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
737 ds_put_format(s, "nw_frag=%s,",
738 f->nw_frag & FLOW_NW_FRAG_ANY
739 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
740 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
743 case FLOW_NW_FRAG_ANY:
744 ds_put_format(s, "nw_frag=%s,",
745 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
748 case FLOW_NW_FRAG_LATER:
749 ds_put_format(s, "nw_frag=%s,",
750 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
753 if (f->nw_proto == IPPROTO_ICMP) {
754 format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
755 format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
756 } else if (f->nw_proto == IPPROTO_ICMPV6) {
757 format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
758 format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
759 format_ipv6_netmask(s, "nd_target", &f->nd_target,
760 &wc->nd_target_mask);
761 format_eth_masked(s, "nd_sll", f->arp_sha, wc->arp_sha_mask);
762 format_eth_masked(s, "nd_tll", f->arp_tha, wc->arp_tha_mask);
764 format_be16_masked(s, "tp_src", f->tp_src, wc->tp_src_mask);
765 format_be16_masked(s, "tp_dst", f->tp_dst, wc->tp_dst_mask);
768 if (s->length > start_len && ds_last(s) == ',') {
773 /* Converts 'rule' to a string and returns the string. The caller must free
774 * the string (with free()). */
776 cls_rule_to_string(const struct cls_rule *rule)
778 struct ds s = DS_EMPTY_INITIALIZER;
779 cls_rule_format(rule, &s);
780 return ds_steal_cstr(&s);
784 cls_rule_print(const struct cls_rule *rule)
786 char *s = cls_rule_to_string(rule);
791 /* Initializes 'cls' as a classifier that initially contains no classification
794 classifier_init(struct classifier *cls)
797 hmap_init(&cls->tables);
800 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
801 * caller's responsibility. */
803 classifier_destroy(struct classifier *cls)
806 struct cls_table *table, *next_table;
808 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
809 hmap_destroy(&table->rules);
810 hmap_remove(&cls->tables, &table->hmap_node);
813 hmap_destroy(&cls->tables);
817 /* Returns true if 'cls' contains no classification rules, false otherwise. */
819 classifier_is_empty(const struct classifier *cls)
821 return cls->n_rules == 0;
824 /* Returns the number of rules in 'classifier'. */
826 classifier_count(const struct classifier *cls)
831 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
832 * must not modify or free it.
834 * If 'cls' already contains an identical rule (including wildcards, values of
835 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
836 * rule that was replaced. The caller takes ownership of the returned rule and
837 * is thus responsible for freeing it, etc., as necessary.
839 * Returns NULL if 'cls' does not contain a rule with an identical key, after
840 * inserting the new rule. In this case, no rules are displaced by the new
841 * rule, even rules that cannot have any effect because the new rule matches a
842 * superset of their flows and has higher priority. */
844 classifier_replace(struct classifier *cls, struct cls_rule *rule)
846 struct cls_rule *old_rule;
847 struct cls_table *table;
849 table = find_table(cls, &rule->wc);
851 table = insert_table(cls, &rule->wc);
854 old_rule = insert_rule(table, rule);
856 table->n_table_rules++;
862 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
863 * must not modify or free it.
865 * 'cls' must not contain an identical rule (including wildcards, values of
866 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
869 classifier_insert(struct classifier *cls, struct cls_rule *rule)
871 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
872 assert(!displaced_rule);
875 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
876 * 'rule', if this is desirable. */
878 classifier_remove(struct classifier *cls, struct cls_rule *rule)
880 struct cls_rule *head;
881 struct cls_table *table;
883 table = find_table(cls, &rule->wc);
884 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
886 list_remove(&rule->list);
887 } else if (list_is_empty(&rule->list)) {
888 hmap_remove(&table->rules, &rule->hmap_node);
890 struct cls_rule *next = CONTAINER_OF(rule->list.next,
891 struct cls_rule, list);
893 list_remove(&rule->list);
894 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
897 if (--table->n_table_rules == 0) {
898 destroy_table(cls, table);
904 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
905 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
906 * of equal priority match 'flow', returns one arbitrarily. */
908 classifier_lookup(const struct classifier *cls, const struct flow *flow)
910 struct cls_table *table;
911 struct cls_rule *best;
914 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
915 struct cls_rule *rule = find_match(table, flow);
916 if (rule && (!best || rule->priority > best->priority)) {
923 /* Finds and returns a rule in 'cls' with exactly the same priority and
924 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
925 * contain an exact match. */
927 classifier_find_rule_exactly(const struct classifier *cls,
928 const struct cls_rule *target)
930 struct cls_rule *head, *rule;
931 struct cls_table *table;
933 table = find_table(cls, &target->wc);
938 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
939 FOR_EACH_RULE_IN_LIST (rule, head) {
940 if (target->priority >= rule->priority) {
941 return target->priority == rule->priority ? rule : NULL;
947 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
948 * considered to overlap if both rules have the same priority and a packet
949 * could match both. */
951 classifier_rule_overlaps(const struct classifier *cls,
952 const struct cls_rule *target)
954 struct cls_table *table;
956 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
957 struct flow_wildcards wc;
958 struct cls_rule *head;
960 flow_wildcards_combine(&wc, &target->wc, &table->wc);
961 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
962 struct cls_rule *rule;
964 FOR_EACH_RULE_IN_LIST (rule, head) {
965 if (rule->priority == target->priority
966 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
976 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
977 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
978 * function returns true if, for every field:
980 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
983 * - 'criteria' wildcards the field,
985 * Conversely, 'rule' does not match 'criteria' and this function returns false
986 * if, for at least one field:
988 * - 'criteria' and 'rule' specify different values for the field, or
990 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
992 * Equivalently, the truth table for whether a field matches is:
997 * r +---------+---------+
998 * i wild | yes | yes |
1000 * e +---------+---------+
1001 * r exact | no |if values|
1003 * a +---------+---------+
1005 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1006 * commands and by OpenFlow 1.0 aggregate and flow stats.
1008 * Ignores rule->priority and criteria->priority. */
1010 cls_rule_is_loose_match(const struct cls_rule *rule,
1011 const struct cls_rule *criteria)
1013 return (!flow_wildcards_has_extra(&rule->wc, &criteria->wc)
1014 && flow_equal_except(&rule->flow, &criteria->flow, &criteria->wc));
1020 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
1023 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
1026 static struct cls_rule *
1027 search_table(const struct cls_table *table, const struct cls_rule *target)
1029 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
1030 struct cls_rule *rule;
1032 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1033 if (rule_matches(rule, target)) {
1041 /* Initializes 'cursor' for iterating through rules in 'cls':
1043 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1045 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1046 * such that cls_rule_is_loose_match(rule, target) returns true.
1048 * Ignores target->priority. */
1050 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
1051 const struct cls_rule *target)
1054 cursor->target = target;
1057 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
1058 * pointer if there are no matches. */
1060 cls_cursor_first(struct cls_cursor *cursor)
1062 struct cls_table *table;
1064 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
1065 struct cls_rule *rule = search_table(table, cursor->target);
1067 cursor->table = table;
1075 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1076 * pointer if there are no more matches. */
1078 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
1080 const struct cls_table *table;
1081 struct cls_rule *next;
1083 next = next_rule_in_list__(rule);
1084 if (next->priority < rule->priority) {
1088 /* 'next' is the head of the list, that is, the rule that is included in
1089 * the table's hmap. (This is important when the classifier contains rules
1090 * that differ only in priority.) */
1092 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
1093 if (rule_matches(rule, cursor->target)) {
1098 table = cursor->table;
1099 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
1100 rule = search_table(table, cursor->target);
1102 cursor->table = table;
1110 static struct cls_table *
1111 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
1113 struct cls_table *table;
1115 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
1117 if (flow_wildcards_equal(wc, &table->wc)) {
1124 static struct cls_table *
1125 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
1127 struct cls_table *table;
1129 table = xzalloc(sizeof *table);
1130 hmap_init(&table->rules);
1132 table->is_catchall = flow_wildcards_is_catchall(&table->wc);
1133 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1139 destroy_table(struct classifier *cls, struct cls_table *table)
1141 hmap_remove(&cls->tables, &table->hmap_node);
1142 hmap_destroy(&table->rules);
1146 static struct cls_rule *
1147 find_match(const struct cls_table *table, const struct flow *flow)
1149 struct cls_rule *rule;
1151 if (table->is_catchall) {
1152 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1159 flow_zero_wildcards(&f, &table->wc);
1160 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1162 if (flow_equal(&f, &rule->flow)) {
1171 static struct cls_rule *
1172 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1174 struct cls_rule *head;
1176 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1177 if (flow_equal(&head->flow, flow)) {
1184 static struct cls_rule *
1185 insert_rule(struct cls_table *table, struct cls_rule *new)
1187 struct cls_rule *head;
1189 new->hmap_node.hash = flow_hash(&new->flow, 0);
1191 head = find_equal(table, &new->flow, new->hmap_node.hash);
1193 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1194 list_init(&new->list);
1197 /* Scan the list for the insertion point that will keep the list in
1198 * order of decreasing priority. */
1199 struct cls_rule *rule;
1200 FOR_EACH_RULE_IN_LIST (rule, head) {
1201 if (new->priority >= rule->priority) {
1203 /* 'new' is the new highest-priority flow in the list. */
1204 hmap_replace(&table->rules,
1205 &rule->hmap_node, &new->hmap_node);
1208 if (new->priority == rule->priority) {
1209 list_replace(&new->list, &rule->list);
1212 list_insert(&rule->list, &new->list);
1218 /* Insert 'new' at the end of the list. */
1219 list_push_back(&head->list, &new->list);
1224 static struct cls_rule *
1225 next_rule_in_list__(struct cls_rule *rule)
1227 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1231 static struct cls_rule *
1232 next_rule_in_list(struct cls_rule *rule)
1234 struct cls_rule *next = next_rule_in_list__(rule);
1235 return next->priority < rule->priority ? next : NULL;
1239 ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b,
1240 const struct in6_addr *mask)
1245 for (i=0; i<4; i++) {
1246 if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) {
1251 for (i=0; i<16; i++) {
1252 if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) {
1263 flow_equal_except(const struct flow *a, const struct flow *b,
1264 const struct flow_wildcards *wildcards)
1266 const flow_wildcards_t wc = wildcards->wildcards;
1269 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
1271 for (i = 0; i < FLOW_N_REGS; i++) {
1272 if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
1277 return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask)
1278 && !((a->metadata ^ b->metadata) & wildcards->metadata_mask)
1279 && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
1280 && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
1281 && (wc & FWW_IN_PORT || a->in_port == b->in_port)
1282 && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
1283 && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
1284 && !((a->tp_src ^ b->tp_src) & wildcards->tp_src_mask)
1285 && !((a->tp_dst ^ b->tp_dst) & wildcards->tp_dst_mask)
1286 && eth_addr_equal_except(a->dl_src, b->dl_src,
1287 wildcards->dl_src_mask)
1288 && eth_addr_equal_except(a->dl_dst, b->dl_dst,
1289 wildcards->dl_dst_mask)
1290 && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
1291 && (wc & FWW_NW_TTL || a->nw_ttl == b->nw_ttl)
1292 && (wc & FWW_NW_DSCP || !((a->nw_tos ^ b->nw_tos) & IP_DSCP_MASK))
1293 && (wc & FWW_NW_ECN || !((a->nw_tos ^ b->nw_tos) & IP_ECN_MASK))
1294 && !((a->nw_frag ^ b->nw_frag) & wildcards->nw_frag_mask)
1295 && eth_addr_equal_except(a->arp_sha, b->arp_sha,
1296 wildcards->arp_sha_mask)
1297 && eth_addr_equal_except(a->arp_tha, b->arp_tha,
1298 wildcards->arp_tha_mask)
1299 && !((a->ipv6_label ^ b->ipv6_label) & wildcards->ipv6_label_mask)
1300 && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src,
1301 &wildcards->ipv6_src_mask)
1302 && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst,
1303 &wildcards->ipv6_dst_mask)
1304 && ipv6_equal_except(&a->nd_target, &b->nd_target,
1305 &wildcards->nd_target_mask));