classifier: Prepare for "struct cls_rule" needing to be destroyed.
[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 flow_wildcards *);
32 static struct cls_table *insert_table(struct classifier *,
33                                       const struct flow_wildcards *);
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 *, const struct flow *,
40                                    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'.
58  *
59  * The caller must eventually destroy 'rule' with cls_rule_destroy().
60  *
61  * 'match' must satisfy the invariant described in the comment at the
62  * definition of struct match.
63  *
64  * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
65  * internally Open vSwitch supports a wider range.) */
66 void
67 cls_rule_init(struct cls_rule *rule,
68               const struct match *match, unsigned int priority)
69 {
70     rule->match = *match;
71     rule->priority = priority;
72 }
73
74 /* Initializes 'dst' as a copy of 'src'.
75  *
76  * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
77 void
78 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
79 {
80     *dst = *src;
81 }
82
83 /* Frees memory referenced by 'rule'.  Doesn't free 'rule' itself (it's
84  * normally embedded into a larger structure).
85  *
86  * ('rule' must not currently be in a classifier.) */
87 void
88 cls_rule_destroy(struct cls_rule *rule OVS_UNUSED)
89 {
90     /* Nothing to do yet. */
91 }
92
93 /* Returns true if 'a' and 'b' match the same packets at the same priority,
94  * false if they differ in some way. */
95 bool
96 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
97 {
98     return a->priority == b->priority && match_equal(&a->match, &b->match);
99 }
100
101 /* Returns a hash value for 'rule', folding in 'basis'. */
102 uint32_t
103 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
104 {
105     return match_hash(&rule->match, hash_int(rule->priority, basis));
106 }
107
108 /* Appends a string describing 'rule' to 's'. */
109 void
110 cls_rule_format(const struct cls_rule *rule, struct ds *s)
111 {
112     match_format(&rule->match, s, rule->priority);
113 }
114 \f
115 /* Initializes 'cls' as a classifier that initially contains no classification
116  * rules. */
117 void
118 classifier_init(struct classifier *cls)
119 {
120     cls->n_rules = 0;
121     hmap_init(&cls->tables);
122 }
123
124 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
125  * caller's responsibility. */
126 void
127 classifier_destroy(struct classifier *cls)
128 {
129     if (cls) {
130         struct cls_table *table, *next_table;
131
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);
135             free(table);
136         }
137         hmap_destroy(&cls->tables);
138     }
139 }
140
141 /* Returns true if 'cls' contains no classification rules, false otherwise. */
142 bool
143 classifier_is_empty(const struct classifier *cls)
144 {
145     return cls->n_rules == 0;
146 }
147
148 /* Returns the number of rules in 'cls'. */
149 int
150 classifier_count(const struct classifier *cls)
151 {
152     return cls->n_rules;
153 }
154
155 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
156  * must not modify or free it.
157  *
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.
163  *
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. */
168 struct cls_rule *
169 classifier_replace(struct classifier *cls, struct cls_rule *rule)
170 {
171     struct cls_rule *old_rule;
172     struct cls_table *table;
173
174     table = find_table(cls, &rule->match.wc);
175     if (!table) {
176         table = insert_table(cls, &rule->match.wc);
177     }
178
179     old_rule = insert_rule(table, rule);
180     if (!old_rule) {
181         table->n_table_rules++;
182         cls->n_rules++;
183     }
184     return old_rule;
185 }
186
187 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
188  * must not modify or free it.
189  *
190  * 'cls' must not contain an identical rule (including wildcards, values of
191  * fixed fields, and priority).  Use classifier_find_rule_exactly() to find
192  * such a rule. */
193 void
194 classifier_insert(struct classifier *cls, struct cls_rule *rule)
195 {
196     struct cls_rule *displaced_rule = classifier_replace(cls, rule);
197     assert(!displaced_rule);
198 }
199
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. */
203 void
204 classifier_remove(struct classifier *cls, struct cls_rule *rule)
205 {
206     struct cls_rule *head;
207     struct cls_table *table;
208
209     table = find_table(cls, &rule->match.wc);
210     head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
211     if (head != rule) {
212         list_remove(&rule->list);
213     } else if (list_is_empty(&rule->list)) {
214         hmap_remove(&table->rules, &rule->hmap_node);
215     } else {
216         struct cls_rule *next = CONTAINER_OF(rule->list.next,
217                                              struct cls_rule, list);
218
219         list_remove(&rule->list);
220         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
221     }
222
223     if (--table->n_table_rules == 0) {
224         destroy_table(cls, table);
225     }
226
227     cls->n_rules--;
228 }
229
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. */
233 struct cls_rule *
234 classifier_lookup(const struct classifier *cls, const struct flow *flow)
235 {
236     struct cls_table *table;
237     struct cls_rule *best;
238
239     best = NULL;
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)) {
243             best = rule;
244         }
245     }
246     return best;
247 }
248
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. */
252 struct cls_rule *
253 classifier_find_rule_exactly(const struct classifier *cls,
254                              const struct cls_rule *target)
255 {
256     struct cls_rule *head, *rule;
257     struct cls_table *table;
258
259     table = find_table(cls, &target->match.wc);
260     if (!table) {
261         return NULL;
262     }
263
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;
269         }
270     }
271     return NULL;
272 }
273
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. */
277 struct cls_rule *
278 classifier_find_match_exactly(const struct classifier *cls,
279                               const struct match *target,
280                               unsigned int priority)
281 {
282     struct cls_rule *retval;
283     struct cls_rule cr;
284
285     cls_rule_init(&cr, target, priority);
286     retval = classifier_find_rule_exactly(cls, &cr);
287     cls_rule_destroy(&cr);
288
289     return retval;
290 }
291
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. */
295 bool
296 classifier_rule_overlaps(const struct classifier *cls,
297                          const struct cls_rule *target)
298 {
299     struct cls_table *table;
300
301     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
302         struct flow_wildcards wc;
303         struct cls_rule *head;
304
305         flow_wildcards_combine(&wc, &target->match.wc, &table->wc);
306         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
307             struct cls_rule *rule;
308
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)) {
313                     return true;
314                 }
315             }
316         }
317     }
318
319     return false;
320 }
321
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:
325  *
326  *   - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
327  *     field, or
328  *
329  *   - 'criteria' wildcards the field,
330  *
331  * Conversely, 'rule' does not match 'criteria' and this function returns false
332  * if, for at least one field:
333  *
334  *   - 'criteria' and 'rule' specify different values for the field, or
335  *
336  *   - 'criteria' specifies a value for the field but 'rule' wildcards it.
337  *
338  * Equivalently, the truth table for whether a field matches is:
339  *
340  *                                     rule
341  *
342  *                   c         wildcard    exact
343  *                   r        +---------+---------+
344  *                   i   wild |   yes   |   yes   |
345  *                   t   card |         |         |
346  *                   e        +---------+---------+
347  *                   r  exact |    no   |if values|
348  *                   i        |         |are equal|
349  *                   a        +---------+---------+
350  *
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.
353  *
354  * Ignores rule->priority. */
355 bool
356 cls_rule_is_loose_match(const struct cls_rule *rule,
357                         const struct match *criteria)
358 {
359     return (!flow_wildcards_has_extra(&rule->match.wc, &criteria->wc)
360             && flow_equal_except(&rule->match.flow, &criteria->flow,
361                                  &criteria->wc));
362 }
363 \f
364 /* Iteration. */
365
366 static bool
367 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
368 {
369     return (!target
370             || flow_equal_except(&rule->match.flow, &target->match.flow,
371                                  &target->match.wc));
372 }
373
374 static struct cls_rule *
375 search_table(const struct cls_table *table, const struct cls_rule *target)
376 {
377     if (!target || !flow_wildcards_has_extra(&table->wc, &target->match.wc)) {
378         struct cls_rule *rule;
379
380         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
381             if (rule_matches(rule, target)) {
382                 return rule;
383             }
384         }
385     }
386     return NULL;
387 }
388
389 /* Initializes 'cursor' for iterating through rules in 'cls':
390  *
391  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
392  *
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.
395  *
396  * Ignores target->priority. */
397 void
398 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
399                 const struct cls_rule *target)
400 {
401     cursor->cls = cls;
402     cursor->target = target;
403 }
404
405 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
406  * pointer if there are no matches. */
407 struct cls_rule *
408 cls_cursor_first(struct cls_cursor *cursor)
409 {
410     struct cls_table *table;
411
412     HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
413         struct cls_rule *rule = search_table(table, cursor->target);
414         if (rule) {
415             cursor->table = table;
416             return rule;
417         }
418     }
419
420     return NULL;
421 }
422
423 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
424  * pointer if there are no more matches. */
425 struct cls_rule *
426 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
427 {
428     const struct cls_table *table;
429     struct cls_rule *next;
430
431     next = next_rule_in_list__(rule);
432     if (next->priority < rule->priority) {
433         return next;
434     }
435
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.) */
439     rule = next;
440     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
441         if (rule_matches(rule, cursor->target)) {
442             return rule;
443         }
444     }
445
446     table = cursor->table;
447     HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
448         rule = search_table(table, cursor->target);
449         if (rule) {
450             cursor->table = table;
451             return rule;
452         }
453     }
454
455     return NULL;
456 }
457 \f
458 static struct cls_table *
459 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
460 {
461     struct cls_table *table;
462
463     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
464                              &cls->tables) {
465         if (flow_wildcards_equal(wc, &table->wc)) {
466             return table;
467         }
468     }
469     return NULL;
470 }
471
472 static struct cls_table *
473 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
474 {
475     struct cls_table *table;
476
477     table = xzalloc(sizeof *table);
478     hmap_init(&table->rules);
479     table->wc = *wc;
480     table->is_catchall = flow_wildcards_is_catchall(&table->wc);
481     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
482
483     return table;
484 }
485
486 static void
487 destroy_table(struct classifier *cls, struct cls_table *table)
488 {
489     hmap_remove(&cls->tables, &table->hmap_node);
490     hmap_destroy(&table->rules);
491     free(table);
492 }
493
494 static struct cls_rule *
495 find_match(const struct cls_table *table, const struct flow *flow)
496 {
497     struct cls_rule *rule;
498
499     if (table->is_catchall) {
500         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
501             return rule;
502         }
503     } else {
504         struct flow f;
505
506         f = *flow;
507         flow_zero_wildcards(&f, &table->wc);
508         HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
509                                  &table->rules) {
510             if (flow_equal(&f, &rule->match.flow)) {
511                 return rule;
512             }
513         }
514     }
515
516     return NULL;
517 }
518
519 static struct cls_rule *
520 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
521 {
522     struct cls_rule *head;
523
524     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
525         if (flow_equal(&head->match.flow, flow)) {
526             return head;
527         }
528     }
529     return NULL;
530 }
531
532 static struct cls_rule *
533 insert_rule(struct cls_table *table, struct cls_rule *new)
534 {
535     struct cls_rule *head;
536
537     new->hmap_node.hash = flow_hash(&new->match.flow, 0);
538
539     head = find_equal(table, &new->match.flow, new->hmap_node.hash);
540     if (!head) {
541         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
542         list_init(&new->list);
543         return NULL;
544     } else {
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) {
550                 if (rule == head) {
551                     /* 'new' is the new highest-priority flow in the list. */
552                     hmap_replace(&table->rules,
553                                  &rule->hmap_node, &new->hmap_node);
554                 }
555
556                 if (new->priority == rule->priority) {
557                     list_replace(&new->list, &rule->list);
558                     return rule;
559                 } else {
560                     list_insert(&rule->list, &new->list);
561                     return NULL;
562                 }
563             }
564         }
565
566         /* Insert 'new' at the end of the list. */
567         list_push_back(&head->list, &new->list);
568         return NULL;
569     }
570 }
571
572 static struct cls_rule *
573 next_rule_in_list__(struct cls_rule *rule)
574 {
575     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
576     return next;
577 }
578
579 static struct cls_rule *
580 next_rule_in_list(struct cls_rule *rule)
581 {
582     struct cls_rule *next = next_rule_in_list__(rule);
583     return next->priority < rule->priority ? next : NULL;
584 }