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