4eed9e42f04a6b84cbba2867340ff5b08adb9a9c
[cascardo/ovs.git] / lib / classifier-private.h
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef CLASSIFIER_PRIVATE_H
18 #define CLASSIFIER_PRIVATE_H 1
19
20 #include "cmap.h"
21 #include "flow.h"
22 #include "hash.h"
23 #include "rculist.h"
24 #include "tag.h"
25
26 /* Classifier internal definitions, subject to change at any time. */
27
28 /* A set of rules that all have the same fields wildcarded. */
29 struct cls_subtable {
30     struct cmap_node cmap_node;    /* Within classifier's 'subtables_map'. */
31
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. */
35
36     /* Accessed by iterators. */
37     struct rculist rules_list;              /* Unordered. */
38
39     /* Identical, but lower priority rules are not inserted to any of the
40      * following data structures. */
41
42     /* These fields are accessed by readers who care about wildcarding. */
43     const tag_type tag;       /* Tag generated from mask for partitioning. */
44     const uint8_t n_indices;                   /* How many indices to use. */
45     const uint8_t index_ofs[CLS_MAX_INDICES];  /* u64 segment boundaries. */
46     unsigned int trie_plen[CLS_MAX_TRIES];  /* Trie prefix length in 'mask'
47                                              * (runtime configurable). */
48     const int ports_mask_len;
49     struct cmap indices[CLS_MAX_INDICES];   /* Staged lookup indices. */
50     rcu_trie_ptr ports_trie;                /* NULL if none. */
51
52     /* These fields are accessed by all readers. */
53     struct cmap rules;                      /* Contains 'cls_match'es. */
54     const struct minimask mask;             /* Wildcards for fields. */
55     /* 'mask' must be the last field. */
56 };
57
58 /* Associates a metadata value (that is, a value of the OpenFlow 1.1+ metadata
59  * field) with tags for the "cls_subtable"s that contain rules that match that
60  * metadata value.  */
61 struct cls_partition {
62     struct cmap_node cmap_node; /* In struct classifier's 'partitions' map. */
63     ovs_be64 metadata;          /* metadata value for this partition. */
64     tag_type tags;              /* OR of each flow's cls_subtable tag. */
65     struct tag_tracker tracker; /* Tracks the bits in 'tags'. */
66 };
67
68 /* Internal representation of a rule in a "struct cls_subtable". */
69 struct cls_match {
70     /* Accessed by everybody. */
71     struct rculist list; /* Identical, lower-priority "cls_match"es. */
72
73     /* Accessed only by writers. */
74     struct cls_partition *partition;
75
76     /* Accessed by readers interested in wildcarding. */
77     const int priority;         /* Larger numbers are higher priorities. */
78     struct cmap_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
79                                                     * 'indices'. */
80     /* Accessed by all readers. */
81     struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */
82     const struct cls_rule *cls_rule;
83     OVSRCU_TYPE(struct cls_conjunction_set *) conj_set;
84     const struct miniflow flow; /* Matching rule. Mask is in the subtable. */
85     /* 'flow' must be the last field. */
86 };
87
88 /* A longest-prefix match tree. */
89 struct trie_node {
90     uint32_t prefix;           /* Prefix bits for this node, MSB first. */
91     uint8_t  n_bits;           /* Never zero, except for the root node. */
92     unsigned int n_rules;      /* Number of rules that have this prefix. */
93     rcu_trie_ptr edges[2];     /* Both NULL if leaf. */
94 };
95
96 /* Max bits per node.  Must fit in struct trie_node's 'prefix'.
97  * Also tested with 16, 8, and 5 to stress the implementation. */
98 #define TRIE_PREFIX_BITS 32
99 \f
100 /* flow/miniflow/minimask/minimatch utilities.
101  * These are only used by the classifier, so place them here to allow
102  * for better optimization. */
103
104 static inline uint64_t
105 miniflow_get_map_in_range(const struct miniflow *miniflow,
106                           uint8_t start, uint8_t end, unsigned int *offset)
107 {
108     uint64_t map = miniflow->map;
109     *offset = 0;
110
111     if (start > 0) {
112         uint64_t msk = (UINT64_C(1) << start) - 1; /* 'start' LSBs set */
113         *offset = count_1bits(map & msk);
114         map &= ~msk;
115     }
116     if (end < FLOW_U64S) {
117         uint64_t msk = (UINT64_C(1) << end) - 1; /* 'end' LSBs set */
118         map &= msk;
119     }
120     return map;
121 }
122
123 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
124  * 'mask', given 'basis'.
125  *
126  * The hash values returned by this function are the same as those returned by
127  * miniflow_hash_in_minimask(), only the form of the arguments differ. */
128 static inline uint32_t
129 flow_hash_in_minimask(const struct flow *flow, const struct minimask *mask,
130                       uint32_t basis)
131 {
132     const uint64_t *mask_values = miniflow_get_values(&mask->masks);
133     const uint64_t *flow_u64 = (const uint64_t *)flow;
134     const uint64_t *p = mask_values;
135     uint32_t hash;
136     int idx;
137
138     hash = basis;
139     MAP_FOR_EACH_INDEX(idx, mask->masks.map) {
140         hash = hash_add64(hash, flow_u64[idx] & *p++);
141     }
142
143     return hash_finish(hash, (p - mask_values) * 8);
144 }
145
146 /* Returns a hash value for the bits of 'flow' where there are 1-bits in
147  * 'mask', given 'basis'.
148  *
149  * The hash values returned by this function are the same as those returned by
150  * flow_hash_in_minimask(), only the form of the arguments differ. */
151 static inline uint32_t
152 miniflow_hash_in_minimask(const struct miniflow *flow,
153                           const struct minimask *mask, uint32_t basis)
154 {
155     const uint64_t *mask_values = miniflow_get_values(&mask->masks);
156     const uint64_t *p = mask_values;
157     uint32_t hash = basis;
158     uint64_t flow_u64;
159
160     MINIFLOW_FOR_EACH_IN_MAP(flow_u64, flow, mask->masks.map) {
161         hash = hash_add64(hash, flow_u64 & *p++);
162     }
163
164     return hash_finish(hash, (p - mask_values) * 8);
165 }
166
167 /* Returns a hash value for the bits of range [start, end) in 'flow',
168  * where there are 1-bits in 'mask', given 'hash'.
169  *
170  * The hash values returned by this function are the same as those returned by
171  * minimatch_hash_range(), only the form of the arguments differ. */
172 static inline uint32_t
173 flow_hash_in_minimask_range(const struct flow *flow,
174                             const struct minimask *mask,
175                             uint8_t start, uint8_t end, uint32_t *basis)
176 {
177     const uint64_t *mask_values = miniflow_get_values(&mask->masks);
178     const uint64_t *flow_u64 = (const uint64_t *)flow;
179     unsigned int offset;
180     uint64_t map;
181     const uint64_t *p;
182     uint32_t hash = *basis;
183     int idx;
184
185     map = miniflow_get_map_in_range(&mask->masks, start, end, &offset);
186     p = mask_values + offset;
187     MAP_FOR_EACH_INDEX(idx, map) {
188         hash = hash_add64(hash, flow_u64[idx] & *p++);
189     }
190
191     *basis = hash; /* Allow continuation from the unfinished value. */
192     return hash_finish(hash, (p - mask_values) * 8);
193 }
194
195 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask. */
196 static inline void
197 flow_wildcards_fold_minimask(struct flow_wildcards *wc,
198                              const struct minimask *mask)
199 {
200     flow_union_with_miniflow(&wc->masks, &mask->masks);
201 }
202
203 /* Fold minimask 'mask''s wildcard mask into 'wc's wildcard mask
204  * in range [start, end). */
205 static inline void
206 flow_wildcards_fold_minimask_range(struct flow_wildcards *wc,
207                                    const struct minimask *mask,
208                                    uint8_t start, uint8_t end)
209 {
210     uint64_t *dst_u64 = (uint64_t *)&wc->masks;
211     unsigned int offset;
212     uint64_t map;
213     const uint64_t *p;
214     int idx;
215
216     map = miniflow_get_map_in_range(&mask->masks, start, end, &offset);
217     p = miniflow_get_values(&mask->masks) + offset;
218     MAP_FOR_EACH_INDEX(idx, map) {
219         dst_u64[idx] |= *p++;
220     }
221 }
222
223 /* Returns a hash value for 'flow', given 'basis'. */
224 static inline uint32_t
225 miniflow_hash(const struct miniflow *flow, uint32_t basis)
226 {
227     const uint64_t *values = miniflow_get_values(flow);
228     const uint64_t *p = values;
229     uint32_t hash = basis;
230     uint64_t hash_map = 0;
231     uint64_t map;
232
233     for (map = flow->map; map; map = zero_rightmost_1bit(map)) {
234         if (*p) {
235             hash = hash_add64(hash, *p);
236             hash_map |= rightmost_1bit(map);
237         }
238         p++;
239     }
240     hash = hash_add64(hash, hash_map);
241
242     return hash_finish(hash, p - values);
243 }
244
245 /* Returns a hash value for 'mask', given 'basis'. */
246 static inline uint32_t
247 minimask_hash(const struct minimask *mask, uint32_t basis)
248 {
249     return miniflow_hash(&mask->masks, basis);
250 }
251
252 /* Returns a hash value for 'match', given 'basis'. */
253 static inline uint32_t
254 minimatch_hash(const struct minimatch *match, uint32_t basis)
255 {
256     return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
257 }
258
259 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
260  * given 'basis'.
261  *
262  * The hash values returned by this function are the same as those returned by
263  * flow_hash_in_minimask_range(), only the form of the arguments differ. */
264 static inline uint32_t
265 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
266                      uint32_t *basis)
267 {
268     unsigned int offset;
269     const uint64_t *p, *q;
270     uint32_t hash = *basis;
271     int n, i;
272
273     n = count_1bits(miniflow_get_map_in_range(&match->mask.masks, start, end,
274                                               &offset));
275     q = miniflow_get_values(&match->mask.masks) + offset;
276     p = miniflow_get_values(&match->flow) + offset;
277
278     for (i = 0; i < n; i++) {
279         hash = hash_add64(hash, p[i] & q[i]);
280     }
281     *basis = hash; /* Allow continuation from the unfinished value. */
282     return hash_finish(hash, (offset + n) * 8);
283 }
284
285 #endif