classifier: Make use of the classifier thread safe.
[sliver-openvswitch.git] / tests / test-classifier.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "random.h"
38 #include "unaligned.h"
39
40 #undef NDEBUG
41 #include <assert.h>
42
43 /* Fields in a rule. */
44 #define CLS_FIELDS                                                  \
45     /*        struct flow    all-caps */  \
46     /*        member name    name     */  \
47     /*        -----------    -------- */  \
48     CLS_FIELD(tunnel.tun_id, TUN_ID)      \
49     CLS_FIELD(metadata,      METADATA)    \
50     CLS_FIELD(nw_src,        NW_SRC)      \
51     CLS_FIELD(nw_dst,        NW_DST)      \
52     CLS_FIELD(in_port,       IN_PORT)     \
53     CLS_FIELD(vlan_tci,      VLAN_TCI)    \
54     CLS_FIELD(dl_type,       DL_TYPE)     \
55     CLS_FIELD(tp_src,        TP_SRC)      \
56     CLS_FIELD(tp_dst,        TP_DST)      \
57     CLS_FIELD(dl_src,        DL_SRC)      \
58     CLS_FIELD(dl_dst,        DL_DST)      \
59     CLS_FIELD(nw_proto,      NW_PROTO)    \
60     CLS_FIELD(nw_tos,        NW_DSCP)
61
62 /* Field indexes.
63  *
64  * (These are also indexed into struct classifier's 'tables' array.) */
65 enum {
66 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
67     CLS_FIELDS
68 #undef CLS_FIELD
69     CLS_N_FIELDS
70 };
71
72 /* Field information. */
73 struct cls_field {
74     int ofs;                    /* Offset in struct flow. */
75     int len;                    /* Length in bytes. */
76     const char *name;           /* Name (for debugging). */
77 };
78
79 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
80 #define CLS_FIELD(MEMBER, NAME)                 \
81     { offsetof(struct flow, MEMBER),            \
82       sizeof ((struct flow *)0)->MEMBER,        \
83       #NAME },
84     CLS_FIELDS
85 #undef CLS_FIELD
86 };
87
88 struct test_rule {
89     int aux;                    /* Auxiliary data. */
90     struct cls_rule cls_rule;   /* Classifier rule data. */
91 };
92
93 static struct test_rule *
94 test_rule_from_cls_rule(const struct cls_rule *rule)
95 {
96     return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
97 }
98
99 static void
100 test_rule_destroy(struct test_rule *rule)
101 {
102     if (rule) {
103         cls_rule_destroy(&rule->cls_rule);
104         free(rule);
105     }
106 }
107
108 static struct test_rule *make_rule(int wc_fields, unsigned int priority,
109                                    int value_pat);
110 static void free_rule(struct test_rule *);
111 static struct test_rule *clone_rule(const struct test_rule *);
112
113 /* Trivial (linear) classifier. */
114 struct tcls {
115     size_t n_rules;
116     size_t allocated_rules;
117     struct test_rule **rules;
118 };
119
120 static void
121 tcls_init(struct tcls *tcls)
122 {
123     tcls->n_rules = 0;
124     tcls->allocated_rules = 0;
125     tcls->rules = NULL;
126 }
127
128 static void
129 tcls_destroy(struct tcls *tcls)
130 {
131     if (tcls) {
132         size_t i;
133
134         for (i = 0; i < tcls->n_rules; i++) {
135             test_rule_destroy(tcls->rules[i]);
136         }
137         free(tcls->rules);
138     }
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     for (i = 0; i < tcls->n_rules; i++) {
153         const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
154         if (cls_rule_equal(pos, &rule->cls_rule)) {
155             /* Exact match. */
156             free_rule(tcls->rules[i]);
157             tcls->rules[i] = clone_rule(rule);
158             return tcls->rules[i];
159         } else if (pos->priority < rule->cls_rule.priority) {
160             break;
161         }
162     }
163
164     if (tcls->n_rules >= tcls->allocated_rules) {
165         tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
166                                  sizeof *tcls->rules);
167     }
168     if (i != tcls->n_rules) {
169         memmove(&tcls->rules[i + 1], &tcls->rules[i],
170                 sizeof *tcls->rules * (tcls->n_rules - i));
171     }
172     tcls->rules[i] = clone_rule(rule);
173     tcls->n_rules++;
174     return tcls->rules[i];
175 }
176
177 static void
178 tcls_remove(struct tcls *cls, const struct test_rule *rule)
179 {
180     size_t i;
181
182     for (i = 0; i < cls->n_rules; i++) {
183         struct test_rule *pos = cls->rules[i];
184         if (pos == rule) {
185             test_rule_destroy(pos);
186
187             memmove(&cls->rules[i], &cls->rules[i + 1],
188                     sizeof *cls->rules * (cls->n_rules - i - 1));
189
190             cls->n_rules--;
191             return;
192         }
193     }
194     NOT_REACHED();
195 }
196
197 static bool
198 match(const struct cls_rule *wild_, const struct flow *fixed)
199 {
200     struct match wild;
201     int f_idx;
202
203     minimatch_expand(&wild_->match, &wild);
204     for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
205         bool eq;
206
207         if (f_idx == CLS_F_IDX_NW_SRC) {
208             eq = !((fixed->nw_src ^ wild.flow.nw_src)
209                    & wild.wc.masks.nw_src);
210         } else if (f_idx == CLS_F_IDX_NW_DST) {
211             eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
212                    & wild.wc.masks.nw_dst);
213         } else if (f_idx == CLS_F_IDX_TP_SRC) {
214             eq = !((fixed->tp_src ^ wild.flow.tp_src)
215                    & wild.wc.masks.tp_src);
216         } else if (f_idx == CLS_F_IDX_TP_DST) {
217             eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
218                    & wild.wc.masks.tp_dst);
219         } else if (f_idx == CLS_F_IDX_DL_SRC) {
220             eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
221                                        wild.wc.masks.dl_src);
222         } else if (f_idx == CLS_F_IDX_DL_DST) {
223             eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
224                                        wild.wc.masks.dl_dst);
225         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
226             eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
227                    & wild.wc.masks.vlan_tci);
228         } else if (f_idx == CLS_F_IDX_TUN_ID) {
229             eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
230                    & wild.wc.masks.tunnel.tun_id);
231         } else if (f_idx == CLS_F_IDX_METADATA) {
232             eq = !((fixed->metadata ^ wild.flow.metadata)
233                    & wild.wc.masks.metadata);
234         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
235             eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
236                    (wild.wc.masks.nw_tos & IP_DSCP_MASK));
237         } else if (f_idx == CLS_F_IDX_NW_PROTO) {
238             eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
239                    & wild.wc.masks.nw_proto);
240         } else if (f_idx == CLS_F_IDX_DL_TYPE) {
241             eq = !((fixed->dl_type ^ wild.flow.dl_type)
242                    & wild.wc.masks.dl_type);
243         } else if (f_idx == CLS_F_IDX_IN_PORT) {
244             eq = !((fixed->in_port.ofp_port
245                     ^ wild.flow.in_port.ofp_port)
246                    & wild.wc.masks.in_port.ofp_port);
247         } else {
248             NOT_REACHED();
249         }
250
251         if (!eq) {
252             return false;
253         }
254     }
255     return true;
256 }
257
258 static struct cls_rule *
259 tcls_lookup(const struct tcls *cls, const struct flow *flow)
260 {
261     size_t i;
262
263     for (i = 0; i < cls->n_rules; i++) {
264         struct test_rule *pos = cls->rules[i];
265         if (match(&pos->cls_rule, flow)) {
266             return &pos->cls_rule;
267         }
268     }
269     return NULL;
270 }
271
272 static void
273 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
274 {
275     size_t i;
276
277     for (i = 0; i < cls->n_rules; ) {
278         struct test_rule *pos = cls->rules[i];
279         if (!minimask_has_extra(&pos->cls_rule.match.mask,
280                                 &target->match.mask)) {
281             struct flow flow;
282
283             miniflow_expand(&pos->cls_rule.match.flow, &flow);
284             if (match(target, &flow)) {
285                 tcls_remove(cls, pos);
286                 continue;
287             }
288         }
289         i++;
290     }
291 }
292 \f
293 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
294                                     CONSTANT_HTONL(0xc0a04455) };
295 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
296                                     CONSTANT_HTONL(0xc0a04455) };
297 static ovs_be64 tun_id_values[] = {
298     0,
299     CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
300 static ovs_be64 metadata_values[] = {
301     0,
302     CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
303 static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
304 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
305 static ovs_be16 dl_type_values[]
306             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
307 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
308                                     CONSTANT_HTONS(80) };
309 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
310 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
311                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
312 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
313                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
314 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
315 static uint8_t nw_dscp_values[] = { 48, 0 };
316
317 static void *values[CLS_N_FIELDS][2];
318
319 static void
320 init_values(void)
321 {
322     values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
323     values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
324
325     values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
326     values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
327
328     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
329     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
330
331     values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
332     values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
333
334     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
335     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
336
337     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
338     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
339
340     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
341     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
342
343     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
344     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
345
346     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
347     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
348
349     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
350     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
351
352     values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
353     values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
354
355     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
356     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
357
358     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
359     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
360 }
361
362 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
363 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
364 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
365 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
366 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
367 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
368 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
369 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
370 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
371 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
372 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
373 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
374 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
375
376 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
377                        N_NW_DST_VALUES *        \
378                        N_TUN_ID_VALUES *        \
379                        N_IN_PORT_VALUES *       \
380                        N_VLAN_TCI_VALUES *       \
381                        N_DL_TYPE_VALUES *       \
382                        N_TP_SRC_VALUES *        \
383                        N_TP_DST_VALUES *        \
384                        N_DL_SRC_VALUES *        \
385                        N_DL_DST_VALUES *        \
386                        N_NW_PROTO_VALUES *      \
387                        N_NW_DSCP_VALUES)
388
389 static unsigned int
390 get_value(unsigned int *x, unsigned n_values)
391 {
392     unsigned int rem = *x % n_values;
393     *x /= n_values;
394     return rem;
395 }
396
397 static void
398 compare_classifiers(struct classifier *cls, struct tcls *tcls)
399     OVS_REQ_RDLOCK(cls->rwlock)
400 {
401     static const int confidence = 500;
402     unsigned int i;
403
404     assert(classifier_count(cls) == tcls->n_rules);
405     for (i = 0; i < confidence; i++) {
406         struct cls_rule *cr0, *cr1;
407         struct flow flow;
408         unsigned int x;
409
410         x = random_range(N_FLOW_VALUES);
411         memset(&flow, 0, sizeof flow);
412         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
413         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
414         flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
415         flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
416         flow.in_port.ofp_port = in_port_values[get_value(&x,
417                                                    N_IN_PORT_VALUES)];
418         flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
419         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
420         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
421         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
422         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
423                ETH_ADDR_LEN);
424         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
425                ETH_ADDR_LEN);
426         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
427         flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
428
429         cr0 = classifier_lookup(cls, &flow, NULL);
430         cr1 = tcls_lookup(tcls, &flow);
431         assert((cr0 == NULL) == (cr1 == NULL));
432         if (cr0 != NULL) {
433             const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
434             const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
435
436             assert(cls_rule_equal(cr0, cr1));
437             assert(tr0->aux == tr1->aux);
438         }
439     }
440 }
441
442 static void
443 destroy_classifier(struct classifier *cls)
444 {
445     struct test_rule *rule, *next_rule;
446     struct cls_cursor cursor;
447
448     ovs_rwlock_wrlock(&cls->rwlock);
449     cls_cursor_init(&cursor, cls, NULL);
450     CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
451         classifier_remove(cls, &rule->cls_rule);
452         free_rule(rule);
453     }
454     ovs_rwlock_unlock(&cls->rwlock);
455     classifier_destroy(cls);
456 }
457
458 static void
459 check_tables(const struct classifier *cls, int n_tables, int n_rules,
460              int n_dups) OVS_REQ_RDLOCK(cls->rwlock)
461 {
462     const struct cls_table *table;
463     struct test_rule *test_rule;
464     struct cls_cursor cursor;
465     int found_tables = 0;
466     int found_rules = 0;
467     int found_dups = 0;
468     int found_rules2 = 0;
469
470     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
471         const struct cls_rule *head;
472         unsigned int max_priority = 0;
473         unsigned int max_count = 0;
474
475         assert(!hmap_is_empty(&table->rules));
476
477         found_tables++;
478         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
479             unsigned int prev_priority = UINT_MAX;
480             const struct cls_rule *rule;
481
482             if (head->priority > max_priority) {
483                 max_priority = head->priority;
484                 max_count = 1;
485             } else if (head->priority == max_priority) {
486                 ++max_count;
487             }
488
489             found_rules++;
490             LIST_FOR_EACH (rule, list, &head->list) {
491                 assert(rule->priority < prev_priority);
492                 assert(rule->priority <= table->max_priority);
493
494                 prev_priority = rule->priority;
495                 found_rules++;
496                 found_dups++;
497                 assert(classifier_find_rule_exactly(cls, rule) == rule);
498             }
499         }
500         assert(table->max_priority == max_priority);
501         assert(table->max_count == max_count);
502     }
503
504     assert(found_tables == hmap_count(&cls->tables));
505     assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
506     assert(n_rules == -1 || found_rules == n_rules);
507     assert(n_dups == -1 || found_dups == n_dups);
508
509     cls_cursor_init(&cursor, cls, NULL);
510     CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
511         found_rules2++;
512     }
513     assert(found_rules == found_rules2);
514 }
515
516 static struct test_rule *
517 make_rule(int wc_fields, unsigned int priority, int value_pat)
518 {
519     const struct cls_field *f;
520     struct test_rule *rule;
521     struct match match;
522
523     match_init_catchall(&match);
524     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
525         int f_idx = f - cls_fields;
526         int value_idx = (value_pat & (1u << f_idx)) != 0;
527         memcpy((char *) &match.flow + f->ofs,
528                values[f_idx][value_idx], f->len);
529
530         if (f_idx == CLS_F_IDX_NW_SRC) {
531             match.wc.masks.nw_src = htonl(UINT32_MAX);
532         } else if (f_idx == CLS_F_IDX_NW_DST) {
533             match.wc.masks.nw_dst = htonl(UINT32_MAX);
534         } else if (f_idx == CLS_F_IDX_TP_SRC) {
535             match.wc.masks.tp_src = htons(UINT16_MAX);
536         } else if (f_idx == CLS_F_IDX_TP_DST) {
537             match.wc.masks.tp_dst = htons(UINT16_MAX);
538         } else if (f_idx == CLS_F_IDX_DL_SRC) {
539             memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
540         } else if (f_idx == CLS_F_IDX_DL_DST) {
541             memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
542         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
543             match.wc.masks.vlan_tci = htons(UINT16_MAX);
544         } else if (f_idx == CLS_F_IDX_TUN_ID) {
545             match.wc.masks.tunnel.tun_id = htonll(UINT64_MAX);
546         } else if (f_idx == CLS_F_IDX_METADATA) {
547             match.wc.masks.metadata = htonll(UINT64_MAX);
548         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
549             match.wc.masks.nw_tos |= IP_DSCP_MASK;
550         } else if (f_idx == CLS_F_IDX_NW_PROTO) {
551             match.wc.masks.nw_proto = UINT8_MAX;
552         } else if (f_idx == CLS_F_IDX_DL_TYPE) {
553             match.wc.masks.dl_type = htons(UINT16_MAX);
554         } else if (f_idx == CLS_F_IDX_IN_PORT) {
555             match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
556         } else {
557             NOT_REACHED();
558         }
559     }
560
561     rule = xzalloc(sizeof *rule);
562     cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
563     return rule;
564 }
565
566 static struct test_rule *
567 clone_rule(const struct test_rule *src)
568 {
569     struct test_rule *dst;
570
571     dst = xmalloc(sizeof *dst);
572     dst->aux = src->aux;
573     cls_rule_clone(&dst->cls_rule, &src->cls_rule);
574     return dst;
575 }
576
577 static void
578 free_rule(struct test_rule *rule)
579 {
580     cls_rule_destroy(&rule->cls_rule);
581     free(rule);
582 }
583
584 static void
585 shuffle(unsigned int *p, size_t n)
586 {
587     for (; n > 1; n--, p++) {
588         unsigned int *q = &p[random_range(n)];
589         unsigned int tmp = *p;
590         *p = *q;
591         *q = tmp;
592     }
593 }
594
595 static void
596 shuffle_u32s(uint32_t *p, size_t n)
597 {
598     for (; n > 1; n--, p++) {
599         uint32_t *q = &p[random_range(n)];
600         uint32_t tmp = *p;
601         *p = *q;
602         *q = tmp;
603     }
604 }
605 \f
606 /* Classifier tests. */
607
608 /* Tests an empty classifier. */
609 static void
610 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
611 {
612     struct classifier cls;
613     struct tcls tcls;
614
615     classifier_init(&cls);
616     ovs_rwlock_rdlock(&cls.rwlock);
617     tcls_init(&tcls);
618     assert(classifier_is_empty(&cls));
619     assert(tcls_is_empty(&tcls));
620     compare_classifiers(&cls, &tcls);
621     ovs_rwlock_unlock(&cls.rwlock);
622     classifier_destroy(&cls);
623     tcls_destroy(&tcls);
624 }
625
626 /* Destroys a null classifier. */
627 static void
628 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
629 {
630     classifier_destroy(NULL);
631 }
632
633 /* Tests classification with one rule at a time. */
634 static void
635 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
636 {
637     unsigned int wc_fields;     /* Hilarious. */
638
639     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
640         struct classifier cls;
641         struct test_rule *rule, *tcls_rule;
642         struct tcls tcls;
643
644         rule = make_rule(wc_fields,
645                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
646
647         classifier_init(&cls);
648         ovs_rwlock_wrlock(&cls.rwlock);
649         tcls_init(&tcls);
650
651         tcls_rule = tcls_insert(&tcls, rule);
652         classifier_insert(&cls, &rule->cls_rule);
653         check_tables(&cls, 1, 1, 0);
654         compare_classifiers(&cls, &tcls);
655
656         classifier_remove(&cls, &rule->cls_rule);
657         tcls_remove(&tcls, tcls_rule);
658         assert(classifier_is_empty(&cls));
659         assert(tcls_is_empty(&tcls));
660         compare_classifiers(&cls, &tcls);
661
662         free_rule(rule);
663         ovs_rwlock_unlock(&cls.rwlock);
664         classifier_destroy(&cls);
665         tcls_destroy(&tcls);
666     }
667 }
668
669 /* Tests replacing one rule by another. */
670 static void
671 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
672 {
673     unsigned int wc_fields;
674
675     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
676         struct classifier cls;
677         struct test_rule *rule1;
678         struct test_rule *rule2;
679         struct tcls tcls;
680
681         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
682         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
683         rule2->aux += 5;
684         rule2->aux += 5;
685
686         classifier_init(&cls);
687         ovs_rwlock_wrlock(&cls.rwlock);
688         tcls_init(&tcls);
689         tcls_insert(&tcls, rule1);
690         classifier_insert(&cls, &rule1->cls_rule);
691         check_tables(&cls, 1, 1, 0);
692         compare_classifiers(&cls, &tcls);
693         tcls_destroy(&tcls);
694
695         tcls_init(&tcls);
696         tcls_insert(&tcls, rule2);
697         assert(test_rule_from_cls_rule(
698                    classifier_replace(&cls, &rule2->cls_rule)) == rule1);
699         free_rule(rule1);
700         check_tables(&cls, 1, 1, 0);
701         compare_classifiers(&cls, &tcls);
702         tcls_destroy(&tcls);
703         ovs_rwlock_unlock(&cls.rwlock);
704         destroy_classifier(&cls);
705     }
706 }
707
708 static int
709 factorial(int n_items)
710 {
711     int n, i;
712
713     n = 1;
714     for (i = 2; i <= n_items; i++) {
715         n *= i;
716     }
717     return n;
718 }
719
720 static void
721 swap(int *a, int *b)
722 {
723     int tmp = *a;
724     *a = *b;
725     *b = tmp;
726 }
727
728 static void
729 reverse(int *a, int n)
730 {
731     int i;
732
733     for (i = 0; i < n / 2; i++) {
734         int j = n - (i + 1);
735         swap(&a[i], &a[j]);
736     }
737 }
738
739 static bool
740 next_permutation(int *a, int n)
741 {
742     int k;
743
744     for (k = n - 2; k >= 0; k--) {
745         if (a[k] < a[k + 1]) {
746             int l;
747
748             for (l = n - 1; ; l--) {
749                 if (a[l] > a[k]) {
750                     swap(&a[k], &a[l]);
751                     reverse(a + (k + 1), n - (k + 1));
752                     return true;
753                 }
754             }
755         }
756     }
757     return false;
758 }
759
760 /* Tests classification with rules that have the same matching criteria. */
761 static void
762 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
763 {
764     enum { N_RULES = 3 };
765     int n_pris;
766
767     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
768         int ops[N_RULES * 2];
769         int pris[N_RULES];
770         int n_permutations;
771         int i;
772
773         pris[0] = 0;
774         for (i = 1; i < N_RULES; i++) {
775             pris[i] = pris[i - 1] + (n_pris > i);
776         }
777
778         for (i = 0; i < N_RULES * 2; i++) {
779             ops[i] = i / 2;
780         }
781
782         n_permutations = 0;
783         do {
784             struct test_rule *rules[N_RULES];
785             struct test_rule *tcls_rules[N_RULES];
786             int pri_rules[N_RULES];
787             struct classifier cls;
788             struct tcls tcls;
789
790             n_permutations++;
791
792             for (i = 0; i < N_RULES; i++) {
793                 rules[i] = make_rule(456, pris[i], 0);
794                 tcls_rules[i] = NULL;
795                 pri_rules[i] = -1;
796             }
797
798             classifier_init(&cls);
799             ovs_rwlock_wrlock(&cls.rwlock);
800             tcls_init(&tcls);
801
802             for (i = 0; i < ARRAY_SIZE(ops); i++) {
803                 int j = ops[i];
804                 int m, n;
805
806                 if (!tcls_rules[j]) {
807                     struct test_rule *displaced_rule;
808
809                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
810                     displaced_rule = test_rule_from_cls_rule(
811                         classifier_replace(&cls, &rules[j]->cls_rule));
812                     if (pri_rules[pris[j]] >= 0) {
813                         int k = pri_rules[pris[j]];
814                         assert(displaced_rule != NULL);
815                         assert(displaced_rule != rules[j]);
816                         assert(pris[j] == displaced_rule->cls_rule.priority);
817                         tcls_rules[k] = NULL;
818                     } else {
819                         assert(displaced_rule == NULL);
820                     }
821                     pri_rules[pris[j]] = j;
822                 } else {
823                     classifier_remove(&cls, &rules[j]->cls_rule);
824                     tcls_remove(&tcls, tcls_rules[j]);
825                     tcls_rules[j] = NULL;
826                     pri_rules[pris[j]] = -1;
827                 }
828
829                 n = 0;
830                 for (m = 0; m < N_RULES; m++) {
831                     n += tcls_rules[m] != NULL;
832                 }
833                 check_tables(&cls, n > 0, n, n - 1);
834
835                 compare_classifiers(&cls, &tcls);
836             }
837
838             ovs_rwlock_unlock(&cls.rwlock);
839             classifier_destroy(&cls);
840             tcls_destroy(&tcls);
841
842             for (i = 0; i < N_RULES; i++) {
843                 free_rule(rules[i]);
844             }
845         } while (next_permutation(ops, ARRAY_SIZE(ops)));
846         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
847     }
848 }
849
850 static int
851 count_ones(unsigned long int x)
852 {
853     int n = 0;
854
855     while (x) {
856         x = zero_rightmost_1bit(x);
857         n++;
858     }
859
860     return n;
861 }
862
863 static bool
864 array_contains(int *array, int n, int value)
865 {
866     int i;
867
868     for (i = 0; i < n; i++) {
869         if (array[i] == value) {
870             return true;
871         }
872     }
873
874     return false;
875 }
876
877 /* Tests classification with two rules at a time that fall into the same
878  * table but different lists. */
879 static void
880 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
881 {
882     int iteration;
883
884     for (iteration = 0; iteration < 50; iteration++) {
885         enum { N_RULES = 20 };
886         struct test_rule *rules[N_RULES];
887         struct test_rule *tcls_rules[N_RULES];
888         struct classifier cls;
889         struct tcls tcls;
890         int value_pats[N_RULES];
891         int value_mask;
892         int wcf;
893         int i;
894
895         do {
896             wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
897             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
898         } while ((1 << count_ones(value_mask)) < N_RULES);
899
900         classifier_init(&cls);
901         ovs_rwlock_wrlock(&cls.rwlock);
902         tcls_init(&tcls);
903
904         for (i = 0; i < N_RULES; i++) {
905             unsigned int priority = random_uint32();
906
907             do {
908                 value_pats[i] = random_uint32() & value_mask;
909             } while (array_contains(value_pats, i, value_pats[i]));
910
911             rules[i] = make_rule(wcf, priority, value_pats[i]);
912             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
913             classifier_insert(&cls, &rules[i]->cls_rule);
914
915             check_tables(&cls, 1, i + 1, 0);
916             compare_classifiers(&cls, &tcls);
917         }
918
919         for (i = 0; i < N_RULES; i++) {
920             tcls_remove(&tcls, tcls_rules[i]);
921             classifier_remove(&cls, &rules[i]->cls_rule);
922             free_rule(rules[i]);
923
924             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
925             compare_classifiers(&cls, &tcls);
926         }
927
928         ovs_rwlock_unlock(&cls.rwlock);
929         classifier_destroy(&cls);
930         tcls_destroy(&tcls);
931     }
932 }
933
934 /* Tests classification with many rules at a time that fall into random lists
935  * in 'n' tables. */
936 static void
937 test_many_rules_in_n_tables(int n_tables)
938 {
939     enum { MAX_RULES = 50 };
940     int wcfs[10];
941     int iteration;
942     int i;
943
944     assert(n_tables < 10);
945     for (i = 0; i < n_tables; i++) {
946         do {
947             wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
948         } while (array_contains(wcfs, i, wcfs[i]));
949     }
950
951     for (iteration = 0; iteration < 30; iteration++) {
952         unsigned int priorities[MAX_RULES];
953         struct classifier cls;
954         struct tcls tcls;
955
956         random_set_seed(iteration + 1);
957         for (i = 0; i < MAX_RULES; i++) {
958             priorities[i] = i * 129;
959         }
960         shuffle(priorities, ARRAY_SIZE(priorities));
961
962         classifier_init(&cls);
963         ovs_rwlock_wrlock(&cls.rwlock);
964         tcls_init(&tcls);
965
966         for (i = 0; i < MAX_RULES; i++) {
967             struct test_rule *rule;
968             unsigned int priority = priorities[i];
969             int wcf = wcfs[random_range(n_tables)];
970             int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
971             rule = make_rule(wcf, priority, value_pat);
972             tcls_insert(&tcls, rule);
973             classifier_insert(&cls, &rule->cls_rule);
974             check_tables(&cls, -1, i + 1, -1);
975             compare_classifiers(&cls, &tcls);
976         }
977
978         while (!classifier_is_empty(&cls)) {
979             struct test_rule *rule, *next_rule;
980             struct test_rule *target;
981             struct cls_cursor cursor;
982
983             target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
984
985             cls_cursor_init(&cursor, &cls, &target->cls_rule);
986             CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
987                 classifier_remove(&cls, &rule->cls_rule);
988                 free_rule(rule);
989             }
990             tcls_delete_matches(&tcls, &target->cls_rule);
991             compare_classifiers(&cls, &tcls);
992             check_tables(&cls, -1, -1, -1);
993             free_rule(target);
994         }
995
996         ovs_rwlock_unlock(&cls.rwlock);
997         destroy_classifier(&cls);
998         tcls_destroy(&tcls);
999     }
1000 }
1001
1002 static void
1003 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1004 {
1005     test_many_rules_in_n_tables(2);
1006 }
1007
1008 static void
1009 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1010 {
1011     test_many_rules_in_n_tables(5);
1012 }
1013 \f
1014 /* Miniflow tests. */
1015
1016 static uint32_t
1017 random_value(void)
1018 {
1019     static const uint32_t values[] =
1020         { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1021           0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1022
1023     return values[random_range(ARRAY_SIZE(values))];
1024 }
1025
1026 static bool
1027 choose(unsigned int n, unsigned int *idxp)
1028 {
1029     if (*idxp < n) {
1030         return true;
1031     } else {
1032         *idxp -= n;
1033         return false;
1034     }
1035 }
1036
1037 static bool
1038 init_consecutive_values(int n_consecutive, struct flow *flow,
1039                         unsigned int *idxp)
1040 {
1041     uint32_t *flow_u32 = (uint32_t *) flow;
1042
1043     if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1044         int i;
1045
1046         for (i = 0; i < n_consecutive; i++) {
1047             flow_u32[*idxp + i] = random_value();
1048         }
1049         return true;
1050     } else {
1051         return false;
1052     }
1053 }
1054
1055 static bool
1056 next_random_flow(struct flow *flow, unsigned int idx)
1057 {
1058     uint32_t *flow_u32 = (uint32_t *) flow;
1059     int i;
1060
1061     memset(flow, 0, sizeof *flow);
1062
1063     /* Empty flow. */
1064     if (choose(1, &idx)) {
1065         return true;
1066     }
1067
1068     /* All flows with a small number of consecutive nonzero values. */
1069     for (i = 1; i <= 4; i++) {
1070         if (init_consecutive_values(i, flow, &idx)) {
1071             return true;
1072         }
1073     }
1074
1075     /* All flows with a large number of consecutive nonzero values. */
1076     for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1077         if (init_consecutive_values(i, flow, &idx)) {
1078             return true;
1079         }
1080     }
1081
1082     /* All flows with exactly two nonconsecutive nonzero values. */
1083     if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1084         int ofs1;
1085
1086         for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1087             int ofs2;
1088
1089             for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1090                 if (choose(1, &idx)) {
1091                     flow_u32[ofs1] = random_value();
1092                     flow_u32[ofs2] = random_value();
1093                     return true;
1094                 }
1095             }
1096         }
1097         NOT_REACHED();
1098     }
1099
1100     /* 16 randomly chosen flows with N >= 3 nonzero values. */
1101     if (choose(16 * (FLOW_U32S - 4), &idx)) {
1102         int n = idx / 16 + 3;
1103         int i;
1104
1105         for (i = 0; i < n; i++) {
1106             flow_u32[i] = random_value();
1107         }
1108         shuffle_u32s(flow_u32, FLOW_U32S);
1109
1110         return true;
1111     }
1112
1113     return false;
1114 }
1115
1116 static void
1117 any_random_flow(struct flow *flow)
1118 {
1119     static unsigned int max;
1120     if (!max) {
1121         while (next_random_flow(flow, max)) {
1122             max++;
1123         }
1124     }
1125
1126     next_random_flow(flow, random_range(max));
1127 }
1128
1129 static void
1130 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1131 {
1132     const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1133     uint32_t *flow_u32 = (uint32_t *) flow;
1134     int i;
1135
1136     for (i = 0; i < FLOW_U32S; i++) {
1137         if (mask_u32[i] != 0) {
1138             uint32_t bit;
1139
1140             do {
1141                 bit = 1u << random_range(32);
1142             } while (!(bit & mask_u32[i]));
1143             flow_u32[i] ^= bit;
1144         }
1145     }
1146 }
1147
1148 static void
1149 wildcard_extra_bits(struct flow_wildcards *mask)
1150 {
1151     uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1152     int i;
1153
1154     for (i = 0; i < FLOW_U32S; i++) {
1155         if (mask_u32[i] != 0) {
1156             uint32_t bit;
1157
1158             do {
1159                 bit = 1u << random_range(32);
1160             } while (!(bit & mask_u32[i]));
1161             mask_u32[i] &= ~bit;
1162         }
1163     }
1164 }
1165
1166 static void
1167 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1168 {
1169     struct flow flow;
1170     unsigned int idx;
1171
1172     random_set_seed(0xb3faca38);
1173     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1174         const uint32_t *flow_u32 = (const uint32_t *) &flow;
1175         struct miniflow miniflow, miniflow2, miniflow3;
1176         struct flow flow2, flow3;
1177         struct flow_wildcards mask;
1178         struct minimask minimask;
1179         int i;
1180
1181         /* Convert flow to miniflow. */
1182         miniflow_init(&miniflow, &flow);
1183
1184         /* Check that the flow equals its miniflow. */
1185         assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1186         for (i = 0; i < FLOW_U32S; i++) {
1187             assert(miniflow_get(&miniflow, i) == flow_u32[i]);
1188         }
1189
1190         /* Check that the miniflow equals itself. */
1191         assert(miniflow_equal(&miniflow, &miniflow));
1192
1193         /* Convert miniflow back to flow and verify that it's the same. */
1194         miniflow_expand(&miniflow, &flow2);
1195         assert(flow_equal(&flow, &flow2));
1196
1197         /* Check that copying a miniflow works properly. */
1198         miniflow_clone(&miniflow2, &miniflow);
1199         assert(miniflow_equal(&miniflow, &miniflow2));
1200         assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1201         miniflow_expand(&miniflow2, &flow3);
1202         assert(flow_equal(&flow, &flow3));
1203
1204         /* Check that masked matches work as expected for identical flows and
1205          * miniflows. */
1206         do {
1207             next_random_flow(&mask.masks, 1);
1208         } while (flow_wildcards_is_catchall(&mask));
1209         minimask_init(&minimask, &mask);
1210         assert(minimask_is_catchall(&minimask)
1211                == flow_wildcards_is_catchall(&mask));
1212         assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1213         assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1214         assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1215                flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1216
1217         /* Check that masked matches work as expected for differing flows and
1218          * miniflows. */
1219         toggle_masked_flow_bits(&flow2, &mask);
1220         assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1221         miniflow_init(&miniflow3, &flow2);
1222         assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1223
1224         /* Clean up. */
1225         miniflow_destroy(&miniflow);
1226         miniflow_destroy(&miniflow2);
1227         miniflow_destroy(&miniflow3);
1228         minimask_destroy(&minimask);
1229     }
1230 }
1231
1232 static void
1233 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1234 {
1235     struct flow_wildcards catchall;
1236     struct minimask minicatchall;
1237     struct flow flow;
1238     unsigned int idx;
1239
1240     flow_wildcards_init_catchall(&catchall);
1241     minimask_init(&minicatchall, &catchall);
1242     assert(minimask_is_catchall(&minicatchall));
1243
1244     random_set_seed(0x2ec7905b);
1245     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1246         struct flow_wildcards mask;
1247         struct minimask minimask;
1248
1249         mask.masks = flow;
1250         minimask_init(&minimask, &mask);
1251         assert(!minimask_has_extra(&minimask, &minimask));
1252         assert(minimask_has_extra(&minicatchall, &minimask)
1253                == !minimask_is_catchall(&minimask));
1254         if (!minimask_is_catchall(&minimask)) {
1255             struct minimask minimask2;
1256
1257             wildcard_extra_bits(&mask);
1258             minimask_init(&minimask2, &mask);
1259             assert(minimask_has_extra(&minimask2, &minimask));
1260             assert(!minimask_has_extra(&minimask, &minimask2));
1261             minimask_destroy(&minimask2);
1262         }
1263
1264         minimask_destroy(&minimask);
1265     }
1266
1267     minimask_destroy(&minicatchall);
1268 }
1269
1270 static void
1271 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1272 {
1273     struct flow_wildcards catchall;
1274     struct minimask minicatchall;
1275     struct flow flow;
1276     unsigned int idx;
1277
1278     flow_wildcards_init_catchall(&catchall);
1279     minimask_init(&minicatchall, &catchall);
1280     assert(minimask_is_catchall(&minicatchall));
1281
1282     random_set_seed(0x181bf0cd);
1283     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1284         struct minimask minimask, minimask2, minicombined;
1285         struct flow_wildcards mask, mask2, combined, combined2;
1286         uint32_t storage[FLOW_U32S];
1287         struct flow flow2;
1288
1289         mask.masks = flow;
1290         minimask_init(&minimask, &mask);
1291
1292         minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1293         assert(minimask_is_catchall(&minicombined));
1294
1295         any_random_flow(&flow2);
1296         mask2.masks = flow2;
1297         minimask_init(&minimask2, &mask2);
1298
1299         minimask_combine(&minicombined, &minimask, &minimask2, storage);
1300         flow_wildcards_and(&combined, &mask, &mask2);
1301         minimask_expand(&minicombined, &combined2);
1302         assert(flow_wildcards_equal(&combined, &combined2));
1303
1304         minimask_destroy(&minimask);
1305         minimask_destroy(&minimask2);
1306     }
1307
1308     minimask_destroy(&minicatchall);
1309 }
1310 \f
1311 static const struct command commands[] = {
1312     /* Classifier tests. */
1313     {"empty", 0, 0, test_empty},
1314     {"destroy-null", 0, 0, test_destroy_null},
1315     {"single-rule", 0, 0, test_single_rule},
1316     {"rule-replacement", 0, 0, test_rule_replacement},
1317     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
1318     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
1319     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1320     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
1321
1322     /* Miniflow and minimask tests. */
1323     {"miniflow", 0, 0, test_miniflow},
1324         {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1325         {"minimask_combine", 0, 0, test_minimask_combine},
1326
1327     {NULL, 0, 0, NULL},
1328 };
1329
1330 int
1331 main(int argc, char *argv[])
1332 {
1333     set_program_name(argv[0]);
1334     init_values();
1335     run_command(argc - 1, argv + 1, commands);
1336     return 0;
1337 }