classifier: Maintain tables in descending priority order.
[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     struct list tables_priority; /* Tables in descending priority order */
45 };
46
47 /* A set of rules that all have the same fields wildcarded. */
48 struct cls_table {
49     struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
50     struct list list_node;      /* Within classifier 'tables_priority_list' */
51     struct hmap rules;          /* Contains "struct cls_rule"s. */
52     struct minimask mask;       /* Wildcards for fields. */
53     int n_table_rules;          /* Number of rules, including duplicates. */
54     unsigned int max_priority;  /* Max priority of any rule in the table. */
55     unsigned int max_count;     /* Count of max_priority rules. */
56 };
57
58 /* Returns true if 'table' is a "catch-all" table that will match every
59  * packet (if there is no higher-priority match). */
60 static inline bool
61 cls_table_is_catchall(const struct cls_table *table)
62 {
63     return minimask_is_catchall(&table->mask);
64 }
65
66 /* A rule in a "struct classifier". */
67 struct cls_rule {
68     struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
69     struct list list;           /* List of identical, lower-priority rules. */
70     struct minimatch match;     /* Matching rule. */
71     unsigned int priority;      /* Larger numbers are higher priorities. */
72 };
73
74 void cls_rule_init(struct cls_rule *, const struct match *,
75                    unsigned int priority);
76 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
77                                   unsigned int priority);
78 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
79 void cls_rule_destroy(struct cls_rule *);
80
81 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
82 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
83
84 void cls_rule_format(const struct cls_rule *, struct ds *);
85
86 bool cls_rule_is_catchall(const struct cls_rule *);
87
88 bool cls_rule_is_loose_match(const struct cls_rule *rule,
89                              const struct minimatch *criteria);
90
91 void classifier_init(struct classifier *);
92 void classifier_destroy(struct classifier *);
93 bool classifier_is_empty(const struct classifier *);
94 int classifier_count(const struct classifier *);
95 void classifier_insert(struct classifier *, struct cls_rule *);
96 struct cls_rule *classifier_replace(struct classifier *, struct cls_rule *);
97 void classifier_remove(struct classifier *, struct cls_rule *);
98 struct cls_rule *classifier_lookup(const struct classifier *,
99                                    const struct flow *);
100 bool classifier_rule_overlaps(const struct classifier *,
101                               const struct cls_rule *);
102
103 typedef void cls_cb_func(struct cls_rule *, void *aux);
104
105 struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
106                                               const struct cls_rule *);
107 struct cls_rule *classifier_find_match_exactly(const struct classifier *,
108                                                const struct match *,
109                                                unsigned int priority);
110 \f
111 /* Iteration. */
112
113 struct cls_cursor {
114     const struct classifier *cls;
115     const struct cls_table *table;
116     const struct cls_rule *target;
117 };
118
119 void cls_cursor_init(struct cls_cursor *, const struct classifier *,
120                      const struct cls_rule *match);
121 struct cls_rule *cls_cursor_first(struct cls_cursor *);
122 struct cls_rule *cls_cursor_next(struct cls_cursor *, struct cls_rule *);
123
124 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
125     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
126          &(RULE)->MEMBER != NULL;                                       \
127          ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
128                           MEMBER))
129
130 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
131     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
132          (&(RULE)->MEMBER != NULL                                       \
133           ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
134                              MEMBER)                                    \
135           : 0);                                                         \
136          (RULE) = (NEXT))
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 #endif /* classifier.h */