334d0dab55033a16e651d7e7c73f80feb43f54d0
[cascardo/ovs.git] / lib / classifier.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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 <errno.h>
20 #include <netinet/in.h>
21 #include "byte-order.h"
22 #include "dynamic-string.h"
23 #include "flow.h"
24 #include "hash.h"
25 #include "cmap.h"
26 #include "list.h"
27 #include "odp-util.h"
28 #include "ofp-util.h"
29 #include "packets.h"
30 #include "tag.h"
31 #include "util.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(classifier);
35
36 struct trie_ctx;
37
38 /* Ports trie depends on both ports sharing the same ovs_be32. */
39 #define TP_PORTS_OFS32 (offsetof(struct flow, tp_src) / 4)
40 BUILD_ASSERT_DECL(TP_PORTS_OFS32 == offsetof(struct flow, tp_dst) / 4);
41
42 /* A set of rules that all have the same fields wildcarded. */
43 struct cls_subtable {
44     /* The fields are only used by writers and iterators. */
45     struct cmap_node cmap_node; /* Within struct classifier 'subtables_map'. */
46
47     /* The fields are only used by writers. */
48     int n_rules OVS_GUARDED;                /* Number of rules, including
49                                              * duplicates. */
50     unsigned int max_priority OVS_GUARDED;  /* Max priority of any rule in
51                                              * the subtable. */
52     unsigned int max_count OVS_GUARDED;     /* Count of max_priority rules. */
53
54     /* These fields are accessed by readers who care about wildcarding. */
55     tag_type tag;       /* Tag generated from mask for partitioning (const). */
56     uint8_t n_indices;                   /* How many indices to use (const). */
57     uint8_t index_ofs[CLS_MAX_INDICES];   /* u32 segment boundaries (const). */
58     unsigned int trie_plen[CLS_MAX_TRIES];  /* Trie prefix length in 'mask'
59                                              * (runtime configurable). */
60     int ports_mask_len;                     /* (const) */
61     struct cmap indices[CLS_MAX_INDICES];   /* Staged lookup indices. */
62     rcu_trie_ptr ports_trie;                /* NULL if none. */
63
64     /* These fields are accessed by all readers. */
65     struct cmap rules;                      /* Contains "struct cls_rule"s. */
66     struct minimask mask;                   /* Wildcards for fields (const). */
67     /* 'mask' must be the last field. */
68 };
69
70 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
71  * field) with tags for the "cls_subtable"s that contain rules that match that
72  * metadata value.  */
73 struct cls_partition {
74     struct cmap_node cmap_node; /* In struct classifier's 'partitions' map. */
75     ovs_be64 metadata;          /* metadata value for this partition. */
76     tag_type tags;              /* OR of each flow's cls_subtable tag. */
77     struct tag_tracker tracker OVS_GUARDED; /* Tracks the bits in 'tags'. */
78 };
79
80 /* Internal representation of a rule in a "struct cls_subtable". */
81 struct cls_match {
82     /* Accessed only by writers and iterators. */
83     struct list list OVS_GUARDED; /* List of identical, lower-priority rules. */
84
85     /* Accessed only by writers. */
86     struct cls_partition *partition OVS_GUARDED;
87
88     /* Accessed by readers interested in wildcarding. */
89     unsigned int priority;      /* Larger numbers are higher priorities. */
90     struct cmap_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
91                                                     * 'indices'. */
92     /* Accessed by all readers. */
93     struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */
94     struct cls_rule *cls_rule;
95     struct miniflow flow;       /* Matching rule. Mask is in the subtable. */
96     /* 'flow' must be the last field. */
97 };
98
99 static struct cls_match *
100 cls_match_alloc(struct cls_rule *rule)
101 {
102     int count = count_1bits(rule->match.flow.map);
103
104     struct cls_match *cls_match
105         = xmalloc(sizeof *cls_match - sizeof cls_match->flow.inline_values
106                   + MINIFLOW_VALUES_SIZE(count));
107
108     cls_match->cls_rule = rule;
109     miniflow_clone_inline(&cls_match->flow, &rule->match.flow, count);
110     cls_match->priority = rule->priority;
111     rule->cls_match = cls_match;
112
113     return cls_match;
114 }
115
116 static struct cls_subtable *find_subtable(const struct classifier *cls,
117                                           const struct minimask *)
118     OVS_REQUIRES(cls->mutex);
119 static struct cls_subtable *insert_subtable(struct classifier *cls,
120                                             const struct minimask *)
121     OVS_REQUIRES(cls->mutex);
122 static void destroy_subtable(struct classifier *cls, struct cls_subtable *)
123     OVS_REQUIRES(cls->mutex);
124 static struct cls_match *insert_rule(struct classifier *cls,
125                                      struct cls_subtable *, struct cls_rule *)
126     OVS_REQUIRES(cls->mutex);
127
128 static struct cls_match *find_match_wc(const struct cls_subtable *,
129                                        const struct flow *, struct trie_ctx *,
130                                        unsigned int n_tries,
131                                        struct flow_wildcards *);
132 static struct cls_match *find_equal(struct cls_subtable *,
133                                     const struct miniflow *, uint32_t hash);
134
135 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list.
136  * Classifier's mutex must be held while iterating, as the list is
137  * protoceted by it. */
138 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD)                               \
139     for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
140 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD)                    \
141     for ((RULE) = (HEAD);                                               \
142          (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true);    \
143          (RULE) = (NEXT))
144
145 static struct cls_match *next_rule_in_list__(struct cls_match *);
146 static struct cls_match *next_rule_in_list(struct cls_match *);
147
148 static unsigned int minimask_get_prefix_len(const struct minimask *,
149                                             const struct mf_field *);
150 static void trie_init(struct classifier *cls, int trie_idx,
151                       const struct mf_field *)
152     OVS_REQUIRES(cls->mutex);
153 static unsigned int trie_lookup(const struct cls_trie *, const struct flow *,
154                                 union mf_value *plens);
155 static unsigned int trie_lookup_value(const rcu_trie_ptr *,
156                                       const ovs_be32 value[], ovs_be32 plens[],
157                                       unsigned int value_bits);
158 static void trie_destroy(rcu_trie_ptr *);
159 static void trie_insert(struct cls_trie *, const struct cls_rule *, int mlen);
160 static void trie_insert_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
161                                int mlen);
162 static void trie_remove(struct cls_trie *, const struct cls_rule *, int mlen);
163 static void trie_remove_prefix(rcu_trie_ptr *, const ovs_be32 *prefix,
164                                int mlen);
165 static void mask_set_prefix_bits(struct flow_wildcards *, uint8_t be32ofs,
166                                  unsigned int n_bits);
167 static bool mask_prefix_bits_set(const struct flow_wildcards *,
168                                  uint8_t be32ofs, unsigned int n_bits);
169 \f
170 /* flow/miniflow/minimask/minimatch utilities.
171  * These are only used by the classifier, so place them here to allow
172  * for better optimization. */
173
174 static inline uint64_t
175 miniflow_get_map_in_range(const struct miniflow *miniflow,
176                           uint8_t start, uint8_t end, unsigned int *offset)
177 {
178     uint64_t map = miniflow->map;
179     *offset = 0;
180
181     if (start > 0) {
182         uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
183         *offset = count_1bits(map & msk);
184         map &= ~msk;
185     }
186     if (end < FLOW_U32S) {
187         uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
188         map &= msk;
189     }
190     return map;
191 }
192
193 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
194  * 'mask', given 'basis'.
195  *
196  * The hash values returned by this function are the same as those returned by
197  * miniflow_hash_in_minimask(), only the form of the arguments differ. */
198 static inline uint32_t
199 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
200                       uint32_t basis)
201 {
202     const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
203     const uint32_t *flow_u32 = (const uint32_t *)flow;
204     const uint32_t *p = mask_values;
205     uint32_t hash;
206     uint64_t map;
207
208     hash = basis;
209     for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
210         hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
211     }
212
213     return hash_finish(hash, (p - mask_values) * 4);
214 }
215
216 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
217  * 'mask', given 'basis'.
218  *
219  * The hash values returned by this function are the same as those returned by
220  * flow_hash_in_minimask(), only the form of the arguments differ. */
221 static inline uint32_t
222 miniflow_hash_in_minimask(const struct miniflow *flow,
223                           const struct minimask *mask, uint32_t basis)
224 {
225     const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
226     const uint32_t *p = mask_values;
227     uint32_t hash = basis;
228     uint32_t flow_u32;
229
230     MINIFLOW_FOR_EACH_IN_MAP(flow_u32, flow, mask->masks.map) {
231         hash = hash_add(hash, flow_u32 & *p++);
232     }
233
234     return hash_finish(hash, (p - mask_values) * 4);
235 }
236
237 /* Returns a hash value for the bits of range [start, end) in 'flow',
238  * where there are 1-bits in 'mask', given 'hash'.
239  *
240  * The hash values returned by this function are the same as those returned by
241  * minimatch_hash_range(), only the form of the arguments differ. */
242 static inline uint32_t
243 flow_hash_in_minimask_range(const struct flow *flow,
244                             const struct minimask *mask,
245                             uint8_t start, uint8_t end, uint32_t *basis)
246 {
247     const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
248     const uint32_t *flow_u32 = (const uint32_t *)flow;
249     unsigned int offset;
250     uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
251                                              &offset);
252     const uint32_t *p = mask_values + offset;
253     uint32_t hash = *basis;
254
255     for (; map; map = zero_rightmost_1bit(map)) {
256         hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
257     }
258
259     *basis = hash; /* Allow continuation from the unfinished value. */
260     return hash_finish(hash, (p - mask_values) * 4);
261 }
262
263 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
264 static inline void
265 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
266                              const struct minimask *mask)
267 {
268     flow_union_with_miniflow(&wc->masks, &mask->masks);
269 }
270
271 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
272  * in range [start, end). */
273 static inline void
274 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
275                                    const struct minimask *mask,
276                                    uint8_t start, uint8_t end)
277 {
278     uint32_t *dst_u32 = (uint32_t *)&wc->masks;
279     unsigned int offset;
280     uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
281                                              &offset);
282     const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
283
284     for (; map; map = zero_rightmost_1bit(map)) {
285         dst_u32[raw_ctz(map)] |= *p++;
286     }
287 }
288
289 /* Returns a hash value for 'flow', given 'basis'. */
290 static inline uint32_t
291 miniflow_hash(const struct miniflow *flow, uint32_t basis)
292 {
293     const uint32_t *values = miniflow_get_u32_values(flow);
294     const uint32_t *p = values;
295     uint32_t hash = basis;
296     uint64_t hash_map = 0;
297     uint64_t map;
298
299     for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
300         if (*p) {
301             hash = hash_add(hash, *p);
302             hash_map |= rightmost_1bit(map);
303         }
304         p++;
305     }
306     hash = hash_add(hash, hash_map);
307     hash = hash_add(hash, hash_map >> 32);
308
309     return hash_finish(hash, p - values);
310 }
311
312 /* Returns a hash value for 'mask', given 'basis'. */
313 static inline uint32_t
314 minimask_hash(const struct minimask *mask, uint32_t basis)
315 {
316     return miniflow_hash(&mask->masks, basis);
317 }
318
319 /* Returns a hash value for 'match', given 'basis'. */
320 static inline uint32_t
321 minimatch_hash(const struct minimatch *match, uint32_t basis)
322 {
323     return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
324 }
325
326 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
327  * given 'basis'.
328  *
329  * The hash values returned by this function are the same as those returned by
330  * flow_hash_in_minimask_range(), only the form of the arguments differ. */
331 static inline uint32_t
332 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
333                      uint32_t *basis)
334 {
335     unsigned int offset;
336     const uint32_t *p, *q;
337     uint32_t hash = *basis;
338     int n, i;
339
340     n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
341                                               &offset));
342     q = miniflow_get_u32_values(&match->mask.masks) + offset;
343     p = miniflow_get_u32_values(&match->flow) + offset;
344
345     for (i = 0; i < n; i++) {
346         hash = hash_add(hash, p[i] & q[i]);
347     }
348     *basis = hash; /* Allow continuation from the unfinished value. */
349     return hash_finish(hash, (offset + n) * 4);
350 }
351
352 \f
353 /* cls_rule. */
354
355 /* Initializes 'rule' to match packets specified by 'match' at the given
356  * 'priority'.  'match' must satisfy the invariant described in the comment at
357  * the definition of struct match.
358  *
359  * The caller must eventually destroy 'rule' with cls_rule_destroy().
360  *
361  * (OpenFlow uses priorities between 0 and UINT16_MAX, inclusive, but
362  * internally Open vSwitch supports a wider range.) */
363 void
364 cls_rule_init(struct cls_rule *rule,
365               const struct match *match, unsigned int priority)
366 {
367     minimatch_init(&rule->match, match);
368     rule->priority = priority;
369     rule->cls_match = NULL;
370 }
371
372 /* Same as cls_rule_init() for initialization from a "struct minimatch". */
373 void
374 cls_rule_init_from_minimatch(struct cls_rule *rule,
375                              const struct minimatch *match,
376                              unsigned int priority)
377 {
378     minimatch_clone(&rule->match, match);
379     rule->priority = priority;
380     rule->cls_match = NULL;
381 }
382
383 /* Initializes 'dst' as a copy of 'src'.
384  *
385  * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
386 void
387 cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
388 {
389     minimatch_clone(&dst->match, &src->match);
390     dst->priority = src->priority;
391     dst->cls_match = NULL;
392 }
393
394 /* Initializes 'dst' with the data in 'src', destroying 'src'.
395  *
396  * The caller must eventually destroy 'dst' with cls_rule_destroy(). */
397 void
398 cls_rule_move(struct cls_rule *dst, struct cls_rule *src)
399 {
400     minimatch_move(&dst->match, &src->match);
401     dst->priority = src->priority;
402     dst->cls_match = NULL;
403 }
404
405 /* Frees memory referenced by 'rule'.  Doesn't free 'rule' itself (it's
406  * normally embedded into a larger structure).
407  *
408  * ('rule' must not currently be in a classifier.) */
409 void
410 cls_rule_destroy(struct cls_rule *rule)
411 {
412     ovs_assert(!rule->cls_match);
413     minimatch_destroy(&rule->match);
414 }
415
416 /* Returns true if 'a' and 'b' match the same packets at the same priority,
417  * false if they differ in some way. */
418 bool
419 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
420 {
421     return a->priority == b->priority && minimatch_equal(&a->match, &b->match);
422 }
423
424 /* Returns a hash value for 'rule', folding in 'basis'. */
425 uint32_t
426 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
427 {
428     return minimatch_hash(&rule->match, hash_int(rule->priority, basis));
429 }
430
431 /* Appends a string describing 'rule' to 's'. */
432 void
433 cls_rule_format(const struct cls_rule *rule, struct ds *s)
434 {
435     minimatch_format(&rule->match, s, rule->priority);
436 }
437
438 /* Returns true if 'rule' matches every packet, false otherwise. */
439 bool
440 cls_rule_is_catchall(const struct cls_rule *rule)
441 {
442     return minimask_is_catchall(&rule->match.mask);
443 }
444 \f
445 /* Initializes 'cls' as a classifier that initially contains no classification
446  * rules. */
447 void
448 classifier_init(struct classifier *cls, const uint8_t *flow_segments)
449     OVS_EXCLUDED(cls->mutex)
450 {
451     ovs_mutex_init(&cls->mutex);
452     ovs_mutex_lock(&cls->mutex);
453     cls->n_rules = 0;
454     cmap_init(&cls->subtables_map);
455     pvector_init(&cls->subtables);
456     cmap_init(&cls->partitions);
457     cls->n_flow_segments = 0;
458     if (flow_segments) {
459         while (cls->n_flow_segments < CLS_MAX_INDICES
460                && *flow_segments < FLOW_U32S) {
461             cls->flow_segments[cls->n_flow_segments++] = *flow_segments++;
462         }
463     }
464     cls->n_tries = 0;
465     for (int i = 0; i < CLS_MAX_TRIES; i++) {
466         trie_init(cls, i, NULL);
467     }
468     ovs_mutex_unlock(&cls->mutex);
469 }
470
471 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
472  * caller's responsibility.
473  * May only be called after all the readers have been terminated. */
474 void
475 classifier_destroy(struct classifier *cls)
476     OVS_EXCLUDED(cls->mutex)
477 {
478     if (cls) {
479         struct cls_partition *partition;
480         struct cls_subtable *subtable;
481         int i;
482
483         ovs_mutex_lock(&cls->mutex);
484         for (i = 0; i < cls->n_tries; i++) {
485             trie_destroy(&cls->tries[i].root);
486         }
487
488         CMAP_FOR_EACH_SAFE (subtable, cmap_node, &cls->subtables_map) {
489             destroy_subtable(cls, subtable);
490         }
491         cmap_destroy(&cls->subtables_map);
492
493         CMAP_FOR_EACH_SAFE (partition, cmap_node, &cls->partitions) {
494             ovsrcu_postpone(free, partition);
495         }
496         cmap_destroy(&cls->partitions);
497
498         pvector_destroy(&cls->subtables);
499         ovs_mutex_unlock(&cls->mutex);
500         ovs_mutex_destroy(&cls->mutex);
501     }
502 }
503
504 /* Set the fields for which prefix lookup should be performed. */
505 bool
506 classifier_set_prefix_fields(struct classifier *cls,
507                              const enum mf_field_id *trie_fields,
508                              unsigned int n_fields)
509     OVS_EXCLUDED(cls->mutex)
510 {
511     const struct mf_field * new_fields[CLS_MAX_TRIES];
512     struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
513     int i, n_tries = 0;
514     bool changed = false;
515
516     ovs_mutex_lock(&cls->mutex);
517     for (i = 0; i < n_fields && n_tries < CLS_MAX_TRIES; i++) {
518         const struct mf_field *field = mf_from_id(trie_fields[i]);
519         if (field->flow_be32ofs < 0 || field->n_bits % 32) {
520             /* Incompatible field.  This is the only place where we
521              * enforce these requirements, but the rest of the trie code
522              * depends on the flow_be32ofs to be non-negative and the
523              * field length to be a multiple of 32 bits. */
524             continue;
525         }
526
527         if (bitmap_is_set(fields.bm, trie_fields[i])) {
528             /* Duplicate field, there is no need to build more than
529              * one index for any one field. */
530             continue;
531         }
532         bitmap_set1(fields.bm, trie_fields[i]);
533
534         new_fields[n_tries] = NULL;
535         if (n_tries >= cls->n_tries || field != cls->tries[n_tries].field) {
536             new_fields[n_tries] = field;
537             changed = true;
538         }
539         n_tries++;
540     }
541
542     if (changed || n_tries < cls->n_tries) {
543         struct cls_subtable *subtable;
544
545         /* Trie configuration needs to change.  Disable trie lookups
546          * for the tries that are changing and wait all the current readers
547          * with the old configuration to be done. */
548         changed = false;
549         CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
550             for (i = 0; i < cls->n_tries; i++) {
551                 if ((i < n_tries && new_fields[i]) || i >= n_tries) {
552                     if (subtable->trie_plen[i]) {
553                         subtable->trie_plen[i] = 0;
554                         changed = true;
555                     }
556                 }
557             }
558         }
559         /* Synchronize if any readers were using tries.  The readers may
560          * temporarily function without the trie lookup based optimizations. */
561         if (changed) {
562             /* ovsrcu_synchronize() functions as a memory barrier, so it does
563              * not matter that subtable->trie_plen is not atomic. */
564             ovsrcu_synchronize();
565         }
566
567         /* Now set up the tries. */
568         for (i = 0; i < n_tries; i++) {
569             if (new_fields[i]) {
570                 trie_init(cls, i, new_fields[i]);
571             }
572         }
573         /* Destroy the rest, if any. */
574         for (; i < cls->n_tries; i++) {
575             trie_init(cls, i, NULL);
576         }
577
578         cls->n_tries = n_tries;
579         ovs_mutex_unlock(&cls->mutex);
580         return true;
581     }
582
583     ovs_mutex_unlock(&cls->mutex);
584     return false; /* No change. */
585 }
586
587 static void
588 trie_init(struct classifier *cls, int trie_idx, const struct mf_field *field)
589     OVS_REQUIRES(cls->mutex)
590 {
591     struct cls_trie *trie = &cls->tries[trie_idx];
592     struct cls_subtable *subtable;
593
594     if (trie_idx < cls->n_tries) {
595         trie_destroy(&trie->root);
596     } else {
597         ovsrcu_set_hidden(&trie->root, NULL);
598     }
599     trie->field = field;
600
601     /* Add existing rules to the new trie. */
602     CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
603         unsigned int plen;
604
605         plen = field ? minimask_get_prefix_len(&subtable->mask, field) : 0;
606         if (plen) {
607             struct cls_match *head;
608
609             CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
610                 struct cls_match *match;
611
612                 FOR_EACH_RULE_IN_LIST (match, head) {
613                     trie_insert(trie, match->cls_rule, plen);
614                 }
615             }
616         }
617         /* Initialize subtable's prefix length on this field.  This will
618          * allow readers to use the trie. */
619         atomic_thread_fence(memory_order_release);
620         subtable->trie_plen[trie_idx] = plen;
621     }
622 }
623
624 /* Returns true if 'cls' contains no classification rules, false otherwise.
625  * Checking the cmap requires no locking. */
626 bool
627 classifier_is_empty(const struct classifier *cls)
628 {
629     return cmap_is_empty(&cls->subtables_map);
630 }
631
632 /* Returns the number of rules in 'cls'. */
633 int
634 classifier_count(const struct classifier *cls)
635     OVS_NO_THREAD_SAFETY_ANALYSIS
636 {
637     /* n_rules is an int, so in the presence of concurrent writers this will
638      * return either the old or a new value. */
639     return cls->n_rules;
640 }
641
642 static uint32_t
643 hash_metadata(ovs_be64 metadata_)
644 {
645     uint64_t metadata = (OVS_FORCE uint64_t) metadata_;
646     return hash_uint64(metadata);
647 }
648
649 static struct cls_partition *
650 find_partition(const struct classifier *cls, ovs_be64 metadata, uint32_t hash)
651 {
652     struct cls_partition *partition;
653
654     CMAP_FOR_EACH_WITH_HASH (partition, cmap_node, hash, &cls->partitions) {
655         if (partition->metadata == metadata) {
656             return partition;
657         }
658     }
659
660     return NULL;
661 }
662
663 static struct cls_partition *
664 create_partition(struct classifier *cls, struct cls_subtable *subtable,
665                  ovs_be64 metadata)
666     OVS_REQUIRES(cls->mutex)
667 {
668     uint32_t hash = hash_metadata(metadata);
669     struct cls_partition *partition = find_partition(cls, metadata, hash);
670     if (!partition) {
671         partition = xmalloc(sizeof *partition);
672         partition->metadata = metadata;
673         partition->tags = 0;
674         tag_tracker_init(&partition->tracker);
675         cmap_insert(&cls->partitions, &partition->cmap_node, hash);
676     }
677     tag_tracker_add(&partition->tracker, &partition->tags, subtable->tag);
678     return partition;
679 }
680
681 static inline ovs_be32 minimatch_get_ports(const struct minimatch *match)
682 {
683     /* Could optimize to use the same map if needed for fast path. */
684     return MINIFLOW_GET_BE32(&match->flow, tp_src)
685         & MINIFLOW_GET_BE32(&match->mask.masks, tp_src);
686 }
687
688 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
689  * must not modify or free it.
690  *
691  * If 'cls' already contains an identical rule (including wildcards, values of
692  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
693  * rule that was replaced.  The caller takes ownership of the returned rule and
694  * is thus responsible for destroying it with cls_rule_destroy(), freeing the
695  * memory block in which it resides, etc., as necessary.
696  *
697  * Returns NULL if 'cls' does not contain a rule with an identical key, after
698  * inserting the new rule.  In this case, no rules are displaced by the new
699  * rule, even rules that cannot have any effect because the new rule matches a
700  * superset of their flows and has higher priority. */
701 struct cls_rule *
702 classifier_replace(struct classifier *cls, struct cls_rule *rule)
703     OVS_EXCLUDED(cls->mutex)
704 {
705     struct cls_match *old_rule;
706     struct cls_subtable *subtable;
707     struct cls_rule *old_cls_rule = NULL;
708
709     ovs_mutex_lock(&cls->mutex);
710     subtable = find_subtable(cls, &rule->match.mask);
711     if (!subtable) {
712         subtable = insert_subtable(cls, &rule->match.mask);
713     }
714
715     old_rule = insert_rule(cls, subtable, rule);
716     if (!old_rule) {
717         old_cls_rule = NULL;
718
719         rule->cls_match->partition = NULL;
720         if (minimask_get_metadata_mask(&rule->match.mask) == OVS_BE64_MAX) {
721             ovs_be64 metadata = miniflow_get_metadata(&rule->match.flow);
722             rule->cls_match->partition = create_partition(cls, subtable,
723                                                           metadata);
724         }
725
726         cls->n_rules++;
727
728         for (int i = 0; i < cls->n_tries; i++) {
729             if (subtable->trie_plen[i]) {
730                 trie_insert(&cls->tries[i], rule, subtable->trie_plen[i]);
731             }
732         }
733
734         /* Ports trie. */
735         if (subtable->ports_mask_len) {
736             /* We mask the value to be inserted to always have the wildcarded
737              * bits in known (zero) state, so we can include them in comparison
738              * and they will always match (== their original value does not
739              * matter). */
740             ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
741
742             trie_insert_prefix(&subtable->ports_trie, &masked_ports,
743                                subtable->ports_mask_len);
744         }
745     } else {
746         old_cls_rule = old_rule->cls_rule;
747         rule->cls_match->partition = old_rule->partition;
748         old_cls_rule->cls_match = NULL;
749
750         /* 'old_rule' contains a cmap_node, which may not be freed
751          * immediately. */
752         ovsrcu_postpone(free, old_rule);
753     }
754     ovs_mutex_unlock(&cls->mutex);
755     return old_cls_rule;
756 }
757
758 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
759  * must not modify or free it.
760  *
761  * 'cls' must not contain an identical rule (including wildcards, values of
762  * fixed fields, and priority).  Use classifier_find_rule_exactly() to find
763  * such a rule. */
764 void
765 classifier_insert(struct classifier *cls, struct cls_rule *rule)
766 {
767     struct cls_rule *displaced_rule = classifier_replace(cls, rule);
768     ovs_assert(!displaced_rule);
769 }
770
771 /* Removes 'rule' from 'cls'.  It is the caller's responsibility to destroy
772  * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
773  * resides, etc., as necessary. */
774 void
775 classifier_remove(struct classifier *cls, struct cls_rule *rule)
776     OVS_EXCLUDED(cls->mutex)
777 {
778     struct cls_partition *partition;
779     struct cls_match *cls_match = rule->cls_match;
780     struct cls_match *head;
781     struct cls_subtable *subtable;
782     int i;
783     uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
784     uint8_t prev_be32ofs = 0;
785
786     ovs_assert(cls_match);
787
788     ovs_mutex_lock(&cls->mutex);
789     subtable = find_subtable(cls, &rule->match.mask);
790     ovs_assert(subtable);
791
792     if (subtable->ports_mask_len) {
793         ovs_be32 masked_ports = minimatch_get_ports(&rule->match);
794
795         trie_remove_prefix(&subtable->ports_trie,
796                            &masked_ports, subtable->ports_mask_len);
797     }
798     for (i = 0; i < cls->n_tries; i++) {
799         if (subtable->trie_plen[i]) {
800             trie_remove(&cls->tries[i], rule, subtable->trie_plen[i]);
801         }
802     }
803
804     /* Remove rule node from indices. */
805     for (i = 0; i < subtable->n_indices; i++) {
806         ihash[i] = minimatch_hash_range(&rule->match, prev_be32ofs,
807                                         subtable->index_ofs[i], &basis);
808         cmap_remove(&subtable->indices[i], &cls_match->index_nodes[i],
809                     ihash[i]);
810         prev_be32ofs = subtable->index_ofs[i];
811     }
812     hash = minimatch_hash_range(&rule->match, prev_be32ofs, FLOW_U32S, &basis);
813
814     head = find_equal(subtable, &rule->match.flow, hash);
815     if (head != cls_match) {
816         list_remove(&cls_match->list);
817     } else if (list_is_empty(&cls_match->list)) {
818         cmap_remove(&subtable->rules, &cls_match->cmap_node, hash);
819     } else {
820         struct cls_match *next = CONTAINER_OF(cls_match->list.next,
821                                               struct cls_match, list);
822
823         list_remove(&cls_match->list);
824         cmap_replace(&subtable->rules, &cls_match->cmap_node,
825                      &next->cmap_node, hash);
826     }
827
828     partition = cls_match->partition;
829     if (partition) {
830         tag_tracker_subtract(&partition->tracker, &partition->tags,
831                              subtable->tag);
832         if (!partition->tags) {
833             cmap_remove(&cls->partitions, &partition->cmap_node,
834                         hash_metadata(partition->metadata));
835             ovsrcu_postpone(free, partition);
836         }
837     }
838
839     if (--subtable->n_rules == 0) {
840         destroy_subtable(cls, subtable);
841     } else if (subtable->max_priority == cls_match->priority
842                && --subtable->max_count == 0) {
843         /* Find the new 'max_priority' and 'max_count'. */
844         struct cls_match *head;
845         unsigned int max_priority = 0;
846
847         CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
848             if (head->priority > max_priority) {
849                 max_priority = head->priority;
850                 subtable->max_count = 1;
851             } else if (head->priority == max_priority) {
852                 ++subtable->max_count;
853             }
854         }
855         subtable->max_priority = max_priority;
856         pvector_change_priority(&cls->subtables, subtable, max_priority);
857     }
858
859     cls->n_rules--;
860
861     rule->cls_match = NULL;
862     ovsrcu_postpone(free, cls_match);
863     ovs_mutex_unlock(&cls->mutex);
864 }
865
866 /* Prefix tree context.  Valid when 'lookup_done' is true.  Can skip all
867  * subtables which have a prefix match on the trie field, but whose prefix
868  * length is not indicated in 'match_plens'.  For example, a subtable that
869  * has a 8-bit trie field prefix match can be skipped if
870  * !be_get_bit_at(&match_plens, 8 - 1).  If skipped, 'maskbits' prefix bits
871  * must be unwildcarded to make datapath flow only match packets it should. */
872 struct trie_ctx {
873     const struct cls_trie *trie;
874     bool lookup_done;        /* Status of the lookup. */
875     uint8_t be32ofs;         /* U32 offset of the field in question. */
876     unsigned int maskbits;   /* Prefix length needed to avoid false matches. */
877     union mf_value match_plens; /* Bitmask of prefix lengths with possible
878                                  * matches. */
879 };
880
881 static void
882 trie_ctx_init(struct trie_ctx *ctx, const struct cls_trie *trie)
883 {
884     ctx->trie = trie;
885     ctx->be32ofs = trie->field->flow_be32ofs;
886     ctx->lookup_done = false;
887 }
888
889 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
890  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
891  * of equal priority match 'flow', returns one arbitrarily.
892  *
893  * If a rule is found and 'wc' is non-null, bitwise-OR's 'wc' with the
894  * set of bits that were significant in the lookup.  At some point
895  * earlier, 'wc' should have been initialized (e.g., by
896  * flow_wildcards_init_catchall()). */
897 struct cls_rule *
898 classifier_lookup(const struct classifier *cls, const struct flow *flow,
899                   struct flow_wildcards *wc)
900 {
901     const struct cls_partition *partition;
902     tag_type tags;
903     int64_t best_priority = -1;
904     const struct cls_match *best;
905     struct trie_ctx trie_ctx[CLS_MAX_TRIES];
906     struct cls_subtable *subtable;
907
908     /* Synchronize for cls->n_tries and subtable->trie_plen.  They can change
909      * when table configuration changes, which happens typically only on
910      * startup. */
911     atomic_thread_fence(memory_order_acquire);
912
913     /* Determine 'tags' such that, if 'subtable->tag' doesn't intersect them,
914      * then 'flow' cannot possibly match in 'subtable':
915      *
916      *     - If flow->metadata maps to a given 'partition', then we can use
917      *       'tags' for 'partition->tags'.
918      *
919      *     - If flow->metadata has no partition, then no rule in 'cls' has an
920      *       exact-match for flow->metadata.  That means that we don't need to
921      *       search any subtable that includes flow->metadata in its mask.
922      *
923      * In either case, we always need to search any cls_subtables that do not
924      * include flow->metadata in its mask.  One way to do that would be to
925      * check the "cls_subtable"s explicitly for that, but that would require an
926      * extra branch per subtable.  Instead, we mark such a cls_subtable's
927      * 'tags' as TAG_ALL and make sure that 'tags' is never empty.  This means
928      * that 'tags' always intersects such a cls_subtable's 'tags', so we don't
929      * need a special case.
930      */
931     partition = (cmap_is_empty(&cls->partitions)
932                  ? NULL
933                  : find_partition(cls, flow->metadata,
934                                   hash_metadata(flow->metadata)));
935     tags = partition ? partition->tags : TAG_ARBITRARY;
936
937     /* Initialize trie contexts for match_find_wc(). */
938     for (int i = 0; i < cls->n_tries; i++) {
939         trie_ctx_init(&trie_ctx[i], &cls->tries[i]);
940     }
941
942     best = NULL;
943     PVECTOR_FOR_EACH_PRIORITY(subtable, best_priority, 2,
944                               sizeof(struct cls_subtable), &cls->subtables) {
945         struct cls_match *rule;
946
947         if (!tag_intersects(tags, subtable->tag)) {
948             continue;
949         }
950
951         rule = find_match_wc(subtable, flow, trie_ctx, cls->n_tries, wc);
952         if (rule && (int64_t)rule->priority > best_priority) {
953             best_priority = (int64_t)rule->priority;
954             best = rule;
955         }
956     }
957
958     return best ? best->cls_rule : NULL;
959 }
960
961 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
962  * 'match' specifies a particular value has the correct value in 'target'.
963  *
964  * 'flow' and 'mask' have the same mask! */
965 static bool
966 miniflow_and_mask_matches_miniflow(const struct miniflow *flow,
967                                    const struct minimask *mask,
968                                    const struct miniflow *target)
969 {
970     const uint32_t *flowp = miniflow_get_u32_values(flow);
971     const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
972     uint32_t target_u32;
973
974     MINIFLOW_FOR_EACH_IN_MAP(target_u32, target, mask->masks.map) {
975         if ((*flowp++ ^ target_u32) & *maskp++) {
976             return false;
977         }
978     }
979
980     return true;
981 }
982
983 static inline struct cls_match *
984 find_match_miniflow(const struct cls_subtable *subtable,
985                     const struct miniflow *flow,
986                     uint32_t hash)
987 {
988     struct cls_match *rule;
989
990     CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
991         if (miniflow_and_mask_matches_miniflow(&rule->flow, &subtable->mask,
992                                                flow)) {
993             return rule;
994         }
995     }
996
997     return NULL;
998 }
999
1000 /* For each miniflow in 'flows' performs a classifier lookup writing the result
1001  * into the corresponding slot in 'rules'.  If a particular entry in 'flows' is
1002  * NULL it is skipped.
1003  *
1004  * This function is optimized for use in the userspace datapath and therefore
1005  * does not implement a lot of features available in the standard
1006  * classifier_lookup() function.  Specifically, it does not implement
1007  * priorities, instead returning any rule which matches the flow. */
1008 void
1009 classifier_lookup_miniflow_batch(const struct classifier *cls,
1010                                  const struct miniflow **flows,
1011                                  struct cls_rule **rules, size_t len)
1012 {
1013     struct cls_subtable *subtable;
1014     size_t i, begin = 0;
1015
1016     memset(rules, 0, len * sizeof *rules);
1017     PVECTOR_FOR_EACH (subtable, &cls->subtables) {
1018         for (i = begin; i < len; i++) {
1019             struct cls_match *match;
1020             uint32_t hash;
1021
1022             if (OVS_UNLIKELY(rules[i] || !flows[i])) {
1023                 continue;
1024             }
1025
1026             hash = miniflow_hash_in_minimask(flows[i], &subtable->mask, 0);
1027             match = find_match_miniflow(subtable, flows[i], hash);
1028             if (OVS_UNLIKELY(match)) {
1029                 rules[i] = match->cls_rule;
1030             }
1031         }
1032
1033         while (begin < len && (rules[begin] || !flows[begin])) {
1034             begin++;
1035         }
1036         if (begin >= len) {
1037             break;
1038         }
1039     }
1040 }
1041
1042 /* Finds and returns a rule in 'cls' with exactly the same priority and
1043  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
1044  * contain an exact match. */
1045 struct cls_rule *
1046 classifier_find_rule_exactly(const struct classifier *cls,
1047                              const struct cls_rule *target)
1048     OVS_EXCLUDED(cls->mutex)
1049 {
1050     struct cls_match *head, *rule;
1051     struct cls_subtable *subtable;
1052
1053     ovs_mutex_lock(&cls->mutex);
1054     subtable = find_subtable(cls, &target->match.mask);
1055     if (!subtable) {
1056         goto out;
1057     }
1058
1059     /* Skip if there is no hope. */
1060     if (target->priority > subtable->max_priority) {
1061         goto out;
1062     }
1063
1064     head = find_equal(subtable, &target->match.flow,
1065                       miniflow_hash_in_minimask(&target->match.flow,
1066                                                 &target->match.mask, 0));
1067     FOR_EACH_RULE_IN_LIST (rule, head) {
1068         if (target->priority >= rule->priority) {
1069             ovs_mutex_unlock(&cls->mutex);
1070             return target->priority == rule->priority ? rule->cls_rule : NULL;
1071         }
1072     }
1073 out:
1074     ovs_mutex_unlock(&cls->mutex);
1075     return NULL;
1076 }
1077
1078 /* Finds and returns a rule in 'cls' with priority 'priority' and exactly the
1079  * same matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
1080  * contain an exact match. */
1081 struct cls_rule *
1082 classifier_find_match_exactly(const struct classifier *cls,
1083                               const struct match *target,
1084                               unsigned int priority)
1085 {
1086     struct cls_rule *retval;
1087     struct cls_rule cr;
1088
1089     cls_rule_init(&cr, target, priority);
1090     retval = classifier_find_rule_exactly(cls, &cr);
1091     cls_rule_destroy(&cr);
1092
1093     return retval;
1094 }
1095
1096 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
1097  * considered to overlap if both rules have the same priority and a packet
1098  * could match both. */
1099 bool
1100 classifier_rule_overlaps(const struct classifier *cls,
1101                          const struct cls_rule *target)
1102     OVS_EXCLUDED(cls->mutex)
1103 {
1104     struct cls_subtable *subtable;
1105     int64_t stop_at_priority = (int64_t)target->priority - 1;
1106
1107     ovs_mutex_lock(&cls->mutex);
1108     /* Iterate subtables in the descending max priority order. */
1109     PVECTOR_FOR_EACH_PRIORITY (subtable, stop_at_priority, 2,
1110                                sizeof(struct cls_subtable), &cls->subtables) {
1111         uint32_t storage[FLOW_U32S];
1112         struct minimask mask;
1113         struct cls_match *head;
1114
1115         minimask_combine(&mask, &target->match.mask, &subtable->mask, storage);
1116         CMAP_FOR_EACH (head, cmap_node, &subtable->rules) {
1117             struct cls_match *rule;
1118
1119             FOR_EACH_RULE_IN_LIST (rule, head) {
1120                 if (rule->priority < target->priority) {
1121                     break; /* Rules in descending priority order. */
1122                 }
1123                 if (rule->priority == target->priority
1124                     && miniflow_equal_in_minimask(&target->match.flow,
1125                                                   &rule->flow, &mask)) {
1126                     ovs_mutex_unlock(&cls->mutex);
1127                     return true;
1128                 }
1129             }
1130         }
1131     }
1132
1133     ovs_mutex_unlock(&cls->mutex);
1134     return false;
1135 }
1136
1137 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
1138  * specific than 'criteria'.  That is, 'rule' matches 'criteria' and this
1139  * function returns true if, for every field:
1140  *
1141  *   - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
1142  *     field, or
1143  *
1144  *   - 'criteria' wildcards the field,
1145  *
1146  * Conversely, 'rule' does not match 'criteria' and this function returns false
1147  * if, for at least one field:
1148  *
1149  *   - 'criteria' and 'rule' specify different values for the field, or
1150  *
1151  *   - 'criteria' specifies a value for the field but 'rule' wildcards it.
1152  *
1153  * Equivalently, the truth table for whether a field matches is:
1154  *
1155  *                                     rule
1156  *
1157  *                   c         wildcard    exact
1158  *                   r        +---------+---------+
1159  *                   i   wild |   yes   |   yes   |
1160  *                   t   card |         |         |
1161  *                   e        +---------+---------+
1162  *                   r  exact |    no   |if values|
1163  *                   i        |         |are equal|
1164  *                   a        +---------+---------+
1165  *
1166  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1167  * commands and by OpenFlow 1.0 aggregate and flow stats.
1168  *
1169  * Ignores rule->priority. */
1170 bool
1171 cls_rule_is_loose_match(const struct cls_rule *rule,
1172                         const struct minimatch *criteria)
1173 {
1174     return (!minimask_has_extra(&rule->match.mask, &criteria->mask)
1175             && miniflow_equal_in_minimask(&rule->match.flow, &criteria->flow,
1176                                           &criteria->mask));
1177 }
1178 \f
1179 /* Iteration. */
1180
1181 static bool
1182 rule_matches(const struct cls_match *rule, const struct cls_rule *target)
1183 {
1184     return (!target
1185             || miniflow_equal_in_minimask(&rule->flow,
1186                                           &target->match.flow,
1187                                           &target->match.mask));
1188 }
1189
1190 static struct cls_match *
1191 search_subtable(const struct cls_subtable *subtable,
1192                 struct cls_cursor *cursor)
1193 {
1194     if (!cursor->target
1195         || !minimask_has_extra(&subtable->mask, &cursor->target->match.mask)) {
1196         struct cls_match *rule;
1197
1198         CMAP_CURSOR_FOR_EACH (rule, cmap_node, &cursor->rules,
1199                               &subtable->rules) {
1200             if (rule_matches(rule, cursor->target)) {
1201                 return rule;
1202             }
1203         }
1204     }
1205     return NULL;
1206 }
1207
1208 /* Initializes 'cursor' for iterating through rules in 'cls', and returns the
1209  * first matching cls_rule via '*pnode', or NULL if there are no matches.
1210  *
1211  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
1212  *
1213  *     - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1214  *       such that cls_rule_is_loose_match(rule, target) returns true.
1215  *
1216  * Ignores target->priority. */
1217 struct cls_cursor cls_cursor_start(const struct classifier *cls,
1218                                    const struct cls_rule *target,
1219                                    bool safe)
1220     OVS_NO_THREAD_SAFETY_ANALYSIS
1221 {
1222     struct cls_cursor cursor;
1223     struct cls_subtable *subtable;
1224
1225     cursor.safe = safe;
1226     cursor.cls = cls;
1227     cursor.target = target && !cls_rule_is_catchall(target) ? target : NULL;
1228     cursor.rule = NULL;
1229
1230     /* Find first rule. */
1231     ovs_mutex_lock(&cursor.cls->mutex);
1232     CMAP_CURSOR_FOR_EACH (subtable, cmap_node, &cursor.subtables,
1233                           &cursor.cls->subtables_map) {
1234         struct cls_match *rule = search_subtable(subtable, &cursor);
1235
1236         if (rule) {
1237             cursor.subtable = subtable;
1238             cursor.rule = rule->cls_rule;
1239             break;
1240         }
1241     }
1242
1243     /* Leave locked if requested and have a rule. */
1244     if (safe || !cursor.rule) {
1245         ovs_mutex_unlock(&cursor.cls->mutex);
1246     }
1247     return cursor;
1248 }
1249
1250 static struct cls_rule *
1251 cls_cursor_next(struct cls_cursor *cursor)
1252     OVS_NO_THREAD_SAFETY_ANALYSIS
1253 {
1254     struct cls_match *rule = cursor->rule->cls_match;
1255     const struct cls_subtable *subtable;
1256     struct cls_match *next;
1257
1258     next = next_rule_in_list__(rule);
1259     if (next->priority < rule->priority) {
1260         return next->cls_rule;
1261     }
1262
1263     /* 'next' is the head of the list, that is, the rule that is included in
1264      * the subtable's map.  (This is important when the classifier contains
1265      * rules that differ only in priority.) */
1266     rule = next;
1267     CMAP_CURSOR_FOR_EACH_CONTINUE (rule, cmap_node, &cursor->rules) {
1268         if (rule_matches(rule, cursor->target)) {
1269             return rule->cls_rule;
1270         }
1271     }
1272
1273     subtable = cursor->subtable;
1274     CMAP_CURSOR_FOR_EACH_CONTINUE (subtable, cmap_node, &cursor->subtables) {
1275         rule = search_subtable(subtable, cursor);
1276         if (rule) {
1277             cursor->subtable = subtable;
1278             return rule->cls_rule;
1279         }
1280     }
1281
1282     return NULL;
1283 }
1284
1285 /* Sets 'cursor->rule' to the next matching cls_rule in 'cursor''s iteration,
1286  * or to null if all matching rules have been visited. */
1287 void
1288 cls_cursor_advance(struct cls_cursor *cursor)
1289     OVS_NO_THREAD_SAFETY_ANALYSIS
1290 {
1291     if (cursor->safe) {
1292         ovs_mutex_lock(&cursor->cls->mutex);
1293     }
1294     cursor->rule = cls_cursor_next(cursor);
1295     if (cursor->safe || !cursor->rule) {
1296         ovs_mutex_unlock(&cursor->cls->mutex);
1297     }
1298 }
1299 \f
1300 static struct cls_subtable *
1301 find_subtable(const struct classifier *cls, const struct minimask *mask)
1302     OVS_REQUIRES(cls->mutex)
1303 {
1304     struct cls_subtable *subtable;
1305
1306     CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, minimask_hash(mask, 0),
1307                              &cls->subtables_map) {
1308         if (minimask_equal(mask, &subtable->mask)) {
1309             return subtable;
1310         }
1311     }
1312     return NULL;
1313 }
1314
1315 /* The new subtable will be visible to the readers only after this. */
1316 static struct cls_subtable *
1317 insert_subtable(struct classifier *cls, const struct minimask *mask)
1318     OVS_REQUIRES(cls->mutex)
1319 {
1320     uint32_t hash = minimask_hash(mask, 0);
1321     struct cls_subtable *subtable;
1322     int i, index = 0;
1323     struct flow_wildcards old, new;
1324     uint8_t prev;
1325     int count = count_1bits(mask->masks.map);
1326
1327     subtable = xzalloc(sizeof *subtable - sizeof mask->masks.inline_values
1328                        + MINIFLOW_VALUES_SIZE(count));
1329     cmap_init(&subtable->rules);
1330     miniflow_clone_inline(&subtable->mask.masks, &mask->masks, count);
1331
1332     /* Init indices for segmented lookup, if any. */
1333     flow_wildcards_init_catchall(&new);
1334     old = new;
1335     prev = 0;
1336     for (i = 0; i < cls->n_flow_segments; i++) {
1337         flow_wildcards_fold_minimask_range(&new, mask, prev,
1338                                            cls->flow_segments[i]);
1339         /* Add an index if it adds mask bits. */
1340         if (!flow_wildcards_equal(&new, &old)) {
1341             cmap_init(&subtable->indices[index]);
1342             subtable->index_ofs[index] = cls->flow_segments[i];
1343             index++;
1344             old = new;
1345         }
1346         prev = cls->flow_segments[i];
1347     }
1348     /* Check if the rest of the subtable's mask adds any bits,
1349      * and remove the last index if it doesn't. */
1350     if (index > 0) {
1351         flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
1352         if (flow_wildcards_equal(&new, &old)) {
1353             --index;
1354             subtable->index_ofs[index] = 0;
1355             cmap_destroy(&subtable->indices[index]);
1356         }
1357     }
1358     subtable->n_indices = index;
1359
1360     subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
1361                      ? tag_create_deterministic(hash)
1362                      : TAG_ALL);
1363
1364     for (i = 0; i < cls->n_tries; i++) {
1365         subtable->trie_plen[i] = minimask_get_prefix_len(mask,
1366                                                          cls->tries[i].field);
1367     }
1368
1369     /* Ports trie. */
1370     ovsrcu_set_hidden(&subtable->ports_trie, NULL);
1371     subtable->ports_mask_len
1372         = 32 - ctz32(ntohl(MINIFLOW_GET_BE32(&mask->masks, tp_src)));
1373
1374     cmap_insert(&cls->subtables_map, &subtable->cmap_node, hash);
1375
1376     return subtable;
1377 }
1378
1379 static void
1380 destroy_subtable(struct classifier *cls, struct cls_subtable *subtable)
1381     OVS_REQUIRES(cls->mutex)
1382 {
1383     int i;
1384
1385     pvector_remove(&cls->subtables, subtable);
1386     trie_destroy(&subtable->ports_trie);
1387
1388     for (i = 0; i < subtable->n_indices; i++) {
1389         cmap_destroy(&subtable->indices[i]);
1390     }
1391     cmap_remove(&cls->subtables_map, &subtable->cmap_node,
1392                 minimask_hash(&subtable->mask, 0));
1393     minimask_destroy(&subtable->mask);
1394     cmap_destroy(&subtable->rules);
1395     ovsrcu_postpone(free, subtable);
1396 }
1397
1398 struct range {
1399     uint8_t start;
1400     uint8_t end;
1401 };
1402
1403 static unsigned int be_get_bit_at(const ovs_be32 value[], unsigned int ofs);
1404
1405 /* Return 'true' if can skip rest of the subtable based on the prefix trie
1406  * lookup results. */
1407 static inline bool
1408 check_tries(struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1409             const unsigned int field_plen[CLS_MAX_TRIES],
1410             const struct range ofs, const struct flow *flow,
1411             struct flow_wildcards *wc)
1412 {
1413     int j;
1414
1415     /* Check if we could avoid fully unwildcarding the next level of
1416      * fields using the prefix tries.  The trie checks are done only as
1417      * needed to avoid folding in additional bits to the wildcards mask. */
1418     for (j = 0; j < n_tries; j++) {
1419         /* Is the trie field relevant for this subtable? */
1420         if (field_plen[j]) {
1421             struct trie_ctx *ctx = &trie_ctx[j];
1422             uint8_t be32ofs = ctx->be32ofs;
1423
1424             /* Is the trie field within the current range of fields? */
1425             if (be32ofs >= ofs.start && be32ofs < ofs.end) {
1426                 /* On-demand trie lookup. */
1427                 if (!ctx->lookup_done) {
1428                     memset(&ctx->match_plens, 0, sizeof ctx->match_plens);
1429                     ctx->maskbits = trie_lookup(ctx->trie, flow,
1430                                                 &ctx->match_plens);
1431                     ctx->lookup_done = true;
1432                 }
1433                 /* Possible to skip the rest of the subtable if subtable's
1434                  * prefix on the field is not included in the lookup result. */
1435                 if (!be_get_bit_at(&ctx->match_plens.be32, field_plen[j] - 1)) {
1436                     /* We want the trie lookup to never result in unwildcarding
1437                      * any bits that would not be unwildcarded otherwise.
1438                      * Since the trie is shared by the whole classifier, it is
1439                      * possible that the 'maskbits' contain bits that are
1440                      * irrelevant for the partition relevant for the current
1441                      * packet.  Hence the checks below. */
1442
1443                     /* Check that the trie result will not unwildcard more bits
1444                      * than this subtable would otherwise. */
1445                     if (ctx->maskbits <= field_plen[j]) {
1446                         /* Unwildcard the bits and skip the rest. */
1447                         mask_set_prefix_bits(wc, be32ofs, ctx->maskbits);
1448                         /* Note: Prerequisite already unwildcarded, as the only
1449                          * prerequisite of the supported trie lookup fields is
1450                          * the ethertype, which is always unwildcarded. */
1451                         return true;
1452                     }
1453                     /* Can skip if the field is already unwildcarded. */
1454                     if (mask_prefix_bits_set(wc, be32ofs, ctx->maskbits)) {
1455                         return true;
1456                     }
1457                 }
1458             }
1459         }
1460     }
1461     return false;
1462 }
1463
1464 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1465  * for which 'flow', for which 'mask' has a bit set, specifies a particular
1466  * value has the correct value in 'target'.
1467  *
1468  * This function is equivalent to miniflow_equal_flow_in_minimask(flow,
1469  * target, mask) but this is faster because of the invariant that
1470  * flow->map and mask->masks.map are the same, and that this version
1471  * takes the 'wc'. */
1472 static inline bool
1473 miniflow_and_mask_matches_flow(const struct miniflow *flow,
1474                                const struct minimask *mask,
1475                                const struct flow *target)
1476 {
1477     const uint32_t *flowp = miniflow_get_u32_values(flow);
1478     const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1479     uint32_t idx;
1480
1481     MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1482         uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & *maskp++;
1483
1484         if (diff) {
1485             return false;
1486         }
1487     }
1488
1489     return true;
1490 }
1491
1492 static inline struct cls_match *
1493 find_match(const struct cls_subtable *subtable, const struct flow *flow,
1494            uint32_t hash)
1495 {
1496     struct cls_match *rule;
1497
1498     CMAP_FOR_EACH_WITH_HASH (rule, cmap_node, hash, &subtable->rules) {
1499         if (miniflow_and_mask_matches_flow(&rule->flow, &subtable->mask,
1500                                            flow)) {
1501             return rule;
1502         }
1503     }
1504
1505     return NULL;
1506 }
1507
1508 /* Returns true if 'target' satisifies 'flow'/'mask', that is, if each bit
1509  * for which 'flow', for which 'mask' has a bit set, specifies a particular
1510  * value has the correct value in 'target'.
1511  *
1512  * This function is equivalent to miniflow_and_mask_matches_flow() but this
1513  * version fills in the mask bits in 'wc'. */
1514 static inline bool
1515 miniflow_and_mask_matches_flow_wc(const struct miniflow *flow,
1516                                   const struct minimask *mask,
1517                                   const struct flow *target,
1518                                   struct flow_wildcards *wc)
1519 {
1520     const uint32_t *flowp = miniflow_get_u32_values(flow);
1521     const uint32_t *maskp = miniflow_get_u32_values(&mask->masks);
1522     uint32_t idx;
1523
1524     MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1525         uint32_t mask = *maskp++;
1526         uint32_t diff = (*flowp++ ^ flow_u32_value(target, idx)) & mask;
1527
1528         if (diff) {
1529             /* Only unwildcard if none of the differing bits is already
1530              * exact-matched. */
1531             if (!(flow_u32_value(&wc->masks, idx) & diff)) {
1532                 /* Keep one bit of the difference. */
1533                 *flow_u32_lvalue(&wc->masks, idx) |= rightmost_1bit(diff);
1534             }
1535             return false;
1536         }
1537         /* Fill in the bits that were looked at. */
1538         *flow_u32_lvalue(&wc->masks, idx) |= mask;
1539     }
1540
1541     return true;
1542 }
1543
1544 /* Unwildcard the fields looked up so far, if any. */
1545 static void
1546 fill_range_wc(const struct cls_subtable *subtable, struct flow_wildcards *wc,
1547               uint8_t to)
1548 {
1549     if (to) {
1550         flow_wildcards_fold_minimask_range(wc, &subtable->mask, 0, to);
1551     }
1552 }
1553
1554 static struct cls_match *
1555 find_match_wc(const struct cls_subtable *subtable, const struct flow *flow,
1556               struct trie_ctx trie_ctx[CLS_MAX_TRIES], unsigned int n_tries,
1557               struct flow_wildcards *wc)
1558 {
1559     uint32_t basis = 0, hash;
1560     struct cls_match *rule;
1561     int i;
1562     struct range ofs;
1563
1564     if (OVS_UNLIKELY(!wc)) {
1565         return find_match(subtable, flow,
1566                           flow_hash_in_minimask(flow, &subtable->mask, 0));
1567     }
1568
1569     ofs.start = 0;
1570     /* Try to finish early by checking fields in segments. */
1571     for (i = 0; i < subtable->n_indices; i++) {
1572         struct cmap_node *inode;
1573
1574         ofs.end = subtable->index_ofs[i];
1575
1576         if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow,
1577                         wc)) {
1578             /* 'wc' bits for the trie field set, now unwildcard the preceding
1579              * bits used so far. */
1580             fill_range_wc(subtable, wc, ofs.start);
1581             return NULL;
1582         }
1583         hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1584                                            ofs.end, &basis);
1585         inode = cmap_find(&subtable->indices[i], hash);
1586         if (!inode) {
1587             /* No match, can stop immediately, but must fold in the bits
1588              * used in lookup so far. */
1589             fill_range_wc(subtable, wc, ofs.end);
1590             return NULL;
1591         }
1592
1593         /* If we have narrowed down to a single rule already, check whether
1594          * that rule matches.  Either way, we're done.
1595          *
1596          * (Rare) hash collisions may cause us to miss the opportunity for this
1597          * optimization. */
1598         if (!cmap_node_next(inode)) {
1599             ASSIGN_CONTAINER(rule, inode - i, index_nodes);
1600             if (miniflow_and_mask_matches_flow_wc(&rule->flow, &subtable->mask,
1601                                                   flow, wc)) {
1602                 return rule;
1603             }
1604             return NULL;
1605         }
1606         ofs.start = ofs.end;
1607     }
1608     ofs.end = FLOW_U32S;
1609     /* Trie check for the final range. */
1610     if (check_tries(trie_ctx, n_tries, subtable->trie_plen, ofs, flow, wc)) {
1611         fill_range_wc(subtable, wc, ofs.start);
1612         return NULL;
1613     }
1614     hash = flow_hash_in_minimask_range(flow, &subtable->mask, ofs.start,
1615                                        ofs.end, &basis);
1616     rule = find_match(subtable, flow, hash);
1617     if (!rule && subtable->ports_mask_len) {
1618         /* Ports are always part of the final range, if any.
1619          * No match was found for the ports.  Use the ports trie to figure out
1620          * which ports bits to unwildcard. */
1621         unsigned int mbits;
1622         ovs_be32 value, plens, mask;
1623
1624         mask = MINIFLOW_GET_BE32(&subtable->mask.masks, tp_src);
1625         value = ((OVS_FORCE ovs_be32 *)flow)[TP_PORTS_OFS32] & mask;
1626         mbits = trie_lookup_value(&subtable->ports_trie, &value, &plens, 32);
1627
1628         ((OVS_FORCE ovs_be32 *)&wc->masks)[TP_PORTS_OFS32] |=
1629             mask & htonl(~0 << (32 - mbits));
1630
1631         /* Unwildcard all bits in the mask upto the ports, as they were used
1632          * to determine there is no match. */
1633         fill_range_wc(subtable, wc, TP_PORTS_OFS32);
1634         return NULL;
1635     }
1636
1637     /* Must unwildcard all the fields, as they were looked at. */
1638     flow_wildcards_fold_minimask(wc, &subtable->mask);
1639     return rule;
1640 }
1641
1642 static struct cls_match *
1643 find_equal(struct cls_subtable *subtable, const struct miniflow *flow,
1644            uint32_t hash)
1645 {
1646     struct cls_match *head;
1647
1648     CMAP_FOR_EACH_WITH_HASH (head, cmap_node, hash, &subtable->rules) {
1649         if (miniflow_equal(&head->flow, flow)) {
1650             return head;
1651         }
1652     }
1653     return NULL;
1654 }
1655
1656 /*
1657  * As the readers are operating concurrently with the modifications, a
1658  * concurrent reader may or may not see the new rule, depending on how
1659  * the concurrent events overlap with each other.  This is no
1660  * different from the former locked behavior, but there the visibility
1661  * of the new rule only depended on the timing of the locking
1662  * functions.
1663  *
1664  * The new rule is first added to the segment indices, so the readers
1665  * may find the rule in the indices before the rule is visible in the
1666  * subtables 'rules' map.  This may result in us losing the
1667  * opportunity to quit lookups earlier, resulting in sub-optimal
1668  * wildcarding.  This will be fixed by forthcoming revalidation always
1669  * scheduled after flow table changes.
1670  *
1671  * Similar behavior may happen due to us removing the overlapping rule
1672  * (if any) from the indices only after the new rule has been added.
1673  *
1674  * The subtable's max priority is updated only after the rule is
1675  * inserted, so the concurrent readers may not see the rule, as the
1676  * updated priority ordered subtable list will only be visible after
1677  * the subtable's max priority is updated.
1678  *
1679  * Similarly, the classifier's partitions for new rules are updated by
1680  * the caller after this function, so the readers may keep skipping
1681  * the subtable until they see the updated partitions.
1682  */
1683 static struct cls_match *
1684 insert_rule(struct classifier *cls, struct cls_subtable *subtable,
1685             struct cls_rule *new_rule)
1686     OVS_REQUIRES(cls->mutex)
1687 {
1688     struct cls_match *old = NULL;
1689     struct cls_match *new = cls_match_alloc(new_rule);
1690     struct cls_match *head;
1691     int i;
1692     uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES];
1693     uint8_t prev_be32ofs = 0;
1694
1695     /* Add new node to segment indices. */
1696     for (i = 0; i < subtable->n_indices; i++) {
1697         ihash[i] = minimatch_hash_range(&new_rule->match, prev_be32ofs,
1698                                         subtable->index_ofs[i], &basis);
1699         cmap_insert(&subtable->indices[i], &new->index_nodes[i], ihash[i]);
1700         prev_be32ofs = subtable->index_ofs[i];
1701     }
1702     hash = minimatch_hash_range(&new_rule->match, prev_be32ofs, FLOW_U32S,
1703                                 &basis);
1704     head = find_equal(subtable, &new_rule->match.flow, hash);
1705     if (!head) {
1706         cmap_insert(&subtable->rules, &new->cmap_node, hash);
1707         list_init(&new->list);
1708         goto out;
1709     } else {
1710         /* Scan the list for the insertion point that will keep the list in
1711          * order of decreasing priority. */
1712         struct cls_match *rule;
1713
1714         FOR_EACH_RULE_IN_LIST (rule, head) {
1715             if (new->priority >= rule->priority) {
1716                 if (rule == head) {
1717                     /* 'new' is the new highest-priority flow in the list. */
1718                     cmap_replace(&subtable->rules, &rule->cmap_node,
1719                                  &new->cmap_node, hash);
1720                 }
1721
1722                 if (new->priority == rule->priority) {
1723                     list_replace(&new->list, &rule->list);
1724                     old = rule;
1725                 } else {
1726                     list_insert(&rule->list, &new->list);
1727                 }
1728                 goto out;
1729             }
1730         }
1731
1732         /* Insert 'new' at the end of the list. */
1733         list_push_back(&head->list, &new->list);
1734     }
1735
1736  out:
1737     if (!old) {
1738         subtable->n_rules++;
1739
1740         /* Rule was added, not replaced.  Update 'subtable's 'max_priority'
1741          * and 'max_count', if necessary. */
1742         if (subtable->n_rules == 1) {
1743             subtable->max_priority = new->priority;
1744             subtable->max_count = 1;
1745             pvector_insert(&cls->subtables, subtable, new->priority);
1746         } else if (subtable->max_priority == new->priority) {
1747             ++subtable->max_count;
1748         } else if (new->priority > subtable->max_priority) {
1749             subtable->max_priority = new->priority;
1750             subtable->max_count = 1;
1751             pvector_change_priority(&cls->subtables, subtable, new->priority);
1752         }
1753     } else {
1754         /* Remove old node from indices. */
1755         for (i = 0; i < subtable->n_indices; i++) {
1756             cmap_remove(&subtable->indices[i], &old->index_nodes[i], ihash[i]);
1757         }
1758     }
1759     return old;
1760 }
1761
1762 static struct cls_match *
1763 next_rule_in_list__(struct cls_match *rule)
1764     OVS_NO_THREAD_SAFETY_ANALYSIS
1765 {
1766     struct cls_match *next = OBJECT_CONTAINING(rule->list.next, next, list);
1767     return next;
1768 }
1769
1770 static struct cls_match *
1771 next_rule_in_list(struct cls_match *rule)
1772 {
1773     struct cls_match *next = next_rule_in_list__(rule);
1774     return next->priority < rule->priority ? next : NULL;
1775 }
1776 \f
1777 /* A longest-prefix match tree. */
1778 struct trie_node {
1779     uint32_t prefix;           /* Prefix bits for this node, MSB first. */
1780     uint8_t  n_bits;           /* Never zero, except for the root node. */
1781     unsigned int n_rules;      /* Number of rules that have this prefix. */
1782     rcu_trie_ptr edges[2];     /* Both NULL if leaf. */
1783 };
1784
1785 /* Max bits per node.  Must fit in struct trie_node's 'prefix'.
1786  * Also tested with 16, 8, and 5 to stress the implementation. */
1787 #define TRIE_PREFIX_BITS 32
1788
1789 /* Return at least 'plen' bits of the 'prefix', starting at bit offset 'ofs'.
1790  * Prefixes are in the network byte order, and the offset 0 corresponds to
1791  * the most significant bit of the first byte.  The offset can be read as
1792  * "how many bits to skip from the start of the prefix starting at 'pr'". */
1793 static uint32_t
1794 raw_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1795 {
1796     uint32_t prefix;
1797
1798     pr += ofs / 32; /* Where to start. */
1799     ofs %= 32;      /* How many bits to skip at 'pr'. */
1800
1801     prefix = ntohl(*pr) << ofs; /* Get the first 32 - ofs bits. */
1802     if (plen > 32 - ofs) {      /* Need more than we have already? */
1803         prefix |= ntohl(*++pr) >> (32 - ofs);
1804     }
1805     /* Return with possible unwanted bits at the end. */
1806     return prefix;
1807 }
1808
1809 /* Return min(TRIE_PREFIX_BITS, plen) bits of the 'prefix', starting at bit
1810  * offset 'ofs'.  Prefixes are in the network byte order, and the offset 0
1811  * corresponds to the most significant bit of the first byte.  The offset can
1812  * be read as "how many bits to skip from the start of the prefix starting at
1813  * 'pr'". */
1814 static uint32_t
1815 trie_get_prefix(const ovs_be32 pr[], unsigned int ofs, unsigned int plen)
1816 {
1817     if (!plen) {
1818         return 0;
1819     }
1820     if (plen > TRIE_PREFIX_BITS) {
1821         plen = TRIE_PREFIX_BITS; /* Get at most TRIE_PREFIX_BITS. */
1822     }
1823     /* Return with unwanted bits cleared. */
1824     return raw_get_prefix(pr, ofs, plen) & ~0u << (32 - plen);
1825 }
1826
1827 /* Return the number of equal bits in 'n_bits' of 'prefix's MSBs and a 'value'
1828  * starting at "MSB 0"-based offset 'ofs'. */
1829 static unsigned int
1830 prefix_equal_bits(uint32_t prefix, unsigned int n_bits, const ovs_be32 value[],
1831                   unsigned int ofs)
1832 {
1833     uint64_t diff = prefix ^ raw_get_prefix(value, ofs, n_bits);
1834     /* Set the bit after the relevant bits to limit the result. */
1835     return raw_clz64(diff << 32 | UINT64_C(1) << (63 - n_bits));
1836 }
1837
1838 /* Return the number of equal bits in 'node' prefix and a 'prefix' of length
1839  * 'plen', starting at "MSB 0"-based offset 'ofs'. */
1840 static unsigned int
1841 trie_prefix_equal_bits(const struct trie_node *node, const ovs_be32 prefix[],
1842                        unsigned int ofs, unsigned int plen)
1843 {
1844     return prefix_equal_bits(node->prefix, MIN(node->n_bits, plen - ofs),
1845                              prefix, ofs);
1846 }
1847
1848 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int.  'ofs' can
1849  * be greater than 31. */
1850 static unsigned int
1851 be_get_bit_at(const ovs_be32 value[], unsigned int ofs)
1852 {
1853     return (((const uint8_t *)value)[ofs / 8] >> (7 - ofs % 8)) & 1u;
1854 }
1855
1856 /* Return the bit at ("MSB 0"-based) offset 'ofs' as an int.  'ofs' must
1857  * be between 0 and 31, inclusive. */
1858 static unsigned int
1859 get_bit_at(const uint32_t prefix, unsigned int ofs)
1860 {
1861     return (prefix >> (31 - ofs)) & 1u;
1862 }
1863
1864 /* Create new branch. */
1865 static struct trie_node *
1866 trie_branch_create(const ovs_be32 *prefix, unsigned int ofs, unsigned int plen,
1867                    unsigned int n_rules)
1868 {
1869     struct trie_node *node = xmalloc(sizeof *node);
1870
1871     node->prefix = trie_get_prefix(prefix, ofs, plen);
1872
1873     if (plen <= TRIE_PREFIX_BITS) {
1874         node->n_bits = plen;
1875         ovsrcu_set_hidden(&node->edges[0], NULL);
1876         ovsrcu_set_hidden(&node->edges[1], NULL);
1877         node->n_rules = n_rules;
1878     } else { /* Need intermediate nodes. */
1879         struct trie_node *subnode = trie_branch_create(prefix,
1880                                                        ofs + TRIE_PREFIX_BITS,
1881                                                        plen - TRIE_PREFIX_BITS,
1882                                                        n_rules);
1883         int bit = get_bit_at(subnode->prefix, 0);
1884         node->n_bits = TRIE_PREFIX_BITS;
1885         ovsrcu_set_hidden(&node->edges[bit], subnode);
1886         ovsrcu_set_hidden(&node->edges[!bit], NULL);
1887         node->n_rules = 0;
1888     }
1889     return node;
1890 }
1891
1892 static void
1893 trie_node_destroy(const struct trie_node *node)
1894 {
1895     ovsrcu_postpone(free, CONST_CAST(struct trie_node *, node));
1896 }
1897
1898 /* Copy a trie node for modification and postpone delete the old one. */
1899 static struct trie_node *
1900 trie_node_rcu_realloc(const struct trie_node *node)
1901 {
1902     struct trie_node *new_node = xmalloc(sizeof *node);
1903
1904     *new_node = *node;
1905     trie_node_destroy(node);
1906
1907     return new_node;
1908 }
1909
1910 /* May only be called while holding the classifier mutex. */
1911 static void
1912 trie_destroy(rcu_trie_ptr *trie)
1913 {
1914     struct trie_node *node = ovsrcu_get_protected(struct trie_node *, trie);
1915
1916     if (node) {
1917         ovsrcu_set_hidden(trie, NULL);
1918         trie_destroy(&node->edges[0]);
1919         trie_destroy(&node->edges[1]);
1920         trie_node_destroy(node);
1921     }
1922 }
1923
1924 static bool
1925 trie_is_leaf(const struct trie_node *trie)
1926 {
1927     /* No children? */
1928     return !ovsrcu_get(struct trie_node *, &trie->edges[0])
1929         && !ovsrcu_get(struct trie_node *, &trie->edges[1]);
1930 }
1931
1932 static void
1933 mask_set_prefix_bits(struct flow_wildcards *wc, uint8_t be32ofs,
1934                      unsigned int n_bits)
1935 {
1936     ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1937     unsigned int i;
1938
1939     for (i = 0; i < n_bits / 32; i++) {
1940         mask[i] = OVS_BE32_MAX;
1941     }
1942     if (n_bits % 32) {
1943         mask[i] |= htonl(~0u << (32 - n_bits % 32));
1944     }
1945 }
1946
1947 static bool
1948 mask_prefix_bits_set(const struct flow_wildcards *wc, uint8_t be32ofs,
1949                      unsigned int n_bits)
1950 {
1951     ovs_be32 *mask = &((ovs_be32 *)&wc->masks)[be32ofs];
1952     unsigned int i;
1953     ovs_be32 zeroes = 0;
1954
1955     for (i = 0; i < n_bits / 32; i++) {
1956         zeroes |= ~mask[i];
1957     }
1958     if (n_bits % 32) {
1959         zeroes |= ~mask[i] & htonl(~0u << (32 - n_bits % 32));
1960     }
1961
1962     return !zeroes; /* All 'n_bits' bits set. */
1963 }
1964
1965 static rcu_trie_ptr *
1966 trie_next_edge(struct trie_node *node, const ovs_be32 value[],
1967                unsigned int ofs)
1968 {
1969     return node->edges + be_get_bit_at(value, ofs);
1970 }
1971
1972 static const struct trie_node *
1973 trie_next_node(const struct trie_node *node, const ovs_be32 value[],
1974                unsigned int ofs)
1975 {
1976     return ovsrcu_get(struct trie_node *,
1977                       &node->edges[be_get_bit_at(value, ofs)]);
1978 }
1979
1980 /* Set the bit at ("MSB 0"-based) offset 'ofs'.  'ofs' can be greater than 31.
1981  */
1982 static void
1983 be_set_bit_at(ovs_be32 value[], unsigned int ofs)
1984 {
1985     ((uint8_t *)value)[ofs / 8] |= 1u << (7 - ofs % 8);
1986 }
1987
1988 /* Returns the number of bits in the prefix mask necessary to determine a
1989  * mismatch, in case there are longer prefixes in the tree below the one that
1990  * matched.
1991  * '*plens' will have a bit set for each prefix length that may have matching
1992  * rules.  The caller is responsible for clearing the '*plens' prior to
1993  * calling this.
1994  */
1995 static unsigned int
1996 trie_lookup_value(const rcu_trie_ptr *trie, const ovs_be32 value[],
1997                   ovs_be32 plens[], unsigned int n_bits)
1998 {
1999     const struct trie_node *prev = NULL;
2000     const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
2001     unsigned int match_len = 0; /* Number of matching bits. */
2002
2003     for (; node; prev = node, node = trie_next_node(node, value, match_len)) {
2004         unsigned int eqbits;
2005         /* Check if this edge can be followed. */
2006         eqbits = prefix_equal_bits(node->prefix, node->n_bits, value,
2007                                    match_len);
2008         match_len += eqbits;
2009         if (eqbits < node->n_bits) { /* Mismatch, nothing more to be found. */
2010             /* Bit at offset 'match_len' differed. */
2011             return match_len + 1; /* Includes the first mismatching bit. */
2012         }
2013         /* Full match, check if rules exist at this prefix length. */
2014         if (node->n_rules > 0) {
2015             be_set_bit_at(plens, match_len - 1);
2016         }
2017         if (match_len >= n_bits) {
2018             return n_bits; /* Full prefix. */
2019         }
2020     }
2021     /* node == NULL.  Full match so far, but we tried to follow an
2022      * non-existing branch.  Need to exclude the other branch if it exists
2023      * (it does not if we were called on an empty trie or 'prev' is a leaf
2024      * node). */
2025     return !prev || trie_is_leaf(prev) ? match_len : match_len + 1;
2026 }
2027
2028 static unsigned int
2029 trie_lookup(const struct cls_trie *trie, const struct flow *flow,
2030             union mf_value *plens)
2031 {
2032     const struct mf_field *mf = trie->field;
2033
2034     /* Check that current flow matches the prerequisites for the trie
2035      * field.  Some match fields are used for multiple purposes, so we
2036      * must check that the trie is relevant for this flow. */
2037     if (mf_are_prereqs_ok(mf, flow)) {
2038         return trie_lookup_value(&trie->root,
2039                                  &((ovs_be32 *)flow)[mf->flow_be32ofs],
2040                                  &plens->be32, mf->n_bits);
2041     }
2042     memset(plens, 0xff, sizeof *plens); /* All prefixes, no skipping. */
2043     return 0; /* Value not used in this case. */
2044 }
2045
2046 /* Returns the length of a prefix match mask for the field 'mf' in 'minimask'.
2047  * Returns the u32 offset to the miniflow data in '*miniflow_index', if
2048  * 'miniflow_index' is not NULL. */
2049 static unsigned int
2050 minimask_get_prefix_len(const struct minimask *minimask,
2051                         const struct mf_field *mf)
2052 {
2053     unsigned int n_bits = 0, mask_tz = 0; /* Non-zero when end of mask seen. */
2054     uint8_t u32_ofs = mf->flow_be32ofs;
2055     uint8_t u32_end = u32_ofs + mf->n_bytes / 4;
2056
2057     for (; u32_ofs < u32_end; ++u32_ofs) {
2058         uint32_t mask;
2059         mask = ntohl((OVS_FORCE ovs_be32)minimask_get(minimask, u32_ofs));
2060
2061         /* Validate mask, count the mask length. */
2062         if (mask_tz) {
2063             if (mask) {
2064                 return 0; /* No bits allowed after mask ended. */
2065             }
2066         } else {
2067             if (~mask & (~mask + 1)) {
2068                 return 0; /* Mask not contiguous. */
2069             }
2070             mask_tz = ctz32(mask);
2071             n_bits += 32 - mask_tz;
2072         }
2073     }
2074
2075     return n_bits;
2076 }
2077
2078 /*
2079  * This is called only when mask prefix is known to be CIDR and non-zero.
2080  * Relies on the fact that the flow and mask have the same map, and since
2081  * the mask is CIDR, the storage for the flow field exists even if it
2082  * happened to be zeros.
2083  */
2084 static const ovs_be32 *
2085 minimatch_get_prefix(const struct minimatch *match, const struct mf_field *mf)
2086 {
2087     return miniflow_get_be32_values(&match->flow) +
2088         count_1bits(match->flow.map & ((UINT64_C(1) << mf->flow_be32ofs) - 1));
2089 }
2090
2091 /* Insert rule in to the prefix tree.
2092  * 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2093  * in 'rule'. */
2094 static void
2095 trie_insert(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2096 {
2097     trie_insert_prefix(&trie->root,
2098                        minimatch_get_prefix(&rule->match, trie->field), mlen);
2099 }
2100
2101 static void
2102 trie_insert_prefix(rcu_trie_ptr *edge, const ovs_be32 *prefix, int mlen)
2103 {
2104     struct trie_node *node;
2105     int ofs = 0;
2106
2107     /* Walk the tree. */
2108     for (; (node = ovsrcu_get_protected(struct trie_node *, edge));
2109          edge = trie_next_edge(node, prefix, ofs)) {
2110         unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2111         ofs += eqbits;
2112         if (eqbits < node->n_bits) {
2113             /* Mismatch, new node needs to be inserted above. */
2114             int old_branch = get_bit_at(node->prefix, eqbits);
2115             struct trie_node *new_parent;
2116
2117             new_parent = trie_branch_create(prefix, ofs - eqbits, eqbits,
2118                                             ofs == mlen ? 1 : 0);
2119             /* Copy the node to modify it. */
2120             node = trie_node_rcu_realloc(node);
2121             /* Adjust the new node for its new position in the tree. */
2122             node->prefix <<= eqbits;
2123             node->n_bits -= eqbits;
2124             ovsrcu_set_hidden(&new_parent->edges[old_branch], node);
2125
2126             /* Check if need a new branch for the new rule. */
2127             if (ofs < mlen) {
2128                 ovsrcu_set_hidden(&new_parent->edges[!old_branch],
2129                                   trie_branch_create(prefix, ofs, mlen - ofs,
2130                                                      1));
2131             }
2132             ovsrcu_set(edge, new_parent); /* Publish changes. */
2133             return;
2134         }
2135         /* Full match so far. */
2136
2137         if (ofs == mlen) {
2138             /* Full match at the current node, rule needs to be added here. */
2139             node->n_rules++;
2140             return;
2141         }
2142     }
2143     /* Must insert a new tree branch for the new rule. */
2144     ovsrcu_set(edge, trie_branch_create(prefix, ofs, mlen - ofs, 1));
2145 }
2146
2147 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2148  * in 'rule'. */
2149 static void
2150 trie_remove(struct cls_trie *trie, const struct cls_rule *rule, int mlen)
2151 {
2152     trie_remove_prefix(&trie->root,
2153                        minimatch_get_prefix(&rule->match, trie->field), mlen);
2154 }
2155
2156 /* 'mlen' must be the (non-zero) CIDR prefix length of the 'trie->field' mask
2157  * in 'rule'. */
2158 static void
2159 trie_remove_prefix(rcu_trie_ptr *root, const ovs_be32 *prefix, int mlen)
2160 {
2161     struct trie_node *node;
2162     rcu_trie_ptr *edges[sizeof(union mf_value) * 8];
2163     int depth = 0, ofs = 0;
2164
2165     /* Walk the tree. */
2166     for (edges[0] = root;
2167          (node = ovsrcu_get_protected(struct trie_node *, edges[depth]));
2168          edges[++depth] = trie_next_edge(node, prefix, ofs)) {
2169         unsigned int eqbits = trie_prefix_equal_bits(node, prefix, ofs, mlen);
2170
2171         if (eqbits < node->n_bits) {
2172             /* Mismatch, nothing to be removed.  This should never happen, as
2173              * only rules in the classifier are ever removed. */
2174             break; /* Log a warning. */
2175         }
2176         /* Full match so far. */
2177         ofs += eqbits;
2178
2179         if (ofs == mlen) {
2180             /* Full prefix match at the current node, remove rule here. */
2181             if (!node->n_rules) {
2182                 break; /* Log a warning. */
2183             }
2184             node->n_rules--;
2185
2186             /* Check if can prune the tree. */
2187             while (!node->n_rules) {
2188                 struct trie_node *next,
2189                     *edge0 = ovsrcu_get_protected(struct trie_node *,
2190                                                   &node->edges[0]),
2191                     *edge1 = ovsrcu_get_protected(struct trie_node *,
2192                                                   &node->edges[1]);
2193
2194                 if (edge0 && edge1) {
2195                     break; /* A branching point, cannot prune. */
2196                 }
2197
2198                 /* Else have at most one child node, remove this node. */
2199                 next = edge0 ? edge0 : edge1;
2200
2201                 if (next) {
2202                     if (node->n_bits + next->n_bits > TRIE_PREFIX_BITS) {
2203                         break;   /* Cannot combine. */
2204                     }
2205                     next = trie_node_rcu_realloc(next); /* Modify. */
2206
2207                     /* Combine node with next. */
2208                     next->prefix = node->prefix | next->prefix >> node->n_bits;
2209                     next->n_bits += node->n_bits;
2210                 }
2211                 /* Update the parent's edge. */
2212                 ovsrcu_set(edges[depth], next); /* Publish changes. */
2213                 trie_node_destroy(node);
2214
2215                 if (next || !depth) {
2216                     /* Branch not pruned or at root, nothing more to do. */
2217                     break;
2218                 }
2219                 node = ovsrcu_get_protected(struct trie_node *,
2220                                             edges[--depth]);
2221             }
2222             return;
2223         }
2224     }
2225     /* Cannot go deeper. This should never happen, since only rules
2226      * that actually exist in the classifier are ever removed. */
2227     VLOG_WARN("Trying to remove non-existing rule from a prefix trie.");
2228 }