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