classifier: New function cls_rule_move().
[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 'dst' 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 /* 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
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
115 cls_rule_destroy(struct cls_rule *rule)
116 {
117     minimatch_destroy(&rule->match);
118 }
119
120 /* Returns true if 'a' and 'b' match the same packets at the same priority,
121  * false if they differ in some way. */
122 bool
123 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
124 {
125     return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
126 }
127
128 /* Returns a hash value for 'rule', folding in 'basis'. */
129 uint32_t
130 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
131 {
132     return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
133 }
134
135 /* Appends a string describing 'rule' to 's'. */
136 void
137 cls_rule_format(const struct cls_rule *rule, struct ds *s)
138 {
139     minimatch_format(&rule->match, s, rule->priority);
140 }
141
142 /* Returns true if 'rule' matches every packet, false otherwise. */
143 bool
144 cls_rule_is_catchall(const struct cls_rule *rule)
145 {
146     return minimask_is_catchall(&rule->match.mask);
147 }
148 \f
149 /* Initializes 'cls' as a classifier that initially contains no classification
150  * rules. */
151 void
152 classifier_init(struct classifier *cls)
153 {
154     cls->n_rules = 0;
155     hmap_init(&cls->tables);
156     list_init(&cls->tables_priority);
157     ovs_rwlock_init(&cls->rwlock);
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) {
166         struct cls_table *table, *next_table;
167
168         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
169             destroy_table(cls, table);
170         }
171         hmap_destroy(&cls->tables);
172         ovs_rwlock_destroy(&cls->rwlock);
173     }
174 }
175
176 /* Returns true if 'cls' contains no classification rules, false otherwise. */
177 bool
178 classifier_is_empty(const struct classifier *cls)
179 {
180     return cls->n_rules == 0;
181 }
182
183 /* Returns the number of rules in 'cls'. */
184 int
185 classifier_count(const struct classifier *cls)
186 {
187     return cls->n_rules;
188 }
189
190 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
191  * must not modify or free it.
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
196  * is thus responsible for destroying it with cls_rule_destroy(), freeing the
197  * memory block in which it resides, etc., as necessary.
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 *
204 classifier_replace(struct classifier *cls, struct cls_rule *rule)
205 {
206     struct cls_rule *old_rule;
207     struct cls_table *table;
208
209     table = find_table(cls, &rule->match.mask);
210     if (!table) {
211         table = insert_table(cls, &rule->match.mask);
212     }
213
214     old_rule = insert_rule(cls, table, rule);
215     if (!old_rule) {
216         table->n_table_rules++;
217         cls->n_rules++;
218     }
219     return old_rule;
220 }
221
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);
232     ovs_assert(!displaced_rule);
233 }
234
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. */
238 void
239 classifier_remove(struct classifier *cls, struct cls_rule *rule)
240 {
241     struct cls_rule *head;
242     struct cls_table *table;
243
244     table = find_table(cls, &rule->match.mask);
245     head = find_equal(table, &rule->match.flow, rule->hmap_node.hash);
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);
253
254         list_remove(&rule->list);
255         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
256     }
257
258     if (--table->n_table_rules == 0) {
259         destroy_table(cls, table);
260     } else {
261         update_tables_after_removal(cls, table, rule->priority);
262     }
263     cls->n_rules--;
264 }
265
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
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()). */
274 struct cls_rule *
275 classifier_lookup(const struct classifier *cls, const struct flow *flow,
276                   struct flow_wildcards *wc)
277 {
278     struct cls_table *table;
279     struct cls_rule *best;
280
281     best = NULL;
282     LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
283         struct cls_rule *rule = find_match(table, flow);
284
285         if (wc) {
286             flow_wildcards_fold_minimask(wc, &table->mask);
287         }
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);
297                 if (wc) {
298                     flow_wildcards_fold_minimask(wc, &table->mask);
299                 }
300                 if (rule && rule->priority > best->priority) {
301                     best = rule;
302                 }
303             }
304             break;
305         }
306     }
307     return best;
308 }
309
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
312  * contain an exact match. */
313 struct cls_rule *
314 classifier_find_rule_exactly(const struct classifier *cls,
315                              const struct cls_rule *target)
316 {
317     struct cls_rule *head, *rule;
318     struct cls_table *table;
319
320     table = find_table(cls, &target->match.mask);
321     if (!table) {
322         return NULL;
323     }
324
325     /* Skip if there is no hope. */
326     if (target->priority > table->max_priority) {
327         return NULL;
328     }
329
330     head = find_equal(table, &target->match.flow,
331                       miniflow_hash_in_minimask(&target->match.flow,
332                                                 &target->match.mask, 0));
333     FOR_EACH_RULE_IN_LIST (rule, head) {
334         if (target->priority >= rule->priority) {
335             return target->priority == rule->priority ? rule : NULL;
336         }
337     }
338     return NULL;
339 }
340
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);
354     cls_rule_destroy(&cr);
355
356     return retval;
357 }
358
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. */
362 bool
363 classifier_rule_overlaps(const struct classifier *cls,
364                          const struct cls_rule *target)
365 {
366     struct cls_table *table;
367
368     /* Iterate tables in the descending max priority order. */
369     LIST_FOR_EACH (table, list_node, &cls->tables_priority) {
370         uint32_t storage[FLOW_U32S];
371         struct minimask mask;
372         struct cls_rule *head;
373
374         if (target->priority > table->max_priority) {
375             break; /* Can skip this and the rest of the tables. */
376         }
377
378         minimask_combine(&mask, &target->match.mask, &table->mask, storage);
379         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
380             struct cls_rule *rule;
381
382             FOR_EACH_RULE_IN_LIST (rule, head) {
383                 if (rule->priority < target->priority) {
384                     break; /* Rules in descending priority order. */
385                 }
386                 if (rule->priority == target->priority
387                     && miniflow_equal_in_minimask(&target->match.flow,
388                                                   &rule->match.flow, &mask)) {
389                     return true;
390                 }
391             }
392         }
393     }
394
395     return false;
396 }
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  *
430  * Ignores rule->priority. */
431 bool
432 cls_rule_is_loose_match(const struct cls_rule *rule,
433                         const struct minimatch *criteria)
434 {
435     return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
436             && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
437                                           &criteria->mask));
438 }
439 \f
440 /* Iteration. */
441
442 static bool
443 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
444 {
445     return (!target
446             || miniflow_equal_in_minimask(&rule->match.flow,
447                                           &target->match.flow,
448                                           &target->match.mask));
449 }
450
451 static struct cls_rule *
452 search_table(const struct cls_table *table, const struct cls_rule *target)
453 {
454     if (!target || !minimask_has_extra(&table->mask, &target->match.mask)) {
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
466 /* Initializes 'cursor' for iterating through rules in 'cls':
467  *
468  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
469  *
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.
472  *
473  * Ignores target->priority. */
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;
479     cursor->target = target && !cls_rule_is_catchall(target) ? target : NULL;
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
489     HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
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
508     next = next_rule_in_list__(rule);
509     if (next->priority < rule->priority) {
510         return next;
511     }
512
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;
517     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
518         if (rule_matches(rule, cursor->target)) {
519             return rule;
520         }
521     }
522
523     table = cursor->table;
524     HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
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
535 static struct cls_table *
536 find_table(const struct classifier *cls, const struct minimask *mask)
537 {
538     struct cls_table *table;
539
540     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, minimask_hash(mask, 0),
541                              &cls->tables) {
542         if (minimask_equal(mask, &table->mask)) {
543             return table;
544         }
545     }
546     return NULL;
547 }
548
549 static struct cls_table *
550 insert_table(struct classifier *cls, const struct minimask *mask)
551 {
552     struct cls_table *table;
553
554     table = xzalloc(sizeof *table);
555     hmap_init(&table->rules);
556     minimask_clone(&table->mask, mask);
557     hmap_insert(&cls->tables, &table->hmap_node, minimask_hash(mask, 0));
558     list_push_back(&cls->tables_priority, &table->list_node);
559
560     return table;
561 }
562
563 static void
564 destroy_table(struct classifier *cls, struct cls_table *table)
565 {
566     minimask_destroy(&table->mask);
567     hmap_remove(&cls->tables, &table->hmap_node);
568     hmap_destroy(&table->rules);
569     list_remove(&table->list_node);
570     free(table);
571 }
572
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
663 static struct cls_rule *
664 find_match(const struct cls_table *table, const struct flow *flow)
665 {
666     uint32_t hash = flow_hash_in_minimask(flow, &table->mask, 0);
667     struct cls_rule *rule;
668
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)) {
672             return rule;
673         }
674     }
675
676     return NULL;
677 }
678
679 static struct cls_rule *
680 find_equal(struct cls_table *table, const struct miniflow *flow, uint32_t hash)
681 {
682     struct cls_rule *head;
683
684     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
685         if (miniflow_equal(&head->match.flow, flow)) {
686             return head;
687         }
688     }
689     return NULL;
690 }
691
692 static struct cls_rule *
693 insert_rule(struct classifier *cls,
694             struct cls_table *table, struct cls_rule *new)
695 {
696     struct cls_rule *head;
697     struct cls_rule *old = NULL;
698
699     new->hmap_node.hash = miniflow_hash_in_minimask(&new->match.flow,
700                                                     &new->match.mask, 0);
701
702     head = find_equal(table, &new->match.flow, new->hmap_node.hash);
703     if (!head) {
704         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
705         list_init(&new->list);
706         goto out;
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                 }
718
719                 if (new->priority == rule->priority) {
720                     list_replace(&new->list, &rule->list);
721                     old = rule;
722                     goto out;
723                 } else {
724                     list_insert(&rule->list, &new->list);
725                     goto out;
726                 }
727             }
728         }
729
730         /* Insert 'new' at the end of the list. */
731         list_push_back(&head->list, &new->list);
732     }
733
734  out:
735     if (!old) {
736         update_tables_after_insertion(cls, table, new->priority);
737     }
738     return old;
739 }
740
741 static struct cls_rule *
742 next_rule_in_list__(struct cls_rule *rule)
743 {
744     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
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);
752     return next->priority < rule->priority ? next : NULL;
753 }