wdp-xflow: Always report an up-to-date port list in wx_port_list().
[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
26 const struct cls_field cls_fields[CLS_N_FIELDS + 1] = {
27 #define CLS_FIELD(WILDCARDS, MEMBER, NAME)      \
28     { offsetof(flow_t, MEMBER),                 \
29       sizeof ((flow_t *)0)->MEMBER,             \
30       WILDCARDS,                                \
31       #NAME },
32     CLS_FIELDS
33 #undef CLS_FIELD
34     { sizeof(flow_t), 0, 0, "exact" },
35 };
36
37 static uint32_t hash_fields(const flow_t *, int table_idx);
38 static bool equal_fields(const flow_t *, const flow_t *, int table_idx);
39
40 static int table_idx_from_wildcards(uint32_t wildcards);
41 static struct cls_rule *table_insert(struct hmap *, struct cls_rule *);
42 static struct cls_rule *insert_exact_rule(struct classifier *,
43                                           struct cls_rule *);
44 static struct cls_bucket *find_bucket(struct hmap *, size_t hash,
45                                       const struct cls_rule *);
46 static struct cls_rule *search_table(const struct hmap *table, int field_idx,
47                                      const struct cls_rule *);
48 static struct cls_rule *search_exact_table(const struct classifier *,
49                                            size_t hash, const flow_t *);
50 static bool rules_match_1wild(const struct cls_rule *fixed,
51                               const struct cls_rule *wild, int field_idx);
52 static bool rules_match_2wild(const struct cls_rule *wild1,
53                               const struct cls_rule *wild2, int field_idx);
54
55 /* Converts the flow in 'flow' into a cls_rule in 'rule'. */
56 void
57 cls_rule_from_flow(const flow_t *flow, struct cls_rule *rule)
58 {
59     rule->flow = *flow;
60     if (!rule->flow.wildcards && rule->flow.priority < UINT16_MAX) {
61         rule->flow.priority = UINT16_MAX;
62     }
63     flow_wildcards_init(&rule->wc, flow->wildcards);
64     rule->table_idx = table_idx_from_wildcards(flow->wildcards);
65 }
66
67 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
68  * 'priority'.  If 'tun_id_from_cookie' is set then the upper 32 bits of
69  * 'cookie' are stored in the rule as the tunnel ID. */
70 void
71 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
72                     bool tun_id_from_cookie, uint64_t cookie,
73                     struct cls_rule *rule)
74 {
75     flow_from_match(match, rule->flow.wildcards ? priority : UINT16_MAX,
76                     tun_id_from_cookie, cookie, &rule->flow);
77     flow_wildcards_init(&rule->wc, rule->flow.wildcards);
78     rule->table_idx = table_idx_from_wildcards(rule->flow.wildcards);
79 }
80
81 /* Converts 'rule' to a string and returns the string.  The caller must free
82  * the string (with free()). */
83 char *
84 cls_rule_to_string(const struct cls_rule *rule)
85 {
86     struct ds s = DS_EMPTY_INITIALIZER;
87     ds_put_format(&s, "wildcards=%x priority=%u ",
88                   rule->flow.wildcards, rule->flow.priority);
89     flow_format(&s, &rule->flow);
90     return ds_cstr(&s);
91 }
92
93 /* Prints cls_rule 'rule', for debugging.
94  *
95  * (The output could be improved and expanded, but this was good enough to
96  * debug the classifier.) */
97 void
98 cls_rule_print(const struct cls_rule *rule)
99 {
100     printf("wildcards=%x priority=%u ",
101            rule->flow.wildcards, rule->flow.priority);
102     flow_print(stdout, &rule->flow);
103     putc('\n', stdout);
104 }
105
106 /* Adjusts pointers around 'old', which must be in classifier 'cls', to
107  * compensate for it having been moved in memory to 'new' (e.g. due to
108  * realloc()).
109  *
110  * This function cannot be realized in all possible flow classifier
111  * implementations, so we will probably have to change the interface if we
112  * change the implementation.  Shouldn't be a big deal though. */
113 void
114 cls_rule_moved(struct classifier *cls, struct cls_rule *old,
115                struct cls_rule *new)
116 {
117     if (old != new) {
118         if (new->flow.wildcards) {
119             list_moved(&new->node.list);
120         } else {
121             hmap_node_moved(&cls->exact_table,
122                             &old->node.hmap, &new->node.hmap);
123         }
124     }
125 }
126
127 /* Replaces 'old', which must be in classifier 'cls', by 'new' (e.g. due to
128  * realloc()); that is, after calling this function 'new' will be in 'cls' in
129  * place of 'old'.
130  *
131  * 'new' and 'old' must be exactly the same: wildcard the same fields, have the
132  * same fixed values for non-wildcarded fields, and have the same priority.
133  *
134  * The caller takes ownership of 'old' and is thus responsible for freeing it,
135  * etc., as necessary.
136  *
137  * This function cannot be realized in all possible flow classifier
138  * implementations, so we will probably have to change the interface if we
139  * change the implementation.  Shouldn't be a big deal though. */
140 void
141 cls_rule_replace(struct classifier *cls, const struct cls_rule *old,
142                  struct cls_rule *new)
143 {
144     assert(old != new);
145     assert(old->flow.wildcards == new->flow.wildcards);
146     assert(old->flow.priority == new->flow.priority);
147
148     if (new->flow.wildcards) {
149         list_replace(&new->node.list, &old->node.list);
150     } else {
151         hmap_replace(&cls->exact_table, &old->node.hmap, &new->node.hmap);
152     }
153 }
154 \f
155 /* Initializes 'cls' as a classifier that initially contains no classification
156  * rules. */
157 void
158 classifier_init(struct classifier *cls)
159 {
160     int i;
161
162     cls->n_rules = 0;
163     for (i = 0; i < ARRAY_SIZE(cls->tables); i++) {
164         hmap_init(&cls->tables[i]);
165     }
166     hmap_init(&cls->exact_table);
167 }
168
169 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
170  * caller's responsibility. */
171 void
172 classifier_destroy(struct classifier *cls)
173 {
174     if (cls) {
175         struct cls_bucket *bucket, *next_bucket;
176         struct hmap *tbl;
177
178         for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
179             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
180                 free(bucket);
181             }
182             hmap_destroy(tbl);
183         }
184         hmap_destroy(&cls->exact_table);
185     }
186 }
187
188 /* Returns true if 'cls' does not contain any classification rules, false
189  * otherwise. */
190 bool
191 classifier_is_empty(const struct classifier *cls)
192 {
193     return cls->n_rules == 0;
194 }
195
196 /* Returns the number of rules in 'classifier'. */
197 int
198 classifier_count(const struct classifier *cls)
199 {
200     return cls->n_rules;
201 }
202
203 /* Returns the number of rules in 'classifier' that have no wildcards. */
204 int
205 classifier_count_exact(const struct classifier *cls)
206 {
207     return hmap_count(&cls->exact_table);
208 }
209
210 /* Returns the number of rules in 'classifier' that have at least one
211  * wildcard. */
212 int
213 classifier_count_wild(const struct classifier *cls)
214 {
215     return classifier_count(cls) - classifier_count_exact(cls);
216 }
217
218 /* Inserts 'rule' into 'cls'.  Transfers ownership of 'rule' to 'cls'.
219  *
220  * If 'cls' already contains an identical rule (including wildcards, values of
221  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
222  * rule that was replaced.  The caller takes ownership of the returned rule and
223  * is thus responsible for freeing it, etc., as necessary.
224  *
225  * Returns NULL if 'cls' does not contain a rule with an identical key, after
226  * inserting the new rule.  In this case, no rules are displaced by the new
227  * rule, even rules that cannot have any effect because the new rule matches a
228  * superset of their flows and has higher priority. */
229 struct cls_rule *
230 classifier_insert(struct classifier *cls, struct cls_rule *rule)
231 {
232     struct cls_rule *old;
233     assert((rule->flow.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
234     old = (rule->flow.wildcards
235            ? table_insert(&cls->tables[rule->table_idx], rule)
236            : insert_exact_rule(cls, rule));
237     if (!old) {
238         cls->n_rules++;
239     }
240     return old;
241 }
242
243 /* Inserts 'rule' into 'cls'.  Transfers ownership of 'rule' to 'cls'.
244  *
245  * 'rule' must be an exact-match rule (rule->flow.wildcards must be 0) and 'cls'
246  * must not contain any rule with an identical key. */
247 void
248 classifier_insert_exact(struct classifier *cls, struct cls_rule *rule)
249 {
250     hmap_insert(&cls->exact_table, &rule->node.hmap,
251                 flow_hash_headers(&rule->flow, 0));
252     cls->n_rules++;
253 }
254
255 /* Removes 'rule' from 'cls'.  It is caller's responsibility to free 'rule', if
256  * this is desirable. */
257 void
258 classifier_remove(struct classifier *cls, struct cls_rule *rule)
259 {
260     if (rule->flow.wildcards) {
261         /* Remove 'rule' from bucket.  If that empties the bucket, remove the
262          * bucket from its table. */
263         struct hmap *table = &cls->tables[rule->table_idx];
264         struct list *rules = list_remove(&rule->node.list);
265         if (list_is_empty(rules)) {
266             /* This code is a little tricky.  list_remove() returns the list
267              * element just after the one removed.  Since the list is now
268              * empty, this will be the address of the 'rules' member of the
269              * bucket that was just emptied, so pointer arithmetic (via
270              * CONTAINER_OF) can find that bucket. */
271             struct cls_bucket *bucket;
272             bucket = CONTAINER_OF(rules, struct cls_bucket, rules);
273             hmap_remove(table, &bucket->hmap_node);
274             free(bucket);
275         }
276     } else {
277         /* Remove 'rule' from cls->exact_table. */
278         hmap_remove(&cls->exact_table, &rule->node.hmap);
279     }
280     cls->n_rules--;
281 }
282
283 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
284  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
285  * of equal priority match 'flow', returns one arbitrarily.
286  *
287  * (When multiple rules of equal priority happen to fall into the same bucket,
288  * rules added more recently take priority over rules added less recently, but
289  * this is subject to change and should not be depended upon.) */
290 struct cls_rule *
291 classifier_lookup(const struct classifier *cls, const flow_t *flow)
292 {
293     struct cls_rule *rule = classifier_lookup_exact(cls, flow);
294     if (!rule) {
295         rule = classifier_lookup_wild(cls, flow);
296     }
297     return rule;
298 }
299
300 struct cls_rule *
301 classifier_lookup_exact(const struct classifier *cls, const flow_t *flow)
302 {
303     return (!hmap_is_empty(&cls->exact_table)
304             ? search_exact_table(cls, flow_hash_headers(flow, 0), flow)
305             : NULL);
306 }
307
308 struct cls_rule *
309 classifier_lookup_wild(const struct classifier *cls, const flow_t *flow)
310 {
311     struct cls_rule *best = NULL;
312     if (cls->n_rules > hmap_count(&cls->exact_table)) {
313         struct cls_rule target;
314         int i;
315
316         cls_rule_from_flow(flow, &target);
317         for (i = 0; i < CLS_N_FIELDS; i++) {
318             struct cls_rule *rule = search_table(&cls->tables[i], i, &target);
319             if (rule && (!best || rule->flow.priority > best->flow.priority)) {
320                 best = rule;
321             }
322         }
323     }
324     return best;
325 }
326
327 struct cls_rule *
328 classifier_find_rule_exactly(const struct classifier *cls,
329                              const flow_t *target)
330 {
331     struct cls_bucket *bucket;
332     int table_idx;
333     uint32_t hash;
334
335     if (!target->wildcards) {
336         /* Ignores 'priority'. */
337         return search_exact_table(cls, flow_hash_headers(target, 0), target);
338     }
339
340     assert(target->wildcards == (target->wildcards & OVSFW_ALL));
341     table_idx = table_idx_from_wildcards(target->wildcards);
342     hash = hash_fields(target, table_idx);
343     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash,
344                              &cls->tables[table_idx]) {
345         if (equal_fields(&bucket->fixed, target, table_idx)) {
346             struct cls_rule *pos;
347             LIST_FOR_EACH (pos, node.list, &bucket->rules) {
348                 if (pos->flow.priority < target->priority) {
349                     return NULL;
350                 } else if (pos->flow.priority == target->priority &&
351                            pos->flow.wildcards == target->wildcards &&
352                            flow_equal_headers(target, &pos->flow)) {
353                     return pos;
354                 }
355             }
356         }
357     }
358     return NULL;
359 }
360
361 /* Checks if the flow defined by 'target' overlaps with any other rule at the
362  * same priority in the classifier.  Two rules are considered overlapping if a
363  * packet could match both. */
364 bool
365 classifier_rule_overlaps(const struct classifier *cls, const flow_t *target)
366 {
367     struct cls_rule target_rule;
368     const struct hmap *tbl;
369
370     if (!target->wildcards) {
371         return search_exact_table(cls, flow_hash_headers(target, 0), target) ?
372             true : false;
373     }
374
375     cls_rule_from_flow(target, &target_rule);
376
377     for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
378         struct cls_bucket *bucket;
379
380         HMAP_FOR_EACH (bucket, hmap_node, tbl) {
381             struct cls_rule *rule;
382
383             LIST_FOR_EACH (rule, node.list, &bucket->rules) {
384                 if (rule->flow.priority == target->priority
385                     && rules_match_2wild(rule, &target_rule, 0)) {
386                     return true;
387                 }
388             }
389         }
390     }
391
392     return false;
393 }
394
395 /* Ignores target->flow.priority.
396  *
397  * 'callback' is allowed to delete the rule that is passed as its argument, but
398  * it must not delete (or move) any other rules in 'cls' that are in the same
399  * table as the argument rule.  Two rules are in the same table if their
400  * cls_rule structs have the same table_idx; as a special case, a rule with
401  * wildcards and an exact-match rule will never be in the same table.
402  *
403  * If 'callback' returns nonzero then the iteration stops immediately and
404  * classifier_for_each_match() passes up the return value.  Otherwise,
405  * classifier_for_each_match() returns 0 after completing the iteration. */
406 int
407 classifier_for_each_match(const struct classifier *cls,
408                           const flow_t *target_flow,
409                           int include, cls_cb_func *callback, void *aux)
410 {
411     struct cls_rule target;
412
413     cls_rule_from_flow(target_flow, &target);
414     if (include & CLS_INC_WILD) {
415         const struct hmap *table;
416
417         for (table = &cls->tables[0]; table < &cls->tables[CLS_N_FIELDS];
418              table++) {
419             struct cls_bucket *bucket, *next_bucket;
420
421             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, table) {
422                 /* XXX there is a bit of room for optimization here based on
423                  * rejecting entire buckets on their fixed fields, but it will
424                  * only be worthwhile for big buckets (which we hope we won't
425                  * get anyway, but...) */
426                 struct cls_rule *prev_rule, *rule;
427
428                 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
429                  * callback deletes the last rule in the bucket, then the
430                  * bucket itself will be destroyed.  The bucket contains the
431                  * list head so that's a use-after-free error. */
432                 prev_rule = NULL;
433                 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
434                     if (rules_match_1wild(rule, &target, 0)) {
435                         if (prev_rule) {
436                             int retval = callback(prev_rule, aux);
437                             if (retval) {
438                                 return retval;
439                             }
440                         }
441                         prev_rule = rule;
442                     }
443                 }
444                 if (prev_rule) {
445                     int retval = callback(prev_rule, aux);
446                     if (retval) {
447                         return retval;
448                     }
449                 }
450             }
451         }
452     }
453
454     if (include & CLS_INC_EXACT) {
455         if (target.flow.wildcards) {
456             struct cls_rule *rule, *next_rule;
457
458             HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap,
459                                 &cls->exact_table) {
460                 if (rules_match_1wild(rule, &target, 0)) {
461                     int retval = callback(rule, aux);
462                     if (retval) {
463                         return retval;
464                     }
465                 }
466             }
467         } else {
468             /* Optimization: there can be at most one match in the exact
469              * table. */
470             size_t hash = flow_hash_headers(&target.flow, 0);
471             struct cls_rule *rule = search_exact_table(cls, hash,
472                                                        &target.flow);
473             if (rule) {
474                 int retval = callback(rule, aux);
475                 if (retval) {
476                     return retval;
477                 }
478             }
479         }
480     }
481
482     return 0;
483 }
484
485 /* 'callback' is allowed to delete the rule that is passed as its argument, but
486  * it must not delete (or move) any other rules in 'cls' that are in the same
487  * table as the argument rule.  Two rules are in the same table if their
488  * cls_rule structs have the same table_idx; as a special case, a rule with
489  * wildcards and an exact-match rule will never be in the same table.
490  *
491  * If 'callback' returns nonzero then the iteration stops immediately and
492  * classifier_for_each() passes up the return value.  Otherwise,
493  * classifier_for_each() returns 0 after completing the iteration. */
494 int
495 classifier_for_each(const struct classifier *cls, int include,
496                     int (*callback)(struct cls_rule *, void *aux),
497                     void *aux)
498 {
499     if (include & CLS_INC_WILD) {
500         const struct hmap *tbl;
501
502         for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
503             struct cls_bucket *bucket, *next_bucket;
504
505             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
506                 struct cls_rule *prev_rule, *rule;
507
508                 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
509                  * callback deletes the last rule in the bucket, then the
510                  * bucket itself will be destroyed.  The bucket contains the
511                  * list head so that's a use-after-free error. */
512                 prev_rule = NULL;
513                 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
514                     if (prev_rule) {
515                         int retval = callback(prev_rule, aux);
516                         if (retval) {
517                             return retval;
518                         }
519                     }
520                     prev_rule = rule;
521                 }
522                 if (prev_rule) {
523                     int retval = callback(prev_rule, aux);
524                     if (retval) {
525                         return retval;
526                     }
527                 }
528             }
529         }
530     }
531
532     if (include & CLS_INC_EXACT) {
533         struct cls_rule *rule, *next_rule;
534
535         HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap, &cls->exact_table) {
536             int retval = callback(rule, aux);
537             if (retval) {
538                 return retval;
539             }
540         }
541     }
542
543     return 0;
544 }
545 \f
546 static struct cls_bucket *create_bucket(struct hmap *, size_t hash,
547                                         const flow_t *fixed);
548 static struct cls_rule *bucket_insert(struct cls_bucket *, struct cls_rule *);
549
550 static inline bool equal_bytes(const void *, const void *, size_t n);
551
552 /* Returns a hash computed across the fields in 'flow' whose field indexes
553  * (CLS_F_IDX_*) are less than 'table_idx'.  (If 'table_idx' is
554  * CLS_F_IDX_EXACT, hashes all the fields in 'flow'). */
555 static uint32_t
556 hash_fields(const flow_t *flow, int table_idx)
557 {
558     /* I just know I'm going to hell for writing code this way.
559      *
560      * GCC generates pretty good code here, with only a single taken
561      * conditional jump per execution.  Now the question is, would we be better
562      * off marking this function ALWAYS_INLINE and writing a wrapper that
563      * switches on the value of 'table_idx' to get rid of all the conditional
564      * jumps entirely (except for one in the wrapper)?  Honestly I really,
565      * really hope that it doesn't matter in practice.
566      *
567      * We could do better by calculating hashes incrementally, instead of
568      * starting over from the top each time.  But that would be even uglier. */
569     uint32_t a, b, c;
570     uint32_t tmp[3];
571     size_t n;
572
573     a = b = c = 0xdeadbeef + table_idx;
574     n = 0;
575
576 #define CLS_FIELD(WILDCARDS, MEMBER, NAME)                      \
577     if (table_idx == CLS_F_IDX_##NAME) {                        \
578         /* Done. */                                             \
579         memset((uint8_t *) tmp + n, 0, sizeof tmp - n);         \
580         goto finish;                                            \
581     } else {                                                    \
582         const size_t size = sizeof flow->MEMBER;                \
583         const uint8_t *p1 = (const uint8_t *) &flow->MEMBER;    \
584         const size_t p1_size = MIN(sizeof tmp - n, size);       \
585         const uint8_t *p2 = p1 + p1_size;                       \
586         const size_t p2_size = size - p1_size;                  \
587                                                                 \
588         /* Append to 'tmp' as much data as will fit. */         \
589         memcpy((uint8_t *) tmp + n, p1, p1_size);               \
590         n += p1_size;                                           \
591                                                                 \
592         /* If 'tmp' is full, mix. */                            \
593         if (n == sizeof tmp) {                                  \
594             a += tmp[0];                                        \
595             b += tmp[1];                                        \
596             c += tmp[2];                                        \
597             HASH_MIX(a, b, c);                                  \
598             n = 0;                                              \
599         }                                                       \
600                                                                 \
601         /* Append to 'tmp' any data that didn't fit. */         \
602         memcpy(tmp, p2, p2_size);                               \
603         n += p2_size;                                           \
604     }
605     CLS_FIELDS
606 #undef CLS_FIELD
607
608 finish:
609     a += tmp[0];
610     b += tmp[1];
611     c += tmp[2];
612     HASH_FINAL(a, b, c);
613     return c;
614 }
615
616 /* Compares the fields in 'a' and 'b' whose field indexes (CLS_F_IDX_*) are
617  * less than 'table_idx'.  (If 'table_idx' is CLS_F_IDX_EXACT, compares all the
618  * fields in 'a' and 'b').
619  *
620  * Returns true if all the compared fields are equal, false otherwise. */
621 static bool
622 equal_fields(const flow_t *a, const flow_t *b, int table_idx)
623 {
624     /* XXX The generated code could be better here. */
625 #define CLS_FIELD(WILDCARDS, MEMBER, NAME)                              \
626     if (table_idx == CLS_F_IDX_##NAME) {                                \
627         return true;                                                    \
628     } else if (!equal_bytes(&a->MEMBER, &b->MEMBER, sizeof a->MEMBER)) { \
629         return false;                                                   \
630     }
631     CLS_FIELDS
632 #undef CLS_FIELD
633
634     return true;
635 }
636
637 static int
638 table_idx_from_wildcards(uint32_t wildcards)
639 {
640     if (!wildcards) {
641         return CLS_F_IDX_EXACT;
642     }
643 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
644     if (wildcards & WILDCARDS) {           \
645         return CLS_F_IDX_##NAME;           \
646     }
647     CLS_FIELDS
648 #undef CLS_FIELD
649     NOT_REACHED();
650 }
651
652 /* Inserts 'rule' into 'table'.  Returns the rule, if any, that was displaced
653  * in favor of 'rule'. */
654 static struct cls_rule *
655 table_insert(struct hmap *table, struct cls_rule *rule)
656 {
657     struct cls_bucket *bucket;
658     size_t hash;
659
660     hash = hash_fields(&rule->flow, rule->table_idx);
661     bucket = find_bucket(table, hash, rule);
662     if (!bucket) {
663         bucket = create_bucket(table, hash, &rule->flow);
664     }
665
666     return bucket_insert(bucket, rule);
667 }
668
669 /* Inserts 'rule' into 'bucket', given that 'field' is the first wildcarded
670  * field in 'rule'.
671  *
672  * Returns the rule, if any, that was displaced in favor of 'rule'. */
673 static struct cls_rule *
674 bucket_insert(struct cls_bucket *bucket, struct cls_rule *rule)
675 {
676     struct cls_rule *pos;
677     LIST_FOR_EACH (pos, node.list, &bucket->rules) {
678         if (pos->flow.priority == rule->flow.priority) {
679             if (pos->flow.wildcards == rule->flow.wildcards
680                 && rules_match_1wild(pos, rule, rule->table_idx))
681             {
682                 list_replace(&rule->node.list, &pos->node.list);
683                 return pos;
684             }
685         } else if (pos->flow.priority < rule->flow.priority) {
686             break;
687         }
688     }
689     list_insert(&pos->node.list, &rule->node.list);
690     return NULL;
691 }
692
693 static struct cls_rule *
694 insert_exact_rule(struct classifier *cls, struct cls_rule *rule)
695 {
696     struct cls_rule *old_rule;
697     size_t hash;
698
699     hash = flow_hash_headers(&rule->flow, 0);
700     old_rule = search_exact_table(cls, hash, &rule->flow);
701     if (old_rule) {
702         hmap_remove(&cls->exact_table, &old_rule->node.hmap);
703     }
704     hmap_insert(&cls->exact_table, &rule->node.hmap, hash);
705     return old_rule;
706 }
707
708 /* Returns the bucket in 'table' that has the given 'hash' and the same fields
709  * as 'rule->flow' (up to 'rule->table_idx'), or a null pointer if no bucket
710  * matches. */
711 static struct cls_bucket *
712 find_bucket(struct hmap *table, size_t hash, const struct cls_rule *rule)
713 {
714     struct cls_bucket *bucket;
715     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash, table) {
716         if (equal_fields(&bucket->fixed, &rule->flow, rule->table_idx)) {
717             return bucket;
718         }
719     }
720     return NULL;
721 }
722
723 /* Creates a bucket and inserts it in 'table' with the given 'hash' and 'fixed'
724  * values.  Returns the new bucket. */
725 static struct cls_bucket *
726 create_bucket(struct hmap *table, size_t hash, const flow_t *fixed)
727 {
728     struct cls_bucket *bucket = xmalloc(sizeof *bucket);
729     list_init(&bucket->rules);
730     bucket->fixed = *fixed;
731     hmap_insert(table, &bucket->hmap_node, hash);
732     return bucket;
733 }
734
735 /* Returns true if the 'n' bytes in 'a' and 'b' are equal, false otherwise. */
736 static inline bool ALWAYS_INLINE
737 equal_bytes(const void *a, const void *b, size_t n)
738 {
739 #ifdef __i386__
740     /* For some reason GCC generates stupid code for memcmp() of small
741      * constant integer lengths.  Help it out.
742      *
743      * This function is always inlined, and it is always called with 'n' as a
744      * compile-time constant, so the switch statement gets optimized out and
745      * this whole function just expands to an instruction or two. */
746     switch (n) {
747     case 1:
748         return *(uint8_t *) a == *(uint8_t *) b;
749
750     case 2:
751         return *(uint16_t *) a == *(uint16_t *) b;
752
753     case 4:
754         return *(uint32_t *) a == *(uint32_t *) b;
755
756     case 6:
757         return (*(uint32_t *) a == *(uint32_t *) b
758                 && ((uint16_t *) a)[2] == ((uint16_t *) b)[2]);
759
760     default:
761         abort();
762     }
763 #else
764     /* I hope GCC is smarter on your platform. */
765     return !memcmp(a, b, n);
766 #endif
767 }
768
769 /* Returns the 32-bit unsigned integer at 'p'. */
770 static inline uint32_t
771 read_uint32(const void *p)
772 {
773     /* GCC optimizes this into a single machine instruction on x86. */
774     uint32_t x;
775     memcpy(&x, p, sizeof x);
776     return x;
777 }
778
779 /* Compares the specified field in 'a' and 'b'.  Returns true if the fields are
780  * equal, or if the ofp_match wildcard bits in 'wildcards' are set such that
781  * non-equal values may be ignored.  'nw_src_mask' and 'nw_dst_mask' must be
782  * those that would be set for 'wildcards' by cls_rule_set_masks().
783  *
784  * The compared field is the one with wildcard bit or bits 'field_wc', offset
785  * 'rule_ofs' within cls_rule's "fields" member, and length 'len', in bytes. */
786 static inline bool ALWAYS_INLINE
787 field_matches(const flow_t *a_, const flow_t *b_,
788               uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
789               uint32_t field_wc, int ofs, int len)
790 {
791     /* This function is always inlined, and it is always called with 'field_wc'
792      * as a compile-time constant, so the "if" conditionals here generate no
793      * code. */
794     const void *a = (const uint8_t *) a_ + ofs;
795     const void *b = (const uint8_t *) b_ + ofs;
796     if (!(field_wc & (field_wc - 1))) {
797         /* Handle all the single-bit wildcard cases. */
798         return wildcards & field_wc || equal_bytes(a, b, len);
799     } else if (field_wc == OFPFW_NW_SRC_MASK ||
800                field_wc == OFPFW_NW_DST_MASK) {
801         uint32_t a_ip = read_uint32(a);
802         uint32_t b_ip = read_uint32(b);
803         uint32_t mask = (field_wc == OFPFW_NW_SRC_MASK
804                          ? nw_src_mask : nw_dst_mask);
805         return ((a_ip ^ b_ip) & mask) == 0;
806     } else {
807         abort();
808     }
809 }
810
811 /* Returns true if 'a' and 'b' match, ignoring fields for which the wildcards
812  * in 'wildcards' are set.  'nw_src_mask' and 'nw_dst_mask' must be those that
813  * would be set for 'wildcards' by cls_rule_set_masks().  'field_idx' is the
814  * index of the first field to be compared; fields before 'field_idx' are
815  * assumed to match.  (Always returns true if 'field_idx' is CLS_N_FIELDS.) */
816 static bool
817 rules_match(const struct cls_rule *a, const struct cls_rule *b,
818             uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
819             int field_idx)
820 {
821     /* This is related to Duff's device (see
822      * http://en.wikipedia.org/wiki/Duff's_device).  */
823     switch (field_idx) {
824 #define CLS_FIELD(WILDCARDS, MEMBER, NAME)                          \
825         case CLS_F_IDX_##NAME:                                      \
826             if (!field_matches(&a->flow, &b->flow,                  \
827                                wildcards, nw_src_mask, nw_dst_mask, \
828                                WILDCARDS, offsetof(flow_t, MEMBER), \
829                                sizeof a->flow.MEMBER)) {            \
830                 return false;                                       \
831             }                                                       \
832         /* Fall though */
833         CLS_FIELDS
834 #undef CLS_FIELD
835     }
836     return true;
837 }
838
839 /* Returns true if 'fixed' and 'wild' match.  All fields in 'fixed' must have
840  * fixed values; 'wild' may contain wildcards.
841  *
842  * 'field_idx' is the index of the first field to be compared; fields before
843  * 'field_idx' are assumed to match.  Always returns true if 'field_idx' is
844  * CLS_N_FIELDS. */
845 static bool
846 rules_match_1wild(const struct cls_rule *fixed, const struct cls_rule *wild,
847                   int field_idx)
848 {
849     return rules_match(fixed, wild, wild->flow.wildcards, wild->wc.nw_src_mask,
850                        wild->wc.nw_dst_mask, field_idx);
851 }
852
853 /* Returns true if 'wild1' and 'wild2' match, that is, if their fields
854  * are equal modulo wildcards in 'wild1' or 'wild2'.
855  *
856  * 'field_idx' is the index of the first field to be compared; fields before
857  * 'field_idx' are assumed to match.  Always returns true if 'field_idx' is
858  * CLS_N_FIELDS. */
859 static bool
860 rules_match_2wild(const struct cls_rule *wild1, const struct cls_rule *wild2,
861                   int field_idx)
862 {
863     return rules_match(wild1, wild2,
864                        wild1->flow.wildcards | wild2->flow.wildcards,
865                        wild1->wc.nw_src_mask & wild2->wc.nw_src_mask,
866                        wild1->wc.nw_dst_mask & wild2->wc.nw_dst_mask,
867                        field_idx);
868 }
869
870 /* Searches 'bucket' for a rule that matches 'target'.  Returns the
871  * highest-priority match, if one is found, or a null pointer if there is no
872  * match.
873  *
874  * 'field_idx' must be the index of the first wildcarded field in 'bucket'. */
875 static struct cls_rule *
876 search_bucket(struct cls_bucket *bucket, int field_idx,
877               const struct cls_rule *target)
878 {
879     struct cls_rule *pos;
880
881     if (!equal_fields(&bucket->fixed, &target->flow, field_idx)) {
882         return NULL;
883     }
884
885     LIST_FOR_EACH (pos, node.list, &bucket->rules) {
886         if (rules_match_1wild(target, pos, field_idx)) {
887             return pos;
888         }
889     }
890     return NULL;
891 }
892
893 /* Searches 'table' for a rule that matches 'target'.  Returns the
894  * highest-priority match, if one is found, or a null pointer if there is no
895  * match.
896  *
897  * 'field_idx' must be the index of the first wildcarded field in 'table'. */
898 static struct cls_rule *
899 search_table(const struct hmap *table, int field_idx,
900              const struct cls_rule *target)
901 {
902     struct cls_bucket *bucket;
903
904     switch (hmap_count(table)) {
905         /* In these special cases there's no need to hash.  */
906     case 0:
907         return NULL;
908     case 1:
909         bucket = CONTAINER_OF(hmap_first(table), struct cls_bucket, hmap_node);
910         return search_bucket(bucket, field_idx, target);
911     }
912
913     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node,
914                              hash_fields(&target->flow, field_idx), table) {
915         struct cls_rule *rule = search_bucket(bucket, field_idx, target);
916         if (rule) {
917             return rule;
918         }
919     }
920     return NULL;
921 }
922
923 static struct cls_rule *
924 search_exact_table(const struct classifier *cls, size_t hash,
925                    const flow_t *target)
926 {
927     struct cls_rule *rule;
928
929     HMAP_FOR_EACH_WITH_HASH (rule, node.hmap, hash, &cls->exact_table) {
930         if (flow_equal_headers(&rule->flow, target)) {
931             return rule;
932         }
933     }
934     return NULL;
935 }