2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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"
26 #include "ofp-actions.h"
30 #include "ofproto-provider.h"
32 #include "poll-loop.h"
38 VLOG_DEFINE_THIS_MODULE(fail_open);
43 * In fail-open mode, the switch detects when the controller cannot be
44 * contacted or when the controller is dropping switch connections because the
45 * switch does not pass its admission control policy. In those situations the
46 * switch sets up flows itself using the "normal" action.
48 * There is a little subtlety to implementation, to properly handle the case
49 * where the controller allows switch connections but drops them a few seconds
50 * later for admission control reasons. Because of this case, we don't want to
51 * just stop setting up flows when we connect to the controller: if we did,
52 * then new flow setup and existing flows would stop during the duration of
53 * connection to the controller, and thus the whole network would go down for
54 * that period of time.
56 * So, instead, we add some special cases when we are connected to a
57 * controller, but not yet sure that it has admitted us:
59 * - We set up flows immediately ourselves, but simultaneously send out an
60 * OFPT_PACKET_IN to the controller. We put a special bogus buffer-id in
61 * these OFPT_PACKET_IN messages so that duplicate packets don't get sent
62 * out to the network when the controller replies.
64 * - We also send out OFPT_PACKET_IN messages for totally bogus packets
65 * every so often, in case no real new flows are arriving in the network.
67 * - We don't flush the flow table at the time we connect, because this
68 * could cause network stuttering in a switch with lots of flows or very
69 * high-bandwidth flows by suddenly throwing lots of packets down to
74 struct ofproto *ofproto;
75 struct connmgr *connmgr;
76 int last_disconn_secs;
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 (!connmgr_has_controllers(fo->connmgr)) {
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 return connmgr_get_max_probe_interval(fo->connmgr) * 3;
109 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
111 fail_open_is_active(const struct fail_open *fo)
113 return fo->last_disconn_secs != 0;
117 send_bogus_packet_ins(struct fail_open *fo)
119 struct ofputil_packet_in pin;
120 uint8_t mac[ETH_ADDR_LEN];
123 ofpbuf_init(&b, 128);
124 eth_addr_nicira_random(mac);
125 compose_rarp(&b, mac);
127 memset(&pin, 0, sizeof pin);
129 pin.packet_len = b.size;
130 pin.reason = OFPR_NO_MATCH;
131 pin.send_len = b.size;
132 pin.fmd.in_port = OFPP_LOCAL;
133 connmgr_send_packet_in(fo->connmgr, &pin);
138 /* Enter fail-open mode if we should be in it. */
140 fail_open_run(struct fail_open *fo)
142 int disconn_secs = connmgr_failure_duration(fo->connmgr);
144 /* Enter fail-open mode if 'fo' is not in it but should be. */
145 if (disconn_secs >= trigger_duration(fo)) {
146 if (!fail_open_is_active(fo)) {
147 VLOG_WARN("Could not connect to controller (or switch failed "
148 "controller's post-connection admission control "
149 "policy) for %d seconds, failing open", disconn_secs);
150 fo->last_disconn_secs = disconn_secs;
152 /* Flush all OpenFlow and datapath flows. We will set up our
153 * fail-open rule from fail_open_flushed() when
154 * ofproto_flush_flows() calls back to us. */
155 ofproto_flush_flows(fo->ofproto);
156 } else if (disconn_secs > fo->last_disconn_secs + 60) {
157 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
158 "from controller", disconn_secs);
159 fo->last_disconn_secs = disconn_secs;
163 /* Schedule a bogus packet-in if we're connected and in fail-open. */
164 if (fail_open_is_active(fo)) {
165 if (connmgr_is_any_controller_connected(fo->connmgr)) {
166 bool expired = time_msec() >= fo->next_bogus_packet_in;
168 send_bogus_packet_ins(fo);
170 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
171 fo->next_bogus_packet_in = time_msec() + 2000;
174 fo->next_bogus_packet_in = LLONG_MAX;
180 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
181 * controller, exits fail open mode. */
183 fail_open_maybe_recover(struct fail_open *fo)
185 if (fail_open_is_active(fo)
186 && connmgr_is_any_controller_admitted(fo->connmgr)) {
187 fail_open_recover(fo);
192 fail_open_recover(struct fail_open *fo)
196 VLOG_WARN("No longer in fail-open mode");
197 fo->last_disconn_secs = 0;
198 fo->next_bogus_packet_in = LLONG_MAX;
200 match_init_catchall(&match);
201 ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
205 fail_open_wait(struct fail_open *fo)
207 if (fo->next_bogus_packet_in != LLONG_MAX) {
208 poll_timer_wait_until(fo->next_bogus_packet_in);
213 fail_open_flushed(struct fail_open *fo)
215 int disconn_secs = connmgr_failure_duration(fo->connmgr);
216 bool open = disconn_secs >= trigger_duration(fo);
218 struct ofpbuf ofpacts;
221 /* Set up a flow that matches every packet and directs them to
223 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
224 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
225 ofpact_pad(&ofpacts);
227 match_init_catchall(&match);
228 ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
229 ofpacts.data, ofpacts.size);
231 ofpbuf_uninit(&ofpacts);
235 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
237 fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
239 struct fail_open *fo = xmalloc(sizeof *fo);
240 fo->ofproto = ofproto;
242 fo->last_disconn_secs = 0;
243 fo->next_bogus_packet_in = LLONG_MAX;
244 fo->bogus_packet_counter = rconn_packet_counter_create();
250 fail_open_destroy(struct fail_open *fo)
253 if (fail_open_is_active(fo)) {
254 fail_open_recover(fo);
256 /* We don't own fo->connmgr. */
257 rconn_packet_counter_destroy(fo->bogus_packet_counter);