1 /* Copyright (c) 2011 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.
30 #define MAX_SLAVES 8 /* Maximum supported by this test framework. */
41 struct slave slaves[MAX_SLAVES];
45 slave_lookup(struct slave_group *sg, uint16_t slave_id)
49 for (i = 0; i < sg->n_slaves; i++) {
50 if (sg->slaves[i].slave_id == slave_id) {
51 return &sg->slaves[i];
59 slave_enabled_cb(uint16_t slave_id, void *aux)
63 slave = slave_lookup(aux, slave_id);
64 return slave ? slave->enabled : false;
67 static struct nx_action_bundle *
68 parse_bundle_actions(char *actions)
70 struct nx_action_bundle *nab;
74 bundle_parse_load(&b, actions);
75 nab = ofpbuf_steal_data(&b);
78 if (ntohs(nab->n_slaves) > MAX_SLAVES) {
79 ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
86 mask_str(uint8_t mask, size_t n_bits)
91 n_bits = MIN(n_bits, 8);
92 for (i = 0; i < n_bits; i++) {
93 str[i] = (1 << i) & mask ? '1' : '0';
101 main(int argc, char *argv[])
104 struct nx_action_bundle *nab;
106 size_t i, n_permute, old_n_enabled;
107 struct slave_group sg;
110 set_program_name(argv[0]);
114 ovs_fatal(0, "usage: %s bundle_action", program_name);
117 nab = parse_bundle_actions(argv[1]);
119 /* Generate 'slaves' array. */
121 for (i = 0; i < ntohs(nab->n_slaves); i++) {
122 uint16_t slave_id = bundle_get_slave(nab, i);
124 if (slave_lookup(&sg, slave_id)) {
125 ovs_fatal(0, "Redundant slaves are not supported. ");
128 sg.slaves[sg.n_slaves].slave_id = slave_id;
132 /* Generate flows. */
133 flows = xmalloc(N_FLOWS * sizeof *flows);
134 for (i = 0; i < N_FLOWS; i++) {
135 random_bytes(&flows[i], sizeof flows[i]);
136 flows[i].regs[0] = OFPP_NONE;
139 if (bundle_check(nab, 1024, flows)) {
140 ovs_fatal(0, "Bundle action fails to check.");
143 /* Cycles through each possible liveness permutation for the given
144 * n_slaves. The initial state is equivalent to all slaves down, so we
145 * skip it by starting at i = 1. We do one extra iteration to cover
146 * transitioning from the final state back to the initial state. */
149 n_permute = 1 << sg.n_slaves;
150 for (i = 1; i <= n_permute + 1; i++) {
152 size_t j, n_enabled, changed;
153 double disruption, perfect;
157 mask = i % n_permute;
159 /* Gray coding ensures that in each iteration exactly one slave
160 * changes its liveness. This makes the expected disruption a bit
161 * easier to calculate, and is likely similar to how failures will be
162 * experienced in the wild. */
163 mask = mask ^ (mask >> 1);
165 /* Initialize slaves. */
167 for (j = 0; j < sg.n_slaves; j++) {
168 slave = &sg.slaves[j];
169 slave->flow_count = 0;
170 slave->enabled = ((1 << j) & mask) != 0;
172 if (slave->enabled) {
178 for (j = 0; j < sg.n_slaves; j++) {
179 if (sg.slaves[j].enabled) {
186 for (j = 0; j < N_FLOWS; j++) {
187 struct flow *flow = &flows[j];
188 uint16_t old_slave_id, ofp_port;
190 old_slave_id = flow->regs[0];
191 ofp_port = bundle_execute(nab, flow, slave_enabled_cb, &sg);
192 bundle_execute_load(nab, flow, slave_enabled_cb, &sg);
193 if (flow->regs[0] != ofp_port) {
194 ovs_fatal(0, "bundle_execute_load() and bundle_execute() "
198 if (flow->regs[0] != OFPP_NONE) {
199 slave_lookup(&sg, flow->regs[0])->flow_count++;
202 if (old_slave_id != flow->regs[0]) {
207 if (nab->algorithm == htons(NX_BD_ALG_ACTIVE_BACKUP)) {
208 perfect = active == old_active ? 0.0 : 1.0;
210 if (old_n_enabled || n_enabled) {
211 perfect = 1.0 / MAX(old_n_enabled, n_enabled);
213 /* This will happen when 'sg.n_slaves' is 0. */
218 disruption = changed / (double)N_FLOWS;
219 printf("%s: disruption=%.2f (perfect=%.2f)",
220 mask_str(mask, sg.n_slaves), disruption, perfect);
222 for (j = 0 ; j < sg.n_slaves; j++) {
223 struct slave *slave = &sg.slaves[j];
226 flow_percent = slave->flow_count / (double)N_FLOWS;
227 printf( " %.2f", flow_percent);
229 if (slave->enabled) {
232 if (nab->algorithm == htons(NX_BD_ALG_ACTIVE_BACKUP)) {
233 perfect_fp = j == active ? 1.0 : 0.0;
235 perfect_fp = 1.0 / n_enabled;
238 if (fabs(flow_percent - perfect_fp) >= .01) {
239 fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
240 " differs from perfect=%.5f by more than .01\n",
241 mask_str(mask, sg.n_slaves), slave->slave_id,
242 flow_percent, perfect_fp);
245 } else if (slave->flow_count) {
246 fprintf(stderr, "%s: slave %d: disabled slave received"
247 " flows.\n", mask_str(mask, sg.n_slaves),
254 if (fabs(disruption - perfect) >= .01) {
255 fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
256 " more than .01\n", mask_str(mask, sg.n_slaves),
257 disruption, perfect);
262 old_n_enabled = n_enabled;