vswitchd: fix 32-bit builds
[openvswitch] / tests / test-multipath.c
1 /*
2  * Copyright (c) 2010, 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 #include <config.h>
18
19 #include "multipath.h"
20
21 #include <assert.h>
22 #include <getopt.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "flow.h"
28 #include "ofp-actions.h"
29 #include "random.h"
30 #include "util.h"
31
32 int
33 main(int argc, char *argv[])
34 {
35     enum { MP_MAX_LINKS = 63 };
36     struct ofpact_multipath mp;
37     bool ok = true;
38     int n;
39
40     set_program_name(argv[0]);
41     random_init();
42
43     if (argc != 2) {
44         ovs_fatal(0, "usage: %s multipath_action", program_name);
45     }
46
47     multipath_parse(&mp, argv[1]);
48     for (n = 1; n <= MP_MAX_LINKS; n++) {
49         enum { N_FLOWS = 65536 };
50         double disruption, perfect, distribution;
51         int histogram[MP_MAX_LINKS];
52         double sum_dev2, stddev;
53         int changed;
54         int i;
55
56         changed = 0;
57         memset(histogram, 0, sizeof histogram);
58         for (i = 0; i < N_FLOWS; i++) {
59             int old_link, new_link;
60             struct flow flow;
61
62             random_bytes(&flow, sizeof flow);
63             memset(flow.zeros, 0, sizeof flow.zeros);
64
65             mp.max_link = n - 1;
66             multipath_execute(&mp, &flow);
67             old_link = flow.regs[0];
68
69             mp.max_link = n;
70             multipath_execute(&mp, &flow);
71             new_link = flow.regs[0];
72
73             assert(old_link >= 0 && old_link < n);
74             assert(new_link >= 0 && new_link < n + 1);
75
76             histogram[old_link]++;
77             changed += old_link != new_link;
78         }
79
80         sum_dev2 = 0.0;
81         for (i = 0; i < n; i++) {
82             double mean = (double) N_FLOWS / n;
83             double deviation = histogram[i] - mean;
84
85             sum_dev2 += deviation * deviation;
86         }
87         stddev = sqrt(sum_dev2 / n);
88
89         disruption = (double) changed / N_FLOWS;
90         perfect = 1.0 / (n + 1);
91         distribution = stddev / ((double) N_FLOWS / n);
92         printf("%2d -> %2d: disruption=%.2f (perfect=%.2f); "
93                "stddev/expected=%.4f\n",
94                n, n + 1, disruption, perfect, distribution);
95
96         switch (mp.algorithm) {
97         case NX_MP_ALG_MODULO_N:
98             if (disruption < (n < 2 ? .25 : .5)) {
99                 fprintf(stderr, "%d -> %d: disruption=%.2f < .5\n",
100                         n, n + 1, disruption);
101                 ok = false;
102             }
103             break;
104
105         case NX_MP_ALG_HASH_THRESHOLD:
106             if (disruption < .48 || disruption > .52) {
107                 fprintf(stderr, "%d -> %d: disruption=%.2f not approximately "
108                         ".5\n", n, n + 1, disruption);
109                 ok = false;
110             }
111             break;
112
113         case NX_MP_ALG_ITER_HASH:
114             if (!(n & (n - 1))) {
115                 break;
116             }
117             /* Fall through. */
118         case NX_MP_ALG_HRW:
119             if (fabs(disruption - perfect) >= .01) {
120                 fprintf(stderr, "%d -> %d: disruption=%.5f differs from "
121                         "perfect=%.5f by more than .01\n",
122                         n, n + 1, disruption, perfect);
123                 ok = false;
124             }
125             break;
126
127         default:
128             NOT_REACHED();
129         }
130     }
131
132     return ok ? 0 : 1;
133 }