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