2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
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"
24 #include "mac-learning.h"
27 #include "ofp-actions.h"
30 #include "ofproto-provider.h"
32 #include "dp-packet.h"
33 #include "poll-loop.h"
36 #include "openvswitch/vconn.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(fail_open);
44 * In fail-open mode, the switch detects when the controller cannot be
45 * contacted or when the controller is dropping switch connections because the
46 * switch does not pass its admission control policy. In those situations the
47 * switch sets up flows itself using the "normal" action.
49 * There is a little subtlety to implementation, to properly handle the case
50 * where the controller allows switch connections but drops them a few seconds
51 * later for admission control reasons. Because of this case, we don't want to
52 * just stop setting up flows when we connect to the controller: if we did,
53 * then new flow setup and existing flows would stop during the duration of
54 * connection to the controller, and thus the whole network would go down for
55 * that period of time.
57 * So, instead, we add some special cases when we are connected to a
58 * controller, but not yet sure that it has admitted us:
60 * - We set up flows immediately ourselves, but simultaneously send out an
61 * OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
62 * these OFPT_PACKET_IN messages so that duplicate packets don't get sent
63 * out to the network when the controller replies.
65 * - We also send out OFPT_PACKET_IN messages for totally bogus packets
66 * every so often, in case no real new flows are arriving in the network.
68 * - We don't flush the flow table at the time we connect, because this
69 * could cause network stuttering in a switch with lots of flows or very
70 * high-bandwidth flows by suddenly throwing lots of packets down to
75 struct ofproto *ofproto;
76 struct connmgr *connmgr;
77 int last_disconn_secs;
78 long long int next_bogus_packet_in;
79 struct rconn_packet_counter *bogus_packet_counter;
80 bool fail_open_active;
83 static void fail_open_recover(struct fail_open *);
85 /* Returns the number of seconds of disconnection after which fail-open mode
88 trigger_duration(const struct fail_open *fo)
90 if (!connmgr_has_controllers(fo->connmgr)) {
91 /* Shouldn't ever arrive here, but if we do, never fail open. */
94 /* Otherwise, every controller must have a chance to send an
95 * inactivity probe and reconnect before we fail open, so take the
96 * maximum probe interval and multiply by 3:
98 * - The first interval is the idle time before sending an inactivity
101 * - The second interval is the time allowed for a response to the
104 * - The third interval is the time allowed to reconnect after no
105 * response is received.
107 return connmgr_get_max_probe_interval(fo->connmgr) * 3;
111 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
113 fail_open_is_active(const struct fail_open *fo)
115 return fo->last_disconn_secs != 0;
119 send_bogus_packet_ins(struct fail_open *fo)
124 dp_packet_init(&b, 128);
125 eth_addr_nicira_random(&mac);
126 compose_rarp(&b, mac);
128 struct ofproto_async_msg am = {
129 .oam = OAM_PACKET_IN,
132 .packet = dp_packet_data(&b),
133 .len = dp_packet_size(&b),
134 .flow_metadata = MATCH_CATCHALL_INITIALIZER,
135 .flow_metadata.flow.in_port.ofp_port = OFPP_LOCAL,
136 .flow_metadata.wc.masks.in_port.ofp_port
137 = u16_to_ofp(UINT16_MAX),
138 .reason = OFPR_NO_MATCH,
139 .cookie = OVS_BE64_MAX,
141 .max_len = UINT16_MAX,
144 connmgr_send_async_msg(fo->connmgr, &am);
146 dp_packet_uninit(&b);
149 /* Enter fail-open mode if we should be in it. */
151 fail_open_run(struct fail_open *fo)
153 int disconn_secs = connmgr_failure_duration(fo->connmgr);
155 /* Enter fail-open mode if 'fo' is not in it but should be. */
156 if (disconn_secs >= trigger_duration(fo)) {
157 if (!fail_open_is_active(fo)) {
158 VLOG_WARN("Could not connect to controller (or switch failed "
159 "controller's post-connection admission control "
160 "policy) for %d seconds, failing open", disconn_secs);
161 fo->last_disconn_secs = disconn_secs;
163 /* Flush all OpenFlow and datapath flows. We will set up our
164 * fail-open rule from fail_open_flushed() when
165 * ofproto_flush_flows() calls back to us. */
166 ofproto_flush_flows(fo->ofproto);
167 } else if (disconn_secs > fo->last_disconn_secs + 60) {
168 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
169 "from controller", disconn_secs);
170 fo->last_disconn_secs = disconn_secs;
174 /* Schedule a bogus packet-in if we're connected and in fail-open. */
175 if (fail_open_is_active(fo)) {
176 if (connmgr_is_any_controller_connected(fo->connmgr)) {
177 bool expired = time_msec() >= fo->next_bogus_packet_in;
179 send_bogus_packet_ins(fo);
181 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
182 fo->next_bogus_packet_in = time_msec() + 2000;
185 fo->next_bogus_packet_in = LLONG_MAX;
191 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
192 * controller, exits fail open mode. */
194 fail_open_maybe_recover(struct fail_open *fo)
195 OVS_EXCLUDED(ofproto_mutex)
197 if (fail_open_is_active(fo)
198 && connmgr_is_any_controller_admitted(fo->connmgr)) {
199 fail_open_recover(fo);
204 fail_open_recover(struct fail_open *fo)
205 OVS_EXCLUDED(ofproto_mutex)
209 VLOG_WARN("No longer in fail-open mode");
210 fo->last_disconn_secs = 0;
211 fo->next_bogus_packet_in = LLONG_MAX;
213 match_init_catchall(&match);
214 ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
218 fail_open_wait(struct fail_open *fo)
220 if (fo->next_bogus_packet_in != LLONG_MAX) {
221 poll_timer_wait_until(fo->next_bogus_packet_in);
226 fail_open_flushed(struct fail_open *fo)
227 OVS_EXCLUDED(ofproto_mutex)
229 int disconn_secs = connmgr_failure_duration(fo->connmgr);
230 bool open = disconn_secs >= trigger_duration(fo);
232 struct ofpbuf ofpacts;
235 /* Set up a flow that matches every packet and directs them to
237 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
238 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
240 match_init_catchall(&match);
241 ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
242 ofpacts.data, ofpacts.size);
244 ofpbuf_uninit(&ofpacts);
246 fo->fail_open_active = open;
249 /* Returns the number of fail-open rules currently installed in the flow
252 fail_open_count_rules(const struct fail_open *fo)
254 return fo->fail_open_active != 0;
257 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
259 fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
261 struct fail_open *fo = xmalloc(sizeof *fo);
262 fo->ofproto = ofproto;
264 fo->last_disconn_secs = 0;
265 fo->next_bogus_packet_in = LLONG_MAX;
266 fo->bogus_packet_counter = rconn_packet_counter_create();
267 fo->fail_open_active = false;
273 fail_open_destroy(struct fail_open *fo)
274 OVS_EXCLUDED(ofproto_mutex)
277 if (fail_open_is_active(fo)) {
278 fail_open_recover(fo);
280 /* We don't own fo->connmgr. */
281 rconn_packet_counter_destroy(fo->bogus_packet_counter);