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