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