1 /* Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "ofp-actions.h"
31 #define MAX_SLAVES 8 /* Maximum supported by this test framework. */
42 struct slave slaves[MAX_SLAVES];
46 slave_lookup(struct slave_group *sg, ofp_port_t slave_id)
50 for (i = 0; i < sg->n_slaves; i++) {
51 if (sg->slaves[i].slave_id == slave_id) {
52 return &sg->slaves[i];
60 slave_enabled_cb(ofp_port_t slave_id, void *aux)
64 slave = slave_lookup(aux, slave_id);
65 return slave ? slave->enabled : false;
68 static struct ofpact_bundle *
69 parse_bundle_actions(char *actions)
71 struct ofpact_bundle *bundle;
72 struct ofpbuf ofpacts;
73 struct ofpact *action;
76 ofpbuf_init(&ofpacts, 0);
77 error = bundle_parse_load(actions, &ofpacts);
79 ovs_fatal(0, "%s", error);
82 action = ofpbuf_data(&ofpacts);
83 bundle = ofpact_get_BUNDLE(xmemdup(action, action->len));
84 ofpbuf_uninit(&ofpacts);
86 if (bundle->n_slaves > MAX_SLAVES) {
87 ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
94 mask_str(uint8_t mask, size_t n_bits)
99 n_bits = MIN(n_bits, 8);
100 for (i = 0; i < n_bits; i++) {
101 str[i] = (1 << i) & mask ? '1' : '0';
109 test_bundle_main(int argc, char *argv[])
112 struct ofpact_bundle *bundle;
114 size_t i, n_permute, old_n_enabled;
115 struct slave_group sg;
118 set_program_name(argv[0]);
121 ovs_fatal(0, "usage: %s bundle_action", program_name);
124 bundle = parse_bundle_actions(argv[1]);
126 /* Generate 'slaves' array. */
128 for (i = 0; i < bundle->n_slaves; i++) {
129 ofp_port_t slave_id = bundle->slaves[i];
131 if (slave_lookup(&sg, slave_id)) {
132 ovs_fatal(0, "Redundant slaves are not supported. ");
135 sg.slaves[sg.n_slaves].slave_id = slave_id;
139 /* Generate flows. */
140 flows = xmalloc(N_FLOWS * sizeof *flows);
141 for (i = 0; i < N_FLOWS; i++) {
142 flow_random_hash_fields(&flows[i]);
143 flows[i].regs[0] = ofp_to_u16(OFPP_NONE);
146 /* Cycles through each possible liveness permutation for the given
147 * n_slaves. The initial state is equivalent to all slaves down, so we
148 * skip it by starting at i = 1. We do one extra iteration to cover
149 * transitioning from the final state back to the initial state. */
152 n_permute = 1 << sg.n_slaves;
153 for (i = 1; i <= n_permute + 1; i++) {
155 size_t j, n_enabled, changed;
156 double disruption, perfect;
160 mask = i % n_permute;
162 /* Gray coding ensures that in each iteration exactly one slave
163 * changes its liveness. This makes the expected disruption a bit
164 * easier to calculate, and is likely similar to how failures will be
165 * experienced in the wild. */
166 mask = mask ^ (mask >> 1);
168 /* Initialize slaves. */
170 for (j = 0; j < sg.n_slaves; j++) {
171 slave = &sg.slaves[j];
172 slave->flow_count = 0;
173 slave->enabled = ((1 << j) & mask) != 0;
175 if (slave->enabled) {
181 for (j = 0; j < sg.n_slaves; j++) {
182 if (sg.slaves[j].enabled) {
189 for (j = 0; j < N_FLOWS; j++) {
190 struct flow *flow = &flows[j];
191 ofp_port_t old_slave_id, ofp_port;
192 struct flow_wildcards wc;
194 old_slave_id = u16_to_ofp(flow->regs[0]);
195 ofp_port = bundle_execute(bundle, flow, &wc, slave_enabled_cb,
197 flow->regs[0] = ofp_to_u16(ofp_port);
199 if (ofp_port != OFPP_NONE) {
200 slave_lookup(&sg, ofp_port)->flow_count++;
203 if (old_slave_id != ofp_port) {
208 if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
209 perfect = active == old_active ? 0.0 : 1.0;
211 if (old_n_enabled || n_enabled) {
212 perfect = 1.0 / MAX(old_n_enabled, n_enabled);
214 /* This will happen when 'sg.n_slaves' is 0. */
219 disruption = changed / (double)N_FLOWS;
220 printf("%s: disruption=%.2f (perfect=%.2f)",
221 mask_str(mask, sg.n_slaves), disruption, perfect);
223 for (j = 0 ; j < sg.n_slaves; j++) {
224 struct slave *slave = &sg.slaves[j];
227 flow_percent = slave->flow_count / (double)N_FLOWS;
228 printf( " %.2f", flow_percent);
230 if (slave->enabled) {
233 if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
234 perfect_fp = j == active ? 1.0 : 0.0;
236 perfect_fp = 1.0 / n_enabled;
239 if (fabs(flow_percent - perfect_fp) >= .01) {
240 fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
241 " differs from perfect=%.5f by more than .01\n",
242 mask_str(mask, sg.n_slaves), slave->slave_id,
243 flow_percent, perfect_fp);
246 } else if (slave->flow_count) {
247 fprintf(stderr, "%s: slave %d: disabled slave received"
248 " flows.\n", mask_str(mask, sg.n_slaves),
255 if (fabs(disruption - perfect) >= .01) {
256 fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
257 " more than .01\n", mask_str(mask, sg.n_slaves),
258 disruption, perfect);
263 old_n_enabled = n_enabled;
271 OVSTEST_REGISTER("test-bundle", test_bundle_main);