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 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 struct cls_rule *find_match(const struct cls_table *,
39 static struct cls_rule *find_equal(struct cls_table *,
40 const struct miniflow *, uint32_t hash);
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
57 * 'priority'. 'match' must satisfy the invariant described in the comment at
58 * the definition of struct match.
60 * The caller must eventually destroy 'rule' with cls_rule_destroy().
62 * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
63 * internally Open vSwitch supports a wider range.) */
65 cls_rule_init(struct cls_rule *rule,
66 const struct match *match, unsigned int priority)
68 minimatch_init(&rule->match, match);
69 rule->priority = priority;
72 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
74 cls_rule_init_from_minimatch(struct cls_rule *rule,
75 const struct minimatch *match,
76 unsigned int priority)
78 minimatch_clone(&rule->match, match);
79 rule->priority = priority;
82 /* Initializes 'dst' as a copy of 'src'.
84 * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
86 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
88 minimatch_clone(&dst->match, &src->match);
89 dst->priority = src->priority;
92 /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
93 * normally embedded into a larger structure).
95 * ('rule' must not currently be in a classifier.) */
97 cls_rule_destroy(struct cls_rule *rule)
99 minimatch_destroy(&rule->match);
102 /* Returns true if 'a' and 'b' match the same packets at the same priority,
103 * false if they differ in some way. */
105 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
107 return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
110 /* Returns a hash value for 'rule', folding in 'basis'. */
112 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
114 return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
117 /* Appends a string describing 'rule' to 's'. */
119 cls_rule_format(const struct cls_rule *rule, struct ds *s)
121 minimatch_format(&rule->match, s, rule->priority);
124 /* Returns true if 'rule' matches every packet, false otherwise. */
126 cls_rule_is_catchall(const struct cls_rule *rule)
128 return minimask_is_catchall(&rule->match.mask);
131 /* Initializes 'cls' as a classifier that initially contains no classification
134 classifier_init(struct classifier *cls)
137 hmap_init(&cls->tables);
140 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
141 * caller's responsibility. */
143 classifier_destroy(struct classifier *cls)
146 struct cls_table *table, *next_table;
148 HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
149 hmap_destroy(&table->rules);
150 hmap_remove(&cls->tables, &table->hmap_node);
153 hmap_destroy(&cls->tables);
157 /* Returns true if 'cls' contains no classification rules, false otherwise. */
159 classifier_is_empty(const struct classifier *cls)
161 return cls->n_rules == 0;
164 /* Returns the number of rules in 'cls'. */
166 classifier_count(const struct classifier *cls)
171 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
172 * must not modify or free it.
174 * If 'cls' already contains an identical rule (including wildcards, values of
175 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
176 * rule that was replaced. The caller takes ownership of the returned rule and
177 * is thus responsible for destroying it with cls_rule_destroy(), freeing the
178 * memory block in which it resides, etc., as necessary.
180 * Returns NULL if 'cls' does not contain a rule with an identical key, after
181 * inserting the new rule. In this case, no rules are displaced by the new
182 * rule, even rules that cannot have any effect because the new rule matches a
183 * superset of their flows and has higher priority. */
185 classifier_replace(struct classifier *cls, struct cls_rule *rule)
187 struct cls_rule *old_rule;
188 struct cls_table *table;
190 table = find_table(cls, &rule->match.mask);
192 table = insert_table(cls, &rule->match.mask);
195 old_rule = insert_rule(table, rule);
197 table->n_table_rules++;
203 /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
204 * must not modify or free it.
206 * 'cls' must not contain an identical rule (including wildcards, values of
207 * fixed fields, and priority). Use classifier_find_rule_exactly() to find
210 classifier_insert(struct classifier *cls, struct cls_rule *rule)
212 struct cls_rule *displaced_rule = classifier_replace(cls, rule);
213 assert(!displaced_rule);
216 /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
217 * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
218 * resides, etc., as necessary. */
220 classifier_remove(struct classifier *cls, struct cls_rule *rule)
222 struct cls_rule *head;
223 struct cls_table *table;
225 table = find_table(cls, &rule->match.mask);
226 head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
228 list_remove(&rule->list);
229 } else if (list_is_empty(&rule->list)) {
230 hmap_remove(&table->rules, &rule->hmap_node);
232 struct cls_rule *next = CONTAINER_OF(rule->list.next,
233 struct cls_rule, list);
235 list_remove(&rule->list);
236 hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
239 if (--table->n_table_rules == 0) {
240 destroy_table(cls, table);
246 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
247 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
248 * of equal priority match 'flow', returns one arbitrarily. */
250 classifier_lookup(const struct classifier *cls, const struct flow *flow)
252 struct cls_table *table;
253 struct cls_rule *best;
256 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
257 struct cls_rule *rule = find_match(table, flow);
258 if (rule && (!best || rule->priority > best->priority)) {
265 /* Finds and returns a rule in 'cls' with exactly the same priority and
266 * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
267 * contain an exact match. */
269 classifier_find_rule_exactly(const struct classifier *cls,
270 const struct cls_rule *target)
272 struct cls_rule *head, *rule;
273 struct cls_table *table;
275 table = find_table(cls, &target->match.mask);
280 head = find_equal(table, &target->match.flow,
281 miniflow_hash_in_minimask(&target->match.flow,
282 &target->match.mask, 0));
283 FOR_EACH_RULE_IN_LIST (rule, head) {
284 if (target->priority >= rule->priority) {
285 return target->priority == rule->priority ? rule : NULL;
291 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
292 * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't
293 * contain an exact match. */
295 classifier_find_match_exactly(const struct classifier *cls,
296 const struct match *target,
297 unsigned int priority)
299 struct cls_rule *retval;
302 cls_rule_init(&cr, target, priority);
303 retval = classifier_find_rule_exactly(cls, &cr);
304 cls_rule_destroy(&cr);
309 /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are
310 * considered to overlap if both rules have the same priority and a packet
311 * could match both. */
313 classifier_rule_overlaps(const struct classifier *cls,
314 const struct cls_rule *target)
316 struct cls_table *table;
318 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
319 uint32_t storage[FLOW_U32S];
320 struct minimask mask;
321 struct cls_rule *head;
323 minimask_combine(&mask, &target->match.mask, &table->mask, storage);
324 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
325 struct cls_rule *rule;
327 FOR_EACH_RULE_IN_LIST (rule, head) {
328 if (rule->priority == target->priority
329 && miniflow_equal_in_minimask(&target->match.flow,
330 &rule->match.flow, &mask)) {
340 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
341 * specific than 'criteria'. That is, 'rule' matches 'criteria' and this
342 * function returns true if, for every field:
344 * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
347 * - 'criteria' wildcards the field,
349 * Conversely, 'rule' does not match 'criteria' and this function returns false
350 * if, for at least one field:
352 * - 'criteria' and 'rule' specify different values for the field, or
354 * - 'criteria' specifies a value for the field but 'rule' wildcards it.
356 * Equivalently, the truth table for whether a field matches is:
361 * r +---------+---------+
362 * i wild | yes | yes |
364 * e +---------+---------+
365 * r exact | no |if values|
367 * a +---------+---------+
369 * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
370 * commands and by OpenFlow 1.0 aggregate and flow stats.
372 * Ignores rule->priority. */
374 cls_rule_is_loose_match(const struct cls_rule *rule,
375 const struct minimatch *criteria)
377 return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
378 && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
385 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
388 || miniflow_equal_in_minimask(&rule->match.flow,
390 &target->match.mask));
393 static struct cls_rule *
394 search_table(const struct cls_table *table, const struct cls_rule *target)
396 if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
397 struct cls_rule *rule;
399 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
400 if (rule_matches(rule, target)) {
408 /* Initializes 'cursor' for iterating through rules in 'cls':
410 * - If 'target' is null, the cursor will visit every rule in 'cls'.
412 * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
413 * such that cls_rule_is_loose_match(rule, target) returns true.
415 * Ignores target->priority. */
417 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
418 const struct cls_rule *target)
421 cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
424 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
425 * pointer if there are no matches. */
427 cls_cursor_first(struct cls_cursor *cursor)
429 struct cls_table *table;
431 HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
432 struct cls_rule *rule = search_table(table, cursor->target);
434 cursor->table = table;
442 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
443 * pointer if there are no more matches. */
445 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
447 const struct cls_table *table;
448 struct cls_rule *next;
450 next = next_rule_in_list__(rule);
451 if (next->priority < rule->priority) {
455 /* 'next' is the head of the list, that is, the rule that is included in
456 * the table's hmap. (This is important when the classifier contains rules
457 * that differ only in priority.) */
459 HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
460 if (rule_matches(rule, cursor->target)) {
465 table = cursor->table;
466 HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
467 rule = search_table(table, cursor->target);
469 cursor->table = table;
477 static struct cls_table *
478 find_table(const struct classifier *cls, const struct minimask *mask)
480 struct cls_table *table;
482 HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
484 if (minimask_equal(mask, &table->mask)) {
491 static struct cls_table *
492 insert_table(struct classifier *cls, const struct minimask *mask)
494 struct cls_table *table;
496 table = xzalloc(sizeof *table);
497 hmap_init(&table->rules);
498 minimask_clone(&table->mask, mask);
499 hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
505 destroy_table(struct classifier *cls, struct cls_table *table)
507 minimask_destroy(&table->mask);
508 hmap_remove(&cls->tables, &table->hmap_node);
509 hmap_destroy(&table->rules);
513 static struct cls_rule *
514 find_match(const struct cls_table *table, const struct flow *flow)
516 uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
517 struct cls_rule *rule;
519 HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) {
520 if (miniflow_equal_flow_in_minimask(&rule->match.flow, flow,
529 static struct cls_rule *
530 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
532 struct cls_rule *head;
534 HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
535 if (miniflow_equal(&head->match.flow, flow)) {
542 static struct cls_rule *
543 insert_rule(struct cls_table *table, struct cls_rule *new)
545 struct cls_rule *head;
547 new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
548 &new->match.mask, 0);
550 head = find_equal(table, &new->match.flow, new->hmap_node.hash);
552 hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
553 list_init(&new->list);
556 /* Scan the list for the insertion point that will keep the list in
557 * order of decreasing priority. */
558 struct cls_rule *rule;
559 FOR_EACH_RULE_IN_LIST (rule, head) {
560 if (new->priority >= rule->priority) {
562 /* 'new' is the new highest-priority flow in the list. */
563 hmap_replace(&table->rules,
564 &rule->hmap_node, &new->hmap_node);
567 if (new->priority == rule->priority) {
568 list_replace(&new->list, &rule->list);
571 list_insert(&rule->list, &new->list);
577 /* Insert 'new' at the end of the list. */
578 list_push_back(&head->list, &new->list);
583 static struct cls_rule *
584 next_rule_in_list__(struct cls_rule *rule)
586 struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
590 static struct cls_rule *
591 next_rule_in_list(struct cls_rule *rule)
593 struct cls_rule *next = next_rule_in_list__(rule);
594 return next->priority < rule->priority ? next : NULL;