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