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"
27 #include "ovs-thread.h"
31 VLOG_DEFINE_THIS_MODULE(classifier);
35 /* Prefix trie for a 'field' */
37 const struct mf_field *field; /* Trie field, or NULL. */
38 struct trie_node *root; /* NULL if none. */
41 struct cls_subtable_entry {
42 struct cls_subtable *subtable;
43 uint32_t *mask_values;
45 unsigned int max_priority;
48 struct cls_subtable_cache {
49 struct cls_subtable_entry *subtables;
50 size_t alloc_size; /* Number of allocated elements. */
51 size_t size; /* One past last valid array element. */
55 CLS_MAX_INDICES = 3 /* Maximum number of lookup indices per subtable. */
58 struct cls_classifier {
59 int n_rules; /* Total number of rules. */
60 uint8_t n_flow_segments;
61 uint8_t flow_segments[CLS_MAX_INDICES]; /* Flow segment boundaries to use
62 * for staged lookup. */
63 struct hmap subtables; /* Contains "struct cls_subtable"s. */
64 struct cls_subtable_cache subtables_priority;
65 struct hmap partitions; /* Contains "struct cls_partition"s. */
66 struct cls_trie tries[CLS_MAX_TRIES]; /* Prefix tries. */
70 /* A set of rules that all have the same fields wildcarded. */
72 struct hmap_node hmap_node; /* Within struct cls_classifier 'subtables'
74 struct hmap rules; /* Contains "struct cls_rule"s. */
75 struct minimask mask; /* Wildcards for fields. */
76 int n_rules; /* Number of rules, including duplicates. */
77 unsigned int max_priority; /* Max priority of any rule in the subtable. */
78 unsigned int max_count; /* Count of max_priority rules. */
79 tag_type tag; /* Tag generated from mask for partitioning. */
80 uint8_t n_indices; /* How many indices to use. */
81 uint8_t index_ofs[CLS_MAX_INDICES]; /* u32 flow segment boundaries. */
82 struct hindex indices[CLS_MAX_INDICES]; /* Staged lookup indices. */
83 unsigned int trie_plen[CLS_MAX_TRIES]; /* Trie prefix length in 'mask'. */
86 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
87 * field) with tags for the "cls_subtable"s that contain rules that match that
89 struct cls_partition {
90 struct hmap_node hmap_node; /* In struct cls_classifier's 'partitions'
92 ovs_be64 metadata; /* metadata value for this partition. */
93 tag_type tags; /* OR of each flow's cls_subtable tag. */
94 struct tag_tracker tracker; /* Tracks the bits in 'tags'. */
97 /* Internal representation of a rule in a "struct cls_subtable". */
99 struct cls_rule *cls_rule;
100 struct hindex_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
102 struct hmap_node hmap_node; /* Within struct cls_subtable 'rules'. */
103 unsigned int priority; /* Larger numbers are higher priorities. */
104 struct cls_partition *partition;
105 struct list list; /* List of identical, lower-priority rules. */
106 struct minimatch match; /* Matching rule. */
109 static struct cls_match *
110 cls_match_alloc(struct cls_rule *rule)
112 struct cls_match *cls_match = xmalloc(sizeof *cls_match);
114 cls_match->cls_rule = rule;
115 minimatch_clone(&cls_match->match, &rule->match);
116 cls_match->priority = rule->priority;
117 rule->cls_match = cls_match;
123 static struct cls_subtable *find_subtable(const struct cls_classifier *,
124 const struct minimask *);
125 static struct cls_subtable *insert_subtable(struct cls_classifier *,
126 const struct minimask *);
128 static void destroy_subtable(struct cls_classifier *, struct cls_subtable *);
130 static void update_subtables_after_insertion(struct cls_classifier *,
131 struct cls_subtable *,
132 unsigned int new_priority);
133 static void update_subtables_after_removal(struct cls_classifier *,
134 struct cls_subtable *,
135 unsigned int del_priority);
137 static struct cls_match *find_match_wc(const struct cls_subtable *,
138 const struct flow *, struct trie_ctx *,
139 unsigned int n_tries,
140 struct flow_wildcards *);
141 static struct cls_match *find_equal(struct cls_subtable *,
142 const struct miniflow *, uint32_t hash);
143 static struct cls_match *insert_rule(struct cls_classifier *,
144 struct cls_subtable *, struct cls_rule *);
146 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
147 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
148 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
149 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
150 for ((RULE) = (HEAD); \
151 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
154 static struct cls_match *next_rule_in_list__(struct cls_match *);
155 static struct cls_match *next_rule_in_list(struct cls_match *);
157 static unsigned int minimask_get_prefix_len(const struct minimask *,
158 const struct mf_field *);
159 static void trie_init(struct cls_classifier *, int trie_idx,
160 const struct mf_field *);
161 static unsigned int trie_lookup(const struct cls_trie *, const struct flow *,
162 unsigned int *checkbits);
164 static void trie_destroy(struct trie_node *);
165 static void trie_insert(struct cls_trie *, const struct cls_rule *, int mlen);
166 static void trie_remove(struct cls_trie *, const struct cls_rule *, int mlen);
167 static void mask_set_prefix_bits(struct flow_wildcards *, uint8_t be32ofs,
169 static bool mask_prefix_bits_set(const struct flow_wildcards *,
170 uint8_t be32ofs, unsigned int nbits);
173 cls_subtable_cache_init(struct cls_subtable_cache *array)
175 memset(array, 0, sizeof *array);
179 cls_subtable_cache_destroy(struct cls_subtable_cache *array)
181 free(array->subtables);
182 memset(array, 0, sizeof *array);
185 /* Array insertion. */
187 cls_subtable_cache_push_back(struct cls_subtable_cache *array,
188 struct cls_subtable_entry a)
190 if (array->size == array->alloc_size) {
191 array->subtables = x2nrealloc(array->subtables, &array->alloc_size,
195 array->subtables[array->size++] = a;
198 /* Only for rearranging entries in the same cache. */
200 cls_subtable_cache_splice(struct cls_subtable_entry *to,
201 struct cls_subtable_entry *start,
202 struct cls_subtable_entry *end)
205 /* Same as splicing entries to (start) from [end, to). */
206 struct cls_subtable_entry *temp = to;
207 to = start; start = end; end = temp;
210 while (start != end) {
211 struct cls_subtable_entry temp = *start;
213 memmove(to + 1, to, (start - to) * sizeof *to);
217 } /* Else nothing to be done. */
222 cls_subtable_cache_remove(struct cls_subtable_cache *array,
223 struct cls_subtable_entry *elem)
225 ssize_t size = (&array->subtables[array->size]
226 - (elem + 1)) * sizeof *elem;
228 memmove(elem, elem + 1, size);
233 #define CLS_SUBTABLE_CACHE_FOR_EACH(SUBTABLE, ITER, ARRAY) \
234 for (ITER = (ARRAY)->subtables; \
235 ITER < &(ARRAY)->subtables[(ARRAY)->size] \
236 && OVS_LIKELY(SUBTABLE = ITER->subtable); \
238 #define CLS_SUBTABLE_CACHE_FOR_EACH_CONTINUE(SUBTABLE, ITER, ARRAY) \
240 ITER < &(ARRAY)->subtables[(ARRAY)->size] \
241 && OVS_LIKELY(SUBTABLE = ITER->subtable); \
243 #define CLS_SUBTABLE_CACHE_FOR_EACH_REVERSE(SUBTABLE, ITER, ARRAY) \
244 for (ITER = &(ARRAY)->subtables[(ARRAY)->size]; \
245 ITER > (ARRAY)->subtables \
246 && OVS_LIKELY(SUBTABLE = (--ITER)->subtable);)
249 /* flow/miniflow/minimask/minimatch utilities.
250 * These are only used by the classifier, so place them here to allow
251 * for better optimization. */
253 static inline uint64_t
254 miniflow_get_map_in_range(const struct miniflow *miniflow,
255 uint8_t start, uint8_t end, unsigned int *offset)
257 uint64_t map = miniflow->map;
261 uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
262 *offset = count_1bits(map & msk);
265 if (end < FLOW_U32S) {
266 uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
272 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
273 * 'mask', given 'basis'.
275 * The hash values returned by this function are the same as those returned by
276 * miniflow_hash_in_minimask(), only the form of the arguments differ. */
277 static inline uint32_t
278 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
281 const uint32_t *flow_u32 = (const uint32_t *)flow;
282 const uint32_t *p = mask->masks.values;
287 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
288 hash = mhash_add(hash, flow_u32[raw_ctz(map)] & *p++);
291 return mhash_finish(hash, (p - mask->masks.values) * 4);
294 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
295 * 'mask', given 'basis'.
297 * The hash values returned by this function are the same as those returned by
298 * flow_hash_in_minimask(), only the form of the arguments differ. */
299 static inline uint32_t
300 miniflow_hash_in_minimask(const struct miniflow *flow,
301 const struct minimask *mask, uint32_t basis)
303 const uint32_t *p = mask->masks.values;
304 uint32_t hash = basis;
307 MINIFLOW_FOR_EACH_IN_MAP(flow_u32, flow, mask->masks.map) {
308 hash = mhash_add(hash, flow_u32 & *p++);
311 return mhash_finish(hash, (p - mask->masks.values) * 4);
314 /* Returns a hash value for the bits of range [start, end) in 'flow',
315 * where there are 1-bits in 'mask', given 'hash'.
317 * The hash values returned by this function are the same as those returned by
318 * minimatch_hash_range(), only the form of the arguments differ. */
319 static inline uint32_t
320 flow_hash_in_minimask_range(const struct flow *flow,
321 const struct minimask *mask,
322 uint8_t start, uint8_t end, uint32_t *basis)
324 const uint32_t *flow_u32 = (const uint32_t *)flow;
326 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
328 const uint32_t *p = mask->masks.values + offset;
329 uint32_t hash = *basis;
331 for (; map; map = zero_rightmost_1bit(map)) {
332 hash = mhash_add(hash, flow_u32[raw_ctz(map)] & *p++);
335 *basis = hash; /* Allow continuation from the unfinished value. */
336 return mhash_finish(hash, (p - mask->masks.values) * 4);
339 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
341 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
342 const struct minimask *mask)
344 flow_union_with_miniflow(&wc->masks, &mask->masks);
347 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
348 * in range [start, end). */
350 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
351 const struct minimask *mask,
352 uint8_t start, uint8_t end)
354 uint32_t *dst_u32 = (uint32_t *)&wc->masks;
356 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
358 const uint32_t *p = mask->masks.values + offset;
360 for (; map; map = zero_rightmost_1bit(map)) {
361 dst_u32[raw_ctz(map)] |= *p++;
365 /* Returns a hash value for 'flow', given 'basis'. */
366 static inline uint32_t
367 miniflow_hash(const struct miniflow *flow, uint32_t basis)
369 const uint32_t *p = flow->values;
370 uint32_t hash = basis;
371 uint64_t hash_map = 0;
374 for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
376 hash = mhash_add(hash, *p);
377 hash_map |= rightmost_1bit(map);
381 hash = mhash_add(hash, hash_map);
382 hash = mhash_add(hash, hash_map >> 32);
384 return mhash_finish(hash, p - flow->values);
387 /* Returns a hash value for 'mask', given 'basis'. */
388 static inline uint32_t
389 minimask_hash(const struct minimask *mask, uint32_t basis)
391 return miniflow_hash(&mask->masks, basis);
394 /* Returns a hash value for 'match', given 'basis'. */
395 static inline uint32_t
396 minimatch_hash(const struct minimatch *match, uint32_t basis)
398 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
401 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
404 * The hash values returned by this function are the same as those returned by
405 * flow_hash_in_minimask_range(), only the form of the arguments differ. */
406 static inline uint32_t
407 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
411 const uint32_t *p, *q;
412 uint32_t hash = *basis;
415 n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
417 q = match->mask.masks.values + offset;
418 p = match->flow.values + offset;
420 for (i = 0; i < n; i++) {
421 hash = mhash_add(hash, p[i] & q[i]);
423 *basis = hash; /* Allow continuation from the unfinished value. */
424 return mhash_finish(hash, (offset + n) * 4);
430 /* Initializes 'rule' to match packets specified by 'match' at the given
431 * 'priority'. 'match' must satisfy the invariant described in the comment at
432 * the definition of struct match.
434 * The caller must eventually destroy 'rule' with cls_rule_destroy().
436 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
437 * internally Open vSwitch supports a wider range.) */
439 cls_rule_init(struct cls_rule *rule,
440 const struct match *match, unsigned int priority)
442 minimatch_init(&rule->match, match);
443 rule->priority = priority;
444 rule->cls_match = NULL;
447 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
449 cls_rule_init_from_minimatch(struct cls_rule *rule,
450 const struct minimatch *match,
451 unsigned int priority)
453 minimatch_clone(&rule->match, match);
454 rule->priority = priority;
455 rule->cls_match = NULL;
458 /* Initializes 'dst' as a copy of 'src'.
460 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
462 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
464 minimatch_clone(&dst->match, &src->match);
465 dst->priority = src->priority;
466 dst->cls_match = NULL;
469 /* Initializes 'dst' with the data in 'src', destroying 'src'.
471 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
473 cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
475 minimatch_move(&dst->match, &src->match);
476 dst->priority = src->priority;
477 dst->cls_match = NULL;
480 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
481 * normally embedded into a larger structure).
483 * ('rule' must not currently be in a classifier.) */
485 cls_rule_destroy(struct cls_rule *rule)
487 ovs_assert(!rule->cls_match);
488 minimatch_destroy(&rule->match);
491 /* Returns true if 'a' and 'b' match the same packets at the same priority,
492 * false if they differ in some way. */
494 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
496 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
499 /* Returns a hash value for 'rule', folding in 'basis'. */
501 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
503 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
506 /* Appends a string describing 'rule' to 's'. */
508 cls_rule_format(const struct cls_rule *rule, struct ds *s)
510 minimatch_format(&rule->match, s, rule->priority);
513 /* Returns true if 'rule' matches every packet, false otherwise. */
515 cls_rule_is_catchall(const struct cls_rule *rule)
517 return minimask_is_catchall(&rule->match.mask);
520 /* Initializes 'cls' as a classifier that initially contains no classification
523 classifier_init(struct classifier *cls_, const uint8_t *flow_segments)
525 struct cls_classifier *cls = xmalloc(sizeof *cls);
527 fat_rwlock_init(&cls_->rwlock);
532 hmap_init(&cls->subtables);
533 cls_subtable_cache_init(&cls->subtables_priority);
534 hmap_init(&cls->partitions);
535 cls->n_flow_segments = 0;
537 while (cls->n_flow_segments < CLS_MAX_INDICES
538 && *flow_segments < FLOW_U32S) {
539 cls->flow_segments[cls->n_flow_segments++] = *flow_segments++;
545 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
546 * caller's responsibility. */
548 classifier_destroy(struct classifier *cls_)
551 struct cls_classifier *cls = cls_->cls;
552 struct cls_subtable *partition, *next_partition;
553 struct cls_subtable *subtable, *next_subtable;
556 fat_rwlock_destroy(&cls_->rwlock);
561 for (i = 0; i < cls->n_tries; i++) {
562 trie_destroy(cls->tries[i].root);
565 HMAP_FOR_EACH_SAFE (subtable, next_subtable, hmap_node,
567 destroy_subtable(cls, subtable);
569 hmap_destroy(&cls->subtables);
571 HMAP_FOR_EACH_SAFE (partition, next_partition, hmap_node,
573 hmap_remove(&cls->partitions, &partition->hmap_node);
576 hmap_destroy(&cls->partitions);
578 cls_subtable_cache_destroy(&cls->subtables_priority);
583 /* We use uint64_t as a set for the fields below. */
584 BUILD_ASSERT_DECL(MFF_N_IDS <= 64);
586 /* Set the fields for which prefix lookup should be performed. */
588 classifier_set_prefix_fields(struct classifier *cls_,
589 const enum mf_field_id *trie_fields,
590 unsigned int n_fields)
592 struct cls_classifier *cls = cls_->cls;
596 for (i = 0, trie = 0; i < n_fields && trie < CLS_MAX_TRIES; i++) {
597 const struct mf_field *field = mf_from_id(trie_fields[i]);
598 if (field->flow_be32ofs < 0 || field->n_bits % 32) {
599 /* Incompatible field. This is the only place where we
600 * enforce these requirements, but the rest of the trie code
601 * depends on the flow_be32ofs to be non-negative and the
602 * field length to be a multiple of 32 bits. */
606 if (fields & (UINT64_C(1) << trie_fields[i])) {
607 /* Duplicate field, there is no need to build more than
608 * one index for any one field. */
611 fields |= UINT64_C(1) << trie_fields[i];
613 if (trie >= cls->n_tries || field != cls->tries[trie].field) {
614 trie_init(cls, trie, field);
619 /* Destroy the rest. */
620 for (i = trie; i < cls->n_tries; i++) {
621 trie_init(cls, i, NULL);
627 trie_init(struct cls_classifier *cls, int trie_idx,
628 const struct mf_field *field)
630 struct cls_trie *trie = &cls->tries[trie_idx];
631 struct cls_subtable *subtable;
632 struct cls_subtable_entry *iter;
634 if (trie_idx < cls->n_tries) {
635 trie_destroy(trie->root);
640 /* Add existing rules to the trie. */
641 CLS_SUBTABLE_CACHE_FOR_EACH (subtable, iter, &cls->subtables_priority) {
644 plen = field ? minimask_get_prefix_len(&subtable->mask, field) : 0;
645 /* Initialize subtable's prefix length on this field. */
646 subtable->trie_plen[trie_idx] = plen;
649 struct cls_match *head;
651 HMAP_FOR_EACH (head, hmap_node, &subtable->rules) {
652 struct cls_match *match;
654 FOR_EACH_RULE_IN_LIST (match, head) {
655 trie_insert(trie, match->cls_rule, plen);
662 /* Returns true if 'cls' contains no classification rules, false otherwise. */
664 classifier_is_empty(const struct classifier *cls)
666 return cls->cls->n_rules == 0;
669 /* Returns the number of rules in 'cls'. */
671 classifier_count(const struct classifier *cls)
673 return cls->cls->n_rules;
677 hash_metadata(ovs_be64 metadata_)
679 uint64_t metadata = (OVS_FORCE uint64_t) metadata_;
680 return hash_uint64(metadata);
683 static struct cls_partition *
684 find_partition(const struct cls_classifier *cls, ovs_be64 metadata,
687 struct cls_partition *partition;
689 HMAP_FOR_EACH_IN_BUCKET (partition, hmap_node, hash, &cls->partitions) {
690 if (partition->metadata == metadata) {
698 static struct cls_partition *
699 create_partition(struct cls_classifier *cls, struct cls_subtable *subtable,
702 uint32_t hash = hash_metadata(metadata);
703 struct cls_partition *partition = find_partition(cls, metadata, hash);
705 partition = xmalloc(sizeof *partition);
706 partition->metadata = metadata;
708 tag_tracker_init(&partition->tracker);
709 hmap_insert(&cls->partitions, &partition->hmap_node, hash);
711 tag_tracker_add(&partition->tracker, &partition->tags, subtable->tag);
715 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
716 * must not modify or free it.
718 * If 'cls' already contains an identical rule (including wildcards, values of
719 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
720 * rule that was replaced. The caller takes ownership of the returned rule and
721 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
722 * memory block in which it resides, etc., as necessary.
724 * Returns NULL if 'cls' does not contain a rule with an identical key, after
725 * inserting the new rule. In this case, no rules are displaced by the new
726 * rule, even rules that cannot have any effect because the new rule matches a
727 * superset of their flows and has higher priority. */
729 classifier_replace(struct classifier *cls_, struct cls_rule *rule)
731 struct cls_classifier *cls = cls_->cls;
732 struct cls_match *old_rule;
733 struct cls_subtable *subtable;
735 subtable = find_subtable(cls, &rule->match.mask);
737 subtable = insert_subtable(cls, &rule->match.mask);
740 old_rule = insert_rule(cls, subtable, rule);
744 rule->cls_match->partition = NULL;
745 if (minimask_get_metadata_mask(&rule->match.mask) == OVS_BE64_MAX) {
746 ovs_be64 metadata = miniflow_get_metadata(&rule->match.flow);
747 rule->cls_match->partition = create_partition(cls, subtable,
754 for (i = 0; i < cls->n_tries; i++) {
755 if (subtable->trie_plen[i]) {
756 trie_insert(&cls->tries[i], rule, subtable->trie_plen[i]);
761 struct cls_rule *old_cls_rule = old_rule->cls_rule;
763 rule->cls_match->partition = old_rule->partition;
764 old_cls_rule->cls_match = NULL;
770 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
771 * must not modify or free it.
773 * 'cls' must not contain an identical rule (including wildcards, values of
774 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
777 classifier_insert(struct classifier *cls, struct cls_rule *rule)
779 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
780 ovs_assert(!displaced_rule);
783 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
784 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
785 * resides, etc., as necessary. */
787 classifier_remove(struct classifier *cls_, struct cls_rule *rule)
789 struct cls_classifier *cls = cls_->cls;
790 struct cls_partition *partition;
791 struct cls_match *cls_match = rule->cls_match;
792 struct cls_match *head;
793 struct cls_subtable *subtable;
796 ovs_assert(cls_match);
798 subtable = find_subtable(cls, &rule->match.mask);
800 ovs_assert(subtable);
802 for (i = 0; i < cls->n_tries; i++) {
803 if (subtable->trie_plen[i]) {
804 trie_remove(&cls->tries[i], rule, subtable->trie_plen[i]);
808 /* Remove rule node from indices. */
809 for (i = 0; i < subtable->n_indices; i++) {
810 hindex_remove(&subtable->indices[i], &cls_match->index_nodes[i]);
813 head = find_equal(subtable, &rule->match.flow, cls_match->hmap_node.hash);
814 if (head != cls_match) {
815 list_remove(&cls_match->list);
816 } else if (list_is_empty(&cls_match->list)) {
817 hmap_remove(&subtable->rules, &cls_match->hmap_node);
819 struct cls_match *next = CONTAINER_OF(cls_match->list.next,
820 struct cls_match, list);
822 list_remove(&cls_match->list);
823 hmap_replace(&subtable->rules, &cls_match->hmap_node,
827 partition = cls_match->partition;
829 tag_tracker_subtract(&partition->tracker, &partition->tags,
831 if (!partition->tags) {
832 hmap_remove(&cls->partitions, &partition->hmap_node);
837 if (--subtable->n_rules == 0) {
838 destroy_subtable(cls, subtable);
840 update_subtables_after_removal(cls, subtable, cls_match->priority);
845 rule->cls_match = NULL;
849 /* Prefix tree context. Valid when 'lookup_done' is true. Can skip all
850 * subtables which have more than 'match_plen' bits in their corresponding
851 * field at offset 'be32ofs'. If skipped, 'maskbits' prefix bits should be
852 * unwildcarded to quarantee datapath flow matches only packets it should. */
854 const struct cls_trie *trie;
855 bool lookup_done; /* Status of the lookup. */
856 uint8_t be32ofs; /* U32 offset of the field in question. */
857 unsigned int match_plen; /* Longest prefix than could possibly match. */
858 unsigned int maskbits; /* Prefix length needed to avoid false matches. */
862 trie_ctx_init(struct trie_ctx *ctx, const struct cls_trie *trie)
865 ctx->be32ofs = trie->field->flow_be32ofs;
866 ctx->lookup_done = false;
870 lookahead_subtable(const struct cls_subtable_entry *subtables)
872 ovs_prefetch_range(subtables->subtable, sizeof *subtables->subtable);
873 ovs_prefetch_range(subtables->mask_values, 1);
876 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
877 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
878 * of equal priority match 'flow', returns one arbitrarily.
880 * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
881 * set of bits that were significant in the lookup. At some point
882 * earlier, 'wc' should have been initialized (e.g., by
883 * flow_wildcards_init_catchall()). */
885 classifier_lookup(const struct classifier *cls_, const struct flow *flow,
886 struct flow_wildcards *wc)
888 struct cls_classifier *cls = cls_->cls;
889 const struct cls_partition *partition;
891 struct cls_match *best;
892 struct trie_ctx trie_ctx[CLS_MAX_TRIES];
894 struct cls_subtable_entry *subtables = cls->subtables_priority.subtables;
895 int n_subtables = cls->subtables_priority.size;
896 int64_t best_priority = -1;
898 /* Prefetch the subtables array. */
899 ovs_prefetch_range(subtables, n_subtables * sizeof *subtables);
901 /* Determine 'tags' such that, if 'subtable->tag' doesn't intersect them,
902 * then 'flow' cannot possibly match in 'subtable':
904 * - If flow->metadata maps to a given 'partition', then we can use
905 * 'tags' for 'partition->tags'.
907 * - If flow->metadata has no partition, then no rule in 'cls' has an
908 * exact-match for flow->metadata. That means that we don't need to
909 * search any subtable that includes flow->metadata in its mask.
911 * In either case, we always need to search any cls_subtables that do not
912 * include flow->metadata in its mask. One way to do that would be to
913 * check the "cls_subtable"s explicitly for that, but that would require an
914 * extra branch per subtable. Instead, we mark such a cls_subtable's
915 * 'tags' as TAG_ALL and make sure that 'tags' is never empty. This means
916 * that 'tags' always intersects such a cls_subtable's 'tags', so we don't
917 * need a special case.
919 partition = (hmap_is_empty(&cls->partitions)
921 : find_partition(cls, flow->metadata,
922 hash_metadata(flow->metadata)));
923 tags = partition ? partition->tags : TAG_ARBITRARY;
925 /* Initialize trie contexts for match_find_wc(). */
926 for (i = 0; i < cls->n_tries; i++) {
927 trie_ctx_init(&trie_ctx[i], &cls->tries[i]);
930 /* Prefetch the first subtables. */
931 if (n_subtables > 1) {
932 lookahead_subtable(subtables);
933 lookahead_subtable(subtables + 1);
937 for (i = 0; OVS_LIKELY(i < n_subtables); i++) {
938 struct cls_match *rule;
940 if ((int64_t)subtables[i].max_priority <= best_priority) {
941 /* Subtables are in descending priority order,
942 * can not find anything better. */
946 /* Prefetch a forthcoming subtable. */
947 if (i + 2 < n_subtables) {
948 lookahead_subtable(&subtables[i + 2]);
951 if (!tag_intersects(tags, subtables[i].tag)) {
955 rule = find_match_wc(subtables[i].subtable, flow, trie_ctx,
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 minimatch_matches_miniflow(const struct minimatch *match,
970 const struct miniflow *target)
972 const uint32_t *flowp = (const uint32_t *)match->flow.values;
973 const uint32_t *maskp = (const uint32_t *)match->mask.masks.values;
976 MINIFLOW_FOR_EACH_IN_MAP(target_u32, target, match->mask.masks.map) {
977 if ((*flowp++ ^ target_u32) & *maskp++) {
985 static inline struct cls_match *
986 find_match_miniflow(const struct cls_subtable *subtable,
987 const struct miniflow *flow,
990 struct cls_match *rule;
992 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &subtable->rules) {
993 if (minimatch_matches_miniflow(&rule->match, flow)) {
1001 /* Finds and returns the highest-priority rule in 'cls' that matches
1002 * 'miniflow'. Returns a null pointer if no rules in 'cls' match 'flow'.
1003 * If multiple rules of equal priority match 'flow', returns one arbitrarily.
1005 * This function is optimized for the userspace datapath, which only ever has
1006 * one priority value for it's flows!
1008 struct cls_rule *classifier_lookup_miniflow_first(const struct classifier *cls_,
1009 const struct miniflow *flow)
1011 struct cls_classifier *cls = cls_->cls;
1012 struct cls_subtable *subtable;
1013 struct cls_subtable_entry *iter;
1015 CLS_SUBTABLE_CACHE_FOR_EACH (subtable, iter, &cls->subtables_priority) {
1016 struct cls_match *rule;
1018 rule = find_match_miniflow(subtable, flow,
1019 miniflow_hash_in_minimask(flow,
1023 return rule->cls_rule;
1030 /* Finds and returns a rule in 'cls' with exactly the same priority and
1031 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1032 * contain an exact match. */
1034 classifier_find_rule_exactly(const struct classifier *cls_,
1035 const struct cls_rule *target)
1037 struct cls_classifier *cls = cls_->cls;
1038 struct cls_match *head, *rule;
1039 struct cls_subtable *subtable;
1041 subtable = find_subtable(cls, &target->match.mask);
1046 /* Skip if there is no hope. */
1047 if (target->priority > subtable->max_priority) {
1051 head = find_equal(subtable, &target->match.flow,
1052 miniflow_hash_in_minimask(&target->match.flow,
1053 &target->match.mask, 0));
1054 FOR_EACH_RULE_IN_LIST (rule, head) {
1055 if (target->priority >= rule->priority) {
1056 return target->priority == rule->priority ? rule->cls_rule : NULL;
1062 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
1063 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
1064 * contain an exact match. */
1066 classifier_find_match_exactly(const struct classifier *cls,
1067 const struct match *target,
1068 unsigned int priority)
1070 struct cls_rule *retval;
1073 cls_rule_init(&cr, target, priority);
1074 retval = classifier_find_rule_exactly(cls, &cr);
1075 cls_rule_destroy(&cr);
1080 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
1081 * considered to overlap if both rules have the same priority and a packet
1082 * could match both. */
1084 classifier_rule_overlaps(const struct classifier *cls_,
1085 const struct cls_rule *target)
1087 struct cls_classifier *cls = cls_->cls;
1088 struct cls_subtable *subtable;
1089 struct cls_subtable_entry *iter;
1091 /* Iterate subtables in the descending max priority order. */
1092 CLS_SUBTABLE_CACHE_FOR_EACH (subtable, iter, &cls->subtables_priority) {
1093 uint32_t storage[FLOW_U32S];
1094 struct minimask mask;
1095 struct cls_match *head;
1097 if (target->priority > iter->max_priority) {
1098 break; /* Can skip this and the rest of the subtables. */
1101 minimask_combine(&mask, &target->match.mask, &subtable->mask, storage);
1102 HMAP_FOR_EACH (head, hmap_node, &subtable->rules) {
1103 struct cls_match *rule;
1105 FOR_EACH_RULE_IN_LIST (rule, head) {
1106 if (rule->priority < target->priority) {
1107 break; /* Rules in descending priority order. */
1109 if (rule->priority == target->priority
1110 && miniflow_equal_in_minimask(&target->match.flow,
1111 &rule->match.flow, &mask)) {
1121 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
1122 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
1123 * function returns true if, for every field:
1125 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
1128 * - 'criteria' wildcards the field,
1130 * Conversely, 'rule' does not match 'criteria' and this function returns false
1131 * if, for at least one field:
1133 * - 'criteria' and 'rule' specify different values for the field, or
1135 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
1137 * Equivalently, the truth table for whether a field matches is:
1142 * r +---------+---------+
1143 * i wild | yes | yes |
1145 * e +---------+---------+
1146 * r exact | no |if values|
1148 * a +---------+---------+
1150 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1151 * commands and by OpenFlow 1.0 aggregate and flow stats.
1153 * Ignores rule->priority. */
1155 cls_rule_is_loose_match(const struct cls_rule *rule,
1156 const struct minimatch *criteria)
1158 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
1159 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
1166 rule_matches(const struct cls_match *rule, const struct cls_rule *target)
1169 || miniflow_equal_in_minimask(&rule->match.flow,
1170 &target->match.flow,
1171 &target->match.mask));
1174 static struct cls_match *
1175 search_subtable(const struct cls_subtable *subtable,
1176 const struct cls_rule *target)
1178 if (!target || !minimask_has_extra(&subtable->mask, &target->match.mask)) {
1179 struct cls_match *rule;
1181 HMAP_FOR_EACH (rule, hmap_node, &subtable->rules) {
1182 if (rule_matches(rule, target)) {
1190 /* Initializes 'cursor' for iterating through rules in 'cls':
1192 * - If 'target' is null, the cursor will visit every rule in 'cls'.
1194 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1195 * such that cls_rule_is_loose_match(rule, target) returns true.
1197 * Ignores target->priority. */
1199 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
1200 const struct cls_rule *target)
1202 cursor->cls = cls->cls;
1203 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
1206 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
1207 * pointer if there are no matches. */
1209 cls_cursor_first(struct cls_cursor *cursor)
1211 struct cls_subtable *subtable;
1213 HMAP_FOR_EACH (subtable, hmap_node, &cursor->cls->subtables) {
1214 struct cls_match *rule = search_subtable(subtable, cursor->target);
1216 cursor->subtable = subtable;
1217 return rule->cls_rule;
1224 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1225 * pointer if there are no more matches. */
1227 cls_cursor_next(struct cls_cursor *cursor, const struct cls_rule *rule_)
1229 struct cls_match *rule = CONST_CAST(struct cls_match *, rule_->cls_match);
1230 const struct cls_subtable *subtable;
1231 struct cls_match *next;
1233 next = next_rule_in_list__(rule);
1234 if (next->priority < rule->priority) {
1235 return next->cls_rule;
1238 /* 'next' is the head of the list, that is, the rule that is included in
1239 * the subtable's hmap. (This is important when the classifier contains
1240 * rules that differ only in priority.) */
1242 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->subtable->rules) {
1243 if (rule_matches(rule, cursor->target)) {
1244 return rule->cls_rule;
1248 subtable = cursor->subtable;
1249 HMAP_FOR_EACH_CONTINUE (subtable, hmap_node, &cursor->cls->subtables) {
1250 rule = search_subtable(subtable, cursor->target);
1252 cursor->subtable = subtable;
1253 return rule->cls_rule;
1260 static struct cls_subtable *
1261 find_subtable(const struct cls_classifier *cls, const struct minimask *mask)
1263 struct cls_subtable *subtable;
1265 HMAP_FOR_EACH_IN_BUCKET (subtable, hmap_node, minimask_hash(mask, 0),
1267 if (minimask_equal(mask, &subtable->mask)) {
1274 static struct cls_subtable *
1275 insert_subtable(struct cls_classifier *cls, const struct minimask *mask)
1277 uint32_t hash = minimask_hash(mask, 0);
1278 struct cls_subtable *subtable;
1280 struct flow_wildcards old, new;
1282 struct cls_subtable_entry elem;
1284 subtable = xzalloc(sizeof *subtable);
1285 hmap_init(&subtable->rules);
1286 minimask_clone(&subtable->mask, mask);
1288 /* Init indices for segmented lookup, if any. */
1289 flow_wildcards_init_catchall(&new);
1292 for (i = 0; i < cls->n_flow_segments; i++) {
1293 flow_wildcards_fold_minimask_range(&new, mask, prev,
1294 cls->flow_segments[i]);
1295 /* Add an index if it adds mask bits. */
1296 if (!flow_wildcards_equal(&new, &old)) {
1297 hindex_init(&subtable->indices[index]);
1298 subtable->index_ofs[index] = cls->flow_segments[i];
1302 prev = cls->flow_segments[i];
1304 /* Check if the rest of the subtable's mask adds any bits,
1305 * and remove the last index if it doesn't. */
1307 flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
1308 if (flow_wildcards_equal(&new, &old)) {
1310 subtable->index_ofs[index] = 0;
1311 hindex_destroy(&subtable->indices[index]);
1314 subtable->n_indices = index;
1316 subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
1317 ? tag_create_deterministic(hash)
1320 for (i = 0; i < cls->n_tries; i++) {
1321 subtable->trie_plen[i] = minimask_get_prefix_len(mask,
1322 cls->tries[i].field);
1325 hmap_insert(&cls->subtables, &subtable->hmap_node, hash);
1326 elem.subtable = subtable;
1327 elem.mask_values = subtable->mask.masks.values;
1328 elem.tag = subtable->tag;
1329 elem.max_priority = subtable->max_priority;
1330 cls_subtable_cache_push_back(&cls->subtables_priority, elem);
1336 destroy_subtable(struct cls_classifier *cls, struct cls_subtable *subtable)
1339 struct cls_subtable *table = NULL;
1340 struct cls_subtable_entry *iter;
1342 CLS_SUBTABLE_CACHE_FOR_EACH (table, iter, &cls->subtables_priority) {
1343 if (table == subtable) {
1344 cls_subtable_cache_remove(&cls->subtables_priority, iter);
1349 for (i = 0; i < subtable->n_indices; i++) {
1350 hindex_destroy(&subtable->indices[i]);
1352 minimask_destroy(&subtable->mask);
1353 hmap_remove(&cls->subtables, &subtable->hmap_node);
1354 hmap_destroy(&subtable->rules);
1358 /* This function performs the following updates for 'subtable' in 'cls'
1359 * following the addition of a new rule with priority 'new_priority' to
1362 * - Update 'subtable->max_priority' and 'subtable->max_count' if necessary.
1364 * - Update 'subtable''s position in 'cls->subtables_priority' if necessary.
1366 * This function should only be called after adding a new rule, not after
1367 * replacing a rule by an identical one or modifying a rule in-place. */
1369 update_subtables_after_insertion(struct cls_classifier *cls,
1370 struct cls_subtable *subtable,
1371 unsigned int new_priority)
1373 if (new_priority == subtable->max_priority) {
1374 ++subtable->max_count;
1375 } else if (new_priority > subtable->max_priority) {
1376 struct cls_subtable *table;
1377 struct cls_subtable_entry *iter, *subtable_iter = NULL;
1379 subtable->max_priority = new_priority;
1380 subtable->max_count = 1;
1382 /* Possibly move 'subtable' earlier in the priority list. If we break
1383 * out of the loop, then 'subtable_iter' should be moved just before
1384 * 'iter'. If the loop terminates normally, then 'iter' will be the
1385 * first list element and we'll move subtable just before that
1386 * (e.g. to the front of the list). */
1387 CLS_SUBTABLE_CACHE_FOR_EACH_REVERSE (table, iter, &cls->subtables_priority) {
1388 if (table == subtable) {
1389 subtable_iter = iter; /* Locate the subtable as we go. */
1390 iter->max_priority = new_priority;
1391 } else if (table->max_priority >= new_priority) {
1392 ovs_assert(subtable_iter != NULL);
1398 /* Move 'subtable' just before 'iter' (unless it's already there). */
1399 if (iter != subtable_iter) {
1400 cls_subtable_cache_splice(iter, subtable_iter, subtable_iter + 1);
1405 /* This function performs the following updates for 'subtable' in 'cls'
1406 * following the deletion of a rule with priority 'del_priority' from
1409 * - Update 'subtable->max_priority' and 'subtable->max_count' if necessary.
1411 * - Update 'subtable''s position in 'cls->subtables_priority' if necessary.
1413 * This function should only be called after removing a rule, not after
1414 * replacing a rule by an identical one or modifying a rule in-place. */
1416 update_subtables_after_removal(struct cls_classifier *cls,
1417 struct cls_subtable *subtable,
1418 unsigned int del_priority)
1420 if (del_priority == subtable->max_priority && --subtable->max_count == 0) {
1421 struct cls_match *head;
1422 struct cls_subtable *table;
1423 struct cls_subtable_entry *iter, *subtable_iter = NULL;
1425 subtable->max_priority = 0;
1426 HMAP_FOR_EACH (head, hmap_node, &subtable->rules) {
1427 if (head->priority > subtable->max_priority) {
1428 subtable->max_priority = head->priority;
1429 subtable->max_count = 1;
1430 } else if (head->priority == subtable->max_priority) {
1431 ++subtable->max_count;
1435 /* Possibly move 'subtable' later in the priority list. If we break
1436 * out of the loop, then 'subtable' should be moved just before that
1437 * 'iter'. If the loop terminates normally, then 'iter' will be the
1438 * list head and we'll move subtable just before that (e.g. to the back
1440 CLS_SUBTABLE_CACHE_FOR_EACH (table, iter, &cls->subtables_priority) {
1441 if (table == subtable) {
1442 subtable_iter = iter; /* Locate the subtable as we go. */
1443 iter->max_priority = subtable->max_priority;
1444 } else if (table->max_priority <= subtable->max_priority) {
1445 ovs_assert(subtable_iter != NULL);
1450 /* Move 'subtable' just before 'iter' (unless it's already there). */
1451 if (iter != subtable_iter) {
1452 cls_subtable_cache_splice(iter, subtable_iter, subtable_iter + 1);
1462 /* Return 'true' if can skip rest of the subtable based on the prefix trie
1463 * lookup results. */
1465 check_tries(struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1466 const unsigned int field_plen[CLS_MAX_TRIES],
1467 const struct range ofs, const struct flow *flow,
1468 struct flow_wildcards *wc)
1472 /* Check if we could avoid fully unwildcarding the next level of
1473 * fields using the prefix tries. The trie checks are done only as
1474 * needed to avoid folding in additional bits to the wildcards mask. */
1475 for (j = 0; j < n_tries; j++) {
1476 /* Is the trie field relevant for this subtable? */
1477 if (field_plen[j]) {
1478 struct trie_ctx *ctx = &trie_ctx[j];
1479 uint8_t be32ofs = ctx->be32ofs;
1481 /* Is the trie field within the current range of fields? */
1482 if (be32ofs >= ofs.start && be32ofs < ofs.end) {
1483 /* On-demand trie lookup. */
1484 if (!ctx->lookup_done) {
1485 ctx->match_plen = trie_lookup(ctx->trie, flow,
1487 ctx->lookup_done = true;
1489 /* Possible to skip the rest of the subtable if subtable's
1490 * prefix on the field is longer than what is known to match
1491 * based on the trie lookup. */
1492 if (field_plen[j] > ctx->match_plen) {
1493 /* RFC: We want the trie lookup to never result in
1494 * unwildcarding any bits that would not be unwildcarded
1495 * otherwise. Since the trie is shared by the whole
1496 * classifier, it is possible that the 'maskbits' contain
1497 * bits that are irrelevant for the partition of the
1498 * classifier relevant for the current flow. */
1500 /* Can skip if the field is already unwildcarded. */
1501 if (mask_prefix_bits_set(wc, be32ofs, ctx->maskbits)) {
1504 /* Check that the trie result will not unwildcard more bits
1505 * than this stage will. */
1506 if (ctx->maskbits <= field_plen[j]) {
1507 /* Unwildcard the bits and skip the rest. */
1508 mask_set_prefix_bits(wc, be32ofs, ctx->maskbits);
1509 /* Note: Prerequisite already unwildcarded, as the only
1510 * prerequisite of the supported trie lookup fields is
1511 * the ethertype, which is currently always
1523 static inline struct cls_match *
1524 find_match(const struct cls_subtable *subtable, const struct flow *flow,
1527 struct cls_match *rule;
1529 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &subtable->rules) {
1530 if (minimatch_matches_flow(&rule->match, flow)) {
1538 static struct cls_match *
1539 find_match_wc(const struct cls_subtable *subtable, const struct flow *flow,
1540 struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1541 struct flow_wildcards *wc)
1543 uint32_t basis = 0, hash;
1544 struct cls_match *rule = NULL;
1548 if (OVS_UNLIKELY(!wc)) {
1549 return find_match(subtable, flow,
1550 flow_hash_in_minimask(flow, &subtable->mask, 0));
1554 /* Try to finish early by checking fields in segments. */
1555 for (i = 0; i < subtable->n_indices; i++) {
1556 struct hindex_node *inode;
1557 ofs.end = subtable->index_ofs[i];
1559 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow,
1563 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1565 ofs.start = ofs.end;
1566 inode = hindex_node_with_hash(&subtable->indices[i], hash);
1568 /* No match, can stop immediately, but must fold in the mask
1569 * covered so far. */
1573 /* If we have narrowed down to a single rule already, check whether
1574 * that rule matches. If it does match, then we're done. If it does
1575 * not match, then we know that we will never get a match, but we do
1576 * not yet know how many wildcards we need to fold into 'wc' so we
1577 * continue iterating through indices to find that out. (We won't
1578 * waste time calling minimatch_matches_flow() again because we've set
1581 * This check shows a measurable benefit with non-trivial flow tables.
1583 * (Rare) hash collisions may cause us to miss the opportunity for this
1585 if (!inode->s && !rule) {
1586 ASSIGN_CONTAINER(rule, inode - i, index_nodes);
1587 if (minimatch_matches_flow(&rule->match, flow)) {
1592 ofs.end = FLOW_U32S;
1593 /* Trie check for the final range. */
1594 if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow, wc)) {
1598 /* Multiple potential matches exist, look for one. */
1599 hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1601 rule = find_match(subtable, flow, hash);
1603 /* We already narrowed the matching candidates down to just 'rule',
1604 * but it didn't match. */
1608 /* Must unwildcard all the fields, as they were looked at. */
1609 flow_wildcards_fold_minimask(wc, &subtable->mask);
1613 /* Must unwildcard the fields looked up so far, if any. */
1615 flow_wildcards_fold_minimask_range(wc, &subtable->mask, 0, ofs.start);
1620 static struct cls_match *
1621 find_equal(struct cls_subtable *subtable, const struct miniflow *flow,
1624 struct cls_match *head;
1626 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &subtable->rules) {
1627 if (miniflow_equal(&head->match.flow, flow)) {
1634 static struct cls_match *
1635 insert_rule(struct cls_classifier *cls, struct cls_subtable *subtable,
1636 struct cls_rule *new)
1638 struct cls_match *cls_match = cls_match_alloc(new);
1639 struct cls_match *head;
1640 struct cls_match *old = NULL;
1642 uint32_t basis = 0, hash;
1643 uint8_t prev_be32ofs = 0;
1645 /* Add new node to segment indices. */
1646 for (i = 0; i < subtable->n_indices; i++) {
1647 hash = minimatch_hash_range(&new->match, prev_be32ofs,
1648 subtable->index_ofs[i], &basis);
1649 hindex_insert(&subtable->indices[i], &cls_match->index_nodes[i], hash);
1650 prev_be32ofs = subtable->index_ofs[i];
1652 hash = minimatch_hash_range(&new->match, prev_be32ofs, FLOW_U32S, &basis);
1653 head = find_equal(subtable, &new->match.flow, hash);
1655 hmap_insert(&subtable->rules, &cls_match->hmap_node, hash);
1656 list_init(&cls_match->list);
1659 /* Scan the list for the insertion point that will keep the list in
1660 * order of decreasing priority. */
1661 struct cls_match *rule;
1663 cls_match->hmap_node.hash = hash; /* Otherwise done by hmap_insert. */
1665 FOR_EACH_RULE_IN_LIST (rule, head) {
1666 if (cls_match->priority >= rule->priority) {
1668 /* 'new' is the new highest-priority flow in the list. */
1669 hmap_replace(&subtable->rules,
1670 &rule->hmap_node, &cls_match->hmap_node);
1673 if (cls_match->priority == rule->priority) {
1674 list_replace(&cls_match->list, &rule->list);
1678 list_insert(&rule->list, &cls_match->list);
1684 /* Insert 'new' at the end of the list. */
1685 list_push_back(&head->list, &cls_match->list);
1690 update_subtables_after_insertion(cls, subtable, cls_match->priority);
1692 /* Remove old node from indices. */
1693 for (i = 0; i < subtable->n_indices; i++) {
1694 hindex_remove(&subtable->indices[i], &old->index_nodes[i]);
1700 static struct cls_match *
1701 next_rule_in_list__(struct cls_match *rule)
1703 struct cls_match *next = OBJECT_CONTAINING(rule->list.next, next, list);
1707 static struct cls_match *
1708 next_rule_in_list(struct cls_match *rule)
1710 struct cls_match *next = next_rule_in_list__(rule);
1711 return next->priority < rule->priority ? next : NULL;
1714 /* A longest-prefix match tree. */
1716 uint32_t prefix; /* Prefix bits for this node, MSB first. */
1717 uint8_t nbits; /* Never zero, except for the root node. */
1718 unsigned int n_rules; /* Number of rules that have this prefix. */
1719 struct trie_node *edges[2]; /* Both NULL if leaf. */
1722 /* Max bits per node. Must fit in struct trie_node's 'prefix'.
1723 * Also tested with 16, 8, and 5 to stress the implementation. */
1724 #define TRIE_PREFIX_BITS 32
1726 /* Return at least 'plen' bits of the 'prefix', starting at bit offset 'ofs'.
1727 * Prefixes are in the network byte order, and the offset 0 corresponds to
1728 * the most significant bit of the first byte. The offset can be read as
1729 * "how many bits to skip from the start of the prefix starting at 'pr'". */
1731 raw_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1735 pr += ofs / 32; /* Where to start. */
1736 ofs %= 32; /* How many bits to skip at 'pr'. */
1738 prefix = ntohl(*pr) << ofs; /* Get the first 32 - ofs bits. */
1739 if (plen > 32 - ofs) { /* Need more than we have already? */
1740 prefix |= ntohl(*++pr) >> (32 - ofs);
1742 /* Return with possible unwanted bits at the end. */
1746 /* Return min(TRIE_PREFIX_BITS, plen) bits of the 'prefix', starting at bit
1747 * offset 'ofs'. Prefixes are in the network byte order, and the offset 0
1748 * corresponds to the most significant bit of the first byte. The offset can
1749 * be read as "how many bits to skip from the start of the prefix starting at
1752 trie_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1757 if (plen > TRIE_PREFIX_BITS) {
1758 plen = TRIE_PREFIX_BITS; /* Get at most TRIE_PREFIX_BITS. */
1760 /* Return with unwanted bits cleared. */
1761 return raw_get_prefix(pr, ofs, plen) & ~0u << (32 - plen);
1764 /* Return the number of equal bits in 'nbits' of 'prefix's MSBs and a 'value'
1765 * starting at "MSB 0"-based offset 'ofs'. */
1767 prefix_equal_bits(uint32_t prefix, unsigned int nbits, const ovs_be32 value[],
1770 uint64_t diff = prefix ^ raw_get_prefix(value, ofs, nbits);
1771 /* Set the bit after the relevant bits to limit the result. */
1772 return raw_clz64(diff << 32 | UINT64_C(1) << (63 - nbits));
1775 /* Return the number of equal bits in 'node' prefix and a 'prefix' of length
1776 * 'plen', starting at "MSB 0"-based offset 'ofs'. */
1778 trie_prefix_equal_bits(const struct trie_node *node, const ovs_be32 prefix[],
1779 unsigned int ofs, unsigned int plen)
1781 return prefix_equal_bits(node->prefix, MIN(node->nbits, plen - ofs),
1785 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' can
1786 * be greater than 31. */
1788 be_get_bit_at(const ovs_be32 value[], unsigned int ofs)
1790 return (((const uint8_t *)value)[ofs / 8] >> (7 - ofs % 8)) & 1u;
1793 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int. 'ofs' must
1794 * be between 0 and 31, inclusive. */
1796 get_bit_at(const uint32_t prefix, unsigned int ofs)
1798 return (prefix >> (31 - ofs)) & 1u;
1801 /* Create new branch. */
1802 static struct trie_node *
1803 trie_branch_create(const ovs_be32 *prefix, unsigned int ofs, unsigned int plen,
1804 unsigned int n_rules)
1806 struct trie_node *node = xmalloc(sizeof *node);
1808 node->prefix = trie_get_prefix(prefix, ofs, plen);
1810 if (plen <= TRIE_PREFIX_BITS) {
1812 node->edges[0] = NULL;
1813 node->edges[1] = NULL;
1814 node->n_rules = n_rules;
1815 } else { /* Need intermediate nodes. */
1816 struct trie_node *subnode = trie_branch_create(prefix,
1817 ofs + TRIE_PREFIX_BITS,
1818 plen - TRIE_PREFIX_BITS,
1820 int bit = get_bit_at(subnode->prefix, 0);
1821 node->nbits = TRIE_PREFIX_BITS;
1822 node->edges[bit] = subnode;
1823 node->edges[!bit] = NULL;
1830 trie_node_destroy(struct trie_node *node)
1836 trie_destroy(struct trie_node *node)
1839 trie_destroy(node->edges[0]);
1840 trie_destroy(node->edges[1]);
1846 trie_is_leaf(const struct trie_node *trie)
1848 return !trie->edges[0] && !trie->edges[1]; /* No children. */
1852 mask_set_prefix_bits(struct flow_wildcards *wc, uint8_t be32ofs,
1855 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1858 for (i = 0; i < nbits / 32; i++) {
1859 mask[i] = OVS_BE32_MAX;
1862 mask[i] |= htonl(~0u << (32 - nbits % 32));
1867 mask_prefix_bits_set(const struct flow_wildcards *wc, uint8_t be32ofs,
1870 ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1872 ovs_be32 zeroes = 0;
1874 for (i = 0; i < nbits / 32; i++) {
1878 zeroes |= ~mask[i] & htonl(~0u << (32 - nbits % 32));
1881 return !zeroes; /* All 'nbits' bits set. */
1884 static struct trie_node **
1885 trie_next_edge(struct trie_node *node, const ovs_be32 value[],
1888 return node->edges + be_get_bit_at(value, ofs);
1891 static const struct trie_node *
1892 trie_next_node(const struct trie_node *node, const ovs_be32 value[],
1895 return node->edges[be_get_bit_at(value, ofs)];
1898 /* Return the prefix mask length necessary to find the longest-prefix match for
1899 * the '*value' in the prefix tree 'node'.
1900 * '*checkbits' is set to the number of bits in the prefix mask necessary to
1901 * determine a mismatch, in case there are longer prefixes in the tree below
1902 * the one that matched.
1905 trie_lookup_value(const struct trie_node *node, const ovs_be32 value[],
1906 unsigned int *checkbits)
1908 unsigned int plen = 0, match_len = 0;
1909 const struct trie_node *prev = NULL;
1911 for (; node; prev = node, node = trie_next_node(node, value, plen)) {
1912 unsigned int eqbits;
1913 /* Check if this edge can be followed. */
1914 eqbits = prefix_equal_bits(node->prefix, node->nbits, value, plen);
1916 if (eqbits < node->nbits) { /* Mismatch, nothing more to be found. */
1917 /* Bit at offset 'plen' differed. */
1918 *checkbits = plen + 1; /* Includes the first mismatching bit. */
1921 /* Full match, check if rules exist at this prefix length. */
1922 if (node->n_rules > 0) {
1926 /* Dead end, exclude the other branch if it exists. */
1927 *checkbits = !prev || trie_is_leaf(prev) ? plen : plen + 1;
1932 trie_lookup(const struct cls_trie *trie, const struct flow *flow,
1933 unsigned int *checkbits)
1935 const struct mf_field *mf = trie->field;
1937 /* Check that current flow matches the prerequisites for the trie
1938 * field. Some match fields are used for multiple purposes, so we
1939 * must check that the trie is relevant for this flow. */
1940 if (mf_are_prereqs_ok(mf, flow)) {
1941 return trie_lookup_value(trie->root,
1942 &((ovs_be32 *)flow)[mf->flow_be32ofs],
1945 *checkbits = 0; /* Value not used in this case. */
1949 /* Returns the length of a prefix match mask for the field 'mf' in 'minimask'.
1950 * Returns the u32 offset to the miniflow data in '*miniflow_index', if
1951 * 'miniflow_index' is not NULL. */
1953 minimask_get_prefix_len(const struct minimask *minimask,
1954 const struct mf_field *mf)
1956 unsigned int nbits = 0, mask_tz = 0; /* Non-zero when end of mask seen. */
1957 uint8_t u32_ofs = mf->flow_be32ofs;
1958 uint8_t u32_end = u32_ofs + mf->n_bytes / 4;
1960 for (; u32_ofs < u32_end; ++u32_ofs) {
1962 mask = ntohl((OVS_FORCE ovs_be32)minimask_get(minimask, u32_ofs));
1964 /* Validate mask, count the mask length. */
1967 return 0; /* No bits allowed after mask ended. */
1970 if (~mask & (~mask + 1)) {
1971 return 0; /* Mask not contiguous. */
1973 mask_tz = ctz32(mask);
1974 nbits += 32 - mask_tz;
1982 * This is called only when mask prefix is known to be CIDR and non-zero.
1983 * Relies on the fact that the flow and mask have the same map, and since
1984 * the mask is CIDR, the storage for the flow field exists even if it
1985 * happened to be zeros.
1987 static const ovs_be32 *
1988 minimatch_get_prefix(const struct minimatch *match, const struct mf_field *mf)
1990 return match->flow.values +
1991 count_1bits(match->flow.map & ((UINT64_C(1) << mf->flow_be32ofs) - 1));
1994 /* Insert rule in to the prefix tree.
1995 * 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
1998 trie_insert(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2000 const ovs_be32 *prefix = minimatch_get_prefix(&rule->match, trie->field);
2001 struct trie_node *node;
2002 struct trie_node **edge;
2005 /* Walk the tree. */
2006 for (edge = &trie->root;
2007 (node = *edge) != NULL;
2008 edge = trie_next_edge(node, prefix, ofs)) {
2009 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2011 if (eqbits < node->nbits) {
2012 /* Mismatch, new node needs to be inserted above. */
2013 int old_branch = get_bit_at(node->prefix, eqbits);
2015 /* New parent node. */
2016 *edge = trie_branch_create(prefix, ofs - eqbits, eqbits,
2017 ofs == mlen ? 1 : 0);
2019 /* Adjust old node for its new position in the tree. */
2020 node->prefix <<= eqbits;
2021 node->nbits -= eqbits;
2022 (*edge)->edges[old_branch] = node;
2024 /* Check if need a new branch for the new rule. */
2026 (*edge)->edges[!old_branch]
2027 = trie_branch_create(prefix, ofs, mlen - ofs, 1);
2031 /* Full match so far. */
2034 /* Full match at the current node, rule needs to be added here. */
2039 /* Must insert a new tree branch for the new rule. */
2040 *edge = trie_branch_create(prefix, ofs, mlen - ofs, 1);
2043 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2046 trie_remove(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2048 const ovs_be32 *prefix = minimatch_get_prefix(&rule->match, trie->field);
2049 struct trie_node *node;
2050 struct trie_node **edges[sizeof(union mf_value) * 8];
2051 int depth = 0, ofs = 0;
2053 /* Walk the tree. */
2054 for (edges[depth] = &trie->root;
2055 (node = *edges[depth]) != NULL;
2056 edges[++depth] = trie_next_edge(node, prefix, ofs)) {
2057 unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2058 if (eqbits < node->nbits) {
2059 /* Mismatch, nothing to be removed. This should never happen, as
2060 * only rules in the classifier are ever removed. */
2061 break; /* Log a warning. */
2063 /* Full match so far. */
2067 /* Full prefix match at the current node, remove rule here. */
2068 if (!node->n_rules) {
2069 break; /* Log a warning. */
2073 /* Check if can prune the tree. */
2074 while (!node->n_rules && !(node->edges[0] && node->edges[1])) {
2075 /* No rules and at most one child node, remove this node. */
2076 struct trie_node *next;
2077 next = node->edges[0] ? node->edges[0] : node->edges[1];
2080 if (node->nbits + next->nbits > TRIE_PREFIX_BITS) {
2081 break; /* Cannot combine. */
2083 /* Combine node with next. */
2084 next->prefix = node->prefix | next->prefix >> node->nbits;
2085 next->nbits += node->nbits;
2087 trie_node_destroy(node);
2088 /* Update the parent's edge. */
2089 *edges[depth] = next;
2090 if (next || !depth) {
2091 /* Branch not pruned or at root, nothing more to do. */
2094 node = *edges[--depth];
2099 /* Cannot go deeper. This should never happen, since only rules
2100 * that actually exist in the classifier are ever removed. */
2101 VLOG_WARN("Trying to remove non-existing rule from a prefix trie.");