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