Merge "citrix" branch into "master.
[sliver-openvswitch.git] / tests / test-classifier.c
1 /*
2  * Copyright (c) 2009 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 uint16_t dl_type_values[]
244             = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
245 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
246 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
247 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
248                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
249 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
250                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
251 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
252
253 static void *values[CLS_N_FIELDS][2];
254
255 static void
256 init_values(void)
257 {
258     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
259     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
260
261     values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
262     values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
263
264     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
265     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
266
267     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
268     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
269
270     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
271     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
272
273     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
274     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
275
276     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
277     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
278
279     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
280     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
281
282     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
283     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
284
285     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
286     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
287 }
288
289 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
290 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
291 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
292 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
293 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
294 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
295 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
296 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
297 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
298 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
299
300 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
301                        N_NW_DST_VALUES *        \
302                        N_IN_PORT_VALUES *       \
303                        N_DL_VLAN_VALUES *       \
304                        N_DL_TYPE_VALUES *       \
305                        N_TP_SRC_VALUES *        \
306                        N_TP_DST_VALUES *        \
307                        N_DL_SRC_VALUES *        \
308                        N_DL_DST_VALUES *        \
309                        N_NW_PROTO_VALUES)
310
311 static unsigned int
312 get_value(unsigned int *x, unsigned n_values)
313 {
314     unsigned int rem = *x % n_values;
315     *x /= n_values;
316     return rem;
317 }
318
319 static struct cls_rule *
320 lookup_with_include_bits(const struct classifier *cls,
321                          const flow_t *flow, int include)
322 {
323     switch (include) {
324     case CLS_INC_WILD:
325         return classifier_lookup_wild(cls, flow);
326     case CLS_INC_EXACT:
327         return classifier_lookup_exact(cls, flow);
328     case CLS_INC_WILD | CLS_INC_EXACT:
329         return classifier_lookup(cls, flow);
330     default:
331         abort();
332     }
333 }
334
335 static void
336 compare_classifiers(struct classifier *cls, struct tcls *tcls)
337 {
338     unsigned int i;
339
340     assert(classifier_count(cls) == tcls->n_rules);
341     assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
342     for (i = 0; i < N_FLOW_VALUES; i++) {
343         struct cls_rule *cr0, *cr1;
344         flow_t flow;
345         unsigned int x;
346         int include;
347
348         x = i;
349         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
350         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
351         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
352         flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
353         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
354         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
355         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
356         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
357                ETH_ADDR_LEN);
358         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
359                ETH_ADDR_LEN);
360         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
361         flow.reserved = 0;
362
363         for (include = 1; include <= 3; include++) {
364             cr0 = lookup_with_include_bits(cls, &flow, include);
365             cr1 = tcls_lookup(tcls, &flow, include);
366             assert((cr0 == NULL) == (cr1 == NULL));
367             if (cr0 != NULL) {
368                 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
369                 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
370
371                 assert(flow_equal(&cr0->flow, &cr1->flow));
372                 assert(cr0->wc.wildcards == cr1->wc.wildcards);
373                 assert(cr0->priority == cr1->priority);
374                 /* Skip nw_src_mask and nw_dst_mask, because they are derived
375                  * members whose values are used only for optimization. */
376                 assert(tr0->aux == tr1->aux);
377             }
378         }
379     }
380 }
381
382 static void
383 free_rule(struct cls_rule *cls_rule, void *cls)
384 {
385     classifier_remove(cls, cls_rule);
386     free(test_rule_from_cls_rule(cls_rule));
387 }
388
389 static void
390 destroy_classifier(struct classifier *cls)
391 {
392     classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
393     classifier_destroy(cls);
394 }
395
396 static void
397 check_tables(const struct classifier *cls,
398              int n_tables, int n_buckets, int n_rules)
399 {
400     int found_tables = 0;
401     int found_buckets = 0;
402     int found_rules = 0;
403     int i;
404
405     BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
406     for (i = 0; i < CLS_N_FIELDS; i++) {
407         const struct cls_bucket *bucket;
408         if (!hmap_is_empty(&cls->tables[i])) {
409             found_tables++;
410         }
411         HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
412             found_buckets++;
413             assert(!list_is_empty(&bucket->rules));
414             found_rules += list_size(&bucket->rules);
415         }
416     }
417
418     if (!hmap_is_empty(&cls->exact_table)) {
419         found_tables++;
420         found_buckets++;
421         found_rules += hmap_count(&cls->exact_table);
422     }
423
424     assert(n_tables == -1 || found_tables == n_tables);
425     assert(n_rules == -1 || found_rules == n_rules);
426     assert(n_buckets == -1 || found_buckets == n_buckets);
427 }
428
429 static struct test_rule *
430 make_rule(int wc_fields, unsigned int priority, int value_pat)
431 {
432     const struct cls_field *f;
433     struct test_rule *rule;
434     uint32_t wildcards;
435     flow_t flow;
436
437     wildcards = 0;
438     memset(&flow, 0, sizeof flow);
439     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
440         int f_idx = f - cls_fields;
441         if (wc_fields & (1u << f_idx)) {
442             wildcards |= f->wildcards;
443         } else {
444             int value_idx = (value_pat & (1u << f_idx)) != 0;
445             memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
446         }
447     }
448
449     rule = xcalloc(1, sizeof *rule);
450     cls_rule_from_flow(&rule->cls_rule, &flow, wildcards,
451                        !wildcards ? UINT_MAX : priority);
452     return rule;
453 }
454
455 static void
456 shuffle(unsigned int *p, size_t n)
457 {
458     for (; n > 1; n--, p++) {
459         unsigned int *q = &p[rand() % n];
460         unsigned int tmp = *p;
461         *p = *q;
462         *q = tmp;
463     }
464 }
465 \f
466 /* Tests an empty classifier. */
467 static void
468 test_empty(void)
469 {
470     struct classifier cls;
471     struct tcls tcls;
472
473     classifier_init(&cls);
474     tcls_init(&tcls);
475     assert(classifier_is_empty(&cls));
476     assert(tcls_is_empty(&tcls));
477     compare_classifiers(&cls, &tcls);
478     classifier_destroy(&cls);
479     tcls_destroy(&tcls);
480 }
481
482 /* Destroys a null classifier. */
483 static void
484 test_destroy_null(void)
485 {
486     classifier_destroy(NULL);
487 }
488
489 /* Tests classification with one rule at a time. */
490 static void
491 test_single_rule(void)
492 {
493     unsigned int wc_fields;     /* Hilarious. */
494
495     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
496         struct classifier cls;
497         struct test_rule *rule, *tcls_rule;
498         struct tcls tcls;
499
500         rule = make_rule(wc_fields,
501                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
502
503         classifier_init(&cls);
504         tcls_init(&tcls);
505
506         tcls_rule = tcls_insert(&tcls, rule);
507         if (wc_fields) {
508             assert(!classifier_insert(&cls, &rule->cls_rule));
509         } else {
510             classifier_insert_exact(&cls, &rule->cls_rule);
511         }
512         check_tables(&cls, 1, 1, 1);
513         compare_classifiers(&cls, &tcls);
514
515         classifier_remove(&cls, &rule->cls_rule);
516         tcls_remove(&tcls, tcls_rule);
517         assert(classifier_is_empty(&cls));
518         assert(tcls_is_empty(&tcls));
519         compare_classifiers(&cls, &tcls);
520
521         free(rule);
522         classifier_destroy(&cls);
523         tcls_destroy(&tcls);
524     }
525 }
526
527 /* Tests replacing one rule by another. */
528 static void
529 test_rule_replacement(void)
530 {
531     unsigned int wc_fields;
532
533     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
534         struct classifier cls;
535         struct test_rule *rule1, *tcls_rule1;
536         struct test_rule *rule2, *tcls_rule2;
537         struct tcls tcls;
538
539         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
540         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
541         rule2->aux += 5;
542         rule2->aux += 5;
543
544         classifier_init(&cls);
545         tcls_init(&tcls);
546         tcls_rule1 = tcls_insert(&tcls, rule1);
547         assert(!classifier_insert(&cls, &rule1->cls_rule));
548         check_tables(&cls, 1, 1, 1);
549         compare_classifiers(&cls, &tcls);
550         tcls_destroy(&tcls);
551
552         tcls_init(&tcls);
553         tcls_rule2 = tcls_insert(&tcls, rule2);
554         assert(test_rule_from_cls_rule(
555                    classifier_insert(&cls, &rule2->cls_rule)) == rule1);
556         free(rule1);
557         check_tables(&cls, 1, 1, 1);
558         compare_classifiers(&cls, &tcls);
559         tcls_destroy(&tcls);
560         destroy_classifier(&cls);
561     }
562 }
563
564 static int
565 table_mask(int table)
566 {
567     return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
568 }
569
570 static int
571 random_wcf_in_table(int table, int seed)
572 {
573     int wc_fields = (1u << table) | hash_int(seed, 0);
574     return wc_fields & table_mask(table);
575 }
576
577 /* Tests classification with two rules at a time that fall into the same
578  * bucket. */
579 static void
580 test_two_rules_in_one_bucket(void)
581 {
582     int table, rel_pri, wcf_pat, value_pat;
583
584     for (table = 0; table <= CLS_N_FIELDS; table++) {
585         for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
586             for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
587                 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
588                 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
589                     struct test_rule *rule1, *tcls_rule1;
590                     struct test_rule *rule2, *tcls_rule2;
591                     struct test_rule *displaced_rule;
592                     struct classifier cls;
593                     struct tcls tcls;
594                     unsigned int pri1, pri2;
595                     int wcf1, wcf2;
596
597                     if (table != CLS_F_IDX_EXACT) {
598                         /* We can use identical priorities in this test because
599                          * the classifier always chooses the rule added later
600                          * for equal-priority rules that fall into the same
601                          * bucket.  */
602                         pri1 = table * 257 + 50;
603                         pri2 = pri1 + rel_pri;
604
605                         wcf1 = (wcf_pat & 1
606                                 ? random_wcf_in_table(table, pri1)
607                                 : 1u << table);
608                         wcf2 = (wcf_pat & 2
609                                 ? random_wcf_in_table(table, pri2)
610                                 : 1u << table);
611                         if (value_pat) {
612                             wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
613                             wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
614                         }
615                     } else {
616                         /* This classifier always puts exact-match rules at
617                          * maximum priority.  */
618                         pri1 = pri2 = UINT_MAX;
619
620                         /* No wildcard fields. */
621                         wcf1 = wcf2 = 0;
622                     }
623
624                     rule1 = make_rule(wcf1, pri1, 0);
625                     rule2 = make_rule(wcf2, pri2,
626                                       value_pat << (CLS_N_FIELDS - 1));
627
628                     classifier_init(&cls);
629                     tcls_init(&tcls);
630
631                     tcls_rule1 = tcls_insert(&tcls, rule1);
632                     tcls_rule2 = tcls_insert(&tcls, rule2);
633                     assert(!classifier_insert(&cls, &rule1->cls_rule));
634                     displaced_rule = test_rule_from_cls_rule(
635                         classifier_insert(&cls, &rule2->cls_rule));
636                     if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
637                         assert(!displaced_rule);
638
639                         check_tables(&cls, 1, 1, 2);
640                         compare_classifiers(&cls, &tcls);
641
642                         classifier_remove(&cls, &rule1->cls_rule);
643                         tcls_remove(&tcls, tcls_rule1);
644                         check_tables(&cls, 1, 1, 1);
645                         compare_classifiers(&cls, &tcls);
646                     } else {
647                         assert(displaced_rule == rule1);
648                         check_tables(&cls, 1, 1, 1);
649                         compare_classifiers(&cls, &tcls);
650                     }
651                     free(rule1);
652
653                     classifier_remove(&cls, &rule2->cls_rule);
654                     tcls_remove(&tcls, tcls_rule2);
655                     compare_classifiers(&cls, &tcls);
656                     free(rule2);
657
658                     destroy_classifier(&cls);
659                     tcls_destroy(&tcls);
660                 }
661             }
662         }
663     }
664 }
665
666 /* Tests classification with two rules at a time that fall into the same
667  * table but different buckets. */
668 static void
669 test_two_rules_in_one_table(void)
670 {
671     int table, rel_pri, wcf_pat;
672
673     /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
674     for (table = 1; table < CLS_N_FIELDS; table++) {
675         for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
676             for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
677                 struct test_rule *rule1, *tcls_rule1;
678                 struct test_rule *rule2, *tcls_rule2;
679                 struct classifier cls;
680                 struct tcls tcls;
681                 unsigned int pri1, pri2;
682                 int wcf1, wcf2;
683                 int value_mask, value_pat1, value_pat2;
684                 int i;
685
686                 /* We can use identical priorities in this test because the
687                  * classifier always chooses the rule added later for
688                  * equal-priority rules that fall into the same table.  */
689                 pri1 = table * 257 + 50;
690                 pri2 = pri1 + rel_pri;
691
692                 if (wcf_pat & 4) {
693                     wcf1 = wcf2 = random_wcf_in_table(table, pri1);
694                 } else {
695                     wcf1 = (wcf_pat & 1
696                             ? random_wcf_in_table(table, pri1)
697                             : 1u << table);
698                     wcf2 = (wcf_pat & 2
699                             ? random_wcf_in_table(table, pri2)
700                             : 1u << table);
701                 }
702
703                 /* Generate value patterns that will put the two rules into
704                  * different buckets. */
705                 value_mask = ((1u << table) - 1);
706                 value_pat1 = hash_int(pri1, 1) & value_mask;
707                 i = 0;
708                 do {
709                     value_pat2 = (hash_int(pri2, i++) & value_mask);
710                 } while (value_pat1 == value_pat2);
711                 rule1 = make_rule(wcf1, pri1, value_pat1);
712                 rule2 = make_rule(wcf2, pri2, value_pat2);
713
714                 classifier_init(&cls);
715                 tcls_init(&tcls);
716
717                 tcls_rule1 = tcls_insert(&tcls, rule1);
718                 tcls_rule2 = tcls_insert(&tcls, rule2);
719                 assert(!classifier_insert(&cls, &rule1->cls_rule));
720                 assert(!classifier_insert(&cls, &rule2->cls_rule));
721                 check_tables(&cls, 1, 2, 2);
722                 compare_classifiers(&cls, &tcls);
723
724                 classifier_remove(&cls, &rule1->cls_rule);
725                 tcls_remove(&tcls, tcls_rule1);
726                 check_tables(&cls, 1, 1, 1);
727                 compare_classifiers(&cls, &tcls);
728                 free(rule1);
729
730                 classifier_remove(&cls, &rule2->cls_rule);
731                 tcls_remove(&tcls, tcls_rule2);
732                 compare_classifiers(&cls, &tcls);
733                 free(rule2);
734
735                 classifier_destroy(&cls);
736                 tcls_destroy(&tcls);
737             }
738         }
739     }
740 }
741
742 /* Tests classification with two rules at a time that fall into different
743  * tables. */
744 static void
745 test_two_rules_in_different_tables(void)
746 {
747     int table1, table2, rel_pri, wcf_pat;
748
749     for (table1 = 0; table1 < CLS_N_FIELDS; table1++) {
750         for (table2 = table1 + 1; table2 <= CLS_N_FIELDS; table2++) {
751             for (rel_pri = 0; rel_pri < 2; rel_pri++) {
752                 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
753                     struct test_rule *rule1, *tcls_rule1;
754                     struct test_rule *rule2, *tcls_rule2;
755                     struct classifier cls;
756                     struct tcls tcls;
757                     unsigned int pri1, pri2;
758                     int wcf1, wcf2;
759
760                     /* We must use unique priorities in this test because the
761                      * classifier makes the rule choice undefined for rules of
762                      * equal priority that fall into different tables.  (In
763                      * practice, lower-numbered tables win.)  */
764                     pri1 = table1 * 257 + 50;
765                     pri2 = rel_pri ? pri1 - 1 : pri1 + 1;
766
767                     wcf1 = (wcf_pat & 1
768                             ? random_wcf_in_table(table1, pri1)
769                             : 1u << table1);
770                     wcf2 = (wcf_pat & 2
771                             ? random_wcf_in_table(table2, pri2)
772                             : 1u << table2);
773
774                     if (table2 == CLS_F_IDX_EXACT) {
775                         pri2 = UINT16_MAX;
776                         wcf2 = 0;
777                     }
778
779                     rule1 = make_rule(wcf1, pri1, 0);
780                     rule2 = make_rule(wcf2, pri2, 0);
781
782                     classifier_init(&cls);
783                     tcls_init(&tcls);
784
785                     tcls_rule1 = tcls_insert(&tcls, rule1);
786                     tcls_rule2 = tcls_insert(&tcls, rule2);
787                     assert(!classifier_insert(&cls, &rule1->cls_rule));
788                     assert(!classifier_insert(&cls, &rule2->cls_rule));
789                     check_tables(&cls, 2, 2, 2);
790                     compare_classifiers(&cls, &tcls);
791
792                     classifier_remove(&cls, &rule1->cls_rule);
793                     tcls_remove(&tcls, tcls_rule1);
794                     check_tables(&cls, 1, 1, 1);
795                     compare_classifiers(&cls, &tcls);
796                     free(rule1);
797
798                     classifier_remove(&cls, &rule2->cls_rule);
799                     tcls_remove(&tcls, tcls_rule2);
800                     compare_classifiers(&cls, &tcls);
801                     free(rule2);
802
803                     classifier_destroy(&cls);
804                     tcls_destroy(&tcls);
805                 }
806             }
807         }
808     }
809 }
810
811 /* Tests classification with many rules at a time that fall into the same
812  * bucket but have unique priorities (and various wildcards). */
813 static void
814 test_many_rules_in_one_bucket(void)
815 {
816     enum { MAX_RULES = 50 };
817     int iteration, table;
818
819     for (iteration = 0; iteration < 3; iteration++) {
820         for (table = 0; table <= CLS_N_FIELDS; table++) {
821             unsigned int priorities[MAX_RULES];
822             struct classifier cls;
823             struct tcls tcls;
824             int i;
825
826             srand(hash_int(table, iteration));
827             for (i = 0; i < MAX_RULES; i++) {
828                 priorities[i] = i * 129;
829             }
830             shuffle(priorities, ARRAY_SIZE(priorities));
831
832             classifier_init(&cls);
833             tcls_init(&tcls);
834
835             for (i = 0; i < MAX_RULES; i++) {
836                 struct test_rule *rule;
837                 unsigned int priority = priorities[i];
838                 int wcf;
839
840                 wcf = random_wcf_in_table(table, priority);
841                 rule = make_rule(wcf, priority,
842                                  table == CLS_F_IDX_EXACT ? i : 1234);
843                 tcls_insert(&tcls, rule);
844                 assert(!classifier_insert(&cls, &rule->cls_rule));
845                 check_tables(&cls, 1, 1, i + 1);
846                 compare_classifiers(&cls, &tcls);
847             }
848
849             destroy_classifier(&cls);
850             tcls_destroy(&tcls);
851         }
852     }
853 }
854
855 /* Tests classification with many rules at a time that fall into the same
856  * table but random buckets. */
857 static void
858 test_many_rules_in_one_table(void)
859 {
860     enum { MAX_RULES = 50 };
861     int iteration, table;
862
863     for (iteration = 0; iteration < 3; iteration++) {
864         for (table = 0; table < CLS_N_FIELDS; table++) {
865             unsigned int priorities[MAX_RULES];
866             struct classifier cls;
867             struct tcls tcls;
868             int i;
869
870             srand(hash_int(table, iteration));
871             for (i = 0; i < MAX_RULES; i++) {
872                 priorities[i] = i * 129;
873             }
874             shuffle(priorities, ARRAY_SIZE(priorities));
875
876             classifier_init(&cls);
877             tcls_init(&tcls);
878
879             for (i = 0; i < MAX_RULES; i++) {
880                 struct test_rule *rule;
881                 unsigned int priority = priorities[i];
882                 int wcf;
883
884                 wcf = random_wcf_in_table(table, priority);
885                 rule = make_rule(wcf, priority, hash_int(priority, 1));
886                 tcls_insert(&tcls, rule);
887                 assert(!classifier_insert(&cls, &rule->cls_rule));
888                 check_tables(&cls, 1, -1, i + 1);
889                 compare_classifiers(&cls, &tcls);
890             }
891
892             destroy_classifier(&cls);
893             tcls_destroy(&tcls);
894         }
895     }
896 }
897
898 /* Tests classification with many rules at a time that fall into random buckets
899  * in random tables. */
900 static void
901 test_many_rules_in_different_tables(void)
902 {
903     enum { MAX_RULES = 50 };
904     int iteration;
905
906     for (iteration = 0; iteration < 30; iteration++) {
907         unsigned int priorities[MAX_RULES];
908         struct classifier cls;
909         struct tcls tcls;
910         int i;
911
912         srand(iteration);
913         for (i = 0; i < MAX_RULES; i++) {
914             priorities[i] = i * 129;
915         }
916         shuffle(priorities, ARRAY_SIZE(priorities));
917
918         classifier_init(&cls);
919         tcls_init(&tcls);
920
921         for (i = 0; i < MAX_RULES; i++) {
922             struct test_rule *rule;
923             unsigned int priority = priorities[i];
924             int table = rand() % (CLS_N_FIELDS + 1);
925             int wcf = random_wcf_in_table(table, rand());
926             int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
927             rule = make_rule(wcf, priority, value_pat);
928             tcls_insert(&tcls, rule);
929             assert(!classifier_insert(&cls, &rule->cls_rule));
930             check_tables(&cls, -1, -1, i + 1);
931             compare_classifiers(&cls, &tcls);
932         }
933
934         while (!classifier_is_empty(&cls)) {
935             struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
936                                              sizeof(struct test_rule));
937             int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
938             include |= (rule->cls_rule.wc.wildcards
939                         ? CLS_INC_WILD : CLS_INC_EXACT);
940             classifier_for_each_match(&cls, &rule->cls_rule, include,
941                                       free_rule, &cls);
942             tcls_delete_matches(&tcls, &rule->cls_rule, include);
943             compare_classifiers(&cls, &tcls);
944             free(rule);
945         }
946         putchar('.');
947         fflush(stdout);
948
949         destroy_classifier(&cls);
950         tcls_destroy(&tcls);
951     }
952 }
953 \f
954 static void
955 run_test(void (*function)(void))
956 {
957     function();
958     putchar('.');
959     fflush(stdout);
960 }
961
962 int
963 main(void)
964 {
965     init_values();
966     run_test(test_empty);
967     run_test(test_destroy_null);
968     run_test(test_single_rule);
969     run_test(test_rule_replacement);
970     run_test(test_two_rules_in_one_bucket);
971     run_test(test_two_rules_in_one_table);
972     run_test(test_two_rules_in_different_tables);
973     run_test(test_many_rules_in_one_bucket);
974     run_test(test_many_rules_in_one_table);
975     run_test(test_many_rules_in_different_tables);
976     putchar('\n');
977     return 0;
978 }