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