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