2009-07-08 13:19:16 -07:00
|
|
|
/*
|
2011-02-04 13:17:46 -08:00
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -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:
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* 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.
|
2009-07-08 13:19:16 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "fail-open.h"
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
2010-10-20 16:46:48 -07:00
|
|
|
#include "classifier.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "flow.h"
|
|
|
|
#include "mac-learning.h"
|
|
|
|
#include "odp-util.h"
|
2010-05-27 13:14:05 -07:00
|
|
|
#include "ofp-util.h"
|
2009-09-16 15:12:19 -07:00
|
|
|
#include "ofpbuf.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "ofproto.h"
|
2009-09-16 15:12:19 -07:00
|
|
|
#include "pktbuf.h"
|
|
|
|
#include "poll-loop.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "rconn.h"
|
|
|
|
#include "timeval.h"
|
2009-09-16 15:12:19 -07:00
|
|
|
#include "vconn.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "vlog.h"
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
VLOG_DEFINE_THIS_MODULE(fail_open);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
/*
|
|
|
|
* Fail-open mode.
|
|
|
|
*
|
|
|
|
* In fail-open mode, the switch detects when the controller cannot be
|
|
|
|
* contacted or when the controller is dropping switch connections because the
|
|
|
|
* switch does not pass its admission control policy. In those situations the
|
|
|
|
* switch sets up flows itself using the "normal" action.
|
|
|
|
*
|
|
|
|
* There is a little subtlety to implementation, to properly handle the case
|
|
|
|
* where the controller allows switch connections but drops them a few seconds
|
|
|
|
* later for admission control reasons. Because of this case, we don't want to
|
|
|
|
* just stop setting up flows when we connect to the controller: if we did,
|
|
|
|
* then new flow setup and existing flows would stop during the duration of
|
|
|
|
* connection to the controller, and thus the whole network would go down for
|
|
|
|
* that period of time.
|
|
|
|
*
|
2010-04-07 12:57:21 -07:00
|
|
|
* So, instead, we add some special cases when we are connected to a
|
|
|
|
* controller, but not yet sure that it has admitted us:
|
2009-09-16 15:12:19 -07:00
|
|
|
*
|
|
|
|
* - We set up flows immediately ourselves, but simultaneously send out an
|
|
|
|
* OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
|
|
|
|
* these OFPT_PACKET_IN messages so that duplicate packets don't get sent
|
|
|
|
* out to the network when the controller replies.
|
|
|
|
*
|
|
|
|
* - We also send out OFPT_PACKET_IN messages for totally bogus packets
|
|
|
|
* every so often, in case no real new flows are arriving in the network.
|
|
|
|
*
|
|
|
|
* - We don't flush the flow table at the time we connect, because this
|
|
|
|
* could cause network stuttering in a switch with lots of flows or very
|
|
|
|
* high-bandwidth flows by suddenly throwing lots of packets down to
|
|
|
|
* userspace.
|
|
|
|
*/
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
struct fail_open {
|
|
|
|
struct ofproto *ofproto;
|
2010-04-20 10:43:42 -07:00
|
|
|
struct rconn **controllers;
|
|
|
|
size_t n_controllers;
|
2009-07-08 13:19:16 -07:00
|
|
|
int last_disconn_secs;
|
2009-09-16 15:12:19 -07:00
|
|
|
long long int next_bogus_packet_in;
|
|
|
|
struct rconn_packet_counter *bogus_packet_counter;
|
2009-07-08 13:19:16 -07:00
|
|
|
};
|
|
|
|
|
2010-02-23 15:14:15 -08:00
|
|
|
static void fail_open_recover(struct fail_open *);
|
|
|
|
|
2010-04-20 10:43:42 -07:00
|
|
|
/* Returns the number of seconds of disconnection after which fail-open mode
|
|
|
|
* should activate. */
|
|
|
|
static int
|
|
|
|
trigger_duration(const struct fail_open *fo)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2010-04-20 10:43:42 -07:00
|
|
|
if (!fo->n_controllers) {
|
|
|
|
/* Shouldn't ever arrive here, but if we do, never fail open. */
|
|
|
|
return INT_MAX;
|
|
|
|
} else {
|
|
|
|
/* Otherwise, every controller must have a chance to send an
|
|
|
|
* inactivity probe and reconnect before we fail open, so take the
|
|
|
|
* maximum probe interval and multiply by 3:
|
|
|
|
*
|
|
|
|
* - The first interval is the idle time before sending an inactivity
|
|
|
|
* probe.
|
|
|
|
*
|
|
|
|
* - The second interval is the time allowed for a response to the
|
|
|
|
* inactivity probe.
|
|
|
|
*
|
|
|
|
* - The third interval is the time allowed to reconnect after no
|
|
|
|
* response is received.
|
|
|
|
*/
|
|
|
|
int max_probe_interval;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
max_probe_interval = 0;
|
|
|
|
for (i = 0; i < fo->n_controllers; i++) {
|
|
|
|
int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
|
|
|
|
max_probe_interval = MAX(max_probe_interval, probe_interval);
|
|
|
|
}
|
|
|
|
return max_probe_interval * 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of seconds for which all controllers have been
|
|
|
|
* disconnected. */
|
|
|
|
static int
|
|
|
|
failure_duration(const struct fail_open *fo)
|
|
|
|
{
|
|
|
|
int min_failure_duration;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!fo->n_controllers) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
min_failure_duration = INT_MAX;
|
|
|
|
for (i = 0; i < fo->n_controllers; i++) {
|
|
|
|
int failure_duration = rconn_failure_duration(fo->controllers[i]);
|
|
|
|
min_failure_duration = MIN(min_failure_duration, failure_duration);
|
|
|
|
}
|
|
|
|
return min_failure_duration;
|
2009-09-16 15:12:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
|
|
|
|
bool
|
|
|
|
fail_open_is_active(const struct fail_open *fo)
|
|
|
|
{
|
|
|
|
return fo->last_disconn_secs != 0;
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2010-04-20 10:43:42 -07:00
|
|
|
/* Returns true if at least one controller is connected (regardless of whether
|
|
|
|
* those controllers are believed to have authenticated and accepted this
|
|
|
|
* switch), false if none of them are connected. */
|
|
|
|
static bool
|
|
|
|
any_controller_is_connected(const struct fail_open *fo)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < fo->n_controllers; i++) {
|
|
|
|
if (rconn_is_connected(fo->controllers[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if at least one controller is believed to have authenticated
|
|
|
|
* and accepted this switch, false otherwise. */
|
|
|
|
static bool
|
|
|
|
any_controller_is_admitted(const struct fail_open *fo)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < fo->n_controllers; i++) {
|
|
|
|
if (rconn_is_admitted(fo->controllers[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
static void
|
2010-04-20 10:43:42 -07:00
|
|
|
send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
|
2009-09-16 15:12:19 -07:00
|
|
|
{
|
|
|
|
uint8_t mac[ETH_ADDR_LEN];
|
|
|
|
struct ofpbuf *opi;
|
|
|
|
struct ofpbuf b;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
/* Compose ofp_packet_in. */
|
|
|
|
ofpbuf_init(&b, 128);
|
2010-02-01 18:20:22 -05:00
|
|
|
eth_addr_nicira_random(mac);
|
2009-09-16 15:12:19 -07:00
|
|
|
compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
|
|
|
|
opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
|
|
|
|
ofpbuf_uninit(&b);
|
|
|
|
|
|
|
|
/* Send. */
|
2010-04-20 10:43:42 -07:00
|
|
|
rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_bogus_packet_ins(struct fail_open *fo)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < fo->n_controllers; i++) {
|
|
|
|
if (rconn_is_connected(fo->controllers[i])) {
|
|
|
|
send_bogus_packet_in(fo, fo->controllers[i]);
|
|
|
|
}
|
|
|
|
}
|
2009-09-16 15:12:19 -07:00
|
|
|
}
|
|
|
|
|
2010-04-20 10:43:42 -07:00
|
|
|
/* Enter fail-open mode if we should be in it. */
|
2009-09-16 15:12:19 -07:00
|
|
|
void
|
|
|
|
fail_open_run(struct fail_open *fo)
|
|
|
|
{
|
2010-04-20 10:43:42 -07:00
|
|
|
int disconn_secs = failure_duration(fo);
|
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
/* Enter fail-open mode if 'fo' is not in it but should be. */
|
2010-04-20 10:43:42 -07:00
|
|
|
if (disconn_secs >= trigger_duration(fo)) {
|
2009-09-16 15:12:19 -07:00
|
|
|
if (!fail_open_is_active(fo)) {
|
2009-08-10 18:09:33 -07:00
|
|
|
VLOG_WARN("Could not connect to controller (or switch failed "
|
|
|
|
"controller's post-connection admission control "
|
|
|
|
"policy) for %d seconds, failing open", disconn_secs);
|
2009-07-08 13:19:16 -07:00
|
|
|
fo->last_disconn_secs = disconn_secs;
|
|
|
|
|
|
|
|
/* Flush all OpenFlow and datapath flows. We will set up our
|
|
|
|
* fail-open rule from fail_open_flushed() when
|
|
|
|
* ofproto_flush_flows() calls back to us. */
|
|
|
|
ofproto_flush_flows(fo->ofproto);
|
2009-09-16 15:12:19 -07:00
|
|
|
} else if (disconn_secs > fo->last_disconn_secs + 60) {
|
|
|
|
VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
|
|
|
|
"from controller", disconn_secs);
|
|
|
|
fo->last_disconn_secs = disconn_secs;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
}
|
2009-09-16 15:12:19 -07:00
|
|
|
|
|
|
|
/* Schedule a bogus packet-in if we're connected and in fail-open. */
|
|
|
|
if (fail_open_is_active(fo)) {
|
2010-04-20 10:43:42 -07:00
|
|
|
if (any_controller_is_connected(fo)) {
|
2009-09-16 15:12:19 -07:00
|
|
|
bool expired = time_msec() >= fo->next_bogus_packet_in;
|
|
|
|
if (expired) {
|
2010-04-20 10:43:42 -07:00
|
|
|
send_bogus_packet_ins(fo);
|
2009-09-16 15:12:19 -07:00
|
|
|
}
|
|
|
|
if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
|
|
|
|
fo->next_bogus_packet_in = time_msec() + 2000;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fo->next_bogus_packet_in = LLONG_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
/* If 'fo' is currently in fail-open mode and its rconn has connected to the
|
|
|
|
* controller, exits fail open mode. */
|
2009-07-08 13:19:16 -07:00
|
|
|
void
|
2009-09-16 15:12:19 -07:00
|
|
|
fail_open_maybe_recover(struct fail_open *fo)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2010-04-20 10:43:42 -07:00
|
|
|
if (any_controller_is_admitted(fo)) {
|
2010-02-23 15:14:15 -08:00
|
|
|
fail_open_recover(fo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fail_open_recover(struct fail_open *fo)
|
|
|
|
{
|
|
|
|
if (fail_open_is_active(fo)) {
|
2010-10-20 16:46:48 -07:00
|
|
|
struct cls_rule rule;
|
2009-09-16 15:12:19 -07:00
|
|
|
|
|
|
|
VLOG_WARN("No longer in fail-open mode");
|
|
|
|
fo->last_disconn_secs = 0;
|
|
|
|
fo->next_bogus_packet_in = LLONG_MAX;
|
|
|
|
|
2010-10-20 16:46:48 -07:00
|
|
|
cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
|
|
|
|
ofproto_delete_flow(fo->ofproto, &rule);
|
2009-09-16 15:12:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fail_open_wait(struct fail_open *fo)
|
|
|
|
{
|
|
|
|
if (fo->next_bogus_packet_in != LLONG_MAX) {
|
2010-05-12 12:53:07 -07:00
|
|
|
poll_timer_wait_until(fo->next_bogus_packet_in);
|
2009-09-16 15:12:19 -07:00
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fail_open_flushed(struct fail_open *fo)
|
|
|
|
{
|
2010-04-20 10:43:42 -07:00
|
|
|
int disconn_secs = failure_duration(fo);
|
|
|
|
bool open = disconn_secs >= trigger_duration(fo);
|
2009-07-08 13:19:16 -07:00
|
|
|
if (open) {
|
|
|
|
union ofp_action action;
|
2010-10-20 16:46:48 -07:00
|
|
|
struct cls_rule rule;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
/* Set up a flow that matches every packet and directs them to
|
|
|
|
* OFPP_NORMAL. */
|
|
|
|
memset(&action, 0, sizeof action);
|
|
|
|
action.type = htons(OFPAT_OUTPUT);
|
|
|
|
action.output.len = htons(sizeof action);
|
|
|
|
action.output.port = htons(OFPP_NORMAL);
|
2010-10-20 16:46:48 -07:00
|
|
|
|
|
|
|
cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
|
2010-10-29 11:38:39 -07:00
|
|
|
ofproto_add_flow(fo->ofproto, &rule, &action, 1);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 13:17:46 -08:00
|
|
|
/* Creates and returns a new struct fail_open for 'ofproto'.
|
2010-04-20 10:43:42 -07:00
|
|
|
*
|
|
|
|
* The caller should register its set of controllers with
|
|
|
|
* fail_open_set_controllers(). (There should be at least one controller,
|
|
|
|
* otherwise there isn't any point in having the struct fail_open around.) */
|
2009-07-08 13:19:16 -07:00
|
|
|
struct fail_open *
|
2011-02-04 13:17:46 -08:00
|
|
|
fail_open_create(struct ofproto *ofproto)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
struct fail_open *fo = xmalloc(sizeof *fo);
|
|
|
|
fo->ofproto = ofproto;
|
2010-04-20 10:43:42 -07:00
|
|
|
fo->controllers = NULL;
|
|
|
|
fo->n_controllers = 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
fo->last_disconn_secs = 0;
|
2009-09-16 15:12:19 -07:00
|
|
|
fo->next_bogus_packet_in = LLONG_MAX;
|
|
|
|
fo->bogus_packet_counter = rconn_packet_counter_create();
|
2009-07-08 13:19:16 -07:00
|
|
|
return fo;
|
|
|
|
}
|
|
|
|
|
2010-04-20 10:43:42 -07:00
|
|
|
/* Registers the 'n' rconns in 'rconns' as connections to the controller for
|
|
|
|
* 'fo'. The caller must ensure that all of the rconns remain valid until 'fo'
|
|
|
|
* is destroyed or a new set is registered in a subsequent call.
|
|
|
|
*
|
|
|
|
* Takes ownership of the 'rconns' array, but not of the rconns that it points
|
|
|
|
* to (of which the caller retains ownership). */
|
2009-07-08 13:19:16 -07:00
|
|
|
void
|
2010-04-20 10:43:42 -07:00
|
|
|
fail_open_set_controllers(struct fail_open *fo,
|
|
|
|
struct rconn **rconns, size_t n)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
2010-04-20 10:43:42 -07:00
|
|
|
free(fo->controllers);
|
|
|
|
fo->controllers = rconns;
|
|
|
|
fo->n_controllers = n;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
2010-04-20 10:43:42 -07:00
|
|
|
/* Destroys 'fo'. */
|
2009-07-08 13:19:16 -07:00
|
|
|
void
|
|
|
|
fail_open_destroy(struct fail_open *fo)
|
|
|
|
{
|
|
|
|
if (fo) {
|
2010-02-23 15:14:15 -08:00
|
|
|
fail_open_recover(fo);
|
2010-04-20 10:43:42 -07:00
|
|
|
free(fo->controllers);
|
|
|
|
/* We don't own the rconns behind fo->controllers. */
|
2009-09-16 15:12:19 -07:00
|
|
|
rconn_packet_counter_destroy(fo->bogus_packet_counter);
|
2009-07-08 13:19:16 -07:00
|
|
|
free(fo);
|
|
|
|
}
|
|
|
|
}
|