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