]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/classifier-private.h
2 * Copyright (c) 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef CLASSIFIER_PRIVATE_H
18 #define CLASSIFIER_PRIVATE_H 1
26 /* Classifier internal definitions, subject to change at any time. */
28 /* A set of rules that all have the same fields wildcarded. */
30 struct cmap_node cmap_node
; /* Within classifier's 'subtables_map'. */
32 /* These fields are only used by writers. */
33 int max_priority
; /* Max priority of any rule in subtable. */
34 unsigned int max_count
; /* Count of max_priority rules. */
36 /* Accessed by iterators. */
37 struct rculist rules_list
; /* Unordered. */
39 /* Identical, but lower priority rules are not inserted to any of the
40 * following data structures. */
42 /* These fields are accessed by readers who care about wildcarding. */
43 const uint8_t n_indices
; /* How many indices to use. */
44 const struct flowmap index_maps
[CLS_MAX_INDICES
+ 1]; /* Stage maps. */
45 unsigned int trie_plen
[CLS_MAX_TRIES
]; /* Trie prefix length in 'mask'
46 * (runtime configurable). */
47 const int ports_mask_len
;
48 struct ccmap indices
[CLS_MAX_INDICES
]; /* Staged lookup indices. */
49 rcu_trie_ptr ports_trie
; /* NULL if none. */
51 /* These fields are accessed by all readers. */
52 struct cmap rules
; /* Contains 'cls_match'es. */
53 const struct minimask mask
; /* Wildcards for fields. */
54 /* 'mask' must be the last field. */
57 /* Internal representation of a rule in a "struct cls_subtable".
59 * The 'next' member is an element in a singly linked, null-terminated list.
60 * This list links together identical "cls_match"es in order of decreasing
61 * priority. The classifier code maintains the invariant that at most one rule
62 * of a given priority is visible for any given lookup version.
65 /* Accessed by everybody. */
66 OVSRCU_TYPE(struct cls_match
*) next
; /* Equal, lower-priority matches. */
67 OVSRCU_TYPE(struct cls_conjunction_set
*) conj_set
;
69 /* Accessed by readers interested in wildcarding. */
70 const int priority
; /* Larger numbers are higher priorities. */
72 /* Accessed by all readers. */
73 struct cmap_node cmap_node
; /* Within struct cls_subtable 'rules'. */
75 /* Rule versioning. */
76 struct versions versions
;
78 const struct cls_rule
*cls_rule
;
79 const struct miniflow flow
; /* Matching rule. Mask is in the subtable. */
80 /* 'flow' must be the last field. */
83 /* Utilities for accessing the 'cls_match' member of struct cls_rule. */
84 static inline struct cls_match
*
85 get_cls_match_protected(const struct cls_rule
*rule
)
87 return ovsrcu_get_protected(struct cls_match
*, &rule
->cls_match
);
90 static inline struct cls_match
*
91 get_cls_match(const struct cls_rule
*rule
)
93 return ovsrcu_get(struct cls_match
*, &rule
->cls_match
);
96 /* Must be RCU postponed. */
97 void cls_match_free_cb(struct cls_match
*);
100 cls_match_set_remove_version(struct cls_match
*rule
, ovs_version_t version
)
102 versions_set_remove_version(&rule
->versions
, version
);
106 cls_match_visible_in_version(const struct cls_match
*rule
,
107 ovs_version_t version
)
109 return versions_visible_in_version(&rule
->versions
, version
);
113 cls_match_is_eventually_invisible(const struct cls_match
*rule
)
115 return versions_is_eventually_invisible(&rule
->versions
);
119 /* cls_match 'next' */
121 static inline const struct cls_match
*
122 cls_match_next(const struct cls_match
*rule
)
124 return ovsrcu_get(struct cls_match
*, &rule
->next
);
127 static inline struct cls_match
*
128 cls_match_next_protected(const struct cls_match
*rule
)
130 return ovsrcu_get_protected(struct cls_match
*, &rule
->next
);
133 /* Puts 'rule' in the position between 'prev' and 'next'. If 'prev' == NULL,
134 * then the 'rule' is the new list head, and if 'next' == NULL, the rule is the
136 * If there are any nodes between 'prev' and 'next', they are dropped from the
139 cls_match_insert(struct cls_match
*prev
, struct cls_match
*next
,
140 struct cls_match
*rule
)
142 ovsrcu_set_hidden(&rule
->next
, next
);
145 ovsrcu_set(&prev
->next
, rule
);
149 /* Puts 'new_rule' in the position of 'old_rule', which is the next node after
150 * 'prev'. If 'prev' == NULL, then the 'new_rule' is the new list head.
152 * The replaced cls_match still links to the later rules, and may still be
153 * referenced by other threads until all other threads quiesce. The replaced
154 * rule may not be re-inserted, re-initialized, or deleted until after all
155 * other threads have quiesced (use ovsrcu_postpone). */
157 cls_match_replace(struct cls_match
*prev
,
158 struct cls_match
*old_rule
, struct cls_match
*new_rule
)
160 cls_match_insert(prev
, cls_match_next_protected(old_rule
), new_rule
);
163 /* Removes 'rule' following 'prev' from the list. If 'prev' is NULL, then the
164 * 'rule' is a list head, and the caller is responsible for maintaining its
165 * list head pointer (if any).
167 * Afterward, the removed rule is not linked to any more, but still links to
168 * the following rules, and may still be referenced by other threads until all
169 * other threads quiesce. The removed rule may not be re-inserted,
170 * re-initialized, or deleted until after all other threads have quiesced (use
174 cls_match_remove(struct cls_match
*prev
, struct cls_match
*rule
)
177 ovsrcu_set(&prev
->next
, cls_match_next_protected(rule
));
181 #define CLS_MATCH_FOR_EACH(ITER, HEAD) \
182 for ((ITER) = (HEAD); (ITER); (ITER) = cls_match_next(ITER))
184 #define CLS_MATCH_FOR_EACH_AFTER_HEAD(ITER, HEAD) \
185 CLS_MATCH_FOR_EACH(ITER, cls_match_next(HEAD))
187 /* Iterate cls_matches keeping the previous pointer for modifications. */
188 #define FOR_EACH_RULE_IN_LIST_PROTECTED(ITER, PREV, HEAD) \
189 for ((PREV) = NULL, (ITER) = (HEAD); \
191 (PREV) = (ITER), (ITER) = cls_match_next_protected(ITER))
194 /* A longest-prefix match tree. */
196 uint32_t prefix
; /* Prefix bits for this node, MSB first. */
197 uint8_t n_bits
; /* Never zero, except for the root node. */
198 unsigned int n_rules
; /* Number of rules that have this prefix. */
199 rcu_trie_ptr edges
[2]; /* Both NULL if leaf. */
202 /* Max bits per node. Must fit in struct trie_node's 'prefix'.
203 * Also tested with 16, 8, and 5 to stress the implementation. */
204 #define TRIE_PREFIX_BITS 32
206 /* flow/miniflow/minimask/minimatch utilities.
207 * These are only used by the classifier, so place them here to allow
208 * for better optimization. */
210 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
211 * 'mask', given 'basis'.
213 * The hash values returned by this function are the same as those returned by
214 * miniflow_hash_in_minimask(), only the form of the arguments differ. */
215 static inline uint32_t
216 flow_hash_in_minimask(const struct flow
*flow
, const struct minimask
*mask
,
219 const uint64_t *mask_values
= miniflow_get_values(&mask
->masks
);
220 const uint64_t *flow_u64
= (const uint64_t *)flow
;
221 const uint64_t *p
= mask_values
;
222 uint32_t hash
= basis
;
225 FLOWMAP_FOR_EACH_MAP (map
, mask
->masks
.map
) {
228 MAP_FOR_EACH_INDEX (idx
, map
) {
229 hash
= hash_add64(hash
, flow_u64
[idx
] & *p
++);
231 flow_u64
+= MAP_T_BITS
;
234 return hash_finish(hash
, (p
- mask_values
) * 8);
237 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
238 * 'mask', given 'basis'.
240 * The hash values returned by this function are the same as those returned by
241 * flow_hash_in_minimask(), only the form of the arguments differ. */
242 static inline uint32_t
243 miniflow_hash_in_minimask(const struct miniflow
*flow
,
244 const struct minimask
*mask
, uint32_t basis
)
246 const uint64_t *mask_values
= miniflow_get_values(&mask
->masks
);
247 const uint64_t *p
= mask_values
;
248 uint32_t hash
= basis
;
251 MINIFLOW_FOR_EACH_IN_FLOWMAP(value
, flow
, mask
->masks
.map
) {
252 hash
= hash_add64(hash
, value
& *p
++);
255 return hash_finish(hash
, (p
- mask_values
) * 8);
258 /* Returns a hash value for the values of 'flow', indicated by 'range', where
259 * there are 1-bits in 'mask', given 'basis'. 'range' must be a continuous
260 * subset of the bits in 'mask''s map, representing a continuous range of the
261 * minimask's mask data. '*offset' must be the number of 64-bit units of the
262 * minimask's data to skip to get to the first unit covered by 'range'. On
263 * return '*offset' is updated with the number of 64-bit units of the minimask
266 * Typically this function is called for successive ranges of minimask's masks,
267 * and the first invocation passes '*offset' as zero.
269 * The hash values returned by this function are the same as those returned by
270 * minimatch_hash_range(), only the form of the arguments differ. */
271 static inline uint32_t
272 flow_hash_in_minimask_range(const struct flow
*flow
,
273 const struct minimask
*mask
,
274 const struct flowmap range
,
275 unsigned int *offset
,
278 const uint64_t *mask_values
= miniflow_get_values(&mask
->masks
);
279 const uint64_t *flow_u64
= (const uint64_t *)flow
;
280 const uint64_t *p
= mask_values
+ *offset
;
281 uint32_t hash
= *basis
;
284 FLOWMAP_FOR_EACH_MAP (map
, range
) {
287 MAP_FOR_EACH_INDEX (idx
, map
) {
288 hash
= hash_add64(hash
, flow_u64
[idx
] & *p
++);
290 flow_u64
+= MAP_T_BITS
;
293 *basis
= hash
; /* Allow continuation from the unfinished value. */
294 *offset
= p
- mask_values
;
295 return hash_finish(hash
, *offset
* 8);
298 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
300 flow_wildcards_fold_minimask(struct flow_wildcards
*wc
,
301 const struct minimask
*mask
)
303 flow_union_with_miniflow(&wc
->masks
, &mask
->masks
);
306 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask for bits in
307 * 'fmap'. 1-bits in 'fmap' are a subset of 1-bits in 'mask''s map. */
309 flow_wildcards_fold_minimask_in_map(struct flow_wildcards
*wc
,
310 const struct minimask
*mask
,
311 const struct flowmap fmap
)
313 flow_union_with_miniflow_subset(&wc
->masks
, &mask
->masks
, fmap
);
316 /* Returns a hash value for 'mask', given 'basis'. */
317 static inline uint32_t
318 minimask_hash(const struct minimask
*mask
, uint32_t basis
)
320 const uint64_t *p
= miniflow_get_values(&mask
->masks
);
321 size_t n_values
= miniflow_n_values(&mask
->masks
);
322 uint32_t hash
= basis
;
324 for (size_t i
= 0; i
< n_values
; i
++) {
325 hash
= hash_add64(hash
, *p
++);
329 FLOWMAP_FOR_EACH_MAP (map
, mask
->masks
.map
) {
330 hash
= hash_add64(hash
, map
);
333 return hash_finish(hash
, n_values
);
336 /* Returns a hash value for the values of 'match->flow', indicated by 'range',
337 * where there are 1-bits in 'match->mask', given 'basis'. 'range' must be a
338 * continuous subset of the bits in the map of 'match', representing a
339 * continuous range of the mask data of 'match'. '*offset' must be the number
340 * of 64-bit units of the match data to skip to get to the first unit covered
341 * by 'range'. On return '*offset' is updated with the number of 64-bit units
342 * of the match consumed.
344 * Typically this function is called for successive ranges of minimask's masks,
345 * and the first invocation passes '*offset' as zero.
347 * The hash values returned by this function are the same as those returned by
348 * flow_hash_in_minimask_range(), only the form of the arguments differ. */
349 static inline uint32_t
350 minimatch_hash_range(const struct minimatch
*match
,
351 const struct flowmap range
, unsigned int *offset
,
354 const uint64_t *p
= miniflow_get_values(match
->flow
) + *offset
;
355 const uint64_t *q
= miniflow_get_values(&match
->mask
->masks
) + *offset
;
356 unsigned int n
= flowmap_n_1bits(range
);
357 uint32_t hash
= *basis
;
359 for (unsigned int i
= 0; i
< n
; i
++) {
360 hash
= hash_add64(hash
, p
[i
] & q
[i
]);
362 *basis
= hash
; /* Allow continuation from the unfinished value. */
364 return hash_finish(hash
, *offset
* 8);