Remove mpls_depth field from flow
[sliver-openvswitch.git] / tests / test-bundle.c
1 /* Copyright (c) 2011, 2012, 2013 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "bundle.h"
19
20 #include <math.h>
21 #include <stdlib.h>
22
23 #include "flow.h"
24 #include "ofp-actions.h"
25 #include "ofpbuf.h"
26 #include "random.h"
27
28 #include "util.h"
29
30 #define N_FLOWS  50000
31 #define MAX_SLAVES 8 /* Maximum supported by this test framework. */
32
33 struct slave {
34     ofp_port_t slave_id;
35
36     bool enabled;
37     size_t flow_count;
38 };
39
40 struct slave_group {
41     size_t n_slaves;
42     struct slave slaves[MAX_SLAVES];
43 };
44
45 static struct slave *
46 slave_lookup(struct slave_group *sg, ofp_port_t slave_id)
47 {
48     size_t i;
49
50     for (i = 0; i < sg->n_slaves; i++) {
51         if (sg->slaves[i].slave_id == slave_id) {
52             return &sg->slaves[i];
53         }
54     }
55
56     return NULL;
57 }
58
59 static bool
60 slave_enabled_cb(ofp_port_t slave_id, void *aux)
61 {
62     struct slave *slave;
63
64     slave = slave_lookup(aux, slave_id);
65     return slave ? slave->enabled : false;
66 }
67
68 static struct ofpact_bundle *
69 parse_bundle_actions(char *actions)
70 {
71     struct ofpact_bundle *bundle;
72     struct ofpbuf ofpacts;
73     struct ofpact *action;
74     char *error;
75
76     ofpbuf_init(&ofpacts, 0);
77     error = bundle_parse_load(actions, &ofpacts);
78     if (error) {
79         ovs_fatal(0, "%s", error);
80     }
81
82     action = ofpacts.data;
83     bundle = ofpact_get_BUNDLE(xmemdup(action, action->len));
84     ofpbuf_uninit(&ofpacts);
85
86     if (bundle->n_slaves > MAX_SLAVES) {
87         ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
88     }
89
90     return bundle;
91 }
92
93 static const char *
94 mask_str(uint8_t mask, size_t n_bits)
95 {
96     static char str[9];
97     size_t i;
98
99     n_bits = MIN(n_bits, 8);
100     for (i = 0; i < n_bits; i++) {
101         str[i] = (1 << i) & mask ? '1' : '0';
102     }
103     str[i] = '\0';
104
105     return str;
106 }
107
108 int
109 main(int argc, char *argv[])
110 {
111     bool ok = true;
112     struct ofpact_bundle *bundle;
113     struct flow *flows;
114     size_t i, n_permute, old_n_enabled;
115     struct slave_group sg;
116     int old_active;
117
118     set_program_name(argv[0]);
119     random_init();
120
121     if (argc != 2) {
122         ovs_fatal(0, "usage: %s bundle_action", program_name);
123     }
124
125     bundle = parse_bundle_actions(argv[1]);
126
127     /* Generate 'slaves' array. */
128     sg.n_slaves = 0;
129     for (i = 0; i < bundle->n_slaves; i++) {
130         ofp_port_t slave_id = bundle->slaves[i];
131
132         if (slave_lookup(&sg, slave_id)) {
133             ovs_fatal(0, "Redundant slaves are not supported. ");
134         }
135
136         sg.slaves[sg.n_slaves].slave_id = slave_id;
137         sg.n_slaves++;
138     }
139
140     /* Generate flows. */
141     flows = xmalloc(N_FLOWS * sizeof *flows);
142     for (i = 0; i < N_FLOWS; i++) {
143         random_bytes(&flows[i], sizeof flows[i]);
144         flows[i].regs[0] = ofp_to_u16(OFPP_NONE);
145     }
146
147     /* Cycles through each possible liveness permutation for the given
148      * n_slaves.  The initial state is equivalent to all slaves down, so we
149      * skip it by starting at i = 1. We do one extra iteration to cover
150      * transitioning from the final state back to the initial state. */
151     old_n_enabled = 0;
152     old_active = -1;
153     n_permute = 1 << sg.n_slaves;
154     for (i = 1; i <= n_permute + 1; i++) {
155         struct slave *slave;
156         size_t j, n_enabled, changed;
157         double disruption, perfect;
158         uint8_t mask;
159         int active;
160
161         mask = i % n_permute;
162
163         /* Gray coding ensures that in each iteration exactly one slave
164          * changes its liveness.  This makes the expected disruption a bit
165          * easier to calculate, and is likely similar to how failures will be
166          * experienced in the wild. */
167         mask = mask ^ (mask >> 1);
168
169         /* Initialize slaves. */
170         n_enabled = 0;
171         for (j = 0; j < sg.n_slaves; j++) {
172             slave = &sg.slaves[j];
173             slave->flow_count = 0;
174             slave->enabled = ((1 << j) & mask) != 0;
175
176             if (slave->enabled) {
177                 n_enabled++;
178             }
179         }
180
181         active = -1;
182         for (j = 0; j < sg.n_slaves; j++) {
183             if (sg.slaves[j].enabled) {
184                 active = j;
185                 break;
186             }
187         }
188
189         changed = 0;
190         for (j = 0; j < N_FLOWS; j++) {
191             struct flow *flow = &flows[j];
192             ofp_port_t old_slave_id, ofp_port;
193             struct flow_wildcards wc;
194
195             old_slave_id = u16_to_ofp(flow->regs[0]);
196             ofp_port = bundle_execute(bundle, flow, &wc, slave_enabled_cb,
197                                       &sg);
198             flow->regs[0] = ofp_to_u16(ofp_port);
199
200             if (ofp_port != OFPP_NONE) {
201                 slave_lookup(&sg, ofp_port)->flow_count++;
202             }
203
204             if (old_slave_id != ofp_port) {
205                 changed++;
206             }
207         }
208
209         if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
210             perfect = active == old_active ? 0.0 : 1.0;
211         } else {
212             if (old_n_enabled || n_enabled) {
213                 perfect = 1.0 / MAX(old_n_enabled, n_enabled);
214             } else {
215                 /* This will happen when 'sg.n_slaves' is 0. */
216                 perfect = 0;
217             }
218         }
219
220         disruption = changed / (double)N_FLOWS;
221         printf("%s: disruption=%.2f (perfect=%.2f)",
222                mask_str(mask, sg.n_slaves), disruption, perfect);
223
224         for (j = 0 ; j < sg.n_slaves; j++) {
225             struct slave *slave = &sg.slaves[j];
226             double flow_percent;
227
228             flow_percent = slave->flow_count / (double)N_FLOWS;
229             printf( " %.2f", flow_percent);
230
231             if (slave->enabled) {
232                 double perfect_fp;
233
234                 if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
235                     perfect_fp = j == active ? 1.0 : 0.0;
236                 } else {
237                     perfect_fp = 1.0 / n_enabled;
238                 }
239
240                 if (fabs(flow_percent - perfect_fp) >= .01) {
241                     fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
242                             " differs from perfect=%.5f by more than .01\n",
243                             mask_str(mask, sg.n_slaves), slave->slave_id,
244                             flow_percent, perfect_fp);
245                     ok = false;
246                 }
247             } else if (slave->flow_count) {
248                 fprintf(stderr, "%s: slave %d: disabled slave received"
249                         " flows.\n", mask_str(mask, sg.n_slaves),
250                         slave->slave_id);
251                 ok = false;
252             }
253         }
254         printf("\n");
255
256         if (fabs(disruption - perfect) >= .01) {
257             fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
258                     " more than .01\n", mask_str(mask, sg.n_slaves),
259                     disruption, perfect);
260             ok = false;
261         }
262
263         old_active = active;
264         old_n_enabled = n_enabled;
265     }
266
267     free(bundle);
268     free(flows);
269     return ok ? 0 : 1;
270 }