2013-07-08 10:15:00 -07:00
|
|
|
/* Copyright (c) 2011, 2012, 2013 Nicira, Inc.
|
2011-06-10 17:45:45 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "bundle.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "flow.h"
|
2012-07-03 22:17:14 -07:00
|
|
|
#include "ofp-actions.h"
|
2011-06-10 17:45:45 -07:00
|
|
|
#include "ofpbuf.h"
|
|
|
|
#include "random.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define N_FLOWS 50000
|
|
|
|
#define MAX_SLAVES 8 /* Maximum supported by this test framework. */
|
|
|
|
|
|
|
|
struct slave {
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t slave_id;
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
bool enabled;
|
|
|
|
size_t flow_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct slave_group {
|
|
|
|
size_t n_slaves;
|
|
|
|
struct slave slaves[MAX_SLAVES];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct slave *
|
2013-06-19 16:58:44 -07:00
|
|
|
slave_lookup(struct slave_group *sg, ofp_port_t slave_id)
|
2011-06-10 17:45:45 -07:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < sg->n_slaves; i++) {
|
|
|
|
if (sg->slaves[i].slave_id == slave_id) {
|
|
|
|
return &sg->slaves[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-06-19 16:58:44 -07:00
|
|
|
slave_enabled_cb(ofp_port_t slave_id, void *aux)
|
2011-06-10 17:45:45 -07:00
|
|
|
{
|
|
|
|
struct slave *slave;
|
|
|
|
|
|
|
|
slave = slave_lookup(aux, slave_id);
|
|
|
|
return slave ? slave->enabled : false;
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
static struct ofpact_bundle *
|
2011-06-10 17:45:45 -07:00
|
|
|
parse_bundle_actions(char *actions)
|
|
|
|
{
|
2012-07-03 22:17:14 -07:00
|
|
|
struct ofpact_bundle *bundle;
|
|
|
|
struct ofpbuf ofpacts;
|
|
|
|
struct ofpact *action;
|
2013-07-08 10:15:00 -07:00
|
|
|
char *error;
|
2011-06-10 17:45:45 -07:00
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
ofpbuf_init(&ofpacts, 0);
|
2013-07-08 10:15:00 -07:00
|
|
|
error = bundle_parse_load(actions, &ofpacts);
|
|
|
|
if (error) {
|
|
|
|
ovs_fatal(0, "%s", error);
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
action = ofpacts.data;
|
|
|
|
bundle = ofpact_get_BUNDLE(xmemdup(action, action->len));
|
|
|
|
ofpbuf_uninit(&ofpacts);
|
2011-06-10 17:45:45 -07:00
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
if (bundle->n_slaves > MAX_SLAVES) {
|
2011-06-10 17:45:45 -07:00
|
|
|
ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
return bundle;
|
2011-06-10 17:45:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
mask_str(uint8_t mask, size_t n_bits)
|
|
|
|
{
|
|
|
|
static char str[9];
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
n_bits = MIN(n_bits, 8);
|
|
|
|
for (i = 0; i < n_bits; i++) {
|
|
|
|
str[i] = (1 << i) & mask ? '1' : '0';
|
|
|
|
}
|
|
|
|
str[i] = '\0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
bool ok = true;
|
2012-07-03 22:17:14 -07:00
|
|
|
struct ofpact_bundle *bundle;
|
2011-06-10 17:45:45 -07:00
|
|
|
struct flow *flows;
|
|
|
|
size_t i, n_permute, old_n_enabled;
|
|
|
|
struct slave_group sg;
|
2011-07-18 13:50:26 -07:00
|
|
|
int old_active;
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
set_program_name(argv[0]);
|
|
|
|
random_init();
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
ovs_fatal(0, "usage: %s bundle_action", program_name);
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
bundle = parse_bundle_actions(argv[1]);
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
/* Generate 'slaves' array. */
|
|
|
|
sg.n_slaves = 0;
|
2012-07-03 22:17:14 -07:00
|
|
|
for (i = 0; i < bundle->n_slaves; i++) {
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t slave_id = bundle->slaves[i];
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
if (slave_lookup(&sg, slave_id)) {
|
|
|
|
ovs_fatal(0, "Redundant slaves are not supported. ");
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.slaves[sg.n_slaves].slave_id = slave_id;
|
|
|
|
sg.n_slaves++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate flows. */
|
|
|
|
flows = xmalloc(N_FLOWS * sizeof *flows);
|
|
|
|
for (i = 0; i < N_FLOWS; i++) {
|
|
|
|
random_bytes(&flows[i], sizeof flows[i]);
|
2013-06-19 16:58:44 -07:00
|
|
|
flows[i].regs[0] = ofp_to_u16(OFPP_NONE);
|
2011-06-10 17:45:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cycles through each possible liveness permutation for the given
|
|
|
|
* n_slaves. The initial state is equivalent to all slaves down, so we
|
|
|
|
* skip it by starting at i = 1. We do one extra iteration to cover
|
|
|
|
* transitioning from the final state back to the initial state. */
|
|
|
|
old_n_enabled = 0;
|
2011-07-18 13:50:26 -07:00
|
|
|
old_active = -1;
|
2011-06-10 17:45:45 -07:00
|
|
|
n_permute = 1 << sg.n_slaves;
|
|
|
|
for (i = 1; i <= n_permute + 1; i++) {
|
|
|
|
struct slave *slave;
|
|
|
|
size_t j, n_enabled, changed;
|
|
|
|
double disruption, perfect;
|
|
|
|
uint8_t mask;
|
2011-07-18 13:50:26 -07:00
|
|
|
int active;
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
mask = i % n_permute;
|
|
|
|
|
|
|
|
/* Gray coding ensures that in each iteration exactly one slave
|
|
|
|
* changes its liveness. This makes the expected disruption a bit
|
|
|
|
* easier to calculate, and is likely similar to how failures will be
|
|
|
|
* experienced in the wild. */
|
|
|
|
mask = mask ^ (mask >> 1);
|
|
|
|
|
|
|
|
/* Initialize slaves. */
|
|
|
|
n_enabled = 0;
|
|
|
|
for (j = 0; j < sg.n_slaves; j++) {
|
|
|
|
slave = &sg.slaves[j];
|
|
|
|
slave->flow_count = 0;
|
|
|
|
slave->enabled = ((1 << j) & mask) != 0;
|
|
|
|
|
|
|
|
if (slave->enabled) {
|
|
|
|
n_enabled++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-18 13:50:26 -07:00
|
|
|
active = -1;
|
|
|
|
for (j = 0; j < sg.n_slaves; j++) {
|
|
|
|
if (sg.slaves[j].enabled) {
|
|
|
|
active = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-10 17:45:45 -07:00
|
|
|
changed = 0;
|
|
|
|
for (j = 0; j < N_FLOWS; j++) {
|
|
|
|
struct flow *flow = &flows[j];
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t old_slave_id, ofp_port;
|
2013-05-14 18:24:43 -07:00
|
|
|
struct flow_wildcards wc;
|
2011-06-10 17:45:45 -07:00
|
|
|
|
2013-06-19 16:58:44 -07:00
|
|
|
old_slave_id = u16_to_ofp(flow->regs[0]);
|
2013-05-14 18:24:43 -07:00
|
|
|
ofp_port = bundle_execute(bundle, flow, &wc, slave_enabled_cb,
|
|
|
|
&sg);
|
2013-06-19 16:58:44 -07:00
|
|
|
flow->regs[0] = ofp_to_u16(ofp_port);
|
2011-06-10 17:45:45 -07:00
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
if (ofp_port != OFPP_NONE) {
|
|
|
|
slave_lookup(&sg, ofp_port)->flow_count++;
|
2011-06-10 17:45:45 -07:00
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
if (old_slave_id != ofp_port) {
|
2011-06-10 17:45:45 -07:00
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
|
2011-07-18 13:50:26 -07:00
|
|
|
perfect = active == old_active ? 0.0 : 1.0;
|
2011-06-10 17:45:45 -07:00
|
|
|
} else {
|
2011-07-18 13:50:26 -07:00
|
|
|
if (old_n_enabled || n_enabled) {
|
|
|
|
perfect = 1.0 / MAX(old_n_enabled, n_enabled);
|
|
|
|
} else {
|
|
|
|
/* This will happen when 'sg.n_slaves' is 0. */
|
|
|
|
perfect = 0;
|
|
|
|
}
|
2011-06-10 17:45:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
disruption = changed / (double)N_FLOWS;
|
2011-07-18 13:50:26 -07:00
|
|
|
printf("%s: disruption=%.2f (perfect=%.2f)",
|
2011-06-10 17:45:45 -07:00
|
|
|
mask_str(mask, sg.n_slaves), disruption, perfect);
|
|
|
|
|
|
|
|
for (j = 0 ; j < sg.n_slaves; j++) {
|
|
|
|
struct slave *slave = &sg.slaves[j];
|
|
|
|
double flow_percent;
|
|
|
|
|
|
|
|
flow_percent = slave->flow_count / (double)N_FLOWS;
|
2011-07-18 13:50:26 -07:00
|
|
|
printf( " %.2f", flow_percent);
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
if (slave->enabled) {
|
2011-07-18 13:50:26 -07:00
|
|
|
double perfect_fp;
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
|
2011-07-18 13:50:26 -07:00
|
|
|
perfect_fp = j == active ? 1.0 : 0.0;
|
|
|
|
} else {
|
|
|
|
perfect_fp = 1.0 / n_enabled;
|
|
|
|
}
|
2011-06-10 17:45:45 -07:00
|
|
|
|
|
|
|
if (fabs(flow_percent - perfect_fp) >= .01) {
|
|
|
|
fprintf(stderr, "%s: slave %d: flow_percentage=%.5f for"
|
|
|
|
" differs from perfect=%.5f by more than .01\n",
|
|
|
|
mask_str(mask, sg.n_slaves), slave->slave_id,
|
|
|
|
flow_percent, perfect_fp);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else if (slave->flow_count) {
|
|
|
|
fprintf(stderr, "%s: slave %d: disabled slave received"
|
|
|
|
" flows.\n", mask_str(mask, sg.n_slaves),
|
|
|
|
slave->slave_id);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (fabs(disruption - perfect) >= .01) {
|
|
|
|
fprintf(stderr, "%s: disruption=%.5f differs from perfect=%.5f by"
|
|
|
|
" more than .01\n", mask_str(mask, sg.n_slaves),
|
|
|
|
disruption, perfect);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
2011-07-18 13:50:26 -07:00
|
|
|
old_active = active;
|
2011-06-10 17:45:45 -07:00
|
|
|
old_n_enabled = n_enabled;
|
|
|
|
}
|
|
|
|
|
2012-07-03 22:17:14 -07:00
|
|
|
free(bundle);
|
2011-06-10 17:45:45 -07:00
|
|
|
free(flows);
|
|
|
|
return ok ? 0 : 1;
|
|
|
|
}
|