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