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