2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
20 #include <netinet/in.h>
21 #include "byte-order.h"
22 #include "dynamic-string.h"
34 VLOG_DEFINE_THIS_MODULE(classifier);
38 /* Ports trie depends on both ports sharing the same ovs_be32. */
39 #define TP_PORTS_OFS32 (offsetof(struct flow, tp_src) / 4)
40 BUILD_ASSERT_DECL(TP_PORTS_OFS32 == offsetof(struct flow, tp_dst) / 4);
42 /* A set of rules that all have the same fields wildcarded. */
44 /* The fields are only used by writers and iterators. */
45 struct cmap_node cmap_node; /* Within struct classifier 'subtables_map'. */
47 /* The fields are only used by writers. */
48 int n_rules OVS_GUARDED; /* Number of rules, including
50 unsigned int max_priority OVS_GUARDED; /* Max priority of any rule in
52 unsigned int max_count OVS_GUARDED; /* Count of max_priority rules. */
54 /* These fields are accessed by readers who care about wildcarding. */
55 tag_type tag; /* Tag generated from mask for partitioning (const). */
56 uint8_t n_indices; /* How many indices to use (const). */
57 uint8_t index_ofs[CLS_MAX_INDICES]; /* u32 segment boundaries (const). */
58 unsigned int trie_plen[CLS_MAX_TRIES]; /* Trie prefix length in 'mask'
59 * (runtime configurable). */
60 int ports_mask_len; /* (const) */
61 struct cmap indices[CLS_MAX_INDICES]; /* Staged lookup indices. */
62 rcu_trie_ptr ports_trie; /* NULL if none. */
64 /* These fields are accessed by all readers. */
65 struct cmap rules; /* Contains "struct cls_rule"s. */
66 struct minimask mask; /* Wildcards for fields (const). */
67 /* 'mask' must be the last field. */
70 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
71 * field) with tags for the "cls_subtable"s that contain rules that match that
73 struct cls_partition {
74 struct cmap_node cmap_node; /* In struct classifier's 'partitions' map. */
75 ovs_be64 metadata; /* metadata value for this partition. */
76 tag_type tags; /* OR of each flow's cls_subtable tag. */
77 struct tag_tracker tracker OVS_GUARDED; /* Tracks the bits in 'tags'. */
80 /* Internal representation of a rule in a "struct cls_subtable". */
82 /* Accessed only by writers and iterators. */
83 struct list list OVS_GUARDED; /* List of identical, lower-priority rules. */
85 /* Accessed only by writers. */
86 struct cls_partition *partition OVS_GUARDED;
88 /* Accessed by readers interested in wildcarding. */
89 unsigned int priority; /* Larger numbers are higher priorities. */
90 struct cmap_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
92 /* Accessed by all readers. */
93 struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */
94 struct cls_rule *cls_rule;
95 struct miniflow flow; /* Matching rule. Mask is in the subtable. */
96 /* 'flow' must be the last field. */
99 static struct cls_match *
100 cls_match_alloc(struct cls_rule *rule)
102 int count = count_1bits(rule->match.flow.map);
104 struct cls_match *cls_match
105 = xmalloc(sizeof *cls_match - sizeof cls_match->flow.inline_values
106 + MINIFLOW_VALUES_SIZE(count));
108 cls_match->cls_rule = rule;
109 miniflow_clone_inline(&cls_match->flow, &rule->match.flow, count);
110 cls_match->priority = rule->priority;
111 rule->cls_match = cls_match;
116 static struct cls_subtable *find_subtable(const struct classifier *cls,
117 const struct minimask *)
118 OVS_REQUIRES(cls->mutex);
119 static struct cls_subtable *insert_subtable(struct classifier *cls,
120 const struct minimask *)
121 OVS_REQUIRES(cls->mutex);
122 static void destroy_subtable(struct classifier *cls, struct cls_subtable *)
123 OVS_REQUIRES(cls->mutex);
124 static struct cls_match *insert_rule(struct classifier *cls,
125 struct cls_subtable *, struct cls_rule *)
126 OVS_REQUIRES(cls->mutex);
128 static struct cls_match *find_match_wc(const struct cls_subtable *,
129 const struct flow *, struct trie_ctx *,
130 unsigned int n_tries,
131 struct flow_wildcards *);
132 static struct cls_match *find_equal(struct cls_subtable *,
133 const struct miniflow *, uint32_t hash);
135 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list.
136 * Classifier's mutex must be held while iterating, as the list is
137 * protoceted by it. */
138 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
139 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
140 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
141 for ((RULE) = (HEAD); \
142 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
145 static struct cls_match *next_rule_in_list__(struct cls_match *);
146 static struct cls_match *next_rule_in_list(struct cls_match *);
148 static unsigned int minimask_get_prefix_len(const struct minimask *,
149 const struct mf_field *);
150 static void trie_init(struct classifier *cls, int trie_idx,
151 const struct mf_field *)
152 OVS_REQUIRES(cls->mutex);
153 static unsigned int trie_lookup(const struct cls_trie *, const struct flow *,
154 union mf_value *plens);
155 static unsigned int trie_lookup_value(const rcu_trie_ptr *,
156 const ovs_be32 value[], ovs_be32 plens[],
157 unsigned int value_bits);
158 static void trie_destroy(rcu_trie_ptr *);
159 static void trie_insert(struct cls_trie *, const struct cls_rule *, int mlen);
160 static void trie_insert_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
162 static void trie_remove(struct cls_trie *, const struct cls_rule *, int mlen);
163 static void trie_remove_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
165 static void mask_set_prefix_bits(struct flow_wildcards *, uint8_t be32ofs,
166 unsigned int n_bits);
167 static bool mask_prefix_bits_set(const struct flow_wildcards *,
168 uint8_t be32ofs, unsigned int n_bits);
170 /* flow/miniflow/minimask/minimatch utilities.
171 * These are only used by the classifier, so place them here to allow
172 * for better optimization. */
174 static inline uint64_t
175 miniflow_get_map_in_range(const struct miniflow *miniflow,
176 uint8_t start, uint8_t end, unsigned int *offset)
178 uint64_t map = miniflow->map;
182 uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
183 *offset = count_1bits(map & msk);
186 if (end < FLOW_U32S) {
187 uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
193 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
194 * 'mask', given 'basis'.
196 * The hash values returned by this function are the same as those returned by
197 * miniflow_hash_in_minimask(), only the form of the arguments differ. */
198 static inline uint32_t
199 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
202 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
203 const uint32_t *flow_u32 = (const uint32_t *)flow;
204 const uint32_t *p = mask_values;
209 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
210 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
213 return hash_finish(hash, (p - mask_values) * 4);
216 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
217 * 'mask', given 'basis'.
219 * The hash values returned by this function are the same as those returned by
220 * flow_hash_in_minimask(), only the form of the arguments differ. */
221 static inline uint32_t
222 miniflow_hash_in_minimask(const struct miniflow *flow,
223 const struct minimask *mask, uint32_t basis)
225 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
226 const uint32_t *p = mask_values;
227 uint32_t hash = basis;
230 MINIFLOW_FOR_EACH_IN_MAP(flow_u32, flow, mask->masks.map) {
231 hash = hash_add(hash, flow_u32 & *p++);
234 return hash_finish(hash, (p - mask_values) * 4);
237 /* Returns a hash value for the bits of range [start, end) in 'flow',
238 * where there are 1-bits in 'mask', given 'hash'.
240 * The hash values returned by this function are the same as those returned by
241 * minimatch_hash_range(), only the form of the arguments differ. */
242 static inline uint32_t
243 flow_hash_in_minimask_range(const struct flow *flow,
244 const struct minimask *mask,
245 uint8_t start, uint8_t end, uint32_t *basis)
247 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
248 const uint32_t *flow_u32 = (const uint32_t *)flow;
250 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
252 const uint32_t *p = mask_values + offset;
253 uint32_t hash = *basis;
255 for (; map; map = zero_rightmost_1bit(map)) {
256 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
259 *basis = hash; /* Allow continuation from the unfinished value. */
260 return hash_finish(hash, (p - mask_values) * 4);
263 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
265 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
266 const struct minimask *mask)
268 flow_union_with_miniflow(&wc->masks, &mask->masks);
271 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
272 * in range [start, end). */
274 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
275 const struct minimask *mask,
276 uint8_t start, uint8_t end)
278 uint32_t *dst_u32 = (uint32_t *)&wc->masks;
280 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
282 const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
284 for (; map; map = zero_rightmost_1bit(map)) {
285 dst_u32[raw_ctz(map)] |= *p++;
289 /* Returns a hash value for 'flow', given 'basis'. */
290 static inline uint32_t
291 miniflow_hash(const struct miniflow *flow, uint32_t basis)
293 const uint32_t *values = miniflow_get_u32_values(flow);
294 const uint32_t *p = values;
295 uint32_t hash = basis;
296 uint64_t hash_map = 0;
299 for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
301 hash = hash_add(hash, *p);
302 hash_map |= rightmost_1bit(map);
306 hash = hash_add(hash, hash_map);
307 hash = hash_add(hash, hash_map >> 32);
309 return hash_finish(hash, p - values);
312 /* Returns a hash value for 'mask', given 'basis'. */
313 static inline uint32_t
314 minimask_hash(const struct minimask *mask, uint32_t basis)
316 return miniflow_hash(&mask->masks, basis);
319 /* Returns a hash value for 'match', given 'basis'. */
320 static inline uint32_t
321 minimatch_hash(const struct minimatch *match, uint32_t basis)
323 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
326 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
329 * The hash values returned by this function are the same as those returned by
330 * flow_hash_in_minimask_range(), only the form of the arguments differ. */
331 static inline uint32_t
332 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
336 const uint32_t *p, *q;
337 uint32_t hash = *basis;
340 n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
342 q = miniflow_get_u32_values(&match->mask.masks) + offset;
343 p = miniflow_get_u32_values(&match->flow) + offset;
345 for (i = 0; i < n; i++) {
346 hash = hash_add(hash, p[i] & q[i]);
348 *basis = hash; /* Allow continuation from the unfinished value. */
349 return hash_finish(hash, (offset + n) * 4);
355 /* Initializes 'rule' to match packets specified by 'match' at the given
356 * 'priority'. 'match' must satisfy the invariant described in the comment at
357 * the definition of struct match.
359 * The caller must eventually destroy 'rule' with cls_rule_destroy().
361 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
362 * internally Open vSwitch supports a wider range.) */
364 cls_rule_init(struct cls_rule *rule,
365 const struct match *match, unsigned int priority)
367 minimatch_init(&rule->match, match);
368 rule->priority = priority;
369 rule->cls_match = NULL;
372 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
374 cls_rule_init_from_minimatch(struct cls_rule *rule,
375 const struct minimatch *match,
376 unsigned int priority)
378 minimatch_clone(&rule->match, match);
379 rule->priority = priority;
380 rule->cls_match = NULL;
383 /* Initializes 'dst' as a copy of 'src'.
385 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
387 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
389 minimatch_clone(&dst->match, &src->match);
390 dst->priority = src->priority;
391 dst->cls_match = NULL;
394 /* Initializes 'dst' with the data in 'src', destroying 'src'.
396 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
398 cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
400 minimatch_move(&dst->match, &src->match);
401 dst->priority = src->priority;
402 dst->cls_match = NULL;
405 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
406 * normally embedded into a larger structure).
408 * ('rule' must not currently be in a classifier.) */
410 cls_rule_destroy(struct cls_rule *rule)
412 ovs_assert(!rule->cls_match);
413 minimatch_destroy(&rule->match);
416 /* Returns true if 'a' and 'b' match the same packets at the same priority,
417 * false if they differ in some way. */
419 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
421 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
424 /* Returns a hash value for 'rule', folding in 'basis'. */
426 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
428 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
431 /* Appends a string describing 'rule' to 's'. */
433 cls_rule_format(const struct cls_rule *rule, struct ds *s)
435 minimatch_format(&rule->match, s, rule->priority);
438 /* Returns true if 'rule' matches every packet, false otherwise. */
440 cls_rule_is_catchall(const struct cls_rule *rule)
442 return minimask_is_catchall(&rule->match.mask);
445 /* Initializes 'cls' as a classifier that initially contains no classification
448 classifier_init(struct classifier *cls, const uint8_t *flow_segments)
449 OVS_EXCLUDED(cls->mutex)
451 ovs_mutex_init(&cls->mutex);
452 ovs_mutex_lock(&cls->mutex);
454 cmap_init(&cls->subtables_map);
455 pvector_init(&cls->subtables);
456 cmap_init(&cls->partitions);
457 cls->n_flow_segments = 0;
459 while (cls->n_flow_segments < CLS_MAX_INDICES
460 && *flow_segments < FLOW_U32S) {
461 cls->flow_segments[cls->n_flow_segments++] = *flow_segments++;
465 for (int i = 0; i < CLS_MAX_TRIES; i++) {
466 trie_init(cls, i, NULL);
468 ovs_mutex_unlock(&cls->mutex);
471 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
472 * caller's responsibility.
473 * May only be called after all the readers have been terminated. */
475 classifier_destroy(struct classifier *cls)
476 OVS_EXCLUDED(cls->mutex)
479 struct cls_partition *partition, *next_partition;
480 struct cls_subtable *subtable, *next_subtable;
483 ovs_mutex_lock(&cls->mutex);
484 for (i = 0; i < cls->n_tries; i++) {
485 trie_destroy(&cls->tries[i].root);
488 CMAP_FOR_EACH_SAFE (subtable, next_subtable, cmap_node,
489 &cls->subtables_map) {
490 destroy_subtable(cls, subtable);
492 cmap_destroy(&cls->subtables_map);
494 CMAP_FOR_EACH_SAFE (partition, next_partition, cmap_node,
496 ovsrcu_postpone(free, partition);
498 cmap_destroy(&cls->partitions);
500 pvector_destroy(&cls->subtables);
501 ovs_mutex_unlock(&cls->mutex);
502 ovs_mutex_destroy(&cls->mutex);
506 /* We use uint64_t as a set for the fields below. */
507 BUILD_ASSERT_DECL(MFF_N_IDS <= 64);
509 /* Set the fields for which prefix lookup should be performed. */
511 classifier_set_prefix_fields(struct classifier *cls,
512 const enum mf_field_id *trie_fields,
513 unsigned int n_fields)
514 OVS_EXCLUDED(cls->mutex)
517 const struct mf_field * new_fields[CLS_MAX_TRIES];
519 bool changed = false;
521 ovs_mutex_lock(&cls->mutex);
522 for (i = 0; i < n_fields && n_tries < CLS_MAX_TRIES; i++) {
523 const struct mf_field *field = mf_from_id(trie_fields[i]);
524 if (field->flow_be32ofs < 0 || field->n_bits % 32) {
525 /* Incompatible field. This is the only place where we
526 * enforce these requirements, but the rest of the trie code
527 * depends on the flow_be32ofs to be non-negative and the
528 * field length to be a multiple of 32 bits. */
532 if (fields & (UINT64_C(1) << trie_fields[i])) {
533 /* Duplicate field, there is no need to build more than
534 * one index for any one field. */
537 fields |= UINT64_C(1) << trie_fields[i];
539 new_fields[n_tries] = NULL;
540 if (n_tries >= cls->n_tries || field != cls->tries[n_tries].field) {
541 new_fields[n_tries] = field;
547 if (changed || n_tries < cls->n_tries) {
548 struct cls_subtable *subtable;
550 /* Trie configuration needs to change. Disable trie lookups
551 * for the tries that are changing and wait all the current readers
552 * with the old configuration to be done. */
554 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
555 for (i = 0; i < cls->n_tries; i++) {
556 if ((i < n_tries && new_fields[i]) || i >= n_tries) {
557 if (subtable->trie_plen[i]) {
558 subtable->trie_plen[i] = 0;
564 /* Synchronize if any readers were using tries. The readers may
565 * temporarily function without the trie lookup based optimizations. */
567 /* ovsrcu_synchronize() functions as a memory barrier, so it does
568 * not matter that subtable->trie_plen is not atomic. */
569 ovsrcu_synchronize();
572 /* Now set up the tries. */
573 for (i = 0; i < n_tries; i++) {
575 trie_init(cls, i, new_fields[i]);
578 /* Destroy the rest, if any. */
579 for (; i < cls->n_tries; i++) {
580 trie_init(cls, i, NULL);
583 cls->n_tries = n_tries;
584 ovs_mutex_unlock(&cls->mutex);
588 ovs_mutex_unlock(&cls->mutex);
589 return false; /* No change. */
593 trie_init(struct classifier *cls, int trie_idx, const struct mf_field *field)
594 OVS_REQUIRES(cls->mutex)
596 struct cls_trie *trie = &cls->tries[trie_idx];
597 struct cls_subtable *subtable;
599 if (trie_idx < cls->n_tries) {
600 trie_destroy(&trie->root);
602 ovsrcu_set_hidden(&trie->root, NULL);
606 /* Add existing rules to the new trie. */
607 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
610 plen = field ? minimask_get_prefix_len(&subtable->mask, field) : 0;
612 struct cls_match *head;
614 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
615 struct cls_match *match;
617 FOR_EACH_RULE_IN_LIST (match, head) {
618 trie_insert(trie, match->cls_rule, plen);
622 /* Initialize subtable's prefix length on this field. This will
623 * allow readers to use the trie. */
624 atomic_thread_fence(memory_order_release);
625 subtable->trie_plen[trie_idx] = plen;
629 /* Returns true if 'cls' contains no classification rules, false otherwise.
630 * Checking the cmap requires no locking. */
632 classifier_is_empty(const struct classifier *cls)
634 return cmap_is_empty(&cls->subtables_map);
637 /* Returns the number of rules in 'cls'. */
639 classifier_count(const struct classifier *cls)
640 OVS_NO_THREAD_SAFETY_ANALYSIS
642 /* n_rules is an int, so in the presence of concurrent writers this will
643 * return either the old or a new value. */
648 hash_metadata(ovs_be64 metadata_)
650 uint64_t metadata = (OVS_FORCE uint64_t) metadata_;
651 return hash_uint64(metadata);
654 static struct cls_partition *
655 find_partition(const struct classifier *cls, ovs_be64 metadata, uint32_t hash)
657 struct cls_partition *partition;
659 CMAP_FOR_EACH_WITH_HASH (partition, cmap_node, hash, &cls->partitions) {
660 if (partition->metadata == metadata) {
668 static struct cls_partition *
669 create_partition(struct classifier *cls, struct cls_subtable *subtable,
671 OVS_REQUIRES(cls->mutex)
673 uint32_t hash = hash_metadata(metadata);
674 struct cls_partition *partition = find_partition(cls, metadata, hash);
676 partition = xmalloc(sizeof *partition);
677 partition->metadata = metadata;
679 tag_tracker_init(&partition->tracker);
680 cmap_insert(&cls->partitions, &partition->cmap_node, hash);
682 tag_tracker_add(&partition->tracker, &partition->tags, subtable->tag);
686 static inline ovs_be32 minimatch_get_ports(const struct minimatch *match)
688 /* Could optimize to use the same map if needed for fast path. */
689 return MINIFLOW_GET_BE32(&match->flow, tp_src)
690 & MINIFLOW_GET_BE32(&match->mask.masks, tp_src);
693 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
694 * must not modify or free it.
696 * If 'cls' already contains an identical rule (including wildcards, values of
697 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
698 * rule that was replaced. The caller takes ownership of the returned rule and
699 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
700 * memory block in which it resides, etc., as necessary.
702 * Returns NULL if 'cls' does not contain a rule with an identical key, after
703 * inserting the new rule. In this case, no rules are displaced by the new
704 * rule, even rules that cannot have any effect because the new rule matches a
705 * superset of their flows and has higher priority. */
707 classifier_replace(struct classifier *cls, struct cls_rule *rule)
708 OVS_EXCLUDED(cls->mutex)
710 struct cls_match *old_rule;
711 struct cls_subtable *subtable;
712 struct cls_rule *old_cls_rule = NULL;
714 ovs_mutex_lock(&cls->mutex);
715 subtable = find_subtable(cls, &rule->match.mask);
717 subtable = insert_subtable(cls, &rule->match.mask);
720 old_rule = insert_rule(cls, subtable, rule);
724 rule->cls_match->partition = NULL;
725 if (minimask_get_metadata_mask(&rule->match.mask) == OVS_BE64_MAX) {
726 ovs_be64 metadata = miniflow_get_metadata(&rule->match.flow);
727 rule->cls_match->partition = create_partition(cls, subtable,
733 for (int i = 0; i < cls->n_tries; i++) {
734 if (subtable->trie_plen[i]) {
735 trie_insert(&cls->tries[i], rule, subtable->trie_plen[i]);
740 if (subtable->ports_mask_len) {
741 /* We mask the value to be inserted to always have the wildcarded
742 * bits in known (zero) state, so we can include them in comparison
743 * and they will always match (== their original value does not
745 ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
747 trie_insert_prefix(&subtable->ports_trie, &masked_ports,
748 subtable->ports_mask_len);
751 old_cls_rule = old_rule->cls_rule;
752 rule->cls_match->partition = old_rule->partition;
753 old_cls_rule->cls_match = NULL;
755 /* 'old_rule' contains a cmap_node, which may not be freed
757 ovsrcu_postpone(free, old_rule);
759 ovs_mutex_unlock(&cls->mutex);
763 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
764 * must not modify or free it.
766 * 'cls' must not contain an identical rule (including wildcards, values of
767 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
770 classifier_insert(struct classifier *cls, struct cls_rule *rule)
772 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
773 ovs_assert(!displaced_rule);
776 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
777 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
778 * resides, etc., as necessary. */
780 classifier_remove(struct classifier *cls, struct cls_rule *rule)
781 OVS_EXCLUDED(cls->mutex)
783 struct cls_partition *partition;
784 struct cls_match *cls_match = rule->cls_match;
785 struct cls_match *head;
786 struct cls_subtable *subtable;
788 uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
789 uint8_t prev_be32ofs = 0;
791 ovs_assert(cls_match);
793 ovs_mutex_lock(&cls->mutex);
794 subtable = find_subtable(cls, &rule->match.mask);
795 ovs_assert(subtable);
797 if (subtable->ports_mask_len) {
798 ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
800 trie_remove_prefix(&subtable->ports_trie,
801 &masked_ports, subtable->ports_mask_len);
803 for (i = 0; i < cls->n_tries; i++) {
804 if (subtable->trie_plen[i]) {
805 trie_remove(&cls->tries[i], rule, subtable->trie_plen[i]);
809 /* Remove rule node from indices. */
810 for (i = 0; i < subtable->n_indices; i++) {
811 ihash[i] = minimatch_hash_range(&rule->match, prev_be32ofs,
812 subtable->index_ofs[i], &basis);
813 cmap_remove(&subtable->indices[i], &cls_match->index_nodes[i],
815 prev_be32ofs = subtable->index_ofs[i];
817 hash = minimatch_hash_range(&rule->match, prev_be32ofs, FLOW_U32S, &basis);
819 head = find_equal(subtable, &rule->match.flow, hash);
820 if (head != cls_match) {
821 list_remove(&cls_match->list);
822 } else if (list_is_empty(&cls_match->list)) {
823 cmap_remove(&subtable->rules, &cls_match->cmap_node, hash);
825 struct cls_match *next = CONTAINER_OF(cls_match->list.next,
826 struct cls_match, list);
828 list_remove(&cls_match->list);
829 cmap_replace(&subtable->rules, &cls_match->cmap_node,
830 &next->cmap_node, hash);
833 partition = cls_match->partition;
835 tag_tracker_subtract(&partition->tracker, &partition->tags,
837 if (!partition->tags) {
838 cmap_remove(&cls->partitions, &partition->cmap_node,
839 hash_metadata(partition->metadata));
840 ovsrcu_postpone(free, partition);
844 if (--subtable->n_rules == 0) {
845 destroy_subtable(cls, subtable);
846 } else if (subtable->max_priority == cls_match->priority
847 && --subtable->max_count == 0) {
848 /* Find the new 'max_priority' and 'max_count'. */
849 struct cls_match *head;
850 unsigned int max_priority = 0;
852 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
853 if (head->priority > max_priority) {
854 max_priority = head->priority;
855 subtable->max_count = 1;
856 } else if (head->priority == max_priority) {
857 ++subtable->max_count;
860 subtable->max_priority = max_priority;
861 pvector_change_priority(&cls->subtables, subtable, max_priority);
866 rule->cls_match = NULL;
867 ovsrcu_postpone(free, cls_match);
868 ovs_mutex_unlock(&cls->mutex);
871 /* Prefix tree context. Valid when 'lookup_done' is true. Can skip all
872 * subtables which have a prefix match on the trie field, but whose prefix
873 * length is not indicated in 'match_plens'. For example, a subtable that
874 * has a 8-bit trie field prefix match can be skipped if
875 * !be_get_bit_at(&match_plens, 8 - 1). If skipped, 'maskbits' prefix bits
876 * must be unwildcarded to make datapath flow only match packets it should. */
878 const struct cls_trie *trie;
879 bool lookup_done; /* Status of the lookup. */
880 uint8_t be32ofs; /* U32 offset of the field in question. */
881 unsigned int maskbits; /* Prefix length needed to avoid false matches. */
882 union mf_value match_plens; /* Bitmask of prefix lengths with possible
887 trie_ctx_init(struct trie_ctx *ctx, const struct cls_trie *trie)
890 ctx->be32ofs = trie->field->flow_be32ofs;
891 ctx->lookup_done = false;
894 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
895 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
896 * of equal priority match 'flow', returns one arbitrarily.
898 * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
899 * set of bits that were significant in the lookup. At some point
900 * earlier, 'wc' should have been initialized (e.g., by
901 * flow_wildcards_init_catchall()). */
903 classifier_lookup(const struct classifier *cls, const struct flow *flow,
904 struct flow_wildcards *wc)
906 const struct cls_partition *partition;
908 int64_t best_priority = -1;
909 const struct cls_match *best;
910 struct trie_ctx trie_ctx[CLS_MAX_TRIES];
911 struct cls_subtable *subtable;
913 /* Synchronize for cls->n_tries and subtable->trie_plen. They can change
914 * when table configuration changes, which happens typically only on
916 atomic_thread_fence(memory_order_acquire);
918 /* Determine 'tags' such that, if 'subtable->tag' doesn't intersect them,
919 * then 'flow' cannot possibly match in 'subtable':
921 * - If flow->metadata maps to a given 'partition', then we can use
922 * 'tags' for 'partition->tags'.
924 * - If flow->metadata has no partition, then no rule in 'cls' has an
925 * exact-match for flow->metadata. That means that we don't need to
926 * search any subtable that includes flow->metadata in its mask.
928 * In either case, we always need to search any cls_subtables that do not
929 * include flow->metadata in its mask. One way to do that would be to
930 * check the "cls_subtable"s explicitly for that, but that would require an
931 * extra branch per subtable. Instead, we mark such a cls_subtable's
932 * 'tags' as TAG_ALL and make sure that 'tags' is never empty. This means
933 * that 'tags' always intersects such a cls_subtable's 'tags', so we don't
934 * need a special case.
936 partition = (cmap_is_empty(&cls->partitions)
938 : find_partition(cls, flow->metadata,
939 hash_metadata(flow->metadata)));
940 tags = partition ? partition->tags : TAG_ARBITRARY;
942 /* Initialize trie contexts for match_find_wc(). */
943 for (int i = 0; i < cls->n_tries; i++) {
944 trie_ctx_init(&trie_ctx[i], &cls->tries[i]);
948 PVECTOR_FOR_EACH_PRIORITY(subtable, best_priority, 2,
949 sizeof(struct cls_subtable), &cls->subtables) {
950 struct cls_match *rule;
952 if (!tag_intersects(tags, subtable->tag)) {
956 rule = find_match_wc(subtable, flow, trie_ctx, cls->n_tries, wc);
957 if (rule && (int64_t)rule->priority > best_priority) {
958 best_priority = (int64_t)rule->priority;
963 return best ? best->cls_rule : NULL;
966 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
967 * 'match' specifies a particular value has the correct value in 'target'.
969 * 'flow' and 'mask' have the same mask! */
971 miniflow_and_mask_matches_miniflow(const struct miniflow *flow,
972 const struct minimask *mask,
973 const struct miniflow *target)
975 const uint32_t *flowp = miniflow_get_u32_values(flow);
976 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
979 MINIFLOW_FOR_EACH_IN_MAP(target_u32, target, mask->masks.map) {
980 if ((*flowp++ ^ target_u32) & *maskp++) {
988 static inline struct cls_match *
989 find_match_miniflow(const struct cls_subtable *subtable,
990 const struct miniflow *flow,
993 struct cls_match *rule;
995 CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
996 if (miniflow_and_mask_matches_miniflow(&rule->flow, &subtable->mask,
1005 /* For each miniflow in 'flows' performs a classifier lookup writing the result
1006 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
1007 * NULL it is skipped.
1009 * This function is optimized for use in the userspace datapath and therefore
1010 * does not implement a lot of features available in the standard
1011 * classifier_lookup() function. Specifically, it does not implement
1012 * priorities, instead returning any rule which matches the flow. */
1014 classifier_lookup_miniflow_batch(const struct classifier *cls,
1015 const struct miniflow **flows,
1016 struct cls_rule **rules, size_t len)
1018 struct cls_subtable *subtable;
1019 size_t i, begin = 0;
1021 memset(rules, 0, len * sizeof *rules);
1022 PVECTOR_FOR_EACH (subtable, &cls->subtables) {
1023 for (i = begin; i < len; i++) {
1024 struct cls_match *match;
1027 if (OVS_UNLIKELY(rules[i] || !flows[i])) {
1031 hash = miniflow_hash_in_minimask(flows[i], &subtable->mask, 0);
1032 match = find_match_miniflow(subtable, flows[i], hash);
1033 if (OVS_UNLIKELY(match)) {
1034 rules[i] = match->cls_rule;
1038 while (begin < len && (rules[begin] || !flows[begin])) {
1047 /* Finds and returns a rule in 'cls' with exactly the same priority and
1048 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1049 * contain an exact match. */
1051 classifier_find_rule_exactly(const struct classifier *cls,
1052 const struct cls_rule *target)
1053 OVS_EXCLUDED(cls->mutex)
1055 struct cls_match *head, *rule;
1056 struct cls_subtable *subtable;
1058 ovs_mutex_lock(&cls->mutex);
1059 subtable = find_subtable(cls, &target->match.mask);
1064 /* Skip if there is no hope. */
1065 if (target->priority > subtable->max_priority) {
1069 head = find_equal(subtable, &target->match.flow,
1070 miniflow_hash_in_minimask(&target->match.flow,
1071 &target->match.mask, 0));
1072 FOR_EACH_RULE_IN_LIST (rule, head) {
1073 if (target->priority >= rule->priority) {
1074 ovs_mutex_unlock(&cls->mutex);
1075 return target->priority == rule->priority ? rule->cls_rule : NULL;
1079 ovs_mutex_unlock(&cls->mutex);
1083 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
1084 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1085 * contain an exact match. */
1087 classifier_find_match_exactly(const struct classifier *cls,
1088 const struct match *target,
1089 unsigned int priority)
1091 struct cls_rule *retval;
1094 cls_rule_init(&cr, target, priority);
1095 retval = classifier_find_rule_exactly(cls, &cr);
1096 cls_rule_destroy(&cr);
1101 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
1102 * considered to overlap if both rules have the same priority and a packet
1103 * could match both. */
1105 classifier_rule_overlaps(const struct classifier *cls,
1106 const struct cls_rule *target)
1107 OVS_EXCLUDED(cls->mutex)
1109 struct cls_subtable *subtable;
1110 int64_t stop_at_priority = (int64_t)target->priority - 1;
1112 ovs_mutex_lock(&cls->mutex);
1113 /* Iterate subtables in the descending max priority order. */
1114 PVECTOR_FOR_EACH_PRIORITY (subtable, stop_at_priority, 2,
1115 sizeof(struct cls_subtable), &cls->subtables) {
1116 uint32_t storage[FLOW_U32S];
1117 struct minimask mask;
1118 struct cls_match *head;
1120 minimask_combine(&mask, &target->match.mask, &subtable->mask, storage);
1121 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
1122 struct cls_match *rule;
1124 FOR_EACH_RULE_IN_LIST (rule, head) {
1125 if (rule->priority < target->priority) {
1126 break; /* Rules in descending priority order. */
1128 if (rule->priority == target->priority
1129 && miniflow_equal_in_minimask(&target->match.flow,
1130 &rule->flow, &mask)) {
1131 ovs_mutex_unlock(&cls->mutex);
1138 ovs_mutex_unlock(&cls->mutex);
1142 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
1143 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
1144 * function returns true if, for every field:
1146 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
1149 * - 'criteria' wildcards the field,
1151 * Conversely, 'rule' does not match 'criteria' and this function returns false
1152 * if, for at least one field:
1154 * - 'criteria' and 'rule' specify different values for the field, or
1156 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
1158 * Equivalently, the truth table for whether a field matches is:
1163 * r +---------+---------+
1164 * i wild | yes | yes |
1166 * e +---------+---------+
1167 * r exact | no |if values|
1169 * a +---------+---------+
1171 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1172 * commands and by OpenFlow 1.0 aggregate and flow stats.
1174 * Ignores rule->priority. */
1176 cls_rule_is_loose_match(const struct cls_rule *rule,
1177 const struct minimatch *criteria)
1179 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
1180 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
1187 rule_matches(const struct cls_match *rule, const struct cls_rule *target)
1190 || miniflow_equal_in_minimask(&rule->flow,
1191 &target->match.flow,
1192 &target->match.mask));
1195 static struct cls_match *
1196 search_subtable(const struct cls_subtable *subtable,
1197 struct cls_cursor *cursor)
1200 || !minimask_has_extra(&subtable->mask, &cursor->target->match.mask)) {
1201 struct cls_match *rule;
1203 CMAP_CURSOR_FOR_EACH (rule, cmap_node, &cursor->rules,
1205 if (rule_matches(rule, cursor->target)) {
1213 /* Initializes 'cursor' for iterating through rules in 'cls', and returns the
1214 * first matching cls_rule via '*pnode', or NULL if there are no matches.
1216 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1218 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1219 * such that cls_rule_is_loose_match(rule, target) returns true.
1221 * Ignores target->priority. */
1222 struct cls_cursor cls_cursor_init(const struct classifier *cls,
1223 const struct cls_rule *target,
1224 void **pnode, const void *offset, bool safe)
1225 OVS_NO_THREAD_SAFETY_ANALYSIS
1227 struct cls_cursor cursor;
1228 struct cls_subtable *subtable;
1229 struct cls_rule *cls_rule = NULL;
1233 cursor.target = target && !cls_rule_is_catchall(target) ? target : NULL;
1235 /* Find first rule. */
1236 ovs_mutex_lock(&cursor.cls->mutex);
1237 CMAP_CURSOR_FOR_EACH (subtable, cmap_node, &cursor.subtables,
1238 &cursor.cls->subtables_map) {
1239 struct cls_match *rule = search_subtable(subtable, &cursor);
1242 cursor.subtable = subtable;
1243 cls_rule = rule->cls_rule;
1247 *pnode = (char *)cls_rule + (ptrdiff_t)offset;
1249 /* Leave locked if requested and have a rule. */
1250 if (safe || !cls_rule) {
1251 ovs_mutex_unlock(&cursor.cls->mutex);
1257 cls_cursor_next_unlock(struct cls_cursor *cursor, struct cls_rule *rule)
1258 OVS_NO_THREAD_SAFETY_ANALYSIS
1260 /* Release the mutex if no rule, or 'safe' mode. */
1261 if (!rule || cursor->safe) {
1262 ovs_mutex_unlock(&cursor->cls->mutex);
1266 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1267 * pointer if there are no more matches. */
1269 cls_cursor_next(struct cls_cursor *cursor, const struct cls_rule *rule_)
1270 OVS_NO_THREAD_SAFETY_ANALYSIS
1272 struct cls_match *rule = CONST_CAST(struct cls_match *, rule_->cls_match);
1273 const struct cls_subtable *subtable;
1274 struct cls_match *next;
1276 /* Lock if not locked already. */
1278 ovs_mutex_lock(&cursor->cls->mutex);
1281 next = next_rule_in_list__(rule);
1282 if (next->priority < rule->priority) {
1283 cls_cursor_next_unlock(cursor, next->cls_rule);
1284 return next->cls_rule;
1287 /* 'next' is the head of the list, that is, the rule that is included in
1288 * the subtable's map. (This is important when the classifier contains
1289 * rules that differ only in priority.) */
1291 CMAP_CURSOR_FOR_EACH_CONTINUE (rule, cmap_node, &cursor->rules) {
1292 if (rule_matches(rule, cursor->target)) {
1293 cls_cursor_next_unlock(cursor, rule->cls_rule);
1294 return rule->cls_rule;
1298 subtable = cursor->subtable;
1299 CMAP_CURSOR_FOR_EACH_CONTINUE (subtable, cmap_node, &cursor->subtables) {
1300 rule = search_subtable(subtable, cursor);
1302 cursor->subtable = subtable;
1303 cls_cursor_next_unlock(cursor, rule->cls_rule);
1304 return rule->cls_rule;
1308 ovs_mutex_unlock(&cursor->cls->mutex);
1312 static struct cls_subtable *
1313 find_subtable(const struct classifier *cls, const struct minimask *mask)
1314 OVS_REQUIRES(cls->mutex)
1316 struct cls_subtable *subtable;
1318 CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, minimask_hash(mask, 0),
1319 &cls->subtables_map) {
1320 if (minimask_equal(mask, &subtable->mask)) {
1327 /* The new subtable will be visible to the readers only after this. */
1328 static struct cls_subtable *
1329 insert_subtable(struct classifier *cls, const struct minimask *mask)
1330 OVS_REQUIRES(cls->mutex)
1332 uint32_t hash = minimask_hash(mask, 0);
1333 struct cls_subtable *subtable;
1335 struct flow_wildcards old, new;
1337 int count = count_1bits(mask->masks.map);
1339 subtable = xzalloc(sizeof *subtable - sizeof mask->masks.inline_values
1340 + MINIFLOW_VALUES_SIZE(count));
1341 cmap_init(&subtable->rules);
1342 miniflow_clone_inline(&subtable->mask.masks, &mask->masks, count);
1344 /* Init indices for segmented lookup, if any. */
1345 flow_wildcards_init_catchall(&new);
1348 for (i = 0; i < cls->n_flow_segments; i++) {
1349 flow_wildcards_fold_minimask_range(&new, mask, prev,
1350 cls->flow_segments[i]);
1351 /* Add an index if it adds mask bits. */
1352 if (!flow_wildcards_equal(&new, &old)) {
1353 cmap_init(&subtable->indices[index]);
1354 subtable->index_ofs[index] = cls->flow_segments[i];
1358 prev = cls->flow_segments[i];
1360 /* Check if the rest of the subtable's mask adds any bits,
1361 * and remove the last index if it doesn't. */
1363 flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
1364 if (flow_wildcards_equal(&new, &old)) {
1366 subtable->index_ofs[index] = 0;
1367 cmap_destroy(&subtable->indices[index]);
1370 subtable->n_indices = index;
1372 subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
1373 ? tag_create_deterministic(hash)
1376 for (i = 0; i < cls->n_tries; i++) {
1377 subtable->trie_plen[i] = minimask_get_prefix_len(mask,
1378 cls->tries[i].field);
1382 ovsrcu_set_hidden(&subtable->ports_trie, NULL);
1383 subtable->ports_mask_len
1384 = 32 - ctz32(ntohl(MINIFLOW_GET_BE32(&mask->masks, tp_src)));
1386 cmap_insert(&cls->subtables_map, &subtable->cmap_node, hash);
1392 destroy_subtable(struct classifier *cls, struct cls_subtable *subtable)
1393 OVS_REQUIRES(cls->mutex)
1397 pvector_remove(&cls->subtables, subtable);
1398 trie_destroy(&subtable->ports_trie);
1400 for (i = 0; i < subtable->n_indices; i++) {
1401 cmap_destroy(&subtable->indices[i]);
1403 cmap_remove(&cls->subtables_map, &subtable->cmap_node,
1404 minimask_hash(&subtable->mask, 0));
1405 minimask_destroy(&subtable->mask);
1406 cmap_destroy(&subtable->rules);
1407 ovsrcu_postpone(free, subtable);
1415 static unsigned int be_get_bit_at(const ovs_be32 value[], unsigned int ofs);
1417 /* Return 'true' if can skip rest of the subtable based on the prefix trie
1418 * lookup results. */
1420 check_tries(struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1421 const unsigned int field_plen[CLS_MAX_TRIES],
1422 const struct range ofs, const struct flow *flow,
1423 struct flow_wildcards *wc)
1427 /* Check if we could avoid fully unwildcarding the next level of
1428 * fields using the prefix tries. The trie checks are done only as
1429 * needed to avoid folding in additional bits to the wildcards mask. */
1430 for (j = 0; j < n_tries; j++) {
1431 /* Is the trie field relevant for this subtable? */
1432 if (field_plen[j]) {
1433 struct trie_ctx *ctx = &trie_ctx[j];
1434 uint8_t be32ofs = ctx->be32ofs;
1436 /* Is the trie field within the current range of fields? */
1437 if (be32ofs >= ofs.start && be32ofs < ofs.end) {
1438 /* On-demand trie lookup. */
1439 if (!ctx->lookup_done) {
1440 memset(&ctx->match_plens, 0, sizeof ctx->match_plens);
1441 ctx->maskbits = trie_lookup(ctx->trie, flow,
1443 ctx->lookup_done = true;
1445 /* Possible to skip the rest of the subtable if subtable's
1446 * prefix on the field is not included in the lookup result. */
1447 if (!be_get_bit_at(&ctx->match_plens.be32, field_plen[j] - 1)) {
1448 /* RFC: We want the trie lookup to never result in
1449 * unwildcarding any bits that would not be unwildcarded
1450 * otherwise. Since the trie is shared by the whole
1451 * classifier, it is possible that the 'maskbits' contain
1452 * bits that are irrelevant for the partition of the
1453 * classifier relevant for the current flow. */
1455 /* Can skip if the field is already unwildcarded. */
1456 if (mask_prefix_bits_set(wc, be32ofs, ctx->maskbits)) {
1459 /* Check that the trie result will not unwildcard more bits
1460 * than this stage will. */
1461 if (ctx->maskbits <= field_plen[j]) {
1462 /* Unwildcard the bits and skip the rest. */
1463 mask_set_prefix_bits(wc, be32ofs, ctx->maskbits);
1464 /* Note: Prerequisite already unwildcarded, as the only
1465 * prerequisite of the supported trie lookup fields is
1466 * the ethertype, which is currently always
1478 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1479 * for which 'flow', for which 'mask' has a bit set, specifies a particular
1480 * value has the correct value in 'target'.
1482 * This function is equivalent to miniflow_equal_flow_in_minimask(flow,
1483 * target, mask) but this is faster because of the invariant that
1484 * flow->map and mask->masks.map are the same, and that this version
1485 * takes the 'wc'. */
1487 miniflow_and_mask_matches_flow(const struct miniflow *flow,
1488 const struct minimask *mask,
1489 const struct flow *target)
1491 const uint32_t *flowp = miniflow_get_u32_values(flow);
1492 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1495 MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1496 uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & *maskp++;
1506 static inline struct cls_match *
1507 find_match(const struct cls_subtable *subtable, const struct flow *flow,
1510 struct cls_match *rule;
1512 CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
1513 if (miniflow_and_mask_matches_flow(&rule->flow, &subtable->mask,
1522 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1523 * for which 'flow', for which 'mask' has a bit set, specifies a particular
1524 * value has the correct value in 'target'.
1526 * This function is equivalent to miniflow_and_mask_matches_flow() but this
1527 * version fills in the mask bits in 'wc'. */
1529 miniflow_and_mask_matches_flow_wc(const struct miniflow *flow,
1530 const struct minimask *mask,
1531 const struct flow *target,
1532 struct flow_wildcards *wc)
1534 const uint32_t *flowp = miniflow_get_u32_values(flow);
1535 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1538 MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1539 uint32_t mask = *maskp++;
1540 uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & mask;
1543 /* Only unwildcard if none of the differing bits is already
1545 if (!(flow_u32_value(&wc->masks, idx) & diff)) {
1546 /* Keep one bit of the difference. */
1547 *flow_u32_lvalue(&wc->masks, idx) |= rightmost_1bit(diff);
1551 /* Fill in the bits that were looked at. */
1552 *flow_u32_lvalue(&wc->masks, idx) |= mask;
1558 /* Unwildcard the fields looked up so far, if any. */
1560 fill_range_wc(const struct cls_subtable *subtable, struct flow_wildcards *wc,
1564 flow_wildcards_fold_minimask_range(wc, &subtable->mask, 0, to);
1568 static struct cls_match *
1569 find_match_wc(const struct cls_subtable *subtable, const struct flow *flow,
1570 struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1571 struct flow_wildcards *wc)
1573 uint32_t basis = 0, hash;
1574 struct cls_match *rule;
1578 if (OVS_UNLIKELY(!wc)) {
1579 return find_match(subtable, flow,
1580 flow_hash_in_minimask(flow, &subtable->mask, 0));
1584 /* Try to finish early by checking fields in segments. */
1585 for (i = 0; i < subtable->n_indices; i++) {
1586 struct cmap_node *inode;
1588 ofs.end = subtable->index_ofs[i];
1590 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow,
1592 /* 'wc' bits for the trie field set, now unwildcard the preceding
1593 * bits used so far. */
1594 fill_range_wc(subtable, wc, ofs.start);
1597 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1599 inode = cmap_find(&subtable->indices[i], hash);
1601 /* No match, can stop immediately, but must fold in the bits
1602 * used in lookup so far. */
1603 fill_range_wc(subtable, wc, ofs.end);
1607 /* If we have narrowed down to a single rule already, check whether
1608 * that rule matches. Either way, we're done.
1610 * (Rare) hash collisions may cause us to miss the opportunity for this
1612 if (!cmap_node_next(inode)) {
1613 ASSIGN_CONTAINER(rule, inode - i, index_nodes);
1614 if (miniflow_and_mask_matches_flow_wc(&rule->flow, &subtable->mask,
1620 ofs.start = ofs.end;
1622 ofs.end = FLOW_U32S;
1623 /* Trie check for the final range. */
1624 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow, wc)) {
1625 fill_range_wc(subtable, wc, ofs.start);
1628 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1630 rule = find_match(subtable, flow, hash);
1631 if (!rule && subtable->ports_mask_len) {
1632 /* Ports are always part of the final range, if any.
1633 * No match was found for the ports. Use the ports trie to figure out
1634 * which ports bits to unwildcard. */
1636 ovs_be32 value, plens, mask;
1638 mask = MINIFLOW_GET_BE32(&subtable->mask.masks, tp_src);
1639 value = ((OVS_FORCE ovs_be32 *)flow)[TP_PORTS_OFS32] & mask;
1640 mbits = trie_lookup_value(&subtable->ports_trie, &value, &plens, 32);
1642 ((OVS_FORCE ovs_be32 *)&wc->masks)[TP_PORTS_OFS32] |=
1643 mask & htonl(~0 << (32 - mbits));
1645 /* Unwildcard all bits in the mask upto the ports, as they were used
1646 * to determine there is no match. */
1647 fill_range_wc(subtable, wc, TP_PORTS_OFS32);
1651 /* Must unwildcard all the fields, as they were looked at. */
1652 flow_wildcards_fold_minimask(wc, &subtable->mask);
1656 static struct cls_match *
1657 find_equal(struct cls_subtable *subtable, const struct miniflow *flow,
1660 struct cls_match *head;
1662 CMAP_FOR_EACH_WITH_HASH (head, cmap_node, hash, &subtable->rules) {
1663 if (miniflow_equal(&head->flow, flow)) {
1671 * As the readers are operating concurrently with the modifications, a
1672 * concurrent reader may or may not see the new rule, depending on how
1673 * the concurrent events overlap with each other. This is no
1674 * different from the former locked behavior, but there the visibility
1675 * of the new rule only depended on the timing of the locking
1678 * The new rule is first added to the segment indices, so the readers
1679 * may find the rule in the indices before the rule is visible in the
1680 * subtables 'rules' map. This may result in us losing the
1681 * opportunity to quit lookups earlier, resulting in sub-optimal
1682 * wildcarding. This will be fixed by forthcoming revalidation always
1683 * scheduled after flow table changes.
1685 * Similar behavior may happen due to us removing the overlapping rule
1686 * (if any) from the indices only after the new rule has been added.
1688 * The subtable's max priority is updated only after the rule is
1689 * inserted, so the concurrent readers may not see the rule, as the
1690 * updated priority ordered subtable list will only be visible after
1691 * the subtable's max priority is updated.
1693 * Similarly, the classifier's partitions for new rules are updated by
1694 * the caller after this function, so the readers may keep skipping
1695 * the subtable until they see the updated partitions.
1697 static struct cls_match *
1698 insert_rule(struct classifier *cls, struct cls_subtable *subtable,
1699 struct cls_rule *new_rule)
1700 OVS_REQUIRES(cls->mutex)
1702 struct cls_match *old = NULL;
1703 struct cls_match *new = cls_match_alloc(new_rule);
1704 struct cls_match *head;
1706 uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
1707 uint8_t prev_be32ofs = 0;
1709 /* Add new node to segment indices. */
1710 for (i = 0; i < subtable->n_indices; i++) {
1711 ihash[i] = minimatch_hash_range(&new_rule->match, prev_be32ofs,
1712 subtable->index_ofs[i], &basis);
1713 cmap_insert(&subtable->indices[i], &new->index_nodes[i], ihash[i]);
1714 prev_be32ofs = subtable->index_ofs[i];
1716 hash = minimatch_hash_range(&new_rule->match, prev_be32ofs, FLOW_U32S,
1718 head = find_equal(subtable, &new_rule->match.flow, hash);
1720 cmap_insert(&subtable->rules, &new->cmap_node, hash);
1721 list_init(&new->list);
1724 /* Scan the list for the insertion point that will keep the list in
1725 * order of decreasing priority. */
1726 struct cls_match *rule;
1728 FOR_EACH_RULE_IN_LIST (rule, head) {
1729 if (new->priority >= rule->priority) {
1731 /* 'new' is the new highest-priority flow in the list. */
1732 cmap_replace(&subtable->rules, &rule->cmap_node,
1733 &new->cmap_node, hash);
1736 if (new->priority == rule->priority) {
1737 list_replace(&new->list, &rule->list);
1740 list_insert(&rule->list, &new->list);
1746 /* Insert 'new' at the end of the list. */
1747 list_push_back(&head->list, &new->list);
1752 subtable->n_rules++;
1754 /* Rule was added, not replaced. Update 'subtable's 'max_priority'
1755 * and 'max_count', if necessary. */
1756 if (subtable->n_rules == 1) {
1757 subtable->max_priority = new->priority;
1758 subtable->max_count = 1;
1759 pvector_insert(&cls->subtables, subtable, new->priority);
1760 } else if (subtable->max_priority == new->priority) {
1761 ++subtable->max_count;
1762 } else if (new->priority > subtable->max_priority) {
1763 subtable->max_priority = new->priority;
1764 subtable->max_count = 1;
1765 pvector_change_priority(&cls->subtables, subtable, new->priority);
1768 /* Remove old node from indices. */
1769 for (i = 0; i < subtable->n_indices; i++) {
1770 cmap_remove(&subtable->indices[i], &old->index_nodes[i], ihash[i]);
1776 static struct cls_match *
1777 next_rule_in_list__(struct cls_match *rule)
1778 OVS_NO_THREAD_SAFETY_ANALYSIS
1780 struct cls_match *next = OBJECT_CONTAINING(rule->list.next, next, list);
1784 static struct cls_match *
1785 next_rule_in_list(struct cls_match *rule)
1787 struct cls_match *next = next_rule_in_list__(rule);
1788 return next->priority < rule->priority ? next : NULL;
1791 /* A longest-prefix match tree. */
1793 uint32_t prefix; /* Prefix bits for this node, MSB first. */
1794 uint8_t n_bits; /* Never zero, except for the root node. */
1795 unsigned int n_rules; /* Number of rules that have this prefix. */
1796 rcu_trie_ptr edges[2]; /* Both NULL if leaf. */
1799 /* Max bits per node. Must fit in struct trie_node's 'prefix'.
1800 * Also tested with 16, 8, and 5 to stress the implementation. */
1801 #define TRIE_PREFIX_BITS 32
1803 /* Return at least 'plen' bits of the 'prefix', starting at bit offset 'ofs'.
1804 * Prefixes are in the network byte order, and the offset 0 corresponds to
1805 * the most significant bit of the first byte. The offset can be read as
1806 * "how many bits to skip from the start of the prefix starting at 'pr'". */
1808 raw_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1812 pr += ofs / 32; /* Where to start. */
1813 ofs %= 32; /* How many bits to skip at 'pr'. */
1815 prefix = ntohl(*pr) << ofs; /* Get the first 32 - ofs bits. */
1816 if (plen > 32 - ofs) { /* Need more than we have already? */
1817 prefix |= ntohl(*++pr) >> (32 - ofs);
1819 /* Return with possible unwanted bits at the end. */
1823 /* Return min(TRIE_PREFIX_BITS, plen) bits of the 'prefix', starting at bit
1824 * offset 'ofs'. Prefixes are in the network byte order, and the offset 0
1825 * corresponds to the most significant bit of the first byte. The offset can
1826 * be read as "how many bits to skip from the start of the prefix starting at
1829 trie_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1834 if (plen > TRIE_PREFIX_BITS) {
1835 plen = TRIE_PREFIX_BITS; /* Get at most TRIE_PREFIX_BITS. */
1837 /* Return with unwanted bits cleared. */
1838 return raw_get_prefix(pr, ofs, plen) & ~0u << (32 - plen);
1841 /* Return the number of equal bits in 'n_bits' of 'prefix's MSBs and a 'value'
1842 * starting at "MSB 0"-based offset 'ofs'. */
1844 prefix_equal_bits(uint32_t prefix, unsigned int n_bits, const ovs_be32 value[],
1847 uint64_t diff = prefix ^ raw_get_prefix(value, ofs, n_bits);
1848 /* Set the bit after the relevant bits to limit the result. */
1849 return raw_clz64(diff << 32 | UINT64_C(1) << (63 - n_bits));
1852 /* Return the number of equal bits in 'node' prefix and a 'prefix' of length
1853 * 'plen', starting at "MSB 0"-based offset 'ofs'. */
1855 trie_prefix_equal_bits(const struct trie_node *node, const ovs_be32 prefix[],
1856 unsigned int ofs, unsigned int plen)
1858 return prefix_equal_bits(node->prefix, MIN(node->n_bits, plen - ofs),
1862 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' can
1863 * be greater than 31. */
1865 be_get_bit_at(const ovs_be32 value[], unsigned int ofs)
1867 return (((const uint8_t *)value)[ofs / 8] >> (7 - ofs % 8)) & 1u;
1870 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' must
1871 * be between 0 and 31, inclusive. */
1873 get_bit_at(const uint32_t prefix, unsigned int ofs)
1875 return (prefix >> (31 - ofs)) & 1u;
1878 /* Create new branch. */
1879 static struct trie_node *
1880 trie_branch_create(const ovs_be32 *prefix, unsigned int ofs, unsigned int plen,
1881 unsigned int n_rules)
1883 struct trie_node *node = xmalloc(sizeof *node);
1885 node->prefix = trie_get_prefix(prefix, ofs, plen);
1887 if (plen <= TRIE_PREFIX_BITS) {
1888 node->n_bits = plen;
1889 ovsrcu_set_hidden(&node->edges[0], NULL);
1890 ovsrcu_set_hidden(&node->edges[1], NULL);
1891 node->n_rules = n_rules;
1892 } else { /* Need intermediate nodes. */
1893 struct trie_node *subnode = trie_branch_create(prefix,
1894 ofs + TRIE_PREFIX_BITS,
1895 plen - TRIE_PREFIX_BITS,
1897 int bit = get_bit_at(subnode->prefix, 0);
1898 node->n_bits = TRIE_PREFIX_BITS;
1899 ovsrcu_set_hidden(&node->edges[bit], subnode);
1900 ovsrcu_set_hidden(&node->edges[!bit], NULL);
1907 trie_node_destroy(const struct trie_node *node)
1909 ovsrcu_postpone(free, CONST_CAST(struct trie_node *, node));
1912 /* Copy a trie node for modification and postpone delete the old one. */
1913 static struct trie_node *
1914 trie_node_rcu_realloc(const struct trie_node *node)
1916 struct trie_node *new_node = xmalloc(sizeof *node);
1919 trie_node_destroy(node);
1924 /* May only be called while holding the classifier mutex. */
1926 trie_destroy(rcu_trie_ptr *trie)
1928 struct trie_node *node = ovsrcu_get_protected(struct trie_node *, trie);
1931 ovsrcu_set_hidden(trie, NULL);
1932 trie_destroy(&node->edges[0]);
1933 trie_destroy(&node->edges[1]);
1934 trie_node_destroy(node);
1939 trie_is_leaf(const struct trie_node *trie)
1942 return !ovsrcu_get(struct trie_node *, &trie->edges[0])
1943 && !ovsrcu_get(struct trie_node *, &trie->edges[1]);
1947 mask_set_prefix_bits(struct flow_wildcards *wc, uint8_t be32ofs,
1948 unsigned int n_bits)
1950 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1953 for (i = 0; i < n_bits / 32; i++) {
1954 mask[i] = OVS_BE32_MAX;
1957 mask[i] |= htonl(~0u << (32 - n_bits % 32));
1962 mask_prefix_bits_set(const struct flow_wildcards *wc, uint8_t be32ofs,
1963 unsigned int n_bits)
1965 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1967 ovs_be32 zeroes = 0;
1969 for (i = 0; i < n_bits / 32; i++) {
1973 zeroes |= ~mask[i] & htonl(~0u << (32 - n_bits % 32));
1976 return !zeroes; /* All 'n_bits' bits set. */
1979 static rcu_trie_ptr *
1980 trie_next_edge(struct trie_node *node, const ovs_be32 value[],
1983 return node->edges + be_get_bit_at(value, ofs);
1986 static const struct trie_node *
1987 trie_next_node(const struct trie_node *node, const ovs_be32 value[],
1990 return ovsrcu_get(struct trie_node *,
1991 &node->edges[be_get_bit_at(value, ofs)]);
1994 /* Set the bit at ("MSB 0"-based) offset 'ofs'. 'ofs' can be greater than 31.
1997 be_set_bit_at(ovs_be32 value[], unsigned int ofs)
1999 ((uint8_t *)value)[ofs / 8] |= 1u << (7 - ofs % 8);
2002 /* Returns the number of bits in the prefix mask necessary to determine a
2003 * mismatch, in case there are longer prefixes in the tree below the one that
2005 * '*plens' will have a bit set for each prefix length that may have matching
2006 * rules. The caller is responsible for clearing the '*plens' prior to
2010 trie_lookup_value(const rcu_trie_ptr *trie, const ovs_be32 value[],
2011 ovs_be32 plens[], unsigned int n_bits)
2013 const struct trie_node *prev = NULL;
2014 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
2015 unsigned int match_len = 0; /* Number of matching bits. */
2017 for (; node; prev = node, node = trie_next_node(node, value, match_len)) {
2018 unsigned int eqbits;
2019 /* Check if this edge can be followed. */
2020 eqbits = prefix_equal_bits(node->prefix, node->n_bits, value,
2022 match_len += eqbits;
2023 if (eqbits < node->n_bits) { /* Mismatch, nothing more to be found. */
2024 /* Bit at offset 'match_len' differed. */
2025 return match_len + 1; /* Includes the first mismatching bit. */
2027 /* Full match, check if rules exist at this prefix length. */
2028 if (node->n_rules > 0) {
2029 be_set_bit_at(plens, match_len - 1);
2031 if (match_len >= n_bits) {
2032 return n_bits; /* Full prefix. */
2035 /* node == NULL. Full match so far, but we tried to follow an
2036 * non-existing branch. Need to exclude the other branch if it exists
2037 * (it does not if we were called on an empty trie or 'prev' is a leaf
2039 return !prev || trie_is_leaf(prev) ? match_len : match_len + 1;
2043 trie_lookup(const struct cls_trie *trie, const struct flow *flow,
2044 union mf_value *plens)
2046 const struct mf_field *mf = trie->field;
2048 /* Check that current flow matches the prerequisites for the trie
2049 * field. Some match fields are used for multiple purposes, so we
2050 * must check that the trie is relevant for this flow. */
2051 if (mf_are_prereqs_ok(mf, flow)) {
2052 return trie_lookup_value(&trie->root,
2053 &((ovs_be32 *)flow)[mf->flow_be32ofs],
2054 &plens->be32, mf->n_bits);
2056 memset(plens, 0xff, sizeof *plens); /* All prefixes, no skipping. */
2057 return 0; /* Value not used in this case. */
2060 /* Returns the length of a prefix match mask for the field 'mf' in 'minimask'.
2061 * Returns the u32 offset to the miniflow data in '*miniflow_index', if
2062 * 'miniflow_index' is not NULL. */
2064 minimask_get_prefix_len(const struct minimask *minimask,
2065 const struct mf_field *mf)
2067 unsigned int n_bits = 0, mask_tz = 0; /* Non-zero when end of mask seen. */
2068 uint8_t u32_ofs = mf->flow_be32ofs;
2069 uint8_t u32_end = u32_ofs + mf->n_bytes / 4;
2071 for (; u32_ofs < u32_end; ++u32_ofs) {
2073 mask = ntohl((OVS_FORCE ovs_be32)minimask_get(minimask, u32_ofs));
2075 /* Validate mask, count the mask length. */
2078 return 0; /* No bits allowed after mask ended. */
2081 if (~mask & (~mask + 1)) {
2082 return 0; /* Mask not contiguous. */
2084 mask_tz = ctz32(mask);
2085 n_bits += 32 - mask_tz;
2093 * This is called only when mask prefix is known to be CIDR and non-zero.
2094 * Relies on the fact that the flow and mask have the same map, and since
2095 * the mask is CIDR, the storage for the flow field exists even if it
2096 * happened to be zeros.
2098 static const ovs_be32 *
2099 minimatch_get_prefix(const struct minimatch *match, const struct mf_field *mf)
2101 return miniflow_get_be32_values(&match->flow) +
2102 count_1bits(match->flow.map & ((UINT64_C(1) << mf->flow_be32ofs) - 1));
2105 /* Insert rule in to the prefix tree.
2106 * 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2109 trie_insert(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2111 trie_insert_prefix(&trie->root,
2112 minimatch_get_prefix(&rule->match, trie->field), mlen);
2116 trie_insert_prefix(rcu_trie_ptr *edge, const ovs_be32 *prefix, int mlen)
2118 struct trie_node *node;
2121 /* Walk the tree. */
2122 for (; (node = ovsrcu_get_protected(struct trie_node *, edge));
2123 edge = trie_next_edge(node, prefix, ofs)) {
2124 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2126 if (eqbits < node->n_bits) {
2127 /* Mismatch, new node needs to be inserted above. */
2128 int old_branch = get_bit_at(node->prefix, eqbits);
2129 struct trie_node *new_parent;
2131 new_parent = trie_branch_create(prefix, ofs - eqbits, eqbits,
2132 ofs == mlen ? 1 : 0);
2133 /* Copy the node to modify it. */
2134 node = trie_node_rcu_realloc(node);
2135 /* Adjust the new node for its new position in the tree. */
2136 node->prefix <<= eqbits;
2137 node->n_bits -= eqbits;
2138 ovsrcu_set_hidden(&new_parent->edges[old_branch], node);
2140 /* Check if need a new branch for the new rule. */
2142 ovsrcu_set_hidden(&new_parent->edges[!old_branch],
2143 trie_branch_create(prefix, ofs, mlen - ofs,
2146 ovsrcu_set(edge, new_parent); /* Publish changes. */
2149 /* Full match so far. */
2152 /* Full match at the current node, rule needs to be added here. */
2157 /* Must insert a new tree branch for the new rule. */
2158 ovsrcu_set(edge, trie_branch_create(prefix, ofs, mlen - ofs, 1));
2161 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2164 trie_remove(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2166 trie_remove_prefix(&trie->root,
2167 minimatch_get_prefix(&rule->match, trie->field), mlen);
2170 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2173 trie_remove_prefix(rcu_trie_ptr *root, const ovs_be32 *prefix, int mlen)
2175 struct trie_node *node;
2176 rcu_trie_ptr *edges[sizeof(union mf_value) * 8];
2177 int depth = 0, ofs = 0;
2179 /* Walk the tree. */
2180 for (edges[0] = root;
2181 (node = ovsrcu_get_protected(struct trie_node *, edges[depth]));
2182 edges[++depth] = trie_next_edge(node, prefix, ofs)) {
2183 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2185 if (eqbits < node->n_bits) {
2186 /* Mismatch, nothing to be removed. This should never happen, as
2187 * only rules in the classifier are ever removed. */
2188 break; /* Log a warning. */
2190 /* Full match so far. */
2194 /* Full prefix match at the current node, remove rule here. */
2195 if (!node->n_rules) {
2196 break; /* Log a warning. */
2200 /* Check if can prune the tree. */
2201 while (!node->n_rules) {
2202 struct trie_node *next,
2203 *edge0 = ovsrcu_get_protected(struct trie_node *,
2205 *edge1 = ovsrcu_get_protected(struct trie_node *,
2208 if (edge0 && edge1) {
2209 break; /* A branching point, cannot prune. */
2212 /* Else have at most one child node, remove this node. */
2213 next = edge0 ? edge0 : edge1;
2216 if (node->n_bits + next->n_bits > TRIE_PREFIX_BITS) {
2217 break; /* Cannot combine. */
2219 next = trie_node_rcu_realloc(next); /* Modify. */
2221 /* Combine node with next. */
2222 next->prefix = node->prefix | next->prefix >> node->n_bits;
2223 next->n_bits += node->n_bits;
2225 /* Update the parent's edge. */
2226 ovsrcu_set(edges[depth], next); /* Publish changes. */
2227 trie_node_destroy(node);
2229 if (next || !depth) {
2230 /* Branch not pruned or at root, nothing more to do. */
2233 node = ovsrcu_get_protected(struct trie_node *,
2239 /* Cannot go deeper. This should never happen, since only rules
2240 * that actually exist in the classifier are ever removed. */
2241 VLOG_WARN("Trying to remove non-existing rule from a prefix trie.");