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