d1fe524f400a014aeedcc05a3bd5e53760dccc5c
[sliver-openvswitch.git] / lib / classifier.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "classifier.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "hash.h"
26 #include "odp-util.h"
27 #include "ofp-util.h"
28 #include "packets.h"
29
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 *);
34
35 static void destroy_table(struct classifier *, struct cls_table *);
36
37 static struct cls_rule *find_match(const struct cls_table *,
38                                    const struct flow *);
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 *);
42
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);    \
49          (RULE) = (NEXT))
50
51 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
52 static struct cls_rule *next_rule_in_list(struct cls_rule *);
53 \f
54 /* cls_rule. */
55
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.
59  *
60  * The caller must eventually destroy 'rule' with cls_rule_destroy().
61  *
62  * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
63  * internally Open vSwitch supports a wider range.) */
64 void
65 cls_rule_init(struct cls_rule *rule,
66               const struct match *match, unsigned int priority)
67 {
68     minimatch_init(&rule->match, match);
69     rule->priority = priority;
70 }
71
72 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
73 void
74 cls_rule_init_from_minimatch(struct cls_rule *rule,
75                              const struct minimatch *match,
76                              unsigned int priority)
77 {
78     minimatch_clone(&rule->match, match);
79     rule->priority = priority;
80 }
81
82 /* Initializes 'dst' as a copy of 'src'.
83  *
84  * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
85 void
86 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
87 {
88     minimatch_clone(&dst->match, &src->match);
89     dst->priority = src->priority;
90 }
91
92 /* Frees memory referenced by 'rule'.  Doesn't free 'rule' itself (it's
93  * normally embedded into a larger structure).
94  *
95  * ('rule' must not currently be in a classifier.) */
96 void
97 cls_rule_destroy(struct cls_rule *rule)
98 {
99     minimatch_destroy(&rule->match);
100 }
101
102 /* Returns true if 'a' and 'b' match the same packets at the same priority,
103  * false if they differ in some way. */
104 bool
105 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
106 {
107     return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
108 }
109
110 /* Returns a hash value for 'rule', folding in 'basis'. */
111 uint32_t
112 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
113 {
114     return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
115 }
116
117 /* Appends a string describing 'rule' to 's'. */
118 void
119 cls_rule_format(const struct cls_rule *rule, struct ds *s)
120 {
121     minimatch_format(&rule->match, s, rule->priority);
122 }
123
124 /* Returns true if 'rule' matches every packet, false otherwise. */
125 bool
126 cls_rule_is_catchall(const struct cls_rule *rule)
127 {
128     return minimask_is_catchall(&rule->match.mask);
129 }
130 \f
131 /* Initializes 'cls' as a classifier that initially contains no classification
132  * rules. */
133 void
134 classifier_init(struct classifier *cls)
135 {
136     cls->n_rules = 0;
137     hmap_init(&cls->tables);
138 }
139
140 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
141  * caller's responsibility. */
142 void
143 classifier_destroy(struct classifier *cls)
144 {
145     if (cls) {
146         struct cls_table *table, *next_table;
147
148         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
149             destroy_table(cls, table);
150         }
151         hmap_destroy(&cls->tables);
152     }
153 }
154
155 /* Returns true if 'cls' contains no classification rules, false otherwise. */
156 bool
157 classifier_is_empty(const struct classifier *cls)
158 {
159     return cls->n_rules == 0;
160 }
161
162 /* Returns the number of rules in 'cls'. */
163 int
164 classifier_count(const struct classifier *cls)
165 {
166     return cls->n_rules;
167 }
168
169 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
170  * must not modify or free it.
171  *
172  * If 'cls' already contains an identical rule (including wildcards, values of
173  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
174  * rule that was replaced.  The caller takes ownership of the returned rule and
175  * is thus responsible for destroying it with cls_rule_destroy(), freeing the
176  * memory block in which it resides, etc., as necessary.
177  *
178  * Returns NULL if 'cls' does not contain a rule with an identical key, after
179  * inserting the new rule.  In this case, no rules are displaced by the new
180  * rule, even rules that cannot have any effect because the new rule matches a
181  * superset of their flows and has higher priority. */
182 struct cls_rule *
183 classifier_replace(struct classifier *cls, struct cls_rule *rule)
184 {
185     struct cls_rule *old_rule;
186     struct cls_table *table;
187
188     table = find_table(cls, &rule->match.mask);
189     if (!table) {
190         table = insert_table(cls, &rule->match.mask);
191     }
192
193     old_rule = insert_rule(table, rule);
194     if (!old_rule) {
195         table->n_table_rules++;
196         cls->n_rules++;
197     }
198     return old_rule;
199 }
200
201 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
202  * must not modify or free it.
203  *
204  * 'cls' must not contain an identical rule (including wildcards, values of
205  * fixed fields, and priority).  Use classifier_find_rule_exactly() to find
206  * such a rule. */
207 void
208 classifier_insert(struct classifier *cls, struct cls_rule *rule)
209 {
210     struct cls_rule *displaced_rule = classifier_replace(cls, rule);
211     assert(!displaced_rule);
212 }
213
214 /* Removes 'rule' from 'cls'.  It is the caller's responsibility to destroy
215  * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
216  * resides, etc., as necessary. */
217 void
218 classifier_remove(struct classifier *cls, struct cls_rule *rule)
219 {
220     struct cls_rule *head;
221     struct cls_table *table;
222
223     table = find_table(cls, &rule->match.mask);
224     head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
225     if (head != rule) {
226         list_remove(&rule->list);
227     } else if (list_is_empty(&rule->list)) {
228         hmap_remove(&table->rules, &rule->hmap_node);
229     } else {
230         struct cls_rule *next = CONTAINER_OF(rule->list.next,
231                                              struct cls_rule, list);
232
233         list_remove(&rule->list);
234         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
235     }
236
237     if (--table->n_table_rules == 0) {
238         destroy_table(cls, table);
239     }
240
241     cls->n_rules--;
242 }
243
244 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
245  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
246  * of equal priority match 'flow', returns one arbitrarily. */
247 struct cls_rule *
248 classifier_lookup(const struct classifier *cls, const struct flow *flow)
249 {
250     struct cls_table *table;
251     struct cls_rule *best;
252
253     best = NULL;
254     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
255         struct cls_rule *rule = find_match(table, flow);
256         if (rule && (!best || rule->priority > best->priority)) {
257             best = rule;
258         }
259     }
260     return best;
261 }
262
263 /* Finds and returns a rule in 'cls' with exactly the same priority and
264  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
265  * contain an exact match. */
266 struct cls_rule *
267 classifier_find_rule_exactly(const struct classifier *cls,
268                              const struct cls_rule *target)
269 {
270     struct cls_rule *head, *rule;
271     struct cls_table *table;
272
273     table = find_table(cls, &target->match.mask);
274     if (!table) {
275         return NULL;
276     }
277
278     head = find_equal(table, &target->match.flow,
279                       miniflow_hash_in_minimask(&target->match.flow,
280                                                 &target->match.mask, 0));
281     FOR_EACH_RULE_IN_LIST (rule, head) {
282         if (target->priority >= rule->priority) {
283             return target->priority == rule->priority ? rule : NULL;
284         }
285     }
286     return NULL;
287 }
288
289 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
290  * same matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
291  * contain an exact match. */
292 struct cls_rule *
293 classifier_find_match_exactly(const struct classifier *cls,
294                               const struct match *target,
295                               unsigned int priority)
296 {
297     struct cls_rule *retval;
298     struct cls_rule cr;
299
300     cls_rule_init(&cr, target, priority);
301     retval = classifier_find_rule_exactly(cls, &cr);
302     cls_rule_destroy(&cr);
303
304     return retval;
305 }
306
307 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
308  * considered to overlap if both rules have the same priority and a packet
309  * could match both. */
310 bool
311 classifier_rule_overlaps(const struct classifier *cls,
312                          const struct cls_rule *target)
313 {
314     struct cls_table *table;
315
316     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
317         uint32_t storage[FLOW_U32S];
318         struct minimask mask;
319         struct cls_rule *head;
320
321         minimask_combine(&mask, &target->match.mask, &table->mask, storage);
322         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
323             struct cls_rule *rule;
324
325             FOR_EACH_RULE_IN_LIST (rule, head) {
326                 if (rule->priority == target->priority
327                     && miniflow_equal_in_minimask(&target->match.flow,
328                                                   &rule->match.flow, &mask)) {
329                     return true;
330                 }
331             }
332         }
333     }
334
335     return false;
336 }
337
338 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
339  * specific than 'criteria'.  That is, 'rule' matches 'criteria' and this
340  * function returns true if, for every field:
341  *
342  *   - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
343  *     field, or
344  *
345  *   - 'criteria' wildcards the field,
346  *
347  * Conversely, 'rule' does not match 'criteria' and this function returns false
348  * if, for at least one field:
349  *
350  *   - 'criteria' and 'rule' specify different values for the field, or
351  *
352  *   - 'criteria' specifies a value for the field but 'rule' wildcards it.
353  *
354  * Equivalently, the truth table for whether a field matches is:
355  *
356  *                                     rule
357  *
358  *                   c         wildcard    exact
359  *                   r        +---------+---------+
360  *                   i   wild |   yes   |   yes   |
361  *                   t   card |         |         |
362  *                   e        +---------+---------+
363  *                   r  exact |    no   |if values|
364  *                   i        |         |are equal|
365  *                   a        +---------+---------+
366  *
367  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
368  * commands and by OpenFlow 1.0 aggregate and flow stats.
369  *
370  * Ignores rule->priority. */
371 bool
372 cls_rule_is_loose_match(const struct cls_rule *rule,
373                         const struct minimatch *criteria)
374 {
375     return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
376             && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
377                                           &criteria->mask));
378 }
379 \f
380 /* Iteration. */
381
382 static bool
383 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
384 {
385     return (!target
386             || miniflow_equal_in_minimask(&rule->match.flow,
387                                           &target->match.flow,
388                                           &target->match.mask));
389 }
390
391 static struct cls_rule *
392 search_table(const struct cls_table *table, const struct cls_rule *target)
393 {
394     if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
395         struct cls_rule *rule;
396
397         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
398             if (rule_matches(rule, target)) {
399                 return rule;
400             }
401         }
402     }
403     return NULL;
404 }
405
406 /* Initializes 'cursor' for iterating through rules in 'cls':
407  *
408  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
409  *
410  *     - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
411  *       such that cls_rule_is_loose_match(rule, target) returns true.
412  *
413  * Ignores target->priority. */
414 void
415 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
416                 const struct cls_rule *target)
417 {
418     cursor->cls = cls;
419     cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
420 }
421
422 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
423  * pointer if there are no matches. */
424 struct cls_rule *
425 cls_cursor_first(struct cls_cursor *cursor)
426 {
427     struct cls_table *table;
428
429     HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
430         struct cls_rule *rule = search_table(table, cursor->target);
431         if (rule) {
432             cursor->table = table;
433             return rule;
434         }
435     }
436
437     return NULL;
438 }
439
440 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
441  * pointer if there are no more matches. */
442 struct cls_rule *
443 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
444 {
445     const struct cls_table *table;
446     struct cls_rule *next;
447
448     next = next_rule_in_list__(rule);
449     if (next->priority < rule->priority) {
450         return next;
451     }
452
453     /* 'next' is the head of the list, that is, the rule that is included in
454      * the table's hmap.  (This is important when the classifier contains rules
455      * that differ only in priority.) */
456     rule = next;
457     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
458         if (rule_matches(rule, cursor->target)) {
459             return rule;
460         }
461     }
462
463     table = cursor->table;
464     HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
465         rule = search_table(table, cursor->target);
466         if (rule) {
467             cursor->table = table;
468             return rule;
469         }
470     }
471
472     return NULL;
473 }
474 \f
475 static struct cls_table *
476 find_table(const struct classifier *cls, const struct minimask *mask)
477 {
478     struct cls_table *table;
479
480     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
481                              &cls->tables) {
482         if (minimask_equal(mask, &table->mask)) {
483             return table;
484         }
485     }
486     return NULL;
487 }
488
489 static struct cls_table *
490 insert_table(struct classifier *cls, const struct minimask *mask)
491 {
492     struct cls_table *table;
493
494     table = xzalloc(sizeof *table);
495     hmap_init(&table->rules);
496     minimask_clone(&table->mask, mask);
497     hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
498
499     return table;
500 }
501
502 static void
503 destroy_table(struct classifier *cls, struct cls_table *table)
504 {
505     minimask_destroy(&table->mask);
506     hmap_remove(&cls->tables, &table->hmap_node);
507     hmap_destroy(&table->rules);
508     free(table);
509 }
510
511 static struct cls_rule *
512 find_match(const struct cls_table *table, const struct flow *flow)
513 {
514     uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
515     struct cls_rule *rule;
516
517     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) {
518         if (miniflow_equal_flow_in_minimask(&rule->match.flow, flow,
519                                             &table->mask)) {
520             return rule;
521         }
522     }
523
524     return NULL;
525 }
526
527 static struct cls_rule *
528 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
529 {
530     struct cls_rule *head;
531
532     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
533         if (miniflow_equal(&head->match.flow, flow)) {
534             return head;
535         }
536     }
537     return NULL;
538 }
539
540 static struct cls_rule *
541 insert_rule(struct cls_table *table, struct cls_rule *new)
542 {
543     struct cls_rule *head;
544
545     new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
546                                                     &new->match.mask, 0);
547
548     head = find_equal(table, &new->match.flow, new->hmap_node.hash);
549     if (!head) {
550         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
551         list_init(&new->list);
552         return NULL;
553     } else {
554         /* Scan the list for the insertion point that will keep the list in
555          * order of decreasing priority. */
556         struct cls_rule *rule;
557         FOR_EACH_RULE_IN_LIST (rule, head) {
558             if (new->priority >= rule->priority) {
559                 if (rule == head) {
560                     /* 'new' is the new highest-priority flow in the list. */
561                     hmap_replace(&table->rules,
562                                  &rule->hmap_node, &new->hmap_node);
563                 }
564
565                 if (new->priority == rule->priority) {
566                     list_replace(&new->list, &rule->list);
567                     return rule;
568                 } else {
569                     list_insert(&rule->list, &new->list);
570                     return NULL;
571                 }
572             }
573         }
574
575         /* Insert 'new' at the end of the list. */
576         list_push_back(&head->list, &new->list);
577         return NULL;
578     }
579 }
580
581 static struct cls_rule *
582 next_rule_in_list__(struct cls_rule *rule)
583 {
584     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
585     return next;
586 }
587
588 static struct cls_rule *
589 next_rule_in_list(struct cls_rule *rule)
590 {
591     struct cls_rule *next = next_rule_in_list__(rule);
592     return next->priority < rule->priority ? next : NULL;
593 }