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