datapath: Fix build when CONFIG_COMPAT is enabled.
[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 uint32_t tun_id_values[] = { 0, 0xffff0000 };
244 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
245 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
246 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
247 static uint16_t dl_type_values[]
248             = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
249 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
250 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
251 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
252                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
253 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
254                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
255 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
256 static uint8_t nw_tos_values[] = { 49, 0 };
257
258 static void *values[CLS_N_FIELDS][2];
259
260 static void
261 init_values(void)
262 {
263     values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
264     values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
265
266     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
267     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
268
269     values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
270     values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
271
272     values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
273     values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
274
275     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
276     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
277
278     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
279     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
280
281     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
282     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
283
284     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
285     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
286
287     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
288     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
289
290     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
291     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
292
293     values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
294     values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
295
296     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
297     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
298
299     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
300     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
301 }
302
303 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
304 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
305 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
306 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
307 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
308 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
309 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
310 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
311 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
312 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
313 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
314 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
315 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
316
317 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
318                        N_NW_DST_VALUES *        \
319                        N_TUN_ID_VALUES *        \
320                        N_IN_PORT_VALUES *       \
321                        N_DL_VLAN_VALUES *       \
322                        N_DL_VLAN_PCP_VALUES *   \
323                        N_DL_TYPE_VALUES *       \
324                        N_TP_SRC_VALUES *        \
325                        N_TP_DST_VALUES *        \
326                        N_DL_SRC_VALUES *        \
327                        N_DL_DST_VALUES *        \
328                        N_NW_PROTO_VALUES *      \
329                        N_NW_TOS_VALUES)
330
331 static unsigned int
332 get_value(unsigned int *x, unsigned n_values)
333 {
334     unsigned int rem = *x % n_values;
335     *x /= n_values;
336     return rem;
337 }
338
339 static struct cls_rule *
340 lookup_with_include_bits(const struct classifier *cls,
341                          const flow_t *flow, int include)
342 {
343     switch (include) {
344     case CLS_INC_WILD:
345         return classifier_lookup_wild(cls, flow);
346     case CLS_INC_EXACT:
347         return classifier_lookup_exact(cls, flow);
348     case CLS_INC_WILD | CLS_INC_EXACT:
349         return classifier_lookup(cls, flow);
350     default:
351         abort();
352     }
353 }
354
355 static void
356 compare_classifiers(struct classifier *cls, struct tcls *tcls)
357 {
358     unsigned int i;
359
360     assert(classifier_count(cls) == tcls->n_rules);
361     assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
362     for (i = 0; i < N_FLOW_VALUES; i++) {
363         struct cls_rule *cr0, *cr1;
364         flow_t flow;
365         unsigned int x;
366         int include;
367
368         x = i;
369         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
370         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
371         flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
372         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
373         flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
374         flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
375                 N_DL_VLAN_PCP_VALUES)];
376         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
377         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
378         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
379         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
380                ETH_ADDR_LEN);
381         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
382                ETH_ADDR_LEN);
383         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
384         flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
385
386         for (include = 1; include <= 3; include++) {
387             cr0 = lookup_with_include_bits(cls, &flow, include);
388             cr1 = tcls_lookup(tcls, &flow, include);
389             assert((cr0 == NULL) == (cr1 == NULL));
390             if (cr0 != NULL) {
391                 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
392                 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
393
394                 assert(flow_equal(&cr0->flow, &cr1->flow));
395                 assert(cr0->flow.wildcards == cr1->flow.wildcards);
396                 assert(cr0->flow.priority == cr1->flow.priority);
397                 /* Skip nw_src_mask, nw_dst_mask, and dl_tci_mask, because they
398                  * are derived members used only for optimization. */
399                 assert(tr0->aux == tr1->aux);
400             }
401         }
402     }
403 }
404
405 static void
406 free_rule(struct cls_rule *cls_rule, void *cls)
407 {
408     classifier_remove(cls, cls_rule);
409     free(test_rule_from_cls_rule(cls_rule));
410 }
411
412 static void
413 destroy_classifier(struct classifier *cls)
414 {
415     classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
416     classifier_destroy(cls);
417 }
418
419 static void
420 check_tables(const struct classifier *cls,
421              int n_tables, int n_buckets, int n_rules)
422 {
423     int found_tables = 0;
424     int found_buckets = 0;
425     int found_rules = 0;
426     int i;
427
428     BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
429     for (i = 0; i < CLS_N_FIELDS; i++) {
430         const struct cls_bucket *bucket;
431         if (!hmap_is_empty(&cls->tables[i])) {
432             found_tables++;
433         }
434         HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
435             found_buckets++;
436             assert(!list_is_empty(&bucket->rules));
437             found_rules += list_size(&bucket->rules);
438         }
439     }
440
441     if (!hmap_is_empty(&cls->exact_table)) {
442         found_tables++;
443         found_buckets++;
444         found_rules += hmap_count(&cls->exact_table);
445     }
446
447     assert(n_tables == -1 || found_tables == n_tables);
448     assert(n_rules == -1 || found_rules == n_rules);
449     assert(n_buckets == -1 || found_buckets == n_buckets);
450 }
451
452 static struct test_rule *
453 make_rule(int wc_fields, unsigned int priority, int value_pat)
454 {
455     const struct cls_field *f;
456     struct test_rule *rule;
457     flow_t flow;
458
459     memset(&flow, 0, sizeof flow);
460     flow.priority = priority;
461     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
462         int f_idx = f - cls_fields;
463         if (wc_fields & (1u << f_idx)) {
464             flow.wildcards |= f->wildcards;
465         } else {
466             int value_idx = (value_pat & (1u << f_idx)) != 0;
467             memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
468         }
469     }
470
471     rule = xzalloc(sizeof *rule);
472     cls_rule_from_flow(&flow, &rule->cls_rule);
473     return rule;
474 }
475
476 static void
477 shuffle(unsigned int *p, size_t n)
478 {
479     for (; n > 1; n--, p++) {
480         unsigned int *q = &p[rand() % n];
481         unsigned int tmp = *p;
482         *p = *q;
483         *q = tmp;
484     }
485 }
486 \f
487 /* Tests an empty classifier. */
488 static void
489 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
490 {
491     struct classifier cls;
492     struct tcls tcls;
493
494     classifier_init(&cls);
495     tcls_init(&tcls);
496     assert(classifier_is_empty(&cls));
497     assert(tcls_is_empty(&tcls));
498     compare_classifiers(&cls, &tcls);
499     classifier_destroy(&cls);
500     tcls_destroy(&tcls);
501 }
502
503 /* Destroys a null classifier. */
504 static void
505 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
506 {
507     classifier_destroy(NULL);
508 }
509
510 /* Tests classification with one rule at a time. */
511 static void
512 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
513 {
514     unsigned int wc_fields;     /* Hilarious. */
515
516     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
517         struct classifier cls;
518         struct test_rule *rule, *tcls_rule;
519         struct tcls tcls;
520
521         rule = make_rule(wc_fields,
522                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
523
524         classifier_init(&cls);
525         tcls_init(&tcls);
526
527         tcls_rule = tcls_insert(&tcls, rule);
528         if (wc_fields) {
529             assert(!classifier_insert(&cls, &rule->cls_rule));
530         } else {
531             classifier_insert_exact(&cls, &rule->cls_rule);
532         }
533         check_tables(&cls, 1, 1, 1);
534         compare_classifiers(&cls, &tcls);
535
536         classifier_remove(&cls, &rule->cls_rule);
537         tcls_remove(&tcls, tcls_rule);
538         assert(classifier_is_empty(&cls));
539         assert(tcls_is_empty(&tcls));
540         compare_classifiers(&cls, &tcls);
541
542         free(rule);
543         classifier_destroy(&cls);
544         tcls_destroy(&tcls);
545     }
546 }
547
548 /* Tests replacing one rule by another. */
549 static void
550 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
551 {
552     unsigned int wc_fields;
553
554     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
555         struct classifier cls;
556         struct test_rule *rule1;
557         struct test_rule *rule2;
558         struct tcls tcls;
559
560         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
561         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
562         rule2->aux += 5;
563         rule2->aux += 5;
564
565         classifier_init(&cls);
566         tcls_init(&tcls);
567         tcls_insert(&tcls, rule1);
568         assert(!classifier_insert(&cls, &rule1->cls_rule));
569         check_tables(&cls, 1, 1, 1);
570         compare_classifiers(&cls, &tcls);
571         tcls_destroy(&tcls);
572
573         tcls_init(&tcls);
574         tcls_insert(&tcls, rule2);
575         assert(test_rule_from_cls_rule(
576                    classifier_insert(&cls, &rule2->cls_rule)) == rule1);
577         free(rule1);
578         check_tables(&cls, 1, 1, 1);
579         compare_classifiers(&cls, &tcls);
580         tcls_destroy(&tcls);
581         destroy_classifier(&cls);
582     }
583 }
584
585 static int
586 table_mask(int table)
587 {
588     return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
589 }
590
591 static int
592 random_wcf_in_table(int table, int seed)
593 {
594     int wc_fields = (1u << table) | hash_int(seed, 0);
595     return wc_fields & table_mask(table);
596 }
597
598 /* Tests classification with two rules at a time that fall into the same
599  * bucket. */
600 static void
601 test_two_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
602 {
603     int table, rel_pri, wcf_pat, value_pat;
604
605     for (table = 0; table <= CLS_N_FIELDS; table++) {
606         for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
607             for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
608                 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
609                 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
610                     struct test_rule *rule1, *tcls_rule1;
611                     struct test_rule *rule2, *tcls_rule2;
612                     struct test_rule *displaced_rule;
613                     struct classifier cls;
614                     struct tcls tcls;
615                     unsigned int pri1, pri2;
616                     int wcf1, wcf2;
617
618                     if (table != CLS_F_IDX_EXACT) {
619                         /* We can use identical priorities in this test because
620                          * the classifier always chooses the rule added later
621                          * for equal-priority rules that fall into the same
622                          * bucket.  */
623                         pri1 = table * 257 + 50;
624                         pri2 = pri1 + rel_pri;
625
626                         wcf1 = (wcf_pat & 1
627                                 ? random_wcf_in_table(table, pri1)
628                                 : 1u << table);
629                         wcf2 = (wcf_pat & 2
630                                 ? random_wcf_in_table(table, pri2)
631                                 : 1u << table);
632                         if (value_pat) {
633                             wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
634                             wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
635                         }
636                     } else {
637                         /* This classifier always puts exact-match rules at
638                          * maximum priority.  */
639                         pri1 = pri2 = UINT_MAX;
640
641                         /* No wildcard fields. */
642                         wcf1 = wcf2 = 0;
643                     }
644
645                     rule1 = make_rule(wcf1, pri1, 0);
646                     rule2 = make_rule(wcf2, pri2,
647                                       value_pat << (CLS_N_FIELDS - 1));
648
649                     classifier_init(&cls);
650                     tcls_init(&tcls);
651
652                     tcls_rule1 = tcls_insert(&tcls, rule1);
653                     tcls_rule2 = tcls_insert(&tcls, rule2);
654                     assert(!classifier_insert(&cls, &rule1->cls_rule));
655                     displaced_rule = test_rule_from_cls_rule(
656                         classifier_insert(&cls, &rule2->cls_rule));
657                     if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
658                         assert(!displaced_rule);
659
660                         check_tables(&cls, 1, 1, 2);
661                         compare_classifiers(&cls, &tcls);
662
663                         classifier_remove(&cls, &rule1->cls_rule);
664                         tcls_remove(&tcls, tcls_rule1);
665                         check_tables(&cls, 1, 1, 1);
666                         compare_classifiers(&cls, &tcls);
667                     } else {
668                         assert(displaced_rule == rule1);
669                         check_tables(&cls, 1, 1, 1);
670                         compare_classifiers(&cls, &tcls);
671                     }
672                     free(rule1);
673
674                     classifier_remove(&cls, &rule2->cls_rule);
675                     tcls_remove(&tcls, tcls_rule2);
676                     compare_classifiers(&cls, &tcls);
677                     free(rule2);
678
679                     destroy_classifier(&cls);
680                     tcls_destroy(&tcls);
681                 }
682             }
683         }
684     }
685 }
686
687 /* Tests classification with two rules at a time that fall into the same
688  * table but different buckets. */
689 static void
690 test_two_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
691 {
692     int table, rel_pri, wcf_pat;
693
694     /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
695     for (table = 1; table < CLS_N_FIELDS; table++) {
696         for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
697             for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
698                 struct test_rule *rule1, *tcls_rule1;
699                 struct test_rule *rule2, *tcls_rule2;
700                 struct classifier cls;
701                 struct tcls tcls;
702                 unsigned int pri1, pri2;
703                 int wcf1, wcf2;
704                 int value_mask, value_pat1, value_pat2;
705                 int i;
706
707                 /* We can use identical priorities in this test because the
708                  * classifier always chooses the rule added later for
709                  * equal-priority rules that fall into the same table.  */
710                 pri1 = table * 257 + 50;
711                 pri2 = pri1 + rel_pri;
712
713                 if (wcf_pat & 4) {
714                     wcf1 = wcf2 = random_wcf_in_table(table, pri1);
715                 } else {
716                     wcf1 = (wcf_pat & 1
717                             ? random_wcf_in_table(table, pri1)
718                             : 1u << table);
719                     wcf2 = (wcf_pat & 2
720                             ? random_wcf_in_table(table, pri2)
721                             : 1u << table);
722                 }
723
724                 /* Generate value patterns that will put the two rules into
725                  * different buckets. */
726                 value_mask = ((1u << table) - 1);
727                 value_pat1 = hash_int(pri1, 1) & value_mask;
728                 i = 0;
729                 do {
730                     value_pat2 = (hash_int(pri2, i++) & value_mask);
731                 } while (value_pat1 == value_pat2);
732                 rule1 = make_rule(wcf1, pri1, value_pat1);
733                 rule2 = make_rule(wcf2, pri2, value_pat2);
734
735                 classifier_init(&cls);
736                 tcls_init(&tcls);
737
738                 tcls_rule1 = tcls_insert(&tcls, rule1);
739                 tcls_rule2 = tcls_insert(&tcls, rule2);
740                 assert(!classifier_insert(&cls, &rule1->cls_rule));
741                 assert(!classifier_insert(&cls, &rule2->cls_rule));
742                 check_tables(&cls, 1, 2, 2);
743                 compare_classifiers(&cls, &tcls);
744
745                 classifier_remove(&cls, &rule1->cls_rule);
746                 tcls_remove(&tcls, tcls_rule1);
747                 check_tables(&cls, 1, 1, 1);
748                 compare_classifiers(&cls, &tcls);
749                 free(rule1);
750
751                 classifier_remove(&cls, &rule2->cls_rule);
752                 tcls_remove(&tcls, tcls_rule2);
753                 compare_classifiers(&cls, &tcls);
754                 free(rule2);
755
756                 classifier_destroy(&cls);
757                 tcls_destroy(&tcls);
758             }
759         }
760     }
761 }
762
763 /* Tests classification with two rules at a time that fall into different
764  * tables. */
765 static void
766 test_two_rules_in_different_tables(int argc OVS_UNUSED, 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 }