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"
28 #include "ovs-thread.h"
30 static struct cls_table *find_table(const struct classifier *,
31 const struct minimask *);
32 static struct cls_table *insert_table(struct classifier *,
33 const struct minimask *);
35 static void destroy_table(struct classifier *, struct cls_table *);
37 static void update_tables_after_insertion(struct classifier *,
39 unsigned int new_priority);
40 static void update_tables_after_removal(struct classifier *,
42 unsigned int del_priority);
44 static struct cls_rule *find_match(const struct cls_table *,
46 static struct cls_rule *find_equal(struct cls_table *,
47 const struct miniflow *, uint32_t hash);
48 static struct cls_rule *insert_rule(struct classifier *,
49 struct cls_table *, struct cls_rule *);
51 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
52 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
53 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
54 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
55 for ((RULE) = (HEAD); \
56 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
59 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
60 static struct cls_rule *next_rule_in_list(struct cls_rule *);
64 /* Initializes 'rule' to match packets specified by 'match' at the given
65 * 'priority'. 'match' must satisfy the invariant described in the comment at
66 * the definition of struct match.
68 * The caller must eventually destroy 'rule' with cls_rule_destroy().
70 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
71 * internally Open vSwitch supports a wider range.) */
73 cls_rule_init(struct cls_rule *rule,
74 const struct match *match, unsigned int priority)
76 minimatch_init(&rule->match, match);
77 rule->priority = priority;
80 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
82 cls_rule_init_from_minimatch(struct cls_rule *rule,
83 const struct minimatch *match,
84 unsigned int priority)
86 minimatch_clone(&rule->match, match);
87 rule->priority = priority;
90 /* Initializes 'dst' as a copy of 'src'.
92 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
94 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
96 minimatch_clone(&dst->match, &src->match);
97 dst->priority = src->priority;
100 /* Initializes 'dst' with the data in 'src', destroying 'src'.
102 * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
104 cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
106 minimatch_move(&dst->match, &src->match);
107 dst->priority = src->priority;
110 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
111 * normally embedded into a larger structure).
113 * ('rule' must not currently be in a classifier.) */
115 cls_rule_destroy(struct cls_rule *rule)
117 minimatch_destroy(&rule->match);
120 /* Returns true if 'a' and 'b' match the same packets at the same priority,
121 * false if they differ in some way. */
123 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
125 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
128 /* Returns a hash value for 'rule', folding in 'basis'. */
130 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
132 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
135 /* Appends a string describing 'rule' to 's'. */
137 cls_rule_format(const struct cls_rule *rule, struct ds *s)
139 minimatch_format(&rule->match, s, rule->priority);
142 /* Returns true if 'rule' matches every packet, false otherwise. */
144 cls_rule_is_catchall(const struct cls_rule *rule)
146 return minimask_is_catchall(&rule->match.mask);
149 /* Initializes 'cls' as a classifier that initially contains no classification
152 classifier_init(struct classifier *cls)
155 hmap_init(&cls->tables);
156 list_init(&cls->tables_priority);
157 hmap_init(&cls->partitions);
158 ovs_rwlock_init(&cls->rwlock);
161 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
162 * caller's responsibility. */
164 classifier_destroy(struct classifier *cls)
167 struct cls_table *partition, *next_partition;
168 struct cls_table *table, *next_table;
170 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
171 destroy_table(cls, table);
173 hmap_destroy(&cls->tables);
175 HMAP_FOR_EACH_SAFE (partition, next_partition, hmap_node,
177 hmap_remove(&cls->partitions, &partition->hmap_node);
180 hmap_destroy(&cls->partitions);
181 ovs_rwlock_destroy(&cls->rwlock);
185 /* Returns true if 'cls' contains no classification rules, false otherwise. */
187 classifier_is_empty(const struct classifier *cls)
189 return cls->n_rules == 0;
192 /* Returns the number of rules in 'cls'. */
194 classifier_count(const struct classifier *cls)
200 hash_metadata(ovs_be64 metadata_)
202 uint64_t metadata = (OVS_FORCE uint64_t) metadata_;
203 return hash_2words(metadata, metadata >> 32);
206 static struct cls_partition *
207 find_partition(const struct classifier *cls, ovs_be64 metadata, uint32_t hash)
209 struct cls_partition *partition;
211 HMAP_FOR_EACH_IN_BUCKET (partition, hmap_node, hash, &cls->partitions) {
212 if (partition->metadata == metadata) {
220 static struct cls_partition *
221 create_partition(struct classifier *cls, struct cls_table *table,
224 uint32_t hash = hash_metadata(metadata);
225 struct cls_partition *partition = find_partition(cls, metadata, hash);
227 partition = xmalloc(sizeof *partition);
228 partition->metadata = metadata;
230 tag_tracker_init(&partition->tracker);
231 hmap_insert(&cls->partitions, &partition->hmap_node, hash);
233 tag_tracker_add(&partition->tracker, &partition->tags, table->tag);
237 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
238 * must not modify or free it.
240 * If 'cls' already contains an identical rule (including wildcards, values of
241 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
242 * rule that was replaced. The caller takes ownership of the returned rule and
243 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
244 * memory block in which it resides, etc., as necessary.
246 * Returns NULL if 'cls' does not contain a rule with an identical key, after
247 * inserting the new rule. In this case, no rules are displaced by the new
248 * rule, even rules that cannot have any effect because the new rule matches a
249 * superset of their flows and has higher priority. */
251 classifier_replace(struct classifier *cls, struct cls_rule *rule)
253 struct cls_rule *old_rule;
254 struct cls_table *table;
256 table = find_table(cls, &rule->match.mask);
258 table = insert_table(cls, &rule->match.mask);
261 old_rule = insert_rule(cls, table, rule);
263 if (minimask_get_metadata_mask(&rule->match.mask) == OVS_BE64_MAX) {
264 ovs_be64 metadata = miniflow_get_metadata(&rule->match.flow);
265 rule->partition = create_partition(cls, table, metadata);
267 rule->partition = NULL;
270 table->n_table_rules++;
273 rule->partition = old_rule->partition;
278 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
279 * must not modify or free it.
281 * 'cls' must not contain an identical rule (including wildcards, values of
282 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
285 classifier_insert(struct classifier *cls, struct cls_rule *rule)
287 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
288 ovs_assert(!displaced_rule);
291 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
292 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
293 * resides, etc., as necessary. */
295 classifier_remove(struct classifier *cls, struct cls_rule *rule)
297 struct cls_partition *partition;
298 struct cls_rule *head;
299 struct cls_table *table;
301 table = find_table(cls, &rule->match.mask);
302 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
304 list_remove(&rule->list);
305 } else if (list_is_empty(&rule->list)) {
306 hmap_remove(&table->rules, &rule->hmap_node);
308 struct cls_rule *next = CONTAINER_OF(rule->list.next,
309 struct cls_rule, list);
311 list_remove(&rule->list);
312 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
315 partition = rule->partition;
317 tag_tracker_subtract(&partition->tracker, &partition->tags,
319 if (!partition->tags) {
320 hmap_remove(&cls->partitions, &partition->hmap_node);
325 if (--table->n_table_rules == 0) {
326 destroy_table(cls, table);
328 update_tables_after_removal(cls, table, rule->priority);
333 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
334 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
335 * of equal priority match 'flow', returns one arbitrarily.
337 * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
338 * set of bits that were significant in the lookup. At some point
339 * earlier, 'wc' should have been initialized (e.g., by
340 * flow_wildcards_init_catchall()). */
342 classifier_lookup(const struct classifier *cls, const struct flow *flow,
343 struct flow_wildcards *wc)
345 const struct cls_partition *partition;
346 struct cls_table *table;
347 struct cls_rule *best;
350 /* Determine 'tags' such that, if 'table->tag' doesn't intersect them, then
351 * 'flow' cannot possibly match in 'table':
353 * - If flow->metadata maps to a given 'partition', then we can use
354 * 'tags' for 'partition->tags'.
356 * - If flow->metadata has no partition, then no rule in 'cls' has an
357 * exact-match for flow->metadata. That means that we don't need to
358 * search any table that includes flow->metadata in its mask.
360 * In either case, we always need to search any cls_tables that do not
361 * include flow->metadata in its mask. One way to do that would be to
362 * check the "cls_table"s explicitly for that, but that would require an
363 * extra branch per table. Instead, we mark such a cls_table's 'tags' as
364 * TAG_ALL and make sure that 'tags' is never empty. This means that
365 * 'tags' always intersects such a cls_table's 'tags', so we don't need a
368 partition = (hmap_is_empty(&cls->partitions)
370 : find_partition(cls, flow->metadata,
371 hash_metadata(flow->metadata)));
372 tags = partition ? partition->tags : TAG_ARBITRARY;
375 LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
376 struct cls_rule *rule;
378 if (!tag_intersects(tags, table->tag)) {
382 rule = find_match(table, flow);
384 flow_wildcards_fold_minimask(wc, &table->mask);
388 LIST_FOR_EACH_CONTINUE (table, list_node, &cls->tables_priority) {
389 if (table->max_priority <= best->priority) {
390 /* Tables in descending priority order,
391 * can not find anything better. */
394 if (!tag_intersects(tags, table->tag)) {
398 rule = find_match(table, flow);
400 flow_wildcards_fold_minimask(wc, &table->mask);
402 if (rule && rule->priority > best->priority) {
412 /* Finds and returns a rule in 'cls' with exactly the same priority and
413 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
414 * contain an exact match. */
416 classifier_find_rule_exactly(const struct classifier *cls,
417 const struct cls_rule *target)
419 struct cls_rule *head, *rule;
420 struct cls_table *table;
422 table = find_table(cls, &target->match.mask);
427 /* Skip if there is no hope. */
428 if (target->priority > table->max_priority) {
432 head = find_equal(table, &target->match.flow,
433 miniflow_hash_in_minimask(&target->match.flow,
434 &target->match.mask, 0));
435 FOR_EACH_RULE_IN_LIST (rule, head) {
436 if (target->priority >= rule->priority) {
437 return target->priority == rule->priority ? rule : NULL;
443 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
444 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
445 * contain an exact match. */
447 classifier_find_match_exactly(const struct classifier *cls,
448 const struct match *target,
449 unsigned int priority)
451 struct cls_rule *retval;
454 cls_rule_init(&cr, target, priority);
455 retval = classifier_find_rule_exactly(cls, &cr);
456 cls_rule_destroy(&cr);
461 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
462 * considered to overlap if both rules have the same priority and a packet
463 * could match both. */
465 classifier_rule_overlaps(const struct classifier *cls,
466 const struct cls_rule *target)
468 struct cls_table *table;
470 /* Iterate tables in the descending max priority order. */
471 LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
472 uint32_t storage[FLOW_U32S];
473 struct minimask mask;
474 struct cls_rule *head;
476 if (target->priority > table->max_priority) {
477 break; /* Can skip this and the rest of the tables. */
480 minimask_combine(&mask, &target->match.mask, &table->mask, storage);
481 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
482 struct cls_rule *rule;
484 FOR_EACH_RULE_IN_LIST (rule, head) {
485 if (rule->priority < target->priority) {
486 break; /* Rules in descending priority order. */
488 if (rule->priority == target->priority
489 && miniflow_equal_in_minimask(&target->match.flow,
490 &rule->match.flow, &mask)) {
500 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
501 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
502 * function returns true if, for every field:
504 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
507 * - 'criteria' wildcards the field,
509 * Conversely, 'rule' does not match 'criteria' and this function returns false
510 * if, for at least one field:
512 * - 'criteria' and 'rule' specify different values for the field, or
514 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
516 * Equivalently, the truth table for whether a field matches is:
521 * r +---------+---------+
522 * i wild | yes | yes |
524 * e +---------+---------+
525 * r exact | no |if values|
527 * a +---------+---------+
529 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
530 * commands and by OpenFlow 1.0 aggregate and flow stats.
532 * Ignores rule->priority. */
534 cls_rule_is_loose_match(const struct cls_rule *rule,
535 const struct minimatch *criteria)
537 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
538 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
545 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
548 || miniflow_equal_in_minimask(&rule->match.flow,
550 &target->match.mask));
553 static struct cls_rule *
554 search_table(const struct cls_table *table, const struct cls_rule *target)
556 if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
557 struct cls_rule *rule;
559 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
560 if (rule_matches(rule, target)) {
568 /* Initializes 'cursor' for iterating through rules in 'cls':
570 * - If 'target' is null, the cursor will visit every rule in 'cls'.
572 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
573 * such that cls_rule_is_loose_match(rule, target) returns true.
575 * Ignores target->priority. */
577 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
578 const struct cls_rule *target)
581 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
584 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
585 * pointer if there are no matches. */
587 cls_cursor_first(struct cls_cursor *cursor)
589 struct cls_table *table;
591 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
592 struct cls_rule *rule = search_table(table, cursor->target);
594 cursor->table = table;
602 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
603 * pointer if there are no more matches. */
605 cls_cursor_next(struct cls_cursor *cursor, const struct cls_rule *rule_)
607 struct cls_rule *rule = CONST_CAST(struct cls_rule *, rule_);
608 const struct cls_table *table;
609 struct cls_rule *next;
611 next = next_rule_in_list__(rule);
612 if (next->priority < rule->priority) {
616 /* 'next' is the head of the list, that is, the rule that is included in
617 * the table's hmap. (This is important when the classifier contains rules
618 * that differ only in priority.) */
620 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
621 if (rule_matches(rule, cursor->target)) {
626 table = cursor->table;
627 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
628 rule = search_table(table, cursor->target);
630 cursor->table = table;
638 static struct cls_table *
639 find_table(const struct classifier *cls, const struct minimask *mask)
641 struct cls_table *table;
643 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
645 if (minimask_equal(mask, &table->mask)) {
652 static struct cls_table *
653 insert_table(struct classifier *cls, const struct minimask *mask)
655 uint32_t hash = minimask_hash(mask, 0);
656 struct cls_table *table;
658 table = xzalloc(sizeof *table);
659 hmap_init(&table->rules);
660 minimask_clone(&table->mask, mask);
661 hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
662 list_push_back(&cls->tables_priority, &table->list_node);
663 table->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
664 ? tag_create_deterministic(hash)
671 destroy_table(struct classifier *cls, struct cls_table *table)
673 minimask_destroy(&table->mask);
674 hmap_remove(&cls->tables, &table->hmap_node);
675 hmap_destroy(&table->rules);
676 list_remove(&table->list_node);
680 /* This function performs the following updates for 'table' in 'cls' following
681 * the addition of a new rule with priority 'new_priority' to 'table':
683 * - Update 'table->max_priority' and 'table->max_count' if necessary.
685 * - Update 'table''s position in 'cls->tables_priority' if necessary.
687 * This function should only be called after adding a new rule, not after
688 * replacing a rule by an identical one or modifying a rule in-place. */
690 update_tables_after_insertion(struct classifier *cls, struct cls_table *table,
691 unsigned int new_priority)
693 if (new_priority == table->max_priority) {
695 } else if (new_priority > table->max_priority) {
696 struct cls_table *iter;
698 table->max_priority = new_priority;
699 table->max_count = 1;
701 /* Possibly move 'table' earlier in the priority list. If we break out
702 * of the loop, then 'table' should be moved just after that 'iter'.
703 * If the loop terminates normally, then 'iter' will be the list head
704 * and we'll move table just after that (e.g. to the front of the
707 LIST_FOR_EACH_REVERSE_CONTINUE (iter, list_node,
708 &cls->tables_priority) {
709 if (iter->max_priority >= table->max_priority) {
714 /* Move 'table' just after 'iter' (unless it's already there). */
715 if (iter->list_node.next != &table->list_node) {
716 list_splice(iter->list_node.next,
717 &table->list_node, table->list_node.next);
722 /* This function performs the following updates for 'table' in 'cls' following
723 * the deletion of a rule with priority 'del_priority' from 'table':
725 * - Update 'table->max_priority' and 'table->max_count' if necessary.
727 * - Update 'table''s position in 'cls->tables_priority' if necessary.
729 * This function should only be called after removing a rule, not after
730 * replacing a rule by an identical one or modifying a rule in-place. */
732 update_tables_after_removal(struct classifier *cls, struct cls_table *table,
733 unsigned int del_priority)
735 struct cls_table *iter;
737 if (del_priority == table->max_priority && --table->max_count == 0) {
738 struct cls_rule *head;
740 table->max_priority = 0;
741 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
742 if (head->priority > table->max_priority) {
743 table->max_priority = head->priority;
744 table->max_count = 1;
745 } else if (head->priority == table->max_priority) {
750 /* Possibly move 'table' later in the priority list. If we break out
751 * of the loop, then 'table' should be moved just before that 'iter'.
752 * If the loop terminates normally, then 'iter' will be the list head
753 * and we'll move table just before that (e.g. to the back of the
756 LIST_FOR_EACH_CONTINUE (iter, list_node, &cls->tables_priority) {
757 if (iter->max_priority <= table->max_priority) {
762 /* Move 'table' just before 'iter' (unless it's already there). */
763 if (iter->list_node.prev != &table->list_node) {
764 list_splice(&iter->list_node,
765 &table->list_node, table->list_node.next);
770 static struct cls_rule *
771 find_match(const struct cls_table *table, const struct flow *flow)
773 uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
774 struct cls_rule *rule;
776 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) {
777 if (minimatch_matches_flow(&rule->match, flow)) {
785 static struct cls_rule *
786 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
788 struct cls_rule *head;
790 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
791 if (miniflow_equal(&head->match.flow, flow)) {
798 static struct cls_rule *
799 insert_rule(struct classifier *cls,
800 struct cls_table *table, struct cls_rule *new)
802 struct cls_rule *head;
803 struct cls_rule *old = NULL;
805 new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
806 &new->match.mask, 0);
808 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
810 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
811 list_init(&new->list);
814 /* Scan the list for the insertion point that will keep the list in
815 * order of decreasing priority. */
816 struct cls_rule *rule;
817 FOR_EACH_RULE_IN_LIST (rule, head) {
818 if (new->priority >= rule->priority) {
820 /* 'new' is the new highest-priority flow in the list. */
821 hmap_replace(&table->rules,
822 &rule->hmap_node, &new->hmap_node);
825 if (new->priority == rule->priority) {
826 list_replace(&new->list, &rule->list);
830 list_insert(&rule->list, &new->list);
836 /* Insert 'new' at the end of the list. */
837 list_push_back(&head->list, &new->list);
842 update_tables_after_insertion(cls, table, new->priority);
847 static struct cls_rule *
848 next_rule_in_list__(struct cls_rule *rule)
850 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
854 static struct cls_rule *
855 next_rule_in_list(struct cls_rule *rule)
857 struct cls_rule *next = next_rule_in_list__(rule);
858 return next->priority < rule->priority ? next : NULL;