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