]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2009, 2010, 2011, 2012 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 | /* "White box" tests for classifier. | |
18 | * | |
19 | * With very few exceptions, these tests obtain complete coverage of every | |
20 | * basic block and every branch in the classifier implementation, e.g. a clean | |
21 | * report from "gcov -b". (Covering the exceptions would require finding | |
22 | * collisions in the hash function used for flow data, etc.) | |
23 | * | |
24 | * This test should receive a clean report from "valgrind --leak-check=full": | |
25 | * it frees every heap block that it allocates. | |
26 | */ | |
27 | ||
28 | #include <config.h> | |
064af421 BP |
29 | #include "classifier.h" |
30 | #include <errno.h> | |
31 | #include <limits.h> | |
10a24935 | 32 | #include "byte-order.h" |
3223e977 | 33 | #include "command-line.h" |
064af421 | 34 | #include "flow.h" |
0596e897 | 35 | #include "ofp-util.h" |
064af421 | 36 | #include "packets.h" |
c0a56d9f | 37 | #include "unaligned.h" |
064af421 BP |
38 | |
39 | #undef NDEBUG | |
40 | #include <assert.h> | |
41 | ||
b5d97350 | 42 | /* Fields in a rule. */ |
d8ae4d67 BP |
43 | #define CLS_FIELDS \ |
44 | /* struct flow all-caps */ \ | |
45 | /* FWW_* bit(s) member name name */ \ | |
46 | /* -------------------------- ----------- -------- */ \ | |
8368c090 | 47 | CLS_FIELD(0, tun_id, TUN_ID) \ |
7525e578 | 48 | CLS_FIELD(0, metadata, METADATA) \ |
d8ae4d67 BP |
49 | CLS_FIELD(0, nw_src, NW_SRC) \ |
50 | CLS_FIELD(0, nw_dst, NW_DST) \ | |
51 | CLS_FIELD(FWW_IN_PORT, in_port, IN_PORT) \ | |
66642cb4 | 52 | CLS_FIELD(0, vlan_tci, VLAN_TCI) \ |
d8ae4d67 | 53 | CLS_FIELD(FWW_DL_TYPE, dl_type, DL_TYPE) \ |
73f33563 BP |
54 | CLS_FIELD(0, tp_src, TP_SRC) \ |
55 | CLS_FIELD(0, tp_dst, TP_DST) \ | |
73c0ce34 JS |
56 | CLS_FIELD(0, dl_src, DL_SRC) \ |
57 | CLS_FIELD(0, dl_dst, DL_DST) \ | |
d8ae4d67 | 58 | CLS_FIELD(FWW_NW_PROTO, nw_proto, NW_PROTO) \ |
2486e66a | 59 | CLS_FIELD(FWW_NW_DSCP, nw_tos, NW_DSCP) |
b5d97350 BP |
60 | |
61 | /* Field indexes. | |
62 | * | |
63 | * (These are also indexed into struct classifier's 'tables' array.) */ | |
64 | enum { | |
65 | #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME, | |
66 | CLS_FIELDS | |
67 | #undef CLS_FIELD | |
68 | CLS_N_FIELDS | |
69 | }; | |
70 | ||
71 | /* Field information. */ | |
72 | struct cls_field { | |
73 | int ofs; /* Offset in struct flow. */ | |
74 | int len; /* Length in bytes. */ | |
d8ae4d67 | 75 | flow_wildcards_t wildcards; /* FWW_* bit or bits for this field. */ |
b5d97350 BP |
76 | const char *name; /* Name (for debugging). */ |
77 | }; | |
78 | ||
79 | static const struct cls_field cls_fields[CLS_N_FIELDS] = { | |
80 | #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \ | |
81 | { offsetof(struct flow, MEMBER), \ | |
82 | sizeof ((struct flow *)0)->MEMBER, \ | |
83 | WILDCARDS, \ | |
84 | #NAME }, | |
85 | CLS_FIELDS | |
86 | #undef CLS_FIELD | |
87 | }; | |
88 | ||
064af421 BP |
89 | struct test_rule { |
90 | int aux; /* Auxiliary data. */ | |
91 | struct cls_rule cls_rule; /* Classifier rule data. */ | |
92 | }; | |
93 | ||
94 | static struct test_rule * | |
95 | test_rule_from_cls_rule(const struct cls_rule *rule) | |
96 | { | |
97 | return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL; | |
98 | } | |
99 | ||
100 | /* Trivial (linear) classifier. */ | |
101 | struct tcls { | |
102 | size_t n_rules; | |
103 | size_t allocated_rules; | |
104 | struct test_rule **rules; | |
105 | }; | |
106 | ||
107 | static void | |
108 | tcls_init(struct tcls *tcls) | |
109 | { | |
110 | tcls->n_rules = 0; | |
111 | tcls->allocated_rules = 0; | |
112 | tcls->rules = NULL; | |
113 | } | |
114 | ||
115 | static void | |
116 | tcls_destroy(struct tcls *tcls) | |
117 | { | |
118 | if (tcls) { | |
119 | size_t i; | |
120 | ||
121 | for (i = 0; i < tcls->n_rules; i++) { | |
122 | free(tcls->rules[i]); | |
123 | } | |
124 | free(tcls->rules); | |
125 | } | |
126 | } | |
127 | ||
064af421 BP |
128 | static bool |
129 | tcls_is_empty(const struct tcls *tcls) | |
130 | { | |
131 | return tcls->n_rules == 0; | |
132 | } | |
133 | ||
134 | static struct test_rule * | |
135 | tcls_insert(struct tcls *tcls, const struct test_rule *rule) | |
136 | { | |
137 | size_t i; | |
138 | ||
00561f41 BP |
139 | assert(!flow_wildcards_is_exact(&rule->cls_rule.wc) |
140 | || rule->cls_rule.priority == UINT_MAX); | |
064af421 BP |
141 | for (i = 0; i < tcls->n_rules; i++) { |
142 | const struct cls_rule *pos = &tcls->rules[i]->cls_rule; | |
193eb874 BP |
143 | if (cls_rule_equal(pos, &rule->cls_rule)) { |
144 | /* Exact match. */ | |
064af421 BP |
145 | free(tcls->rules[i]); |
146 | tcls->rules[i] = xmemdup(rule, sizeof *rule); | |
147 | return tcls->rules[i]; | |
af7b73f4 | 148 | } else if (pos->priority < rule->cls_rule.priority) { |
064af421 BP |
149 | break; |
150 | } | |
151 | } | |
152 | ||
153 | if (tcls->n_rules >= tcls->allocated_rules) { | |
154 | tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules, | |
155 | sizeof *tcls->rules); | |
156 | } | |
157 | if (i != tcls->n_rules) { | |
158 | memmove(&tcls->rules[i + 1], &tcls->rules[i], | |
159 | sizeof *tcls->rules * (tcls->n_rules - i)); | |
160 | } | |
161 | tcls->rules[i] = xmemdup(rule, sizeof *rule); | |
162 | tcls->n_rules++; | |
163 | return tcls->rules[i]; | |
164 | } | |
165 | ||
166 | static void | |
167 | tcls_remove(struct tcls *cls, const struct test_rule *rule) | |
168 | { | |
169 | size_t i; | |
170 | ||
171 | for (i = 0; i < cls->n_rules; i++) { | |
172 | struct test_rule *pos = cls->rules[i]; | |
173 | if (pos == rule) { | |
174 | free(pos); | |
175 | memmove(&cls->rules[i], &cls->rules[i + 1], | |
176 | sizeof *cls->rules * (cls->n_rules - i - 1)); | |
177 | cls->n_rules--; | |
178 | return; | |
179 | } | |
180 | } | |
181 | NOT_REACHED(); | |
182 | } | |
183 | ||
064af421 | 184 | static bool |
ae412e7d | 185 | match(const struct cls_rule *wild, const struct flow *fixed) |
064af421 BP |
186 | { |
187 | int f_idx; | |
188 | ||
189 | for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) { | |
190 | const struct cls_field *f = &cls_fields[f_idx]; | |
d8ae4d67 BP |
191 | bool eq; |
192 | ||
193 | if (f->wildcards) { | |
194 | void *wild_field = (char *) &wild->flow + f->ofs; | |
195 | void *fixed_field = (char *) fixed + f->ofs; | |
196 | eq = ((wild->wc.wildcards & f->wildcards) == f->wildcards | |
197 | || !memcmp(wild_field, fixed_field, f->len)); | |
198 | } else if (f_idx == CLS_F_IDX_NW_SRC) { | |
199 | eq = !((fixed->nw_src ^ wild->flow.nw_src) & wild->wc.nw_src_mask); | |
200 | } else if (f_idx == CLS_F_IDX_NW_DST) { | |
201 | eq = !((fixed->nw_dst ^ wild->flow.nw_dst) & wild->wc.nw_dst_mask); | |
73f33563 BP |
202 | } else if (f_idx == CLS_F_IDX_TP_SRC) { |
203 | eq = !((fixed->tp_src ^ wild->flow.tp_src) & wild->wc.tp_src_mask); | |
204 | } else if (f_idx == CLS_F_IDX_TP_DST) { | |
205 | eq = !((fixed->tp_dst ^ wild->flow.tp_dst) & wild->wc.tp_dst_mask); | |
73c0ce34 | 206 | } else if (f_idx == CLS_F_IDX_DL_SRC) { |
3b842fc2 EJ |
207 | eq = eth_addr_equal_except(fixed->dl_src, wild->flow.dl_src, |
208 | wild->wc.dl_src_mask); | |
73c0ce34 | 209 | } else if (f_idx == CLS_F_IDX_DL_DST) { |
3b842fc2 EJ |
210 | eq = eth_addr_equal_except(fixed->dl_dst, wild->flow.dl_dst, |
211 | wild->wc.dl_dst_mask); | |
66642cb4 BP |
212 | } else if (f_idx == CLS_F_IDX_VLAN_TCI) { |
213 | eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci) | |
214 | & wild->wc.vlan_tci_mask); | |
8368c090 BP |
215 | } else if (f_idx == CLS_F_IDX_TUN_ID) { |
216 | eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask); | |
7525e578 JS |
217 | } else if (f_idx == CLS_F_IDX_METADATA) { |
218 | eq = !((fixed->metadata ^ wild->flow.metadata) | |
219 | & wild->wc.metadata_mask); | |
2486e66a JP |
220 | } else if (f_idx == CLS_F_IDX_NW_DSCP) { |
221 | eq = !((fixed->nw_tos ^ wild->flow.nw_tos) & IP_DSCP_MASK); | |
d8ae4d67 BP |
222 | } else { |
223 | NOT_REACHED(); | |
064af421 BP |
224 | } |
225 | ||
d8ae4d67 BP |
226 | if (!eq) { |
227 | return false; | |
064af421 | 228 | } |
064af421 BP |
229 | } |
230 | return true; | |
231 | } | |
232 | ||
233 | static struct cls_rule * | |
3c4486a5 | 234 | tcls_lookup(const struct tcls *cls, const struct flow *flow) |
064af421 BP |
235 | { |
236 | size_t i; | |
237 | ||
238 | for (i = 0; i < cls->n_rules; i++) { | |
239 | struct test_rule *pos = cls->rules[i]; | |
3c4486a5 | 240 | if (match(&pos->cls_rule, flow)) { |
064af421 BP |
241 | return &pos->cls_rule; |
242 | } | |
243 | } | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | static void | |
3c4486a5 | 248 | tcls_delete_matches(struct tcls *cls, const struct cls_rule *target) |
064af421 BP |
249 | { |
250 | size_t i; | |
251 | ||
252 | for (i = 0; i < cls->n_rules; ) { | |
253 | struct test_rule *pos = cls->rules[i]; | |
3c4486a5 | 254 | if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc) |
064af421 BP |
255 | && match(target, &pos->cls_rule.flow)) { |
256 | tcls_remove(cls, pos); | |
257 | } else { | |
258 | i++; | |
259 | } | |
260 | } | |
261 | } | |
262 | \f | |
3c4486a5 | 263 | static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001), |
965f03d8 | 264 | CONSTANT_HTONL(0xc0a04455) }; |
3c4486a5 | 265 | static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002), |
965f03d8 | 266 | CONSTANT_HTONL(0xc0a04455) }; |
b9298d3f BP |
267 | static ovs_be64 tun_id_values[] = { |
268 | 0, | |
269 | CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) }; | |
7525e578 JS |
270 | static ovs_be64 metadata_values[] = { |
271 | 0, | |
272 | CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) }; | |
abe529af | 273 | static uint16_t in_port_values[] = { 1, OFPP_LOCAL }; |
66642cb4 | 274 | static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) }; |
3c4486a5 | 275 | static ovs_be16 dl_type_values[] |
965f03d8 | 276 | = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) }; |
3c4486a5 | 277 | static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362), |
965f03d8 | 278 | CONSTANT_HTONS(80) }; |
3c4486a5 | 279 | static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) }; |
064af421 BP |
280 | static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 }, |
281 | { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } }; | |
282 | static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 }, | |
283 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; | |
6767a2cc | 284 | static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP }; |
2486e66a | 285 | static uint8_t nw_dscp_values[] = { 48, 0 }; |
064af421 BP |
286 | |
287 | static void *values[CLS_N_FIELDS][2]; | |
288 | ||
289 | static void | |
290 | init_values(void) | |
291 | { | |
659586ef JG |
292 | values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0]; |
293 | values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1]; | |
294 | ||
7525e578 JS |
295 | values[CLS_F_IDX_METADATA][0] = &metadata_values[0]; |
296 | values[CLS_F_IDX_METADATA][1] = &metadata_values[1]; | |
297 | ||
064af421 BP |
298 | values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0]; |
299 | values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1]; | |
300 | ||
66642cb4 BP |
301 | values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0]; |
302 | values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1]; | |
959a2ecd | 303 | |
064af421 BP |
304 | values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0]; |
305 | values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1]; | |
306 | ||
307 | values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0]; | |
308 | values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1]; | |
309 | ||
310 | values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0]; | |
311 | values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1]; | |
312 | ||
313 | values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0]; | |
314 | values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1]; | |
315 | ||
316 | values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0]; | |
317 | values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1]; | |
318 | ||
319 | values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0]; | |
320 | values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1]; | |
321 | ||
2486e66a JP |
322 | values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0]; |
323 | values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1]; | |
834377ea | 324 | |
064af421 BP |
325 | values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0]; |
326 | values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1]; | |
327 | ||
328 | values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0]; | |
329 | values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1]; | |
330 | } | |
331 | ||
332 | #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values) | |
333 | #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values) | |
659586ef | 334 | #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values) |
7525e578 | 335 | #define N_METADATA_VALUES ARRAY_SIZE(metadata_values) |
064af421 | 336 | #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values) |
66642cb4 | 337 | #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values) |
064af421 BP |
338 | #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values) |
339 | #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values) | |
340 | #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values) | |
341 | #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values) | |
342 | #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values) | |
343 | #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values) | |
2486e66a | 344 | #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values) |
064af421 BP |
345 | |
346 | #define N_FLOW_VALUES (N_NW_SRC_VALUES * \ | |
347 | N_NW_DST_VALUES * \ | |
659586ef | 348 | N_TUN_ID_VALUES * \ |
064af421 | 349 | N_IN_PORT_VALUES * \ |
66642cb4 | 350 | N_VLAN_TCI_VALUES * \ |
064af421 BP |
351 | N_DL_TYPE_VALUES * \ |
352 | N_TP_SRC_VALUES * \ | |
353 | N_TP_DST_VALUES * \ | |
354 | N_DL_SRC_VALUES * \ | |
355 | N_DL_DST_VALUES * \ | |
834377ea | 356 | N_NW_PROTO_VALUES * \ |
2486e66a | 357 | N_NW_DSCP_VALUES) |
064af421 BP |
358 | |
359 | static unsigned int | |
360 | get_value(unsigned int *x, unsigned n_values) | |
361 | { | |
362 | unsigned int rem = *x % n_values; | |
363 | *x /= n_values; | |
364 | return rem; | |
365 | } | |
366 | ||
064af421 BP |
367 | static void |
368 | compare_classifiers(struct classifier *cls, struct tcls *tcls) | |
369 | { | |
70d3fbe7 | 370 | static const int confidence = 500; |
064af421 BP |
371 | unsigned int i; |
372 | ||
373 | assert(classifier_count(cls) == tcls->n_rules); | |
70d3fbe7 | 374 | for (i = 0; i < confidence; i++) { |
064af421 | 375 | struct cls_rule *cr0, *cr1; |
ae412e7d | 376 | struct flow flow; |
064af421 | 377 | unsigned int x; |
064af421 | 378 | |
70d3fbe7 | 379 | x = rand () % N_FLOW_VALUES; |
064af421 BP |
380 | flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)]; |
381 | flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)]; | |
659586ef | 382 | flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)]; |
7525e578 | 383 | flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)]; |
064af421 | 384 | flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)]; |
66642cb4 | 385 | flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)]; |
064af421 BP |
386 | flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)]; |
387 | flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)]; | |
388 | flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)]; | |
389 | memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)], | |
390 | ETH_ADDR_LEN); | |
391 | memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)], | |
392 | ETH_ADDR_LEN); | |
393 | flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)]; | |
2486e66a | 394 | flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)]; |
064af421 | 395 | |
3c4486a5 BP |
396 | cr0 = classifier_lookup(cls, &flow); |
397 | cr1 = tcls_lookup(tcls, &flow); | |
398 | assert((cr0 == NULL) == (cr1 == NULL)); | |
399 | if (cr0 != NULL) { | |
400 | const struct test_rule *tr0 = test_rule_from_cls_rule(cr0); | |
401 | const struct test_rule *tr1 = test_rule_from_cls_rule(cr1); | |
402 | ||
193eb874 | 403 | assert(cls_rule_equal(cr0, cr1)); |
3c4486a5 | 404 | assert(tr0->aux == tr1->aux); |
064af421 BP |
405 | } |
406 | } | |
407 | } | |
408 | ||
064af421 BP |
409 | static void |
410 | destroy_classifier(struct classifier *cls) | |
411 | { | |
5ecc9d81 BP |
412 | struct test_rule *rule, *next_rule; |
413 | struct cls_cursor cursor; | |
414 | ||
415 | cls_cursor_init(&cursor, cls, NULL); | |
416 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) { | |
417 | classifier_remove(cls, &rule->cls_rule); | |
418 | free(rule); | |
419 | } | |
064af421 BP |
420 | classifier_destroy(cls); |
421 | } | |
422 | ||
423 | static void | |
424 | check_tables(const struct classifier *cls, | |
b5d97350 | 425 | int n_tables, int n_rules, int n_dups) |
064af421 | 426 | { |
b5d97350 | 427 | const struct cls_table *table; |
955f579d BP |
428 | struct test_rule *test_rule; |
429 | struct cls_cursor cursor; | |
064af421 | 430 | int found_tables = 0; |
064af421 | 431 | int found_rules = 0; |
b5d97350 | 432 | int found_dups = 0; |
955f579d | 433 | int found_rules2 = 0; |
064af421 | 434 | |
b5d97350 BP |
435 | HMAP_FOR_EACH (table, hmap_node, &cls->tables) { |
436 | const struct cls_rule *head; | |
437 | ||
438 | assert(!hmap_is_empty(&table->rules)); | |
064af421 | 439 | |
064af421 | 440 | found_tables++; |
b5d97350 BP |
441 | HMAP_FOR_EACH (head, hmap_node, &table->rules) { |
442 | unsigned int prev_priority = UINT_MAX; | |
443 | const struct cls_rule *rule; | |
444 | ||
445 | found_rules++; | |
446 | LIST_FOR_EACH (rule, list, &head->list) { | |
447 | assert(rule->priority < prev_priority); | |
448 | prev_priority = rule->priority; | |
449 | found_rules++; | |
450 | found_dups++; | |
451 | assert(classifier_find_rule_exactly(cls, rule) == rule); | |
452 | } | |
453 | } | |
064af421 BP |
454 | } |
455 | ||
b5d97350 BP |
456 | assert(found_tables == hmap_count(&cls->tables)); |
457 | assert(n_tables == -1 || n_tables == hmap_count(&cls->tables)); | |
064af421 | 458 | assert(n_rules == -1 || found_rules == n_rules); |
b5d97350 | 459 | assert(n_dups == -1 || found_dups == n_dups); |
955f579d BP |
460 | |
461 | cls_cursor_init(&cursor, cls, NULL); | |
462 | CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) { | |
463 | found_rules2++; | |
464 | } | |
465 | assert(found_rules == found_rules2); | |
064af421 BP |
466 | } |
467 | ||
468 | static struct test_rule * | |
469 | make_rule(int wc_fields, unsigned int priority, int value_pat) | |
470 | { | |
471 | const struct cls_field *f; | |
472 | struct test_rule *rule; | |
064af421 | 473 | |
d8ae4d67 BP |
474 | rule = xzalloc(sizeof *rule); |
475 | cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX); | |
064af421 BP |
476 | for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) { |
477 | int f_idx = f - cls_fields; | |
d8ae4d67 BP |
478 | int value_idx = (value_pat & (1u << f_idx)) != 0; |
479 | memcpy((char *) &rule->cls_rule.flow + f->ofs, | |
480 | values[f_idx][value_idx], f->len); | |
481 | ||
482 | if (f->wildcards) { | |
483 | rule->cls_rule.wc.wildcards &= ~f->wildcards; | |
484 | } else if (f_idx == CLS_F_IDX_NW_SRC) { | |
485 | rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX); | |
486 | } else if (f_idx == CLS_F_IDX_NW_DST) { | |
487 | rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX); | |
73f33563 BP |
488 | } else if (f_idx == CLS_F_IDX_TP_SRC) { |
489 | rule->cls_rule.wc.tp_src_mask = htons(UINT16_MAX); | |
490 | } else if (f_idx == CLS_F_IDX_TP_DST) { | |
491 | rule->cls_rule.wc.tp_dst_mask = htons(UINT16_MAX); | |
73c0ce34 JS |
492 | } else if (f_idx == CLS_F_IDX_DL_SRC) { |
493 | memset(rule->cls_rule.wc.dl_src_mask, 0xff, ETH_ADDR_LEN); | |
494 | } else if (f_idx == CLS_F_IDX_DL_DST) { | |
495 | memset(rule->cls_rule.wc.dl_dst_mask, 0xff, ETH_ADDR_LEN); | |
66642cb4 BP |
496 | } else if (f_idx == CLS_F_IDX_VLAN_TCI) { |
497 | rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX); | |
8368c090 BP |
498 | } else if (f_idx == CLS_F_IDX_TUN_ID) { |
499 | rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX); | |
7525e578 JS |
500 | } else if (f_idx == CLS_F_IDX_METADATA) { |
501 | rule->cls_rule.wc.metadata_mask = htonll(UINT64_MAX); | |
064af421 | 502 | } else { |
d8ae4d67 | 503 | NOT_REACHED(); |
064af421 BP |
504 | } |
505 | } | |
064af421 BP |
506 | return rule; |
507 | } | |
508 | ||
509 | static void | |
510 | shuffle(unsigned int *p, size_t n) | |
511 | { | |
512 | for (; n > 1; n--, p++) { | |
513 | unsigned int *q = &p[rand() % n]; | |
514 | unsigned int tmp = *p; | |
515 | *p = *q; | |
516 | *q = tmp; | |
517 | } | |
518 | } | |
519 | \f | |
520 | /* Tests an empty classifier. */ | |
521 | static void | |
3223e977 | 522 | test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
064af421 BP |
523 | { |
524 | struct classifier cls; | |
525 | struct tcls tcls; | |
526 | ||
527 | classifier_init(&cls); | |
528 | tcls_init(&tcls); | |
529 | assert(classifier_is_empty(&cls)); | |
530 | assert(tcls_is_empty(&tcls)); | |
531 | compare_classifiers(&cls, &tcls); | |
532 | classifier_destroy(&cls); | |
533 | tcls_destroy(&tcls); | |
534 | } | |
535 | ||
536 | /* Destroys a null classifier. */ | |
537 | static void | |
3223e977 | 538 | test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
064af421 BP |
539 | { |
540 | classifier_destroy(NULL); | |
541 | } | |
542 | ||
543 | /* Tests classification with one rule at a time. */ | |
544 | static void | |
3223e977 | 545 | test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
064af421 BP |
546 | { |
547 | unsigned int wc_fields; /* Hilarious. */ | |
548 | ||
549 | for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) { | |
550 | struct classifier cls; | |
551 | struct test_rule *rule, *tcls_rule; | |
552 | struct tcls tcls; | |
553 | ||
554 | rule = make_rule(wc_fields, | |
555 | hash_bytes(&wc_fields, sizeof wc_fields, 0), 0); | |
556 | ||
557 | classifier_init(&cls); | |
558 | tcls_init(&tcls); | |
559 | ||
560 | tcls_rule = tcls_insert(&tcls, rule); | |
08944c1d | 561 | classifier_insert(&cls, &rule->cls_rule); |
b5d97350 | 562 | check_tables(&cls, 1, 1, 0); |
064af421 BP |
563 | compare_classifiers(&cls, &tcls); |
564 | ||
565 | classifier_remove(&cls, &rule->cls_rule); | |
566 | tcls_remove(&tcls, tcls_rule); | |
567 | assert(classifier_is_empty(&cls)); | |
568 | assert(tcls_is_empty(&tcls)); | |
569 | compare_classifiers(&cls, &tcls); | |
570 | ||
571 | free(rule); | |
572 | classifier_destroy(&cls); | |
573 | tcls_destroy(&tcls); | |
574 | } | |
575 | } | |
576 | ||
577 | /* Tests replacing one rule by another. */ | |
578 | static void | |
3223e977 | 579 | test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
064af421 BP |
580 | { |
581 | unsigned int wc_fields; | |
582 | ||
583 | for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) { | |
584 | struct classifier cls; | |
f0f4410a BP |
585 | struct test_rule *rule1; |
586 | struct test_rule *rule2; | |
064af421 BP |
587 | struct tcls tcls; |
588 | ||
589 | rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX); | |
590 | rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX); | |
591 | rule2->aux += 5; | |
592 | rule2->aux += 5; | |
593 | ||
594 | classifier_init(&cls); | |
595 | tcls_init(&tcls); | |
f0f4410a | 596 | tcls_insert(&tcls, rule1); |
08944c1d | 597 | classifier_insert(&cls, &rule1->cls_rule); |
b5d97350 | 598 | check_tables(&cls, 1, 1, 0); |
064af421 BP |
599 | compare_classifiers(&cls, &tcls); |
600 | tcls_destroy(&tcls); | |
601 | ||
602 | tcls_init(&tcls); | |
f0f4410a | 603 | tcls_insert(&tcls, rule2); |
064af421 | 604 | assert(test_rule_from_cls_rule( |
08944c1d | 605 | classifier_replace(&cls, &rule2->cls_rule)) == rule1); |
064af421 | 606 | free(rule1); |
b5d97350 | 607 | check_tables(&cls, 1, 1, 0); |
064af421 BP |
608 | compare_classifiers(&cls, &tcls); |
609 | tcls_destroy(&tcls); | |
610 | destroy_classifier(&cls); | |
611 | } | |
612 | } | |
613 | ||
614 | static int | |
b5d97350 | 615 | factorial(int n_items) |
064af421 | 616 | { |
b5d97350 BP |
617 | int n, i; |
618 | ||
619 | n = 1; | |
620 | for (i = 2; i <= n_items; i++) { | |
621 | n *= i; | |
622 | } | |
623 | return n; | |
064af421 BP |
624 | } |
625 | ||
b5d97350 BP |
626 | static void |
627 | swap(int *a, int *b) | |
064af421 | 628 | { |
b5d97350 BP |
629 | int tmp = *a; |
630 | *a = *b; | |
631 | *b = tmp; | |
064af421 BP |
632 | } |
633 | ||
064af421 | 634 | static void |
b5d97350 | 635 | reverse(int *a, int n) |
064af421 | 636 | { |
b5d97350 | 637 | int i; |
064af421 | 638 | |
b5d97350 BP |
639 | for (i = 0; i < n / 2; i++) { |
640 | int j = n - (i + 1); | |
641 | swap(&a[i], &a[j]); | |
642 | } | |
643 | } | |
064af421 | 644 | |
b5d97350 BP |
645 | static bool |
646 | next_permutation(int *a, int n) | |
647 | { | |
648 | int k; | |
064af421 | 649 | |
b5d97350 BP |
650 | for (k = n - 2; k >= 0; k--) { |
651 | if (a[k] < a[k + 1]) { | |
652 | int l; | |
064af421 | 653 | |
b5d97350 BP |
654 | for (l = n - 1; ; l--) { |
655 | if (a[l] > a[k]) { | |
656 | swap(&a[k], &a[l]); | |
657 | reverse(a + (k + 1), n - (k + 1)); | |
658 | return true; | |
064af421 BP |
659 | } |
660 | } | |
661 | } | |
662 | } | |
b5d97350 | 663 | return false; |
064af421 BP |
664 | } |
665 | ||
b5d97350 | 666 | /* Tests classification with rules that have the same matching criteria. */ |
064af421 | 667 | static void |
b5d97350 BP |
668 | test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
669 | { | |
670 | enum { N_RULES = 3 }; | |
671 | int n_pris; | |
064af421 | 672 | |
b5d97350 BP |
673 | for (n_pris = N_RULES; n_pris >= 1; n_pris--) { |
674 | int ops[N_RULES * 2]; | |
675 | int pris[N_RULES]; | |
676 | int n_permutations; | |
677 | int i; | |
064af421 | 678 | |
b5d97350 BP |
679 | pris[0] = 0; |
680 | for (i = 1; i < N_RULES; i++) { | |
681 | pris[i] = pris[i - 1] + (n_pris > i); | |
682 | } | |
064af421 | 683 | |
b5d97350 BP |
684 | for (i = 0; i < N_RULES * 2; i++) { |
685 | ops[i] = i / 2; | |
064af421 | 686 | } |
064af421 | 687 | |
b5d97350 BP |
688 | n_permutations = 0; |
689 | do { | |
690 | struct test_rule *rules[N_RULES]; | |
691 | struct test_rule *tcls_rules[N_RULES]; | |
692 | int pri_rules[N_RULES]; | |
693 | struct classifier cls; | |
694 | struct tcls tcls; | |
064af421 | 695 | |
b5d97350 | 696 | n_permutations++; |
064af421 | 697 | |
b5d97350 BP |
698 | for (i = 0; i < N_RULES; i++) { |
699 | rules[i] = make_rule(456, pris[i], 0); | |
700 | tcls_rules[i] = NULL; | |
701 | pri_rules[i] = -1; | |
702 | } | |
064af421 | 703 | |
b5d97350 BP |
704 | classifier_init(&cls); |
705 | tcls_init(&tcls); | |
064af421 | 706 | |
b5d97350 BP |
707 | for (i = 0; i < ARRAY_SIZE(ops); i++) { |
708 | int j = ops[i]; | |
709 | int m, n; | |
064af421 | 710 | |
b5d97350 BP |
711 | if (!tcls_rules[j]) { |
712 | struct test_rule *displaced_rule; | |
713 | ||
714 | tcls_rules[j] = tcls_insert(&tcls, rules[j]); | |
715 | displaced_rule = test_rule_from_cls_rule( | |
08944c1d | 716 | classifier_replace(&cls, &rules[j]->cls_rule)); |
b5d97350 BP |
717 | if (pri_rules[pris[j]] >= 0) { |
718 | int k = pri_rules[pris[j]]; | |
719 | assert(displaced_rule != NULL); | |
720 | assert(displaced_rule != rules[j]); | |
721 | assert(pris[j] == displaced_rule->cls_rule.priority); | |
722 | tcls_rules[k] = NULL; | |
723 | } else { | |
724 | assert(displaced_rule == NULL); | |
725 | } | |
726 | pri_rules[pris[j]] = j; | |
727 | } else { | |
728 | classifier_remove(&cls, &rules[j]->cls_rule); | |
729 | tcls_remove(&tcls, tcls_rules[j]); | |
730 | tcls_rules[j] = NULL; | |
731 | pri_rules[pris[j]] = -1; | |
732 | } | |
064af421 | 733 | |
b5d97350 BP |
734 | n = 0; |
735 | for (m = 0; m < N_RULES; m++) { | |
736 | n += tcls_rules[m] != NULL; | |
064af421 | 737 | } |
b5d97350 BP |
738 | check_tables(&cls, n > 0, n, n - 1); |
739 | ||
740 | compare_classifiers(&cls, &tcls); | |
064af421 | 741 | } |
b5d97350 BP |
742 | |
743 | classifier_destroy(&cls); | |
744 | tcls_destroy(&tcls); | |
745 | ||
746 | for (i = 0; i < N_RULES; i++) { | |
747 | free(rules[i]); | |
748 | } | |
749 | } while (next_permutation(ops, ARRAY_SIZE(ops))); | |
750 | assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES)); | |
064af421 BP |
751 | } |
752 | } | |
753 | ||
b5d97350 BP |
754 | static int |
755 | count_ones(unsigned long int x) | |
064af421 | 756 | { |
b5d97350 | 757 | int n = 0; |
064af421 | 758 | |
b5d97350 BP |
759 | while (x) { |
760 | x &= x - 1; | |
761 | n++; | |
762 | } | |
064af421 | 763 | |
b5d97350 BP |
764 | return n; |
765 | } | |
064af421 | 766 | |
b5d97350 BP |
767 | static bool |
768 | array_contains(int *array, int n, int value) | |
769 | { | |
770 | int i; | |
064af421 | 771 | |
b5d97350 BP |
772 | for (i = 0; i < n; i++) { |
773 | if (array[i] == value) { | |
774 | return true; | |
064af421 BP |
775 | } |
776 | } | |
b5d97350 BP |
777 | |
778 | return false; | |
064af421 BP |
779 | } |
780 | ||
b5d97350 BP |
781 | /* Tests classification with two rules at a time that fall into the same |
782 | * table but different lists. */ | |
064af421 | 783 | static void |
3223e977 | 784 | test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
064af421 | 785 | { |
b5d97350 | 786 | int iteration; |
064af421 | 787 | |
b5d97350 BP |
788 | for (iteration = 0; iteration < 50; iteration++) { |
789 | enum { N_RULES = 20 }; | |
790 | struct test_rule *rules[N_RULES]; | |
791 | struct test_rule *tcls_rules[N_RULES]; | |
792 | struct classifier cls; | |
793 | struct tcls tcls; | |
794 | int value_pats[N_RULES]; | |
795 | int value_mask; | |
796 | int wcf; | |
797 | int i; | |
064af421 | 798 | |
b5d97350 BP |
799 | do { |
800 | wcf = rand() & ((1u << CLS_N_FIELDS) - 1); | |
801 | value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1); | |
802 | } while ((1 << count_ones(value_mask)) < N_RULES); | |
064af421 | 803 | |
b5d97350 BP |
804 | classifier_init(&cls); |
805 | tcls_init(&tcls); | |
064af421 | 806 | |
b5d97350 BP |
807 | for (i = 0; i < N_RULES; i++) { |
808 | unsigned int priority = rand(); | |
064af421 | 809 | |
b5d97350 BP |
810 | do { |
811 | value_pats[i] = rand() & value_mask; | |
812 | } while (array_contains(value_pats, i, value_pats[i])); | |
064af421 | 813 | |
b5d97350 BP |
814 | rules[i] = make_rule(wcf, priority, value_pats[i]); |
815 | tcls_rules[i] = tcls_insert(&tcls, rules[i]); | |
08944c1d | 816 | classifier_insert(&cls, &rules[i]->cls_rule); |
b5d97350 BP |
817 | |
818 | check_tables(&cls, 1, i + 1, 0); | |
819 | compare_classifiers(&cls, &tcls); | |
820 | } | |
821 | ||
822 | for (i = 0; i < N_RULES; i++) { | |
823 | tcls_remove(&tcls, tcls_rules[i]); | |
824 | classifier_remove(&cls, &rules[i]->cls_rule); | |
825 | free(rules[i]); | |
826 | ||
827 | check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0); | |
828 | compare_classifiers(&cls, &tcls); | |
064af421 | 829 | } |
b5d97350 BP |
830 | |
831 | classifier_destroy(&cls); | |
832 | tcls_destroy(&tcls); | |
064af421 BP |
833 | } |
834 | } | |
835 | ||
b5d97350 BP |
836 | /* Tests classification with many rules at a time that fall into random lists |
837 | * in 'n' tables. */ | |
064af421 | 838 | static void |
b5d97350 | 839 | test_many_rules_in_n_tables(int n_tables) |
064af421 BP |
840 | { |
841 | enum { MAX_RULES = 50 }; | |
b5d97350 | 842 | int wcfs[10]; |
064af421 | 843 | int iteration; |
b5d97350 BP |
844 | int i; |
845 | ||
846 | assert(n_tables < 10); | |
847 | for (i = 0; i < n_tables; i++) { | |
848 | do { | |
849 | wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1); | |
850 | } while (array_contains(wcfs, i, wcfs[i])); | |
851 | } | |
064af421 BP |
852 | |
853 | for (iteration = 0; iteration < 30; iteration++) { | |
854 | unsigned int priorities[MAX_RULES]; | |
855 | struct classifier cls; | |
856 | struct tcls tcls; | |
064af421 BP |
857 | |
858 | srand(iteration); | |
859 | for (i = 0; i < MAX_RULES; i++) { | |
860 | priorities[i] = i * 129; | |
861 | } | |
862 | shuffle(priorities, ARRAY_SIZE(priorities)); | |
863 | ||
864 | classifier_init(&cls); | |
865 | tcls_init(&tcls); | |
866 | ||
867 | for (i = 0; i < MAX_RULES; i++) { | |
868 | struct test_rule *rule; | |
869 | unsigned int priority = priorities[i]; | |
b5d97350 | 870 | int wcf = wcfs[rand() % n_tables]; |
064af421 BP |
871 | int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1); |
872 | rule = make_rule(wcf, priority, value_pat); | |
873 | tcls_insert(&tcls, rule); | |
08944c1d | 874 | classifier_insert(&cls, &rule->cls_rule); |
b5d97350 | 875 | check_tables(&cls, -1, i + 1, -1); |
064af421 BP |
876 | compare_classifiers(&cls, &tcls); |
877 | } | |
878 | ||
879 | while (!classifier_is_empty(&cls)) { | |
5ecc9d81 BP |
880 | struct test_rule *rule, *next_rule; |
881 | struct test_rule *target; | |
882 | struct cls_cursor cursor; | |
883 | ||
884 | target = xmemdup(tcls.rules[rand() % tcls.n_rules], | |
885 | sizeof(struct test_rule)); | |
886 | ||
887 | cls_cursor_init(&cursor, &cls, &target->cls_rule); | |
888 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) { | |
889 | classifier_remove(&cls, &rule->cls_rule); | |
890 | free(rule); | |
891 | } | |
892 | tcls_delete_matches(&tcls, &target->cls_rule); | |
064af421 | 893 | compare_classifiers(&cls, &tcls); |
b5d97350 | 894 | check_tables(&cls, -1, -1, -1); |
5ecc9d81 | 895 | free(target); |
064af421 | 896 | } |
064af421 BP |
897 | |
898 | destroy_classifier(&cls); | |
899 | tcls_destroy(&tcls); | |
900 | } | |
901 | } | |
b5d97350 BP |
902 | |
903 | static void | |
904 | test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
905 | { | |
906 | test_many_rules_in_n_tables(2); | |
907 | } | |
908 | ||
909 | static void | |
910 | test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
911 | { | |
912 | test_many_rules_in_n_tables(5); | |
913 | } | |
064af421 | 914 | \f |
3223e977 BP |
915 | static const struct command commands[] = { |
916 | {"empty", 0, 0, test_empty}, | |
917 | {"destroy-null", 0, 0, test_destroy_null}, | |
918 | {"single-rule", 0, 0, test_single_rule}, | |
919 | {"rule-replacement", 0, 0, test_rule_replacement}, | |
b5d97350 | 920 | {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list}, |
3223e977 | 921 | {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table}, |
b5d97350 BP |
922 | {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables}, |
923 | {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables}, | |
3223e977 BP |
924 | {NULL, 0, 0, NULL}, |
925 | }; | |
064af421 BP |
926 | |
927 | int | |
3223e977 | 928 | main(int argc, char *argv[]) |
064af421 | 929 | { |
b5d97350 | 930 | set_program_name(argv[0]); |
064af421 | 931 | init_values(); |
3223e977 | 932 | run_command(argc - 1, argv + 1, commands); |
064af421 BP |
933 | return 0; |
934 | } |