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