flow: Fully separate flow_wildcards from OpenFlow wildcard bits.
[sliver-openvswitch.git] / lib / classifier.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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 <assert.h>
20 #include <errno.h>
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "hash.h"
26 #include "ofp-util.h"
27 #include "packets.h"
28
29 static struct cls_table *find_table(const struct classifier *,
30                                     const struct flow_wildcards *);
31 static struct cls_table *insert_table(struct classifier *,
32                                       const struct flow_wildcards *);
33
34 static struct cls_table *classifier_first_table(const struct classifier *);
35 static struct cls_table *classifier_next_table(const struct classifier *,
36                                                const struct cls_table *);
37 static void destroy_table(struct classifier *, struct cls_table *);
38
39 static struct cls_rule *find_match(const struct cls_table *,
40                                    const struct flow *);
41 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
42                                    uint32_t hash);
43 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
44
45 static bool flow_equal_except(const struct flow *, const struct flow *,
46                                 const struct flow_wildcards *);
47 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
48
49 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
50 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD)                               \
51     for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
52 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD)                    \
53     for ((RULE) = (HEAD);                                               \
54          (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true);    \
55          (RULE) = (NEXT))
56
57 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
58 static struct cls_rule *next_rule_in_list(struct cls_rule *);
59
60 static struct cls_table *
61 cls_table_from_hmap_node(const struct hmap_node *node)
62 {
63     return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
64 }
65
66 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
67  * 'wildcards' and 'priority'. */
68 void
69 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
70               unsigned int priority, struct cls_rule *rule)
71 {
72     rule->flow = *flow;
73     rule->wc = *wildcards;
74     rule->priority = priority;
75     cls_rule_zero_wildcarded_fields(rule);
76 }
77
78 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
79  * given 'priority'.  (For OpenFlow 1.0, exact-match rule are always highest
80  * priority, so 'priority' should be at least 65535.) */
81 void
82 cls_rule_init_exact(const struct flow *flow,
83                     unsigned int priority, struct cls_rule *rule)
84 {
85     rule->flow = *flow;
86     flow_wildcards_init_exact(&rule->wc);
87     rule->priority = priority;
88 }
89
90 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
91  * priority 'priority'. */
92 void
93 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
94 {
95     memset(&rule->flow, 0, sizeof rule->flow);
96     flow_wildcards_init_catchall(&rule->wc);
97     rule->priority = priority;
98 }
99
100 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
101  * field in 'flow' to all-0-bits.  It is important to maintain this invariant
102  * in a clr_rule that might be inserted into a classifier.
103  *
104  * It is never necessary to call this function directly for a cls_rule that is
105  * initialized or modified only by cls_rule_*() functions.  It is useful to
106  * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
107  */
108 void
109 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
110 {
111     zero_wildcards(&rule->flow, &rule->wc);
112 }
113
114 void
115 cls_rule_set_in_port(struct cls_rule *rule, uint16_t odp_port)
116 {
117     rule->wc.wildcards &= ~FWW_IN_PORT;
118     rule->flow.in_port = odp_port;
119 }
120
121 void
122 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
123 {
124     rule->wc.wildcards &= ~FWW_DL_TYPE;
125     rule->flow.dl_type = dl_type;
126 }
127
128 void
129 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
130 {
131     rule->wc.wildcards &= ~FWW_DL_SRC;
132     memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
133 }
134
135 void
136 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
137 {
138     rule->wc.wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
139     memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
140 }
141
142 bool
143 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
144 {
145     return cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
146 }
147
148 bool
149 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
150 {
151     switch (ntohs(mask)) {
152     case 0xffff:
153         if (tci == htons(0)) {
154             /* Match only packets that have no 802.1Q header. */
155             rule->wc.wildcards &= ~(FWW_DL_VLAN | FWW_DL_VLAN_PCP);
156             rule->flow.dl_vlan = htons(OFP_VLAN_NONE);
157             rule->flow.dl_vlan_pcp = 0;
158             return true;
159         } else if (tci & htons(VLAN_CFI)) {
160             /* Match only packets that have a specific 802.1Q VID and PCP. */
161             rule->wc.wildcards &= ~(FWW_DL_VLAN | FWW_DL_VLAN_PCP);
162             rule->flow.dl_vlan = htons(vlan_tci_to_vid(tci));
163             rule->flow.dl_vlan_pcp = vlan_tci_to_pcp(tci);
164             return true;
165         } else {
166             /* Impossible. */
167             return false;
168         }
169
170     case 0x1fff:
171         if (!(tci & htons(VLAN_CFI))) {
172             return false;
173         } else {
174             /* Match only packets that have a specific 802.1Q VID. */
175             cls_rule_set_dl_vlan(rule, tci & htons(VLAN_VID_MASK));
176             rule->wc.wildcards |= FWW_DL_VLAN_PCP;
177             rule->flow.dl_vlan_pcp = 0;
178             return true;
179         }
180
181     case 0xf000:
182         if (!(tci & htons(VLAN_CFI))) {
183             return false;
184         } else {
185             /* Match only packets that have a specific 802.1Q PCP. */
186             cls_rule_set_dl_vlan_pcp(rule, vlan_tci_to_pcp(tci));
187             rule->wc.wildcards |= FWW_DL_VLAN;
188             rule->flow.dl_vlan = 0;
189             return true;
190         }
191
192     case 0x0000:
193         /* Match anything. */
194         rule->wc.wildcards |= FWW_DL_VLAN | FWW_DL_VLAN_PCP;
195         rule->flow.dl_vlan = htons(0);
196         rule->flow.dl_vlan_pcp = 0;
197         return true;
198
199     default:
200         return false;
201     }
202 }
203
204 void
205 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
206 {
207     if (dl_vlan != htons(OFP_VLAN_NONE)) {
208         dl_vlan &= htons(VLAN_VID_MASK);
209     }
210
211     rule->wc.wildcards &= ~FWW_DL_VLAN;
212     rule->flow.dl_vlan = dl_vlan;
213 }
214
215 void
216 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
217 {
218     rule->wc.wildcards &= ~FWW_DL_VLAN_PCP;
219     rule->flow.dl_vlan_pcp = dl_vlan_pcp & 0x07;
220 }
221
222 void
223 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
224 {
225     rule->wc.wildcards &= ~FWW_TP_SRC;
226     rule->flow.tp_src = tp_src;
227 }
228
229 void
230 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
231 {
232     rule->wc.wildcards &= ~FWW_TP_DST;
233     rule->flow.tp_dst = tp_dst;
234 }
235
236 void
237 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
238 {
239     rule->wc.wildcards &= ~FWW_NW_PROTO;
240     rule->flow.nw_proto = nw_proto;
241 }
242
243 void
244 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
245 {
246     cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
247 }
248
249 bool
250 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
251 {
252     if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
253         rule->flow.nw_src = ip & mask;
254         return true;
255     } else {
256         return false;
257     }
258 }
259
260 void
261 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
262 {
263     cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
264 }
265
266 bool
267 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
268 {
269     if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
270         rule->flow.nw_dst = ip & mask;
271         return true;
272     } else {
273         return false;
274     }
275 }
276
277 void
278 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
279 {
280     rule->wc.wildcards &= ~FWW_NW_TOS;
281     rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
282 }
283
284 void
285 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
286 {
287     rule->wc.wildcards &= ~FWW_TP_SRC;
288     rule->flow.icmp_type = htons(icmp_type);
289
290 }
291
292 void
293 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
294 {
295     rule->wc.wildcards &= ~FWW_TP_DST;
296     rule->flow.icmp_code = htons(icmp_code);
297 }
298
299 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
300  * fields, and have the same values for fixed fields, otherwise false. */
301 bool
302 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
303 {
304     return (a->priority == b->priority
305             && flow_wildcards_equal(&a->wc, &b->wc)
306             && flow_equal(&a->flow, &b->flow));
307 }
308
309 /* Converts 'rule' to a string and returns the string.  The caller must free
310  * the string (with free()). */
311 char *
312 cls_rule_to_string(const struct cls_rule *rule)
313 {
314     struct ds s = DS_EMPTY_INITIALIZER;
315     ds_put_format(&s, "wildcards=%x priority=%u ",
316                   rule->wc.wildcards, rule->priority);
317     flow_format(&s, &rule->flow);
318     return ds_cstr(&s);
319 }
320
321 /* Prints cls_rule 'rule', for debugging.
322  *
323  * (The output could be improved and expanded, but this was good enough to
324  * debug the classifier.) */
325 void
326 cls_rule_print(const struct cls_rule *rule)
327 {
328     printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
329     flow_print(stdout, &rule->flow);
330     putc('\n', stdout);
331 }
332 \f
333 /* Initializes 'cls' as a classifier that initially contains no classification
334  * rules. */
335 void
336 classifier_init(struct classifier *cls)
337 {
338     cls->n_rules = 0;
339     hmap_init(&cls->tables);
340 }
341
342 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
343  * caller's responsibility. */
344 void
345 classifier_destroy(struct classifier *cls)
346 {
347     if (cls) {
348         struct cls_table *table, *next_table;
349
350         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
351             hmap_destroy(&table->rules);
352             hmap_remove(&cls->tables, &table->hmap_node);
353             free(table);
354         }
355         hmap_destroy(&cls->tables);
356     }
357 }
358
359 /* Returns true if 'cls' contains no classification rules, false otherwise. */
360 bool
361 classifier_is_empty(const struct classifier *cls)
362 {
363     return cls->n_rules == 0;
364 }
365
366 /* Returns the number of rules in 'classifier'. */
367 int
368 classifier_count(const struct classifier *cls)
369 {
370     return cls->n_rules;
371 }
372
373 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
374  * must not modify or free it.
375  *
376  * If 'cls' already contains an identical rule (including wildcards, values of
377  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
378  * rule that was replaced.  The caller takes ownership of the returned rule and
379  * is thus responsible for freeing it, etc., as necessary.
380  *
381  * Returns NULL if 'cls' does not contain a rule with an identical key, after
382  * inserting the new rule.  In this case, no rules are displaced by the new
383  * rule, even rules that cannot have any effect because the new rule matches a
384  * superset of their flows and has higher priority. */
385 struct cls_rule *
386 classifier_insert(struct classifier *cls, struct cls_rule *rule)
387 {
388     struct cls_rule *old_rule;
389     struct cls_table *table;
390
391     table = find_table(cls, &rule->wc);
392     if (!table) {
393         table = insert_table(cls, &rule->wc);
394     }
395
396     old_rule = insert_rule(table, rule);
397     if (!old_rule) {
398         table->n_table_rules++;
399         cls->n_rules++;
400     }
401     return old_rule;
402 }
403
404 /* Removes 'rule' from 'cls'.  It is the caller's responsibility to free
405  * 'rule', if this is desirable. */
406 void
407 classifier_remove(struct classifier *cls, struct cls_rule *rule)
408 {
409     struct cls_rule *head;
410     struct cls_table *table;
411
412     table = find_table(cls, &rule->wc);
413     head = find_equal(table, &rule->flow, rule->hmap_node.hash);
414     if (head != rule) {
415         list_remove(&rule->list);
416     } else if (list_is_empty(&rule->list)) {
417         hmap_remove(&table->rules, &rule->hmap_node);
418     } else {
419         struct cls_rule *next = CONTAINER_OF(rule->list.next,
420                                              struct cls_rule, list);
421
422         list_remove(&rule->list);
423         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
424     }
425
426     if (--table->n_table_rules == 0) {
427         destroy_table(cls, table);
428     }
429
430     cls->n_rules--;
431 }
432
433 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
434  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
435  * of equal priority match 'flow', returns one arbitrarily. */
436 struct cls_rule *
437 classifier_lookup(const struct classifier *cls, const struct flow *flow)
438 {
439     struct cls_table *table;
440     struct cls_rule *best;
441
442     best = NULL;
443     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
444         struct cls_rule *rule = find_match(table, flow);
445         if (rule && (!best || rule->priority > best->priority)) {
446             best = rule;
447         }
448     }
449     return best;
450 }
451
452 /* Finds and returns a rule in 'cls' with exactly the same priority and
453  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
454  * contain an exact match.
455  *
456  * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
457  * treats exact-match rules as highest priority). */
458 struct cls_rule *
459 classifier_find_rule_exactly(const struct classifier *cls,
460                              const struct cls_rule *target)
461 {
462     struct cls_rule *head, *rule;
463     struct cls_table *table;
464
465     table = find_table(cls, &target->wc);
466     if (!table) {
467         return NULL;
468     }
469
470     head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
471     if (flow_wildcards_is_exact(&target->wc)) {
472         return head;
473     }
474     FOR_EACH_RULE_IN_LIST (rule, head) {
475         if (target->priority >= rule->priority) {
476             return target->priority == rule->priority ? rule : NULL;
477         }
478     }
479     return NULL;
480 }
481
482 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
483  * considered to overlap if both rules have the same priority and a packet
484  * could match both. */
485 bool
486 classifier_rule_overlaps(const struct classifier *cls,
487                          const struct cls_rule *target)
488 {
489     struct cls_table *table;
490
491     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
492         struct flow_wildcards wc;
493         struct cls_rule *head;
494
495         flow_wildcards_combine(&wc, &target->wc, &table->wc);
496         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
497             struct cls_rule *rule;
498
499             FOR_EACH_RULE_IN_LIST (rule, head) {
500                 if (rule->priority == target->priority
501                     && flow_equal_except(&target->flow, &rule->flow, &wc)) {
502                     return true;
503                 }
504             }
505         }
506     }
507
508     return false;
509 }
510 \f
511 /* Iteration. */
512
513 static bool
514 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
515 {
516     return (!target
517             || flow_equal_except(&rule->flow, &target->flow, &target->wc));
518 }
519
520 static struct cls_rule *
521 search_table(const struct cls_table *table, const struct cls_rule *target)
522 {
523     if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
524         struct cls_rule *rule;
525
526         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
527             if (rule_matches(rule, target)) {
528                 return rule;
529             }
530         }
531     }
532     return NULL;
533 }
534
535 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
536  * 'target' or are more specific than 'target'.  That is, a given 'rule'
537  * matches 'target' if, for every field:
538  *
539  *   - 'target' and 'rule' specify the same (non-wildcarded) value for the
540  *     field, or
541  *
542  *   - 'target' wildcards the field,
543  *
544  * but not if:
545  *
546  *   - 'target' and 'rule' specify different values for the field, or
547  *
548  *   - 'target' specifies a value for the field but 'rule' wildcards it.
549  *
550  * Equivalently, the truth table for whether a field matches is:
551  *
552  *                                     rule
553  *
554  *                             wildcard    exact
555  *                            +---------+---------+
556  *                   t   wild |   yes   |   yes   |
557  *                   a   card |         |         |
558  *                   r        +---------+---------+
559  *                   g  exact |    no   |if values|
560  *                   e        |         |are equal|
561  *                   t        +---------+---------+
562  *
563  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
564  * commands and by OpenFlow 1.0 aggregate and flow stats.
565  *
566  * Ignores target->priority.
567  *
568  * 'target' may be NULL to iterate over every rule in 'cls'. */
569 void
570 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
571                 const struct cls_rule *target)
572 {
573     cursor->cls = cls;
574     cursor->target = target;
575 }
576
577 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
578  * pointer if there are no matches. */
579 struct cls_rule *
580 cls_cursor_first(struct cls_cursor *cursor)
581 {
582     struct cls_table *table;
583
584     for (table = classifier_first_table(cursor->cls); table;
585          table = classifier_next_table(cursor->cls, table)) {
586         struct cls_rule *rule = search_table(table, cursor->target);
587         if (rule) {
588             cursor->table = table;
589             return rule;
590         }
591     }
592
593     return NULL;
594 }
595
596 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
597  * pointer if there are no more matches. */
598 struct cls_rule *
599 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
600 {
601     const struct cls_table *table;
602     struct cls_rule *next;
603
604     next = next_rule_in_list__(rule);
605     if (next->priority < rule->priority) {
606         return next;
607     }
608
609     /* 'next' is the head of the list, that is, the rule that is included in
610      * the table's hmap.  (This is important when the classifier contains rules
611      * that differ only in priority.) */
612     rule = next;
613     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
614         if (rule_matches(rule, cursor->target)) {
615             return rule;
616         }
617     }
618
619     for (table = classifier_next_table(cursor->cls, cursor->table); table;
620          table = classifier_next_table(cursor->cls, table)) {
621         rule = search_table(table, cursor->target);
622         if (rule) {
623             cursor->table = table;
624             return rule;
625         }
626     }
627
628     return NULL;
629 }
630 \f
631 static struct cls_table *
632 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
633 {
634     struct cls_table *table;
635
636     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
637                              &cls->tables) {
638         if (flow_wildcards_equal(wc, &table->wc)) {
639             return table;
640         }
641     }
642     return NULL;
643 }
644
645 static struct cls_table *
646 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
647 {
648     struct cls_table *table;
649
650     table = xzalloc(sizeof *table);
651     hmap_init(&table->rules);
652     table->wc = *wc;
653     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
654
655     return table;
656 }
657
658 static struct cls_table *
659 classifier_first_table(const struct classifier *cls)
660 {
661     return cls_table_from_hmap_node(hmap_first(&cls->tables));
662 }
663
664 static struct cls_table *
665 classifier_next_table(const struct classifier *cls,
666                       const struct cls_table *table)
667 {
668     return cls_table_from_hmap_node(hmap_next(&cls->tables,
669                                               &table->hmap_node));
670 }
671
672 static void
673 destroy_table(struct classifier *cls, struct cls_table *table)
674 {
675     hmap_remove(&cls->tables, &table->hmap_node);
676     hmap_destroy(&table->rules);
677     free(table);
678 }
679
680 static struct cls_rule *
681 find_match(const struct cls_table *table, const struct flow *flow)
682 {
683     struct cls_rule *rule;
684     struct flow f;
685
686     f = *flow;
687     zero_wildcards(&f, &table->wc);
688     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
689                              &table->rules) {
690         if (flow_equal(&f, &rule->flow)) {
691             return rule;
692         }
693     }
694     return NULL;
695 }
696
697 static struct cls_rule *
698 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
699 {
700     struct cls_rule *head;
701
702     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
703         if (flow_equal(&head->flow, flow)) {
704             return head;
705         }
706     }
707     return NULL;
708 }
709
710 static struct cls_rule *
711 insert_rule(struct cls_table *table, struct cls_rule *new)
712 {
713     struct cls_rule *head;
714
715     new->hmap_node.hash = flow_hash(&new->flow, 0);
716
717     head = find_equal(table, &new->flow, new->hmap_node.hash);
718     if (!head) {
719         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
720         list_init(&new->list);
721         return NULL;
722     } else {
723         /* Scan the list for the insertion point that will keep the list in
724          * order of decreasing priority. */
725         struct cls_rule *rule;
726         FOR_EACH_RULE_IN_LIST (rule, head) {
727             if (new->priority >= rule->priority) {
728                 if (rule == head) {
729                     /* 'new' is the new highest-priority flow in the list. */
730                     hmap_replace(&table->rules,
731                                  &rule->hmap_node, &new->hmap_node);
732                 }
733
734                 if (new->priority == rule->priority) {
735                     list_replace(&new->list, &rule->list);
736                     return rule;
737                 } else {
738                     list_insert(&rule->list, &new->list);
739                     return NULL;
740                 }
741             }
742         }
743
744         /* Insert 'new' at the end of the list. */
745         list_push_back(&head->list, &new->list);
746         return NULL;
747     }
748 }
749
750 static struct cls_rule *
751 next_rule_in_list__(struct cls_rule *rule)
752 {
753     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
754     return next;
755 }
756
757 static struct cls_rule *
758 next_rule_in_list(struct cls_rule *rule)
759 {
760     struct cls_rule *next = next_rule_in_list__(rule);
761     return next->priority < rule->priority ? next : NULL;
762 }
763
764 static bool
765 flow_equal_except(const struct flow *a, const struct flow *b,
766                   const struct flow_wildcards *wildcards)
767 {
768     const flow_wildcards_t wc = wildcards->wildcards;
769     int i;
770
771     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
772
773     for (i = 0; i < FLOW_N_REGS; i++) {
774         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
775             return false;
776         }
777     }
778
779     return ((wc & FWW_TUN_ID || a->tun_id == b->tun_id)
780             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
781             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
782             && (wc & FWW_IN_PORT || a->in_port == b->in_port)
783             && (wc & FWW_DL_VLAN || a->dl_vlan == b->dl_vlan)
784             && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
785             && (wc & FWW_TP_SRC || a->tp_src == b->tp_src)
786             && (wc & FWW_TP_DST || a->tp_dst == b->tp_dst)
787             && (wc & FWW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
788             && (wc & FWW_DL_DST
789                 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
790                     && a->dl_dst[1] == b->dl_dst[1]
791                     && a->dl_dst[2] == b->dl_dst[2]
792                     && a->dl_dst[3] == b->dl_dst[3]
793                     && a->dl_dst[4] == b->dl_dst[4]
794                     && a->dl_dst[5] == b->dl_dst[5]))
795             && (wc & FWW_ETH_MCAST
796                 || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
797             && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
798             && (wc & FWW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
799             && (wc & FWW_NW_TOS || a->nw_tos == b->nw_tos));
800 }
801
802 static void
803 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
804 {
805     const flow_wildcards_t wc = wildcards->wildcards;
806     int i;
807
808     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
809
810     for (i = 0; i < FLOW_N_REGS; i++) {
811         flow->regs[i] &= wildcards->reg_masks[i];
812     }
813     if (wc & FWW_TUN_ID) {
814         flow->tun_id = 0;
815     }
816     flow->nw_src &= wildcards->nw_src_mask;
817     flow->nw_dst &= wildcards->nw_dst_mask;
818     if (wc & FWW_IN_PORT) {
819         flow->in_port = 0;
820     }
821     if (wc & FWW_DL_VLAN) {
822         flow->dl_vlan = 0;
823     }
824     if (wc & FWW_DL_TYPE) {
825         flow->dl_type = 0;
826     }
827     if (wc & FWW_TP_SRC) {
828         flow->tp_src = 0;
829     }
830     if (wc & FWW_TP_DST) {
831         flow->tp_dst = 0;
832     }
833     if (wc & FWW_DL_SRC) {
834         memset(flow->dl_src, 0, sizeof flow->dl_src);
835     }
836     if (wc & FWW_DL_DST) {
837         flow->dl_dst[0] &= 0x01;
838         memset(&flow->dl_dst[1], 0, 5);
839     }
840     if (wc & FWW_ETH_MCAST) {
841         flow->dl_dst[0] &= 0xfe;
842     }
843     if (wc & FWW_NW_PROTO) {
844         flow->nw_proto = 0;
845     }
846     if (wc & FWW_DL_VLAN_PCP) {
847         flow->dl_vlan_pcp = 0;
848     }
849     if (wc & FWW_NW_TOS) {
850         flow->nw_tos = 0;
851     }
852 }