Merge "master" branch into "wdp".
[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 "ofpbuf.h"
24 #include "ofproto.h"
25 #include "pktbuf.h"
26 #include "poll-loop.h"
27 #include "rconn.h"
28 #include "status.h"
29 #include "timeval.h"
30 #include "vconn.h"
31
32 #define THIS_MODULE VLM_fail_open
33 #include "vlog.h"
34
35 /*
36  * Fail-open mode.
37  *
38  * In fail-open mode, the switch detects when the controller cannot be
39  * contacted or when the controller is dropping switch connections because the
40  * switch does not pass its admission control policy.  In those situations the
41  * switch sets up flows itself using the "normal" action.
42  *
43  * There is a little subtlety to implementation, to properly handle the case
44  * where the controller allows switch connections but drops them a few seconds
45  * later for admission control reasons.  Because of this case, we don't want to
46  * just stop setting up flows when we connect to the controller: if we did,
47  * then new flow setup and existing flows would stop during the duration of
48  * connection to the controller, and thus the whole network would go down for
49  * that period of time.
50  *
51  * So, instead, we add some special cases when we are connected to a
52  * controller, but not yet sure that it has admitted us:
53  *
54  *     - We set up flows immediately ourselves, but simultaneously send out an
55  *       OFPT_PACKET_IN to the controller.  We put a special bogus buffer-id in
56  *       these OFPT_PACKET_IN messages so that duplicate packets don't get sent
57  *       out to the network when the controller replies.
58  *
59  *     - We also send out OFPT_PACKET_IN messages for totally bogus packets
60  *       every so often, in case no real new flows are arriving in the network.
61  *
62  *     - We don't flush the flow table at the time we connect, because this
63  *       could cause network stuttering in a switch with lots of flows or very
64  *       high-bandwidth flows by suddenly throwing lots of packets down to
65  *       userspace.
66  */
67
68 struct fail_open {
69     struct ofproto *ofproto;
70     struct rconn **controllers;
71     size_t n_controllers;
72     int last_disconn_secs;
73     struct status_category *ss_cat;
74     long long int next_bogus_packet_in;
75     struct rconn_packet_counter *bogus_packet_counter;
76 };
77
78 static void fail_open_recover(struct fail_open *);
79
80 /* Returns the number of seconds of disconnection after which fail-open mode
81  * should activate. */
82 static int
83 trigger_duration(const struct fail_open *fo)
84 {
85     if (!fo->n_controllers) {
86         /* Shouldn't ever arrive here, but if we do, never fail open. */
87         return INT_MAX;
88     } else {
89         /* Otherwise, every controller must have a chance to send an
90          * inactivity probe and reconnect before we fail open, so take the
91          * maximum probe interval and multiply by 3:
92          *
93          *  - The first interval is the idle time before sending an inactivity
94          *    probe.
95          *
96          *  - The second interval is the time allowed for a response to the
97          *    inactivity probe.
98          *
99          *  - The third interval is the time allowed to reconnect after no
100          *    response is received.
101          */
102         int max_probe_interval;
103         size_t i;
104
105         max_probe_interval = 0;
106         for (i = 0; i < fo->n_controllers; i++) {
107             int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
108             max_probe_interval = MAX(max_probe_interval, probe_interval);
109         }
110         return max_probe_interval * 3;
111     }
112 }
113
114 /* Returns the number of seconds for which all controllers have been
115  * disconnected.  */
116 static int
117 failure_duration(const struct fail_open *fo)
118 {
119     int min_failure_duration;
120     size_t i;
121
122     if (!fo->n_controllers) {
123         return 0;
124     }
125
126     min_failure_duration = INT_MAX;
127     for (i = 0; i < fo->n_controllers; i++) {
128         int failure_duration = rconn_failure_duration(fo->controllers[i]);
129         min_failure_duration = MIN(min_failure_duration, failure_duration);
130     }
131     return min_failure_duration;
132 }
133
134 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
135 bool
136 fail_open_is_active(const struct fail_open *fo)
137 {
138     return fo->last_disconn_secs != 0;
139 }
140
141 /* Returns true if at least one controller is connected (regardless of whether
142  * those controllers are believed to have authenticated and accepted this
143  * switch), false if none of them are connected. */
144 static bool
145 any_controller_is_connected(const struct fail_open *fo)
146 {
147     size_t i;
148
149     for (i = 0; i < fo->n_controllers; i++) {
150         if (rconn_is_connected(fo->controllers[i])) {
151             return true;
152         }
153     }
154     return false;
155 }
156
157 /* Returns true if at least one controller is believed to have authenticated
158  * and accepted this switch, false otherwise. */
159 static bool
160 any_controller_is_admitted(const struct fail_open *fo)
161 {
162     size_t i;
163
164     for (i = 0; i < fo->n_controllers; i++) {
165         if (rconn_is_admitted(fo->controllers[i])) {
166             return true;
167         }
168     }
169     return false;
170 }
171
172 static void
173 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
174 {
175     uint8_t mac[ETH_ADDR_LEN];
176     struct ofpbuf *opi;
177     struct ofpbuf b;
178
179     /* Compose ofp_packet_in. */
180     ofpbuf_init(&b, 128);
181     eth_addr_nicira_random(mac);
182     compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
183     opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
184     ofpbuf_uninit(&b);
185
186     /* Send. */
187     rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
188 }
189
190 static void
191 send_bogus_packet_ins(struct fail_open *fo)
192 {
193     size_t i;
194
195     for (i = 0; i < fo->n_controllers; i++) {
196         if (rconn_is_connected(fo->controllers[i])) {
197             send_bogus_packet_in(fo, fo->controllers[i]);
198         }
199     }
200 }
201
202 /* Enter fail-open mode if we should be in it. */
203 void
204 fail_open_run(struct fail_open *fo)
205 {
206     int disconn_secs = failure_duration(fo);
207
208     /* Enter fail-open mode if 'fo' is not in it but should be.  */
209     if (disconn_secs >= trigger_duration(fo)) {
210         if (!fail_open_is_active(fo)) {
211             VLOG_WARN("Could not connect to controller (or switch failed "
212                       "controller's post-connection admission control "
213                       "policy) for %d seconds, failing open", disconn_secs);
214             fo->last_disconn_secs = disconn_secs;
215
216             /* Flush all OpenFlow and datapath flows.  We will set up our
217              * fail-open rule from fail_open_flushed() when
218              * ofproto_flush_flows() calls back to us. */
219             ofproto_flush_flows(fo->ofproto);
220         } else if (disconn_secs > fo->last_disconn_secs + 60) {
221             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
222                       "from controller", disconn_secs);
223             fo->last_disconn_secs = disconn_secs;
224         }
225     }
226
227     /* Schedule a bogus packet-in if we're connected and in fail-open. */
228     if (fail_open_is_active(fo)) {
229         if (any_controller_is_connected(fo)) {
230             bool expired = time_msec() >= fo->next_bogus_packet_in;
231             if (expired) {
232                 send_bogus_packet_ins(fo);
233             }
234             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
235                 fo->next_bogus_packet_in = time_msec() + 2000;
236             }
237         } else {
238             fo->next_bogus_packet_in = LLONG_MAX;
239         }
240     }
241
242 }
243
244 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
245  * controller, exits fail open mode. */
246 void
247 fail_open_maybe_recover(struct fail_open *fo)
248 {
249     if (any_controller_is_admitted(fo)) {
250         fail_open_recover(fo);
251     }
252 }
253
254 static void
255 fail_open_recover(struct fail_open *fo)
256 {
257     if (fail_open_is_active(fo)) {
258         flow_t flow;
259
260         VLOG_WARN("No longer in fail-open mode");
261         fo->last_disconn_secs = 0;
262         fo->next_bogus_packet_in = LLONG_MAX;
263
264         memset(&flow, 0, sizeof flow);
265         flow.wildcards = OVSFW_ALL;
266         flow.priority = FAIL_OPEN_PRIORITY;
267         ofproto_delete_flow(fo->ofproto, &flow);
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         flow.wildcards = OVSFW_ALL;
296         flow.priority = FAIL_OPEN_PRIORITY;
297         ofproto_add_flow(fo->ofproto, &flow, &action, 1, 0);
298     }
299 }
300
301 static void
302 fail_open_status_cb(struct status_reply *sr, void *fo_)
303 {
304     struct fail_open *fo = fo_;
305     int cur_duration = failure_duration(fo);
306     int trigger = trigger_duration(fo);
307
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");
312 }
313
314 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
315  * status with 'switch_status'.
316  *
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.) */
320 struct fail_open *
321 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
322 {
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();
332     return fo;
333 }
334
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.
338  *
339  * Takes ownership of the 'rconns' array, but not of the rconns that it points
340  * to (of which the caller retains ownership). */
341 void
342 fail_open_set_controllers(struct fail_open *fo,
343                           struct rconn **rconns, size_t n)
344 {
345     free(fo->controllers);
346     fo->controllers = rconns;
347     fo->n_controllers = n;
348 }
349
350 /* Destroys 'fo'. */
351 void
352 fail_open_destroy(struct fail_open *fo)
353 {
354     if (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);
360         free(fo);
361     }
362 }