Catalli's threaded switch
[sliver-openvswitch.git] / ofproto / fail-open.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "fail-open.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include "flow.h"
22 #include "mac-learning.h"
23 #include "odp-util.h"
24 #include "ofp-util.h"
25 #include "ofpbuf.h"
26 #include "ofproto.h"
27 #include "pktbuf.h"
28 #include "poll-loop.h"
29 #include "rconn.h"
30 #include "status.h"
31 #include "timeval.h"
32 #include "vconn.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(fail_open)
36
37 /*
38  * Fail-open mode.
39  *
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.
44  *
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.
52  *
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:
55  *
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.
60  *
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.
63  *
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
67  *       userspace.
68  */
69
70 struct fail_open {
71     struct ofproto *ofproto;
72     struct rconn **controllers;
73     size_t n_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;
78 };
79
80 static void fail_open_recover(struct fail_open *);
81
82 /* Returns the number of seconds of disconnection after which fail-open mode
83  * should activate. */
84 static int
85 trigger_duration(const struct fail_open *fo)
86 {
87     if (!fo->n_controllers) {
88         /* Shouldn't ever arrive here, but if we do, never fail open. */
89         return INT_MAX;
90     } else {
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:
94          *
95          *  - The first interval is the idle time before sending an inactivity
96          *    probe.
97          *
98          *  - The second interval is the time allowed for a response to the
99          *    inactivity probe.
100          *
101          *  - The third interval is the time allowed to reconnect after no
102          *    response is received.
103          */
104         int max_probe_interval;
105         size_t i;
106
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);
111         }
112         return max_probe_interval * 3;
113     }
114 }
115
116 /* Returns the number of seconds for which all controllers have been
117  * disconnected.  */
118 static int
119 failure_duration(const struct fail_open *fo)
120 {
121     int min_failure_duration;
122     size_t i;
123
124     if (!fo->n_controllers) {
125         return 0;
126     }
127
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);
132     }
133     return min_failure_duration;
134 }
135
136 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
137 bool
138 fail_open_is_active(const struct fail_open *fo)
139 {
140     return fo->last_disconn_secs != 0;
141 }
142
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. */
146 static bool
147 any_controller_is_connected(const struct fail_open *fo)
148 {
149     size_t i;
150
151     for (i = 0; i < fo->n_controllers; i++) {
152         if (rconn_is_connected(fo->controllers[i])) {
153             return true;
154         }
155     }
156     return false;
157 }
158
159 /* Returns true if at least one controller is believed to have authenticated
160  * and accepted this switch, false otherwise. */
161 static bool
162 any_controller_is_admitted(const struct fail_open *fo)
163 {
164     size_t i;
165
166     for (i = 0; i < fo->n_controllers; i++) {
167         if (rconn_is_admitted(fo->controllers[i])) {
168             return true;
169         }
170     }
171     return false;
172 }
173
174 static void
175 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
176 {
177     uint8_t mac[ETH_ADDR_LEN];
178     struct ofpbuf *opi;
179     struct ofpbuf b;
180
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);
186     ofpbuf_uninit(&b);
187
188     /* Send. */
189     rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
190 }
191
192 static void
193 send_bogus_packet_ins(struct fail_open *fo)
194 {
195     size_t i;
196
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]);
200         }
201     }
202 }
203
204 /* Enter fail-open mode if we should be in it. */
205 void
206 fail_open_run(struct fail_open *fo)
207 {
208     int disconn_secs = failure_duration(fo);
209
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;
217
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;
226         }
227     }
228
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;
233             if (expired) {
234                 send_bogus_packet_ins(fo);
235             }
236             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
237                 fo->next_bogus_packet_in = time_msec() + 2000;
238             }
239         } else {
240             fo->next_bogus_packet_in = LLONG_MAX;
241         }
242     }
243
244 }
245
246 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
247  * controller, exits fail open mode. */
248 void
249 fail_open_maybe_recover(struct fail_open *fo)
250 {
251     if (any_controller_is_admitted(fo)) {
252         fail_open_recover(fo);
253     }
254 }
255
256 static void
257 fail_open_recover(struct fail_open *fo)
258 {
259     if (fail_open_is_active(fo)) {
260         flow_t flow;
261
262         VLOG_WARN("No longer in fail-open mode");
263         fo->last_disconn_secs = 0;
264         fo->next_bogus_packet_in = LLONG_MAX;
265
266         memset(&flow, 0, sizeof flow);
267         ofproto_delete_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY);
268     }
269 }
270
271 void
272 fail_open_wait(struct fail_open *fo)
273 {
274     if (fo->next_bogus_packet_in != LLONG_MAX) {
275         poll_timer_wait_until(fo->next_bogus_packet_in);
276     }
277 }
278
279 void
280 fail_open_flushed(struct fail_open *fo)
281 {
282     int disconn_secs = failure_duration(fo);
283     bool open = disconn_secs >= trigger_duration(fo);
284     if (open) {
285         union ofp_action action;
286         flow_t flow;
287
288         /* Set up a flow that matches every packet and directs them to
289          * OFPP_NORMAL. */
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,
296                          &action, 1, 0);
297     }
298 }
299
300 static void
301 fail_open_status_cb(struct status_reply *sr, void *fo_)
302 {
303     struct fail_open *fo = fo_;
304     int cur_duration = failure_duration(fo);
305     int trigger = trigger_duration(fo);
306
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");
311 }
312
313 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
314  * status with 'switch_status'.
315  *
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.) */
319 struct fail_open *
320 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
321 {
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();
331     return fo;
332 }
333
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.
337  *
338  * Takes ownership of the 'rconns' array, but not of the rconns that it points
339  * to (of which the caller retains ownership). */
340 void
341 fail_open_set_controllers(struct fail_open *fo,
342                           struct rconn **rconns, size_t n)
343 {
344     free(fo->controllers);
345     fo->controllers = rconns;
346     fo->n_controllers = n;
347 }
348
349 /* Destroys 'fo'. */
350 void
351 fail_open_destroy(struct fail_open *fo)
352 {
353     if (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);
359         free(fo);
360     }
361 }