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 /* Returns true if 'rule' matches every packet, false otherwise. */
117 cls_rule_is_catchall(const struct cls_rule *rule)
119 return flow_wildcards_is_catchall(&rule->match.wc);
122 /* Initializes 'cls' as a classifier that initially contains no classification
125 classifier_init(struct classifier *cls)
128 hmap_init(&cls->tables);
131 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
132 * caller's responsibility. */
134 classifier_destroy(struct classifier *cls)
137 struct cls_table *table, *next_table;
139 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
140 hmap_destroy(&table->rules);
141 hmap_remove(&cls->tables, &table->hmap_node);
144 hmap_destroy(&cls->tables);
148 /* Returns true if 'cls' contains no classification rules, false otherwise. */
150 classifier_is_empty(const struct classifier *cls)
152 return cls->n_rules == 0;
155 /* Returns the number of rules in 'cls'. */
157 classifier_count(const struct classifier *cls)
162 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
163 * must not modify or free it.
165 * If 'cls' already contains an identical rule (including wildcards, values of
166 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
167 * rule that was replaced. The caller takes ownership of the returned rule and
168 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
169 * memory block in which it resides, etc., as necessary.
171 * Returns NULL if 'cls' does not contain a rule with an identical key, after
172 * inserting the new rule. In this case, no rules are displaced by the new
173 * rule, even rules that cannot have any effect because the new rule matches a
174 * superset of their flows and has higher priority. */
176 classifier_replace(struct classifier *cls, struct cls_rule *rule)
178 struct cls_rule *old_rule;
179 struct cls_table *table;
181 table = find_table(cls, &rule->match.wc);
183 table = insert_table(cls, &rule->match.wc);
186 old_rule = insert_rule(table, rule);
188 table->n_table_rules++;
194 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
195 * must not modify or free it.
197 * 'cls' must not contain an identical rule (including wildcards, values of
198 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
201 classifier_insert(struct classifier *cls, struct cls_rule *rule)
203 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
204 assert(!displaced_rule);
207 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
208 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
209 * resides, etc., as necessary. */
211 classifier_remove(struct classifier *cls, struct cls_rule *rule)
213 struct cls_rule *head;
214 struct cls_table *table;
216 table = find_table(cls, &rule->match.wc);
217 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
219 list_remove(&rule->list);
220 } else if (list_is_empty(&rule->list)) {
221 hmap_remove(&table->rules, &rule->hmap_node);
223 struct cls_rule *next = CONTAINER_OF(rule->list.next,
224 struct cls_rule, list);
226 list_remove(&rule->list);
227 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
230 if (--table->n_table_rules == 0) {
231 destroy_table(cls, table);
237 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
238 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
239 * of equal priority match 'flow', returns one arbitrarily. */
241 classifier_lookup(const struct classifier *cls, const struct flow *flow)
243 struct cls_table *table;
244 struct cls_rule *best;
247 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
248 struct cls_rule *rule = find_match(table, flow);
249 if (rule && (!best || rule->priority > best->priority)) {
256 /* Finds and returns a rule in 'cls' with exactly the same priority and
257 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
258 * contain an exact match. */
260 classifier_find_rule_exactly(const struct classifier *cls,
261 const struct cls_rule *target)
263 struct cls_rule *head, *rule;
264 struct cls_table *table;
266 table = find_table(cls, &target->match.wc);
271 head = find_equal(table, &target->match.flow,
272 flow_hash(&target->match.flow, 0));
273 FOR_EACH_RULE_IN_LIST (rule, head) {
274 if (target->priority >= rule->priority) {
275 return target->priority == rule->priority ? rule : NULL;
281 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
282 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
283 * contain an exact match. */
285 classifier_find_match_exactly(const struct classifier *cls,
286 const struct match *target,
287 unsigned int priority)
289 struct cls_rule *retval;
292 cls_rule_init(&cr, target, priority);
293 retval = classifier_find_rule_exactly(cls, &cr);
294 cls_rule_destroy(&cr);
299 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
300 * considered to overlap if both rules have the same priority and a packet
301 * could match both. */
303 classifier_rule_overlaps(const struct classifier *cls,
304 const struct cls_rule *target)
306 struct cls_table *table;
308 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
309 struct flow_wildcards wc;
310 struct cls_rule *head;
312 flow_wildcards_combine(&wc, &target->match.wc, &table->wc);
313 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
314 struct cls_rule *rule;
316 FOR_EACH_RULE_IN_LIST (rule, head) {
317 if (rule->priority == target->priority
318 && flow_equal_except(&target->match.flow,
319 &rule->match.flow, &wc)) {
329 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
330 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
331 * function returns true if, for every field:
333 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
336 * - 'criteria' wildcards the field,
338 * Conversely, 'rule' does not match 'criteria' and this function returns false
339 * if, for at least one field:
341 * - 'criteria' and 'rule' specify different values for the field, or
343 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
345 * Equivalently, the truth table for whether a field matches is:
350 * r +---------+---------+
351 * i wild | yes | yes |
353 * e +---------+---------+
354 * r exact | no |if values|
356 * a +---------+---------+
358 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
359 * commands and by OpenFlow 1.0 aggregate and flow stats.
361 * Ignores rule->priority. */
363 cls_rule_is_loose_match(const struct cls_rule *rule,
364 const struct match *criteria)
366 return (!flow_wildcards_has_extra(&rule->match.wc, &criteria->wc)
367 && flow_equal_except(&rule->match.flow, &criteria->flow,
374 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
377 || flow_equal_except(&rule->match.flow, &target->match.flow,
381 static struct cls_rule *
382 search_table(const struct cls_table *table, const struct cls_rule *target)
384 if (!target || !flow_wildcards_has_extra(&table->wc, &target->match.wc)) {
385 struct cls_rule *rule;
387 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
388 if (rule_matches(rule, target)) {
396 /* Initializes 'cursor' for iterating through rules in 'cls':
398 * - If 'target' is null, the cursor will visit every rule in 'cls'.
400 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
401 * such that cls_rule_is_loose_match(rule, target) returns true.
403 * Ignores target->priority. */
405 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
406 const struct cls_rule *target)
409 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
412 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
413 * pointer if there are no matches. */
415 cls_cursor_first(struct cls_cursor *cursor)
417 struct cls_table *table;
419 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
420 struct cls_rule *rule = search_table(table, cursor->target);
422 cursor->table = table;
430 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
431 * pointer if there are no more matches. */
433 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
435 const struct cls_table *table;
436 struct cls_rule *next;
438 next = next_rule_in_list__(rule);
439 if (next->priority < rule->priority) {
443 /* 'next' is the head of the list, that is, the rule that is included in
444 * the table's hmap. (This is important when the classifier contains rules
445 * that differ only in priority.) */
447 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
448 if (rule_matches(rule, cursor->target)) {
453 table = cursor->table;
454 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
455 rule = search_table(table, cursor->target);
457 cursor->table = table;
465 static struct cls_table *
466 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
468 struct cls_table *table;
470 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
472 if (flow_wildcards_equal(wc, &table->wc)) {
479 static struct cls_table *
480 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
482 struct cls_table *table;
484 table = xzalloc(sizeof *table);
485 hmap_init(&table->rules);
487 table->is_catchall = flow_wildcards_is_catchall(&table->wc);
488 hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
494 destroy_table(struct classifier *cls, struct cls_table *table)
496 hmap_remove(&cls->tables, &table->hmap_node);
497 hmap_destroy(&table->rules);
501 static struct cls_rule *
502 find_match(const struct cls_table *table, const struct flow *flow)
504 struct cls_rule *rule;
506 if (table->is_catchall) {
507 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
514 flow_zero_wildcards(&f, &table->wc);
515 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
517 if (flow_equal(&f, &rule->match.flow)) {
526 static struct cls_rule *
527 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
529 struct cls_rule *head;
531 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
532 if (flow_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 = flow_hash(&new->match.flow, 0);
546 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
548 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
549 list_init(&new->list);
552 /* Scan the list for the insertion point that will keep the list in
553 * order of decreasing priority. */
554 struct cls_rule *rule;
555 FOR_EACH_RULE_IN_LIST (rule, head) {
556 if (new->priority >= rule->priority) {
558 /* 'new' is the new highest-priority flow in the list. */
559 hmap_replace(&table->rules,
560 &rule->hmap_node, &new->hmap_node);
563 if (new->priority == rule->priority) {
564 list_replace(&new->list, &rule->list);
567 list_insert(&rule->list, &new->list);
573 /* Insert 'new' at the end of the list. */
574 list_push_back(&head->list, &new->list);
579 static struct cls_rule *
580 next_rule_in_list__(struct cls_rule *rule)
582 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
586 static struct cls_rule *
587 next_rule_in_list(struct cls_rule *rule)
589 struct cls_rule *next = next_rule_in_list__(rule);
590 return next->priority < rule->priority ? next : NULL;