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 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
44 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
45 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
46 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
47 for ((RULE) = (HEAD); \
48 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
51 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
52 static struct cls_rule *next_rule_in_list(struct cls_rule *);
54 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
55 * 'wildcards' and 'priority'. */
57 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
58 unsigned int priority, struct cls_rule *rule)
61 rule->wc = *wildcards;
62 rule->priority = priority;
63 cls_rule_zero_wildcarded_fields(rule);
66 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
67 * given 'priority'. (For OpenFlow 1.0, exact-match rule are always highest
68 * priority, so 'priority' should be at least 65535.) */
70 cls_rule_init_exact(const struct flow *flow,
71 unsigned int priority, struct cls_rule *rule)
74 rule->flow.skb_priority = 0;
75 flow_wildcards_init_exact(&rule->wc);
76 rule->priority = priority;
79 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
80 * priority 'priority'. */
82 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
84 memset(&rule->flow, 0, sizeof rule->flow);
85 flow_wildcards_init_catchall(&rule->wc);
86 rule->priority = priority;
89 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
90 * field in 'flow' to all-0-bits. It is important to maintain this invariant
91 * in a clr_rule that might be inserted into a classifier.
93 * It is never necessary to call this function directly for a cls_rule that is
94 * initialized or modified only by cls_rule_*() functions. It is useful to
95 * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
98 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
100 flow_zero_wildcards(&rule->flow, &rule->wc);
104 cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value)
106 cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX);
110 cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx,
111 uint32_t value, uint32_t mask)
113 assert(reg_idx < FLOW_N_REGS);
114 flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask);
115 rule->flow.regs[reg_idx] = value & mask;
119 cls_rule_set_metadata(struct cls_rule *rule, ovs_be64 metadata)
121 cls_rule_set_metadata_masked(rule, metadata, htonll(UINT64_MAX));
125 cls_rule_set_metadata_masked(struct cls_rule *rule, ovs_be64 metadata,
128 rule->wc.masks.metadata = mask;
129 rule->flow.metadata = metadata & mask;
133 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
135 cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX));
139 cls_rule_set_tun_id_masked(struct cls_rule *rule,
140 ovs_be64 tun_id, ovs_be64 mask)
142 rule->wc.masks.tun_id = mask;
143 rule->flow.tun_id = tun_id & mask;
147 cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port)
149 rule->wc.masks.in_port = UINT16_MAX;
150 rule->flow.in_port = ofp_port;
154 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
156 rule->wc.masks.dl_type = htons(UINT16_MAX);
157 rule->flow.dl_type = dl_type;
160 /* Modifies 'value_src' so that the Ethernet address must match
161 * 'value_dst' exactly. 'mask_dst' is set to all 1s */
163 cls_rule_set_eth(const uint8_t value_src[ETH_ADDR_LEN],
164 uint8_t value_dst[ETH_ADDR_LEN],
165 uint8_t mask_dst[ETH_ADDR_LEN])
167 memcpy(value_dst, value_src, ETH_ADDR_LEN);
168 memset(mask_dst, 0xff, ETH_ADDR_LEN);
171 /* Modifies 'value_src' so that the Ethernet address must match
172 * 'value_src' after each byte is ANDed with the appropriate byte in
173 * 'mask_src'. 'mask_dst' is set to 'mask_src' */
175 cls_rule_set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
176 const uint8_t mask_src[ETH_ADDR_LEN],
177 uint8_t value_dst[ETH_ADDR_LEN],
178 uint8_t mask_dst[ETH_ADDR_LEN])
182 for (i = 0; i < ETH_ADDR_LEN; i++) {
183 value_dst[i] = value_src[i] & mask_src[i];
184 mask_dst[i] = mask_src[i];
188 /* Modifies 'rule' so that the source Ethernet address
189 * must match 'dl_src' exactly. */
191 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
193 cls_rule_set_eth(dl_src, rule->flow.dl_src, rule->wc.masks.dl_src);
196 /* Modifies 'rule' so that the source Ethernet address
197 * must match 'dl_src' after each byte is ANDed with
198 * the appropriate byte in 'mask'. */
200 cls_rule_set_dl_src_masked(struct cls_rule *rule,
201 const uint8_t dl_src[ETH_ADDR_LEN],
202 const uint8_t mask[ETH_ADDR_LEN])
204 cls_rule_set_eth_masked(dl_src, mask,
205 rule->flow.dl_src, rule->wc.masks.dl_src);
208 /* Modifies 'rule' so that the destination Ethernet address
209 * must match 'dl_dst' exactly. */
211 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
213 cls_rule_set_eth(dl_dst, rule->flow.dl_dst, rule->wc.masks.dl_dst);
216 /* Modifies 'rule' so that the destination Ethernet address
217 * must match 'dl_src' after each byte is ANDed with
218 * the appropriate byte in 'mask'. */
220 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
221 const uint8_t dl_dst[ETH_ADDR_LEN],
222 const uint8_t mask[ETH_ADDR_LEN])
224 cls_rule_set_eth_masked(dl_dst, mask,
225 rule->flow.dl_dst, rule->wc.masks.dl_dst);
229 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
231 cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
235 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
237 rule->flow.vlan_tci = tci & mask;
238 rule->wc.masks.vlan_tci = mask;
241 /* Modifies 'rule' so that the VLAN VID is wildcarded. If the PCP is already
242 * wildcarded, then 'rule' will match a packet regardless of whether it has an
243 * 802.1Q header or not. */
245 cls_rule_set_any_vid(struct cls_rule *rule)
247 if (rule->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
248 rule->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
249 rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
251 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
255 /* Modifies 'rule' depending on 'dl_vlan':
257 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
258 * without an 802.1Q header.
260 * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
261 * VID equals the low 12 bits of 'dl_vlan'.
264 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
266 flow_set_dl_vlan(&rule->flow, dl_vlan);
267 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
268 rule->wc.masks.vlan_tci = htons(UINT16_MAX);
270 rule->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
274 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
275 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
278 cls_rule_set_vlan_vid(struct cls_rule *rule, ovs_be16 vid)
280 cls_rule_set_vlan_vid_masked(rule, vid, htons(VLAN_VID_MASK | VLAN_CFI));
284 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
285 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
286 * plus CFI), with the corresponding 'mask'. */
288 cls_rule_set_vlan_vid_masked(struct cls_rule *rule,
289 ovs_be16 vid, ovs_be16 mask)
291 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
292 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
295 flow_set_vlan_vid(&rule->flow, vid & mask);
296 rule->wc.masks.vlan_tci = mask | (rule->wc.masks.vlan_tci & pcp_mask);
299 /* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
300 * wildcarded, then 'rule' will match a packet regardless of whether it has an
301 * 802.1Q header or not. */
303 cls_rule_set_any_pcp(struct cls_rule *rule)
305 if (rule->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
306 rule->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
307 rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
309 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
313 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
314 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
316 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
318 flow_set_vlan_pcp(&rule->flow, dl_vlan_pcp);
319 rule->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
323 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
325 cls_rule_set_tp_src_masked(rule, tp_src, htons(UINT16_MAX));
329 cls_rule_set_tp_src_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
331 rule->flow.tp_src = port & mask;
332 rule->wc.masks.tp_src = mask;
336 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
338 cls_rule_set_tp_dst_masked(rule, tp_dst, htons(UINT16_MAX));
342 cls_rule_set_tp_dst_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
344 rule->flow.tp_dst = port & mask;
345 rule->wc.masks.tp_dst = mask;
349 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
351 rule->flow.nw_proto = nw_proto;
352 rule->wc.masks.nw_proto = UINT8_MAX;
356 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
358 rule->flow.nw_src = nw_src;
359 rule->wc.masks.nw_src = htonl(UINT32_MAX);
363 cls_rule_set_nw_src_masked(struct cls_rule *rule,
364 ovs_be32 nw_src, ovs_be32 mask)
366 rule->flow.nw_src = nw_src & mask;
367 rule->wc.masks.nw_src = mask;
371 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
373 rule->flow.nw_dst = nw_dst;
374 rule->wc.masks.nw_dst = htonl(UINT32_MAX);
378 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
380 rule->flow.nw_dst = ip & mask;
381 rule->wc.masks.nw_dst = mask;
385 cls_rule_set_nw_dscp(struct cls_rule *rule, uint8_t nw_dscp)
387 rule->wc.masks.nw_tos |= IP_DSCP_MASK;
388 rule->flow.nw_tos &= ~IP_DSCP_MASK;
389 rule->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
393 cls_rule_set_nw_ecn(struct cls_rule *rule, uint8_t nw_ecn)
395 rule->wc.masks.nw_tos |= IP_ECN_MASK;
396 rule->flow.nw_tos &= ~IP_ECN_MASK;
397 rule->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
401 cls_rule_set_nw_ttl(struct cls_rule *rule, uint8_t nw_ttl)
403 rule->wc.masks.nw_ttl = UINT8_MAX;
404 rule->flow.nw_ttl = nw_ttl;
408 cls_rule_set_nw_frag(struct cls_rule *rule, uint8_t nw_frag)
410 rule->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
411 rule->flow.nw_frag = nw_frag;
415 cls_rule_set_nw_frag_masked(struct cls_rule *rule,
416 uint8_t nw_frag, uint8_t mask)
418 rule->flow.nw_frag = nw_frag & mask;
419 rule->wc.masks.nw_frag = mask;
423 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
425 cls_rule_set_tp_src(rule, htons(icmp_type));
429 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
431 cls_rule_set_tp_dst(rule, htons(icmp_code));
435 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
437 cls_rule_set_eth(sha, rule->flow.arp_sha, rule->wc.masks.arp_sha);
441 cls_rule_set_arp_sha_masked(struct cls_rule *rule,
442 const uint8_t arp_sha[ETH_ADDR_LEN],
443 const uint8_t mask[ETH_ADDR_LEN])
445 cls_rule_set_eth_masked(arp_sha, mask,
446 rule->flow.arp_sha, rule->wc.masks.arp_sha);
450 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
452 cls_rule_set_eth(tha, rule->flow.arp_tha, rule->wc.masks.arp_tha);
456 cls_rule_set_arp_tha_masked(struct cls_rule *rule,
457 const uint8_t arp_tha[ETH_ADDR_LEN],
458 const uint8_t mask[ETH_ADDR_LEN])
460 cls_rule_set_eth_masked(arp_tha, mask,
461 rule->flow.arp_tha, rule->wc.masks.arp_tha);
465 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
467 rule->flow.ipv6_src = *src;
468 rule->wc.masks.ipv6_src = in6addr_exact;
472 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
473 const struct in6_addr *mask)
475 rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
476 rule->wc.masks.ipv6_src = *mask;
480 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
482 rule->flow.ipv6_dst = *dst;
483 rule->wc.masks.ipv6_dst = in6addr_exact;
487 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
488 const struct in6_addr *mask)
490 rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
491 rule->wc.masks.ipv6_dst = *mask;
495 cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label)
497 cls_rule_set_ipv6_label_masked(rule, ipv6_label, htonl(UINT32_MAX));
501 cls_rule_set_ipv6_label_masked(struct cls_rule *rule, ovs_be32 ipv6_label,
504 rule->flow.ipv6_label = ipv6_label & mask;
505 rule->wc.masks.ipv6_label = mask;
509 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
511 rule->flow.nd_target = *target;
512 rule->wc.masks.nd_target = in6addr_exact;
516 cls_rule_set_nd_target_masked(struct cls_rule *rule,
517 const struct in6_addr *target,
518 const struct in6_addr *mask)
520 rule->flow.nd_target = ipv6_addr_bitand(target, mask);
521 rule->wc.masks.nd_target = *mask;
524 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
525 * fields, and have the same values for fixed fields, otherwise false. */
527 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
529 return (a->priority == b->priority
530 && flow_wildcards_equal(&a->wc, &b->wc)
531 && flow_equal(&a->flow, &b->flow));
534 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
535 * starting from 'basis'. */
537 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
539 uint32_t h0 = flow_hash(&rule->flow, basis);
540 uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
541 return hash_int(rule->priority, h1);
545 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
546 const uint8_t mask[6])
548 if (!eth_addr_is_zero(mask)) {
549 ds_put_format(s, "%s=", name);
550 eth_format_masked(eth, mask, s);
556 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
560 ds_put_format(s, "%s=", name);
561 ip_format_masked(ip, netmask, s);
567 format_ipv6_netmask(struct ds *s, const char *name,
568 const struct in6_addr *addr,
569 const struct in6_addr *netmask)
571 if (!ipv6_mask_is_any(netmask)) {
572 ds_put_format(s, "%s=", name);
573 print_ipv6_masked(s, addr, netmask);
580 format_be16_masked(struct ds *s, const char *name,
581 ovs_be16 value, ovs_be16 mask)
583 if (mask != htons(0)) {
584 ds_put_format(s, "%s=", name);
585 if (mask == htons(UINT16_MAX)) {
586 ds_put_format(s, "%"PRIu16, ntohs(value));
588 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
589 ntohs(value), ntohs(mask));
596 cls_rule_format(const struct cls_rule *rule, struct ds *s)
598 const struct flow_wildcards *wc = &rule->wc;
599 size_t start_len = s->length;
600 const struct flow *f = &rule->flow;
601 bool skip_type = false;
602 bool skip_proto = false;
606 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
608 if (rule->priority != OFP_DEFAULT_PRIORITY) {
609 ds_put_format(s, "priority=%d,", rule->priority);
612 if (wc->masks.dl_type) {
614 if (f->dl_type == htons(ETH_TYPE_IP)) {
615 if (wc->masks.nw_proto) {
617 if (f->nw_proto == IPPROTO_ICMP) {
618 ds_put_cstr(s, "icmp,");
619 } else if (f->nw_proto == IPPROTO_TCP) {
620 ds_put_cstr(s, "tcp,");
621 } else if (f->nw_proto == IPPROTO_UDP) {
622 ds_put_cstr(s, "udp,");
624 ds_put_cstr(s, "ip,");
628 ds_put_cstr(s, "ip,");
630 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
631 if (wc->masks.nw_proto) {
633 if (f->nw_proto == IPPROTO_ICMPV6) {
634 ds_put_cstr(s, "icmp6,");
635 } else if (f->nw_proto == IPPROTO_TCP) {
636 ds_put_cstr(s, "tcp6,");
637 } else if (f->nw_proto == IPPROTO_UDP) {
638 ds_put_cstr(s, "udp6,");
640 ds_put_cstr(s, "ipv6,");
644 ds_put_cstr(s, "ipv6,");
646 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
647 ds_put_cstr(s, "arp,");
652 for (i = 0; i < FLOW_N_REGS; i++) {
653 switch (wc->masks.regs[i]) {
657 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
660 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
661 i, f->regs[i], wc->masks.regs[i]);
665 switch (wc->masks.tun_id) {
668 case CONSTANT_HTONLL(UINT64_MAX):
669 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
672 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
673 ntohll(f->tun_id), ntohll(wc->masks.tun_id));
676 switch (wc->masks.metadata) {
679 case CONSTANT_HTONLL(UINT64_MAX):
680 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
683 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
684 ntohll(f->metadata), ntohll(wc->masks.metadata));
687 if (wc->masks.in_port) {
688 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
690 if (wc->masks.vlan_tci) {
691 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
692 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
693 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
695 if (cfi && f->vlan_tci & htons(VLAN_CFI)
696 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
697 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
698 && (vid_mask || pcp_mask)) {
700 ds_put_format(s, "dl_vlan=%"PRIu16",",
701 vlan_tci_to_vid(f->vlan_tci));
704 ds_put_format(s, "dl_vlan_pcp=%d,",
705 vlan_tci_to_pcp(f->vlan_tci));
707 } else if (wc->masks.vlan_tci == htons(0xffff)) {
708 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
710 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
711 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
714 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
715 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
716 if (!skip_type && wc->masks.dl_type) {
717 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
719 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
720 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
721 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
722 if (wc->masks.ipv6_label) {
723 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
724 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
725 ntohl(f->ipv6_label));
727 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
728 ntohl(f->ipv6_label),
729 ntohl(wc->masks.ipv6_label));
733 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
734 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
736 if (!skip_proto && wc->masks.nw_proto) {
737 if (f->dl_type == htons(ETH_TYPE_ARP)) {
738 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
740 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
743 if (f->dl_type == htons(ETH_TYPE_ARP)) {
744 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
745 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
747 if (wc->masks.nw_tos & IP_DSCP_MASK) {
748 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
750 if (wc->masks.nw_tos & IP_ECN_MASK) {
751 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
753 if (wc->masks.nw_ttl) {
754 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
756 switch (wc->masks.nw_frag) {
757 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
758 ds_put_format(s, "nw_frag=%s,",
759 f->nw_frag & FLOW_NW_FRAG_ANY
760 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
761 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
764 case FLOW_NW_FRAG_ANY:
765 ds_put_format(s, "nw_frag=%s,",
766 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
769 case FLOW_NW_FRAG_LATER:
770 ds_put_format(s, "nw_frag=%s,",
771 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
774 if (f->nw_proto == IPPROTO_ICMP) {
775 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
776 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
777 } else if (f->nw_proto == IPPROTO_ICMPV6) {
778 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
779 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
780 format_ipv6_netmask(s, "nd_target", &f->nd_target,
781 &wc->masks.nd_target);
782 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
783 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
785 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
786 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
789 if (s->length > start_len && ds_last(s) == ',') {
794 /* Converts 'rule' to a string and returns the string. The caller must free
795 * the string (with free()). */
797 cls_rule_to_string(const struct cls_rule *rule)
799 struct ds s = DS_EMPTY_INITIALIZER;
800 cls_rule_format(rule, &s);
801 return ds_steal_cstr(&s);
805 cls_rule_print(const struct cls_rule *rule)
807 char *s = cls_rule_to_string(rule);
812 /* Initializes 'cls' as a classifier that initially contains no classification
815 classifier_init(struct classifier *cls)
818 hmap_init(&cls->tables);
821 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
822 * caller's responsibility. */
824 classifier_destroy(struct classifier *cls)
827 struct cls_table *table, *next_table;
829 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
830 hmap_destroy(&table->rules);
831 hmap_remove(&cls->tables, &table->hmap_node);
834 hmap_destroy(&cls->tables);
838 /* Returns true if 'cls' contains no classification rules, false otherwise. */
840 classifier_is_empty(const struct classifier *cls)
842 return cls->n_rules == 0;
845 /* Returns the number of rules in 'classifier'. */
847 classifier_count(const struct classifier *cls)
852 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
853 * must not modify or free it.
855 * If 'cls' already contains an identical rule (including wildcards, values of
856 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
857 * rule that was replaced. The caller takes ownership of the returned rule and
858 * is thus responsible for freeing it, etc., as necessary.
860 * Returns NULL if 'cls' does not contain a rule with an identical key, after
861 * inserting the new rule. In this case, no rules are displaced by the new
862 * rule, even rules that cannot have any effect because the new rule matches a
863 * superset of their flows and has higher priority. */
865 classifier_replace(struct classifier *cls, struct cls_rule *rule)
867 struct cls_rule *old_rule;
868 struct cls_table *table;
870 table = find_table(cls, &rule->wc);
872 table = insert_table(cls, &rule->wc);
875 old_rule = insert_rule(table, rule);
877 table->n_table_rules++;
883 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
884 * must not modify or free it.
886 * 'cls' must not contain an identical rule (including wildcards, values of
887 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
890 classifier_insert(struct classifier *cls, struct cls_rule *rule)
892 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
893 assert(!displaced_rule);
896 /* Removes 'rule' from 'cls'. It is the caller's responsibility to free
897 * 'rule', if this is desirable. */
899 classifier_remove(struct classifier *cls, struct cls_rule *rule)
901 struct cls_rule *head;
902 struct cls_table *table;
904 table = find_table(cls, &rule->wc);
905 head = find_equal(table, &rule->flow, rule->hmap_node.hash);
907 list_remove(&rule->list);
908 } else if (list_is_empty(&rule->list)) {
909 hmap_remove(&table->rules, &rule->hmap_node);
911 struct cls_rule *next = CONTAINER_OF(rule->list.next,
912 struct cls_rule, list);
914 list_remove(&rule->list);
915 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
918 if (--table->n_table_rules == 0) {
919 destroy_table(cls, table);
925 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
926 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
927 * of equal priority match 'flow', returns one arbitrarily. */
929 classifier_lookup(const struct classifier *cls, const struct flow *flow)
931 struct cls_table *table;
932 struct cls_rule *best;
935 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
936 struct cls_rule *rule = find_match(table, flow);
937 if (rule && (!best || rule->priority > best->priority)) {
944 /* Finds and returns a rule in 'cls' with exactly the same priority and
945 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
946 * contain an exact match. */
948 classifier_find_rule_exactly(const struct classifier *cls,
949 const struct cls_rule *target)
951 struct cls_rule *head, *rule;
952 struct cls_table *table;
954 table = find_table(cls, &target->wc);
959 head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
960 FOR_EACH_RULE_IN_LIST (rule, head) {
961 if (target->priority >= rule->priority) {
962 return target->priority == rule->priority ? rule : NULL;
968 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
969 * considered to overlap if both rules have the same priority and a packet
970 * could match both. */
972 classifier_rule_overlaps(const struct classifier *cls,
973 const struct cls_rule *target)
975 struct cls_table *table;
977 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
978 struct flow_wildcards wc;
979 struct cls_rule *head;
981 flow_wildcards_combine(&wc, &target->wc, &table->wc);
982 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
983 struct cls_rule *rule;
985 FOR_EACH_RULE_IN_LIST (rule, head) {
986 if (rule->priority == target->priority
987 && flow_equal_except(&target->flow, &rule->flow, &wc)) {
997 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
998 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
999 * function returns true if, for every field:
1001 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
1004 * - 'criteria' wildcards the field,
1006 * Conversely, 'rule' does not match 'criteria' and this function returns false
1007 * if, for at least one field:
1009 * - 'criteria' and 'rule' specify different values for the field, or
1011 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
1013 * Equivalently, the truth table for whether a field matches is:
1018 * r +---------+---------+
1019 * i wild | yes | yes |
1021 * e +---------+---------+
1022 * r exact | no |if values|
1024 * a +---------+---------+
1026 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1027 * commands and by OpenFlow 1.0 aggregate and flow stats.
1029 * Ignores rule->priority and criteria->priority. */
1031 cls_rule_is_loose_match(const struct cls_rule *rule,
1032 const struct cls_rule *criteria)
1034 return (!flow_wildcards_has_extra(&rule->wc, &criteria->wc)
1035 && flow_equal_except(&rule->flow, &criteria->flow, &criteria->wc));
1041 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
1044 || flow_equal_except(&rule->flow, &target->flow, &target->wc));
1047 static struct cls_rule *
1048 search_table(const struct cls_table *table, const struct cls_rule *target)
1050 if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
1051 struct cls_rule *rule;
1053 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1054 if (rule_matches(rule, target)) {
1062 /* Initializes 'cursor' for iterating through rules in 'cls':
1064 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1066 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1067 * such that cls_rule_is_loose_match(rule, target) returns true.
1069 * Ignores target->priority. */
1071 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
1072 const struct cls_rule *target)
1075 cursor->target = target;
1078 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
1079 * pointer if there are no matches. */
1081 cls_cursor_first(struct cls_cursor *cursor)
1083 struct cls_table *table;
1085 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
1086 struct cls_rule *rule = search_table(table, cursor->target);
1088 cursor->table = table;
1096 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1097 * pointer if there are no more matches. */
1099 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
1101 const struct cls_table *table;
1102 struct cls_rule *next;
1104 next = next_rule_in_list__(rule);
1105 if (next->priority < rule->priority) {
1109 /* 'next' is the head of the list, that is, the rule that is included in
1110 * the table's hmap. (This is important when the classifier contains rules
1111 * that differ only in priority.) */
1113 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
1114 if (rule_matches(rule, cursor->target)) {
1119 table = cursor->table;
1120 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
1121 rule = search_table(table, cursor->target);
1123 cursor->table = table;
1131 static struct cls_table *
1132 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
1134 struct cls_table *table;
1136 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
1138 if (flow_wildcards_equal(wc, &table->wc)) {
1145 static struct cls_table *
1146 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
1148 struct cls_table *table;
1150 table = xzalloc(sizeof *table);
1151 hmap_init(&table->rules);
1153 table->is_catchall = flow_wildcards_is_catchall(&table->wc);
1154 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1160 destroy_table(struct classifier *cls, struct cls_table *table)
1162 hmap_remove(&cls->tables, &table->hmap_node);
1163 hmap_destroy(&table->rules);
1167 static struct cls_rule *
1168 find_match(const struct cls_table *table, const struct flow *flow)
1170 struct cls_rule *rule;
1172 if (table->is_catchall) {
1173 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1180 flow_zero_wildcards(&f, &table->wc);
1181 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1183 if (flow_equal(&f, &rule->flow)) {
1192 static struct cls_rule *
1193 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1195 struct cls_rule *head;
1197 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1198 if (flow_equal(&head->flow, flow)) {
1205 static struct cls_rule *
1206 insert_rule(struct cls_table *table, struct cls_rule *new)
1208 struct cls_rule *head;
1210 new->hmap_node.hash = flow_hash(&new->flow, 0);
1212 head = find_equal(table, &new->flow, new->hmap_node.hash);
1214 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1215 list_init(&new->list);
1218 /* Scan the list for the insertion point that will keep the list in
1219 * order of decreasing priority. */
1220 struct cls_rule *rule;
1221 FOR_EACH_RULE_IN_LIST (rule, head) {
1222 if (new->priority >= rule->priority) {
1224 /* 'new' is the new highest-priority flow in the list. */
1225 hmap_replace(&table->rules,
1226 &rule->hmap_node, &new->hmap_node);
1229 if (new->priority == rule->priority) {
1230 list_replace(&new->list, &rule->list);
1233 list_insert(&rule->list, &new->list);
1239 /* Insert 'new' at the end of the list. */
1240 list_push_back(&head->list, &new->list);
1245 static struct cls_rule *
1246 next_rule_in_list__(struct cls_rule *rule)
1248 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1252 static struct cls_rule *
1253 next_rule_in_list(struct cls_rule *rule)
1255 struct cls_rule *next = next_rule_in_list__(rule);
1256 return next->priority < rule->priority ? next : NULL;