1 /* Copyright (c) 2011 Nicira Networks.
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(&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;
109 set_program_name(argv[0]);
113 ovs_fatal(0, "usage: %s bundle_action", program_name);
116 nab = parse_bundle_actions(argv[1]);
118 /* Generate 'slaves' array. */
120 for (i = 0; i < ntohs(nab->n_slaves); i++) {
121 uint16_t slave_id = bundle_get_slave(nab, i);
123 if (slave_lookup(&sg, slave_id)) {
124 ovs_fatal(0, "Redundant slaves are not supported. ");
127 sg.slaves[sg.n_slaves].slave_id = slave_id;
131 /* Generate flows. */
132 flows = xmalloc(N_FLOWS * sizeof *flows);
133 for (i = 0; i < N_FLOWS; i++) {
134 random_bytes(&flows[i], sizeof flows[i]);
135 flows[i].regs[0] = OFPP_NONE;
138 /* Cycles through each possible liveness permutation for the given
139 * n_slaves. The initial state is equivalent to all slaves down, so we
140 * skip it by starting at i = 1. We do one extra iteration to cover
141 * transitioning from the final state back to the initial state. */
143 n_permute = 1 << sg.n_slaves;
144 for (i = 1; i <= n_permute + 1; i++) {
146 size_t j, n_enabled, changed;
147 double disruption, perfect;
150 mask = i % n_permute;
152 /* Gray coding ensures that in each iteration exactly one slave
153 * changes its liveness. This makes the expected disruption a bit
154 * easier to calculate, and is likely similar to how failures will be
155 * experienced in the wild. */
156 mask = mask ^ (mask >> 1);
158 /* Initialize slaves. */
160 for (j = 0; j < sg.n_slaves; j++) {
161 slave = &sg.slaves[j];
162 slave->flow_count = 0;
163 slave->enabled = ((1 << j) & mask) != 0;
165 if (slave->enabled) {
171 for (j = 0; j < N_FLOWS; j++) {
172 struct flow *flow = &flows[j];
173 uint16_t old_slave_id;
175 old_slave_id = flow->regs[0];
176 flow->regs[0] = bundle_execute(nab, flow, slave_enabled_cb, &sg);
178 if (flow->regs[0] != OFPP_NONE) {
179 slave_lookup(&sg, flow->regs[0])->flow_count++;
182 if (old_slave_id != flow->regs[0]) {
187 if (old_n_enabled || n_enabled) {
188 perfect = 1.0 / MAX(old_n_enabled, n_enabled);
190 /* This will happen when 'sg.n_slaves' is 0. */
194 disruption = changed / (double)N_FLOWS;
195 printf("%s: disruption=%.2f (perfect=%.2f) ",
196 mask_str(mask, sg.n_slaves), disruption, perfect);
198 for (j = 0 ; j < sg.n_slaves; j++) {
199 struct slave *slave = &sg.slaves[j];
202 flow_percent = slave->flow_count / (double)N_FLOWS;
203 printf("%.2f ", flow_percent);
205 if (slave->enabled) {
206 double perfect_fp = 1.0 / n_enabled;
208 if (fabs(flow_percent - perfect_fp) >= .01) {
209 fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
210 " differs from perfect=%.5f by more than .01\n",
211 mask_str(mask, sg.n_slaves), slave->slave_id,
212 flow_percent, perfect_fp);
215 } else if (slave->flow_count) {
216 fprintf(stderr, "%s: slave %d: disabled slave received"
217 " flows.\n", mask_str(mask, sg.n_slaves),
224 if (fabs(disruption - perfect) >= .01) {
225 fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
226 " more than .01\n", mask_str(mask, sg.n_slaves),
227 disruption, perfect);
231 old_n_enabled = n_enabled;