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"
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
30 static struct cls_table *find_table(const struct classifier *,
31 const struct flow_wildcards *);
32 static struct cls_table *insert_table(struct classifier *,
33 const struct flow_wildcards *);
35 static void destroy_table(struct classifier *, struct cls_table *);
37 static struct cls_rule *find_match(const struct cls_table *,
39 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
41 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
43 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
44 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \
45 for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
46 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \
47 for ((RULE) = (HEAD); \
48 (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \
51 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
52 static struct cls_rule *next_rule_in_list(struct cls_rule *);
56 /* Initializes 'rule' to match packets specified by 'match' at the given
59 * The caller must eventually destroy 'rule' with cls_rule_destroy().
61 * 'match' must satisfy the invariant described in the comment at the
62 * definition of struct match.
64 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
65 * internally Open vSwitch supports a wider range.) */
67 cls_rule_init(struct cls_rule *rule,
68 const struct match *match, unsigned int priority)
71 rule->priority = priority;
74 /* Initializes 'dst' as a copy of 'src'.
76 * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
78 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
83 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
84 * normally embedded into a larger structure).
86 * ('rule' must not currently be in a classifier.) */
88 cls_rule_destroy(struct cls_rule *rule OVS_UNUSED)
90 /* Nothing to do yet. */
93 /* Returns true if 'a' and 'b' match the same packets at the same priority,
94 * false if they differ in some way. */
96 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
98 return a->priority == b->priority && match_equal(&a->match, &b->match);
101 /* Returns a hash value for 'rule', folding in 'basis'. */
103 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
105 return match_hash(&rule->match, hash_int(rule->priority, basis));
108 /* Appends a string describing 'rule' to 's'. */
110 cls_rule_format(const struct cls_rule *rule, struct ds *s)
112 match_format(&rule->match, s, rule->priority);
115 /* Initializes 'cls' as a classifier that initially contains no classification
118 classifier_init(struct classifier *cls)
121 hmap_init(&cls->tables);
124 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
125 * caller's responsibility. */
127 classifier_destroy(struct classifier *cls)
130 struct cls_table *table, *next_table;
132 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
133 hmap_destroy(&table->rules);
134 hmap_remove(&cls->tables, &table->hmap_node);
137 hmap_destroy(&cls->tables);
141 /* Returns true if 'cls' contains no classification rules, false otherwise. */
143 classifier_is_empty(const struct classifier *cls)
145 return cls->n_rules == 0;
148 /* Returns the number of rules in 'cls'. */
150 classifier_count(const struct classifier *cls)
155 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
156 * must not modify or free it.
158 * If 'cls' already contains an identical rule (including wildcards, values of
159 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
160 * rule that was replaced. The caller takes ownership of the returned rule and
161 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
162 * memory block in which it resides, etc., as necessary.
164 * Returns NULL if 'cls' does not contain a rule with an identical key, after
165 * inserting the new rule. In this case, no rules are displaced by the new
166 * rule, even rules that cannot have any effect because the new rule matches a
167 * superset of their flows and has higher priority. */
169 classifier_replace(struct classifier *cls, struct cls_rule *rule)
171 struct cls_rule *old_rule;
172 struct cls_table *table;
174 table = find_table(cls, &rule->match.wc);
176 table = insert_table(cls, &rule->match.wc);
179 old_rule = insert_rule(table, rule);
181 table->n_table_rules++;
187 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
188 * must not modify or free it.
190 * 'cls' must not contain an identical rule (including wildcards, values of
191 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
194 classifier_insert(struct classifier *cls, struct cls_rule *rule)
196 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
197 assert(!displaced_rule);
200 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
201 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
202 * resides, etc., as necessary. */
204 classifier_remove(struct classifier *cls, struct cls_rule *rule)
206 struct cls_rule *head;
207 struct cls_table *table;
209 table = find_table(cls, &rule->match.wc);
210 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
212 list_remove(&rule->list);
213 } else if (list_is_empty(&rule->list)) {
214 hmap_remove(&table->rules, &rule->hmap_node);
216 struct cls_rule *next = CONTAINER_OF(rule->list.next,
217 struct cls_rule, list);
219 list_remove(&rule->list);
220 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
223 if (--table->n_table_rules == 0) {
224 destroy_table(cls, table);
230 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
231 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
232 * of equal priority match 'flow', returns one arbitrarily. */
234 classifier_lookup(const struct classifier *cls, const struct flow *flow)
236 struct cls_table *table;
237 struct cls_rule *best;
240 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
241 struct cls_rule *rule = find_match(table, flow);
242 if (rule && (!best || rule->priority > best->priority)) {
249 /* Finds and returns a rule in 'cls' with exactly the same priority and
250 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
251 * contain an exact match. */
253 classifier_find_rule_exactly(const struct classifier *cls,
254 const struct cls_rule *target)
256 struct cls_rule *head, *rule;
257 struct cls_table *table;
259 table = find_table(cls, &target->match.wc);
264 head = find_equal(table, &target->match.flow,
265 flow_hash(&target->match.flow, 0));
266 FOR_EACH_RULE_IN_LIST (rule, head) {
267 if (target->priority >= rule->priority) {
268 return target->priority == rule->priority ? rule : NULL;
274 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
275 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
276 * contain an exact match. */
278 classifier_find_match_exactly(const struct classifier *cls,
279 const struct match *target,
280 unsigned int priority)
282 struct cls_rule *retval;
285 cls_rule_init(&cr, target, priority);
286 retval = classifier_find_rule_exactly(cls, &cr);
287 cls_rule_destroy(&cr);
292 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
293 * considered to overlap if both rules have the same priority and a packet
294 * could match both. */
296 classifier_rule_overlaps(const struct classifier *cls,
297 const struct cls_rule *target)
299 struct cls_table *table;
301 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
302 struct flow_wildcards wc;
303 struct cls_rule *head;
305 flow_wildcards_combine(&wc, &target->match.wc, &table->wc);
306 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
307 struct cls_rule *rule;
309 FOR_EACH_RULE_IN_LIST (rule, head) {
310 if (rule->priority == target->priority
311 && flow_equal_except(&target->match.flow,
312 &rule->match.flow, &wc)) {
322 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
323 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
324 * function returns true if, for every field:
326 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
329 * - 'criteria' wildcards the field,
331 * Conversely, 'rule' does not match 'criteria' and this function returns false
332 * if, for at least one field:
334 * - 'criteria' and 'rule' specify different values for the field, or
336 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
338 * Equivalently, the truth table for whether a field matches is:
343 * r +---------+---------+
344 * i wild | yes | yes |
346 * e +---------+---------+
347 * r exact | no |if values|
349 * a +---------+---------+
351 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
352 * commands and by OpenFlow 1.0 aggregate and flow stats.
354 * Ignores rule->priority. */
356 cls_rule_is_loose_match(const struct cls_rule *rule,
357 const struct match *criteria)
359 return (!flow_wildcards_has_extra(&rule->match.wc, &criteria->wc)
360 && flow_equal_except(&rule->match.flow, &criteria->flow,
367 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
370 || flow_equal_except(&rule->match.flow, &target->match.flow,
374 static struct cls_rule *
375 search_table(const struct cls_table *table, const struct cls_rule *target)
377 if (!target || !flow_wildcards_has_extra(&table->wc, &target->match.wc)) {
378 struct cls_rule *rule;
380 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
381 if (rule_matches(rule, target)) {
389 /* Initializes 'cursor' for iterating through rules in 'cls':
391 * - If 'target' is null, the cursor will visit every rule in 'cls'.
393 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
394 * such that cls_rule_is_loose_match(rule, target) returns true.
396 * Ignores target->priority. */
398 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
399 const struct cls_rule *target)
402 cursor->target = target;
405 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
406 * pointer if there are no matches. */
408 cls_cursor_first(struct cls_cursor *cursor)
410 struct cls_table *table;
412 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
413 struct cls_rule *rule = search_table(table, cursor->target);
415 cursor->table = table;
423 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
424 * pointer if there are no more matches. */
426 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
428 const struct cls_table *table;
429 struct cls_rule *next;
431 next = next_rule_in_list__(rule);
432 if (next->priority < rule->priority) {
436 /* 'next' is the head of the list, that is, the rule that is included in
437 * the table's hmap. (This is important when the classifier contains rules
438 * that differ only in priority.) */
440 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
441 if (rule_matches(rule, cursor->target)) {
446 table = cursor->table;
447 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
448 rule = search_table(table, cursor->target);
450 cursor->table = table;
458 static struct cls_table *
459 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
461 struct cls_table *table;
463 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
465 if (flow_wildcards_equal(wc, &table->wc)) {
472 static struct cls_table *
473 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
475 struct cls_table *table;
477 table = xzalloc(sizeof *table);
478 hmap_init(&table->rules);
480 table->is_catchall = flow_wildcards_is_catchall(&table->wc);
481 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
487 destroy_table(struct classifier *cls, struct cls_table *table)
489 hmap_remove(&cls->tables, &table->hmap_node);
490 hmap_destroy(&table->rules);
494 static struct cls_rule *
495 find_match(const struct cls_table *table, const struct flow *flow)
497 struct cls_rule *rule;
499 if (table->is_catchall) {
500 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
507 flow_zero_wildcards(&f, &table->wc);
508 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
510 if (flow_equal(&f, &rule->match.flow)) {
519 static struct cls_rule *
520 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
522 struct cls_rule *head;
524 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
525 if (flow_equal(&head->match.flow, flow)) {
532 static struct cls_rule *
533 insert_rule(struct cls_table *table, struct cls_rule *new)
535 struct cls_rule *head;
537 new->hmap_node.hash = flow_hash(&new->match.flow, 0);
539 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
541 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
542 list_init(&new->list);
545 /* Scan the list for the insertion point that will keep the list in
546 * order of decreasing priority. */
547 struct cls_rule *rule;
548 FOR_EACH_RULE_IN_LIST (rule, head) {
549 if (new->priority >= rule->priority) {
551 /* 'new' is the new highest-priority flow in the list. */
552 hmap_replace(&table->rules,
553 &rule->hmap_node, &new->hmap_node);
556 if (new->priority == rule->priority) {
557 list_replace(&new->list, &rule->list);
560 list_insert(&rule->list, &new->list);
566 /* Insert 'new' at the end of the list. */
567 list_push_back(&head->list, &new->list);
572 static struct cls_rule *
573 next_rule_in_list__(struct cls_rule *rule)
575 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
579 static struct cls_rule *
580 next_rule_in_list(struct cls_rule *rule)
582 struct cls_rule *next = next_rule_in_list__(rule);
583 return next->priority < rule->priority ? next : NULL;