Merge commit '259e0b1ad1bfea762a76f0098deb8f8d8db1dfa3'
[sliver-openvswitch.git] / lib / classifier.c
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 #include <config.h>
18 #include "classifier.h"
19 #include <errno.h>
20 #include <netinet/in.h>
21 #include "byte-order.h"
22 #include "dynamic-string.h"
23 #include "flow.h"
24 #include "hash.h"
25 #include "odp-util.h"
26 #include "ofp-util.h"
27 #include "packets.h"
28 #include "ovs-thread.h"
29
30 static struct cls_table *find_table(const struct classifier *,
31                                     const struct minimask *);
32 static struct cls_table *insert_table(struct classifier *,
33                                       const struct minimask *);
34
35 static void destroy_table(struct classifier *, struct cls_table *);
36
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
44 static struct cls_rule *find_match(const struct cls_table *,
45                                    const struct flow *);
46 static struct cls_rule *find_equal(struct cls_table *,
47                                    const struct miniflow *, uint32_t hash);
48 static struct cls_rule *insert_rule(struct classifier *,
49                                     struct cls_table *, struct cls_rule *);
50
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
59 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
60 static struct cls_rule *next_rule_in_list(struct cls_rule *);
61 \f
62 /* cls_rule. */
63
64 /* Initializes 'rule' to match packets specified by 'match' at the given
65  * 'priority'.  'match' must satisfy the invariant described in the comment at
66  * the definition of struct match.
67  *
68  * The caller must eventually destroy 'rule' with cls_rule_destroy().
69  *
70  * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
71  * internally Open vSwitch supports a wider range.) */
72 void
73 cls_rule_init(struct cls_rule *rule,
74               const struct match *match, unsigned int priority)
75 {
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);
87     rule->priority = priority;
88 }
89
90 /* Initializes 'dst' as a copy of 'src'.
91  *
92  * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
93 void
94 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
95 {
96     minimatch_clone(&dst->match, &src->match);
97     dst->priority = src->priority;
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.) */
104 void
105 cls_rule_destroy(struct cls_rule *rule)
106 {
107     minimatch_destroy(&rule->match);
108 }
109
110 /* Returns true if 'a' and 'b' match the same packets at the same priority,
111  * false if they differ in some way. */
112 bool
113 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
114 {
115     return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
116 }
117
118 /* Returns a hash value for 'rule', folding in 'basis'. */
119 uint32_t
120 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
121 {
122     return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
123 }
124
125 /* Appends a string describing 'rule' to 's'. */
126 void
127 cls_rule_format(const struct cls_rule *rule, struct ds *s)
128 {
129     minimatch_format(&rule->match, s, rule->priority);
130 }
131
132 /* Returns true if 'rule' matches every packet, false otherwise. */
133 bool
134 cls_rule_is_catchall(const struct cls_rule *rule)
135 {
136     return minimask_is_catchall(&rule->match.mask);
137 }
138 \f
139 /* Initializes 'cls' as a classifier that initially contains no classification
140  * rules. */
141 void
142 classifier_init(struct classifier *cls)
143 {
144     cls->n_rules = 0;
145     hmap_init(&cls->tables);
146     list_init(&cls->tables_priority);
147     ovs_rwlock_init(&cls->rwlock);
148 }
149
150 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
151  * caller's responsibility. */
152 void
153 classifier_destroy(struct classifier *cls)
154 {
155     if (cls) {
156         struct cls_table *table, *next_table;
157
158         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
159             destroy_table(cls, table);
160         }
161         hmap_destroy(&cls->tables);
162         ovs_rwlock_destroy(&cls->rwlock);
163     }
164 }
165
166 /* Returns true if 'cls' contains no classification rules, false otherwise. */
167 bool
168 classifier_is_empty(const struct classifier *cls)
169 {
170     return cls->n_rules == 0;
171 }
172
173 /* Returns the number of rules in 'cls'. */
174 int
175 classifier_count(const struct classifier *cls)
176 {
177     return cls->n_rules;
178 }
179
180 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
181  * must not modify or free it.
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
186  * is thus responsible for destroying it with cls_rule_destroy(), freeing the
187  * memory block in which it resides, etc., as necessary.
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. */
193 struct cls_rule *
194 classifier_replace(struct classifier *cls, struct cls_rule *rule)
195 {
196     struct cls_rule *old_rule;
197     struct cls_table *table;
198
199     table = find_table(cls, &rule->match.mask);
200     if (!table) {
201         table = insert_table(cls, &rule->match.mask);
202     }
203
204     old_rule = insert_rule(cls, table, rule);
205     if (!old_rule) {
206         table->n_table_rules++;
207         cls->n_rules++;
208     }
209     return old_rule;
210 }
211
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. */
218 void
219 classifier_insert(struct classifier *cls, struct cls_rule *rule)
220 {
221     struct cls_rule *displaced_rule = classifier_replace(cls, rule);
222     ovs_assert(!displaced_rule);
223 }
224
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. */
228 void
229 classifier_remove(struct classifier *cls, struct cls_rule *rule)
230 {
231     struct cls_rule *head;
232     struct cls_table *table;
233
234     table = find_table(cls, &rule->match.mask);
235     head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
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);
243
244         list_remove(&rule->list);
245         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
246     }
247
248     if (--table->n_table_rules == 0) {
249         destroy_table(cls, table);
250     } else {
251         update_tables_after_removal(cls, table, rule->priority);
252     }
253     cls->n_rules--;
254 }
255
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
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()). */
264 struct cls_rule *
265 classifier_lookup(const struct classifier *cls, const struct flow *flow,
266                   struct flow_wildcards *wc)
267 {
268     struct cls_table *table;
269     struct cls_rule *best;
270
271     best = NULL;
272     LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
273         struct cls_rule *rule = find_match(table, flow);
274
275         if (wc) {
276             flow_wildcards_fold_minimask(wc, &table->mask);
277         }
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);
287                 if (wc) {
288                     flow_wildcards_fold_minimask(wc, &table->mask);
289                 }
290                 if (rule && rule->priority > best->priority) {
291                     best = rule;
292                 }
293             }
294             break;
295         }
296     }
297     return best;
298 }
299
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
302  * contain an exact match. */
303 struct cls_rule *
304 classifier_find_rule_exactly(const struct classifier *cls,
305                              const struct cls_rule *target)
306 {
307     struct cls_rule *head, *rule;
308     struct cls_table *table;
309
310     table = find_table(cls, &target->match.mask);
311     if (!table) {
312         return NULL;
313     }
314
315     /* Skip if there is no hope. */
316     if (target->priority > table->max_priority) {
317         return NULL;
318     }
319
320     head = find_equal(table, &target->match.flow,
321                       miniflow_hash_in_minimask(&target->match.flow,
322                                                 &target->match.mask, 0));
323     FOR_EACH_RULE_IN_LIST (rule, head) {
324         if (target->priority >= rule->priority) {
325             return target->priority == rule->priority ? rule : NULL;
326         }
327     }
328     return NULL;
329 }
330
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. */
334 struct cls_rule *
335 classifier_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);
344     cls_rule_destroy(&cr);
345
346     return retval;
347 }
348
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. */
352 bool
353 classifier_rule_overlaps(const struct classifier *cls,
354                          const struct cls_rule *target)
355 {
356     struct cls_table *table;
357
358     /* Iterate tables in the descending max priority order. */
359     LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
360         uint32_t storage[FLOW_U32S];
361         struct minimask mask;
362         struct cls_rule *head;
363
364         if (target->priority > table->max_priority) {
365             break; /* Can skip this and the rest of the tables. */
366         }
367
368         minimask_combine(&mask, &target->match.mask, &table->mask, storage);
369         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
370             struct cls_rule *rule;
371
372             FOR_EACH_RULE_IN_LIST (rule, head) {
373                 if (rule->priority < target->priority) {
374                     break; /* Rules in descending priority order. */
375                 }
376                 if (rule->priority == target->priority
377                     && miniflow_equal_in_minimask(&target->match.flow,
378                                                   &rule->match.flow, &mask)) {
379                     return true;
380                 }
381             }
382         }
383     }
384
385     return false;
386 }
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  *
420  * Ignores rule->priority. */
421 bool
422 cls_rule_is_loose_match(const struct cls_rule *rule,
423                         const struct minimatch *criteria)
424 {
425     return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
426             && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
427                                           &criteria->mask));
428 }
429 \f
430 /* Iteration. */
431
432 static bool
433 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
434 {
435     return (!target
436             || miniflow_equal_in_minimask(&rule->match.flow,
437                                           &target->match.flow,
438                                           &target->match.mask));
439 }
440
441 static struct cls_rule *
442 search_table(const struct cls_table *table, const struct cls_rule *target)
443 {
444     if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
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
456 /* Initializes 'cursor' for iterating through rules in 'cls':
457  *
458  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
459  *
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.
462  *
463  * Ignores target->priority. */
464 void
465 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
466                 const struct cls_rule *target)
467 {
468     cursor->cls = cls;
469     cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
470 }
471
472 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
473  * pointer if there are no matches. */
474 struct cls_rule *
475 cls_cursor_first(struct cls_cursor *cursor)
476 {
477     struct cls_table *table;
478
479     HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
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. */
492 struct cls_rule *
493 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
494 {
495     const struct cls_table *table;
496     struct cls_rule *next;
497
498     next = next_rule_in_list__(rule);
499     if (next->priority < rule->priority) {
500         return next;
501     }
502
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;
507     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
508         if (rule_matches(rule, cursor->target)) {
509             return rule;
510         }
511     }
512
513     table = cursor->table;
514     HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
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
525 static struct cls_table *
526 find_table(const struct classifier *cls, const struct minimask *mask)
527 {
528     struct cls_table *table;
529
530     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
531                              &cls->tables) {
532         if (minimask_equal(mask, &table->mask)) {
533             return table;
534         }
535     }
536     return NULL;
537 }
538
539 static struct cls_table *
540 insert_table(struct classifier *cls, const struct minimask *mask)
541 {
542     struct cls_table *table;
543
544     table = xzalloc(sizeof *table);
545     hmap_init(&table->rules);
546     minimask_clone(&table->mask, mask);
547     hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
548     list_push_back(&cls->tables_priority, &table->list_node);
549
550     return table;
551 }
552
553 static void
554 destroy_table(struct classifier *cls, struct cls_table *table)
555 {
556     minimask_destroy(&table->mask);
557     hmap_remove(&cls->tables, &table->hmap_node);
558     hmap_destroy(&table->rules);
559     list_remove(&table->list_node);
560     free(table);
561 }
562
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. */
572 static void
573 update_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. */
614 static void
615 update_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
653 static struct cls_rule *
654 find_match(const struct cls_table *table, const struct flow *flow)
655 {
656     uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
657     struct cls_rule *rule;
658
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)) {
662             return rule;
663         }
664     }
665
666     return NULL;
667 }
668
669 static struct cls_rule *
670 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
671 {
672     struct cls_rule *head;
673
674     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
675         if (miniflow_equal(&head->match.flow, flow)) {
676             return head;
677         }
678     }
679     return NULL;
680 }
681
682 static struct cls_rule *
683 insert_rule(struct classifier *cls,
684             struct cls_table *table, struct cls_rule *new)
685 {
686     struct cls_rule *head;
687     struct cls_rule *old = NULL;
688
689     new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
690                                                     &new->match.mask, 0);
691
692     head = find_equal(table, &new->match.flow, new->hmap_node.hash);
693     if (!head) {
694         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
695         list_init(&new->list);
696         goto out;
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                 }
708
709                 if (new->priority == rule->priority) {
710                     list_replace(&new->list, &rule->list);
711                     old = rule;
712                     goto out;
713                 } else {
714                     list_insert(&rule->list, &new->list);
715                     goto out;
716                 }
717             }
718         }
719
720         /* Insert 'new' at the end of the list. */
721         list_push_back(&head->list, &new->list);
722     }
723
724  out:
725     if (!old) {
726         update_tables_after_insertion(cls, table, new->priority);
727     }
728     return old;
729 }
730
731 static struct cls_rule *
732 next_rule_in_list__(struct cls_rule *rule)
733 {
734     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
735     return next;
736 }
737
738 static struct cls_rule *
739 next_rule_in_list(struct cls_rule *rule)
740 {
741     struct cls_rule *next = next_rule_in_list__(rule);
742     return next->priority < rule->priority ? next : NULL;
743 }