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