classifier: Delete dead code specialized for the exact table.
[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 "dynamic-string.h"
23 #include "flow.h"
24 #include "hash.h"
25 #include "packets.h"
26
27 static struct cls_table *find_table(const struct classifier *,
28                                     const struct flow_wildcards *);
29 static struct cls_table *insert_table(struct classifier *,
30                                       const struct flow_wildcards *);
31
32 static struct cls_table *classifier_first_table(const struct classifier *);
33 static struct cls_table *classifier_next_table(const struct classifier *,
34                                                const struct cls_table *);
35 static void destroy_table(struct classifier *, struct cls_table *);
36
37 static struct cls_rule *find_match(const struct cls_table *,
38                                    const struct flow *);
39 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
40                                    uint32_t hash);
41 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
42
43 static bool flow_equal_except(const struct flow *, const struct flow *,
44                                 const struct flow_wildcards *);
45 static void zero_wildcards(struct flow *, const struct flow_wildcards *);
46
47 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
48 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD)                               \
49     for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
50 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD)                    \
51     for ((RULE) = (HEAD);                                               \
52          (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true);    \
53          (RULE) = (NEXT))
54
55 static struct cls_rule *next_rule_in_list(struct cls_rule *);
56
57 static struct cls_table *
58 cls_table_from_hmap_node(const struct hmap_node *node)
59 {
60     return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
61 }
62
63 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
64  * 'wildcards' and 'priority'. */
65 void
66 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
67               unsigned int priority, struct cls_rule *rule)
68 {
69     rule->flow = *flow;
70     rule->wc = *wildcards;
71     rule->priority = priority;
72     cls_rule_zero_wildcarded_fields(rule);
73 }
74
75 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
76  * given 'priority'.  (For OpenFlow 1.0, exact-match rule are always highest
77  * priority, so 'priority' should be at least 65535.) */
78 void
79 cls_rule_init_exact(const struct flow *flow,
80                     unsigned int priority, struct cls_rule *rule)
81 {
82     rule->flow = *flow;
83     flow_wildcards_init_exact(&rule->wc);
84     rule->priority = priority;
85 }
86
87 /* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
88  * into a cls_rule in 'rule', with the given 'priority'.  'cookie' is used
89  * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
90 void
91 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
92                     int flow_format, uint64_t cookie,
93                     struct cls_rule *rule)
94 {
95     flow_from_match(match, flow_format, cookie, &rule->flow, &rule->wc);
96     rule->priority = !rule->wc.wildcards ? UINT16_MAX : priority;
97     cls_rule_zero_wildcarded_fields(rule);
98 }
99
100 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
101  * priority 'priority'. */
102 void
103 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
104 {
105     memset(&rule->flow, 0, sizeof rule->flow);
106     flow_wildcards_init(&rule->wc, OVSFW_ALL | FWW_ALL);
107     rule->priority = priority;
108 }
109
110 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
111  * field in 'flow' to all-0-bits.  It is important to maintain this invariant
112  * in a clr_rule that might be inserted into a classifier.
113  *
114  * It is never necessary to call this function directly for a cls_rule that is
115  * initialized or modified only by cls_rule_*() functions.  It is useful to
116  * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
117  */
118 void
119 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
120 {
121     zero_wildcards(&rule->flow, &rule->wc);
122 }
123
124 void
125 cls_rule_set_in_port(struct cls_rule *rule, uint16_t odp_port)
126 {
127     rule->wc.wildcards &= ~OFPFW_IN_PORT;
128     rule->flow.in_port = odp_port;
129 }
130
131 void
132 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
133 {
134     rule->wc.wildcards &= ~OFPFW_DL_TYPE;
135     rule->flow.dl_type = dl_type;
136 }
137
138 void
139 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
140 {
141     rule->wc.wildcards &= ~OFPFW_DL_SRC;
142     memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
143 }
144
145 void
146 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
147 {
148     rule->wc.wildcards &= ~(OFPFW_DL_DST | FWW_ETH_MCAST);
149     memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
150 }
151
152 bool
153 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
154 {
155     return cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
156 }
157
158 bool
159 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
160 {
161     switch (ntohs(mask)) {
162     case 0xffff:
163         if (tci == htons(0)) {
164             /* Match only packets that have no 802.1Q header. */
165             rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
166             rule->flow.dl_vlan = htons(OFP_VLAN_NONE);
167             rule->flow.dl_vlan_pcp = 0;
168             return true;
169         } else if (tci & htons(VLAN_CFI)) {
170             /* Match only packets that have a specific 802.1Q VID and PCP. */
171             rule->wc.wildcards &= ~(OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP);
172             rule->flow.dl_vlan = htons(vlan_tci_to_vid(tci));
173             rule->flow.dl_vlan_pcp = vlan_tci_to_pcp(tci);
174             return true;
175         } else {
176             /* Impossible. */
177             return false;
178         }
179
180     case 0x1fff:
181         if (!(tci & htons(VLAN_CFI))) {
182             return false;
183         } else {
184             /* Match only packets that have a specific 802.1Q VID. */
185             cls_rule_set_dl_vlan(rule, tci & htons(VLAN_VID_MASK));
186             rule->wc.wildcards |= OFPFW_DL_VLAN_PCP;
187             rule->flow.dl_vlan_pcp = 0;
188             return true;
189         }
190
191     case 0xf000:
192         if (!(tci & htons(VLAN_CFI))) {
193             return false;
194         } else {
195             /* Match only packets that have a specific 802.1Q PCP. */
196             cls_rule_set_dl_vlan_pcp(rule, vlan_tci_to_pcp(tci));
197             rule->wc.wildcards |= OFPFW_DL_VLAN;
198             rule->flow.dl_vlan = 0;
199             return true;
200         }
201
202     case 0x0000:
203         /* Match anything. */
204         rule->wc.wildcards |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
205         rule->flow.dl_vlan = htons(0);
206         rule->flow.dl_vlan_pcp = 0;
207         return true;
208
209     default:
210         return false;
211     }
212 }
213
214 void
215 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
216 {
217     if (dl_vlan != htons(OFP_VLAN_NONE)) {
218         dl_vlan &= htons(VLAN_VID_MASK);
219     }
220
221     rule->wc.wildcards &= ~OFPFW_DL_VLAN;
222     rule->flow.dl_vlan = dl_vlan;
223 }
224
225 void
226 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
227 {
228     rule->wc.wildcards &= ~OFPFW_DL_VLAN_PCP;
229     rule->flow.dl_vlan_pcp = dl_vlan_pcp & 0x07;
230 }
231
232 void
233 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
234 {
235     rule->wc.wildcards &= ~OFPFW_TP_SRC;
236     rule->flow.tp_src = tp_src;
237 }
238
239 void
240 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
241 {
242     rule->wc.wildcards &= ~OFPFW_TP_DST;
243     rule->flow.tp_dst = tp_dst;
244 }
245
246 void
247 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
248 {
249     rule->wc.wildcards &= ~OFPFW_NW_PROTO;
250     rule->flow.nw_proto = nw_proto;
251 }
252
253 void
254 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
255 {
256     cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
257 }
258
259 bool
260 cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
261 {
262     if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
263         rule->flow.nw_src = ip & mask;
264         return true;
265     } else {
266         return false;
267     }
268 }
269
270 void
271 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
272 {
273     cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
274 }
275
276 bool
277 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
278 {
279     if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
280         rule->flow.nw_dst = ip & mask;
281         return true;
282     } else {
283         return false;
284     }
285 }
286
287 void
288 cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
289 {
290     rule->wc.wildcards &= ~OFPFW_NW_TOS;
291     rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
292 }
293
294 void
295 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
296 {
297     rule->wc.wildcards &= ~OFPFW_ICMP_TYPE;
298     rule->flow.icmp_type = htons(icmp_type);
299
300 }
301
302 void
303 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
304 {
305     rule->wc.wildcards &= ~OFPFW_ICMP_CODE;
306     rule->flow.icmp_code = htons(icmp_code);
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 && !table->n_refs) {
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 (!target->wc.wildcards) {
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
511 /* Searches 'cls' for rules that exactly match 'target' or are more specific
512  * than 'target'.  That is, a given 'rule' matches 'target' if, for every
513  * field:
514  *
515  *   - 'target' and 'rule' specify the same (non-wildcarded) value for the
516  *     field, or
517  *
518  *   - 'target' wildcards the field,
519  *
520  * but not if:
521  *
522  *   - 'target' and 'rule' specify different values for the field, or
523  *
524  *   - 'target' specifies a value for the field but 'rule' wildcards it.
525  *
526  * Equivalently, the truth table for whether a field matches is:
527  *
528  *                                     rule
529  *
530  *                             wildcard    exact
531  *                            +---------+---------+
532  *                   t   wild |   yes   |   yes   |
533  *                   a   card |         |         |
534  *                   r        +---------+---------+
535  *                   g  exact |    no   |if values|
536  *                   e        |         |are equal|
537  *                   t        +---------+---------+
538  *
539  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
540  * commands and by OpenFlow 1.0 aggregate and flow stats.
541  *
542  * Ignores target->priority.
543  *
544  * 'callback' is allowed to delete the rule that is passed as its argument, but
545  * it must not delete (or move) any other rules in 'cls' that have the same
546  * wildcards as the argument rule. */
547 void
548 classifier_for_each_match(const struct classifier *cls_,
549                           const struct cls_rule *target,
550                           cls_cb_func *callback, void *aux)
551 {
552     struct classifier *cls = (struct classifier *) cls_;
553     struct cls_table *table, *next_table;
554
555     for (table = classifier_first_table(cls); table; table = next_table) {
556         if (!flow_wildcards_has_extra(&table->wc, &target->wc)) {
557             /* We have eliminated the "no" case in the truth table above.  Two
558              * of the three remaining cases are trivial.  We only need to check
559              * the fourth case, where both 'rule' and 'target' require an exact
560              * match. */
561             struct cls_rule *head, *next_head;
562
563             table->n_refs++;
564             HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
565                 if (flow_equal_except(&head->flow, &target->flow,
566                                       &target->wc)) {
567                     struct cls_rule *rule, *next_rule;
568
569                     FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
570                         callback(rule, aux);
571                     }
572                 }
573             }
574             next_table = classifier_next_table(cls, table);
575             if (!--table->n_refs && !table->n_table_rules) {
576                 destroy_table(cls, table);
577             }
578         } else {
579             next_table = classifier_next_table(cls, table);
580         }
581     }
582 }
583
584 /* 'callback' is allowed to delete the rule that is passed as its argument, but
585  * it must not delete (or move) any other rules in 'cls' that have the same
586  * wildcards as the argument rule. */
587 void
588 classifier_for_each(const struct classifier *cls_,
589                     cls_cb_func *callback, void *aux)
590 {
591     struct classifier *cls = (struct classifier *) cls_;
592     struct cls_table *table, *next_table;
593
594     for (table = classifier_first_table(cls); table; table = next_table) {
595         struct cls_rule *head, *next_head;
596
597         table->n_refs++;
598         HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
599             struct cls_rule *rule, *next_rule;
600
601             FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
602                 callback(rule, aux);
603             }
604         }
605         next_table = classifier_next_table(cls, table);
606         if (!--table->n_refs && !table->n_table_rules) {
607             destroy_table(cls, table);
608         }
609     }
610 }
611 \f
612 static struct cls_table *
613 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
614 {
615     struct cls_table *table;
616
617     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
618                              &cls->tables) {
619         if (flow_wildcards_equal(wc, &table->wc)) {
620             return table;
621         }
622     }
623     return NULL;
624 }
625
626 static struct cls_table *
627 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
628 {
629     struct cls_table *table;
630
631     table = xzalloc(sizeof *table);
632     hmap_init(&table->rules);
633     table->wc = *wc;
634     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
635
636     return table;
637 }
638
639 static struct cls_table *
640 classifier_first_table(const struct classifier *cls)
641 {
642     return cls_table_from_hmap_node(hmap_first(&cls->tables));
643 }
644
645 static struct cls_table *
646 classifier_next_table(const struct classifier *cls,
647                       const struct cls_table *table)
648 {
649     return cls_table_from_hmap_node(hmap_next(&cls->tables,
650                                               &table->hmap_node));
651 }
652
653 static void
654 destroy_table(struct classifier *cls, struct cls_table *table)
655 {
656     hmap_remove(&cls->tables, &table->hmap_node);
657     hmap_destroy(&table->rules);
658     free(table);
659 }
660
661 static struct cls_rule *
662 find_match(const struct cls_table *table, const struct flow *flow)
663 {
664     struct cls_rule *rule;
665     struct flow f;
666
667     f = *flow;
668     zero_wildcards(&f, &table->wc);
669     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
670                              &table->rules) {
671         if (flow_equal(&f, &rule->flow)) {
672             return rule;
673         }
674     }
675     return NULL;
676 }
677
678 static struct cls_rule *
679 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
680 {
681     struct cls_rule *head;
682
683     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
684         if (flow_equal(&head->flow, flow)) {
685             return head;
686         }
687     }
688     return NULL;
689 }
690
691 static struct cls_rule *
692 insert_rule(struct cls_table *table, struct cls_rule *new)
693 {
694     struct cls_rule *head;
695
696     new->hmap_node.hash = flow_hash(&new->flow, 0);
697
698     head = find_equal(table, &new->flow, new->hmap_node.hash);
699     if (!head) {
700         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
701         list_init(&new->list);
702         return NULL;
703     } else {
704         /* Scan the list for the insertion point that will keep the list in
705          * order of decreasing priority. */
706         struct cls_rule *rule;
707         FOR_EACH_RULE_IN_LIST (rule, head) {
708             if (new->priority >= rule->priority) {
709                 if (rule == head) {
710                     /* 'new' is the new highest-priority flow in the list. */
711                     hmap_replace(&table->rules,
712                                  &rule->hmap_node, &new->hmap_node);
713                 }
714
715                 if (new->priority == rule->priority) {
716                     list_replace(&new->list, &rule->list);
717                     return rule;
718                 } else {
719                     list_insert(&rule->list, &new->list);
720                     return NULL;
721                 }
722             }
723         }
724
725         /* Insert 'new' at the end of the list. */
726         list_push_back(&head->list, &new->list);
727         return NULL;
728     }
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->priority < rule->priority ? next : NULL;
736 }
737
738 static bool
739 flow_equal_except(const struct flow *a, const struct flow *b,
740                   const struct flow_wildcards *wildcards)
741 {
742     const uint32_t wc = wildcards->wildcards;
743     int i;
744
745     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
746
747     for (i = 0; i < FLOW_N_REGS; i++) {
748         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
749             return false;
750         }
751     }
752
753     return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
754             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
755             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
756             && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
757             && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
758             && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
759             && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
760             && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
761             && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
762             && (wc & OFPFW_DL_DST
763                 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
764                     && a->dl_dst[1] == b->dl_dst[1]
765                     && a->dl_dst[2] == b->dl_dst[2]
766                     && a->dl_dst[3] == b->dl_dst[3]
767                     && a->dl_dst[4] == b->dl_dst[4]
768                     && a->dl_dst[5] == b->dl_dst[5]))
769             && (wc & FWW_ETH_MCAST || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
770             && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
771             && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
772             && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
773 }
774
775 static void
776 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
777 {
778     const uint32_t wc = wildcards->wildcards;
779     int i;
780
781     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
782
783     for (i = 0; i < FLOW_N_REGS; i++) {
784         flow->regs[i] &= wildcards->reg_masks[i];
785     }
786     if (wc & NXFW_TUN_ID) {
787         flow->tun_id = 0;
788     }
789     flow->nw_src &= wildcards->nw_src_mask;
790     flow->nw_dst &= wildcards->nw_dst_mask;
791     if (wc & OFPFW_IN_PORT) {
792         flow->in_port = 0;
793     }
794     if (wc & OFPFW_DL_VLAN) {
795         flow->dl_vlan = 0;
796     }
797     if (wc & OFPFW_DL_TYPE) {
798         flow->dl_type = 0;
799     }
800     if (wc & OFPFW_TP_SRC) {
801         flow->tp_src = 0;
802     }
803     if (wc & OFPFW_TP_DST) {
804         flow->tp_dst = 0;
805     }
806     if (wc & OFPFW_DL_SRC) {
807         memset(flow->dl_src, 0, sizeof flow->dl_src);
808     }
809     if (wc & OFPFW_DL_DST) {
810         flow->dl_dst[0] &= 0x01;
811         memset(&flow->dl_dst[1], 0, 5);
812     }
813     if (wc & FWW_ETH_MCAST) {
814         flow->dl_dst[0] &= 0xfe;
815     }
816     if (wc & OFPFW_NW_PROTO) {
817         flow->nw_proto = 0;
818     }
819     if (wc & OFPFW_DL_VLAN_PCP) {
820         flow->dl_vlan_pcp = 0;
821     }
822     if (wc & OFPFW_NW_TOS) {
823         flow->nw_tos = 0;
824     }
825 }