test-classifier: Use get_unaligned_u32() from unaligned.h.
[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, int include)
233 {
234     size_t i;
235
236     for (i = 0; i < cls->n_rules; i++) {
237         struct test_rule *pos = cls->rules[i];
238         uint32_t wildcards = pos->cls_rule.wc.wildcards;
239         if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
240             && match(&pos->cls_rule, flow)) {
241             return &pos->cls_rule;
242         }
243     }
244     return NULL;
245 }
246
247 static void
248 tcls_delete_matches(struct tcls *cls,
249                     const struct cls_rule *target,
250                     int include)
251 {
252     size_t i;
253
254     for (i = 0; i < cls->n_rules; ) {
255         struct test_rule *pos = cls->rules[i];
256         uint32_t wildcards = pos->cls_rule.wc.wildcards;
257         if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
258             && !flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
259             && match(target, &pos->cls_rule.flow)) {
260             tcls_remove(cls, pos);
261         } else {
262             i++;
263         }
264     }
265 }
266 \f
267 static uint32_t nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
268                                     CONSTANT_HTONL(0xc0a04455) };
269 static uint32_t nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
270                                     CONSTANT_HTONL(0xc0a04455) };
271 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
272 static uint16_t in_port_values[] = { CONSTANT_HTONS(1),
273                                      CONSTANT_HTONS(OFPP_LOCAL) };
274 static uint16_t dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
275 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
276 static uint16_t dl_type_values[]
277             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
278 static uint16_t tp_src_values[] = { CONSTANT_HTONS(49362),
279                                     CONSTANT_HTONS(80) };
280 static uint16_t 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[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
286 static uint8_t nw_tos_values[] = { 49, 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_IN_PORT][0] = &in_port_values[0];
297     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
298
299     values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
300     values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
301
302     values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
303     values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_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_TOS][0] = &nw_tos_values[0];
324     values[CLS_F_IDX_NW_TOS][1] = &nw_tos_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_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
337 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
338 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_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_TOS_VALUES ARRAY_SIZE(nw_tos_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_DL_VLAN_VALUES *       \
352                        N_DL_VLAN_PCP_VALUES *   \
353                        N_DL_TYPE_VALUES *       \
354                        N_TP_SRC_VALUES *        \
355                        N_TP_DST_VALUES *        \
356                        N_DL_SRC_VALUES *        \
357                        N_DL_DST_VALUES *        \
358                        N_NW_PROTO_VALUES *      \
359                        N_NW_TOS_VALUES)
360
361 static unsigned int
362 get_value(unsigned int *x, unsigned n_values)
363 {
364     unsigned int rem = *x % n_values;
365     *x /= n_values;
366     return rem;
367 }
368
369 static void
370 compare_classifiers(struct classifier *cls, struct tcls *tcls)
371 {
372     static const int confidence = 500;
373     unsigned int i;
374
375     assert(classifier_count(cls) == tcls->n_rules);
376     assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
377     for (i = 0; i < confidence; i++) {
378         struct cls_rule *cr0, *cr1;
379         struct flow flow;
380         unsigned int x;
381         int include;
382
383         x = rand () % N_FLOW_VALUES;
384         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
385         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
386         flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
387         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
388         flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
389         flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
390                 N_DL_VLAN_PCP_VALUES)];
391         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
392         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
393         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
394         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
395                ETH_ADDR_LEN);
396         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
397                ETH_ADDR_LEN);
398         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
399         flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
400
401         for (include = 1; include <= 3; include++) {
402             cr0 = classifier_lookup(cls, &flow, include);
403             cr1 = tcls_lookup(tcls, &flow, include);
404             assert((cr0 == NULL) == (cr1 == NULL));
405             if (cr0 != NULL) {
406                 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
407                 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
408
409                 assert(flow_equal(&cr0->flow, &cr1->flow));
410                 assert(cr0->wc.wildcards == cr1->wc.wildcards);
411                 assert(cr0->priority == cr1->priority);
412                 /* Skip nw_src_mask and nw_dst_mask, because they are derived
413                  * members whose values are used only for optimization. */
414                 assert(tr0->aux == tr1->aux);
415             }
416         }
417     }
418 }
419
420 static void
421 free_rule(struct cls_rule *cls_rule, void *cls)
422 {
423     classifier_remove(cls, cls_rule);
424     free(test_rule_from_cls_rule(cls_rule));
425 }
426
427 static void
428 destroy_classifier(struct classifier *cls)
429 {
430     classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
431     classifier_destroy(cls);
432 }
433
434 static void
435 check_tables(const struct classifier *cls,
436              int n_tables, int n_rules, int n_dups)
437 {
438     const struct cls_table *table;
439     const struct test_rule *test_rule;
440     struct flow_wildcards exact_wc;
441     int found_tables = 0;
442     int found_rules = 0;
443     int found_dups = 0;
444     int n_exact1 = 0;
445     int n_exact2 = 0;
446
447     flow_wildcards_init_exact(&exact_wc);
448     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
449         bool is_exact = flow_wildcards_equal(&table->wc, &exact_wc);
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             if (is_exact) {
461                 n_exact1++;
462             }
463             LIST_FOR_EACH (rule, list, &head->list) {
464                 assert(rule->priority < prev_priority);
465                 prev_priority = rule->priority;
466                 found_rules++;
467                 found_dups++;
468                 if (is_exact) {
469                     n_exact1++;
470                 }
471                 assert(classifier_find_rule_exactly(cls, rule) == rule);
472             }
473         }
474     }
475
476     CLASSIFIER_FOR_EACH_EXACT_RULE (test_rule, cls_rule, cls) {
477         n_exact2++;
478     }
479
480     assert(found_tables == hmap_count(&cls->tables));
481     assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
482     assert(n_rules == -1 || found_rules == n_rules);
483     assert(n_dups == -1 || found_dups == n_dups);
484     assert(n_exact1 == n_exact2);
485 }
486
487 static struct test_rule *
488 make_rule(int wc_fields, unsigned int priority, int value_pat)
489 {
490     const struct cls_field *f;
491     struct flow_wildcards wc;
492     struct test_rule *rule;
493     uint32_t wildcards;
494     struct flow flow;
495
496     wildcards = 0;
497     memset(&flow, 0, sizeof flow);
498     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
499         int f_idx = f - cls_fields;
500         if (wc_fields & (1u << f_idx)) {
501             wildcards |= f->wildcards;
502         } else {
503             int value_idx = (value_pat & (1u << f_idx)) != 0;
504             memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
505         }
506     }
507
508     rule = xzalloc(sizeof *rule);
509     flow_wildcards_init(&wc, wildcards);
510     cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
511                   &rule->cls_rule);
512     return rule;
513 }
514
515 static void
516 shuffle(unsigned int *p, size_t n)
517 {
518     for (; n > 1; n--, p++) {
519         unsigned int *q = &p[rand() % n];
520         unsigned int tmp = *p;
521         *p = *q;
522         *q = tmp;
523     }
524 }
525 \f
526 /* Tests an empty classifier. */
527 static void
528 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
529 {
530     struct classifier cls;
531     struct tcls tcls;
532
533     classifier_init(&cls);
534     tcls_init(&tcls);
535     assert(classifier_is_empty(&cls));
536     assert(tcls_is_empty(&tcls));
537     compare_classifiers(&cls, &tcls);
538     classifier_destroy(&cls);
539     tcls_destroy(&tcls);
540 }
541
542 /* Destroys a null classifier. */
543 static void
544 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
545 {
546     classifier_destroy(NULL);
547 }
548
549 /* Tests classification with one rule at a time. */
550 static void
551 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
552 {
553     unsigned int wc_fields;     /* Hilarious. */
554
555     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
556         struct classifier cls;
557         struct test_rule *rule, *tcls_rule;
558         struct tcls tcls;
559
560         rule = make_rule(wc_fields,
561                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
562
563         classifier_init(&cls);
564         tcls_init(&tcls);
565
566         tcls_rule = tcls_insert(&tcls, rule);
567         assert(!classifier_insert(&cls, &rule->cls_rule));
568         check_tables(&cls, 1, 1, 0);
569         compare_classifiers(&cls, &tcls);
570
571         classifier_remove(&cls, &rule->cls_rule);
572         tcls_remove(&tcls, tcls_rule);
573         assert(classifier_is_empty(&cls));
574         assert(tcls_is_empty(&tcls));
575         compare_classifiers(&cls, &tcls);
576
577         free(rule);
578         classifier_destroy(&cls);
579         tcls_destroy(&tcls);
580     }
581 }
582
583 /* Tests replacing one rule by another. */
584 static void
585 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
586 {
587     unsigned int wc_fields;
588
589     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
590         struct classifier cls;
591         struct test_rule *rule1;
592         struct test_rule *rule2;
593         struct tcls tcls;
594
595         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
596         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
597         rule2->aux += 5;
598         rule2->aux += 5;
599
600         classifier_init(&cls);
601         tcls_init(&tcls);
602         tcls_insert(&tcls, rule1);
603         assert(!classifier_insert(&cls, &rule1->cls_rule));
604         check_tables(&cls, 1, 1, 0);
605         compare_classifiers(&cls, &tcls);
606         tcls_destroy(&tcls);
607
608         tcls_init(&tcls);
609         tcls_insert(&tcls, rule2);
610         assert(test_rule_from_cls_rule(
611                    classifier_insert(&cls, &rule2->cls_rule)) == rule1);
612         free(rule1);
613         check_tables(&cls, 1, 1, 0);
614         compare_classifiers(&cls, &tcls);
615         tcls_destroy(&tcls);
616         destroy_classifier(&cls);
617     }
618 }
619
620 static int
621 factorial(int n_items)
622 {
623     int n, i;
624
625     n = 1;
626     for (i = 2; i <= n_items; i++) {
627         n *= i;
628     }
629     return n;
630 }
631
632 static void
633 swap(int *a, int *b)
634 {
635     int tmp = *a;
636     *a = *b;
637     *b = tmp;
638 }
639
640 static void
641 reverse(int *a, int n)
642 {
643     int i;
644
645     for (i = 0; i < n / 2; i++) {
646         int j = n - (i + 1);
647         swap(&a[i], &a[j]);
648     }
649 }
650
651 static bool
652 next_permutation(int *a, int n)
653 {
654     int k;
655
656     for (k = n - 2; k >= 0; k--) {
657         if (a[k] < a[k + 1]) {
658             int l;
659
660             for (l = n - 1; ; l--) {
661                 if (a[l] > a[k]) {
662                     swap(&a[k], &a[l]);
663                     reverse(a + (k + 1), n - (k + 1));
664                     return true;
665                 }
666             }
667         }
668     }
669     return false;
670 }
671
672 /* Tests classification with rules that have the same matching criteria. */
673 static void
674 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
675 {
676     enum { N_RULES = 3 };
677     int n_pris;
678
679     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
680         int ops[N_RULES * 2];
681         int pris[N_RULES];
682         int n_permutations;
683         int i;
684
685         pris[0] = 0;
686         for (i = 1; i < N_RULES; i++) {
687             pris[i] = pris[i - 1] + (n_pris > i);
688         }
689
690         for (i = 0; i < N_RULES * 2; i++) {
691             ops[i] = i / 2;
692         }
693
694         n_permutations = 0;
695         do {
696             struct test_rule *rules[N_RULES];
697             struct test_rule *tcls_rules[N_RULES];
698             int pri_rules[N_RULES];
699             struct classifier cls;
700             struct tcls tcls;
701
702             n_permutations++;
703
704             for (i = 0; i < N_RULES; i++) {
705                 rules[i] = make_rule(456, pris[i], 0);
706                 tcls_rules[i] = NULL;
707                 pri_rules[i] = -1;
708             }
709
710             classifier_init(&cls);
711             tcls_init(&tcls);
712
713             for (i = 0; i < ARRAY_SIZE(ops); i++) {
714                 int j = ops[i];
715                 int m, n;
716
717                 if (!tcls_rules[j]) {
718                     struct test_rule *displaced_rule;
719
720                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
721                     displaced_rule = test_rule_from_cls_rule(
722                         classifier_insert(&cls, &rules[j]->cls_rule));
723                     if (pri_rules[pris[j]] >= 0) {
724                         int k = pri_rules[pris[j]];
725                         assert(displaced_rule != NULL);
726                         assert(displaced_rule != rules[j]);
727                         assert(pris[j] == displaced_rule->cls_rule.priority);
728                         tcls_rules[k] = NULL;
729                     } else {
730                         assert(displaced_rule == NULL);
731                     }
732                     pri_rules[pris[j]] = j;
733                 } else {
734                     classifier_remove(&cls, &rules[j]->cls_rule);
735                     tcls_remove(&tcls, tcls_rules[j]);
736                     tcls_rules[j] = NULL;
737                     pri_rules[pris[j]] = -1;
738                 }
739
740                 n = 0;
741                 for (m = 0; m < N_RULES; m++) {
742                     n += tcls_rules[m] != NULL;
743                 }
744                 check_tables(&cls, n > 0, n, n - 1);
745
746                 compare_classifiers(&cls, &tcls);
747             }
748
749             classifier_destroy(&cls);
750             tcls_destroy(&tcls);
751
752             for (i = 0; i < N_RULES; i++) {
753                 free(rules[i]);
754             }
755         } while (next_permutation(ops, ARRAY_SIZE(ops)));
756         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
757     }
758 }
759
760 static int
761 count_ones(unsigned long int x)
762 {
763     int n = 0;
764
765     while (x) {
766         x &= x - 1;
767         n++;
768     }
769
770     return n;
771 }
772
773 static bool
774 array_contains(int *array, int n, int value)
775 {
776     int i;
777
778     for (i = 0; i < n; i++) {
779         if (array[i] == value) {
780             return true;
781         }
782     }
783
784     return false;
785 }
786
787 /* Tests classification with two rules at a time that fall into the same
788  * table but different lists. */
789 static void
790 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
791 {
792     int iteration;
793
794     for (iteration = 0; iteration < 50; iteration++) {
795         enum { N_RULES = 20 };
796         struct test_rule *rules[N_RULES];
797         struct test_rule *tcls_rules[N_RULES];
798         struct classifier cls;
799         struct tcls tcls;
800         int value_pats[N_RULES];
801         int value_mask;
802         int wcf;
803         int i;
804
805         do {
806             wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
807             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
808         } while ((1 << count_ones(value_mask)) < N_RULES);
809
810         classifier_init(&cls);
811         tcls_init(&tcls);
812
813         for (i = 0; i < N_RULES; i++) {
814             unsigned int priority = rand();
815
816             do {
817                 value_pats[i] = rand() & value_mask;
818             } while (array_contains(value_pats, i, value_pats[i]));
819
820             rules[i] = make_rule(wcf, priority, value_pats[i]);
821             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
822             assert(!classifier_insert(&cls, &rules[i]->cls_rule));
823
824             check_tables(&cls, 1, i + 1, 0);
825             compare_classifiers(&cls, &tcls);
826         }
827
828         for (i = 0; i < N_RULES; i++) {
829             tcls_remove(&tcls, tcls_rules[i]);
830             classifier_remove(&cls, &rules[i]->cls_rule);
831             free(rules[i]);
832
833             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
834             compare_classifiers(&cls, &tcls);
835         }
836
837         classifier_destroy(&cls);
838         tcls_destroy(&tcls);
839     }
840 }
841
842 /* Tests classification with many rules at a time that fall into random lists
843  * in 'n' tables. */
844 static void
845 test_many_rules_in_n_tables(int n_tables)
846 {
847     enum { MAX_RULES = 50 };
848     int wcfs[10];
849     int iteration;
850     int i;
851
852     assert(n_tables < 10);
853     for (i = 0; i < n_tables; i++) {
854         do {
855             wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
856         } while (array_contains(wcfs, i, wcfs[i]));
857     }
858
859     for (iteration = 0; iteration < 30; iteration++) {
860         unsigned int priorities[MAX_RULES];
861         struct classifier cls;
862         struct tcls tcls;
863
864         srand(iteration);
865         for (i = 0; i < MAX_RULES; i++) {
866             priorities[i] = i * 129;
867         }
868         shuffle(priorities, ARRAY_SIZE(priorities));
869
870         classifier_init(&cls);
871         tcls_init(&tcls);
872
873         for (i = 0; i < MAX_RULES; i++) {
874             struct test_rule *rule;
875             unsigned int priority = priorities[i];
876             int wcf = wcfs[rand() % n_tables];
877             int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
878             rule = make_rule(wcf, priority, value_pat);
879             tcls_insert(&tcls, rule);
880             assert(!classifier_insert(&cls, &rule->cls_rule));
881             check_tables(&cls, -1, i + 1, -1);
882             compare_classifiers(&cls, &tcls);
883         }
884
885         while (!classifier_is_empty(&cls)) {
886             struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
887                                              sizeof(struct test_rule));
888             int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
889             include |= (rule->cls_rule.wc.wildcards
890                         ? CLS_INC_WILD : CLS_INC_EXACT);
891             classifier_for_each_match(&cls, &rule->cls_rule, include,
892                                       free_rule, &cls);
893             tcls_delete_matches(&tcls, &rule->cls_rule, include);
894             compare_classifiers(&cls, &tcls);
895             check_tables(&cls, -1, -1, -1);
896             free(rule);
897         }
898
899         destroy_classifier(&cls);
900         tcls_destroy(&tcls);
901     }
902 }
903
904 static void
905 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
906 {
907     test_many_rules_in_n_tables(2);
908 }
909
910 static void
911 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
912 {
913     test_many_rules_in_n_tables(5);
914 }
915 \f
916 static const struct command commands[] = {
917     {"empty", 0, 0, test_empty},
918     {"destroy-null", 0, 0, test_destroy_null},
919     {"single-rule", 0, 0, test_single_rule},
920     {"rule-replacement", 0, 0, test_rule_replacement},
921     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
922     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
923     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
924     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
925     {NULL, 0, 0, NULL},
926 };
927
928 int
929 main(int argc, char *argv[])
930 {
931     set_program_name(argv[0]);
932     init_values();
933     run_command(argc - 1, argv + 1, commands);
934     return 0;
935 }