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