Optimize classifier by maintaining the priority of the highest priority rule in each...
[sliver-openvswitch.git] / lib / classifier.h
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 #ifndef CLASSIFIER_H
18 #define CLASSIFIER_H 1
19
20 /* Flow classifier.
21  *
22  * A classifier is a "struct classifier",
23  *      a hash map from a set of wildcards to a "struct cls_table",
24  *              a hash map from fixed field values to "struct cls_rule",
25  *                      which can contain a list of otherwise identical rules
26  *                      with lower priorities.
27  */
28
29 #include "flow.h"
30 #include "hmap.h"
31 #include "list.h"
32 #include "match.h"
33 #include "openflow/nicira-ext.h"
34 #include "openflow/openflow.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* A flow classifier. */
41 struct classifier {
42     int n_rules;                /* Total number of rules. */
43     struct hmap tables;         /* Contains "struct cls_table"s.  */
44 };
45
46 /* A set of rules that all have the same fields wildcarded. */
47 struct cls_table {
48     struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
49     struct hmap rules;          /* Contains "struct cls_rule"s. */
50     struct minimask mask;       /* Wildcards for fields. */
51     int n_table_rules;          /* Number of rules, including duplicates. */
52     unsigned int max_priority;  /* Max priority of any rule in the table. */
53     unsigned int max_count;     /* Count of max_priority rules. */
54 };
55
56 /* Returns true if 'table' is a "catch-all" table that will match every
57  * packet (if there is no higher-priority match). */
58 static inline bool
59 cls_table_is_catchall(const struct cls_table *table)
60 {
61     return minimask_is_catchall(&table->mask);
62 }
63
64 /* A rule in a "struct classifier". */
65 struct cls_rule {
66     struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
67     struct list list;           /* List of identical, lower-priority rules. */
68     struct minimatch match;     /* Matching rule. */
69     unsigned int priority;      /* Larger numbers are higher priorities. */
70 };
71
72 void cls_rule_init(struct cls_rule *, const struct match *,
73                    unsigned int priority);
74 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
75                                   unsigned int priority);
76 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
77 void cls_rule_destroy(struct cls_rule *);
78
79 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
80 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
81
82 void cls_rule_format(const struct cls_rule *, struct ds *);
83
84 bool cls_rule_is_catchall(const struct cls_rule *);
85
86 bool cls_rule_is_loose_match(const struct cls_rule *rule,
87                              const struct minimatch *criteria);
88
89 void classifier_init(struct classifier *);
90 void classifier_destroy(struct classifier *);
91 bool classifier_is_empty(const struct classifier *);
92 int classifier_count(const struct classifier *);
93 void classifier_insert(struct classifier *, struct cls_rule *);
94 struct cls_rule *classifier_replace(struct classifier *, struct cls_rule *);
95 void classifier_remove(struct classifier *, struct cls_rule *);
96 struct cls_rule *classifier_lookup(const struct classifier *,
97                                    const struct flow *);
98 bool classifier_rule_overlaps(const struct classifier *,
99                               const struct cls_rule *);
100
101 typedef void cls_cb_func(struct cls_rule *, void *aux);
102
103 struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
104                                               const struct cls_rule *);
105 struct cls_rule *classifier_find_match_exactly(const struct classifier *,
106                                                const struct match *,
107                                                unsigned int priority);
108 \f
109 /* Iteration. */
110
111 struct cls_cursor {
112     const struct classifier *cls;
113     const struct cls_table *table;
114     const struct cls_rule *target;
115 };
116
117 void cls_cursor_init(struct cls_cursor *, const struct classifier *,
118                      const struct cls_rule *match);
119 struct cls_rule *cls_cursor_first(struct cls_cursor *);
120 struct cls_rule *cls_cursor_next(struct cls_cursor *, struct cls_rule *);
121
122 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
123     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
124          &(RULE)->MEMBER != NULL;                                       \
125          ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
126                           MEMBER))
127
128 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
129     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
130          (&(RULE)->MEMBER != NULL                                       \
131           ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
132                              MEMBER)                                    \
133           : 0);                                                         \
134          (RULE) = (NEXT))
135
136 #ifdef __cplusplus
137 }
138 #endif
139
140 #endif /* classifier.h */