2 * Copyright (c) 2014 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 /* The fields are only used by writers and iterators. */
31 struct cmap_node cmap_node; /* Within struct classifier 'subtables_map'. */
33 /* The fields are only used by writers. */
34 int max_priority OVS_GUARDED; /* Max priority of any rule in subtable. */
35 unsigned int max_count OVS_GUARDED; /* Count of max_priority rules. */
37 /* Identical, but lower priority rules are not inserted to any of the
38 * following data structures. */
40 /* These fields are accessed by readers who care about wildcarding. */
41 const tag_type tag; /* Tag generated from mask for partitioning. */
42 const uint8_t n_indices; /* How many indices to use. */
43 const uint8_t index_ofs[CLS_MAX_INDICES]; /* u32 segment boundaries. */
44 unsigned int trie_plen[CLS_MAX_TRIES]; /* Trie prefix length in 'mask'
45 * (runtime configurable). */
46 const int ports_mask_len;
47 struct cmap indices[CLS_MAX_INDICES]; /* Staged lookup indices. */
48 rcu_trie_ptr ports_trie; /* NULL if none. */
50 /* These fields are accessed by all readers. */
51 struct cmap rules; /* Contains 'cls_match'es. */
52 const struct minimask mask; /* Wildcards for fields. */
53 /* 'mask' must be the last field. */
56 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
57 * field) with tags for the "cls_subtable"s that contain rules that match that
59 struct cls_partition {
60 struct cmap_node cmap_node; /* In struct classifier's 'partitions' map. */
61 ovs_be64 metadata; /* metadata value for this partition. */
62 tag_type tags; /* OR of each flow's cls_subtable tag. */
63 struct tag_tracker tracker OVS_GUARDED; /* Tracks the bits in 'tags'. */
66 /* Internal representation of a rule in a "struct cls_subtable". */
68 /* Accessed by everybody. */
69 struct rculist list OVS_GUARDED; /* Identical, lower-priority rules. */
71 /* Accessed only by writers. */
72 struct cls_partition *partition OVS_GUARDED;
74 /* Accessed by readers interested in wildcarding. */
75 const int priority; /* Larger numbers are higher priorities. */
76 struct cmap_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
78 /* Accessed by all readers. */
79 struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */
80 const struct cls_rule *cls_rule;
81 const struct miniflow flow; /* Matching rule. Mask is in the subtable. */
82 /* 'flow' must be the last field. */
85 /* A longest-prefix match tree. */
87 uint32_t prefix; /* Prefix bits for this node, MSB first. */
88 uint8_t n_bits; /* Never zero, except for the root node. */
89 unsigned int n_rules; /* Number of rules that have this prefix. */
90 rcu_trie_ptr edges[2]; /* Both NULL if leaf. */
93 /* Max bits per node. Must fit in struct trie_node's 'prefix'.
94 * Also tested with 16, 8, and 5 to stress the implementation. */
95 #define TRIE_PREFIX_BITS 32
97 /* flow/miniflow/minimask/minimatch utilities.
98 * These are only used by the classifier, so place them here to allow
99 * for better optimization. */
101 static inline uint64_t
102 miniflow_get_map_in_range(const struct miniflow *miniflow,
103 uint8_t start, uint8_t end, unsigned int *offset)
105 uint64_t map = miniflow->map;
109 uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
110 *offset = count_1bits(map & msk);
113 if (end < FLOW_U32S) {
114 uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
120 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
121 * 'mask', given 'basis'.
123 * The hash values returned by this function are the same as those returned by
124 * miniflow_hash_in_minimask(), only the form of the arguments differ. */
125 static inline uint32_t
126 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
129 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
130 const uint32_t *flow_u32 = (const uint32_t *)flow;
131 const uint32_t *p = mask_values;
136 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
137 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
140 return hash_finish(hash, (p - mask_values) * 4);
143 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
144 * 'mask', given 'basis'.
146 * The hash values returned by this function are the same as those returned by
147 * flow_hash_in_minimask(), only the form of the arguments differ. */
148 static inline uint32_t
149 miniflow_hash_in_minimask(const struct miniflow *flow,
150 const struct minimask *mask, uint32_t basis)
152 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
153 const uint32_t *p = mask_values;
154 uint32_t hash = basis;
157 MINIFLOW_FOR_EACH_IN_MAP(flow_u32, flow, mask->masks.map) {
158 hash = hash_add(hash, flow_u32 & *p++);
161 return hash_finish(hash, (p - mask_values) * 4);
164 /* Returns a hash value for the bits of range [start, end) in 'flow',
165 * where there are 1-bits in 'mask', given 'hash'.
167 * The hash values returned by this function are the same as those returned by
168 * minimatch_hash_range(), only the form of the arguments differ. */
169 static inline uint32_t
170 flow_hash_in_minimask_range(const struct flow *flow,
171 const struct minimask *mask,
172 uint8_t start, uint8_t end, uint32_t *basis)
174 const uint32_t *mask_values = miniflow_get_u32_values(&mask->masks);
175 const uint32_t *flow_u32 = (const uint32_t *)flow;
177 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
179 const uint32_t *p = mask_values + offset;
180 uint32_t hash = *basis;
182 for (; map; map = zero_rightmost_1bit(map)) {
183 hash = hash_add(hash, flow_u32[raw_ctz(map)] & *p++);
186 *basis = hash; /* Allow continuation from the unfinished value. */
187 return hash_finish(hash, (p - mask_values) * 4);
190 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
192 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
193 const struct minimask *mask)
195 flow_union_with_miniflow(&wc->masks, &mask->masks);
198 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
199 * in range [start, end). */
201 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
202 const struct minimask *mask,
203 uint8_t start, uint8_t end)
205 uint32_t *dst_u32 = (uint32_t *)&wc->masks;
207 uint64_t map = miniflow_get_map_in_range(&mask->masks, start, end,
209 const uint32_t *p = miniflow_get_u32_values(&mask->masks) + offset;
211 for (; map; map = zero_rightmost_1bit(map)) {
212 dst_u32[raw_ctz(map)] |= *p++;
216 /* Returns a hash value for 'flow', given 'basis'. */
217 static inline uint32_t
218 miniflow_hash(const struct miniflow *flow, uint32_t basis)
220 const uint32_t *values = miniflow_get_u32_values(flow);
221 const uint32_t *p = values;
222 uint32_t hash = basis;
223 uint64_t hash_map = 0;
226 for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
228 hash = hash_add(hash, *p);
229 hash_map |= rightmost_1bit(map);
233 hash = hash_add(hash, hash_map);
234 hash = hash_add(hash, hash_map >> 32);
236 return hash_finish(hash, p - values);
239 /* Returns a hash value for 'mask', given 'basis'. */
240 static inline uint32_t
241 minimask_hash(const struct minimask *mask, uint32_t basis)
243 return miniflow_hash(&mask->masks, basis);
246 /* Returns a hash value for 'match', given 'basis'. */
247 static inline uint32_t
248 minimatch_hash(const struct minimatch *match, uint32_t basis)
250 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
253 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
256 * The hash values returned by this function are the same as those returned by
257 * flow_hash_in_minimask_range(), only the form of the arguments differ. */
258 static inline uint32_t
259 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
263 const uint32_t *p, *q;
264 uint32_t hash = *basis;
267 n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
269 q = miniflow_get_u32_values(&match->mask.masks) + offset;
270 p = miniflow_get_u32_values(&match->flow) + offset;
272 for (i = 0; i < n; i++) {
273 hash = hash_add(hash, p[i] & q[i]);
275 *basis = hash; /* Allow continuation from the unfinished value. */
276 return hash_finish(hash, (offset + n) * 4);