2 * Copyright (c) 2009, 2010, 2011, 2012 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"
29 static struct cls_table *find_table(const struct classifier *,
30 const struct minimask *);
31 static struct cls_table *insert_table(struct classifier *,
32 const struct minimask *);
34 static void destroy_table(struct classifier *, struct cls_table *);
36 static struct cls_rule *find_match(const struct cls_table *,
38 static struct cls_rule *find_equal(struct cls_table *,
39 const struct miniflow *, uint32_t hash);
40 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
42 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
43 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
44 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
45 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
46 for ((RULE) = (HEAD); \
47 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
50 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
51 static struct cls_rule *next_rule_in_list(struct cls_rule *);
55 /* Initializes 'rule' to match packets specified by 'match' at the given
56 * 'priority'. 'match' must satisfy the invariant described in the comment at
57 * the definition of struct match.
59 * The caller must eventually destroy 'rule' with cls_rule_destroy().
61 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
62 * internally Open vSwitch supports a wider range.) */
64 cls_rule_init(struct cls_rule *rule,
65 const struct match *match, unsigned int priority)
67 minimatch_init(&rule->match, match);
68 rule->priority = priority;
71 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
73 cls_rule_init_from_minimatch(struct cls_rule *rule,
74 const struct minimatch *match,
75 unsigned int priority)
77 minimatch_clone(&rule->match, match);
78 rule->priority = priority;
81 /* Initializes 'dst' as a copy of 'src'.
83 * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
85 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
87 minimatch_clone(&dst->match, &src->match);
88 dst->priority = src->priority;
91 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
92 * normally embedded into a larger structure).
94 * ('rule' must not currently be in a classifier.) */
96 cls_rule_destroy(struct cls_rule *rule)
98 minimatch_destroy(&rule->match);
101 /* Returns true if 'a' and 'b' match the same packets at the same priority,
102 * false if they differ in some way. */
104 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
106 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
109 /* Returns a hash value for 'rule', folding in 'basis'. */
111 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
113 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
116 /* Appends a string describing 'rule' to 's'. */
118 cls_rule_format(const struct cls_rule *rule, struct ds *s)
120 minimatch_format(&rule->match, s, rule->priority);
123 /* Returns true if 'rule' matches every packet, false otherwise. */
125 cls_rule_is_catchall(const struct cls_rule *rule)
127 return minimask_is_catchall(&rule->match.mask);
130 /* Initializes 'cls' as a classifier that initially contains no classification
133 classifier_init(struct classifier *cls)
136 hmap_init(&cls->tables);
139 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
140 * caller's responsibility. */
142 classifier_destroy(struct classifier *cls)
145 struct cls_table *table, *next_table;
147 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
148 destroy_table(cls, table);
150 hmap_destroy(&cls->tables);
154 /* Returns true if 'cls' contains no classification rules, false otherwise. */
156 classifier_is_empty(const struct classifier *cls)
158 return cls->n_rules == 0;
161 /* Returns the number of rules in 'cls'. */
163 classifier_count(const struct classifier *cls)
168 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
169 * must not modify or free it.
171 * If 'cls' already contains an identical rule (including wildcards, values of
172 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
173 * rule that was replaced. The caller takes ownership of the returned rule and
174 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
175 * memory block in which it resides, etc., as necessary.
177 * Returns NULL if 'cls' does not contain a rule with an identical key, after
178 * inserting the new rule. In this case, no rules are displaced by the new
179 * rule, even rules that cannot have any effect because the new rule matches a
180 * superset of their flows and has higher priority. */
182 classifier_replace(struct classifier *cls, struct cls_rule *rule)
184 struct cls_rule *old_rule;
185 struct cls_table *table;
187 table = find_table(cls, &rule->match.mask);
189 table = insert_table(cls, &rule->match.mask);
192 old_rule = insert_rule(table, rule);
194 table->n_table_rules++;
200 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
201 * must not modify or free it.
203 * 'cls' must not contain an identical rule (including wildcards, values of
204 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
207 classifier_insert(struct classifier *cls, struct cls_rule *rule)
209 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
210 ovs_assert(!displaced_rule);
213 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
214 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
215 * resides, etc., as necessary. */
217 classifier_remove(struct classifier *cls, struct cls_rule *rule)
219 struct cls_rule *head;
220 struct cls_table *table;
222 table = find_table(cls, &rule->match.mask);
223 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
225 list_remove(&rule->list);
226 } else if (list_is_empty(&rule->list)) {
227 hmap_remove(&table->rules, &rule->hmap_node);
229 struct cls_rule *next = CONTAINER_OF(rule->list.next,
230 struct cls_rule, list);
232 list_remove(&rule->list);
233 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
236 if (--table->n_table_rules == 0) {
237 destroy_table(cls, table);
243 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
244 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
245 * of equal priority match 'flow', returns one arbitrarily. */
247 classifier_lookup(const struct classifier *cls, const struct flow *flow)
249 struct cls_table *table;
250 struct cls_rule *best;
253 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
254 struct cls_rule *rule = find_match(table, flow);
255 if (rule && (!best || rule->priority > best->priority)) {
262 /* Finds and returns a rule in 'cls' with exactly the same priority and
263 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
264 * contain an exact match. */
266 classifier_find_rule_exactly(const struct classifier *cls,
267 const struct cls_rule *target)
269 struct cls_rule *head, *rule;
270 struct cls_table *table;
272 table = find_table(cls, &target->match.mask);
277 head = find_equal(table, &target->match.flow,
278 miniflow_hash_in_minimask(&target->match.flow,
279 &target->match.mask, 0));
280 FOR_EACH_RULE_IN_LIST (rule, head) {
281 if (target->priority >= rule->priority) {
282 return target->priority == rule->priority ? rule : NULL;
288 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
289 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
290 * contain an exact match. */
292 classifier_find_match_exactly(const struct classifier *cls,
293 const struct match *target,
294 unsigned int priority)
296 struct cls_rule *retval;
299 cls_rule_init(&cr, target, priority);
300 retval = classifier_find_rule_exactly(cls, &cr);
301 cls_rule_destroy(&cr);
306 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
307 * considered to overlap if both rules have the same priority and a packet
308 * could match both. */
310 classifier_rule_overlaps(const struct classifier *cls,
311 const struct cls_rule *target)
313 struct cls_table *table;
315 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
316 uint32_t storage[FLOW_U32S];
317 struct minimask mask;
318 struct cls_rule *head;
320 minimask_combine(&mask, &target->match.mask, &table->mask, storage);
321 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
322 struct cls_rule *rule;
324 FOR_EACH_RULE_IN_LIST (rule, head) {
325 if (rule->priority == target->priority
326 && miniflow_equal_in_minimask(&target->match.flow,
327 &rule->match.flow, &mask)) {
337 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
338 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
339 * function returns true if, for every field:
341 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
344 * - 'criteria' wildcards the field,
346 * Conversely, 'rule' does not match 'criteria' and this function returns false
347 * if, for at least one field:
349 * - 'criteria' and 'rule' specify different values for the field, or
351 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
353 * Equivalently, the truth table for whether a field matches is:
358 * r +---------+---------+
359 * i wild | yes | yes |
361 * e +---------+---------+
362 * r exact | no |if values|
364 * a +---------+---------+
366 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
367 * commands and by OpenFlow 1.0 aggregate and flow stats.
369 * Ignores rule->priority. */
371 cls_rule_is_loose_match(const struct cls_rule *rule,
372 const struct minimatch *criteria)
374 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
375 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
382 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
385 || miniflow_equal_in_minimask(&rule->match.flow,
387 &target->match.mask));
390 static struct cls_rule *
391 search_table(const struct cls_table *table, const struct cls_rule *target)
393 if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
394 struct cls_rule *rule;
396 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
397 if (rule_matches(rule, target)) {
405 /* Initializes 'cursor' for iterating through rules in 'cls':
407 * - If 'target' is null, the cursor will visit every rule in 'cls'.
409 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
410 * such that cls_rule_is_loose_match(rule, target) returns true.
412 * Ignores target->priority. */
414 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
415 const struct cls_rule *target)
418 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
421 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
422 * pointer if there are no matches. */
424 cls_cursor_first(struct cls_cursor *cursor)
426 struct cls_table *table;
428 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
429 struct cls_rule *rule = search_table(table, cursor->target);
431 cursor->table = table;
439 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
440 * pointer if there are no more matches. */
442 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
444 const struct cls_table *table;
445 struct cls_rule *next;
447 next = next_rule_in_list__(rule);
448 if (next->priority < rule->priority) {
452 /* 'next' is the head of the list, that is, the rule that is included in
453 * the table's hmap. (This is important when the classifier contains rules
454 * that differ only in priority.) */
456 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
457 if (rule_matches(rule, cursor->target)) {
462 table = cursor->table;
463 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
464 rule = search_table(table, cursor->target);
466 cursor->table = table;
474 static struct cls_table *
475 find_table(const struct classifier *cls, const struct minimask *mask)
477 struct cls_table *table;
479 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
481 if (minimask_equal(mask, &table->mask)) {
488 static struct cls_table *
489 insert_table(struct classifier *cls, const struct minimask *mask)
491 struct cls_table *table;
493 table = xzalloc(sizeof *table);
494 hmap_init(&table->rules);
495 minimask_clone(&table->mask, mask);
496 hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
502 destroy_table(struct classifier *cls, struct cls_table *table)
504 minimask_destroy(&table->mask);
505 hmap_remove(&cls->tables, &table->hmap_node);
506 hmap_destroy(&table->rules);
510 static struct cls_rule *
511 find_match(const struct cls_table *table, const struct flow *flow)
513 uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
514 struct cls_rule *rule;
516 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) {
517 if (miniflow_equal_flow_in_minimask(&rule->match.flow, flow,
526 static struct cls_rule *
527 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
529 struct cls_rule *head;
531 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
532 if (miniflow_equal(&head->match.flow, flow)) {
539 static struct cls_rule *
540 insert_rule(struct cls_table *table, struct cls_rule *new)
542 struct cls_rule *head;
544 new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
545 &new->match.mask, 0);
547 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
549 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
550 list_init(&new->list);
553 /* Scan the list for the insertion point that will keep the list in
554 * order of decreasing priority. */
555 struct cls_rule *rule;
556 FOR_EACH_RULE_IN_LIST (rule, head) {
557 if (new->priority >= rule->priority) {
559 /* 'new' is the new highest-priority flow in the list. */
560 hmap_replace(&table->rules,
561 &rule->hmap_node, &new->hmap_node);
564 if (new->priority == rule->priority) {
565 list_replace(&new->list, &rule->list);
568 list_insert(&rule->list, &new->list);
574 /* Insert 'new' at the end of the list. */
575 list_push_back(&head->list, &new->list);
580 static struct cls_rule *
581 next_rule_in_list__(struct cls_rule *rule)
583 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
587 static struct cls_rule *
588 next_rule_in_list(struct cls_rule *rule)
590 struct cls_rule *next = next_rule_in_list__(rule);
591 return next->priority < rule->priority ? next : NULL;