flow: Better abstract flow_wildcards and use it more widely.
[sliver-openvswitch.git] / tests / test-classifier.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* "White box" tests for classifier.
18  *
19  * With very few exceptions, these tests obtain complete coverage of every
20  * basic block and every branch in the classifier implementation, e.g. a clean
21  * report from "gcov -b".  (Covering the exceptions would require finding
22  * collisions in the hash function used for flow data, etc.)
23  *
24  * This test should receive a clean report from "valgrind --leak-check=full":
25  * it frees every heap block that it allocates.
26  */
27
28 #include <config.h>
29 #include "classifier.h"
30 #include <errno.h>
31 #include <limits.h>
32 #include "byte-order.h"
33 #include "command-line.h"
34 #include "flow.h"
35 #include "packets.h"
36
37 #undef NDEBUG
38 #include <assert.h>
39
40 /* Fields in a rule. */
41 #define CLS_FIELDS                                          \
42     /*                           struct flow  all-caps */   \
43     /*        wildcard bit(s)    member name  name     */   \
44     /*        -----------------  -----------  -------- */   \
45     CLS_FIELD(NXFW_TUN_ID,       tun_id,      TUN_ID)       \
46     CLS_FIELD(OFPFW_NW_SRC_MASK, nw_src,      NW_SRC)       \
47     CLS_FIELD(OFPFW_NW_DST_MASK, nw_dst,      NW_DST)       \
48     CLS_FIELD(OFPFW_IN_PORT,     in_port,     IN_PORT)      \
49     CLS_FIELD(OFPFW_DL_VLAN,     dl_vlan,     DL_VLAN)      \
50     CLS_FIELD(OFPFW_DL_TYPE,     dl_type,     DL_TYPE)      \
51     CLS_FIELD(OFPFW_TP_SRC,      tp_src,      TP_SRC)       \
52     CLS_FIELD(OFPFW_TP_DST,      tp_dst,      TP_DST)       \
53     CLS_FIELD(OFPFW_DL_SRC,      dl_src,      DL_SRC)       \
54     CLS_FIELD(OFPFW_DL_DST,      dl_dst,      DL_DST)       \
55     CLS_FIELD(OFPFW_NW_PROTO,    nw_proto,    NW_PROTO)     \
56     CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP)  \
57     CLS_FIELD(OFPFW_NW_TOS,      nw_tos,      NW_TOS)
58
59 /* Field indexes.
60  *
61  * (These are also indexed into struct classifier's 'tables' array.) */
62 enum {
63 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME,
64     CLS_FIELDS
65 #undef CLS_FIELD
66     CLS_N_FIELDS
67 };
68
69 /* Field information. */
70 struct cls_field {
71     int ofs;                    /* Offset in struct flow. */
72     int len;                    /* Length in bytes. */
73     uint32_t wildcards;         /* OFPFW_* bit or bits for this field. */
74     const char *name;           /* Name (for debugging). */
75 };
76
77 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
78 #define CLS_FIELD(WILDCARDS, MEMBER, NAME)      \
79     { offsetof(struct flow, MEMBER),            \
80       sizeof ((struct flow *)0)->MEMBER,        \
81       WILDCARDS,                                \
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 int
127 tcls_count_exact(const struct tcls *tcls)
128 {
129     int n_exact;
130     size_t i;
131
132     n_exact = 0;
133     for (i = 0; i < tcls->n_rules; i++) {
134         n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
135     }
136     return n_exact;
137 }
138
139 static bool
140 tcls_is_empty(const struct tcls *tcls)
141 {
142     return tcls->n_rules == 0;
143 }
144
145 static struct test_rule *
146 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
147 {
148     size_t i;
149
150     assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
151     for (i = 0; i < tcls->n_rules; i++) {
152         const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
153         if (pos->priority == rule->cls_rule.priority
154             && pos->wc.wildcards == rule->cls_rule.wc.wildcards
155             && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
156             /* Exact match.
157              * XXX flow_equal should ignore wildcarded fields */
158             free(tcls->rules[i]);
159             tcls->rules[i] = xmemdup(rule, sizeof *rule);
160             return tcls->rules[i];
161         } else if (pos->priority < rule->cls_rule.priority) {
162             break;
163         }
164     }
165
166     if (tcls->n_rules >= tcls->allocated_rules) {
167         tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
168                                  sizeof *tcls->rules);
169     }
170     if (i != tcls->n_rules) {
171         memmove(&tcls->rules[i + 1], &tcls->rules[i],
172                 sizeof *tcls->rules * (tcls->n_rules - i));
173     }
174     tcls->rules[i] = xmemdup(rule, sizeof *rule);
175     tcls->n_rules++;
176     return tcls->rules[i];
177 }
178
179 static void
180 tcls_remove(struct tcls *cls, const struct test_rule *rule)
181 {
182     size_t i;
183
184     for (i = 0; i < cls->n_rules; i++) {
185         struct test_rule *pos = cls->rules[i];
186         if (pos == rule) {
187             free(pos);
188             memmove(&cls->rules[i], &cls->rules[i + 1],
189                     sizeof *cls->rules * (cls->n_rules - i - 1));
190             cls->n_rules--;
191             return;
192         }
193     }
194     NOT_REACHED();
195 }
196
197 static uint32_t
198 read_uint32(const void *p)
199 {
200     uint32_t x;
201     memcpy(&x, p, sizeof x);
202     return x;
203 }
204
205 static bool
206 match(const struct cls_rule *wild, const struct flow *fixed)
207 {
208     int f_idx;
209
210     for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
211         const struct cls_field *f = &cls_fields[f_idx];
212         void *wild_field = (char *) &wild->flow + f->ofs;
213         void *fixed_field = (char *) fixed + f->ofs;
214
215         if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
216             !memcmp(wild_field, fixed_field, f->len)) {
217             /* Definite match. */
218             continue;
219         }
220
221         if (wild->wc.wildcards & f->wildcards) {
222             uint32_t test = read_uint32(wild_field);
223             uint32_t ip = read_uint32(fixed_field);
224             int shift = (f_idx == CLS_F_IDX_NW_SRC
225                          ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
226             uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
227             if (!((test ^ ip) & mask)) {
228                 continue;
229             }
230         }
231
232         return false;
233     }
234     return true;
235 }
236
237 static struct cls_rule *
238 tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
239 {
240     size_t i;
241
242     for (i = 0; i < cls->n_rules; i++) {
243         struct test_rule *pos = cls->rules[i];
244         uint32_t wildcards = pos->cls_rule.wc.wildcards;
245         if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
246             && match(&pos->cls_rule, flow)) {
247             return &pos->cls_rule;
248         }
249     }
250     return NULL;
251 }
252
253 static void
254 tcls_delete_matches(struct tcls *cls,
255                     const struct cls_rule *target,
256                     int include)
257 {
258     size_t i;
259
260     for (i = 0; i < cls->n_rules; ) {
261         struct test_rule *pos = cls->rules[i];
262         uint32_t wildcards = pos->cls_rule.wc.wildcards;
263         if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
264             && !flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
265             && match(target, &pos->cls_rule.flow)) {
266             tcls_remove(cls, pos);
267         } else {
268             i++;
269         }
270     }
271 }
272 \f
273 static uint32_t nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
274                                     CONSTANT_HTONL(0xc0a04455) };
275 static uint32_t nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
276                                     CONSTANT_HTONL(0xc0a04455) };
277 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
278 static uint16_t in_port_values[] = { CONSTANT_HTONS(1),
279                                      CONSTANT_HTONS(OFPP_LOCAL) };
280 static uint16_t dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
281 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
282 static uint16_t dl_type_values[]
283             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
284 static uint16_t tp_src_values[] = { CONSTANT_HTONS(49362),
285                                     CONSTANT_HTONS(80) };
286 static uint16_t tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
287 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
288                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
289 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
290                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
291 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
292 static uint8_t nw_tos_values[] = { 49, 0 };
293
294 static void *values[CLS_N_FIELDS][2];
295
296 static void
297 init_values(void)
298 {
299     values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
300     values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
301
302     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
303     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
304
305     values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
306     values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
307
308     values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
309     values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
310
311     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
312     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
313
314     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
315     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
316
317     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
318     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
319
320     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
321     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
322
323     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
324     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
325
326     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
327     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
328
329     values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
330     values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
331
332     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
333     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
334
335     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
336     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
337 }
338
339 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
340 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
341 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
342 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
343 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
344 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
345 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
346 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
347 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
348 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
349 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
350 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
351 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
352
353 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
354                        N_NW_DST_VALUES *        \
355                        N_TUN_ID_VALUES *        \
356                        N_IN_PORT_VALUES *       \
357                        N_DL_VLAN_VALUES *       \
358                        N_DL_VLAN_PCP_VALUES *   \
359                        N_DL_TYPE_VALUES *       \
360                        N_TP_SRC_VALUES *        \
361                        N_TP_DST_VALUES *        \
362                        N_DL_SRC_VALUES *        \
363                        N_DL_DST_VALUES *        \
364                        N_NW_PROTO_VALUES *      \
365                        N_NW_TOS_VALUES)
366
367 static unsigned int
368 get_value(unsigned int *x, unsigned n_values)
369 {
370     unsigned int rem = *x % n_values;
371     *x /= n_values;
372     return rem;
373 }
374
375 static void
376 compare_classifiers(struct classifier *cls, struct tcls *tcls)
377 {
378     static const int confidence = 500;
379     unsigned int i;
380
381     assert(classifier_count(cls) == tcls->n_rules);
382     assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
383     for (i = 0; i < confidence; i++) {
384         struct cls_rule *cr0, *cr1;
385         struct flow flow;
386         unsigned int x;
387         int include;
388
389         x = rand () % N_FLOW_VALUES;
390         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
391         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
392         flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
393         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
394         flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
395         flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
396                 N_DL_VLAN_PCP_VALUES)];
397         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
398         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
399         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
400         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
401                ETH_ADDR_LEN);
402         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
403                ETH_ADDR_LEN);
404         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
405         flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
406
407         for (include = 1; include <= 3; include++) {
408             cr0 = classifier_lookup(cls, &flow, include);
409             cr1 = tcls_lookup(tcls, &flow, include);
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(flow_equal(&cr0->flow, &cr1->flow));
416                 assert(cr0->wc.wildcards == cr1->wc.wildcards);
417                 assert(cr0->priority == cr1->priority);
418                 /* Skip nw_src_mask and nw_dst_mask, because they are derived
419                  * members whose values are used only for optimization. */
420                 assert(tr0->aux == tr1->aux);
421             }
422         }
423     }
424 }
425
426 static void
427 free_rule(struct cls_rule *cls_rule, void *cls)
428 {
429     classifier_remove(cls, cls_rule);
430     free(test_rule_from_cls_rule(cls_rule));
431 }
432
433 static void
434 destroy_classifier(struct classifier *cls)
435 {
436     classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
437     classifier_destroy(cls);
438 }
439
440 static void
441 check_tables(const struct classifier *cls,
442              int n_tables, int n_rules, int n_dups)
443 {
444     const struct cls_table *table;
445     int found_tables = 0;
446     int found_rules = 0;
447     int found_dups = 0;
448
449     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
450         const struct cls_rule *head;
451
452         assert(!hmap_is_empty(&table->rules));
453
454         found_tables++;
455         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
456             unsigned int prev_priority = UINT_MAX;
457             const struct cls_rule *rule;
458
459             found_rules++;
460             LIST_FOR_EACH (rule, list, &head->list) {
461                 assert(rule->priority < prev_priority);
462                 prev_priority = rule->priority;
463                 found_rules++;
464                 found_dups++;
465                 assert(classifier_find_rule_exactly(cls, rule) == rule);
466             }
467         }
468     }
469
470     assert(found_tables == hmap_count(&cls->tables));
471     assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
472     assert(n_rules == -1 || found_rules == n_rules);
473     assert(n_dups == -1 || found_dups == n_dups);
474 }
475
476 static struct test_rule *
477 make_rule(int wc_fields, unsigned int priority, int value_pat)
478 {
479     const struct cls_field *f;
480     struct flow_wildcards wc;
481     struct test_rule *rule;
482     uint32_t wildcards;
483     struct flow flow;
484
485     wildcards = 0;
486     memset(&flow, 0, sizeof flow);
487     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
488         int f_idx = f - cls_fields;
489         if (wc_fields & (1u << f_idx)) {
490             wildcards |= f->wildcards;
491         } else {
492             int value_idx = (value_pat & (1u << f_idx)) != 0;
493             memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
494         }
495     }
496
497     rule = xzalloc(sizeof *rule);
498     flow_wildcards_init(&wc, wildcards);
499     cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
500                   &rule->cls_rule);
501     return rule;
502 }
503
504 static void
505 shuffle(unsigned int *p, size_t n)
506 {
507     for (; n > 1; n--, p++) {
508         unsigned int *q = &p[rand() % n];
509         unsigned int tmp = *p;
510         *p = *q;
511         *q = tmp;
512     }
513 }
514 \f
515 /* Tests an empty classifier. */
516 static void
517 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
518 {
519     struct classifier cls;
520     struct tcls tcls;
521
522     classifier_init(&cls);
523     tcls_init(&tcls);
524     assert(classifier_is_empty(&cls));
525     assert(tcls_is_empty(&tcls));
526     compare_classifiers(&cls, &tcls);
527     classifier_destroy(&cls);
528     tcls_destroy(&tcls);
529 }
530
531 /* Destroys a null classifier. */
532 static void
533 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
534 {
535     classifier_destroy(NULL);
536 }
537
538 /* Tests classification with one rule at a time. */
539 static void
540 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
541 {
542     unsigned int wc_fields;     /* Hilarious. */
543
544     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
545         struct classifier cls;
546         struct test_rule *rule, *tcls_rule;
547         struct tcls tcls;
548
549         rule = make_rule(wc_fields,
550                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
551
552         classifier_init(&cls);
553         tcls_init(&tcls);
554
555         tcls_rule = tcls_insert(&tcls, rule);
556         assert(!classifier_insert(&cls, &rule->cls_rule));
557         check_tables(&cls, 1, 1, 0);
558         compare_classifiers(&cls, &tcls);
559
560         classifier_remove(&cls, &rule->cls_rule);
561         tcls_remove(&tcls, tcls_rule);
562         assert(classifier_is_empty(&cls));
563         assert(tcls_is_empty(&tcls));
564         compare_classifiers(&cls, &tcls);
565
566         free(rule);
567         classifier_destroy(&cls);
568         tcls_destroy(&tcls);
569     }
570 }
571
572 /* Tests replacing one rule by another. */
573 static void
574 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
575 {
576     unsigned int wc_fields;
577
578     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
579         struct classifier cls;
580         struct test_rule *rule1;
581         struct test_rule *rule2;
582         struct tcls tcls;
583
584         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
585         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
586         rule2->aux += 5;
587         rule2->aux += 5;
588
589         classifier_init(&cls);
590         tcls_init(&tcls);
591         tcls_insert(&tcls, rule1);
592         assert(!classifier_insert(&cls, &rule1->cls_rule));
593         check_tables(&cls, 1, 1, 0);
594         compare_classifiers(&cls, &tcls);
595         tcls_destroy(&tcls);
596
597         tcls_init(&tcls);
598         tcls_insert(&tcls, rule2);
599         assert(test_rule_from_cls_rule(
600                    classifier_insert(&cls, &rule2->cls_rule)) == rule1);
601         free(rule1);
602         check_tables(&cls, 1, 1, 0);
603         compare_classifiers(&cls, &tcls);
604         tcls_destroy(&tcls);
605         destroy_classifier(&cls);
606     }
607 }
608
609 static int
610 factorial(int n_items)
611 {
612     int n, i;
613
614     n = 1;
615     for (i = 2; i <= n_items; i++) {
616         n *= i;
617     }
618     return n;
619 }
620
621 static void
622 swap(int *a, int *b)
623 {
624     int tmp = *a;
625     *a = *b;
626     *b = tmp;
627 }
628
629 static void
630 reverse(int *a, int n)
631 {
632     int i;
633
634     for (i = 0; i < n / 2; i++) {
635         int j = n - (i + 1);
636         swap(&a[i], &a[j]);
637     }
638 }
639
640 static bool
641 next_permutation(int *a, int n)
642 {
643     int k;
644
645     for (k = n - 2; k >= 0; k--) {
646         if (a[k] < a[k + 1]) {
647             int l;
648
649             for (l = n - 1; ; l--) {
650                 if (a[l] > a[k]) {
651                     swap(&a[k], &a[l]);
652                     reverse(a + (k + 1), n - (k + 1));
653                     return true;
654                 }
655             }
656         }
657     }
658     return false;
659 }
660
661 /* Tests classification with rules that have the same matching criteria. */
662 static void
663 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
664 {
665     enum { N_RULES = 3 };
666     int n_pris;
667
668     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
669         int ops[N_RULES * 2];
670         int pris[N_RULES];
671         int n_permutations;
672         int i;
673
674         pris[0] = 0;
675         for (i = 1; i < N_RULES; i++) {
676             pris[i] = pris[i - 1] + (n_pris > i);
677         }
678
679         for (i = 0; i < N_RULES * 2; i++) {
680             ops[i] = i / 2;
681         }
682
683         n_permutations = 0;
684         do {
685             struct test_rule *rules[N_RULES];
686             struct test_rule *tcls_rules[N_RULES];
687             int pri_rules[N_RULES];
688             struct classifier cls;
689             struct tcls tcls;
690
691             n_permutations++;
692
693             for (i = 0; i < N_RULES; i++) {
694                 rules[i] = make_rule(456, pris[i], 0);
695                 tcls_rules[i] = NULL;
696                 pri_rules[i] = -1;
697             }
698
699             classifier_init(&cls);
700             tcls_init(&tcls);
701
702             for (i = 0; i < ARRAY_SIZE(ops); i++) {
703                 int j = ops[i];
704                 int m, n;
705
706                 if (!tcls_rules[j]) {
707                     struct test_rule *displaced_rule;
708
709                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
710                     displaced_rule = test_rule_from_cls_rule(
711                         classifier_insert(&cls, &rules[j]->cls_rule));
712                     if (pri_rules[pris[j]] >= 0) {
713                         int k = pri_rules[pris[j]];
714                         assert(displaced_rule != NULL);
715                         assert(displaced_rule != rules[j]);
716                         assert(pris[j] == displaced_rule->cls_rule.priority);
717                         tcls_rules[k] = NULL;
718                     } else {
719                         assert(displaced_rule == NULL);
720                     }
721                     pri_rules[pris[j]] = j;
722                 } else {
723                     classifier_remove(&cls, &rules[j]->cls_rule);
724                     tcls_remove(&tcls, tcls_rules[j]);
725                     tcls_rules[j] = NULL;
726                     pri_rules[pris[j]] = -1;
727                 }
728
729                 n = 0;
730                 for (m = 0; m < N_RULES; m++) {
731                     n += tcls_rules[m] != NULL;
732                 }
733                 check_tables(&cls, n > 0, n, n - 1);
734
735                 compare_classifiers(&cls, &tcls);
736             }
737
738             classifier_destroy(&cls);
739             tcls_destroy(&tcls);
740
741             for (i = 0; i < N_RULES; i++) {
742                 free(rules[i]);
743             }
744         } while (next_permutation(ops, ARRAY_SIZE(ops)));
745         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
746     }
747 }
748
749 static int
750 count_ones(unsigned long int x)
751 {
752     int n = 0;
753
754     while (x) {
755         x &= x - 1;
756         n++;
757     }
758
759     return n;
760 }
761
762 static bool
763 array_contains(int *array, int n, int value)
764 {
765     int i;
766
767     for (i = 0; i < n; i++) {
768         if (array[i] == value) {
769             return true;
770         }
771     }
772
773     return false;
774 }
775
776 /* Tests classification with two rules at a time that fall into the same
777  * table but different lists. */
778 static void
779 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
780 {
781     int iteration;
782
783     for (iteration = 0; iteration < 50; iteration++) {
784         enum { N_RULES = 20 };
785         struct test_rule *rules[N_RULES];
786         struct test_rule *tcls_rules[N_RULES];
787         struct classifier cls;
788         struct tcls tcls;
789         int value_pats[N_RULES];
790         int value_mask;
791         int wcf;
792         int i;
793
794         do {
795             wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
796             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
797         } while ((1 << count_ones(value_mask)) < N_RULES);
798
799         classifier_init(&cls);
800         tcls_init(&tcls);
801
802         for (i = 0; i < N_RULES; i++) {
803             unsigned int priority = rand();
804
805             do {
806                 value_pats[i] = rand() & value_mask;
807             } while (array_contains(value_pats, i, value_pats[i]));
808
809             rules[i] = make_rule(wcf, priority, value_pats[i]);
810             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
811             assert(!classifier_insert(&cls, &rules[i]->cls_rule));
812
813             check_tables(&cls, 1, i + 1, 0);
814             compare_classifiers(&cls, &tcls);
815         }
816
817         for (i = 0; i < N_RULES; i++) {
818             tcls_remove(&tcls, tcls_rules[i]);
819             classifier_remove(&cls, &rules[i]->cls_rule);
820             free(rules[i]);
821
822             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
823             compare_classifiers(&cls, &tcls);
824         }
825
826         classifier_destroy(&cls);
827         tcls_destroy(&tcls);
828     }
829 }
830
831 /* Tests classification with many rules at a time that fall into random lists
832  * in 'n' tables. */
833 static void
834 test_many_rules_in_n_tables(int n_tables)
835 {
836     enum { MAX_RULES = 50 };
837     int wcfs[10];
838     int iteration;
839     int i;
840
841     assert(n_tables < 10);
842     for (i = 0; i < n_tables; i++) {
843         do {
844             wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
845         } while (array_contains(wcfs, i, wcfs[i]));
846     }
847
848     for (iteration = 0; iteration < 30; iteration++) {
849         unsigned int priorities[MAX_RULES];
850         struct classifier cls;
851         struct tcls tcls;
852
853         srand(iteration);
854         for (i = 0; i < MAX_RULES; i++) {
855             priorities[i] = i * 129;
856         }
857         shuffle(priorities, ARRAY_SIZE(priorities));
858
859         classifier_init(&cls);
860         tcls_init(&tcls);
861
862         for (i = 0; i < MAX_RULES; i++) {
863             struct test_rule *rule;
864             unsigned int priority = priorities[i];
865             int wcf = wcfs[rand() % n_tables];
866             int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
867             rule = make_rule(wcf, priority, value_pat);
868             tcls_insert(&tcls, rule);
869             assert(!classifier_insert(&cls, &rule->cls_rule));
870             check_tables(&cls, -1, i + 1, -1);
871             compare_classifiers(&cls, &tcls);
872         }
873
874         while (!classifier_is_empty(&cls)) {
875             struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
876                                              sizeof(struct test_rule));
877             int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
878             include |= (rule->cls_rule.wc.wildcards
879                         ? CLS_INC_WILD : CLS_INC_EXACT);
880             classifier_for_each_match(&cls, &rule->cls_rule, include,
881                                       free_rule, &cls);
882             tcls_delete_matches(&tcls, &rule->cls_rule, include);
883             compare_classifiers(&cls, &tcls);
884             check_tables(&cls, -1, -1, -1);
885             free(rule);
886         }
887
888         destroy_classifier(&cls);
889         tcls_destroy(&tcls);
890     }
891 }
892
893 static void
894 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
895 {
896     test_many_rules_in_n_tables(2);
897 }
898
899 static void
900 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
901 {
902     test_many_rules_in_n_tables(5);
903 }
904 \f
905 static const struct command commands[] = {
906     {"empty", 0, 0, test_empty},
907     {"destroy-null", 0, 0, test_destroy_null},
908     {"single-rule", 0, 0, test_single_rule},
909     {"rule-replacement", 0, 0, test_rule_replacement},
910     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
911     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
912     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
913     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
914     {NULL, 0, 0, NULL},
915 };
916
917 int
918 main(int argc, char *argv[])
919 {
920     set_program_name(argv[0]);
921     init_values();
922     run_command(argc - 1, argv + 1, commands);
923     return 0;
924 }