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