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"
21 #include "classifier.h"
23 #include "mac-learning.h"
29 #include "poll-loop.h"
36 VLOG_DEFINE_THIS_MODULE(fail_open);
41 * In fail-open mode, the switch detects when the controller cannot be
42 * contacted or when the controller is dropping switch connections because the
43 * switch does not pass its admission control policy. In those situations the
44 * switch sets up flows itself using the "normal" action.
46 * There is a little subtlety to implementation, to properly handle the case
47 * where the controller allows switch connections but drops them a few seconds
48 * later for admission control reasons. Because of this case, we don't want to
49 * just stop setting up flows when we connect to the controller: if we did,
50 * then new flow setup and existing flows would stop during the duration of
51 * connection to the controller, and thus the whole network would go down for
52 * that period of time.
54 * So, instead, we add some special cases when we are connected to a
55 * controller, but not yet sure that it has admitted us:
57 * - We set up flows immediately ourselves, but simultaneously send out an
58 * OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
59 * these OFPT_PACKET_IN messages so that duplicate packets don't get sent
60 * out to the network when the controller replies.
62 * - We also send out OFPT_PACKET_IN messages for totally bogus packets
63 * every so often, in case no real new flows are arriving in the network.
65 * - We don't flush the flow table at the time we connect, because this
66 * could cause network stuttering in a switch with lots of flows or very
67 * high-bandwidth flows by suddenly throwing lots of packets down to
72 struct ofproto *ofproto;
73 struct rconn **controllers;
75 int last_disconn_secs;
76 struct status_category *ss_cat;
77 long long int next_bogus_packet_in;
78 struct rconn_packet_counter *bogus_packet_counter;
81 static void fail_open_recover(struct fail_open *);
83 /* Returns the number of seconds of disconnection after which fail-open mode
86 trigger_duration(const struct fail_open *fo)
88 if (!fo->n_controllers) {
89 /* Shouldn't ever arrive here, but if we do, never fail open. */
92 /* Otherwise, every controller must have a chance to send an
93 * inactivity probe and reconnect before we fail open, so take the
94 * maximum probe interval and multiply by 3:
96 * - The first interval is the idle time before sending an inactivity
99 * - The second interval is the time allowed for a response to the
102 * - The third interval is the time allowed to reconnect after no
103 * response is received.
105 int max_probe_interval;
108 max_probe_interval = 0;
109 for (i = 0; i < fo->n_controllers; i++) {
110 int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
111 max_probe_interval = MAX(max_probe_interval, probe_interval);
113 return max_probe_interval * 3;
117 /* Returns the number of seconds for which all controllers have been
120 failure_duration(const struct fail_open *fo)
122 int min_failure_duration;
125 if (!fo->n_controllers) {
129 min_failure_duration = INT_MAX;
130 for (i = 0; i < fo->n_controllers; i++) {
131 int failure_duration = rconn_failure_duration(fo->controllers[i]);
132 min_failure_duration = MIN(min_failure_duration, failure_duration);
134 return min_failure_duration;
137 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
139 fail_open_is_active(const struct fail_open *fo)
141 return fo->last_disconn_secs != 0;
144 /* Returns true if at least one controller is connected (regardless of whether
145 * those controllers are believed to have authenticated and accepted this
146 * switch), false if none of them are connected. */
148 any_controller_is_connected(const struct fail_open *fo)
152 for (i = 0; i < fo->n_controllers; i++) {
153 if (rconn_is_connected(fo->controllers[i])) {
160 /* Returns true if at least one controller is believed to have authenticated
161 * and accepted this switch, false otherwise. */
163 any_controller_is_admitted(const struct fail_open *fo)
167 for (i = 0; i < fo->n_controllers; i++) {
168 if (rconn_is_admitted(fo->controllers[i])) {
176 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
178 uint8_t mac[ETH_ADDR_LEN];
182 /* Compose ofp_packet_in. */
183 ofpbuf_init(&b, 128);
184 eth_addr_nicira_random(mac);
185 compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
186 opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
190 rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
194 send_bogus_packet_ins(struct fail_open *fo)
198 for (i = 0; i < fo->n_controllers; i++) {
199 if (rconn_is_connected(fo->controllers[i])) {
200 send_bogus_packet_in(fo, fo->controllers[i]);
205 /* Enter fail-open mode if we should be in it. */
207 fail_open_run(struct fail_open *fo)
209 int disconn_secs = failure_duration(fo);
211 /* Enter fail-open mode if 'fo' is not in it but should be. */
212 if (disconn_secs >= trigger_duration(fo)) {
213 if (!fail_open_is_active(fo)) {
214 VLOG_WARN("Could not connect to controller (or switch failed "
215 "controller's post-connection admission control "
216 "policy) for %d seconds, failing open", disconn_secs);
217 fo->last_disconn_secs = disconn_secs;
219 /* Flush all OpenFlow and datapath flows. We will set up our
220 * fail-open rule from fail_open_flushed() when
221 * ofproto_flush_flows() calls back to us. */
222 ofproto_flush_flows(fo->ofproto);
223 } else if (disconn_secs > fo->last_disconn_secs + 60) {
224 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
225 "from controller", disconn_secs);
226 fo->last_disconn_secs = disconn_secs;
230 /* Schedule a bogus packet-in if we're connected and in fail-open. */
231 if (fail_open_is_active(fo)) {
232 if (any_controller_is_connected(fo)) {
233 bool expired = time_msec() >= fo->next_bogus_packet_in;
235 send_bogus_packet_ins(fo);
237 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
238 fo->next_bogus_packet_in = time_msec() + 2000;
241 fo->next_bogus_packet_in = LLONG_MAX;
247 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
248 * controller, exits fail open mode. */
250 fail_open_maybe_recover(struct fail_open *fo)
252 if (any_controller_is_admitted(fo)) {
253 fail_open_recover(fo);
258 fail_open_recover(struct fail_open *fo)
260 if (fail_open_is_active(fo)) {
261 struct cls_rule rule;
263 VLOG_WARN("No longer in fail-open mode");
264 fo->last_disconn_secs = 0;
265 fo->next_bogus_packet_in = LLONG_MAX;
267 cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
268 ofproto_delete_flow(fo->ofproto, &rule);
273 fail_open_wait(struct fail_open *fo)
275 if (fo->next_bogus_packet_in != LLONG_MAX) {
276 poll_timer_wait_until(fo->next_bogus_packet_in);
281 fail_open_flushed(struct fail_open *fo)
283 int disconn_secs = failure_duration(fo);
284 bool open = disconn_secs >= trigger_duration(fo);
286 union ofp_action action;
287 struct cls_rule rule;
289 /* Set up a flow that matches every packet and directs them to
291 memset(&action, 0, sizeof action);
292 action.type = htons(OFPAT_OUTPUT);
293 action.output.len = htons(sizeof action);
294 action.output.port = htons(OFPP_NORMAL);
296 cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
297 ofproto_add_flow(fo->ofproto, &rule, &action, 1);
302 fail_open_status_cb(struct status_reply *sr, void *fo_)
304 struct fail_open *fo = fo_;
305 int cur_duration = failure_duration(fo);
306 int trigger = trigger_duration(fo);
308 status_reply_put(sr, "trigger-duration=%d", trigger);
309 status_reply_put(sr, "current-duration=%d", cur_duration);
310 status_reply_put(sr, "triggered=%s",
311 cur_duration >= trigger ? "true" : "false");
314 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
315 * status with 'switch_status'.
317 * The caller should register its set of controllers with
318 * fail_open_set_controllers(). (There should be at least one controller,
319 * otherwise there isn't any point in having the struct fail_open around.) */
321 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
323 struct fail_open *fo = xmalloc(sizeof *fo);
324 fo->ofproto = ofproto;
325 fo->controllers = NULL;
326 fo->n_controllers = 0;
327 fo->last_disconn_secs = 0;
328 fo->ss_cat = switch_status_register(switch_status, "fail-open",
329 fail_open_status_cb, fo);
330 fo->next_bogus_packet_in = LLONG_MAX;
331 fo->bogus_packet_counter = rconn_packet_counter_create();
335 /* Registers the 'n' rconns in 'rconns' as connections to the controller for
336 * 'fo'. The caller must ensure that all of the rconns remain valid until 'fo'
337 * is destroyed or a new set is registered in a subsequent call.
339 * Takes ownership of the 'rconns' array, but not of the rconns that it points
340 * to (of which the caller retains ownership). */
342 fail_open_set_controllers(struct fail_open *fo,
343 struct rconn **rconns, size_t n)
345 free(fo->controllers);
346 fo->controllers = rconns;
347 fo->n_controllers = n;
352 fail_open_destroy(struct fail_open *fo)
355 fail_open_recover(fo);
356 free(fo->controllers);
357 /* We don't own the rconns behind fo->controllers. */
358 switch_status_unregister(fo->ss_cat);
359 rconn_packet_counter_destroy(fo->bogus_packet_counter);