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