]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-classifier.c
Merge "master" into "next".
[mirror_ovs.git] / tests / test-classifier.c
1 /*
2 * Copyright (c) 2009, 2010 Nicira Networks.
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 /* "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>
29 #include <limits.h>
30 #include "classifier.h"
31 #include <errno.h>
32 #include <limits.h>
33 #include "flow.h"
34 #include <limits.h>
35 #include "packets.h"
36
37 #undef NDEBUG
38 #include <assert.h>
39
40 struct test_rule {
41 int aux; /* Auxiliary data. */
42 struct cls_rule cls_rule; /* Classifier rule data. */
43 };
44
45 static struct test_rule *
46 test_rule_from_cls_rule(const struct cls_rule *rule)
47 {
48 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
49 }
50
51 /* Trivial (linear) classifier. */
52 struct tcls {
53 size_t n_rules;
54 size_t allocated_rules;
55 struct test_rule **rules;
56 };
57
58 static void
59 tcls_init(struct tcls *tcls)
60 {
61 tcls->n_rules = 0;
62 tcls->allocated_rules = 0;
63 tcls->rules = NULL;
64 }
65
66 static void
67 tcls_destroy(struct tcls *tcls)
68 {
69 if (tcls) {
70 size_t i;
71
72 for (i = 0; i < tcls->n_rules; i++) {
73 free(tcls->rules[i]);
74 }
75 free(tcls->rules);
76 }
77 }
78
79 static int
80 tcls_count_exact(const struct tcls *tcls)
81 {
82 int n_exact;
83 size_t i;
84
85 n_exact = 0;
86 for (i = 0; i < tcls->n_rules; i++) {
87 n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
88 }
89 return n_exact;
90 }
91
92 static bool
93 tcls_is_empty(const struct tcls *tcls)
94 {
95 return tcls->n_rules == 0;
96 }
97
98 static struct test_rule *
99 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
100 {
101 size_t i;
102
103 assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
104 for (i = 0; i < tcls->n_rules; i++) {
105 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
106 if (pos->priority == rule->cls_rule.priority
107 && pos->wc.wildcards == rule->cls_rule.wc.wildcards
108 && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
109 /* Exact match.
110 * XXX flow_equal should ignore wildcarded fields */
111 free(tcls->rules[i]);
112 tcls->rules[i] = xmemdup(rule, sizeof *rule);
113 return tcls->rules[i];
114 } else if (pos->priority <= rule->cls_rule.priority) {
115 break;
116 }
117 }
118
119 if (tcls->n_rules >= tcls->allocated_rules) {
120 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
121 sizeof *tcls->rules);
122 }
123 if (i != tcls->n_rules) {
124 memmove(&tcls->rules[i + 1], &tcls->rules[i],
125 sizeof *tcls->rules * (tcls->n_rules - i));
126 }
127 tcls->rules[i] = xmemdup(rule, sizeof *rule);
128 tcls->n_rules++;
129 return tcls->rules[i];
130 }
131
132 static void
133 tcls_remove(struct tcls *cls, const struct test_rule *rule)
134 {
135 size_t i;
136
137 for (i = 0; i < cls->n_rules; i++) {
138 struct test_rule *pos = cls->rules[i];
139 if (pos == rule) {
140 free(pos);
141 memmove(&cls->rules[i], &cls->rules[i + 1],
142 sizeof *cls->rules * (cls->n_rules - i - 1));
143 cls->n_rules--;
144 return;
145 }
146 }
147 NOT_REACHED();
148 }
149
150 static uint32_t
151 read_uint32(const void *p)
152 {
153 uint32_t x;
154 memcpy(&x, p, sizeof x);
155 return x;
156 }
157
158 static bool
159 match(const struct cls_rule *wild, const flow_t *fixed)
160 {
161 int f_idx;
162
163 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
164 const struct cls_field *f = &cls_fields[f_idx];
165 void *wild_field = (char *) &wild->flow + f->ofs;
166 void *fixed_field = (char *) fixed + f->ofs;
167
168 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
169 !memcmp(wild_field, fixed_field, f->len)) {
170 /* Definite match. */
171 continue;
172 }
173
174 if (wild->wc.wildcards & f->wildcards) {
175 uint32_t test = read_uint32(wild_field);
176 uint32_t ip = read_uint32(fixed_field);
177 int shift = (f_idx == CLS_F_IDX_NW_SRC
178 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
179 uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
180 if (!((test ^ ip) & mask)) {
181 continue;
182 }
183 }
184
185 return false;
186 }
187 return true;
188 }
189
190 static struct cls_rule *
191 tcls_lookup(const struct tcls *cls, const flow_t *flow, int include)
192 {
193 size_t i;
194
195 for (i = 0; i < cls->n_rules; i++) {
196 struct test_rule *pos = cls->rules[i];
197 uint32_t wildcards = pos->cls_rule.wc.wildcards;
198 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
199 && match(&pos->cls_rule, flow)) {
200 return &pos->cls_rule;
201 }
202 }
203 return NULL;
204 }
205
206 static void
207 tcls_delete_matches(struct tcls *cls,
208 const struct cls_rule *target,
209 int include)
210 {
211 size_t i;
212
213 for (i = 0; i < cls->n_rules; ) {
214 struct test_rule *pos = cls->rules[i];
215 uint32_t wildcards = pos->cls_rule.wc.wildcards;
216 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
217 && match(target, &pos->cls_rule.flow)) {
218 tcls_remove(cls, pos);
219 } else {
220 i++;
221 }
222 }
223 }
224 \f
225 #ifdef WORDS_BIGENDIAN
226 #define T_HTONL(VALUE) ((uint32_t) (VALUE))
227 #define T_HTONS(VALUE) ((uint32_t) (VALUE))
228 #else
229 #define T_HTONL(VALUE) (((((uint32_t) (VALUE)) & 0x000000ff) << 24) | \
230 ((((uint32_t) (VALUE)) & 0x0000ff00) << 8) | \
231 ((((uint32_t) (VALUE)) & 0x00ff0000) >> 8) | \
232 ((((uint32_t) (VALUE)) & 0xff000000) >> 24))
233 #define T_HTONS(VALUE) (((((uint16_t) (VALUE)) & 0xff00) >> 8) | \
234 ((((uint16_t) (VALUE)) & 0x00ff) << 8))
235 #endif
236
237 static uint32_t nw_src_values[] = { T_HTONL(0xc0a80001),
238 T_HTONL(0xc0a04455) };
239 static uint32_t nw_dst_values[] = { T_HTONL(0xc0a80002),
240 T_HTONL(0xc0a04455) };
241 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
242 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
243 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
244 static uint16_t dl_type_values[]
245 = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
246 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
247 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
248 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
249 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
250 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
251 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
252 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
253 static uint8_t nw_tos_values[] = { 49, 0 };
254
255 static void *values[CLS_N_FIELDS][2];
256
257 static void
258 init_values(void)
259 {
260 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
261 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
262
263 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
264 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
265
266 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
267 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
268
269 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
270 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
271
272 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
273 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
274
275 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
276 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
277
278 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
279 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
280
281 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
282 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
283
284 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
285 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
286
287 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
288 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
289
290 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
291 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
292
293 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
294 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
295 }
296
297 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
298 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
299 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
300 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
301 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
302 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
303 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
304 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
305 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
306 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
307 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
308 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
309
310 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
311 N_NW_DST_VALUES * \
312 N_IN_PORT_VALUES * \
313 N_DL_VLAN_VALUES * \
314 N_DL_VLAN_PCP_VALUES * \
315 N_DL_TYPE_VALUES * \
316 N_TP_SRC_VALUES * \
317 N_TP_DST_VALUES * \
318 N_DL_SRC_VALUES * \
319 N_DL_DST_VALUES * \
320 N_NW_PROTO_VALUES * \
321 N_NW_TOS_VALUES)
322
323 static unsigned int
324 get_value(unsigned int *x, unsigned n_values)
325 {
326 unsigned int rem = *x % n_values;
327 *x /= n_values;
328 return rem;
329 }
330
331 static struct cls_rule *
332 lookup_with_include_bits(const struct classifier *cls,
333 const flow_t *flow, int include)
334 {
335 switch (include) {
336 case CLS_INC_WILD:
337 return classifier_lookup_wild(cls, flow);
338 case CLS_INC_EXACT:
339 return classifier_lookup_exact(cls, flow);
340 case CLS_INC_WILD | CLS_INC_EXACT:
341 return classifier_lookup(cls, flow);
342 default:
343 abort();
344 }
345 }
346
347 static void
348 compare_classifiers(struct classifier *cls, struct tcls *tcls)
349 {
350 unsigned int i;
351
352 assert(classifier_count(cls) == tcls->n_rules);
353 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
354 for (i = 0; i < N_FLOW_VALUES; i++) {
355 struct cls_rule *cr0, *cr1;
356 flow_t flow;
357 unsigned int x;
358 int include;
359
360 x = i;
361 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
362 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
363 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
364 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
365 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
366 N_DL_VLAN_PCP_VALUES)];
367 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
368 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
369 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
370 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
371 ETH_ADDR_LEN);
372 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
373 ETH_ADDR_LEN);
374 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
375 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
376 memset(flow.reserved, 0, sizeof flow.reserved);
377
378 for (include = 1; include <= 3; include++) {
379 cr0 = lookup_with_include_bits(cls, &flow, include);
380 cr1 = tcls_lookup(tcls, &flow, include);
381 assert((cr0 == NULL) == (cr1 == NULL));
382 if (cr0 != NULL) {
383 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
384 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
385
386 assert(flow_equal(&cr0->flow, &cr1->flow));
387 assert(cr0->wc.wildcards == cr1->wc.wildcards);
388 assert(cr0->priority == cr1->priority);
389 /* Skip nw_src_mask and nw_dst_mask, because they are derived
390 * members whose values are used only for optimization. */
391 assert(tr0->aux == tr1->aux);
392 }
393 }
394 }
395 }
396
397 static void
398 free_rule(struct cls_rule *cls_rule, void *cls)
399 {
400 classifier_remove(cls, cls_rule);
401 free(test_rule_from_cls_rule(cls_rule));
402 }
403
404 static void
405 destroy_classifier(struct classifier *cls)
406 {
407 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
408 classifier_destroy(cls);
409 }
410
411 static void
412 check_tables(const struct classifier *cls,
413 int n_tables, int n_buckets, int n_rules)
414 {
415 int found_tables = 0;
416 int found_buckets = 0;
417 int found_rules = 0;
418 int i;
419
420 BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
421 for (i = 0; i < CLS_N_FIELDS; i++) {
422 const struct cls_bucket *bucket;
423 if (!hmap_is_empty(&cls->tables[i])) {
424 found_tables++;
425 }
426 HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
427 found_buckets++;
428 assert(!list_is_empty(&bucket->rules));
429 found_rules += list_size(&bucket->rules);
430 }
431 }
432
433 if (!hmap_is_empty(&cls->exact_table)) {
434 found_tables++;
435 found_buckets++;
436 found_rules += hmap_count(&cls->exact_table);
437 }
438
439 assert(n_tables == -1 || found_tables == n_tables);
440 assert(n_rules == -1 || found_rules == n_rules);
441 assert(n_buckets == -1 || found_buckets == n_buckets);
442 }
443
444 static struct test_rule *
445 make_rule(int wc_fields, unsigned int priority, int value_pat)
446 {
447 const struct cls_field *f;
448 struct test_rule *rule;
449 uint32_t wildcards;
450 flow_t flow;
451
452 wildcards = 0;
453 memset(&flow, 0, sizeof flow);
454 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
455 int f_idx = f - cls_fields;
456 if (wc_fields & (1u << f_idx)) {
457 wildcards |= f->wildcards;
458 } else {
459 int value_idx = (value_pat & (1u << f_idx)) != 0;
460 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
461 }
462 }
463
464 rule = xzalloc(sizeof *rule);
465 cls_rule_from_flow(&rule->cls_rule, &flow, wildcards,
466 !wildcards ? UINT_MAX : priority);
467 return rule;
468 }
469
470 static void
471 shuffle(unsigned int *p, size_t n)
472 {
473 for (; n > 1; n--, p++) {
474 unsigned int *q = &p[rand() % n];
475 unsigned int tmp = *p;
476 *p = *q;
477 *q = tmp;
478 }
479 }
480 \f
481 /* Tests an empty classifier. */
482 static void
483 test_empty(void)
484 {
485 struct classifier cls;
486 struct tcls tcls;
487
488 classifier_init(&cls);
489 tcls_init(&tcls);
490 assert(classifier_is_empty(&cls));
491 assert(tcls_is_empty(&tcls));
492 compare_classifiers(&cls, &tcls);
493 classifier_destroy(&cls);
494 tcls_destroy(&tcls);
495 }
496
497 /* Destroys a null classifier. */
498 static void
499 test_destroy_null(void)
500 {
501 classifier_destroy(NULL);
502 }
503
504 /* Tests classification with one rule at a time. */
505 static void
506 test_single_rule(void)
507 {
508 unsigned int wc_fields; /* Hilarious. */
509
510 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
511 struct classifier cls;
512 struct test_rule *rule, *tcls_rule;
513 struct tcls tcls;
514
515 rule = make_rule(wc_fields,
516 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
517
518 classifier_init(&cls);
519 tcls_init(&tcls);
520
521 tcls_rule = tcls_insert(&tcls, rule);
522 if (wc_fields) {
523 assert(!classifier_insert(&cls, &rule->cls_rule));
524 } else {
525 classifier_insert_exact(&cls, &rule->cls_rule);
526 }
527 check_tables(&cls, 1, 1, 1);
528 compare_classifiers(&cls, &tcls);
529
530 classifier_remove(&cls, &rule->cls_rule);
531 tcls_remove(&tcls, tcls_rule);
532 assert(classifier_is_empty(&cls));
533 assert(tcls_is_empty(&tcls));
534 compare_classifiers(&cls, &tcls);
535
536 free(rule);
537 classifier_destroy(&cls);
538 tcls_destroy(&tcls);
539 }
540 }
541
542 /* Tests replacing one rule by another. */
543 static void
544 test_rule_replacement(void)
545 {
546 unsigned int wc_fields;
547
548 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
549 struct classifier cls;
550 struct test_rule *rule1;
551 struct test_rule *rule2;
552 struct tcls tcls;
553
554 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
555 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
556 rule2->aux += 5;
557 rule2->aux += 5;
558
559 classifier_init(&cls);
560 tcls_init(&tcls);
561 tcls_insert(&tcls, rule1);
562 assert(!classifier_insert(&cls, &rule1->cls_rule));
563 check_tables(&cls, 1, 1, 1);
564 compare_classifiers(&cls, &tcls);
565 tcls_destroy(&tcls);
566
567 tcls_init(&tcls);
568 tcls_insert(&tcls, rule2);
569 assert(test_rule_from_cls_rule(
570 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
571 free(rule1);
572 check_tables(&cls, 1, 1, 1);
573 compare_classifiers(&cls, &tcls);
574 tcls_destroy(&tcls);
575 destroy_classifier(&cls);
576 }
577 }
578
579 static int
580 table_mask(int table)
581 {
582 return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
583 }
584
585 static int
586 random_wcf_in_table(int table, int seed)
587 {
588 int wc_fields = (1u << table) | hash_int(seed, 0);
589 return wc_fields & table_mask(table);
590 }
591
592 /* Tests classification with two rules at a time that fall into the same
593 * bucket. */
594 static void
595 test_two_rules_in_one_bucket(void)
596 {
597 int table, rel_pri, wcf_pat, value_pat;
598
599 for (table = 0; table <= CLS_N_FIELDS; table++) {
600 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
601 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
602 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
603 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
604 struct test_rule *rule1, *tcls_rule1;
605 struct test_rule *rule2, *tcls_rule2;
606 struct test_rule *displaced_rule;
607 struct classifier cls;
608 struct tcls tcls;
609 unsigned int pri1, pri2;
610 int wcf1, wcf2;
611
612 if (table != CLS_F_IDX_EXACT) {
613 /* We can use identical priorities in this test because
614 * the classifier always chooses the rule added later
615 * for equal-priority rules that fall into the same
616 * bucket. */
617 pri1 = table * 257 + 50;
618 pri2 = pri1 + rel_pri;
619
620 wcf1 = (wcf_pat & 1
621 ? random_wcf_in_table(table, pri1)
622 : 1u << table);
623 wcf2 = (wcf_pat & 2
624 ? random_wcf_in_table(table, pri2)
625 : 1u << table);
626 if (value_pat) {
627 wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
628 wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
629 }
630 } else {
631 /* This classifier always puts exact-match rules at
632 * maximum priority. */
633 pri1 = pri2 = UINT_MAX;
634
635 /* No wildcard fields. */
636 wcf1 = wcf2 = 0;
637 }
638
639 rule1 = make_rule(wcf1, pri1, 0);
640 rule2 = make_rule(wcf2, pri2,
641 value_pat << (CLS_N_FIELDS - 1));
642
643 classifier_init(&cls);
644 tcls_init(&tcls);
645
646 tcls_rule1 = tcls_insert(&tcls, rule1);
647 tcls_rule2 = tcls_insert(&tcls, rule2);
648 assert(!classifier_insert(&cls, &rule1->cls_rule));
649 displaced_rule = test_rule_from_cls_rule(
650 classifier_insert(&cls, &rule2->cls_rule));
651 if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
652 assert(!displaced_rule);
653
654 check_tables(&cls, 1, 1, 2);
655 compare_classifiers(&cls, &tcls);
656
657 classifier_remove(&cls, &rule1->cls_rule);
658 tcls_remove(&tcls, tcls_rule1);
659 check_tables(&cls, 1, 1, 1);
660 compare_classifiers(&cls, &tcls);
661 } else {
662 assert(displaced_rule == rule1);
663 check_tables(&cls, 1, 1, 1);
664 compare_classifiers(&cls, &tcls);
665 }
666 free(rule1);
667
668 classifier_remove(&cls, &rule2->cls_rule);
669 tcls_remove(&tcls, tcls_rule2);
670 compare_classifiers(&cls, &tcls);
671 free(rule2);
672
673 destroy_classifier(&cls);
674 tcls_destroy(&tcls);
675 }
676 }
677 }
678 }
679 }
680
681 /* Tests classification with two rules at a time that fall into the same
682 * table but different buckets. */
683 static void
684 test_two_rules_in_one_table(void)
685 {
686 int table, rel_pri, wcf_pat;
687
688 /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
689 for (table = 1; table < CLS_N_FIELDS; table++) {
690 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
691 for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
692 struct test_rule *rule1, *tcls_rule1;
693 struct test_rule *rule2, *tcls_rule2;
694 struct classifier cls;
695 struct tcls tcls;
696 unsigned int pri1, pri2;
697 int wcf1, wcf2;
698 int value_mask, value_pat1, value_pat2;
699 int i;
700
701 /* We can use identical priorities in this test because the
702 * classifier always chooses the rule added later for
703 * equal-priority rules that fall into the same table. */
704 pri1 = table * 257 + 50;
705 pri2 = pri1 + rel_pri;
706
707 if (wcf_pat & 4) {
708 wcf1 = wcf2 = random_wcf_in_table(table, pri1);
709 } else {
710 wcf1 = (wcf_pat & 1
711 ? random_wcf_in_table(table, pri1)
712 : 1u << table);
713 wcf2 = (wcf_pat & 2
714 ? random_wcf_in_table(table, pri2)
715 : 1u << table);
716 }
717
718 /* Generate value patterns that will put the two rules into
719 * different buckets. */
720 value_mask = ((1u << table) - 1);
721 value_pat1 = hash_int(pri1, 1) & value_mask;
722 i = 0;
723 do {
724 value_pat2 = (hash_int(pri2, i++) & value_mask);
725 } while (value_pat1 == value_pat2);
726 rule1 = make_rule(wcf1, pri1, value_pat1);
727 rule2 = make_rule(wcf2, pri2, value_pat2);
728
729 classifier_init(&cls);
730 tcls_init(&tcls);
731
732 tcls_rule1 = tcls_insert(&tcls, rule1);
733 tcls_rule2 = tcls_insert(&tcls, rule2);
734 assert(!classifier_insert(&cls, &rule1->cls_rule));
735 assert(!classifier_insert(&cls, &rule2->cls_rule));
736 check_tables(&cls, 1, 2, 2);
737 compare_classifiers(&cls, &tcls);
738
739 classifier_remove(&cls, &rule1->cls_rule);
740 tcls_remove(&tcls, tcls_rule1);
741 check_tables(&cls, 1, 1, 1);
742 compare_classifiers(&cls, &tcls);
743 free(rule1);
744
745 classifier_remove(&cls, &rule2->cls_rule);
746 tcls_remove(&tcls, tcls_rule2);
747 compare_classifiers(&cls, &tcls);
748 free(rule2);
749
750 classifier_destroy(&cls);
751 tcls_destroy(&tcls);
752 }
753 }
754 }
755 }
756
757 /* Tests classification with two rules at a time that fall into different
758 * tables. */
759 static void
760 test_two_rules_in_different_tables(void)
761 {
762 int table1, table2, rel_pri, wcf_pat;
763
764 for (table1 = 0; table1 < CLS_N_FIELDS; table1++) {
765 for (table2 = table1 + 1; table2 <= CLS_N_FIELDS; table2++) {
766 for (rel_pri = 0; rel_pri < 2; rel_pri++) {
767 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
768 struct test_rule *rule1, *tcls_rule1;
769 struct test_rule *rule2, *tcls_rule2;
770 struct classifier cls;
771 struct tcls tcls;
772 unsigned int pri1, pri2;
773 int wcf1, wcf2;
774
775 /* We must use unique priorities in this test because the
776 * classifier makes the rule choice undefined for rules of
777 * equal priority that fall into different tables. (In
778 * practice, lower-numbered tables win.) */
779 pri1 = table1 * 257 + 50;
780 pri2 = rel_pri ? pri1 - 1 : pri1 + 1;
781
782 wcf1 = (wcf_pat & 1
783 ? random_wcf_in_table(table1, pri1)
784 : 1u << table1);
785 wcf2 = (wcf_pat & 2
786 ? random_wcf_in_table(table2, pri2)
787 : 1u << table2);
788
789 if (table2 == CLS_F_IDX_EXACT) {
790 pri2 = UINT16_MAX;
791 wcf2 = 0;
792 }
793
794 rule1 = make_rule(wcf1, pri1, 0);
795 rule2 = make_rule(wcf2, pri2, 0);
796
797 classifier_init(&cls);
798 tcls_init(&tcls);
799
800 tcls_rule1 = tcls_insert(&tcls, rule1);
801 tcls_rule2 = tcls_insert(&tcls, rule2);
802 assert(!classifier_insert(&cls, &rule1->cls_rule));
803 assert(!classifier_insert(&cls, &rule2->cls_rule));
804 check_tables(&cls, 2, 2, 2);
805 compare_classifiers(&cls, &tcls);
806
807 classifier_remove(&cls, &rule1->cls_rule);
808 tcls_remove(&tcls, tcls_rule1);
809 check_tables(&cls, 1, 1, 1);
810 compare_classifiers(&cls, &tcls);
811 free(rule1);
812
813 classifier_remove(&cls, &rule2->cls_rule);
814 tcls_remove(&tcls, tcls_rule2);
815 compare_classifiers(&cls, &tcls);
816 free(rule2);
817
818 classifier_destroy(&cls);
819 tcls_destroy(&tcls);
820 }
821 }
822 }
823 }
824 }
825
826 /* Tests classification with many rules at a time that fall into the same
827 * bucket but have unique priorities (and various wildcards). */
828 static void
829 test_many_rules_in_one_bucket(void)
830 {
831 enum { MAX_RULES = 50 };
832 int iteration, table;
833
834 for (iteration = 0; iteration < 3; iteration++) {
835 for (table = 0; table <= CLS_N_FIELDS; table++) {
836 unsigned int priorities[MAX_RULES];
837 struct classifier cls;
838 struct tcls tcls;
839 int i;
840
841 srand(hash_int(table, iteration));
842 for (i = 0; i < MAX_RULES; i++) {
843 priorities[i] = i * 129;
844 }
845 shuffle(priorities, ARRAY_SIZE(priorities));
846
847 classifier_init(&cls);
848 tcls_init(&tcls);
849
850 for (i = 0; i < MAX_RULES; i++) {
851 struct test_rule *rule;
852 unsigned int priority = priorities[i];
853 int wcf;
854
855 wcf = random_wcf_in_table(table, priority);
856 rule = make_rule(wcf, priority,
857 table == CLS_F_IDX_EXACT ? i : 1234);
858 tcls_insert(&tcls, rule);
859 assert(!classifier_insert(&cls, &rule->cls_rule));
860 check_tables(&cls, 1, 1, i + 1);
861 compare_classifiers(&cls, &tcls);
862 }
863
864 destroy_classifier(&cls);
865 tcls_destroy(&tcls);
866 }
867 }
868 }
869
870 /* Tests classification with many rules at a time that fall into the same
871 * table but random buckets. */
872 static void
873 test_many_rules_in_one_table(void)
874 {
875 enum { MAX_RULES = 50 };
876 int iteration, table;
877
878 for (iteration = 0; iteration < 3; iteration++) {
879 for (table = 0; table < CLS_N_FIELDS; table++) {
880 unsigned int priorities[MAX_RULES];
881 struct classifier cls;
882 struct tcls tcls;
883 int i;
884
885 srand(hash_int(table, iteration));
886 for (i = 0; i < MAX_RULES; i++) {
887 priorities[i] = i * 129;
888 }
889 shuffle(priorities, ARRAY_SIZE(priorities));
890
891 classifier_init(&cls);
892 tcls_init(&tcls);
893
894 for (i = 0; i < MAX_RULES; i++) {
895 struct test_rule *rule;
896 unsigned int priority = priorities[i];
897 int wcf;
898
899 wcf = random_wcf_in_table(table, priority);
900 rule = make_rule(wcf, priority, hash_int(priority, 1));
901 tcls_insert(&tcls, rule);
902 assert(!classifier_insert(&cls, &rule->cls_rule));
903 check_tables(&cls, 1, -1, i + 1);
904 compare_classifiers(&cls, &tcls);
905 }
906
907 destroy_classifier(&cls);
908 tcls_destroy(&tcls);
909 }
910 }
911 }
912
913 /* Tests classification with many rules at a time that fall into random buckets
914 * in random tables. */
915 static void
916 test_many_rules_in_different_tables(void)
917 {
918 enum { MAX_RULES = 50 };
919 int iteration;
920
921 for (iteration = 0; iteration < 30; iteration++) {
922 unsigned int priorities[MAX_RULES];
923 struct classifier cls;
924 struct tcls tcls;
925 int i;
926
927 srand(iteration);
928 for (i = 0; i < MAX_RULES; i++) {
929 priorities[i] = i * 129;
930 }
931 shuffle(priorities, ARRAY_SIZE(priorities));
932
933 classifier_init(&cls);
934 tcls_init(&tcls);
935
936 for (i = 0; i < MAX_RULES; i++) {
937 struct test_rule *rule;
938 unsigned int priority = priorities[i];
939 int table = rand() % (CLS_N_FIELDS + 1);
940 int wcf = random_wcf_in_table(table, rand());
941 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
942 rule = make_rule(wcf, priority, value_pat);
943 tcls_insert(&tcls, rule);
944 assert(!classifier_insert(&cls, &rule->cls_rule));
945 check_tables(&cls, -1, -1, i + 1);
946 compare_classifiers(&cls, &tcls);
947 }
948
949 while (!classifier_is_empty(&cls)) {
950 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
951 sizeof(struct test_rule));
952 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
953 include |= (rule->cls_rule.wc.wildcards
954 ? CLS_INC_WILD : CLS_INC_EXACT);
955 classifier_for_each_match(&cls, &rule->cls_rule, include,
956 free_rule, &cls);
957 tcls_delete_matches(&tcls, &rule->cls_rule, include);
958 compare_classifiers(&cls, &tcls);
959 free(rule);
960 }
961 putchar('.');
962 fflush(stdout);
963
964 destroy_classifier(&cls);
965 tcls_destroy(&tcls);
966 }
967 }
968 \f
969 static void
970 run_test(void (*function)(void))
971 {
972 function();
973 putchar('.');
974 fflush(stdout);
975 }
976
977 int
978 main(void)
979 {
980 init_values();
981 run_test(test_empty);
982 run_test(test_destroy_null);
983 run_test(test_single_rule);
984 run_test(test_rule_replacement);
985 run_test(test_two_rules_in_one_bucket);
986 run_test(test_two_rules_in_one_table);
987 run_test(test_two_rules_in_different_tables);
988 run_test(test_many_rules_in_one_bucket);
989 run_test(test_many_rules_in_one_table);
990 run_test(test_many_rules_in_different_tables);
991 putchar('\n');
992 return 0;
993 }