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