Global replace of Nicira Networks.
[sliver-openvswitch.git] / tests / test-classifier.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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(0,                          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(0,                          tp_src,      TP_SRC)      \
54     CLS_FIELD(0,                          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_DSCP,                nw_tos,      NW_DSCP)
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_TP_SRC) {
202             eq = !((fixed->tp_src ^ wild->flow.tp_src) & wild->wc.tp_src_mask);
203         } else if (f_idx == CLS_F_IDX_TP_DST) {
204             eq = !((fixed->tp_dst ^ wild->flow.tp_dst) & wild->wc.tp_dst_mask);
205         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
206             eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci)
207                    & wild->wc.vlan_tci_mask);
208         } else if (f_idx == CLS_F_IDX_TUN_ID) {
209             eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask);
210         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
211             eq = !((fixed->nw_tos ^ wild->flow.nw_tos) & IP_DSCP_MASK);
212         } else {
213             NOT_REACHED();
214         }
215
216         if (!eq) {
217             return false;
218         }
219     }
220     return true;
221 }
222
223 static struct cls_rule *
224 tcls_lookup(const struct tcls *cls, const struct flow *flow)
225 {
226     size_t i;
227
228     for (i = 0; i < cls->n_rules; i++) {
229         struct test_rule *pos = cls->rules[i];
230         if (match(&pos->cls_rule, flow)) {
231             return &pos->cls_rule;
232         }
233     }
234     return NULL;
235 }
236
237 static void
238 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
239 {
240     size_t i;
241
242     for (i = 0; i < cls->n_rules; ) {
243         struct test_rule *pos = cls->rules[i];
244         if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
245             && match(target, &pos->cls_rule.flow)) {
246             tcls_remove(cls, pos);
247         } else {
248             i++;
249         }
250     }
251 }
252 \f
253 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
254                                     CONSTANT_HTONL(0xc0a04455) };
255 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
256                                     CONSTANT_HTONL(0xc0a04455) };
257 static ovs_be64 tun_id_values[] = {
258     0,
259     CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
260 static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
261 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
262 static ovs_be16 dl_type_values[]
263             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
264 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
265                                     CONSTANT_HTONS(80) };
266 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
267 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
268                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
269 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
270                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
271 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
272 static uint8_t nw_dscp_values[] = { 48, 0 };
273
274 static void *values[CLS_N_FIELDS][2];
275
276 static void
277 init_values(void)
278 {
279     values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
280     values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
281
282     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
283     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
284
285     values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
286     values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
287
288     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
289     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
290
291     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
292     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
293
294     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
295     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
296
297     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
298     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
299
300     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
301     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
302
303     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
304     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
305
306     values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
307     values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
308
309     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
310     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
311
312     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
313     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
314 }
315
316 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
317 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
318 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
319 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
320 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
321 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
322 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
323 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
324 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
325 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
326 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
327 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
328
329 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
330                        N_NW_DST_VALUES *        \
331                        N_TUN_ID_VALUES *        \
332                        N_IN_PORT_VALUES *       \
333                        N_VLAN_TCI_VALUES *       \
334                        N_DL_TYPE_VALUES *       \
335                        N_TP_SRC_VALUES *        \
336                        N_TP_DST_VALUES *        \
337                        N_DL_SRC_VALUES *        \
338                        N_DL_DST_VALUES *        \
339                        N_NW_PROTO_VALUES *      \
340                        N_NW_DSCP_VALUES)
341
342 static unsigned int
343 get_value(unsigned int *x, unsigned n_values)
344 {
345     unsigned int rem = *x % n_values;
346     *x /= n_values;
347     return rem;
348 }
349
350 static void
351 compare_classifiers(struct classifier *cls, struct tcls *tcls)
352 {
353     static const int confidence = 500;
354     unsigned int i;
355
356     assert(classifier_count(cls) == tcls->n_rules);
357     for (i = 0; i < confidence; i++) {
358         struct cls_rule *cr0, *cr1;
359         struct flow flow;
360         unsigned int x;
361
362         x = rand () % N_FLOW_VALUES;
363         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
364         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
365         flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
366         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
367         flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
368         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
369         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
370         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
371         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
372                ETH_ADDR_LEN);
373         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
374                ETH_ADDR_LEN);
375         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
376         flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
377
378         cr0 = classifier_lookup(cls, &flow);
379         cr1 = tcls_lookup(tcls, &flow);
380         assert((cr0 == NULL) == (cr1 == NULL));
381         if (cr0 != NULL) {
382             const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
383             const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
384
385             assert(cls_rule_equal(cr0, cr1));
386             assert(tr0->aux == tr1->aux);
387         }
388     }
389 }
390
391 static void
392 destroy_classifier(struct classifier *cls)
393 {
394     struct test_rule *rule, *next_rule;
395     struct cls_cursor cursor;
396
397     cls_cursor_init(&cursor, cls, NULL);
398     CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
399         classifier_remove(cls, &rule->cls_rule);
400         free(rule);
401     }
402     classifier_destroy(cls);
403 }
404
405 static void
406 check_tables(const struct classifier *cls,
407              int n_tables, int n_rules, int n_dups)
408 {
409     const struct cls_table *table;
410     struct test_rule *test_rule;
411     struct cls_cursor cursor;
412     int found_tables = 0;
413     int found_rules = 0;
414     int found_dups = 0;
415     int found_rules2 = 0;
416
417     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
418         const struct cls_rule *head;
419
420         assert(!hmap_is_empty(&table->rules));
421
422         found_tables++;
423         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
424             unsigned int prev_priority = UINT_MAX;
425             const struct cls_rule *rule;
426
427             found_rules++;
428             LIST_FOR_EACH (rule, list, &head->list) {
429                 assert(rule->priority < prev_priority);
430                 prev_priority = rule->priority;
431                 found_rules++;
432                 found_dups++;
433                 assert(classifier_find_rule_exactly(cls, rule) == rule);
434             }
435         }
436     }
437
438     assert(found_tables == hmap_count(&cls->tables));
439     assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
440     assert(n_rules == -1 || found_rules == n_rules);
441     assert(n_dups == -1 || found_dups == n_dups);
442
443     cls_cursor_init(&cursor, cls, NULL);
444     CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
445         found_rules2++;
446     }
447     assert(found_rules == found_rules2);
448 }
449
450 static struct test_rule *
451 make_rule(int wc_fields, unsigned int priority, int value_pat)
452 {
453     const struct cls_field *f;
454     struct test_rule *rule;
455
456     rule = xzalloc(sizeof *rule);
457     cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX);
458     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
459         int f_idx = f - cls_fields;
460         int value_idx = (value_pat & (1u << f_idx)) != 0;
461         memcpy((char *) &rule->cls_rule.flow + f->ofs,
462                values[f_idx][value_idx], f->len);
463
464         if (f->wildcards) {
465             rule->cls_rule.wc.wildcards &= ~f->wildcards;
466         } else if (f_idx == CLS_F_IDX_NW_SRC) {
467             rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX);
468         } else if (f_idx == CLS_F_IDX_NW_DST) {
469             rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX);
470         } else if (f_idx == CLS_F_IDX_TP_SRC) {
471             rule->cls_rule.wc.tp_src_mask = htons(UINT16_MAX);
472         } else if (f_idx == CLS_F_IDX_TP_DST) {
473             rule->cls_rule.wc.tp_dst_mask = htons(UINT16_MAX);
474         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
475             rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX);
476         } else if (f_idx == CLS_F_IDX_TUN_ID) {
477             rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX);
478         } else {
479             NOT_REACHED();
480         }
481     }
482     return rule;
483 }
484
485 static void
486 shuffle(unsigned int *p, size_t n)
487 {
488     for (; n > 1; n--, p++) {
489         unsigned int *q = &p[rand() % n];
490         unsigned int tmp = *p;
491         *p = *q;
492         *q = tmp;
493     }
494 }
495 \f
496 /* Tests an empty classifier. */
497 static void
498 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
499 {
500     struct classifier cls;
501     struct tcls tcls;
502
503     classifier_init(&cls);
504     tcls_init(&tcls);
505     assert(classifier_is_empty(&cls));
506     assert(tcls_is_empty(&tcls));
507     compare_classifiers(&cls, &tcls);
508     classifier_destroy(&cls);
509     tcls_destroy(&tcls);
510 }
511
512 /* Destroys a null classifier. */
513 static void
514 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
515 {
516     classifier_destroy(NULL);
517 }
518
519 /* Tests classification with one rule at a time. */
520 static void
521 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
522 {
523     unsigned int wc_fields;     /* Hilarious. */
524
525     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
526         struct classifier cls;
527         struct test_rule *rule, *tcls_rule;
528         struct tcls tcls;
529
530         rule = make_rule(wc_fields,
531                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
532
533         classifier_init(&cls);
534         tcls_init(&tcls);
535
536         tcls_rule = tcls_insert(&tcls, rule);
537         classifier_insert(&cls, &rule->cls_rule);
538         check_tables(&cls, 1, 1, 0);
539         compare_classifiers(&cls, &tcls);
540
541         classifier_remove(&cls, &rule->cls_rule);
542         tcls_remove(&tcls, tcls_rule);
543         assert(classifier_is_empty(&cls));
544         assert(tcls_is_empty(&tcls));
545         compare_classifiers(&cls, &tcls);
546
547         free(rule);
548         classifier_destroy(&cls);
549         tcls_destroy(&tcls);
550     }
551 }
552
553 /* Tests replacing one rule by another. */
554 static void
555 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
556 {
557     unsigned int wc_fields;
558
559     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
560         struct classifier cls;
561         struct test_rule *rule1;
562         struct test_rule *rule2;
563         struct tcls tcls;
564
565         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
566         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
567         rule2->aux += 5;
568         rule2->aux += 5;
569
570         classifier_init(&cls);
571         tcls_init(&tcls);
572         tcls_insert(&tcls, rule1);
573         classifier_insert(&cls, &rule1->cls_rule);
574         check_tables(&cls, 1, 1, 0);
575         compare_classifiers(&cls, &tcls);
576         tcls_destroy(&tcls);
577
578         tcls_init(&tcls);
579         tcls_insert(&tcls, rule2);
580         assert(test_rule_from_cls_rule(
581                    classifier_replace(&cls, &rule2->cls_rule)) == rule1);
582         free(rule1);
583         check_tables(&cls, 1, 1, 0);
584         compare_classifiers(&cls, &tcls);
585         tcls_destroy(&tcls);
586         destroy_classifier(&cls);
587     }
588 }
589
590 static int
591 factorial(int n_items)
592 {
593     int n, i;
594
595     n = 1;
596     for (i = 2; i <= n_items; i++) {
597         n *= i;
598     }
599     return n;
600 }
601
602 static void
603 swap(int *a, int *b)
604 {
605     int tmp = *a;
606     *a = *b;
607     *b = tmp;
608 }
609
610 static void
611 reverse(int *a, int n)
612 {
613     int i;
614
615     for (i = 0; i < n / 2; i++) {
616         int j = n - (i + 1);
617         swap(&a[i], &a[j]);
618     }
619 }
620
621 static bool
622 next_permutation(int *a, int n)
623 {
624     int k;
625
626     for (k = n - 2; k >= 0; k--) {
627         if (a[k] < a[k + 1]) {
628             int l;
629
630             for (l = n - 1; ; l--) {
631                 if (a[l] > a[k]) {
632                     swap(&a[k], &a[l]);
633                     reverse(a + (k + 1), n - (k + 1));
634                     return true;
635                 }
636             }
637         }
638     }
639     return false;
640 }
641
642 /* Tests classification with rules that have the same matching criteria. */
643 static void
644 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
645 {
646     enum { N_RULES = 3 };
647     int n_pris;
648
649     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
650         int ops[N_RULES * 2];
651         int pris[N_RULES];
652         int n_permutations;
653         int i;
654
655         pris[0] = 0;
656         for (i = 1; i < N_RULES; i++) {
657             pris[i] = pris[i - 1] + (n_pris > i);
658         }
659
660         for (i = 0; i < N_RULES * 2; i++) {
661             ops[i] = i / 2;
662         }
663
664         n_permutations = 0;
665         do {
666             struct test_rule *rules[N_RULES];
667             struct test_rule *tcls_rules[N_RULES];
668             int pri_rules[N_RULES];
669             struct classifier cls;
670             struct tcls tcls;
671
672             n_permutations++;
673
674             for (i = 0; i < N_RULES; i++) {
675                 rules[i] = make_rule(456, pris[i], 0);
676                 tcls_rules[i] = NULL;
677                 pri_rules[i] = -1;
678             }
679
680             classifier_init(&cls);
681             tcls_init(&tcls);
682
683             for (i = 0; i < ARRAY_SIZE(ops); i++) {
684                 int j = ops[i];
685                 int m, n;
686
687                 if (!tcls_rules[j]) {
688                     struct test_rule *displaced_rule;
689
690                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
691                     displaced_rule = test_rule_from_cls_rule(
692                         classifier_replace(&cls, &rules[j]->cls_rule));
693                     if (pri_rules[pris[j]] >= 0) {
694                         int k = pri_rules[pris[j]];
695                         assert(displaced_rule != NULL);
696                         assert(displaced_rule != rules[j]);
697                         assert(pris[j] == displaced_rule->cls_rule.priority);
698                         tcls_rules[k] = NULL;
699                     } else {
700                         assert(displaced_rule == NULL);
701                     }
702                     pri_rules[pris[j]] = j;
703                 } else {
704                     classifier_remove(&cls, &rules[j]->cls_rule);
705                     tcls_remove(&tcls, tcls_rules[j]);
706                     tcls_rules[j] = NULL;
707                     pri_rules[pris[j]] = -1;
708                 }
709
710                 n = 0;
711                 for (m = 0; m < N_RULES; m++) {
712                     n += tcls_rules[m] != NULL;
713                 }
714                 check_tables(&cls, n > 0, n, n - 1);
715
716                 compare_classifiers(&cls, &tcls);
717             }
718
719             classifier_destroy(&cls);
720             tcls_destroy(&tcls);
721
722             for (i = 0; i < N_RULES; i++) {
723                 free(rules[i]);
724             }
725         } while (next_permutation(ops, ARRAY_SIZE(ops)));
726         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
727     }
728 }
729
730 static int
731 count_ones(unsigned long int x)
732 {
733     int n = 0;
734
735     while (x) {
736         x &= x - 1;
737         n++;
738     }
739
740     return n;
741 }
742
743 static bool
744 array_contains(int *array, int n, int value)
745 {
746     int i;
747
748     for (i = 0; i < n; i++) {
749         if (array[i] == value) {
750             return true;
751         }
752     }
753
754     return false;
755 }
756
757 /* Tests classification with two rules at a time that fall into the same
758  * table but different lists. */
759 static void
760 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
761 {
762     int iteration;
763
764     for (iteration = 0; iteration < 50; iteration++) {
765         enum { N_RULES = 20 };
766         struct test_rule *rules[N_RULES];
767         struct test_rule *tcls_rules[N_RULES];
768         struct classifier cls;
769         struct tcls tcls;
770         int value_pats[N_RULES];
771         int value_mask;
772         int wcf;
773         int i;
774
775         do {
776             wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
777             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
778         } while ((1 << count_ones(value_mask)) < N_RULES);
779
780         classifier_init(&cls);
781         tcls_init(&tcls);
782
783         for (i = 0; i < N_RULES; i++) {
784             unsigned int priority = rand();
785
786             do {
787                 value_pats[i] = rand() & value_mask;
788             } while (array_contains(value_pats, i, value_pats[i]));
789
790             rules[i] = make_rule(wcf, priority, value_pats[i]);
791             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
792             classifier_insert(&cls, &rules[i]->cls_rule);
793
794             check_tables(&cls, 1, i + 1, 0);
795             compare_classifiers(&cls, &tcls);
796         }
797
798         for (i = 0; i < N_RULES; i++) {
799             tcls_remove(&tcls, tcls_rules[i]);
800             classifier_remove(&cls, &rules[i]->cls_rule);
801             free(rules[i]);
802
803             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
804             compare_classifiers(&cls, &tcls);
805         }
806
807         classifier_destroy(&cls);
808         tcls_destroy(&tcls);
809     }
810 }
811
812 /* Tests classification with many rules at a time that fall into random lists
813  * in 'n' tables. */
814 static void
815 test_many_rules_in_n_tables(int n_tables)
816 {
817     enum { MAX_RULES = 50 };
818     int wcfs[10];
819     int iteration;
820     int i;
821
822     assert(n_tables < 10);
823     for (i = 0; i < n_tables; i++) {
824         do {
825             wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
826         } while (array_contains(wcfs, i, wcfs[i]));
827     }
828
829     for (iteration = 0; iteration < 30; iteration++) {
830         unsigned int priorities[MAX_RULES];
831         struct classifier cls;
832         struct tcls tcls;
833
834         srand(iteration);
835         for (i = 0; i < MAX_RULES; i++) {
836             priorities[i] = i * 129;
837         }
838         shuffle(priorities, ARRAY_SIZE(priorities));
839
840         classifier_init(&cls);
841         tcls_init(&tcls);
842
843         for (i = 0; i < MAX_RULES; i++) {
844             struct test_rule *rule;
845             unsigned int priority = priorities[i];
846             int wcf = wcfs[rand() % n_tables];
847             int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
848             rule = make_rule(wcf, priority, value_pat);
849             tcls_insert(&tcls, rule);
850             classifier_insert(&cls, &rule->cls_rule);
851             check_tables(&cls, -1, i + 1, -1);
852             compare_classifiers(&cls, &tcls);
853         }
854
855         while (!classifier_is_empty(&cls)) {
856             struct test_rule *rule, *next_rule;
857             struct test_rule *target;
858             struct cls_cursor cursor;
859
860             target = xmemdup(tcls.rules[rand() % tcls.n_rules],
861                              sizeof(struct test_rule));
862
863             cls_cursor_init(&cursor, &cls, &target->cls_rule);
864             CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
865                 classifier_remove(&cls, &rule->cls_rule);
866                 free(rule);
867             }
868             tcls_delete_matches(&tcls, &target->cls_rule);
869             compare_classifiers(&cls, &tcls);
870             check_tables(&cls, -1, -1, -1);
871             free(target);
872         }
873
874         destroy_classifier(&cls);
875         tcls_destroy(&tcls);
876     }
877 }
878
879 static void
880 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
881 {
882     test_many_rules_in_n_tables(2);
883 }
884
885 static void
886 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
887 {
888     test_many_rules_in_n_tables(5);
889 }
890 \f
891 static const struct command commands[] = {
892     {"empty", 0, 0, test_empty},
893     {"destroy-null", 0, 0, test_destroy_null},
894     {"single-rule", 0, 0, test_single_rule},
895     {"rule-replacement", 0, 0, test_rule_replacement},
896     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
897     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
898     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
899     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
900     {NULL, 0, 0, NULL},
901 };
902
903 int
904 main(int argc, char *argv[])
905 {
906     set_program_name(argv[0]);
907     init_values();
908     run_command(argc - 1, argv + 1, commands);
909     return 0;
910 }