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