2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "fail-open.h"
22 #include "mac-learning.h"
27 #include "poll-loop.h"
33 #define THIS_MODULE VLM_fail_open
39 * In fail-open mode, the switch detects when the controller cannot be
40 * contacted or when the controller is dropping switch connections because the
41 * switch does not pass its admission control policy. In those situations the
42 * switch sets up flows itself using the "normal" action.
44 * There is a little subtlety to implementation, to properly handle the case
45 * where the controller allows switch connections but drops them a few seconds
46 * later for admission control reasons. Because of this case, we don't want to
47 * just stop setting up flows when we connect to the controller: if we did,
48 * then new flow setup and existing flows would stop during the duration of
49 * connection to the controller, and thus the whole network would go down for
50 * that period of time.
52 * So, instead, we add some special cases when we are connected to a
53 * controller, but not yet sure that it has admitted us:
55 * - We set up flows immediately ourselves, but simultaneously send out an
56 * OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
57 * these OFPT_PACKET_IN messages so that duplicate packets don't get sent
58 * out to the network when the controller replies.
60 * - We also send out OFPT_PACKET_IN messages for totally bogus packets
61 * every so often, in case no real new flows are arriving in the network.
63 * - We don't flush the flow table at the time we connect, because this
64 * could cause network stuttering in a switch with lots of flows or very
65 * high-bandwidth flows by suddenly throwing lots of packets down to
70 struct ofproto *ofproto;
71 struct rconn **controllers;
73 int last_disconn_secs;
74 struct status_category *ss_cat;
75 long long int next_bogus_packet_in;
76 struct rconn_packet_counter *bogus_packet_counter;
79 static void fail_open_recover(struct fail_open *);
81 /* Returns the number of seconds of disconnection after which fail-open mode
84 trigger_duration(const struct fail_open *fo)
86 if (!fo->n_controllers) {
87 /* Shouldn't ever arrive here, but if we do, never fail open. */
90 /* Otherwise, every controller must have a chance to send an
91 * inactivity probe and reconnect before we fail open, so take the
92 * maximum probe interval and multiply by 3:
94 * - The first interval is the idle time before sending an inactivity
97 * - The second interval is the time allowed for a response to the
100 * - The third interval is the time allowed to reconnect after no
101 * response is received.
103 int max_probe_interval;
106 max_probe_interval = 0;
107 for (i = 0; i < fo->n_controllers; i++) {
108 int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
109 max_probe_interval = MAX(max_probe_interval, probe_interval);
111 return max_probe_interval * 3;
115 /* Returns the number of seconds for which all controllers have been
118 failure_duration(const struct fail_open *fo)
120 int min_failure_duration;
123 if (!fo->n_controllers) {
127 min_failure_duration = INT_MAX;
128 for (i = 0; i < fo->n_controllers; i++) {
129 int failure_duration = rconn_failure_duration(fo->controllers[i]);
130 min_failure_duration = MIN(min_failure_duration, failure_duration);
132 return min_failure_duration;
135 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
137 fail_open_is_active(const struct fail_open *fo)
139 return fo->last_disconn_secs != 0;
142 /* Returns true if at least one controller is connected (regardless of whether
143 * those controllers are believed to have authenticated and accepted this
144 * switch), false if none of them are connected. */
146 any_controller_is_connected(const struct fail_open *fo)
150 for (i = 0; i < fo->n_controllers; i++) {
151 if (rconn_is_connected(fo->controllers[i])) {
158 /* Returns true if at least one controller is believed to have authenticated
159 * and accepted this switch, false otherwise. */
161 any_controller_is_admitted(const struct fail_open *fo)
165 for (i = 0; i < fo->n_controllers; i++) {
166 if (rconn_is_admitted(fo->controllers[i])) {
174 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
176 uint8_t mac[ETH_ADDR_LEN];
180 /* Compose ofp_packet_in. */
181 ofpbuf_init(&b, 128);
182 eth_addr_nicira_random(mac);
183 compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
184 opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
188 rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
192 send_bogus_packet_ins(struct fail_open *fo)
196 for (i = 0; i < fo->n_controllers; i++) {
197 if (rconn_is_connected(fo->controllers[i])) {
198 send_bogus_packet_in(fo, fo->controllers[i]);
203 /* Enter fail-open mode if we should be in it. */
205 fail_open_run(struct fail_open *fo)
207 int disconn_secs = failure_duration(fo);
209 /* Enter fail-open mode if 'fo' is not in it but should be. */
210 if (disconn_secs >= trigger_duration(fo)) {
211 if (!fail_open_is_active(fo)) {
212 VLOG_WARN("Could not connect to controller (or switch failed "
213 "controller's post-connection admission control "
214 "policy) for %d seconds, failing open", disconn_secs);
215 fo->last_disconn_secs = disconn_secs;
217 /* Flush all OpenFlow and datapath flows. We will set up our
218 * fail-open rule from fail_open_flushed() when
219 * ofproto_flush_flows() calls back to us. */
220 ofproto_flush_flows(fo->ofproto);
221 } else if (disconn_secs > fo->last_disconn_secs + 60) {
222 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
223 "from controller", disconn_secs);
224 fo->last_disconn_secs = disconn_secs;
228 /* Schedule a bogus packet-in if we're connected and in fail-open. */
229 if (fail_open_is_active(fo)) {
230 if (any_controller_is_connected(fo)) {
231 bool expired = time_msec() >= fo->next_bogus_packet_in;
233 send_bogus_packet_ins(fo);
235 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
236 fo->next_bogus_packet_in = time_msec() + 2000;
239 fo->next_bogus_packet_in = LLONG_MAX;
245 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
246 * controller, exits fail open mode. */
248 fail_open_maybe_recover(struct fail_open *fo)
250 if (any_controller_is_admitted(fo)) {
251 fail_open_recover(fo);
256 fail_open_recover(struct fail_open *fo)
258 if (fail_open_is_active(fo)) {
261 VLOG_WARN("No longer in fail-open mode");
262 fo->last_disconn_secs = 0;
263 fo->next_bogus_packet_in = LLONG_MAX;
265 memset(&flow, 0, sizeof flow);
266 ofproto_delete_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY);
271 fail_open_wait(struct fail_open *fo)
273 if (fo->next_bogus_packet_in != LLONG_MAX) {
274 poll_timer_wait(fo->next_bogus_packet_in - time_msec());
279 fail_open_flushed(struct fail_open *fo)
281 int disconn_secs = failure_duration(fo);
282 bool open = disconn_secs >= trigger_duration(fo);
284 union ofp_action action;
287 /* Set up a flow that matches every packet and directs them to
289 memset(&action, 0, sizeof action);
290 action.type = htons(OFPAT_OUTPUT);
291 action.output.len = htons(sizeof action);
292 action.output.port = htons(OFPP_NORMAL);
293 memset(&flow, 0, sizeof flow);
294 ofproto_add_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY,
300 fail_open_status_cb(struct status_reply *sr, void *fo_)
302 struct fail_open *fo = fo_;
303 int cur_duration = failure_duration(fo);
304 int trigger = trigger_duration(fo);
306 status_reply_put(sr, "trigger-duration=%d", trigger);
307 status_reply_put(sr, "current-duration=%d", cur_duration);
308 status_reply_put(sr, "triggered=%s",
309 cur_duration >= trigger ? "true" : "false");
312 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
313 * status with 'switch_status'.
315 * The caller should register its set of controllers with
316 * fail_open_set_controllers(). (There should be at least one controller,
317 * otherwise there isn't any point in having the struct fail_open around.) */
319 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
321 struct fail_open *fo = xmalloc(sizeof *fo);
322 fo->ofproto = ofproto;
323 fo->controllers = NULL;
324 fo->n_controllers = 0;
325 fo->last_disconn_secs = 0;
326 fo->ss_cat = switch_status_register(switch_status, "fail-open",
327 fail_open_status_cb, fo);
328 fo->next_bogus_packet_in = LLONG_MAX;
329 fo->bogus_packet_counter = rconn_packet_counter_create();
333 /* Registers the 'n' rconns in 'rconns' as connections to the controller for
334 * 'fo'. The caller must ensure that all of the rconns remain valid until 'fo'
335 * is destroyed or a new set is registered in a subsequent call.
337 * Takes ownership of the 'rconns' array, but not of the rconns that it points
338 * to (of which the caller retains ownership). */
340 fail_open_set_controllers(struct fail_open *fo,
341 struct rconn **rconns, size_t n)
343 free(fo->controllers);
344 fo->controllers = rconns;
345 fo->n_controllers = n;
350 fail_open_destroy(struct fail_open *fo)
353 fail_open_recover(fo);
354 free(fo->controllers);
355 /* We don't own the rconns behind fo->controllers. */
356 switch_status_unregister(fo->ss_cat);
357 rconn_packet_counter_destroy(fo->bogus_packet_counter);