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