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"
28 #include "poll-loop.h"
35 VLOG_DEFINE_THIS_MODULE(fail_open)
40 * In fail-open mode, the switch detects when the controller cannot be
41 * contacted or when the controller is dropping switch connections because the
42 * switch does not pass its admission control policy. In those situations the
43 * switch sets up flows itself using the "normal" action.
45 * There is a little subtlety to implementation, to properly handle the case
46 * where the controller allows switch connections but drops them a few seconds
47 * later for admission control reasons. Because of this case, we don't want to
48 * just stop setting up flows when we connect to the controller: if we did,
49 * then new flow setup and existing flows would stop during the duration of
50 * connection to the controller, and thus the whole network would go down for
51 * that period of time.
53 * So, instead, we add some special cases when we are connected to a
54 * controller, but not yet sure that it has admitted us:
56 * - We set up flows immediately ourselves, but simultaneously send out an
57 * OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
58 * these OFPT_PACKET_IN messages so that duplicate packets don't get sent
59 * out to the network when the controller replies.
61 * - We also send out OFPT_PACKET_IN messages for totally bogus packets
62 * every so often, in case no real new flows are arriving in the network.
64 * - We don't flush the flow table at the time we connect, because this
65 * could cause network stuttering in a switch with lots of flows or very
66 * high-bandwidth flows by suddenly throwing lots of packets down to
71 struct ofproto *ofproto;
72 struct rconn **controllers;
74 int last_disconn_secs;
75 struct status_category *ss_cat;
76 long long int next_bogus_packet_in;
77 struct rconn_packet_counter *bogus_packet_counter;
80 static void fail_open_recover(struct fail_open *);
82 /* Returns the number of seconds of disconnection after which fail-open mode
85 trigger_duration(const struct fail_open *fo)
87 if (!fo->n_controllers) {
88 /* Shouldn't ever arrive here, but if we do, never fail open. */
91 /* Otherwise, every controller must have a chance to send an
92 * inactivity probe and reconnect before we fail open, so take the
93 * maximum probe interval and multiply by 3:
95 * - The first interval is the idle time before sending an inactivity
98 * - The second interval is the time allowed for a response to the
101 * - The third interval is the time allowed to reconnect after no
102 * response is received.
104 int max_probe_interval;
107 max_probe_interval = 0;
108 for (i = 0; i < fo->n_controllers; i++) {
109 int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
110 max_probe_interval = MAX(max_probe_interval, probe_interval);
112 return max_probe_interval * 3;
116 /* Returns the number of seconds for which all controllers have been
119 failure_duration(const struct fail_open *fo)
121 int min_failure_duration;
124 if (!fo->n_controllers) {
128 min_failure_duration = INT_MAX;
129 for (i = 0; i < fo->n_controllers; i++) {
130 int failure_duration = rconn_failure_duration(fo->controllers[i]);
131 min_failure_duration = MIN(min_failure_duration, failure_duration);
133 return min_failure_duration;
136 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
138 fail_open_is_active(const struct fail_open *fo)
140 return fo->last_disconn_secs != 0;
143 /* Returns true if at least one controller is connected (regardless of whether
144 * those controllers are believed to have authenticated and accepted this
145 * switch), false if none of them are connected. */
147 any_controller_is_connected(const struct fail_open *fo)
151 for (i = 0; i < fo->n_controllers; i++) {
152 if (rconn_is_connected(fo->controllers[i])) {
159 /* Returns true if at least one controller is believed to have authenticated
160 * and accepted this switch, false otherwise. */
162 any_controller_is_admitted(const struct fail_open *fo)
166 for (i = 0; i < fo->n_controllers; i++) {
167 if (rconn_is_admitted(fo->controllers[i])) {
175 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
177 uint8_t mac[ETH_ADDR_LEN];
181 /* Compose ofp_packet_in. */
182 ofpbuf_init(&b, 128);
183 eth_addr_nicira_random(mac);
184 compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
185 opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
189 rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
193 send_bogus_packet_ins(struct fail_open *fo)
197 for (i = 0; i < fo->n_controllers; i++) {
198 if (rconn_is_connected(fo->controllers[i])) {
199 send_bogus_packet_in(fo, fo->controllers[i]);
204 /* Enter fail-open mode if we should be in it. */
206 fail_open_run(struct fail_open *fo)
208 int disconn_secs = failure_duration(fo);
210 /* Enter fail-open mode if 'fo' is not in it but should be. */
211 if (disconn_secs >= trigger_duration(fo)) {
212 if (!fail_open_is_active(fo)) {
213 VLOG_WARN("Could not connect to controller (or switch failed "
214 "controller's post-connection admission control "
215 "policy) for %d seconds, failing open", disconn_secs);
216 fo->last_disconn_secs = disconn_secs;
218 /* Flush all OpenFlow and datapath flows. We will set up our
219 * fail-open rule from fail_open_flushed() when
220 * ofproto_flush_flows() calls back to us. */
221 ofproto_flush_flows(fo->ofproto);
222 } else if (disconn_secs > fo->last_disconn_secs + 60) {
223 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
224 "from controller", disconn_secs);
225 fo->last_disconn_secs = disconn_secs;
229 /* Schedule a bogus packet-in if we're connected and in fail-open. */
230 if (fail_open_is_active(fo)) {
231 if (any_controller_is_connected(fo)) {
232 bool expired = time_msec() >= fo->next_bogus_packet_in;
234 send_bogus_packet_ins(fo);
236 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
237 fo->next_bogus_packet_in = time_msec() + 2000;
240 fo->next_bogus_packet_in = LLONG_MAX;
246 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
247 * controller, exits fail open mode. */
249 fail_open_maybe_recover(struct fail_open *fo)
251 if (any_controller_is_admitted(fo)) {
252 fail_open_recover(fo);
257 fail_open_recover(struct fail_open *fo)
259 if (fail_open_is_active(fo)) {
262 VLOG_WARN("No longer in fail-open mode");
263 fo->last_disconn_secs = 0;
264 fo->next_bogus_packet_in = LLONG_MAX;
266 memset(&flow, 0, sizeof flow);
267 ofproto_delete_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY);
272 fail_open_wait(struct fail_open *fo)
274 if (fo->next_bogus_packet_in != LLONG_MAX) {
275 poll_timer_wait_until(fo->next_bogus_packet_in);
280 fail_open_flushed(struct fail_open *fo)
282 int disconn_secs = failure_duration(fo);
283 bool open = disconn_secs >= trigger_duration(fo);
285 union ofp_action action;
288 /* Set up a flow that matches every packet and directs them to
290 memset(&action, 0, sizeof action);
291 action.type = htons(OFPAT_OUTPUT);
292 action.output.len = htons(sizeof action);
293 action.output.port = htons(OFPP_NORMAL);
294 memset(&flow, 0, sizeof flow);
295 ofproto_add_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY,
301 fail_open_status_cb(struct status_reply *sr, void *fo_)
303 struct fail_open *fo = fo_;
304 int cur_duration = failure_duration(fo);
305 int trigger = trigger_duration(fo);
307 status_reply_put(sr, "trigger-duration=%d", trigger);
308 status_reply_put(sr, "current-duration=%d", cur_duration);
309 status_reply_put(sr, "triggered=%s",
310 cur_duration >= trigger ? "true" : "false");
313 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
314 * status with 'switch_status'.
316 * The caller should register its set of controllers with
317 * fail_open_set_controllers(). (There should be at least one controller,
318 * otherwise there isn't any point in having the struct fail_open around.) */
320 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
322 struct fail_open *fo = xmalloc(sizeof *fo);
323 fo->ofproto = ofproto;
324 fo->controllers = NULL;
325 fo->n_controllers = 0;
326 fo->last_disconn_secs = 0;
327 fo->ss_cat = switch_status_register(switch_status, "fail-open",
328 fail_open_status_cb, fo);
329 fo->next_bogus_packet_in = LLONG_MAX;
330 fo->bogus_packet_counter = rconn_packet_counter_create();
334 /* Registers the 'n' rconns in 'rconns' as connections to the controller for
335 * 'fo'. The caller must ensure that all of the rconns remain valid until 'fo'
336 * is destroyed or a new set is registered in a subsequent call.
338 * Takes ownership of the 'rconns' array, but not of the rconns that it points
339 * to (of which the caller retains ownership). */
341 fail_open_set_controllers(struct fail_open *fo,
342 struct rconn **rconns, size_t n)
344 free(fo->controllers);
345 fo->controllers = rconns;
346 fo->n_controllers = n;
351 fail_open_destroy(struct fail_open *fo)
354 fail_open_recover(fo);
355 free(fo->controllers);
356 /* We don't own the rconns behind fo->controllers. */
357 switch_status_unregister(fo->ss_cat);
358 rconn_packet_counter_destroy(fo->bogus_packet_counter);