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