classifier: Drop CLS_INC_* enumerations and related 'include' parameters.
[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 static struct cls_rule *
64 cls_rule_from_hmap_node(const struct hmap_node *node)
65 {
66     return node ? CONTAINER_OF(node, struct cls_rule, hmap_node) : NULL;
67 }
68
69 /* Returns the cls_table within 'cls' that has no wildcards, or NULL if there
70  * is none.  */
71 struct cls_table *
72 classifier_exact_table(const struct classifier *cls)
73 {
74     struct flow_wildcards exact_wc;
75     flow_wildcards_init_exact(&exact_wc);
76     return find_table(cls, &exact_wc);
77 }
78
79 /* Returns the first rule in 'table', or a null pointer if 'table' is NULL. */
80 struct cls_rule *
81 cls_table_first_rule(const struct cls_table *table)
82 {
83     return table ? cls_rule_from_hmap_node(hmap_first(&table->rules)) : NULL;
84 }
85
86 /* Returns the next rule in 'table' following 'rule', or a null pointer if
87  * 'rule' is the last rule in 'table'. */
88 struct cls_rule *
89 cls_table_next_rule(const struct cls_table *table, const struct cls_rule *rule)
90 {
91     struct cls_rule *next
92         = CONTAINER_OF(rule->list.next, struct cls_rule, list);
93
94     return (next->priority < rule->priority
95             ? next
96             : cls_rule_from_hmap_node(hmap_next(&table->rules,
97                                                 &next->hmap_node)));
98 }
99
100 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
101  * 'wildcards' and 'priority'. */
102 void
103 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
104               unsigned int priority, struct cls_rule *rule)
105 {
106     rule->flow = *flow;
107     rule->wc = *wildcards;
108     rule->priority = priority;
109     cls_rule_zero_wildcarded_fields(rule);
110 }
111
112 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
113  * given 'priority'.  (For OpenFlow 1.0, exact-match rule are always highest
114  * priority, so 'priority' should be at least 65535.) */
115 void
116 cls_rule_init_exact(const struct flow *flow,
117                     unsigned int priority, struct cls_rule *rule)
118 {
119     rule->flow = *flow;
120     flow_wildcards_init_exact(&rule->wc);
121     rule->priority = priority;
122 }
123
124 /* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
125  * into a cls_rule in 'rule', with the given 'priority'.  'cookie' is used
126  * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
127 void
128 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
129                     int flow_format, uint64_t cookie,
130                     struct cls_rule *rule)
131 {
132     flow_from_match(match, flow_format, cookie, &rule->flow, &rule->wc);
133     rule->priority = !rule->wc.wildcards ? UINT16_MAX : priority;
134     cls_rule_zero_wildcarded_fields(rule);
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 | FWW_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 | FWW_ETH_MCAST);
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 struct cls_rule *
482 classifier_lookup(const struct classifier *cls, const struct flow *flow)
483 {
484     struct cls_table *table;
485     struct cls_rule *best;
486
487     best = NULL;
488     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
489         struct cls_rule *rule = find_match(table, flow);
490         if (rule && (!best || rule->priority > best->priority)) {
491             best = rule;
492         }
493     }
494     return best;
495 }
496
497 /* Finds and returns a rule in 'cls' with exactly the same priority and
498  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
499  * contain an exact match.
500  *
501  * Priority is ignored for exact-match rules (because OpenFlow 1.0 always
502  * treats exact-match rules as highest priority). */
503 struct cls_rule *
504 classifier_find_rule_exactly(const struct classifier *cls,
505                              const struct cls_rule *target)
506 {
507     struct cls_rule *head, *rule;
508     struct cls_table *table;
509
510     table = find_table(cls, &target->wc);
511     if (!table) {
512         return NULL;
513     }
514
515     head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
516     if (!target->wc.wildcards) {
517         return head;
518     }
519     FOR_EACH_RULE_IN_LIST (rule, head) {
520         if (target->priority >= rule->priority) {
521             return target->priority == rule->priority ? rule : NULL;
522         }
523     }
524     return NULL;
525 }
526
527 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
528  * considered to overlap if both rules have the same priority and a packet
529  * could match both. */
530 bool
531 classifier_rule_overlaps(const struct classifier *cls,
532                          const struct cls_rule *target)
533 {
534     struct cls_table *table;
535
536     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
537         struct flow_wildcards wc;
538         struct cls_rule *head;
539
540         flow_wildcards_combine(&wc, &target->wc, &table->wc);
541         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
542             struct cls_rule *rule;
543
544             FOR_EACH_RULE_IN_LIST (rule, head) {
545                 if (rule->priority == target->priority
546                     && flow_equal_except(&target->flow, &rule->flow, &wc)) {
547                     return true;
548                 }
549             }
550         }
551     }
552
553     return false;
554 }
555
556 /* Searches 'cls' for rules that exactly match 'target' or are more specific
557  * than 'target'.  That is, a given 'rule' matches 'target' if, for every
558  * field:
559  *
560  *   - 'target' and 'rule' specify the same (non-wildcarded) value for the
561  *     field, or
562  *
563  *   - 'target' wildcards the field,
564  *
565  * but not if:
566  *
567  *   - 'target' and 'rule' specify different values for the field, or
568  *
569  *   - 'target' specifies a value for the field but 'rule' wildcards it.
570  *
571  * Equivalently, the truth table for whether a field matches is:
572  *
573  *                                     rule
574  *
575  *                             wildcard    exact
576  *                            +---------+---------+
577  *                   t   wild |   yes   |   yes   |
578  *                   a   card |         |         |
579  *                   r        +---------+---------+
580  *                   g  exact |    no   |if values|
581  *                   e        |         |are equal|
582  *                   t        +---------+---------+
583  *
584  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
585  * commands and by OpenFlow 1.0 aggregate and flow stats.
586  *
587  * Ignores target->priority.
588  *
589  * 'callback' is allowed to delete the rule that is passed as its argument, but
590  * it must not delete (or move) any other rules in 'cls' that have the same
591  * wildcards as the argument rule. */
592 void
593 classifier_for_each_match(const struct classifier *cls_,
594                           const struct cls_rule *target,
595                           cls_cb_func *callback, void *aux)
596 {
597     struct classifier *cls = (struct classifier *) cls_;
598     struct cls_table *table, *next_table;
599
600     for (table = classifier_first_table(cls); table; table = next_table) {
601         if (!flow_wildcards_has_extra(&table->wc, &target->wc)) {
602             /* We have eliminated the "no" case in the truth table above.  Two
603              * of the three remaining cases are trivial.  We only need to check
604              * the fourth case, where both 'rule' and 'target' require an exact
605              * match. */
606             struct cls_rule *head, *next_head;
607
608             table->n_refs++;
609             HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
610                 if (flow_equal_except(&head->flow, &target->flow,
611                                       &target->wc)) {
612                     struct cls_rule *rule, *next_rule;
613
614                     FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
615                         callback(rule, aux);
616                     }
617                 }
618             }
619             next_table = classifier_next_table(cls, table);
620             if (!--table->n_refs && !table->n_table_rules) {
621                 destroy_table(cls, table);
622             }
623         } else {
624             next_table = classifier_next_table(cls, table);
625         }
626     }
627 }
628
629 /* 'callback' is allowed to delete the rule that is passed as its argument, but
630  * it must not delete (or move) any other rules in 'cls' that have the same
631  * wildcards as the argument rule. */
632 void
633 classifier_for_each(const struct classifier *cls_,
634                     cls_cb_func *callback, void *aux)
635 {
636     struct classifier *cls = (struct classifier *) cls_;
637     struct cls_table *table, *next_table;
638
639     for (table = classifier_first_table(cls); table; table = next_table) {
640         struct cls_rule *head, *next_head;
641
642         table->n_refs++;
643         HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
644             struct cls_rule *rule, *next_rule;
645
646             FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
647                 callback(rule, aux);
648             }
649         }
650         next_table = classifier_next_table(cls, table);
651         if (!--table->n_refs && !table->n_table_rules) {
652             destroy_table(cls, table);
653         }
654     }
655 }
656 \f
657 static struct cls_table *
658 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
659 {
660     struct cls_table *table;
661
662     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc),
663                              &cls->tables) {
664         if (flow_wildcards_equal(wc, &table->wc)) {
665             return table;
666         }
667     }
668     return NULL;
669 }
670
671 static struct cls_table *
672 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
673 {
674     struct cls_table *table;
675
676     table = xzalloc(sizeof *table);
677     hmap_init(&table->rules);
678     table->wc = *wc;
679     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc));
680
681     return table;
682 }
683
684 static struct cls_table *
685 classifier_first_table(const struct classifier *cls)
686 {
687     return cls_table_from_hmap_node(hmap_first(&cls->tables));
688 }
689
690 static struct cls_table *
691 classifier_next_table(const struct classifier *cls,
692                       const struct cls_table *table)
693 {
694     return cls_table_from_hmap_node(hmap_next(&cls->tables,
695                                               &table->hmap_node));
696 }
697
698 static void
699 destroy_table(struct classifier *cls, struct cls_table *table)
700 {
701     hmap_remove(&cls->tables, &table->hmap_node);
702     hmap_destroy(&table->rules);
703     free(table);
704 }
705
706 static struct cls_rule *
707 find_match(const struct cls_table *table, const struct flow *flow)
708 {
709     struct cls_rule *rule;
710     struct flow f;
711
712     f = *flow;
713     zero_wildcards(&f, &table->wc);
714     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
715                              &table->rules) {
716         if (flow_equal(&f, &rule->flow)) {
717             return rule;
718         }
719     }
720     return NULL;
721 }
722
723 static struct cls_rule *
724 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
725 {
726     struct cls_rule *head;
727
728     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
729         if (flow_equal(&head->flow, flow)) {
730             return head;
731         }
732     }
733     return NULL;
734 }
735
736 static struct cls_rule *
737 insert_rule(struct cls_table *table, struct cls_rule *new)
738 {
739     struct cls_rule *head;
740
741     new->hmap_node.hash = flow_hash(&new->flow, 0);
742
743     head = find_equal(table, &new->flow, new->hmap_node.hash);
744     if (!head) {
745         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
746         list_init(&new->list);
747         return NULL;
748     } else {
749         /* Scan the list for the insertion point that will keep the list in
750          * order of decreasing priority. */
751         struct cls_rule *rule;
752         FOR_EACH_RULE_IN_LIST (rule, head) {
753             if (new->priority >= rule->priority) {
754                 if (rule == head) {
755                     /* 'new' is the new highest-priority flow in the list. */
756                     hmap_replace(&table->rules,
757                                  &rule->hmap_node, &new->hmap_node);
758                 }
759
760                 if (new->priority == rule->priority) {
761                     list_replace(&new->list, &rule->list);
762                     return rule;
763                 } else {
764                     list_insert(&rule->list, &new->list);
765                     return NULL;
766                 }
767             }
768         }
769
770         /* Insert 'new' at the end of the list. */
771         list_push_back(&head->list, &new->list);
772         return NULL;
773     }
774 }
775
776 static struct cls_rule *
777 next_rule_in_list(struct cls_rule *rule)
778 {
779     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
780     return next->priority < rule->priority ? next : NULL;
781 }
782
783 static bool
784 flow_equal_except(const struct flow *a, const struct flow *b,
785                   const struct flow_wildcards *wildcards)
786 {
787     const uint32_t wc = wildcards->wildcards;
788     int i;
789
790     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + FLOW_N_REGS * 4);
791
792     for (i = 0; i < FLOW_N_REGS; i++) {
793         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
794             return false;
795         }
796     }
797
798     return ((wc & NXFW_TUN_ID || a->tun_id == b->tun_id)
799             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
800             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
801             && (wc & OFPFW_IN_PORT || a->in_port == b->in_port)
802             && (wc & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
803             && (wc & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
804             && (wc & OFPFW_TP_SRC || a->tp_src == b->tp_src)
805             && (wc & OFPFW_TP_DST || a->tp_dst == b->tp_dst)
806             && (wc & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
807             && (wc & OFPFW_DL_DST
808                 || (!((a->dl_dst[0] ^ b->dl_dst[0]) & 0xfe)
809                     && a->dl_dst[1] == b->dl_dst[1]
810                     && a->dl_dst[2] == b->dl_dst[2]
811                     && a->dl_dst[3] == b->dl_dst[3]
812                     && a->dl_dst[4] == b->dl_dst[4]
813                     && a->dl_dst[5] == b->dl_dst[5]))
814             && (wc & FWW_ETH_MCAST || !((a->dl_dst[0] ^ b->dl_dst[0]) & 0x01))
815             && (wc & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
816             && (wc & OFPFW_DL_VLAN_PCP || a->dl_vlan_pcp == b->dl_vlan_pcp)
817             && (wc & OFPFW_NW_TOS || a->nw_tos == b->nw_tos));
818 }
819
820 static void
821 zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
822 {
823     const uint32_t wc = wildcards->wildcards;
824     int i;
825
826     BUILD_ASSERT_DECL(FLOW_SIG_SIZE == 37 + 4 * FLOW_N_REGS);
827
828     for (i = 0; i < FLOW_N_REGS; i++) {
829         flow->regs[i] &= wildcards->reg_masks[i];
830     }
831     if (wc & NXFW_TUN_ID) {
832         flow->tun_id = 0;
833     }
834     flow->nw_src &= wildcards->nw_src_mask;
835     flow->nw_dst &= wildcards->nw_dst_mask;
836     if (wc & OFPFW_IN_PORT) {
837         flow->in_port = 0;
838     }
839     if (wc & OFPFW_DL_VLAN) {
840         flow->dl_vlan = 0;
841     }
842     if (wc & OFPFW_DL_TYPE) {
843         flow->dl_type = 0;
844     }
845     if (wc & OFPFW_TP_SRC) {
846         flow->tp_src = 0;
847     }
848     if (wc & OFPFW_TP_DST) {
849         flow->tp_dst = 0;
850     }
851     if (wc & OFPFW_DL_SRC) {
852         memset(flow->dl_src, 0, sizeof flow->dl_src);
853     }
854     if (wc & OFPFW_DL_DST) {
855         flow->dl_dst[0] &= 0x01;
856         memset(&flow->dl_dst[1], 0, 5);
857     }
858     if (wc & FWW_ETH_MCAST) {
859         flow->dl_dst[0] &= 0xfe;
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 }