lib/classifier: Use internal mutex.
[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 #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         /* This assertion is here to suppress a GCC 4.9 array-bounds warning */
436         ovs_assert(cls->cls->n_tries <= CLS_MAX_TRIES);
437
438         cr0 = classifier_lookup(cls, &flow, &wc);
439         cr1 = tcls_lookup(tcls, &flow);
440         assert((cr0 == NULL) == (cr1 == NULL));
441         if (cr0 != NULL) {
442             const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
443             const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
444
445             assert(cls_rule_equal(cr0, cr1));
446             assert(tr0->aux == tr1->aux);
447         }
448         cr2 = classifier_lookup(cls, &flow, NULL);
449         assert(cr2 == cr0);
450     }
451 }
452
453 static void
454 destroy_classifier(struct classifier *cls)
455 {
456     struct test_rule *rule, *next_rule;
457
458     CLS_FOR_EACH_SAFE (rule, next_rule, cls_rule, cls) {
459         fat_rwlock_wrlock(&cls->rwlock);
460         classifier_remove(cls, &rule->cls_rule);
461         fat_rwlock_unlock(&cls->rwlock);
462         free_rule(rule);
463     }
464     classifier_destroy(cls);
465 }
466
467 static void
468 pvector_verify(struct pvector *pvec)
469 {
470     void *ptr OVS_UNUSED;
471     unsigned int priority, prev_priority = UINT_MAX;
472
473     PVECTOR_FOR_EACH (ptr, pvec) {
474         priority = cursor__.vector[cursor__.entry_idx].priority;
475         if (priority > prev_priority) {
476             VLOG_ABORT("Priority vector is out of order (%u > %u)",
477                        priority, prev_priority);
478         }
479         prev_priority = priority;
480     }
481 }
482
483 static void
484 check_tables(const struct classifier *cls, int n_tables, int n_rules,
485              int n_dups) OVS_EXCLUDED(cls->rwlock)
486 {
487     const struct cls_subtable *table;
488     struct test_rule *test_rule;
489     int found_tables = 0;
490     int found_rules = 0;
491     int found_dups = 0;
492     int found_rules2 = 0;
493
494     pvector_verify(&cls->cls->subtables);
495
496     CMAP_FOR_EACH (table, cmap_node, &cls->cls->subtables_map) {
497         const struct cls_match *head;
498         unsigned int max_priority = 0;
499         unsigned int max_count = 0;
500         bool found = false;
501         const struct cls_subtable *iter;
502
503         /* Locate the subtable from 'subtables'. */
504         PVECTOR_FOR_EACH (iter, &cls->cls->subtables) {
505             if (iter == table) {
506                 if (found) {
507                     VLOG_ABORT("Subtable %p duplicated in 'subtables'.",
508                                table);
509                 }
510                 found = true;
511             }
512         }
513         if (!found) {
514             VLOG_ABORT("Subtable %p not found from 'subtables'.", table);
515         }
516
517         assert(!cmap_is_empty(&table->rules));
518
519         found_tables++;
520         CMAP_FOR_EACH (head, cmap_node, &table->rules) {
521             unsigned int prev_priority = UINT_MAX;
522             const struct cls_match *rule;
523
524             if (head->priority > max_priority) {
525                 max_priority = head->priority;
526                 max_count = 1;
527             } else if (head->priority == max_priority) {
528                 ++max_count;
529             }
530
531             found_rules++;
532             ovs_mutex_lock(&cls->cls->mutex);
533             LIST_FOR_EACH (rule, list, &head->list) {
534                 assert(rule->priority < prev_priority);
535                 assert(rule->priority <= table->max_priority);
536
537                 prev_priority = rule->priority;
538                 found_rules++;
539                 found_dups++;
540                 ovs_mutex_unlock(&cls->cls->mutex);
541                 assert(classifier_find_rule_exactly(cls, rule->cls_rule)
542                        == rule->cls_rule);
543                 ovs_mutex_lock(&cls->cls->mutex);
544             }
545             ovs_mutex_unlock(&cls->cls->mutex);
546         }
547         ovs_mutex_lock(&cls->cls->mutex);
548         assert(table->max_priority == max_priority);
549         assert(table->max_count == max_count);
550         ovs_mutex_unlock(&cls->cls->mutex);
551     }
552
553     assert(found_tables == cmap_count(&cls->cls->subtables_map));
554     assert(found_tables == pvector_count(&cls->cls->subtables));
555     assert(n_tables == -1 || n_tables == cmap_count(&cls->cls->subtables_map));
556     assert(n_rules == -1 || found_rules == n_rules);
557     assert(n_dups == -1 || found_dups == n_dups);
558
559     CLS_FOR_EACH (test_rule, cls_rule, cls) {
560         found_rules2++;
561     }
562     assert(found_rules == found_rules2);
563 }
564
565 static struct test_rule *
566 make_rule(int wc_fields, unsigned int priority, int value_pat)
567 {
568     const struct cls_field *f;
569     struct test_rule *rule;
570     struct match match;
571
572     match_init_catchall(&match);
573     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
574         int f_idx = f - cls_fields;
575         int value_idx = (value_pat & (1u << f_idx)) != 0;
576         memcpy((char *) &match.flow + f->ofs,
577                values[f_idx][value_idx], f->len);
578
579         if (f_idx == CLS_F_IDX_NW_SRC) {
580             match.wc.masks.nw_src = OVS_BE32_MAX;
581         } else if (f_idx == CLS_F_IDX_NW_DST) {
582             match.wc.masks.nw_dst = OVS_BE32_MAX;
583         } else if (f_idx == CLS_F_IDX_TP_SRC) {
584             match.wc.masks.tp_src = OVS_BE16_MAX;
585         } else if (f_idx == CLS_F_IDX_TP_DST) {
586             match.wc.masks.tp_dst = OVS_BE16_MAX;
587         } else if (f_idx == CLS_F_IDX_DL_SRC) {
588             memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
589         } else if (f_idx == CLS_F_IDX_DL_DST) {
590             memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
591         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
592             match.wc.masks.vlan_tci = OVS_BE16_MAX;
593         } else if (f_idx == CLS_F_IDX_TUN_ID) {
594             match.wc.masks.tunnel.tun_id = OVS_BE64_MAX;
595         } else if (f_idx == CLS_F_IDX_METADATA) {
596             match.wc.masks.metadata = OVS_BE64_MAX;
597         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
598             match.wc.masks.nw_tos |= IP_DSCP_MASK;
599         } else if (f_idx == CLS_F_IDX_NW_PROTO) {
600             match.wc.masks.nw_proto = UINT8_MAX;
601         } else if (f_idx == CLS_F_IDX_DL_TYPE) {
602             match.wc.masks.dl_type = OVS_BE16_MAX;
603         } else if (f_idx == CLS_F_IDX_IN_PORT) {
604             match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
605         } else {
606             OVS_NOT_REACHED();
607         }
608     }
609
610     rule = xzalloc(sizeof *rule);
611     cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
612     return rule;
613 }
614
615 static struct test_rule *
616 clone_rule(const struct test_rule *src)
617 {
618     struct test_rule *dst;
619
620     dst = xmalloc(sizeof *dst);
621     dst->aux = src->aux;
622     cls_rule_clone(&dst->cls_rule, &src->cls_rule);
623     return dst;
624 }
625
626 static void
627 free_rule(struct test_rule *rule)
628 {
629     cls_rule_destroy(&rule->cls_rule);
630     free(rule);
631 }
632
633 static void
634 shuffle(unsigned int *p, size_t n)
635 {
636     for (; n > 1; n--, p++) {
637         unsigned int *q = &p[random_range(n)];
638         unsigned int tmp = *p;
639         *p = *q;
640         *q = tmp;
641     }
642 }
643
644 static void
645 shuffle_u32s(uint32_t *p, size_t n)
646 {
647     for (; n > 1; n--, p++) {
648         uint32_t *q = &p[random_range(n)];
649         uint32_t tmp = *p;
650         *p = *q;
651         *q = tmp;
652     }
653 }
654 \f
655 /* Classifier tests. */
656
657 static enum mf_field_id trie_fields[2] = {
658     MFF_IPV4_DST, MFF_IPV4_SRC
659 };
660
661 /* Tests an empty classifier. */
662 static void
663 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
664 {
665     struct classifier cls;
666     struct tcls tcls;
667
668     classifier_init(&cls, flow_segment_u32s);
669     fat_rwlock_wrlock(&cls.rwlock);
670     classifier_set_prefix_fields(&cls, trie_fields, ARRAY_SIZE(trie_fields));
671     tcls_init(&tcls);
672     assert(classifier_is_empty(&cls));
673     assert(tcls_is_empty(&tcls));
674     compare_classifiers(&cls, &tcls);
675     fat_rwlock_unlock(&cls.rwlock);
676     classifier_destroy(&cls);
677     tcls_destroy(&tcls);
678 }
679
680 /* Destroys a null classifier. */
681 static void
682 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
683 {
684     classifier_destroy(NULL);
685 }
686
687 /* Tests classification with one rule at a time. */
688 static void
689 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
690 {
691     unsigned int wc_fields;     /* Hilarious. */
692
693     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
694         struct classifier cls;
695         struct test_rule *rule, *tcls_rule;
696         struct tcls tcls;
697
698         rule = make_rule(wc_fields,
699                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
700
701         classifier_init(&cls, flow_segment_u32s);
702         fat_rwlock_wrlock(&cls.rwlock);
703         classifier_set_prefix_fields(&cls, trie_fields,
704                                      ARRAY_SIZE(trie_fields));
705         tcls_init(&tcls);
706
707         tcls_rule = tcls_insert(&tcls, rule);
708         classifier_insert(&cls, &rule->cls_rule);
709         compare_classifiers(&cls, &tcls);
710         fat_rwlock_unlock(&cls.rwlock);
711         check_tables(&cls, 1, 1, 0);
712
713         fat_rwlock_wrlock(&cls.rwlock);
714         classifier_remove(&cls, &rule->cls_rule);
715         tcls_remove(&tcls, tcls_rule);
716         assert(classifier_is_empty(&cls));
717         assert(tcls_is_empty(&tcls));
718         compare_classifiers(&cls, &tcls);
719         fat_rwlock_unlock(&cls.rwlock);
720
721         free_rule(rule);
722         classifier_destroy(&cls);
723         tcls_destroy(&tcls);
724     }
725 }
726
727 /* Tests replacing one rule by another. */
728 static void
729 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
730 {
731     unsigned int wc_fields;
732
733     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
734         struct classifier cls;
735         struct test_rule *rule1;
736         struct test_rule *rule2;
737         struct tcls tcls;
738
739         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
740         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
741         rule2->aux += 5;
742         rule2->aux += 5;
743
744         classifier_init(&cls, flow_segment_u32s);
745         fat_rwlock_wrlock(&cls.rwlock);
746         classifier_set_prefix_fields(&cls, trie_fields,
747                                      ARRAY_SIZE(trie_fields));
748         tcls_init(&tcls);
749         tcls_insert(&tcls, rule1);
750         classifier_insert(&cls, &rule1->cls_rule);
751         compare_classifiers(&cls, &tcls);
752         fat_rwlock_unlock(&cls.rwlock);
753         check_tables(&cls, 1, 1, 0);
754         tcls_destroy(&tcls);
755
756         tcls_init(&tcls);
757         tcls_insert(&tcls, rule2);
758
759         fat_rwlock_wrlock(&cls.rwlock);
760         assert(test_rule_from_cls_rule(
761                    classifier_replace(&cls, &rule2->cls_rule)) == rule1);
762         free_rule(rule1);
763         compare_classifiers(&cls, &tcls);
764         fat_rwlock_unlock(&cls.rwlock);
765         check_tables(&cls, 1, 1, 0);
766
767         tcls_destroy(&tcls);
768         destroy_classifier(&cls);
769     }
770 }
771
772 static int
773 factorial(int n_items)
774 {
775     int n, i;
776
777     n = 1;
778     for (i = 2; i <= n_items; i++) {
779         n *= i;
780     }
781     return n;
782 }
783
784 static void
785 swap(int *a, int *b)
786 {
787     int tmp = *a;
788     *a = *b;
789     *b = tmp;
790 }
791
792 static void
793 reverse(int *a, int n)
794 {
795     int i;
796
797     for (i = 0; i < n / 2; i++) {
798         int j = n - (i + 1);
799         swap(&a[i], &a[j]);
800     }
801 }
802
803 static bool
804 next_permutation(int *a, int n)
805 {
806     int k;
807
808     for (k = n - 2; k >= 0; k--) {
809         if (a[k] < a[k + 1]) {
810             int l;
811
812             for (l = n - 1; ; l--) {
813                 if (a[l] > a[k]) {
814                     swap(&a[k], &a[l]);
815                     reverse(a + (k + 1), n - (k + 1));
816                     return true;
817                 }
818             }
819         }
820     }
821     return false;
822 }
823
824 /* Tests classification with rules that have the same matching criteria. */
825 static void
826 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
827 {
828     enum { N_RULES = 3 };
829     int n_pris;
830
831     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
832         int ops[N_RULES * 2];
833         int pris[N_RULES];
834         int n_permutations;
835         int i;
836
837         pris[0] = 0;
838         for (i = 1; i < N_RULES; i++) {
839             pris[i] = pris[i - 1] + (n_pris > i);
840         }
841
842         for (i = 0; i < N_RULES * 2; i++) {
843             ops[i] = i / 2;
844         }
845
846         n_permutations = 0;
847         do {
848             struct test_rule *rules[N_RULES];
849             struct test_rule *tcls_rules[N_RULES];
850             int pri_rules[N_RULES];
851             struct classifier cls;
852             struct tcls tcls;
853
854             n_permutations++;
855
856             for (i = 0; i < N_RULES; i++) {
857                 rules[i] = make_rule(456, pris[i], 0);
858                 tcls_rules[i] = NULL;
859                 pri_rules[i] = -1;
860             }
861
862             classifier_init(&cls, flow_segment_u32s);
863             fat_rwlock_wrlock(&cls.rwlock);
864             classifier_set_prefix_fields(&cls, trie_fields,
865                                          ARRAY_SIZE(trie_fields));
866             fat_rwlock_unlock(&cls.rwlock);
867             tcls_init(&tcls);
868
869             for (i = 0; i < ARRAY_SIZE(ops); i++) {
870                 int j = ops[i];
871                 int m, n;
872
873                 fat_rwlock_wrlock(&cls.rwlock);
874                 if (!tcls_rules[j]) {
875                     struct test_rule *displaced_rule;
876
877                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
878                     displaced_rule = test_rule_from_cls_rule(
879                         classifier_replace(&cls, &rules[j]->cls_rule));
880                     if (pri_rules[pris[j]] >= 0) {
881                         int k = pri_rules[pris[j]];
882                         assert(displaced_rule != NULL);
883                         assert(displaced_rule != rules[j]);
884                         assert(pris[j] == displaced_rule->cls_rule.priority);
885                         tcls_rules[k] = NULL;
886                     } else {
887                         assert(displaced_rule == NULL);
888                     }
889                     pri_rules[pris[j]] = j;
890                 } else {
891                     classifier_remove(&cls, &rules[j]->cls_rule);
892                     tcls_remove(&tcls, tcls_rules[j]);
893                     tcls_rules[j] = NULL;
894                     pri_rules[pris[j]] = -1;
895                 }
896                 compare_classifiers(&cls, &tcls);
897                 fat_rwlock_unlock(&cls.rwlock);
898
899                 n = 0;
900                 for (m = 0; m < N_RULES; m++) {
901                     n += tcls_rules[m] != NULL;
902                 }
903                 check_tables(&cls, n > 0, n, n - 1);
904             }
905
906             fat_rwlock_wrlock(&cls.rwlock);
907             for (i = 0; i < N_RULES; i++) {
908                 if (rules[i]->cls_rule.cls_match) {
909                     classifier_remove(&cls, &rules[i]->cls_rule);
910                 }
911                 free_rule(rules[i]);
912             }
913             fat_rwlock_unlock(&cls.rwlock);
914             classifier_destroy(&cls);
915             tcls_destroy(&tcls);
916         } while (next_permutation(ops, ARRAY_SIZE(ops)));
917         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
918     }
919 }
920
921 static int
922 count_ones(unsigned long int x)
923 {
924     int n = 0;
925
926     while (x) {
927         x = zero_rightmost_1bit(x);
928         n++;
929     }
930
931     return n;
932 }
933
934 static bool
935 array_contains(int *array, int n, int value)
936 {
937     int i;
938
939     for (i = 0; i < n; i++) {
940         if (array[i] == value) {
941             return true;
942         }
943     }
944
945     return false;
946 }
947
948 /* Tests classification with two rules at a time that fall into the same
949  * table but different lists. */
950 static void
951 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
952 {
953     int iteration;
954
955     for (iteration = 0; iteration < 50; iteration++) {
956         enum { N_RULES = 20 };
957         struct test_rule *rules[N_RULES];
958         struct test_rule *tcls_rules[N_RULES];
959         struct classifier cls;
960         struct tcls tcls;
961         int value_pats[N_RULES];
962         int value_mask;
963         int wcf;
964         int i;
965
966         do {
967             wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
968             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
969         } while ((1 << count_ones(value_mask)) < N_RULES);
970
971         classifier_init(&cls, flow_segment_u32s);
972         fat_rwlock_wrlock(&cls.rwlock);
973         classifier_set_prefix_fields(&cls, trie_fields,
974                                      ARRAY_SIZE(trie_fields));
975         fat_rwlock_unlock(&cls.rwlock);
976         tcls_init(&tcls);
977
978         for (i = 0; i < N_RULES; i++) {
979             unsigned int priority = random_uint32();
980
981             do {
982                 value_pats[i] = random_uint32() & value_mask;
983             } while (array_contains(value_pats, i, value_pats[i]));
984
985             rules[i] = make_rule(wcf, priority, value_pats[i]);
986             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
987
988             fat_rwlock_wrlock(&cls.rwlock);
989             classifier_insert(&cls, &rules[i]->cls_rule);
990             compare_classifiers(&cls, &tcls);
991             fat_rwlock_unlock(&cls.rwlock);
992
993             check_tables(&cls, 1, i + 1, 0);
994         }
995
996         for (i = 0; i < N_RULES; i++) {
997             tcls_remove(&tcls, tcls_rules[i]);
998             fat_rwlock_wrlock(&cls.rwlock);
999             classifier_remove(&cls, &rules[i]->cls_rule);
1000             compare_classifiers(&cls, &tcls);
1001             fat_rwlock_unlock(&cls.rwlock);
1002             free_rule(rules[i]);
1003
1004             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
1005         }
1006
1007         classifier_destroy(&cls);
1008         tcls_destroy(&tcls);
1009     }
1010 }
1011
1012 /* Tests classification with many rules at a time that fall into random lists
1013  * in 'n' tables. */
1014 static void
1015 test_many_rules_in_n_tables(int n_tables)
1016 {
1017     enum { MAX_RULES = 50 };
1018     int wcfs[10];
1019     int iteration;
1020     int i;
1021
1022     assert(n_tables < 10);
1023     for (i = 0; i < n_tables; i++) {
1024         do {
1025             wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1026         } while (array_contains(wcfs, i, wcfs[i]));
1027     }
1028
1029     for (iteration = 0; iteration < 30; iteration++) {
1030         unsigned int priorities[MAX_RULES];
1031         struct classifier cls;
1032         struct tcls tcls;
1033
1034         random_set_seed(iteration + 1);
1035         for (i = 0; i < MAX_RULES; i++) {
1036             priorities[i] = i * 129;
1037         }
1038         shuffle(priorities, ARRAY_SIZE(priorities));
1039
1040         classifier_init(&cls, flow_segment_u32s);
1041         fat_rwlock_wrlock(&cls.rwlock);
1042         classifier_set_prefix_fields(&cls, trie_fields,
1043                                      ARRAY_SIZE(trie_fields));
1044         fat_rwlock_unlock(&cls.rwlock);
1045         tcls_init(&tcls);
1046
1047         for (i = 0; i < MAX_RULES; i++) {
1048             struct test_rule *rule;
1049             unsigned int priority = priorities[i];
1050             int wcf = wcfs[random_range(n_tables)];
1051             int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1052             rule = make_rule(wcf, priority, value_pat);
1053             tcls_insert(&tcls, rule);
1054             fat_rwlock_wrlock(&cls.rwlock);
1055             classifier_insert(&cls, &rule->cls_rule);
1056             compare_classifiers(&cls, &tcls);
1057             fat_rwlock_unlock(&cls.rwlock);
1058             check_tables(&cls, -1, i + 1, -1);
1059         }
1060
1061         while (!classifier_is_empty(&cls)) {
1062             struct test_rule *rule, *next_rule;
1063             struct test_rule *target;
1064
1065             target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
1066
1067             CLS_FOR_EACH_TARGET_SAFE (rule, next_rule, cls_rule, &cls,
1068                                       &target->cls_rule) {
1069                 fat_rwlock_wrlock(&cls.rwlock);
1070                 classifier_remove(&cls, &rule->cls_rule);
1071                 fat_rwlock_unlock(&cls.rwlock);
1072                 free_rule(rule);
1073             }
1074
1075             tcls_delete_matches(&tcls, &target->cls_rule);
1076             fat_rwlock_rdlock(&cls.rwlock);
1077             compare_classifiers(&cls, &tcls);
1078             fat_rwlock_unlock(&cls.rwlock);
1079             check_tables(&cls, -1, -1, -1);
1080             free_rule(target);
1081         }
1082
1083         destroy_classifier(&cls);
1084         tcls_destroy(&tcls);
1085     }
1086 }
1087
1088 static void
1089 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1090 {
1091     test_many_rules_in_n_tables(2);
1092 }
1093
1094 static void
1095 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1096 {
1097     test_many_rules_in_n_tables(5);
1098 }
1099 \f
1100 /* Miniflow tests. */
1101
1102 static uint32_t
1103 random_value(void)
1104 {
1105     static const uint32_t values[] =
1106         { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1107           0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1108
1109     return values[random_range(ARRAY_SIZE(values))];
1110 }
1111
1112 static bool
1113 choose(unsigned int n, unsigned int *idxp)
1114 {
1115     if (*idxp < n) {
1116         return true;
1117     } else {
1118         *idxp -= n;
1119         return false;
1120     }
1121 }
1122
1123 static bool
1124 init_consecutive_values(int n_consecutive, struct flow *flow,
1125                         unsigned int *idxp)
1126 {
1127     uint32_t *flow_u32 = (uint32_t *) flow;
1128
1129     if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1130         int i;
1131
1132         for (i = 0; i < n_consecutive; i++) {
1133             flow_u32[*idxp + i] = random_value();
1134         }
1135         return true;
1136     } else {
1137         return false;
1138     }
1139 }
1140
1141 static bool
1142 next_random_flow(struct flow *flow, unsigned int idx)
1143 {
1144     uint32_t *flow_u32 = (uint32_t *) flow;
1145     int i;
1146
1147     memset(flow, 0, sizeof *flow);
1148
1149     /* Empty flow. */
1150     if (choose(1, &idx)) {
1151         return true;
1152     }
1153
1154     /* All flows with a small number of consecutive nonzero values. */
1155     for (i = 1; i <= 4; i++) {
1156         if (init_consecutive_values(i, flow, &idx)) {
1157             return true;
1158         }
1159     }
1160
1161     /* All flows with a large number of consecutive nonzero values. */
1162     for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1163         if (init_consecutive_values(i, flow, &idx)) {
1164             return true;
1165         }
1166     }
1167
1168     /* All flows with exactly two nonconsecutive nonzero values. */
1169     if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1170         int ofs1;
1171
1172         for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1173             int ofs2;
1174
1175             for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1176                 if (choose(1, &idx)) {
1177                     flow_u32[ofs1] = random_value();
1178                     flow_u32[ofs2] = random_value();
1179                     return true;
1180                 }
1181             }
1182         }
1183         OVS_NOT_REACHED();
1184     }
1185
1186     /* 16 randomly chosen flows with N >= 3 nonzero values. */
1187     if (choose(16 * (FLOW_U32S - 4), &idx)) {
1188         int n = idx / 16 + 3;
1189         int i;
1190
1191         for (i = 0; i < n; i++) {
1192             flow_u32[i] = random_value();
1193         }
1194         shuffle_u32s(flow_u32, FLOW_U32S);
1195
1196         return true;
1197     }
1198
1199     return false;
1200 }
1201
1202 static void
1203 any_random_flow(struct flow *flow)
1204 {
1205     static unsigned int max;
1206     if (!max) {
1207         while (next_random_flow(flow, max)) {
1208             max++;
1209         }
1210     }
1211
1212     next_random_flow(flow, random_range(max));
1213 }
1214
1215 static void
1216 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1217 {
1218     const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1219     uint32_t *flow_u32 = (uint32_t *) flow;
1220     int i;
1221
1222     for (i = 0; i < FLOW_U32S; i++) {
1223         if (mask_u32[i] != 0) {
1224             uint32_t bit;
1225
1226             do {
1227                 bit = 1u << random_range(32);
1228             } while (!(bit & mask_u32[i]));
1229             flow_u32[i] ^= bit;
1230         }
1231     }
1232 }
1233
1234 static void
1235 wildcard_extra_bits(struct flow_wildcards *mask)
1236 {
1237     uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1238     int i;
1239
1240     for (i = 0; i < FLOW_U32S; i++) {
1241         if (mask_u32[i] != 0) {
1242             uint32_t bit;
1243
1244             do {
1245                 bit = 1u << random_range(32);
1246             } while (!(bit & mask_u32[i]));
1247             mask_u32[i] &= ~bit;
1248         }
1249     }
1250 }
1251
1252 static void
1253 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1254 {
1255     struct flow flow;
1256     unsigned int idx;
1257
1258     random_set_seed(0xb3faca38);
1259     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1260         const uint32_t *flow_u32 = (const uint32_t *) &flow;
1261         struct miniflow miniflow, miniflow2, miniflow3;
1262         struct flow flow2, flow3;
1263         struct flow_wildcards mask;
1264         struct minimask minimask;
1265         int i;
1266
1267         /* Convert flow to miniflow. */
1268         miniflow_init(&miniflow, &flow);
1269
1270         /* Check that the flow equals its miniflow. */
1271         assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1272         for (i = 0; i < FLOW_U32S; i++) {
1273             assert(MINIFLOW_GET_TYPE(&miniflow, uint32_t, i * 4)
1274                    == flow_u32[i]);
1275         }
1276
1277         /* Check that the miniflow equals itself. */
1278         assert(miniflow_equal(&miniflow, &miniflow));
1279
1280         /* Convert miniflow back to flow and verify that it's the same. */
1281         miniflow_expand(&miniflow, &flow2);
1282         assert(flow_equal(&flow, &flow2));
1283
1284         /* Check that copying a miniflow works properly. */
1285         miniflow_clone(&miniflow2, &miniflow);
1286         assert(miniflow_equal(&miniflow, &miniflow2));
1287         assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1288         miniflow_expand(&miniflow2, &flow3);
1289         assert(flow_equal(&flow, &flow3));
1290
1291         /* Check that masked matches work as expected for identical flows and
1292          * miniflows. */
1293         do {
1294             next_random_flow(&mask.masks, 1);
1295         } while (flow_wildcards_is_catchall(&mask));
1296         minimask_init(&minimask, &mask);
1297         assert(minimask_is_catchall(&minimask)
1298                == flow_wildcards_is_catchall(&mask));
1299         assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1300         assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1301         assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1302                flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1303
1304         /* Check that masked matches work as expected for differing flows and
1305          * miniflows. */
1306         toggle_masked_flow_bits(&flow2, &mask);
1307         assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1308         miniflow_init(&miniflow3, &flow2);
1309         assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1310
1311         /* Clean up. */
1312         miniflow_destroy(&miniflow);
1313         miniflow_destroy(&miniflow2);
1314         miniflow_destroy(&miniflow3);
1315         minimask_destroy(&minimask);
1316     }
1317 }
1318
1319 static void
1320 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1321 {
1322     struct flow_wildcards catchall;
1323     struct minimask minicatchall;
1324     struct flow flow;
1325     unsigned int idx;
1326
1327     flow_wildcards_init_catchall(&catchall);
1328     minimask_init(&minicatchall, &catchall);
1329     assert(minimask_is_catchall(&minicatchall));
1330
1331     random_set_seed(0x2ec7905b);
1332     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1333         struct flow_wildcards mask;
1334         struct minimask minimask;
1335
1336         mask.masks = flow;
1337         minimask_init(&minimask, &mask);
1338         assert(!minimask_has_extra(&minimask, &minimask));
1339         assert(minimask_has_extra(&minicatchall, &minimask)
1340                == !minimask_is_catchall(&minimask));
1341         if (!minimask_is_catchall(&minimask)) {
1342             struct minimask minimask2;
1343
1344             wildcard_extra_bits(&mask);
1345             minimask_init(&minimask2, &mask);
1346             assert(minimask_has_extra(&minimask2, &minimask));
1347             assert(!minimask_has_extra(&minimask, &minimask2));
1348             minimask_destroy(&minimask2);
1349         }
1350
1351         minimask_destroy(&minimask);
1352     }
1353
1354     minimask_destroy(&minicatchall);
1355 }
1356
1357 static void
1358 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1359 {
1360     struct flow_wildcards catchall;
1361     struct minimask minicatchall;
1362     struct flow flow;
1363     unsigned int idx;
1364
1365     flow_wildcards_init_catchall(&catchall);
1366     minimask_init(&minicatchall, &catchall);
1367     assert(minimask_is_catchall(&minicatchall));
1368
1369     random_set_seed(0x181bf0cd);
1370     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1371         struct minimask minimask, minimask2, minicombined;
1372         struct flow_wildcards mask, mask2, combined, combined2;
1373         uint32_t storage[FLOW_U32S];
1374         struct flow flow2;
1375
1376         mask.masks = flow;
1377         minimask_init(&minimask, &mask);
1378
1379         minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1380         assert(minimask_is_catchall(&minicombined));
1381
1382         any_random_flow(&flow2);
1383         mask2.masks = flow2;
1384         minimask_init(&minimask2, &mask2);
1385
1386         minimask_combine(&minicombined, &minimask, &minimask2, storage);
1387         flow_wildcards_and(&combined, &mask, &mask2);
1388         minimask_expand(&minicombined, &combined2);
1389         assert(flow_wildcards_equal(&combined, &combined2));
1390
1391         minimask_destroy(&minimask);
1392         minimask_destroy(&minimask2);
1393     }
1394
1395     minimask_destroy(&minicatchall);
1396 }
1397 \f
1398 static const struct command commands[] = {
1399     /* Classifier tests. */
1400     {"empty", 0, 0, test_empty},
1401     {"destroy-null", 0, 0, test_destroy_null},
1402     {"single-rule", 0, 0, test_single_rule},
1403     {"rule-replacement", 0, 0, test_rule_replacement},
1404     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
1405     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
1406     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1407     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
1408
1409     /* Miniflow and minimask tests. */
1410     {"miniflow", 0, 0, test_miniflow},
1411     {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1412     {"minimask_combine", 0, 0, test_minimask_combine},
1413
1414     {NULL, 0, 0, NULL},
1415 };
1416
1417 static void
1418 test_classifier_main(int argc, char *argv[])
1419 {
1420     set_program_name(argv[0]);
1421     init_values();
1422     run_command(argc - 1, argv + 1, commands);
1423 }
1424
1425 OVSTEST_REGISTER("test-classifier", test_classifier_main);