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