1 /* Copyright (c) 2011, 2012 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, uint16_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(uint16_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;
75 ofpbuf_init(&ofpacts, 0);
76 bundle_parse_load(actions, &ofpacts);
77 action = ofpacts.data;
78 bundle = ofpact_get_BUNDLE(xmemdup(action, action->len));
79 ofpbuf_uninit(&ofpacts);
81 if (bundle->n_slaves > MAX_SLAVES) {
82 ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
89 mask_str(uint8_t mask, size_t n_bits)
94 n_bits = MIN(n_bits, 8);
95 for (i = 0; i < n_bits; i++) {
96 str[i] = (1 << i) & mask ? '1' : '0';
104 main(int argc, char *argv[])
107 struct ofpact_bundle *bundle;
109 size_t i, n_permute, old_n_enabled;
110 struct slave_group sg;
113 set_program_name(argv[0]);
117 ovs_fatal(0, "usage: %s bundle_action", program_name);
120 bundle = parse_bundle_actions(argv[1]);
122 /* Generate 'slaves' array. */
124 for (i = 0; i < bundle->n_slaves; i++) {
125 uint16_t slave_id = bundle->slaves[i];
127 if (slave_lookup(&sg, slave_id)) {
128 ovs_fatal(0, "Redundant slaves are not supported. ");
131 sg.slaves[sg.n_slaves].slave_id = slave_id;
135 /* Generate flows. */
136 flows = xmalloc(N_FLOWS * sizeof *flows);
137 for (i = 0; i < N_FLOWS; i++) {
138 random_bytes(&flows[i], sizeof flows[i]);
139 memset(flows[i].zeros, 0, sizeof flows[i].zeros);
140 flows[i].mpls_depth = 0;
141 flows[i].regs[0] = OFPP_NONE;
144 /* Cycles through each possible liveness permutation for the given
145 * n_slaves. The initial state is equivalent to all slaves down, so we
146 * skip it by starting at i = 1. We do one extra iteration to cover
147 * transitioning from the final state back to the initial state. */
150 n_permute = 1 << sg.n_slaves;
151 for (i = 1; i <= n_permute + 1; i++) {
153 size_t j, n_enabled, changed;
154 double disruption, perfect;
158 mask = i % n_permute;
160 /* Gray coding ensures that in each iteration exactly one slave
161 * changes its liveness. This makes the expected disruption a bit
162 * easier to calculate, and is likely similar to how failures will be
163 * experienced in the wild. */
164 mask = mask ^ (mask >> 1);
166 /* Initialize slaves. */
168 for (j = 0; j < sg.n_slaves; j++) {
169 slave = &sg.slaves[j];
170 slave->flow_count = 0;
171 slave->enabled = ((1 << j) & mask) != 0;
173 if (slave->enabled) {
179 for (j = 0; j < sg.n_slaves; j++) {
180 if (sg.slaves[j].enabled) {
187 for (j = 0; j < N_FLOWS; j++) {
188 struct flow *flow = &flows[j];
189 uint16_t old_slave_id, ofp_port;
191 old_slave_id = flow->regs[0];
192 ofp_port = bundle_execute(bundle, flow, slave_enabled_cb, &sg);
193 flow->regs[0] = ofp_port;
195 if (ofp_port != OFPP_NONE) {
196 slave_lookup(&sg, ofp_port)->flow_count++;
199 if (old_slave_id != ofp_port) {
204 if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
205 perfect = active == old_active ? 0.0 : 1.0;
207 if (old_n_enabled || n_enabled) {
208 perfect = 1.0 / MAX(old_n_enabled, n_enabled);
210 /* This will happen when 'sg.n_slaves' is 0. */
215 disruption = changed / (double)N_FLOWS;
216 printf("%s: disruption=%.2f (perfect=%.2f)",
217 mask_str(mask, sg.n_slaves), disruption, perfect);
219 for (j = 0 ; j < sg.n_slaves; j++) {
220 struct slave *slave = &sg.slaves[j];
223 flow_percent = slave->flow_count / (double)N_FLOWS;
224 printf( " %.2f", flow_percent);
226 if (slave->enabled) {
229 if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
230 perfect_fp = j == active ? 1.0 : 0.0;
232 perfect_fp = 1.0 / n_enabled;
235 if (fabs(flow_percent - perfect_fp) >= .01) {
236 fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
237 " differs from perfect=%.5f by more than .01\n",
238 mask_str(mask, sg.n_slaves), slave->slave_id,
239 flow_percent, perfect_fp);
242 } else if (slave->flow_count) {
243 fprintf(stderr, "%s: slave %d: disabled slave received"
244 " flows.\n", mask_str(mask, sg.n_slaves),
251 if (fabs(disruption - perfect) >= .01) {
252 fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
253 " more than .01\n", mask_str(mask, sg.n_slaves),
254 disruption, perfect);
259 old_n_enabled = n_enabled;