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 'rule' 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 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
101 * normally embedded into a larger structure).
103 * ('rule' must not currently be in a classifier.) */
105 cls_rule_destroy(struct cls_rule *rule)
107 minimatch_destroy(&rule->match);
110 /* Returns true if 'a' and 'b' match the same packets at the same priority,
111 * false if they differ in some way. */
113 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
115 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
118 /* Returns a hash value for 'rule', folding in 'basis'. */
120 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
122 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
125 /* Appends a string describing 'rule' to 's'. */
127 cls_rule_format(const struct cls_rule *rule, struct ds *s)
129 minimatch_format(&rule->match, s, rule->priority);
132 /* Returns true if 'rule' matches every packet, false otherwise. */
134 cls_rule_is_catchall(const struct cls_rule *rule)
136 return minimask_is_catchall(&rule->match.mask);
139 /* Initializes 'cls' as a classifier that initially contains no classification
142 classifier_init(struct classifier *cls)
145 hmap_init(&cls->tables);
146 list_init(&cls->tables_priority);
147 ovs_rwlock_init(&cls->rwlock);
150 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
151 * caller's responsibility. */
153 classifier_destroy(struct classifier *cls)
156 struct cls_table *table, *next_table;
158 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
159 destroy_table(cls, table);
161 hmap_destroy(&cls->tables);
162 ovs_rwlock_destroy(&cls->rwlock);
166 /* Returns true if 'cls' contains no classification rules, false otherwise. */
168 classifier_is_empty(const struct classifier *cls)
170 return cls->n_rules == 0;
173 /* Returns the number of rules in 'cls'. */
175 classifier_count(const struct classifier *cls)
180 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
181 * must not modify or free it.
183 * If 'cls' already contains an identical rule (including wildcards, values of
184 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
185 * rule that was replaced. The caller takes ownership of the returned rule and
186 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
187 * memory block in which it resides, etc., as necessary.
189 * Returns NULL if 'cls' does not contain a rule with an identical key, after
190 * inserting the new rule. In this case, no rules are displaced by the new
191 * rule, even rules that cannot have any effect because the new rule matches a
192 * superset of their flows and has higher priority. */
194 classifier_replace(struct classifier *cls, struct cls_rule *rule)
196 struct cls_rule *old_rule;
197 struct cls_table *table;
199 table = find_table(cls, &rule->match.mask);
201 table = insert_table(cls, &rule->match.mask);
204 old_rule = insert_rule(cls, table, rule);
206 table->n_table_rules++;
212 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
213 * must not modify or free it.
215 * 'cls' must not contain an identical rule (including wildcards, values of
216 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
219 classifier_insert(struct classifier *cls, struct cls_rule *rule)
221 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
222 ovs_assert(!displaced_rule);
225 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
226 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
227 * resides, etc., as necessary. */
229 classifier_remove(struct classifier *cls, struct cls_rule *rule)
231 struct cls_rule *head;
232 struct cls_table *table;
234 table = find_table(cls, &rule->match.mask);
235 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
237 list_remove(&rule->list);
238 } else if (list_is_empty(&rule->list)) {
239 hmap_remove(&table->rules, &rule->hmap_node);
241 struct cls_rule *next = CONTAINER_OF(rule->list.next,
242 struct cls_rule, list);
244 list_remove(&rule->list);
245 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
248 if (--table->n_table_rules == 0) {
249 destroy_table(cls, table);
251 update_tables_after_removal(cls, table, rule->priority);
256 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
257 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
258 * of equal priority match 'flow', returns one arbitrarily.
260 * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
261 * set of bits that were significant in the lookup. At some point
262 * earlier, 'wc' should have been initialized (e.g., by
263 * flow_wildcards_init_catchall()). */
265 classifier_lookup(const struct classifier *cls, const struct flow *flow,
266 struct flow_wildcards *wc)
268 struct cls_table *table;
269 struct cls_rule *best;
272 LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
273 struct cls_rule *rule = find_match(table, flow);
276 flow_wildcards_fold_minimask(wc, &table->mask);
280 LIST_FOR_EACH_CONTINUE (table, list_node, &cls->tables_priority) {
281 if (table->max_priority <= best->priority) {
282 /* Tables in descending priority order,
283 * can not find anything better. */
286 rule = find_match(table, flow);
288 flow_wildcards_fold_minimask(wc, &table->mask);
290 if (rule && rule->priority > best->priority) {
300 /* Finds and returns a rule in 'cls' with exactly the same priority and
301 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
302 * contain an exact match. */
304 classifier_find_rule_exactly(const struct classifier *cls,
305 const struct cls_rule *target)
307 struct cls_rule *head, *rule;
308 struct cls_table *table;
310 table = find_table(cls, &target->match.mask);
315 /* Skip if there is no hope. */
316 if (target->priority > table->max_priority) {
320 head = find_equal(table, &target->match.flow,
321 miniflow_hash_in_minimask(&target->match.flow,
322 &target->match.mask, 0));
323 FOR_EACH_RULE_IN_LIST (rule, head) {
324 if (target->priority >= rule->priority) {
325 return target->priority == rule->priority ? rule : NULL;
331 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
332 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
333 * contain an exact match. */
335 classifier_find_match_exactly(const struct classifier *cls,
336 const struct match *target,
337 unsigned int priority)
339 struct cls_rule *retval;
342 cls_rule_init(&cr, target, priority);
343 retval = classifier_find_rule_exactly(cls, &cr);
344 cls_rule_destroy(&cr);
349 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
350 * considered to overlap if both rules have the same priority and a packet
351 * could match both. */
353 classifier_rule_overlaps(const struct classifier *cls,
354 const struct cls_rule *target)
356 struct cls_table *table;
358 /* Iterate tables in the descending max priority order. */
359 LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
360 uint32_t storage[FLOW_U32S];
361 struct minimask mask;
362 struct cls_rule *head;
364 if (target->priority > table->max_priority) {
365 break; /* Can skip this and the rest of the tables. */
368 minimask_combine(&mask, &target->match.mask, &table->mask, storage);
369 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
370 struct cls_rule *rule;
372 FOR_EACH_RULE_IN_LIST (rule, head) {
373 if (rule->priority < target->priority) {
374 break; /* Rules in descending priority order. */
376 if (rule->priority == target->priority
377 && miniflow_equal_in_minimask(&target->match.flow,
378 &rule->match.flow, &mask)) {
388 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
389 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
390 * function returns true if, for every field:
392 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
395 * - 'criteria' wildcards the field,
397 * Conversely, 'rule' does not match 'criteria' and this function returns false
398 * if, for at least one field:
400 * - 'criteria' and 'rule' specify different values for the field, or
402 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
404 * Equivalently, the truth table for whether a field matches is:
409 * r +---------+---------+
410 * i wild | yes | yes |
412 * e +---------+---------+
413 * r exact | no |if values|
415 * a +---------+---------+
417 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
418 * commands and by OpenFlow 1.0 aggregate and flow stats.
420 * Ignores rule->priority. */
422 cls_rule_is_loose_match(const struct cls_rule *rule,
423 const struct minimatch *criteria)
425 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
426 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
433 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
436 || miniflow_equal_in_minimask(&rule->match.flow,
438 &target->match.mask));
441 static struct cls_rule *
442 search_table(const struct cls_table *table, const struct cls_rule *target)
444 if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
445 struct cls_rule *rule;
447 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
448 if (rule_matches(rule, target)) {
456 /* Initializes 'cursor' for iterating through rules in 'cls':
458 * - If 'target' is null, the cursor will visit every rule in 'cls'.
460 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
461 * such that cls_rule_is_loose_match(rule, target) returns true.
463 * Ignores target->priority. */
465 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
466 const struct cls_rule *target)
469 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
472 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
473 * pointer if there are no matches. */
475 cls_cursor_first(struct cls_cursor *cursor)
477 struct cls_table *table;
479 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
480 struct cls_rule *rule = search_table(table, cursor->target);
482 cursor->table = table;
490 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
491 * pointer if there are no more matches. */
493 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
495 const struct cls_table *table;
496 struct cls_rule *next;
498 next = next_rule_in_list__(rule);
499 if (next->priority < rule->priority) {
503 /* 'next' is the head of the list, that is, the rule that is included in
504 * the table's hmap. (This is important when the classifier contains rules
505 * that differ only in priority.) */
507 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
508 if (rule_matches(rule, cursor->target)) {
513 table = cursor->table;
514 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
515 rule = search_table(table, cursor->target);
517 cursor->table = table;
525 static struct cls_table *
526 find_table(const struct classifier *cls, const struct minimask *mask)
528 struct cls_table *table;
530 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
532 if (minimask_equal(mask, &table->mask)) {
539 static struct cls_table *
540 insert_table(struct classifier *cls, const struct minimask *mask)
542 struct cls_table *table;
544 table = xzalloc(sizeof *table);
545 hmap_init(&table->rules);
546 minimask_clone(&table->mask, mask);
547 hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
548 list_push_back(&cls->tables_priority, &table->list_node);
554 destroy_table(struct classifier *cls, struct cls_table *table)
556 minimask_destroy(&table->mask);
557 hmap_remove(&cls->tables, &table->hmap_node);
558 hmap_destroy(&table->rules);
559 list_remove(&table->list_node);
563 /* This function performs the following updates for 'table' in 'cls' following
564 * the addition of a new rule with priority 'new_priority' to 'table':
566 * - Update 'table->max_priority' and 'table->max_count' if necessary.
568 * - Update 'table''s position in 'cls->tables_priority' if necessary.
570 * This function should only be called after adding a new rule, not after
571 * replacing a rule by an identical one or modifying a rule in-place. */
573 update_tables_after_insertion(struct classifier *cls, struct cls_table *table,
574 unsigned int new_priority)
576 if (new_priority == table->max_priority) {
578 } else if (new_priority > table->max_priority) {
579 struct cls_table *iter;
581 table->max_priority = new_priority;
582 table->max_count = 1;
584 /* Possibly move 'table' earlier in the priority list. If we break out
585 * of the loop, then 'table' should be moved just after that 'iter'.
586 * If the loop terminates normally, then 'iter' will be the list head
587 * and we'll move table just after that (e.g. to the front of the
590 LIST_FOR_EACH_REVERSE_CONTINUE (iter, list_node,
591 &cls->tables_priority) {
592 if (iter->max_priority >= table->max_priority) {
597 /* Move 'table' just after 'iter' (unless it's already there). */
598 if (iter->list_node.next != &table->list_node) {
599 list_splice(iter->list_node.next,
600 &table->list_node, table->list_node.next);
605 /* This function performs the following updates for 'table' in 'cls' following
606 * the deletion of a rule with priority 'del_priority' from 'table':
608 * - Update 'table->max_priority' and 'table->max_count' if necessary.
610 * - Update 'table''s position in 'cls->tables_priority' if necessary.
612 * This function should only be called after removing a rule, not after
613 * replacing a rule by an identical one or modifying a rule in-place. */
615 update_tables_after_removal(struct classifier *cls, struct cls_table *table,
616 unsigned int del_priority)
618 struct cls_table *iter;
620 if (del_priority == table->max_priority && --table->max_count == 0) {
621 struct cls_rule *head;
623 table->max_priority = 0;
624 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
625 if (head->priority > table->max_priority) {
626 table->max_priority = head->priority;
627 table->max_count = 1;
628 } else if (head->priority == table->max_priority) {
633 /* Possibly move 'table' later in the priority list. If we break out
634 * of the loop, then 'table' should be moved just before that 'iter'.
635 * If the loop terminates normally, then 'iter' will be the list head
636 * and we'll move table just before that (e.g. to the back of the
639 LIST_FOR_EACH_CONTINUE (iter, list_node, &cls->tables_priority) {
640 if (iter->max_priority <= table->max_priority) {
645 /* Move 'table' just before 'iter' (unless it's already there). */
646 if (iter->list_node.prev != &table->list_node) {
647 list_splice(&iter->list_node,
648 &table->list_node, table->list_node.next);
653 static struct cls_rule *
654 find_match(const struct cls_table *table, const struct flow *flow)
656 uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
657 struct cls_rule *rule;
659 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) {
660 if (miniflow_equal_flow_in_minimask(&rule->match.flow, flow,
669 static struct cls_rule *
670 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
672 struct cls_rule *head;
674 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
675 if (miniflow_equal(&head->match.flow, flow)) {
682 static struct cls_rule *
683 insert_rule(struct classifier *cls,
684 struct cls_table *table, struct cls_rule *new)
686 struct cls_rule *head;
687 struct cls_rule *old = NULL;
689 new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
690 &new->match.mask, 0);
692 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
694 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
695 list_init(&new->list);
698 /* Scan the list for the insertion point that will keep the list in
699 * order of decreasing priority. */
700 struct cls_rule *rule;
701 FOR_EACH_RULE_IN_LIST (rule, head) {
702 if (new->priority >= rule->priority) {
704 /* 'new' is the new highest-priority flow in the list. */
705 hmap_replace(&table->rules,
706 &rule->hmap_node, &new->hmap_node);
709 if (new->priority == rule->priority) {
710 list_replace(&new->list, &rule->list);
714 list_insert(&rule->list, &new->list);
720 /* Insert 'new' at the end of the list. */
721 list_push_back(&head->list, &new->list);
726 update_tables_after_insertion(cls, table, new->priority);
731 static struct cls_rule *
732 next_rule_in_list__(struct cls_rule *rule)
734 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
738 static struct cls_rule *
739 next_rule_in_list(struct cls_rule *rule)
741 struct cls_rule *next = next_rule_in_list__(rule);
742 return next->priority < rule->priority ? next : NULL;