]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
4aacd02d | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "classifier.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <netinet/in.h> | |
844dff32 | 21 | #include "byte-order.h" |
68d1c8c3 | 22 | #include "dynamic-string.h" |
064af421 BP |
23 | #include "flow.h" |
24 | #include "hash.h" | |
07b37e8f | 25 | #include "odp-util.h" |
d8ae4d67 | 26 | #include "ofp-util.h" |
b5d97350 | 27 | #include "packets.h" |
0b4f2078 | 28 | #include "ovs-thread.h" |
064af421 | 29 | |
b5d97350 | 30 | static struct cls_table *find_table(const struct classifier *, |
5cb7a798 | 31 | const struct minimask *); |
b5d97350 | 32 | static struct cls_table *insert_table(struct classifier *, |
5cb7a798 | 33 | const struct minimask *); |
b5d97350 | 34 | |
b5d97350 BP |
35 | static void destroy_table(struct classifier *, struct cls_table *); |
36 | ||
4aacd02d BP |
37 | static void update_tables_after_insertion(struct classifier *, |
38 | struct cls_table *, | |
39 | unsigned int new_priority); | |
40 | static void update_tables_after_removal(struct classifier *, | |
41 | struct cls_table *, | |
42 | unsigned int del_priority); | |
43 | ||
b5d97350 BP |
44 | static struct cls_rule *find_match(const struct cls_table *, |
45 | const struct flow *); | |
5cb7a798 BP |
46 | static struct cls_rule *find_equal(struct cls_table *, |
47 | const struct miniflow *, uint32_t hash); | |
4aacd02d BP |
48 | static struct cls_rule *insert_rule(struct classifier *, |
49 | struct cls_table *, struct cls_rule *); | |
b5d97350 | 50 | |
b5d97350 BP |
51 | /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */ |
52 | #define FOR_EACH_RULE_IN_LIST(RULE, HEAD) \ | |
53 | for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE)) | |
54 | #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD) \ | |
55 | for ((RULE) = (HEAD); \ | |
56 | (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true); \ | |
57 | (RULE) = (NEXT)) | |
58 | ||
955f579d | 59 | static struct cls_rule *next_rule_in_list__(struct cls_rule *); |
b5d97350 | 60 | static struct cls_rule *next_rule_in_list(struct cls_rule *); |
81a76618 BP |
61 | \f |
62 | /* cls_rule. */ | |
b5d97350 | 63 | |
81a76618 | 64 | /* Initializes 'rule' to match packets specified by 'match' at the given |
5cb7a798 BP |
65 | * 'priority'. 'match' must satisfy the invariant described in the comment at |
66 | * the definition of struct match. | |
66642cb4 | 67 | * |
48d28ac1 BP |
68 | * The caller must eventually destroy 'rule' with cls_rule_destroy(). |
69 | * | |
81a76618 BP |
70 | * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but |
71 | * internally Open vSwitch supports a wider range.) */ | |
47284b1f | 72 | void |
81a76618 BP |
73 | cls_rule_init(struct cls_rule *rule, |
74 | const struct match *match, unsigned int priority) | |
47284b1f | 75 | { |
5cb7a798 BP |
76 | minimatch_init(&rule->match, match); |
77 | rule->priority = priority; | |
78 | } | |
79 | ||
80 | /* Same as cls_rule_init() for initialization from a "struct minimatch". */ | |
81 | void | |
82 | cls_rule_init_from_minimatch(struct cls_rule *rule, | |
83 | const struct minimatch *match, | |
84 | unsigned int priority) | |
85 | { | |
86 | minimatch_clone(&rule->match, match); | |
81a76618 | 87 | rule->priority = priority; |
685a51a5 JP |
88 | } |
89 | ||
48d28ac1 BP |
90 | /* Initializes 'dst' as a copy of 'src'. |
91 | * | |
b2c1f00b | 92 | * The caller must eventually destroy 'dst' with cls_rule_destroy(). */ |
48d28ac1 BP |
93 | void |
94 | cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src) | |
95 | { | |
5cb7a798 BP |
96 | minimatch_clone(&dst->match, &src->match); |
97 | dst->priority = src->priority; | |
48d28ac1 BP |
98 | } |
99 | ||
b2c1f00b BP |
100 | /* Initializes 'dst' with the data in 'src', destroying 'src'. |
101 | * | |
102 | * The caller must eventually destroy 'dst' with cls_rule_destroy(). */ | |
103 | void | |
104 | cls_rule_move(struct cls_rule *dst, struct cls_rule *src) | |
105 | { | |
106 | minimatch_move(&dst->match, &src->match); | |
107 | dst->priority = src->priority; | |
108 | } | |
109 | ||
48d28ac1 BP |
110 | /* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's |
111 | * normally embedded into a larger structure). | |
112 | * | |
113 | * ('rule' must not currently be in a classifier.) */ | |
114 | void | |
5cb7a798 | 115 | cls_rule_destroy(struct cls_rule *rule) |
48d28ac1 | 116 | { |
5cb7a798 | 117 | minimatch_destroy(&rule->match); |
48d28ac1 BP |
118 | } |
119 | ||
81a76618 BP |
120 | /* Returns true if 'a' and 'b' match the same packets at the same priority, |
121 | * false if they differ in some way. */ | |
193eb874 BP |
122 | bool |
123 | cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b) | |
124 | { | |
5cb7a798 | 125 | return a->priority == b->priority && minimatch_equal(&a->match, &b->match); |
193eb874 BP |
126 | } |
127 | ||
81a76618 | 128 | /* Returns a hash value for 'rule', folding in 'basis'. */ |
57452fdc BP |
129 | uint32_t |
130 | cls_rule_hash(const struct cls_rule *rule, uint32_t basis) | |
131 | { | |
5cb7a798 | 132 | return minimatch_hash(&rule->match, hash_int(rule->priority, basis)); |
73f33563 BP |
133 | } |
134 | ||
81a76618 | 135 | /* Appends a string describing 'rule' to 's'. */ |
07b37e8f BP |
136 | void |
137 | cls_rule_format(const struct cls_rule *rule, struct ds *s) | |
138 | { | |
5cb7a798 | 139 | minimatch_format(&rule->match, s, rule->priority); |
064af421 | 140 | } |
3ca1de08 BP |
141 | |
142 | /* Returns true if 'rule' matches every packet, false otherwise. */ | |
143 | bool | |
144 | cls_rule_is_catchall(const struct cls_rule *rule) | |
145 | { | |
5cb7a798 | 146 | return minimask_is_catchall(&rule->match.mask); |
3ca1de08 | 147 | } |
064af421 BP |
148 | \f |
149 | /* Initializes 'cls' as a classifier that initially contains no classification | |
150 | * rules. */ | |
151 | void | |
152 | classifier_init(struct classifier *cls) | |
153 | { | |
064af421 | 154 | cls->n_rules = 0; |
b5d97350 | 155 | hmap_init(&cls->tables); |
1f3c5efc | 156 | list_init(&cls->tables_priority); |
0b4f2078 | 157 | ovs_rwlock_init(&cls->rwlock); |
064af421 BP |
158 | } |
159 | ||
160 | /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the | |
161 | * caller's responsibility. */ | |
162 | void | |
163 | classifier_destroy(struct classifier *cls) | |
164 | { | |
165 | if (cls) { | |
b5d97350 | 166 | struct cls_table *table, *next_table; |
064af421 | 167 | |
b5d97350 | 168 | HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) { |
0c09d44e | 169 | destroy_table(cls, table); |
064af421 | 170 | } |
b5d97350 | 171 | hmap_destroy(&cls->tables); |
0b4f2078 | 172 | ovs_rwlock_destroy(&cls->rwlock); |
064af421 BP |
173 | } |
174 | } | |
175 | ||
b5d97350 | 176 | /* Returns true if 'cls' contains no classification rules, false otherwise. */ |
064af421 BP |
177 | bool |
178 | classifier_is_empty(const struct classifier *cls) | |
179 | { | |
180 | return cls->n_rules == 0; | |
181 | } | |
182 | ||
dbda2960 | 183 | /* Returns the number of rules in 'cls'. */ |
064af421 BP |
184 | int |
185 | classifier_count(const struct classifier *cls) | |
186 | { | |
187 | return cls->n_rules; | |
188 | } | |
189 | ||
b5d97350 BP |
190 | /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller |
191 | * must not modify or free it. | |
064af421 BP |
192 | * |
193 | * If 'cls' already contains an identical rule (including wildcards, values of | |
194 | * fixed fields, and priority), replaces the old rule by 'rule' and returns the | |
195 | * rule that was replaced. The caller takes ownership of the returned rule and | |
48d28ac1 BP |
196 | * is thus responsible for destroying it with cls_rule_destroy(), freeing the |
197 | * memory block in which it resides, etc., as necessary. | |
064af421 BP |
198 | * |
199 | * Returns NULL if 'cls' does not contain a rule with an identical key, after | |
200 | * inserting the new rule. In this case, no rules are displaced by the new | |
201 | * rule, even rules that cannot have any effect because the new rule matches a | |
202 | * superset of their flows and has higher priority. */ | |
203 | struct cls_rule * | |
08944c1d | 204 | classifier_replace(struct classifier *cls, struct cls_rule *rule) |
064af421 | 205 | { |
b5d97350 BP |
206 | struct cls_rule *old_rule; |
207 | struct cls_table *table; | |
208 | ||
5cb7a798 | 209 | table = find_table(cls, &rule->match.mask); |
b5d97350 | 210 | if (!table) { |
5cb7a798 | 211 | table = insert_table(cls, &rule->match.mask); |
b5d97350 BP |
212 | } |
213 | ||
4aacd02d | 214 | old_rule = insert_rule(cls, table, rule); |
b5d97350 BP |
215 | if (!old_rule) { |
216 | table->n_table_rules++; | |
064af421 BP |
217 | cls->n_rules++; |
218 | } | |
b5d97350 | 219 | return old_rule; |
064af421 BP |
220 | } |
221 | ||
08944c1d BP |
222 | /* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller |
223 | * must not modify or free it. | |
224 | * | |
225 | * 'cls' must not contain an identical rule (including wildcards, values of | |
226 | * fixed fields, and priority). Use classifier_find_rule_exactly() to find | |
227 | * such a rule. */ | |
228 | void | |
229 | classifier_insert(struct classifier *cls, struct cls_rule *rule) | |
230 | { | |
231 | struct cls_rule *displaced_rule = classifier_replace(cls, rule); | |
cb22974d | 232 | ovs_assert(!displaced_rule); |
08944c1d BP |
233 | } |
234 | ||
48d28ac1 BP |
235 | /* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy |
236 | * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule' | |
237 | * resides, etc., as necessary. */ | |
064af421 BP |
238 | void |
239 | classifier_remove(struct classifier *cls, struct cls_rule *rule) | |
240 | { | |
b5d97350 BP |
241 | struct cls_rule *head; |
242 | struct cls_table *table; | |
064af421 | 243 | |
5cb7a798 | 244 | table = find_table(cls, &rule->match.mask); |
81a76618 | 245 | head = find_equal(table, &rule->match.flow, rule->hmap_node.hash); |
b5d97350 BP |
246 | if (head != rule) { |
247 | list_remove(&rule->list); | |
248 | } else if (list_is_empty(&rule->list)) { | |
249 | hmap_remove(&table->rules, &rule->hmap_node); | |
250 | } else { | |
251 | struct cls_rule *next = CONTAINER_OF(rule->list.next, | |
252 | struct cls_rule, list); | |
064af421 | 253 | |
b5d97350 BP |
254 | list_remove(&rule->list); |
255 | hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node); | |
256 | } | |
064af421 | 257 | |
f6acdb44 | 258 | if (--table->n_table_rules == 0) { |
b5d97350 | 259 | destroy_table(cls, table); |
4aacd02d BP |
260 | } else { |
261 | update_tables_after_removal(cls, table, rule->priority); | |
4d935a6b | 262 | } |
b5d97350 | 263 | cls->n_rules--; |
064af421 BP |
264 | } |
265 | ||
48c3de13 BP |
266 | /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'. |
267 | * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules | |
74f74083 EJ |
268 | * of equal priority match 'flow', returns one arbitrarily. |
269 | * | |
270 | * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the | |
271 | * set of bits that were significant in the lookup. At some point | |
272 | * earlier, 'wc' should have been initialized (e.g., by | |
273 | * flow_wildcards_init_catchall()). */ | |
48c3de13 | 274 | struct cls_rule * |
74f74083 EJ |
275 | classifier_lookup(const struct classifier *cls, const struct flow *flow, |
276 | struct flow_wildcards *wc) | |
48c3de13 | 277 | { |
b5d97350 BP |
278 | struct cls_table *table; |
279 | struct cls_rule *best; | |
48c3de13 | 280 | |
b5d97350 | 281 | best = NULL; |
1f3c5efc JR |
282 | LIST_FOR_EACH (table, list_node, &cls->tables_priority) { |
283 | struct cls_rule *rule = find_match(table, flow); | |
74f74083 EJ |
284 | |
285 | if (wc) { | |
286 | flow_wildcards_fold_minimask(wc, &table->mask); | |
287 | } | |
1f3c5efc JR |
288 | if (rule) { |
289 | best = rule; | |
290 | LIST_FOR_EACH_CONTINUE (table, list_node, &cls->tables_priority) { | |
291 | if (table->max_priority <= best->priority) { | |
292 | /* Tables in descending priority order, | |
293 | * can not find anything better. */ | |
294 | return best; | |
295 | } | |
296 | rule = find_match(table, flow); | |
74f74083 EJ |
297 | if (wc) { |
298 | flow_wildcards_fold_minimask(wc, &table->mask); | |
299 | } | |
1f3c5efc JR |
300 | if (rule && rule->priority > best->priority) { |
301 | best = rule; | |
302 | } | |
4d935a6b | 303 | } |
1f3c5efc | 304 | break; |
b5d97350 | 305 | } |
48c3de13 | 306 | } |
b5d97350 | 307 | return best; |
48c3de13 BP |
308 | } |
309 | ||
b5d97350 BP |
310 | /* Finds and returns a rule in 'cls' with exactly the same priority and |
311 | * matching criteria as 'target'. Returns a null pointer if 'cls' doesn't | |
c084ce1d | 312 | * contain an exact match. */ |
064af421 BP |
313 | struct cls_rule * |
314 | classifier_find_rule_exactly(const struct classifier *cls, | |
76ecc721 | 315 | const struct cls_rule *target) |
064af421 | 316 | { |
b5d97350 BP |
317 | struct cls_rule *head, *rule; |
318 | struct cls_table *table; | |
064af421 | 319 | |
5cb7a798 | 320 | table = find_table(cls, &target->match.mask); |
b5d97350 BP |
321 | if (!table) { |
322 | return NULL; | |
064af421 BP |
323 | } |
324 | ||
4d935a6b JR |
325 | /* Skip if there is no hope. */ |
326 | if (target->priority > table->max_priority) { | |
327 | return NULL; | |
328 | } | |
329 | ||
81a76618 | 330 | head = find_equal(table, &target->match.flow, |
5cb7a798 BP |
331 | miniflow_hash_in_minimask(&target->match.flow, |
332 | &target->match.mask, 0)); | |
b5d97350 BP |
333 | FOR_EACH_RULE_IN_LIST (rule, head) { |
334 | if (target->priority >= rule->priority) { | |
335 | return target->priority == rule->priority ? rule : NULL; | |
064af421 BP |
336 | } |
337 | } | |
338 | return NULL; | |
339 | } | |
340 | ||
81a76618 BP |
341 | /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the |
342 | * same matching criteria as 'target'. Returns a null pointer if 'cls' doesn't | |
343 | * contain an exact match. */ | |
344 | struct cls_rule * | |
345 | classifier_find_match_exactly(const struct classifier *cls, | |
346 | const struct match *target, | |
347 | unsigned int priority) | |
348 | { | |
349 | struct cls_rule *retval; | |
350 | struct cls_rule cr; | |
351 | ||
352 | cls_rule_init(&cr, target, priority); | |
353 | retval = classifier_find_rule_exactly(cls, &cr); | |
48d28ac1 | 354 | cls_rule_destroy(&cr); |
81a76618 BP |
355 | |
356 | return retval; | |
357 | } | |
358 | ||
faa50f40 BP |
359 | /* Checks if 'target' would overlap any other rule in 'cls'. Two rules are |
360 | * considered to overlap if both rules have the same priority and a packet | |
361 | * could match both. */ | |
49bdc010 JP |
362 | bool |
363 | classifier_rule_overlaps(const struct classifier *cls, | |
faa50f40 | 364 | const struct cls_rule *target) |
49bdc010 | 365 | { |
b5d97350 | 366 | struct cls_table *table; |
49bdc010 | 367 | |
1f3c5efc JR |
368 | /* Iterate tables in the descending max priority order. */ |
369 | LIST_FOR_EACH (table, list_node, &cls->tables_priority) { | |
5cb7a798 BP |
370 | uint32_t storage[FLOW_U32S]; |
371 | struct minimask mask; | |
b5d97350 | 372 | struct cls_rule *head; |
49bdc010 | 373 | |
4d935a6b | 374 | if (target->priority > table->max_priority) { |
1f3c5efc | 375 | break; /* Can skip this and the rest of the tables. */ |
4d935a6b JR |
376 | } |
377 | ||
5cb7a798 | 378 | minimask_combine(&mask, &target->match.mask, &table->mask, storage); |
b5d97350 | 379 | HMAP_FOR_EACH (head, hmap_node, &table->rules) { |
49bdc010 JP |
380 | struct cls_rule *rule; |
381 | ||
b5d97350 | 382 | FOR_EACH_RULE_IN_LIST (rule, head) { |
4d935a6b JR |
383 | if (rule->priority < target->priority) { |
384 | break; /* Rules in descending priority order. */ | |
385 | } | |
faa50f40 | 386 | if (rule->priority == target->priority |
5cb7a798 BP |
387 | && miniflow_equal_in_minimask(&target->match.flow, |
388 | &rule->match.flow, &mask)) { | |
49bdc010 JP |
389 | return true; |
390 | } | |
391 | } | |
392 | } | |
393 | } | |
394 | ||
395 | return false; | |
396 | } | |
6ceeaa92 BP |
397 | |
398 | /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more | |
399 | * specific than 'criteria'. That is, 'rule' matches 'criteria' and this | |
400 | * function returns true if, for every field: | |
401 | * | |
402 | * - 'criteria' and 'rule' specify the same (non-wildcarded) value for the | |
403 | * field, or | |
404 | * | |
405 | * - 'criteria' wildcards the field, | |
406 | * | |
407 | * Conversely, 'rule' does not match 'criteria' and this function returns false | |
408 | * if, for at least one field: | |
409 | * | |
410 | * - 'criteria' and 'rule' specify different values for the field, or | |
411 | * | |
412 | * - 'criteria' specifies a value for the field but 'rule' wildcards it. | |
413 | * | |
414 | * Equivalently, the truth table for whether a field matches is: | |
415 | * | |
416 | * rule | |
417 | * | |
418 | * c wildcard exact | |
419 | * r +---------+---------+ | |
420 | * i wild | yes | yes | | |
421 | * t card | | | | |
422 | * e +---------+---------+ | |
423 | * r exact | no |if values| | |
424 | * i | |are equal| | |
425 | * a +---------+---------+ | |
426 | * | |
427 | * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD | |
428 | * commands and by OpenFlow 1.0 aggregate and flow stats. | |
429 | * | |
81a76618 | 430 | * Ignores rule->priority. */ |
6ceeaa92 BP |
431 | bool |
432 | cls_rule_is_loose_match(const struct cls_rule *rule, | |
5cb7a798 | 433 | const struct minimatch *criteria) |
6ceeaa92 | 434 | { |
5cb7a798 BP |
435 | return (!minimask_has_extra(&rule->match.mask, &criteria->mask) |
436 | && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow, | |
437 | &criteria->mask)); | |
6ceeaa92 | 438 | } |
b5d97350 | 439 | \f |
5ecc9d81 BP |
440 | /* Iteration. */ |
441 | ||
442 | static bool | |
443 | rule_matches(const struct cls_rule *rule, const struct cls_rule *target) | |
444 | { | |
445 | return (!target | |
5cb7a798 BP |
446 | || miniflow_equal_in_minimask(&rule->match.flow, |
447 | &target->match.flow, | |
448 | &target->match.mask)); | |
5ecc9d81 BP |
449 | } |
450 | ||
451 | static struct cls_rule * | |
452 | search_table(const struct cls_table *table, const struct cls_rule *target) | |
453 | { | |
5cb7a798 | 454 | if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) { |
5ecc9d81 BP |
455 | struct cls_rule *rule; |
456 | ||
457 | HMAP_FOR_EACH (rule, hmap_node, &table->rules) { | |
458 | if (rule_matches(rule, target)) { | |
459 | return rule; | |
460 | } | |
461 | } | |
462 | } | |
463 | return NULL; | |
464 | } | |
465 | ||
6ceeaa92 | 466 | /* Initializes 'cursor' for iterating through rules in 'cls': |
5ecc9d81 | 467 | * |
6ceeaa92 | 468 | * - If 'target' is null, the cursor will visit every rule in 'cls'. |
5ecc9d81 | 469 | * |
6ceeaa92 BP |
470 | * - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls' |
471 | * such that cls_rule_is_loose_match(rule, target) returns true. | |
5ecc9d81 | 472 | * |
6ceeaa92 | 473 | * Ignores target->priority. */ |
5ecc9d81 BP |
474 | void |
475 | cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls, | |
476 | const struct cls_rule *target) | |
477 | { | |
478 | cursor->cls = cls; | |
3ca1de08 | 479 | cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL; |
5ecc9d81 BP |
480 | } |
481 | ||
482 | /* Returns the first matching cls_rule in 'cursor''s iteration, or a null | |
483 | * pointer if there are no matches. */ | |
484 | struct cls_rule * | |
485 | cls_cursor_first(struct cls_cursor *cursor) | |
486 | { | |
487 | struct cls_table *table; | |
488 | ||
7ac8d8cf | 489 | HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) { |
5ecc9d81 BP |
490 | struct cls_rule *rule = search_table(table, cursor->target); |
491 | if (rule) { | |
492 | cursor->table = table; | |
493 | return rule; | |
494 | } | |
495 | } | |
496 | ||
497 | return NULL; | |
498 | } | |
499 | ||
500 | /* Returns the next matching cls_rule in 'cursor''s iteration, or a null | |
501 | * pointer if there are no more matches. */ | |
502 | struct cls_rule * | |
503 | cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule) | |
504 | { | |
505 | const struct cls_table *table; | |
506 | struct cls_rule *next; | |
507 | ||
955f579d BP |
508 | next = next_rule_in_list__(rule); |
509 | if (next->priority < rule->priority) { | |
5ecc9d81 BP |
510 | return next; |
511 | } | |
512 | ||
955f579d BP |
513 | /* 'next' is the head of the list, that is, the rule that is included in |
514 | * the table's hmap. (This is important when the classifier contains rules | |
515 | * that differ only in priority.) */ | |
516 | rule = next; | |
5ecc9d81 BP |
517 | HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) { |
518 | if (rule_matches(rule, cursor->target)) { | |
519 | return rule; | |
520 | } | |
521 | } | |
522 | ||
7ac8d8cf BP |
523 | table = cursor->table; |
524 | HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) { | |
5ecc9d81 BP |
525 | rule = search_table(table, cursor->target); |
526 | if (rule) { | |
527 | cursor->table = table; | |
528 | return rule; | |
529 | } | |
530 | } | |
531 | ||
532 | return NULL; | |
533 | } | |
534 | \f | |
b5d97350 | 535 | static struct cls_table * |
5cb7a798 | 536 | find_table(const struct classifier *cls, const struct minimask *mask) |
b5d97350 BP |
537 | { |
538 | struct cls_table *table; | |
064af421 | 539 | |
5cb7a798 | 540 | HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0), |
b5d97350 | 541 | &cls->tables) { |
5cb7a798 | 542 | if (minimask_equal(mask, &table->mask)) { |
b5d97350 | 543 | return table; |
064af421 BP |
544 | } |
545 | } | |
b5d97350 | 546 | return NULL; |
064af421 | 547 | } |
064af421 | 548 | |
b5d97350 | 549 | static struct cls_table * |
5cb7a798 | 550 | insert_table(struct classifier *cls, const struct minimask *mask) |
b5d97350 BP |
551 | { |
552 | struct cls_table *table; | |
064af421 | 553 | |
b5d97350 BP |
554 | table = xzalloc(sizeof *table); |
555 | hmap_init(&table->rules); | |
5cb7a798 BP |
556 | minimask_clone(&table->mask, mask); |
557 | hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0)); | |
1f3c5efc | 558 | list_push_back(&cls->tables_priority, &table->list_node); |
064af421 | 559 | |
b5d97350 | 560 | return table; |
064af421 BP |
561 | } |
562 | ||
b5d97350 BP |
563 | static void |
564 | destroy_table(struct classifier *cls, struct cls_table *table) | |
565 | { | |
5cb7a798 | 566 | minimask_destroy(&table->mask); |
b5d97350 BP |
567 | hmap_remove(&cls->tables, &table->hmap_node); |
568 | hmap_destroy(&table->rules); | |
1f3c5efc | 569 | list_remove(&table->list_node); |
b5d97350 BP |
570 | free(table); |
571 | } | |
064af421 | 572 | |
4aacd02d BP |
573 | /* This function performs the following updates for 'table' in 'cls' following |
574 | * the addition of a new rule with priority 'new_priority' to 'table': | |
575 | * | |
576 | * - Update 'table->max_priority' and 'table->max_count' if necessary. | |
577 | * | |
578 | * - Update 'table''s position in 'cls->tables_priority' if necessary. | |
579 | * | |
580 | * This function should only be called after adding a new rule, not after | |
581 | * replacing a rule by an identical one or modifying a rule in-place. */ | |
582 | static void | |
583 | update_tables_after_insertion(struct classifier *cls, struct cls_table *table, | |
584 | unsigned int new_priority) | |
585 | { | |
586 | if (new_priority == table->max_priority) { | |
587 | ++table->max_count; | |
588 | } else if (new_priority > table->max_priority) { | |
589 | struct cls_table *iter; | |
590 | ||
591 | table->max_priority = new_priority; | |
592 | table->max_count = 1; | |
593 | ||
594 | /* Possibly move 'table' earlier in the priority list. If we break out | |
595 | * of the loop, then 'table' should be moved just after that 'iter'. | |
596 | * If the loop terminates normally, then 'iter' will be the list head | |
597 | * and we'll move table just after that (e.g. to the front of the | |
598 | * list). */ | |
599 | iter = table; | |
600 | LIST_FOR_EACH_REVERSE_CONTINUE (iter, list_node, | |
601 | &cls->tables_priority) { | |
602 | if (iter->max_priority >= table->max_priority) { | |
603 | break; | |
604 | } | |
605 | } | |
606 | ||
607 | /* Move 'table' just after 'iter' (unless it's already there). */ | |
608 | if (iter->list_node.next != &table->list_node) { | |
609 | list_splice(iter->list_node.next, | |
610 | &table->list_node, table->list_node.next); | |
611 | } | |
612 | } | |
613 | } | |
614 | ||
615 | /* This function performs the following updates for 'table' in 'cls' following | |
616 | * the deletion of a rule with priority 'del_priority' from 'table': | |
617 | * | |
618 | * - Update 'table->max_priority' and 'table->max_count' if necessary. | |
619 | * | |
620 | * - Update 'table''s position in 'cls->tables_priority' if necessary. | |
621 | * | |
622 | * This function should only be called after removing a rule, not after | |
623 | * replacing a rule by an identical one or modifying a rule in-place. */ | |
624 | static void | |
625 | update_tables_after_removal(struct classifier *cls, struct cls_table *table, | |
626 | unsigned int del_priority) | |
627 | { | |
628 | struct cls_table *iter; | |
629 | ||
630 | if (del_priority == table->max_priority && --table->max_count == 0) { | |
631 | struct cls_rule *head; | |
632 | ||
633 | table->max_priority = 0; | |
634 | HMAP_FOR_EACH (head, hmap_node, &table->rules) { | |
635 | if (head->priority > table->max_priority) { | |
636 | table->max_priority = head->priority; | |
637 | table->max_count = 1; | |
638 | } else if (head->priority == table->max_priority) { | |
639 | ++table->max_count; | |
640 | } | |
641 | } | |
642 | ||
643 | /* Possibly move 'table' later in the priority list. If we break out | |
644 | * of the loop, then 'table' should be moved just before that 'iter'. | |
645 | * If the loop terminates normally, then 'iter' will be the list head | |
646 | * and we'll move table just before that (e.g. to the back of the | |
647 | * list). */ | |
648 | iter = table; | |
649 | LIST_FOR_EACH_CONTINUE (iter, list_node, &cls->tables_priority) { | |
650 | if (iter->max_priority <= table->max_priority) { | |
651 | break; | |
652 | } | |
653 | } | |
654 | ||
655 | /* Move 'table' just before 'iter' (unless it's already there). */ | |
656 | if (iter->list_node.prev != &table->list_node) { | |
657 | list_splice(&iter->list_node, | |
658 | &table->list_node, table->list_node.next); | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
064af421 | 663 | static struct cls_rule * |
b5d97350 BP |
664 | find_match(const struct cls_table *table, const struct flow *flow) |
665 | { | |
5cb7a798 | 666 | uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0); |
b5d97350 | 667 | struct cls_rule *rule; |
b5d97350 | 668 | |
5cb7a798 BP |
669 | HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &table->rules) { |
670 | if (miniflow_equal_flow_in_minimask(&rule->match.flow, flow, | |
671 | &table->mask)) { | |
b5d97350 | 672 | return rule; |
064af421 BP |
673 | } |
674 | } | |
c23740be | 675 | |
064af421 BP |
676 | return NULL; |
677 | } | |
678 | ||
679 | static struct cls_rule * | |
5cb7a798 | 680 | find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash) |
064af421 | 681 | { |
b5d97350 | 682 | struct cls_rule *head; |
064af421 | 683 | |
b5d97350 | 684 | HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) { |
5cb7a798 | 685 | if (miniflow_equal(&head->match.flow, flow)) { |
b5d97350 | 686 | return head; |
064af421 BP |
687 | } |
688 | } | |
689 | return NULL; | |
690 | } | |
691 | ||
b5d97350 | 692 | static struct cls_rule * |
4aacd02d BP |
693 | insert_rule(struct classifier *cls, |
694 | struct cls_table *table, struct cls_rule *new) | |
064af421 | 695 | { |
b5d97350 | 696 | struct cls_rule *head; |
4aacd02d | 697 | struct cls_rule *old = NULL; |
064af421 | 698 | |
5cb7a798 BP |
699 | new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow, |
700 | &new->match.mask, 0); | |
064af421 | 701 | |
81a76618 | 702 | head = find_equal(table, &new->match.flow, new->hmap_node.hash); |
b5d97350 BP |
703 | if (!head) { |
704 | hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash); | |
705 | list_init(&new->list); | |
4aacd02d | 706 | goto out; |
b5d97350 BP |
707 | } else { |
708 | /* Scan the list for the insertion point that will keep the list in | |
709 | * order of decreasing priority. */ | |
710 | struct cls_rule *rule; | |
711 | FOR_EACH_RULE_IN_LIST (rule, head) { | |
712 | if (new->priority >= rule->priority) { | |
713 | if (rule == head) { | |
714 | /* 'new' is the new highest-priority flow in the list. */ | |
715 | hmap_replace(&table->rules, | |
716 | &rule->hmap_node, &new->hmap_node); | |
717 | } | |
064af421 | 718 | |
b5d97350 BP |
719 | if (new->priority == rule->priority) { |
720 | list_replace(&new->list, &rule->list); | |
4aacd02d BP |
721 | old = rule; |
722 | goto out; | |
b5d97350 BP |
723 | } else { |
724 | list_insert(&rule->list, &new->list); | |
4aacd02d | 725 | goto out; |
b5d97350 BP |
726 | } |
727 | } | |
728 | } | |
064af421 | 729 | |
b5d97350 BP |
730 | /* Insert 'new' at the end of the list. */ |
731 | list_push_back(&head->list, &new->list); | |
064af421 | 732 | } |
4aacd02d BP |
733 | |
734 | out: | |
735 | if (!old) { | |
736 | update_tables_after_insertion(cls, table, new->priority); | |
737 | } | |
738 | return old; | |
064af421 BP |
739 | } |
740 | ||
b5d97350 | 741 | static struct cls_rule * |
955f579d | 742 | next_rule_in_list__(struct cls_rule *rule) |
064af421 | 743 | { |
b5d97350 | 744 | struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list); |
955f579d BP |
745 | return next; |
746 | } | |
747 | ||
748 | static struct cls_rule * | |
749 | next_rule_in_list(struct cls_rule *rule) | |
750 | { | |
751 | struct cls_rule *next = next_rule_in_list__(rule); | |
b5d97350 | 752 | return next->priority < rule->priority ? next : NULL; |
064af421 | 753 | } |