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