2 * Copyright (c) 2008, 2009 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 caseswhen we are connected to a controller,
53 * 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 *controller;
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 /* Returns true if 'fo' should be in fail-open mode, otherwise false. */
81 should_fail_open(const struct fail_open *fo)
83 return rconn_failure_duration(fo->controller) >= fo->trigger_duration;
86 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
88 fail_open_is_active(const struct fail_open *fo)
90 return fo->last_disconn_secs != 0;
94 send_bogus_packet_in(struct fail_open *fo)
96 uint8_t mac[ETH_ADDR_LEN];
100 /* Compose ofp_packet_in. */
101 ofpbuf_init(&b, 128);
102 eth_addr_random(mac);
103 compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
104 opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
108 rconn_send_with_limit(fo->controller, opi, fo->bogus_packet_counter, 1);
111 /* Enter fail-open mode if we should be in it. Handle reconnecting to a
112 * controller from fail-open mode. */
114 fail_open_run(struct fail_open *fo)
116 /* Enter fail-open mode if 'fo' is not in it but should be. */
117 if (should_fail_open(fo)) {
118 int disconn_secs = rconn_failure_duration(fo->controller);
119 if (!fail_open_is_active(fo)) {
120 VLOG_WARN("Could not connect to controller (or switch failed "
121 "controller's post-connection admission control "
122 "policy) for %d seconds, failing open", disconn_secs);
123 fo->last_disconn_secs = disconn_secs;
125 /* Flush all OpenFlow and datapath flows. We will set up our
126 * fail-open rule from fail_open_flushed() when
127 * ofproto_flush_flows() calls back to us. */
128 ofproto_flush_flows(fo->ofproto);
129 } else if (disconn_secs > fo->last_disconn_secs + 60) {
130 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
131 "from controller", disconn_secs);
132 fo->last_disconn_secs = disconn_secs;
136 /* Schedule a bogus packet-in if we're connected and in fail-open. */
137 if (fail_open_is_active(fo)) {
138 if (rconn_is_connected(fo->controller)) {
139 bool expired = time_msec() >= fo->next_bogus_packet_in;
141 send_bogus_packet_in(fo);
143 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
144 fo->next_bogus_packet_in = time_msec() + 2000;
147 fo->next_bogus_packet_in = LLONG_MAX;
153 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
154 * controller, exits fail open mode. */
156 fail_open_maybe_recover(struct fail_open *fo)
158 if (fail_open_is_active(fo) && rconn_is_admitted(fo->controller)) {
161 VLOG_WARN("No longer in fail-open mode");
162 fo->last_disconn_secs = 0;
163 fo->next_bogus_packet_in = LLONG_MAX;
165 memset(&flow, 0, sizeof flow);
166 ofproto_delete_flow(fo->ofproto, &flow, OFPFW_ALL, FAIL_OPEN_PRIORITY);
171 fail_open_wait(struct fail_open *fo)
173 if (fo->next_bogus_packet_in != LLONG_MAX) {
174 poll_timer_wait(fo->next_bogus_packet_in - time_msec());
179 fail_open_flushed(struct fail_open *fo)
181 int disconn_secs = rconn_failure_duration(fo->controller);
182 bool open = disconn_secs >= fo->trigger_duration;
184 union ofp_action action;
187 /* Set up a flow that matches every packet and directs them to
189 memset(&action, 0, sizeof action);
190 action.type = htons(OFPAT_OUTPUT);
191 action.output.len = htons(sizeof action);
192 action.output.port = htons(OFPP_NORMAL);
193 memset(&flow, 0, sizeof flow);
194 ofproto_add_flow(fo->ofproto, &flow, OFPFW_ALL, FAIL_OPEN_PRIORITY,
200 fail_open_status_cb(struct status_reply *sr, void *fo_)
202 struct fail_open *fo = fo_;
203 int cur_duration = rconn_failure_duration(fo->controller);
205 status_reply_put(sr, "trigger-duration=%d", fo->trigger_duration);
206 status_reply_put(sr, "current-duration=%d", cur_duration);
207 status_reply_put(sr, "triggered=%s",
208 cur_duration >= fo->trigger_duration ? "true" : "false");
212 fail_open_create(struct ofproto *ofproto,
213 int trigger_duration, struct switch_status *switch_status,
214 struct rconn *controller)
216 struct fail_open *fo = xmalloc(sizeof *fo);
217 fo->ofproto = ofproto;
218 fo->controller = controller;
219 fo->trigger_duration = trigger_duration;
220 fo->last_disconn_secs = 0;
221 fo->ss_cat = switch_status_register(switch_status, "fail-open",
222 fail_open_status_cb, fo);
223 fo->next_bogus_packet_in = LLONG_MAX;
224 fo->bogus_packet_counter = rconn_packet_counter_create();
229 fail_open_set_trigger_duration(struct fail_open *fo, int trigger_duration)
231 fo->trigger_duration = trigger_duration;
235 fail_open_destroy(struct fail_open *fo)
238 /* We don't own fo->controller. */
239 switch_status_unregister(fo->ss_cat);
240 rconn_packet_counter_destroy(fo->bogus_packet_counter);