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