5a454586e88edd4ab705c9ef17d0a48b204cf9c1
[cascardo/ovs.git] / lib / classifier.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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  * Thread-safety
29  * =============
30  *
31  * When locked properly, the classifier is thread safe as long as the following
32  * conditions are satisfied.
33  * - Only the main thread calls functions requiring a write lock.
34  * - Only the main thread is allowed to iterate over rules. */
35
36 #include "flow.h"
37 #include "hmap.h"
38 #include "list.h"
39 #include "match.h"
40 #include "openflow/nicira-ext.h"
41 #include "openflow/openflow.h"
42 #include "ovs-thread.h"
43 #include "util.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /* A flow classifier. */
50 struct classifier {
51     int n_rules;                /* Total number of rules. */
52     struct hmap tables;         /* Contains "struct cls_table"s.  */
53     struct list tables_priority; /* Tables in descending priority order */
54     struct ovs_rwlock rwlock;
55 };
56
57 /* A set of rules that all have the same fields wildcarded. */
58 struct cls_table {
59     struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
60     struct list list_node;      /* Within classifier 'tables_priority_list' */
61     struct hmap rules;          /* Contains "struct cls_rule"s. */
62     struct minimask mask;       /* Wildcards for fields. */
63     int n_table_rules;          /* Number of rules, including duplicates. */
64     unsigned int max_priority;  /* Max priority of any rule in the table. */
65     unsigned int max_count;     /* Count of max_priority rules. */
66 };
67
68 /* Returns true if 'table' is a "catch-all" table that will match every
69  * packet (if there is no higher-priority match). */
70 static inline bool
71 cls_table_is_catchall(const struct cls_table *table)
72 {
73     return minimask_is_catchall(&table->mask);
74 }
75
76 /* A rule in a "struct classifier". */
77 struct cls_rule {
78     struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
79     struct list list;           /* List of identical, lower-priority rules. */
80     struct minimatch match;     /* Matching rule. */
81     unsigned int priority;      /* Larger numbers are higher priorities. */
82 };
83
84 void cls_rule_init(struct cls_rule *, const struct match *,
85                    unsigned int priority);
86 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
87                                   unsigned int priority);
88 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
89 void cls_rule_move(struct cls_rule *dst, struct cls_rule *src);
90 void cls_rule_destroy(struct cls_rule *);
91
92 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
93 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
94
95 void cls_rule_format(const struct cls_rule *, struct ds *);
96
97 bool cls_rule_is_catchall(const struct cls_rule *);
98
99 bool cls_rule_is_loose_match(const struct cls_rule *rule,
100                              const struct minimatch *criteria);
101
102 void classifier_init(struct classifier *cls);
103 void classifier_destroy(struct classifier *);
104 bool classifier_is_empty(const struct classifier *cls)
105     OVS_REQ_RDLOCK(cls->rwlock);
106 int classifier_count(const struct classifier *cls)
107     OVS_REQ_RDLOCK(cls->rwlock);
108 void classifier_insert(struct classifier *cls, struct cls_rule *)
109     OVS_REQ_WRLOCK(cls->rwlock);
110 struct cls_rule *classifier_replace(struct classifier *cls, struct cls_rule *)
111     OVS_REQ_WRLOCK(cls->rwlock);
112 void classifier_remove(struct classifier *cls, struct cls_rule *)
113     OVS_REQ_WRLOCK(cls->rwlock);
114 struct cls_rule *classifier_lookup(const struct classifier *cls,
115                                    const struct flow *,
116                                    struct flow_wildcards *)
117     OVS_REQ_RDLOCK(cls->rwlock);
118 bool classifier_rule_overlaps(const struct classifier *cls,
119                               const struct cls_rule *)
120     OVS_REQ_RDLOCK(cls->rwlock);
121
122 typedef void cls_cb_func(struct cls_rule *, void *aux);
123
124 struct cls_rule *classifier_find_rule_exactly(const struct classifier *cls,
125                                               const struct cls_rule *)
126     OVS_REQ_RDLOCK(cls->rwlock);
127 struct cls_rule *classifier_find_match_exactly(const struct classifier *cls,
128                                                const struct match *,
129                                                unsigned int priority)
130     OVS_REQ_RDLOCK(cls->rwlock);
131 \f
132 /* Iteration. */
133
134 struct cls_cursor {
135     const struct classifier *cls;
136     const struct cls_table *table;
137     const struct cls_rule *target;
138 };
139
140 void cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
141                      const struct cls_rule *match) OVS_REQ_RDLOCK(cls->rwlock);
142 struct cls_rule *cls_cursor_first(struct cls_cursor *cursor);
143 struct cls_rule *cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *);
144
145 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
146     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
147          RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER);                 \
148          ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
149                           MEMBER))
150
151 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
152     for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
153          (RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER)                 \
154           ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
155                              MEMBER), 1                                 \
156           : 0);                                                         \
157          (RULE) = (NEXT))
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163 #endif /* classifier.h */