classifier: Add functions and macros for iteration, and use them in ofproto.
[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 /* Iteration. */
613
614 static bool
615 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
616 {
617     return (!target
618             || flow_equal_except(&rule->flow, &target->flow, &target->wc));
619 }
620
621 static struct cls_rule *
622 search_table(const struct cls_table *table, const struct cls_rule *target)
623 {
624     if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
625         struct cls_rule *rule;
626
627         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
628             if (rule_matches(rule, target)) {
629                 return rule;
630             }
631         }
632     }
633     return NULL;
634 }
635
636 /* Initializes 'cursor' for iterating through 'cls' rules that exactly match
637  * 'target' or are more specific than 'target'.  That is, a given 'rule'
638  * matches 'target' if, for every field:
639  *
640  *   - 'target' and 'rule' specify the same (non-wildcarded) value for the
641  *     field, or
642  *
643  *   - 'target' wildcards the field,
644  *
645  * but not if:
646  *
647  *   - 'target' and 'rule' specify different values for the field, or
648  *
649  *   - 'target' specifies a value for the field but 'rule' wildcards it.
650  *
651  * Equivalently, the truth table for whether a field matches is:
652  *
653  *                                     rule
654  *
655  *                             wildcard    exact
656  *                            +---------+---------+
657  *                   t   wild |   yes   |   yes   |
658  *                   a   card |         |         |
659  *                   r        +---------+---------+
660  *                   g  exact |    no   |if values|
661  *                   e        |         |are equal|
662  *                   t        +---------+---------+
663  *
664  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
665  * commands and by OpenFlow 1.0 aggregate and flow stats.
666  *
667  * Ignores target->priority.
668  *
669  * 'target' may be NULL to iterate over every rule in 'cls'. */
670 void
671 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
672                 const struct cls_rule *target)
673 {
674     cursor->cls = cls;
675     cursor->target = target;
676 }
677
678 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
679  * pointer if there are no matches. */
680 struct cls_rule *
681 cls_cursor_first(struct cls_cursor *cursor)
682 {
683     struct cls_table *table;
684
685     for (table = classifier_first_table(cursor->cls); table;
686          table = classifier_next_table(cursor->cls, table)) {
687         struct cls_rule *rule = search_table(table, cursor->target);
688         if (rule) {
689             cursor->table = table;
690             return rule;
691         }
692     }
693
694     return NULL;
695 }
696
697 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
698  * pointer if there are no more matches. */
699 struct cls_rule *
700 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
701 {
702     const struct cls_table *table;
703     struct cls_rule *next;
704
705     next = next_rule_in_list(rule);
706     if (next) {
707         return next;
708     }
709
710     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
711         if (rule_matches(rule, cursor->target)) {
712             return rule;
713         }
714     }
715
716     for (table = classifier_next_table(cursor->cls, cursor->table); table;
717          table = classifier_next_table(cursor->cls, table)) {
718         rule = search_table(table, cursor->target);
719         if (rule) {
720             cursor->table = table;
721             return rule;
722         }
723     }
724
725     return NULL;
726 }
727 \f
728 static struct cls_table *
729 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
730 {
731     struct cls_table *table;
732
733     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
734                              &cls->tables) {
735         if (flow_wildcards_equal(wc, &table->wc)) {
736             return table;
737         }
738     }
739     return NULL;
740 }
741
742 static struct cls_table *
743 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
744 {
745     struct cls_table *table;
746
747     table = xzalloc(sizeof *table);
748     hmap_init(&table->rules);
749     table->wc = *wc;
750     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
751
752     return table;
753 }
754
755 static struct cls_table *
756 classifier_first_table(const struct classifier *cls)
757 {
758     return cls_table_from_hmap_node(hmap_first(&cls->tables));
759 }
760
761 static struct cls_table *
762 classifier_next_table(const struct classifier *cls,
763                       const struct cls_table *table)
764 {
765     return cls_table_from_hmap_node(hmap_next(&cls->tables,
766                                               &table->hmap_node));
767 }
768
769 static void
770 destroy_table(struct classifier *cls, struct cls_table *table)
771 {
772     hmap_remove(&cls->tables, &table->hmap_node);
773     hmap_destroy(&table->rules);
774     free(table);
775 }
776
777 static struct cls_rule *
778 find_match(const struct cls_table *table, const struct flow *flow)
779 {
780     struct cls_rule *rule;
781     struct flow f;
782
783     f = *flow;
784     zero_wildcards(&f, &table->wc);
785     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
786                              &table->rules) {
787         if (flow_equal(&f, &rule->flow)) {
788             return rule;
789         }
790     }
791     return NULL;
792 }
793
794 static struct cls_rule *
795 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
796 {
797     struct cls_rule *head;
798
799     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
800         if (flow_equal(&head->flow, flow)) {
801             return head;
802         }
803     }
804     return NULL;
805 }
806
807 static struct cls_rule *
808 insert_rule(struct cls_table *table, struct cls_rule *new)
809 {
810     struct cls_rule *head;
811
812     new->hmap_node.hash = flow_hash(&new->flow, 0);
813
814     head = find_equal(table, &new->flow, new->hmap_node.hash);
815     if (!head) {
816         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
817         list_init(&new->list);
818         return NULL;
819     } else {
820         /* Scan the list for the insertion point that will keep the list in
821          * order of decreasing priority. */
822         struct cls_rule *rule;
823         FOR_EACH_RULE_IN_LIST (rule, head) {
824             if (new->priority >= rule->priority) {
825                 if (rule == head) {
826                     /* 'new' is the new highest-priority flow in the list. */
827                     hmap_replace(&table->rules,
828                                  &rule->hmap_node, &new->hmap_node);
829                 }
830
831                 if (new->priority == rule->priority) {
832                     list_replace(&new->list, &rule->list);
833                     return rule;
834                 } else {
835                     list_insert(&rule->list, &new->list);
836                     return NULL;
837                 }
838             }
839         }
840
841         /* Insert 'new' at the end of the list. */
842         list_push_back(&head->list, &new->list);
843         return NULL;
844     }
845 }
846
847 static struct cls_rule *
848 next_rule_in_list(struct cls_rule *rule)
849 {
850     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
851     return next->priority < rule->priority ? next : NULL;
852 }
853
854 static bool
855 flow_equal_except(const struct flow *a, const struct flow *b,
856                   const struct flow_wildcards *wildcards)
857 {
858     const uint32_t wc = wildcards->wildcards;
859     int i;
860
861     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
862
863     for (i = 0; i < FLOW_N_REGS; i++) {
864         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
865             return false;
866         }
867     }
868
869     return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
870             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
871             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
872             && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
873             && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
874             && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
875             && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
876             && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
877             && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
878             && (wc & OFPFW_DL_DST
879                 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
880                     && a->dl_dst[1] == b->dl_dst[1]
881                     && a->dl_dst[2] == b->dl_dst[2]
882                     && a->dl_dst[3] == b->dl_dst[3]
883                     && a->dl_dst[4] == b->dl_dst[4]
884                     && a->dl_dst[5] == b->dl_dst[5]))
885             && (wc & FWW_ETH_MCAST || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
886             && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
887             && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
888             && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
889 }
890
891 static void
892 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
893 {
894     const uint32_t wc = wildcards->wildcards;
895     int i;
896
897     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
898
899     for (i = 0; i < FLOW_N_REGS; i++) {
900         flow->regs[i] &= wildcards->reg_masks[i];
901     }
902     if (wc & NXFW_TUN_ID) {
903         flow->tun_id = 0;
904     }
905     flow->nw_src &= wildcards->nw_src_mask;
906     flow->nw_dst &= wildcards->nw_dst_mask;
907     if (wc & OFPFW_IN_PORT) {
908         flow->in_port = 0;
909     }
910     if (wc & OFPFW_DL_VLAN) {
911         flow->dl_vlan = 0;
912     }
913     if (wc & OFPFW_DL_TYPE) {
914         flow->dl_type = 0;
915     }
916     if (wc & OFPFW_TP_SRC) {
917         flow->tp_src = 0;
918     }
919     if (wc & OFPFW_TP_DST) {
920         flow->tp_dst = 0;
921     }
922     if (wc & OFPFW_DL_SRC) {
923         memset(flow->dl_src, 0, sizeof flow->dl_src);
924     }
925     if (wc & OFPFW_DL_DST) {
926         flow->dl_dst[0] &= 0x01;
927         memset(&flow->dl_dst[1], 0, 5);
928     }
929     if (wc & FWW_ETH_MCAST) {
930         flow->dl_dst[0] &= 0xfe;
931     }
932     if (wc & OFPFW_NW_PROTO) {
933         flow->nw_proto = 0;
934     }
935     if (wc & OFPFW_DL_VLAN_PCP) {
936         flow->dl_vlan_pcp = 0;
937     }
938     if (wc & OFPFW_NW_TOS) {
939         flow->nw_tos = 0;
940     }
941 }