2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define CLASSIFIER_H 1
26 * A flow classifier holds any number of "rules", each of which specifies
27 * values to match for some fields or subfields and a priority. The primary
28 * design goal for the classifier is that, given a packet, it can as quickly as
29 * possible find the highest-priority rule that matches the packet.
31 * Each OpenFlow table is implemented as a flow classifier.
37 * Suppose that all the rules in a classifier had the same form. For example,
38 * suppose that they all matched on the source and destination Ethernet address
39 * and wildcarded all the other fields. Then the obvious way to implement a
40 * classifier would be a hash table on the source and destination Ethernet
41 * addresses. If new classification rules came along with a different form,
42 * you could add a second hash table that hashed on the fields matched in those
43 * rules. With two hash tables, you look up a given flow in each hash table.
44 * If there are no matches, the classifier didn't contain a match; if you find
45 * a match in one of them, that's the result; if you find a match in both of
46 * them, then the result is the rule with the higher priority.
48 * This is how the classifier works. In a "struct classifier", each form of
49 * "struct cls_rule" present (based on its ->match.mask) goes into a separate
50 * "struct cls_table". A lookup does a hash lookup in every "struct cls_table"
51 * in the classifier and tracks the highest-priority match that it finds. The
52 * tables are kept in a descending priority order according to the highest
53 * priority rule in each table, which allows lookup to skip over tables that
54 * can't possibly have a higher-priority match than already found.
56 * One detail: a classifier can contain multiple rules that are identical other
57 * than their priority. When this happens, only the highest priority rule out
58 * of a group of otherwise identical rules is stored directly in the "struct
59 * cls_table", with the other almost-identical rules chained off a linked list
60 * inside that highest-priority rule.
66 * Suppose that a given classifier is being used to handle multiple stages in a
67 * pipeline using "resubmit", with metadata (that is, the OpenFlow 1.1+ field
68 * named "metadata") distinguishing between the different stages. For example,
69 * metadata value 1 might identify ingress rules, metadata value 2 might
70 * identify ACLs, and metadata value 3 might identify egress rules. Such a
71 * classifier is essentially partitioned into multiple sub-classifiers on the
72 * basis of the metadata value.
74 * The classifier has a special optimization to speed up matching in this
77 * - Each cls_table that matches on metadata gets a tag derived from the
78 * table's mask, so that it is likely that each table has a unique tag.
79 * (Duplicate tags have a performance cost but do not affect
82 * - For each metadata value matched by any cls_rule, the classifier
83 * constructs a "struct cls_partition" indexed by the metadata value.
84 * The cls_partition has a 'tags' member whose value is the bitwise-OR of
85 * the tags of each cls_table that contains any rule that matches on the
86 * cls_partition's metadata value. In other words, struct cls_partition
87 * associates metadata values with tables that need to be checked with
88 * flows with that specific metadata value.
90 * Thus, a flow lookup can start by looking up the partition associated with
91 * the flow's metadata, and then skip over any cls_table whose 'tag' does not
92 * intersect the partition's 'tags'. (The flow must also be looked up in any
93 * cls_table that doesn't match on metadata. We handle that by giving any such
94 * cls_table TAG_ALL as its 'tags' so that it matches any tag.)
100 * When locked properly, the classifier is thread safe as long as the following
101 * conditions are satisfied.
102 * - Only the main thread calls functions requiring a write lock.
103 * - Only the main thread is allowed to iterate over rules. */
110 #include "openflow/nicira-ext.h"
111 #include "openflow/openflow.h"
112 #include "ovs-thread.h"
119 /* Needed only for the lock annotation in struct classifier. */
120 extern struct ovs_mutex ofproto_mutex;
122 /* A flow classifier. */
124 int n_rules; /* Total number of rules. */
125 struct hmap tables; /* Contains "struct cls_table"s. */
126 struct list tables_priority; /* Tables in descending priority order */
127 struct hmap partitions; /* Contains "struct cls_partition"s. */
128 struct ovs_rwlock rwlock OVS_ACQ_AFTER(ofproto_mutex);
131 /* A set of rules that all have the same fields wildcarded. */
133 struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
134 struct list list_node; /* Within classifier 'tables_priority_list' */
135 struct hmap rules; /* Contains "struct cls_rule"s. */
136 struct minimask mask; /* Wildcards for fields. */
137 int n_table_rules; /* Number of rules, including duplicates. */
138 unsigned int max_priority; /* Max priority of any rule in the table. */
139 unsigned int max_count; /* Count of max_priority rules. */
140 tag_type tag; /* Tag generated from mask for partitioning. */
143 /* Returns true if 'table' is a "catch-all" table that will match every
144 * packet (if there is no higher-priority match). */
146 cls_table_is_catchall(const struct cls_table *table)
148 return minimask_is_catchall(&table->mask);
151 /* A rule in a "struct classifier". */
153 struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
154 struct list list; /* List of identical, lower-priority rules. */
155 struct minimatch match; /* Matching rule. */
156 unsigned int priority; /* Larger numbers are higher priorities. */
157 struct cls_partition *partition;
160 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
161 * field) with tags for the "cls_table"s that contain rules that match that
163 struct cls_partition {
164 struct hmap_node hmap_node; /* In struct classifier's 'partitions' hmap. */
165 ovs_be64 metadata; /* metadata value for this partition. */
166 tag_type tags; /* OR of each included flow's cls_table tag. */
167 struct tag_tracker tracker; /* Tracks the bits in 'tags'. */
170 void cls_rule_init(struct cls_rule *, const struct match *,
171 unsigned int priority);
172 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
173 unsigned int priority);
174 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
175 void cls_rule_move(struct cls_rule *dst, struct cls_rule *src);
176 void cls_rule_destroy(struct cls_rule *);
178 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
179 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
181 void cls_rule_format(const struct cls_rule *, struct ds *);
183 bool cls_rule_is_catchall(const struct cls_rule *);
185 bool cls_rule_is_loose_match(const struct cls_rule *rule,
186 const struct minimatch *criteria);
188 void classifier_init(struct classifier *cls);
189 void classifier_destroy(struct classifier *);
190 bool classifier_is_empty(const struct classifier *cls)
191 OVS_REQ_RDLOCK(cls->rwlock);
192 int classifier_count(const struct classifier *cls)
193 OVS_REQ_RDLOCK(cls->rwlock);
194 void classifier_insert(struct classifier *cls, struct cls_rule *)
195 OVS_REQ_WRLOCK(cls->rwlock);
196 struct cls_rule *classifier_replace(struct classifier *cls, struct cls_rule *)
197 OVS_REQ_WRLOCK(cls->rwlock);
198 void classifier_remove(struct classifier *cls, struct cls_rule *)
199 OVS_REQ_WRLOCK(cls->rwlock);
200 struct cls_rule *classifier_lookup(const struct classifier *cls,
202 struct flow_wildcards *)
203 OVS_REQ_RDLOCK(cls->rwlock);
204 bool classifier_rule_overlaps(const struct classifier *cls,
205 const struct cls_rule *)
206 OVS_REQ_RDLOCK(cls->rwlock);
208 typedef void cls_cb_func(struct cls_rule *, void *aux);
210 struct cls_rule *classifier_find_rule_exactly(const struct classifier *cls,
211 const struct cls_rule *)
212 OVS_REQ_RDLOCK(cls->rwlock);
213 struct cls_rule *classifier_find_match_exactly(const struct classifier *cls,
214 const struct match *,
215 unsigned int priority)
216 OVS_REQ_RDLOCK(cls->rwlock);
221 const struct classifier *cls;
222 const struct cls_table *table;
223 const struct cls_rule *target;
226 void cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
227 const struct cls_rule *match) OVS_REQ_RDLOCK(cls->rwlock);
228 struct cls_rule *cls_cursor_first(struct cls_cursor *cursor);
229 struct cls_rule *cls_cursor_next(struct cls_cursor *cursor, const struct cls_rule *);
231 #define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR) \
232 for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER); \
233 RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER); \
234 ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
237 #define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR) \
238 for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER); \
239 (RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER) \
240 ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
249 #endif /* classifier.h */