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 unsigned int *checkbits);
155 static unsigned int trie_lookup_value(const rcu_trie_ptr *,
156 const ovs_be32 value[],
157 unsigned int value_bits,
158 unsigned int *checkbits);
159 static void trie_destroy(rcu_trie_ptr *);
160 static void trie_insert(struct cls_trie *, const struct cls_rule *, int mlen);
161 static void trie_insert_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
163 static void trie_remove(struct cls_trie *, const struct cls_rule *, int mlen);
164 static void trie_remove_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
166 static void mask_set_prefix_bits(struct flow_wildcards *, uint8_t be32ofs,
167 unsigned int n_bits);
168 static bool mask_prefix_bits_set(const struct flow_wildcards *,
169 uint8_t be32ofs, unsigned int n_bits);
171 /* flow/miniflow/minimask/minimatch utilities.
172 * These are only used by the classifier, so place them here to allow
173 * for better optimization. */
175 static inline uint64_t
176 miniflow_get_map_in_range(const struct miniflow *miniflow,
177 uint8_t start, uint8_t end, unsigned int *offset)
179 uint64_t map = miniflow->map;
183 uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
184 *offset = count_1bits(map & msk);
187 if (end < FLOW_U32S) {
188 uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
194 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
195 * 'mask', given 'basis'.
197 * The hash values returned by this function are the same as those returned by
198 * miniflow_hash_in_minimask(), only the form of the arguments differ. */
199 static inline uint32_t
200 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
203 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
204 const uint32_t *flow_u32 = (const uint32_t *)flow;
205 const uint32_t *p = mask_values;
210 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
211 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
214 return hash_finish(hash, (p - mask_values) * 4);
217 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
218 * 'mask', given 'basis'.
220 * The hash values returned by this function are the same as those returned by
221 * flow_hash_in_minimask(), only the form of the arguments differ. */
222 static inline uint32_t
223 miniflow_hash_in_minimask(const struct miniflow *flow,
224 const struct minimask *mask, uint32_t basis)
226 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
227 const uint32_t *p = mask_values;
228 uint32_t hash = basis;
231 MINIFLOW_FOR_EACH_IN_MAP(flow_u32, flow, mask->masks.map) {
232 hash = hash_add(hash, flow_u32 & *p++);
235 return hash_finish(hash, (p - mask_values) * 4);
238 /* Returns a hash value for the bits of range [start, end) in 'flow',
239 * where there are 1-bits in 'mask', given 'hash'.
241 * The hash values returned by this function are the same as those returned by
242 * minimatch_hash_range(), only the form of the arguments differ. */
243 static inline uint32_t
244 flow_hash_in_minimask_range(const struct flow *flow,
245 const struct minimask *mask,
246 uint8_t start, uint8_t end, uint32_t *basis)
248 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
249 const uint32_t *flow_u32 = (const uint32_t *)flow;
251 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
253 const uint32_t *p = mask_values + offset;
254 uint32_t hash = *basis;
256 for (; map; map = zero_rightmost_1bit(map)) {
257 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
260 *basis = hash; /* Allow continuation from the unfinished value. */
261 return hash_finish(hash, (p - mask_values) * 4);
264 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
266 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
267 const struct minimask *mask)
269 flow_union_with_miniflow(&wc->masks, &mask->masks);
272 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
273 * in range [start, end). */
275 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
276 const struct minimask *mask,
277 uint8_t start, uint8_t end)
279 uint32_t *dst_u32 = (uint32_t *)&wc->masks;
281 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
283 const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
285 for (; map; map = zero_rightmost_1bit(map)) {
286 dst_u32[raw_ctz(map)] |= *p++;
290 /* Returns a hash value for 'flow', given 'basis'. */
291 static inline uint32_t
292 miniflow_hash(const struct miniflow *flow, uint32_t basis)
294 const uint32_t *values = miniflow_get_u32_values(flow);
295 const uint32_t *p = values;
296 uint32_t hash = basis;
297 uint64_t hash_map = 0;
300 for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
302 hash = hash_add(hash, *p);
303 hash_map |= rightmost_1bit(map);
307 hash = hash_add(hash, hash_map);
308 hash = hash_add(hash, hash_map >> 32);
310 return hash_finish(hash, p - values);
313 /* Returns a hash value for 'mask', given 'basis'. */
314 static inline uint32_t
315 minimask_hash(const struct minimask *mask, uint32_t basis)
317 return miniflow_hash(&mask->masks, basis);
320 /* Returns a hash value for 'match', given 'basis'. */
321 static inline uint32_t
322 minimatch_hash(const struct minimatch *match, uint32_t basis)
324 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
327 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
330 * The hash values returned by this function are the same as those returned by
331 * flow_hash_in_minimask_range(), only the form of the arguments differ. */
332 static inline uint32_t
333 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
337 const uint32_t *p, *q;
338 uint32_t hash = *basis;
341 n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
343 q = miniflow_get_u32_values(&match->mask.masks) + offset;
344 p = miniflow_get_u32_values(&match->flow) + offset;
346 for (i = 0; i < n; i++) {
347 hash = hash_add(hash, p[i] & q[i]);
349 *basis = hash; /* Allow continuation from the unfinished value. */
350 return hash_finish(hash, (offset + n) * 4);
356 /* Initializes 'rule' to match packets specified by 'match' at the given
357 * 'priority'. 'match' must satisfy the invariant described in the comment at
358 * the definition of struct match.
360 * The caller must eventually destroy 'rule' with cls_rule_destroy().
362 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
363 * internally Open vSwitch supports a wider range.) */
365 cls_rule_init(struct cls_rule *rule,
366 const struct match *match, unsigned int priority)
368 minimatch_init(&rule->match, match);
369 rule->priority = priority;
370 rule->cls_match = NULL;
373 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
375 cls_rule_init_from_minimatch(struct cls_rule *rule,
376 const struct minimatch *match,
377 unsigned int priority)
379 minimatch_clone(&rule->match, match);
380 rule->priority = priority;
381 rule->cls_match = NULL;
384 /* Initializes 'dst' as a copy of 'src'.
386 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
388 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
390 minimatch_clone(&dst->match, &src->match);
391 dst->priority = src->priority;
392 dst->cls_match = NULL;
395 /* Initializes 'dst' with the data in 'src', destroying 'src'.
397 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
399 cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
401 minimatch_move(&dst->match, &src->match);
402 dst->priority = src->priority;
403 dst->cls_match = NULL;
406 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
407 * normally embedded into a larger structure).
409 * ('rule' must not currently be in a classifier.) */
411 cls_rule_destroy(struct cls_rule *rule)
413 ovs_assert(!rule->cls_match);
414 minimatch_destroy(&rule->match);
417 /* Returns true if 'a' and 'b' match the same packets at the same priority,
418 * false if they differ in some way. */
420 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
422 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
425 /* Returns a hash value for 'rule', folding in 'basis'. */
427 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
429 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
432 /* Appends a string describing 'rule' to 's'. */
434 cls_rule_format(const struct cls_rule *rule, struct ds *s)
436 minimatch_format(&rule->match, s, rule->priority);
439 /* Returns true if 'rule' matches every packet, false otherwise. */
441 cls_rule_is_catchall(const struct cls_rule *rule)
443 return minimask_is_catchall(&rule->match.mask);
446 /* Initializes 'cls' as a classifier that initially contains no classification
449 classifier_init(struct classifier *cls, const uint8_t *flow_segments)
450 OVS_EXCLUDED(cls->mutex)
452 ovs_mutex_init(&cls->mutex);
453 ovs_mutex_lock(&cls->mutex);
455 cmap_init(&cls->subtables_map);
456 pvector_init(&cls->subtables);
457 cmap_init(&cls->partitions);
458 cls->n_flow_segments = 0;
460 while (cls->n_flow_segments < CLS_MAX_INDICES
461 && *flow_segments < FLOW_U32S) {
462 cls->flow_segments[cls->n_flow_segments++] = *flow_segments++;
466 for (int i = 0; i < CLS_MAX_TRIES; i++) {
467 trie_init(cls, i, NULL);
469 ovs_mutex_unlock(&cls->mutex);
472 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
473 * caller's responsibility.
474 * May only be called after all the readers have been terminated. */
476 classifier_destroy(struct classifier *cls)
477 OVS_EXCLUDED(cls->mutex)
480 struct cls_partition *partition, *next_partition;
481 struct cls_subtable *subtable, *next_subtable;
484 ovs_mutex_lock(&cls->mutex);
485 for (i = 0; i < cls->n_tries; i++) {
486 trie_destroy(&cls->tries[i].root);
489 CMAP_FOR_EACH_SAFE (subtable, next_subtable, cmap_node,
490 &cls->subtables_map) {
491 destroy_subtable(cls, subtable);
493 cmap_destroy(&cls->subtables_map);
495 CMAP_FOR_EACH_SAFE (partition, next_partition, cmap_node,
497 ovsrcu_postpone(free, partition);
499 cmap_destroy(&cls->partitions);
501 pvector_destroy(&cls->subtables);
502 ovs_mutex_unlock(&cls->mutex);
503 ovs_mutex_destroy(&cls->mutex);
507 /* We use uint64_t as a set for the fields below. */
508 BUILD_ASSERT_DECL(MFF_N_IDS <= 64);
510 /* Set the fields for which prefix lookup should be performed. */
512 classifier_set_prefix_fields(struct classifier *cls,
513 const enum mf_field_id *trie_fields,
514 unsigned int n_fields)
515 OVS_EXCLUDED(cls->mutex)
518 const struct mf_field * new_fields[CLS_MAX_TRIES];
520 bool changed = false;
522 ovs_mutex_lock(&cls->mutex);
523 for (i = 0; i < n_fields && n_tries < CLS_MAX_TRIES; i++) {
524 const struct mf_field *field = mf_from_id(trie_fields[i]);
525 if (field->flow_be32ofs < 0 || field->n_bits % 32) {
526 /* Incompatible field. This is the only place where we
527 * enforce these requirements, but the rest of the trie code
528 * depends on the flow_be32ofs to be non-negative and the
529 * field length to be a multiple of 32 bits. */
533 if (fields & (UINT64_C(1) << trie_fields[i])) {
534 /* Duplicate field, there is no need to build more than
535 * one index for any one field. */
538 fields |= UINT64_C(1) << trie_fields[i];
540 new_fields[n_tries] = NULL;
541 if (n_tries >= cls->n_tries || field != cls->tries[n_tries].field) {
542 new_fields[n_tries] = field;
548 if (changed || n_tries < cls->n_tries) {
549 struct cls_subtable *subtable;
551 /* Trie configuration needs to change. Disable trie lookups
552 * for the tries that are changing and wait all the current readers
553 * with the old configuration to be done. */
555 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
556 for (i = 0; i < cls->n_tries; i++) {
557 if ((i < n_tries && new_fields[i]) || i >= n_tries) {
558 if (subtable->trie_plen[i]) {
559 subtable->trie_plen[i] = 0;
565 /* Synchronize if any readers were using tries. The readers may
566 * temporarily function without the trie lookup based optimizations. */
568 /* ovsrcu_synchronize() functions as a memory barrier, so it does
569 * not matter that subtable->trie_plen is not atomic. */
570 ovsrcu_synchronize();
573 /* Now set up the tries. */
574 for (i = 0; i < n_tries; i++) {
576 trie_init(cls, i, new_fields[i]);
579 /* Destroy the rest, if any. */
580 for (; i < cls->n_tries; i++) {
581 trie_init(cls, i, NULL);
584 cls->n_tries = n_tries;
585 ovs_mutex_unlock(&cls->mutex);
589 ovs_mutex_unlock(&cls->mutex);
590 return false; /* No change. */
594 trie_init(struct classifier *cls, int trie_idx, const struct mf_field *field)
595 OVS_REQUIRES(cls->mutex)
597 struct cls_trie *trie = &cls->tries[trie_idx];
598 struct cls_subtable *subtable;
600 if (trie_idx < cls->n_tries) {
601 trie_destroy(&trie->root);
603 ovsrcu_set_hidden(&trie->root, NULL);
607 /* Add existing rules to the new trie. */
608 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
611 plen = field ? minimask_get_prefix_len(&subtable->mask, field) : 0;
613 struct cls_match *head;
615 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
616 struct cls_match *match;
618 FOR_EACH_RULE_IN_LIST (match, head) {
619 trie_insert(trie, match->cls_rule, plen);
623 /* Initialize subtable's prefix length on this field. This will
624 * allow readers to use the trie. */
625 atomic_thread_fence(memory_order_release);
626 subtable->trie_plen[trie_idx] = plen;
630 /* Returns true if 'cls' contains no classification rules, false otherwise.
631 * Checking the cmap requires no locking. */
633 classifier_is_empty(const struct classifier *cls)
635 return cmap_is_empty(&cls->subtables_map);
638 /* Returns the number of rules in 'cls'. */
640 classifier_count(const struct classifier *cls)
641 OVS_NO_THREAD_SAFETY_ANALYSIS
643 /* n_rules is an int, so in the presence of concurrent writers this will
644 * return either the old or a new value. */
649 hash_metadata(ovs_be64 metadata_)
651 uint64_t metadata = (OVS_FORCE uint64_t) metadata_;
652 return hash_uint64(metadata);
655 static struct cls_partition *
656 find_partition(const struct classifier *cls, ovs_be64 metadata, uint32_t hash)
658 struct cls_partition *partition;
660 CMAP_FOR_EACH_WITH_HASH (partition, cmap_node, hash, &cls->partitions) {
661 if (partition->metadata == metadata) {
669 static struct cls_partition *
670 create_partition(struct classifier *cls, struct cls_subtable *subtable,
672 OVS_REQUIRES(cls->mutex)
674 uint32_t hash = hash_metadata(metadata);
675 struct cls_partition *partition = find_partition(cls, metadata, hash);
677 partition = xmalloc(sizeof *partition);
678 partition->metadata = metadata;
680 tag_tracker_init(&partition->tracker);
681 cmap_insert(&cls->partitions, &partition->cmap_node, hash);
683 tag_tracker_add(&partition->tracker, &partition->tags, subtable->tag);
687 static inline ovs_be32 minimatch_get_ports(const struct minimatch *match)
689 /* Could optimize to use the same map if needed for fast path. */
690 return MINIFLOW_GET_BE32(&match->flow, tp_src)
691 & MINIFLOW_GET_BE32(&match->mask.masks, tp_src);
694 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
695 * must not modify or free it.
697 * If 'cls' already contains an identical rule (including wildcards, values of
698 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
699 * rule that was replaced. The caller takes ownership of the returned rule and
700 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
701 * memory block in which it resides, etc., as necessary.
703 * Returns NULL if 'cls' does not contain a rule with an identical key, after
704 * inserting the new rule. In this case, no rules are displaced by the new
705 * rule, even rules that cannot have any effect because the new rule matches a
706 * superset of their flows and has higher priority. */
708 classifier_replace(struct classifier *cls, struct cls_rule *rule)
709 OVS_EXCLUDED(cls->mutex)
711 struct cls_match *old_rule;
712 struct cls_subtable *subtable;
713 struct cls_rule *old_cls_rule = NULL;
715 ovs_mutex_lock(&cls->mutex);
716 subtable = find_subtable(cls, &rule->match.mask);
718 subtable = insert_subtable(cls, &rule->match.mask);
721 old_rule = insert_rule(cls, subtable, rule);
725 rule->cls_match->partition = NULL;
726 if (minimask_get_metadata_mask(&rule->match.mask) == OVS_BE64_MAX) {
727 ovs_be64 metadata = miniflow_get_metadata(&rule->match.flow);
728 rule->cls_match->partition = create_partition(cls, subtable,
734 for (int i = 0; i < cls->n_tries; i++) {
735 if (subtable->trie_plen[i]) {
736 trie_insert(&cls->tries[i], rule, subtable->trie_plen[i]);
741 if (subtable->ports_mask_len) {
742 /* We mask the value to be inserted to always have the wildcarded
743 * bits in known (zero) state, so we can include them in comparison
744 * and they will always match (== their original value does not
746 ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
748 trie_insert_prefix(&subtable->ports_trie, &masked_ports,
749 subtable->ports_mask_len);
752 old_cls_rule = old_rule->cls_rule;
753 rule->cls_match->partition = old_rule->partition;
754 old_cls_rule->cls_match = NULL;
756 /* 'old_rule' contains a cmap_node, which may not be freed
758 ovsrcu_postpone(free, old_rule);
760 ovs_mutex_unlock(&cls->mutex);
764 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
765 * must not modify or free it.
767 * 'cls' must not contain an identical rule (including wildcards, values of
768 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
771 classifier_insert(struct classifier *cls, struct cls_rule *rule)
773 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
774 ovs_assert(!displaced_rule);
777 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
778 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
779 * resides, etc., as necessary. */
781 classifier_remove(struct classifier *cls, struct cls_rule *rule)
782 OVS_EXCLUDED(cls->mutex)
784 struct cls_partition *partition;
785 struct cls_match *cls_match = rule->cls_match;
786 struct cls_match *head;
787 struct cls_subtable *subtable;
789 uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
790 uint8_t prev_be32ofs = 0;
792 ovs_assert(cls_match);
794 ovs_mutex_lock(&cls->mutex);
795 subtable = find_subtable(cls, &rule->match.mask);
796 ovs_assert(subtable);
798 if (subtable->ports_mask_len) {
799 ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
801 trie_remove_prefix(&subtable->ports_trie,
802 &masked_ports, subtable->ports_mask_len);
804 for (i = 0; i < cls->n_tries; i++) {
805 if (subtable->trie_plen[i]) {
806 trie_remove(&cls->tries[i], rule, subtable->trie_plen[i]);
810 /* Remove rule node from indices. */
811 for (i = 0; i < subtable->n_indices; i++) {
812 ihash[i] = minimatch_hash_range(&rule->match, prev_be32ofs,
813 subtable->index_ofs[i], &basis);
814 cmap_remove(&subtable->indices[i], &cls_match->index_nodes[i],
816 prev_be32ofs = subtable->index_ofs[i];
818 hash = minimatch_hash_range(&rule->match, prev_be32ofs, FLOW_U32S, &basis);
820 head = find_equal(subtable, &rule->match.flow, hash);
821 if (head != cls_match) {
822 list_remove(&cls_match->list);
823 } else if (list_is_empty(&cls_match->list)) {
824 cmap_remove(&subtable->rules, &cls_match->cmap_node, hash);
826 struct cls_match *next = CONTAINER_OF(cls_match->list.next,
827 struct cls_match, list);
829 list_remove(&cls_match->list);
830 cmap_replace(&subtable->rules, &cls_match->cmap_node,
831 &next->cmap_node, hash);
834 partition = cls_match->partition;
836 tag_tracker_subtract(&partition->tracker, &partition->tags,
838 if (!partition->tags) {
839 cmap_remove(&cls->partitions, &partition->cmap_node,
840 hash_metadata(partition->metadata));
841 ovsrcu_postpone(free, partition);
845 if (--subtable->n_rules == 0) {
846 destroy_subtable(cls, subtable);
847 } else if (subtable->max_priority == cls_match->priority
848 && --subtable->max_count == 0) {
849 /* Find the new 'max_priority' and 'max_count'. */
850 struct cls_match *head;
851 unsigned int max_priority = 0;
853 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
854 if (head->priority > max_priority) {
855 max_priority = head->priority;
856 subtable->max_count = 1;
857 } else if (head->priority == max_priority) {
858 ++subtable->max_count;
861 subtable->max_priority = max_priority;
862 pvector_change_priority(&cls->subtables, subtable, max_priority);
867 rule->cls_match = NULL;
868 ovsrcu_postpone(free, cls_match);
869 ovs_mutex_unlock(&cls->mutex);
872 /* Prefix tree context. Valid when 'lookup_done' is true. Can skip all
873 * subtables which have more than 'match_plen' bits in their corresponding
874 * field at offset 'be32ofs'. If skipped, 'maskbits' prefix bits should be
875 * unwildcarded to quarantee datapath flow matches only packets it should. */
877 const struct cls_trie *trie;
878 bool lookup_done; /* Status of the lookup. */
879 uint8_t be32ofs; /* U32 offset of the field in question. */
880 unsigned int match_plen; /* Longest prefix than could possibly match. */
881 unsigned int maskbits; /* Prefix length needed to avoid false matches. */
885 trie_ctx_init(struct trie_ctx *ctx, const struct cls_trie *trie)
888 ctx->be32ofs = trie->field->flow_be32ofs;
889 ctx->lookup_done = false;
892 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
893 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
894 * of equal priority match 'flow', returns one arbitrarily.
896 * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
897 * set of bits that were significant in the lookup. At some point
898 * earlier, 'wc' should have been initialized (e.g., by
899 * flow_wildcards_init_catchall()). */
901 classifier_lookup(const struct classifier *cls, const struct flow *flow,
902 struct flow_wildcards *wc)
904 const struct cls_partition *partition;
906 int64_t best_priority = -1;
907 const struct cls_match *best;
908 struct trie_ctx trie_ctx[CLS_MAX_TRIES];
909 struct cls_subtable *subtable;
911 /* Synchronize for cls->n_tries and subtable->trie_plen. They can change
912 * when table configuration changes, which happens typically only on
914 atomic_thread_fence(memory_order_acquire);
916 /* Determine 'tags' such that, if 'subtable->tag' doesn't intersect them,
917 * then 'flow' cannot possibly match in 'subtable':
919 * - If flow->metadata maps to a given 'partition', then we can use
920 * 'tags' for 'partition->tags'.
922 * - If flow->metadata has no partition, then no rule in 'cls' has an
923 * exact-match for flow->metadata. That means that we don't need to
924 * search any subtable that includes flow->metadata in its mask.
926 * In either case, we always need to search any cls_subtables that do not
927 * include flow->metadata in its mask. One way to do that would be to
928 * check the "cls_subtable"s explicitly for that, but that would require an
929 * extra branch per subtable. Instead, we mark such a cls_subtable's
930 * 'tags' as TAG_ALL and make sure that 'tags' is never empty. This means
931 * that 'tags' always intersects such a cls_subtable's 'tags', so we don't
932 * need a special case.
934 partition = (cmap_is_empty(&cls->partitions)
936 : find_partition(cls, flow->metadata,
937 hash_metadata(flow->metadata)));
938 tags = partition ? partition->tags : TAG_ARBITRARY;
940 /* Initialize trie contexts for match_find_wc(). */
941 for (int i = 0; i < cls->n_tries; i++) {
942 trie_ctx_init(&trie_ctx[i], &cls->tries[i]);
946 PVECTOR_FOR_EACH_PRIORITY(subtable, best_priority, 2,
947 sizeof(struct cls_subtable), &cls->subtables) {
948 struct cls_match *rule;
950 if (!tag_intersects(tags, subtable->tag)) {
954 rule = find_match_wc(subtable, flow, trie_ctx, cls->n_tries, wc);
955 if (rule && (int64_t)rule->priority > best_priority) {
956 best_priority = (int64_t)rule->priority;
961 return best ? best->cls_rule : NULL;
964 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
965 * 'match' specifies a particular value has the correct value in 'target'.
967 * 'flow' and 'mask' have the same mask! */
969 miniflow_and_mask_matches_miniflow(const struct miniflow *flow,
970 const struct minimask *mask,
971 const struct miniflow *target)
973 const uint32_t *flowp = miniflow_get_u32_values(flow);
974 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
977 MINIFLOW_FOR_EACH_IN_MAP(target_u32, target, mask->masks.map) {
978 if ((*flowp++ ^ target_u32) & *maskp++) {
986 static inline struct cls_match *
987 find_match_miniflow(const struct cls_subtable *subtable,
988 const struct miniflow *flow,
991 struct cls_match *rule;
993 CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
994 if (miniflow_and_mask_matches_miniflow(&rule->flow, &subtable->mask,
1003 /* For each miniflow in 'flows' performs a classifier lookup writing the result
1004 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
1005 * NULL it is skipped.
1007 * This function is optimized for use in the userspace datapath and therefore
1008 * does not implement a lot of features available in the standard
1009 * classifier_lookup() function. Specifically, it does not implement
1010 * priorities, instead returning any rule which matches the flow. */
1012 classifier_lookup_miniflow_batch(const struct classifier *cls,
1013 const struct miniflow **flows,
1014 struct cls_rule **rules, size_t len)
1016 struct cls_subtable *subtable;
1017 size_t i, begin = 0;
1019 memset(rules, 0, len * sizeof *rules);
1020 PVECTOR_FOR_EACH (subtable, &cls->subtables) {
1021 for (i = begin; i < len; i++) {
1022 struct cls_match *match;
1025 if (OVS_UNLIKELY(rules[i] || !flows[i])) {
1029 hash = miniflow_hash_in_minimask(flows[i], &subtable->mask, 0);
1030 match = find_match_miniflow(subtable, flows[i], hash);
1031 if (OVS_UNLIKELY(match)) {
1032 rules[i] = match->cls_rule;
1036 while (begin < len && (rules[begin] || !flows[begin])) {
1045 /* Finds and returns a rule in 'cls' with exactly the same priority and
1046 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1047 * contain an exact match. */
1049 classifier_find_rule_exactly(const struct classifier *cls,
1050 const struct cls_rule *target)
1051 OVS_EXCLUDED(cls->mutex)
1053 struct cls_match *head, *rule;
1054 struct cls_subtable *subtable;
1056 ovs_mutex_lock(&cls->mutex);
1057 subtable = find_subtable(cls, &target->match.mask);
1062 /* Skip if there is no hope. */
1063 if (target->priority > subtable->max_priority) {
1067 head = find_equal(subtable, &target->match.flow,
1068 miniflow_hash_in_minimask(&target->match.flow,
1069 &target->match.mask, 0));
1070 FOR_EACH_RULE_IN_LIST (rule, head) {
1071 if (target->priority >= rule->priority) {
1072 ovs_mutex_unlock(&cls->mutex);
1073 return target->priority == rule->priority ? rule->cls_rule : NULL;
1077 ovs_mutex_unlock(&cls->mutex);
1081 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
1082 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1083 * contain an exact match. */
1085 classifier_find_match_exactly(const struct classifier *cls,
1086 const struct match *target,
1087 unsigned int priority)
1089 struct cls_rule *retval;
1092 cls_rule_init(&cr, target, priority);
1093 retval = classifier_find_rule_exactly(cls, &cr);
1094 cls_rule_destroy(&cr);
1099 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
1100 * considered to overlap if both rules have the same priority and a packet
1101 * could match both. */
1103 classifier_rule_overlaps(const struct classifier *cls,
1104 const struct cls_rule *target)
1105 OVS_EXCLUDED(cls->mutex)
1107 struct cls_subtable *subtable;
1108 int64_t stop_at_priority = (int64_t)target->priority - 1;
1110 ovs_mutex_lock(&cls->mutex);
1111 /* Iterate subtables in the descending max priority order. */
1112 PVECTOR_FOR_EACH_PRIORITY (subtable, stop_at_priority, 2,
1113 sizeof(struct cls_subtable), &cls->subtables) {
1114 uint32_t storage[FLOW_U32S];
1115 struct minimask mask;
1116 struct cls_match *head;
1118 minimask_combine(&mask, &target->match.mask, &subtable->mask, storage);
1119 CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
1120 struct cls_match *rule;
1122 FOR_EACH_RULE_IN_LIST (rule, head) {
1123 if (rule->priority < target->priority) {
1124 break; /* Rules in descending priority order. */
1126 if (rule->priority == target->priority
1127 && miniflow_equal_in_minimask(&target->match.flow,
1128 &rule->flow, &mask)) {
1129 ovs_mutex_unlock(&cls->mutex);
1136 ovs_mutex_unlock(&cls->mutex);
1140 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
1141 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
1142 * function returns true if, for every field:
1144 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
1147 * - 'criteria' wildcards the field,
1149 * Conversely, 'rule' does not match 'criteria' and this function returns false
1150 * if, for at least one field:
1152 * - 'criteria' and 'rule' specify different values for the field, or
1154 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
1156 * Equivalently, the truth table for whether a field matches is:
1161 * r +---------+---------+
1162 * i wild | yes | yes |
1164 * e +---------+---------+
1165 * r exact | no |if values|
1167 * a +---------+---------+
1169 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1170 * commands and by OpenFlow 1.0 aggregate and flow stats.
1172 * Ignores rule->priority. */
1174 cls_rule_is_loose_match(const struct cls_rule *rule,
1175 const struct minimatch *criteria)
1177 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
1178 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
1185 rule_matches(const struct cls_match *rule, const struct cls_rule *target)
1188 || miniflow_equal_in_minimask(&rule->flow,
1189 &target->match.flow,
1190 &target->match.mask));
1193 static struct cls_match *
1194 search_subtable(const struct cls_subtable *subtable,
1195 struct cls_cursor *cursor)
1198 || !minimask_has_extra(&subtable->mask, &cursor->target->match.mask)) {
1199 struct cls_match *rule;
1201 CMAP_CURSOR_FOR_EACH (rule, cmap_node, &cursor->rules,
1203 if (rule_matches(rule, cursor->target)) {
1211 /* Initializes 'cursor' for iterating through rules in 'cls', and returns the
1212 * first matching cls_rule via '*pnode', or NULL if there are no matches.
1214 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1216 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1217 * such that cls_rule_is_loose_match(rule, target) returns true.
1219 * Ignores target->priority. */
1220 struct cls_cursor cls_cursor_init(const struct classifier *cls,
1221 const struct cls_rule *target,
1222 void **pnode, const void *offset, bool safe)
1223 OVS_NO_THREAD_SAFETY_ANALYSIS
1225 struct cls_cursor cursor;
1226 struct cls_subtable *subtable;
1227 struct cls_rule *cls_rule = NULL;
1231 cursor.target = target && !cls_rule_is_catchall(target) ? target : NULL;
1233 /* Find first rule. */
1234 ovs_mutex_lock(&cursor.cls->mutex);
1235 CMAP_CURSOR_FOR_EACH (subtable, cmap_node, &cursor.subtables,
1236 &cursor.cls->subtables_map) {
1237 struct cls_match *rule = search_subtable(subtable, &cursor);
1240 cursor.subtable = subtable;
1241 cls_rule = rule->cls_rule;
1245 *pnode = (char *)cls_rule + (ptrdiff_t)offset;
1247 /* Leave locked if requested and have a rule. */
1248 if (safe || !cls_rule) {
1249 ovs_mutex_unlock(&cursor.cls->mutex);
1255 cls_cursor_next_unlock(struct cls_cursor *cursor, struct cls_rule *rule)
1256 OVS_NO_THREAD_SAFETY_ANALYSIS
1258 /* Release the mutex if no rule, or 'safe' mode. */
1259 if (!rule || cursor->safe) {
1260 ovs_mutex_unlock(&cursor->cls->mutex);
1264 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1265 * pointer if there are no more matches. */
1267 cls_cursor_next(struct cls_cursor *cursor, const struct cls_rule *rule_)
1268 OVS_NO_THREAD_SAFETY_ANALYSIS
1270 struct cls_match *rule = CONST_CAST(struct cls_match *, rule_->cls_match);
1271 const struct cls_subtable *subtable;
1272 struct cls_match *next;
1274 /* Lock if not locked already. */
1276 ovs_mutex_lock(&cursor->cls->mutex);
1279 next = next_rule_in_list__(rule);
1280 if (next->priority < rule->priority) {
1281 cls_cursor_next_unlock(cursor, next->cls_rule);
1282 return next->cls_rule;
1285 /* 'next' is the head of the list, that is, the rule that is included in
1286 * the subtable's map. (This is important when the classifier contains
1287 * rules that differ only in priority.) */
1289 CMAP_CURSOR_FOR_EACH_CONTINUE (rule, cmap_node, &cursor->rules) {
1290 if (rule_matches(rule, cursor->target)) {
1291 cls_cursor_next_unlock(cursor, rule->cls_rule);
1292 return rule->cls_rule;
1296 subtable = cursor->subtable;
1297 CMAP_CURSOR_FOR_EACH_CONTINUE (subtable, cmap_node, &cursor->subtables) {
1298 rule = search_subtable(subtable, cursor);
1300 cursor->subtable = subtable;
1301 cls_cursor_next_unlock(cursor, rule->cls_rule);
1302 return rule->cls_rule;
1306 ovs_mutex_unlock(&cursor->cls->mutex);
1310 static struct cls_subtable *
1311 find_subtable(const struct classifier *cls, const struct minimask *mask)
1312 OVS_REQUIRES(cls->mutex)
1314 struct cls_subtable *subtable;
1316 CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, minimask_hash(mask, 0),
1317 &cls->subtables_map) {
1318 if (minimask_equal(mask, &subtable->mask)) {
1325 /* The new subtable will be visible to the readers only after this. */
1326 static struct cls_subtable *
1327 insert_subtable(struct classifier *cls, const struct minimask *mask)
1328 OVS_REQUIRES(cls->mutex)
1330 uint32_t hash = minimask_hash(mask, 0);
1331 struct cls_subtable *subtable;
1333 struct flow_wildcards old, new;
1335 int count = count_1bits(mask->masks.map);
1337 subtable = xzalloc(sizeof *subtable - sizeof mask->masks.inline_values
1338 + MINIFLOW_VALUES_SIZE(count));
1339 cmap_init(&subtable->rules);
1340 miniflow_clone_inline(&subtable->mask.masks, &mask->masks, count);
1342 /* Init indices for segmented lookup, if any. */
1343 flow_wildcards_init_catchall(&new);
1346 for (i = 0; i < cls->n_flow_segments; i++) {
1347 flow_wildcards_fold_minimask_range(&new, mask, prev,
1348 cls->flow_segments[i]);
1349 /* Add an index if it adds mask bits. */
1350 if (!flow_wildcards_equal(&new, &old)) {
1351 cmap_init(&subtable->indices[index]);
1352 subtable->index_ofs[index] = cls->flow_segments[i];
1356 prev = cls->flow_segments[i];
1358 /* Check if the rest of the subtable's mask adds any bits,
1359 * and remove the last index if it doesn't. */
1361 flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
1362 if (flow_wildcards_equal(&new, &old)) {
1364 subtable->index_ofs[index] = 0;
1365 cmap_destroy(&subtable->indices[index]);
1368 subtable->n_indices = index;
1370 subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
1371 ? tag_create_deterministic(hash)
1374 for (i = 0; i < cls->n_tries; i++) {
1375 subtable->trie_plen[i] = minimask_get_prefix_len(mask,
1376 cls->tries[i].field);
1380 ovsrcu_set_hidden(&subtable->ports_trie, NULL);
1381 subtable->ports_mask_len
1382 = 32 - ctz32(ntohl(MINIFLOW_GET_BE32(&mask->masks, tp_src)));
1384 cmap_insert(&cls->subtables_map, &subtable->cmap_node, hash);
1390 destroy_subtable(struct classifier *cls, struct cls_subtable *subtable)
1391 OVS_REQUIRES(cls->mutex)
1395 pvector_remove(&cls->subtables, subtable);
1396 trie_destroy(&subtable->ports_trie);
1398 for (i = 0; i < subtable->n_indices; i++) {
1399 cmap_destroy(&subtable->indices[i]);
1401 cmap_remove(&cls->subtables_map, &subtable->cmap_node,
1402 minimask_hash(&subtable->mask, 0));
1403 minimask_destroy(&subtable->mask);
1404 cmap_destroy(&subtable->rules);
1405 ovsrcu_postpone(free, subtable);
1413 /* Return 'true' if can skip rest of the subtable based on the prefix trie
1414 * lookup results. */
1416 check_tries(struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1417 const unsigned int field_plen[CLS_MAX_TRIES],
1418 const struct range ofs, const struct flow *flow,
1419 struct flow_wildcards *wc)
1423 /* Check if we could avoid fully unwildcarding the next level of
1424 * fields using the prefix tries. The trie checks are done only as
1425 * needed to avoid folding in additional bits to the wildcards mask. */
1426 for (j = 0; j < n_tries; j++) {
1427 /* Is the trie field relevant for this subtable? */
1428 if (field_plen[j]) {
1429 struct trie_ctx *ctx = &trie_ctx[j];
1430 uint8_t be32ofs = ctx->be32ofs;
1432 /* Is the trie field within the current range of fields? */
1433 if (be32ofs >= ofs.start && be32ofs < ofs.end) {
1434 /* On-demand trie lookup. */
1435 if (!ctx->lookup_done) {
1436 ctx->match_plen = trie_lookup(ctx->trie, flow,
1438 ctx->lookup_done = true;
1440 /* Possible to skip the rest of the subtable if subtable's
1441 * prefix on the field is longer than what is known to match
1442 * based on the trie lookup. */
1443 if (field_plen[j] > ctx->match_plen) {
1444 /* RFC: We want the trie lookup to never result in
1445 * unwildcarding any bits that would not be unwildcarded
1446 * otherwise. Since the trie is shared by the whole
1447 * classifier, it is possible that the 'maskbits' contain
1448 * bits that are irrelevant for the partition of the
1449 * classifier relevant for the current flow. */
1451 /* Can skip if the field is already unwildcarded. */
1452 if (mask_prefix_bits_set(wc, be32ofs, ctx->maskbits)) {
1455 /* Check that the trie result will not unwildcard more bits
1456 * than this stage will. */
1457 if (ctx->maskbits <= field_plen[j]) {
1458 /* Unwildcard the bits and skip the rest. */
1459 mask_set_prefix_bits(wc, be32ofs, ctx->maskbits);
1460 /* Note: Prerequisite already unwildcarded, as the only
1461 * prerequisite of the supported trie lookup fields is
1462 * the ethertype, which is currently always
1474 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1475 * for which 'flow', for which 'mask' has a bit set, specifies a particular
1476 * value has the correct value in 'target'.
1478 * This function is equivalent to miniflow_equal_flow_in_minimask(flow,
1479 * target, mask) but this is faster because of the invariant that
1480 * flow->map and mask->masks.map are the same, and that this version
1481 * takes the 'wc'. */
1483 miniflow_and_mask_matches_flow(const struct miniflow *flow,
1484 const struct minimask *mask,
1485 const struct flow *target)
1487 const uint32_t *flowp = miniflow_get_u32_values(flow);
1488 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1491 MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1492 uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & *maskp++;
1502 static inline struct cls_match *
1503 find_match(const struct cls_subtable *subtable, const struct flow *flow,
1506 struct cls_match *rule;
1508 CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
1509 if (miniflow_and_mask_matches_flow(&rule->flow, &subtable->mask,
1518 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1519 * for which 'flow', for which 'mask' has a bit set, specifies a particular
1520 * value has the correct value in 'target'.
1522 * This function is equivalent to miniflow_and_mask_matches_flow() but this
1523 * version fills in the mask bits in 'wc'. */
1525 miniflow_and_mask_matches_flow_wc(const struct miniflow *flow,
1526 const struct minimask *mask,
1527 const struct flow *target,
1528 struct flow_wildcards *wc)
1530 const uint32_t *flowp = miniflow_get_u32_values(flow);
1531 const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1534 MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1535 uint32_t mask = *maskp++;
1536 uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & mask;
1539 /* Only unwildcard if none of the differing bits is already
1541 if (!(flow_u32_value(&wc->masks, idx) & diff)) {
1542 /* Keep one bit of the difference. */
1543 *flow_u32_lvalue(&wc->masks, idx) |= rightmost_1bit(diff);
1547 /* Fill in the bits that were looked at. */
1548 *flow_u32_lvalue(&wc->masks, idx) |= mask;
1554 /* Unwildcard the fields looked up so far, if any. */
1556 fill_range_wc(const struct cls_subtable *subtable, struct flow_wildcards *wc,
1560 flow_wildcards_fold_minimask_range(wc, &subtable->mask, 0, to);
1564 static struct cls_match *
1565 find_match_wc(const struct cls_subtable *subtable, const struct flow *flow,
1566 struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1567 struct flow_wildcards *wc)
1569 uint32_t basis = 0, hash;
1570 struct cls_match *rule;
1574 if (OVS_UNLIKELY(!wc)) {
1575 return find_match(subtable, flow,
1576 flow_hash_in_minimask(flow, &subtable->mask, 0));
1580 /* Try to finish early by checking fields in segments. */
1581 for (i = 0; i < subtable->n_indices; i++) {
1582 struct cmap_node *inode;
1584 ofs.end = subtable->index_ofs[i];
1586 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow,
1588 /* 'wc' bits for the trie field set, now unwildcard the preceding
1589 * bits used so far. */
1590 fill_range_wc(subtable, wc, ofs.start);
1593 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1595 inode = cmap_find(&subtable->indices[i], hash);
1597 /* No match, can stop immediately, but must fold in the bits
1598 * used in lookup so far. */
1599 fill_range_wc(subtable, wc, ofs.end);
1603 /* If we have narrowed down to a single rule already, check whether
1604 * that rule matches. Either way, we're done.
1606 * (Rare) hash collisions may cause us to miss the opportunity for this
1608 if (!cmap_node_next(inode)) {
1609 ASSIGN_CONTAINER(rule, inode - i, index_nodes);
1610 if (miniflow_and_mask_matches_flow_wc(&rule->flow, &subtable->mask,
1616 ofs.start = ofs.end;
1618 ofs.end = FLOW_U32S;
1619 /* Trie check for the final range. */
1620 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow, wc)) {
1621 fill_range_wc(subtable, wc, ofs.start);
1624 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1626 rule = find_match(subtable, flow, hash);
1627 if (!rule && subtable->ports_mask_len) {
1628 /* Ports are always part of the final range, if any.
1629 * No match was found for the ports. Use the ports trie to figure out
1630 * which ports bits to unwildcard. */
1632 ovs_be32 value, mask;
1634 mask = MINIFLOW_GET_BE32(&subtable->mask.masks, tp_src);
1635 value = ((OVS_FORCE ovs_be32 *)flow)[TP_PORTS_OFS32] & mask;
1636 trie_lookup_value(&subtable->ports_trie, &value, 32, &mbits);
1638 ((OVS_FORCE ovs_be32 *)&wc->masks)[TP_PORTS_OFS32] |=
1639 mask & htonl(~0 << (32 - mbits));
1641 /* Unwildcard all bits in the mask upto the ports, as they were used
1642 * to determine there is no match. */
1643 fill_range_wc(subtable, wc, TP_PORTS_OFS32);
1647 /* Must unwildcard all the fields, as they were looked at. */
1648 flow_wildcards_fold_minimask(wc, &subtable->mask);
1652 static struct cls_match *
1653 find_equal(struct cls_subtable *subtable, const struct miniflow *flow,
1656 struct cls_match *head;
1658 CMAP_FOR_EACH_WITH_HASH (head, cmap_node, hash, &subtable->rules) {
1659 if (miniflow_equal(&head->flow, flow)) {
1667 * As the readers are operating concurrently with the modifications, a
1668 * concurrent reader may or may not see the new rule, depending on how
1669 * the concurrent events overlap with each other. This is no
1670 * different from the former locked behavior, but there the visibility
1671 * of the new rule only depended on the timing of the locking
1674 * The new rule is first added to the segment indices, so the readers
1675 * may find the rule in the indices before the rule is visible in the
1676 * subtables 'rules' map. This may result in us losing the
1677 * opportunity to quit lookups earlier, resulting in sub-optimal
1678 * wildcarding. This will be fixed by forthcoming revalidation always
1679 * scheduled after flow table changes.
1681 * Similar behavior may happen due to us removing the overlapping rule
1682 * (if any) from the indices only after the new rule has been added.
1684 * The subtable's max priority is updated only after the rule is
1685 * inserted, so the concurrent readers may not see the rule, as the
1686 * updated priority ordered subtable list will only be visible after
1687 * the subtable's max priority is updated.
1689 * Similarly, the classifier's partitions for new rules are updated by
1690 * the caller after this function, so the readers may keep skipping
1691 * the subtable until they see the updated partitions.
1693 static struct cls_match *
1694 insert_rule(struct classifier *cls, struct cls_subtable *subtable,
1695 struct cls_rule *new_rule)
1696 OVS_REQUIRES(cls->mutex)
1698 struct cls_match *old = NULL;
1699 struct cls_match *new = cls_match_alloc(new_rule);
1700 struct cls_match *head;
1702 uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
1703 uint8_t prev_be32ofs = 0;
1705 /* Add new node to segment indices. */
1706 for (i = 0; i < subtable->n_indices; i++) {
1707 ihash[i] = minimatch_hash_range(&new_rule->match, prev_be32ofs,
1708 subtable->index_ofs[i], &basis);
1709 cmap_insert(&subtable->indices[i], &new->index_nodes[i], ihash[i]);
1710 prev_be32ofs = subtable->index_ofs[i];
1712 hash = minimatch_hash_range(&new_rule->match, prev_be32ofs, FLOW_U32S,
1714 head = find_equal(subtable, &new_rule->match.flow, hash);
1716 cmap_insert(&subtable->rules, &new->cmap_node, hash);
1717 list_init(&new->list);
1720 /* Scan the list for the insertion point that will keep the list in
1721 * order of decreasing priority. */
1722 struct cls_match *rule;
1724 FOR_EACH_RULE_IN_LIST (rule, head) {
1725 if (new->priority >= rule->priority) {
1727 /* 'new' is the new highest-priority flow in the list. */
1728 cmap_replace(&subtable->rules, &rule->cmap_node,
1729 &new->cmap_node, hash);
1732 if (new->priority == rule->priority) {
1733 list_replace(&new->list, &rule->list);
1736 list_insert(&rule->list, &new->list);
1742 /* Insert 'new' at the end of the list. */
1743 list_push_back(&head->list, &new->list);
1748 subtable->n_rules++;
1750 /* Rule was added, not replaced. Update 'subtable's 'max_priority'
1751 * and 'max_count', if necessary. */
1752 if (subtable->n_rules == 1) {
1753 subtable->max_priority = new->priority;
1754 subtable->max_count = 1;
1755 pvector_insert(&cls->subtables, subtable, new->priority);
1756 } else if (subtable->max_priority == new->priority) {
1757 ++subtable->max_count;
1758 } else if (new->priority > subtable->max_priority) {
1759 subtable->max_priority = new->priority;
1760 subtable->max_count = 1;
1761 pvector_change_priority(&cls->subtables, subtable, new->priority);
1764 /* Remove old node from indices. */
1765 for (i = 0; i < subtable->n_indices; i++) {
1766 cmap_remove(&subtable->indices[i], &old->index_nodes[i], ihash[i]);
1772 static struct cls_match *
1773 next_rule_in_list__(struct cls_match *rule)
1774 OVS_NO_THREAD_SAFETY_ANALYSIS
1776 struct cls_match *next = OBJECT_CONTAINING(rule->list.next, next, list);
1780 static struct cls_match *
1781 next_rule_in_list(struct cls_match *rule)
1783 struct cls_match *next = next_rule_in_list__(rule);
1784 return next->priority < rule->priority ? next : NULL;
1787 /* A longest-prefix match tree. */
1789 uint32_t prefix; /* Prefix bits for this node, MSB first. */
1790 uint8_t n_bits; /* Never zero, except for the root node. */
1791 unsigned int n_rules; /* Number of rules that have this prefix. */
1792 rcu_trie_ptr edges[2]; /* Both NULL if leaf. */
1795 /* Max bits per node. Must fit in struct trie_node's 'prefix'.
1796 * Also tested with 16, 8, and 5 to stress the implementation. */
1797 #define TRIE_PREFIX_BITS 32
1799 /* Return at least 'plen' bits of the 'prefix', starting at bit offset 'ofs'.
1800 * Prefixes are in the network byte order, and the offset 0 corresponds to
1801 * the most significant bit of the first byte. The offset can be read as
1802 * "how many bits to skip from the start of the prefix starting at 'pr'". */
1804 raw_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1808 pr += ofs / 32; /* Where to start. */
1809 ofs %= 32; /* How many bits to skip at 'pr'. */
1811 prefix = ntohl(*pr) << ofs; /* Get the first 32 - ofs bits. */
1812 if (plen > 32 - ofs) { /* Need more than we have already? */
1813 prefix |= ntohl(*++pr) >> (32 - ofs);
1815 /* Return with possible unwanted bits at the end. */
1819 /* Return min(TRIE_PREFIX_BITS, plen) bits of the 'prefix', starting at bit
1820 * offset 'ofs'. Prefixes are in the network byte order, and the offset 0
1821 * corresponds to the most significant bit of the first byte. The offset can
1822 * be read as "how many bits to skip from the start of the prefix starting at
1825 trie_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1830 if (plen > TRIE_PREFIX_BITS) {
1831 plen = TRIE_PREFIX_BITS; /* Get at most TRIE_PREFIX_BITS. */
1833 /* Return with unwanted bits cleared. */
1834 return raw_get_prefix(pr, ofs, plen) & ~0u << (32 - plen);
1837 /* Return the number of equal bits in 'n_bits' of 'prefix's MSBs and a 'value'
1838 * starting at "MSB 0"-based offset 'ofs'. */
1840 prefix_equal_bits(uint32_t prefix, unsigned int n_bits, const ovs_be32 value[],
1843 uint64_t diff = prefix ^ raw_get_prefix(value, ofs, n_bits);
1844 /* Set the bit after the relevant bits to limit the result. */
1845 return raw_clz64(diff << 32 | UINT64_C(1) << (63 - n_bits));
1848 /* Return the number of equal bits in 'node' prefix and a 'prefix' of length
1849 * 'plen', starting at "MSB 0"-based offset 'ofs'. */
1851 trie_prefix_equal_bits(const struct trie_node *node, const ovs_be32 prefix[],
1852 unsigned int ofs, unsigned int plen)
1854 return prefix_equal_bits(node->prefix, MIN(node->n_bits, plen - ofs),
1858 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' can
1859 * be greater than 31. */
1861 be_get_bit_at(const ovs_be32 value[], unsigned int ofs)
1863 return (((const uint8_t *)value)[ofs / 8] >> (7 - ofs % 8)) & 1u;
1866 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' must
1867 * be between 0 and 31, inclusive. */
1869 get_bit_at(const uint32_t prefix, unsigned int ofs)
1871 return (prefix >> (31 - ofs)) & 1u;
1874 /* Create new branch. */
1875 static struct trie_node *
1876 trie_branch_create(const ovs_be32 *prefix, unsigned int ofs, unsigned int plen,
1877 unsigned int n_rules)
1879 struct trie_node *node = xmalloc(sizeof *node);
1881 node->prefix = trie_get_prefix(prefix, ofs, plen);
1883 if (plen <= TRIE_PREFIX_BITS) {
1884 node->n_bits = plen;
1885 ovsrcu_set_hidden(&node->edges[0], NULL);
1886 ovsrcu_set_hidden(&node->edges[1], NULL);
1887 node->n_rules = n_rules;
1888 } else { /* Need intermediate nodes. */
1889 struct trie_node *subnode = trie_branch_create(prefix,
1890 ofs + TRIE_PREFIX_BITS,
1891 plen - TRIE_PREFIX_BITS,
1893 int bit = get_bit_at(subnode->prefix, 0);
1894 node->n_bits = TRIE_PREFIX_BITS;
1895 ovsrcu_set_hidden(&node->edges[bit], subnode);
1896 ovsrcu_set_hidden(&node->edges[!bit], NULL);
1903 trie_node_destroy(const struct trie_node *node)
1905 ovsrcu_postpone(free, CONST_CAST(struct trie_node *, node));
1908 /* Copy a trie node for modification and postpone delete the old one. */
1909 static struct trie_node *
1910 trie_node_rcu_realloc(const struct trie_node *node)
1912 struct trie_node *new_node = xmalloc(sizeof *node);
1915 trie_node_destroy(node);
1920 /* May only be called while holding the classifier mutex. */
1922 trie_destroy(rcu_trie_ptr *trie)
1924 struct trie_node *node = ovsrcu_get_protected(struct trie_node *, trie);
1927 ovsrcu_set_hidden(trie, NULL);
1928 trie_destroy(&node->edges[0]);
1929 trie_destroy(&node->edges[1]);
1930 trie_node_destroy(node);
1935 trie_is_leaf(const struct trie_node *trie)
1938 return !ovsrcu_get(struct trie_node *, &trie->edges[0])
1939 && !ovsrcu_get(struct trie_node *, &trie->edges[1]);
1943 mask_set_prefix_bits(struct flow_wildcards *wc, uint8_t be32ofs,
1944 unsigned int n_bits)
1946 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1949 for (i = 0; i < n_bits / 32; i++) {
1950 mask[i] = OVS_BE32_MAX;
1953 mask[i] |= htonl(~0u << (32 - n_bits % 32));
1958 mask_prefix_bits_set(const struct flow_wildcards *wc, uint8_t be32ofs,
1959 unsigned int n_bits)
1961 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1963 ovs_be32 zeroes = 0;
1965 for (i = 0; i < n_bits / 32; i++) {
1969 zeroes |= ~mask[i] & htonl(~0u << (32 - n_bits % 32));
1972 return !zeroes; /* All 'n_bits' bits set. */
1975 static rcu_trie_ptr *
1976 trie_next_edge(struct trie_node *node, const ovs_be32 value[],
1979 return node->edges + be_get_bit_at(value, ofs);
1982 static const struct trie_node *
1983 trie_next_node(const struct trie_node *node, const ovs_be32 value[],
1986 return ovsrcu_get(struct trie_node *,
1987 &node->edges[be_get_bit_at(value, ofs)]);
1990 /* Return the prefix mask length necessary to find the longest-prefix match for
1991 * the '*value' in the prefix tree 'node'.
1992 * '*checkbits' is set to the number of bits in the prefix mask necessary to
1993 * determine a mismatch, in case there are longer prefixes in the tree below
1994 * the one that matched.
1997 trie_lookup_value(const rcu_trie_ptr *trie, const ovs_be32 value[],
1998 unsigned int n_bits, unsigned int *checkbits)
2000 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
2001 unsigned int ofs = 0, match_len = 0;
2002 const struct trie_node *prev = NULL;
2004 for (; node; prev = node, node = trie_next_node(node, value, ofs)) {
2005 unsigned int eqbits;
2006 /* Check if this edge can be followed. */
2007 eqbits = prefix_equal_bits(node->prefix, node->n_bits, value, ofs);
2009 if (eqbits < node->n_bits) { /* Mismatch, nothing more to be found. */
2010 /* Bit at offset 'ofs' differed. */
2011 *checkbits = ofs + 1; /* Includes the first mismatching bit. */
2014 /* Full match, check if rules exist at this prefix length. */
2015 if (node->n_rules > 0) {
2018 if (ofs >= n_bits) {
2019 *checkbits = n_bits; /* Full prefix. */
2023 /* node == NULL. Full match so far, but we came to a dead end.
2024 * need to exclude the other branch if it exists. */
2025 *checkbits = !prev || trie_is_leaf(prev) ? ofs : ofs + 1;
2030 trie_lookup(const struct cls_trie *trie, const struct flow *flow,
2031 unsigned int *checkbits)
2033 const struct mf_field *mf = trie->field;
2035 /* Check that current flow matches the prerequisites for the trie
2036 * field. Some match fields are used for multiple purposes, so we
2037 * must check that the trie is relevant for this flow. */
2038 if (mf_are_prereqs_ok(mf, flow)) {
2039 return trie_lookup_value(&trie->root,
2040 &((ovs_be32 *)flow)[mf->flow_be32ofs],
2041 mf->n_bits, checkbits);
2043 *checkbits = 0; /* Value not used in this case. */
2047 /* Returns the length of a prefix match mask for the field 'mf' in 'minimask'.
2048 * Returns the u32 offset to the miniflow data in '*miniflow_index', if
2049 * 'miniflow_index' is not NULL. */
2051 minimask_get_prefix_len(const struct minimask *minimask,
2052 const struct mf_field *mf)
2054 unsigned int n_bits = 0, mask_tz = 0; /* Non-zero when end of mask seen. */
2055 uint8_t u32_ofs = mf->flow_be32ofs;
2056 uint8_t u32_end = u32_ofs + mf->n_bytes / 4;
2058 for (; u32_ofs < u32_end; ++u32_ofs) {
2060 mask = ntohl((OVS_FORCE ovs_be32)minimask_get(minimask, u32_ofs));
2062 /* Validate mask, count the mask length. */
2065 return 0; /* No bits allowed after mask ended. */
2068 if (~mask & (~mask + 1)) {
2069 return 0; /* Mask not contiguous. */
2071 mask_tz = ctz32(mask);
2072 n_bits += 32 - mask_tz;
2080 * This is called only when mask prefix is known to be CIDR and non-zero.
2081 * Relies on the fact that the flow and mask have the same map, and since
2082 * the mask is CIDR, the storage for the flow field exists even if it
2083 * happened to be zeros.
2085 static const ovs_be32 *
2086 minimatch_get_prefix(const struct minimatch *match, const struct mf_field *mf)
2088 return miniflow_get_be32_values(&match->flow) +
2089 count_1bits(match->flow.map & ((UINT64_C(1) << mf->flow_be32ofs) - 1));
2092 /* Insert rule in to the prefix tree.
2093 * 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2096 trie_insert(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2098 trie_insert_prefix(&trie->root,
2099 minimatch_get_prefix(&rule->match, trie->field), mlen);
2103 trie_insert_prefix(rcu_trie_ptr *edge, const ovs_be32 *prefix, int mlen)
2105 struct trie_node *node;
2108 /* Walk the tree. */
2109 for (; (node = ovsrcu_get_protected(struct trie_node *, edge));
2110 edge = trie_next_edge(node, prefix, ofs)) {
2111 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2113 if (eqbits < node->n_bits) {
2114 /* Mismatch, new node needs to be inserted above. */
2115 int old_branch = get_bit_at(node->prefix, eqbits);
2116 struct trie_node *new_parent;
2118 new_parent = trie_branch_create(prefix, ofs - eqbits, eqbits,
2119 ofs == mlen ? 1 : 0);
2120 /* Copy the node to modify it. */
2121 node = trie_node_rcu_realloc(node);
2122 /* Adjust the new node for its new position in the tree. */
2123 node->prefix <<= eqbits;
2124 node->n_bits -= eqbits;
2125 ovsrcu_set_hidden(&new_parent->edges[old_branch], node);
2127 /* Check if need a new branch for the new rule. */
2129 ovsrcu_set_hidden(&new_parent->edges[!old_branch],
2130 trie_branch_create(prefix, ofs, mlen - ofs,
2133 ovsrcu_set(edge, new_parent); /* Publish changes. */
2136 /* Full match so far. */
2139 /* Full match at the current node, rule needs to be added here. */
2144 /* Must insert a new tree branch for the new rule. */
2145 ovsrcu_set(edge, trie_branch_create(prefix, ofs, mlen - ofs, 1));
2148 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2151 trie_remove(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2153 trie_remove_prefix(&trie->root,
2154 minimatch_get_prefix(&rule->match, trie->field), mlen);
2157 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2160 trie_remove_prefix(rcu_trie_ptr *root, const ovs_be32 *prefix, int mlen)
2162 struct trie_node *node;
2163 rcu_trie_ptr *edges[sizeof(union mf_value) * 8];
2164 int depth = 0, ofs = 0;
2166 /* Walk the tree. */
2167 for (edges[0] = root;
2168 (node = ovsrcu_get_protected(struct trie_node *, edges[depth]));
2169 edges[++depth] = trie_next_edge(node, prefix, ofs)) {
2170 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2172 if (eqbits < node->n_bits) {
2173 /* Mismatch, nothing to be removed. This should never happen, as
2174 * only rules in the classifier are ever removed. */
2175 break; /* Log a warning. */
2177 /* Full match so far. */
2181 /* Full prefix match at the current node, remove rule here. */
2182 if (!node->n_rules) {
2183 break; /* Log a warning. */
2187 /* Check if can prune the tree. */
2188 while (!node->n_rules) {
2189 struct trie_node *next,
2190 *edge0 = ovsrcu_get_protected(struct trie_node *,
2192 *edge1 = ovsrcu_get_protected(struct trie_node *,
2195 if (edge0 && edge1) {
2196 break; /* A branching point, cannot prune. */
2199 /* Else have at most one child node, remove this node. */
2200 next = edge0 ? edge0 : edge1;
2203 if (node->n_bits + next->n_bits > TRIE_PREFIX_BITS) {
2204 break; /* Cannot combine. */
2206 next = trie_node_rcu_realloc(next); /* Modify. */
2208 /* Combine node with next. */
2209 next->prefix = node->prefix | next->prefix >> node->n_bits;
2210 next->n_bits += node->n_bits;
2212 /* Update the parent's edge. */
2213 ovsrcu_set(edges[depth], next); /* Publish changes. */
2214 trie_node_destroy(node);
2216 if (next || !depth) {
2217 /* Branch not pruned or at root, nothing more to do. */
2220 node = ovsrcu_get_protected(struct trie_node *,
2226 /* Cannot go deeper. This should never happen, since only rules
2227 * that actually exist in the classifier are ever removed. */
2228 VLOG_WARN("Trying to remove non-existing rule from a prefix trie.");