vswitchd: Use wdp functions instead of xfif ones for enumerating bridges.
[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 "ofp-util.h"
24 #include "ofpbuf.h"
25 #include "ofproto.h"
26 #include "pktbuf.h"
27 #include "poll-loop.h"
28 #include "rconn.h"
29 #include "status.h"
30 #include "timeval.h"
31 #include "vconn.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(fail_open)
35
36 /*
37  * Fail-open mode.
38  *
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.
43  *
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.
51  *
52  * So, instead, we add some special cases when we are connected to a
53  * controller, but not yet sure that it has admitted us:
54  *
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.
59  *
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.
62  *
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
66  *       userspace.
67  */
68
69 struct fail_open {
70     struct ofproto *ofproto;
71     struct rconn **controllers;
72     size_t n_controllers;
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;
77 };
78
79 static void fail_open_recover(struct fail_open *);
80
81 /* Returns the number of seconds of disconnection after which fail-open mode
82  * should activate. */
83 static int
84 trigger_duration(const struct fail_open *fo)
85 {
86     if (!fo->n_controllers) {
87         /* Shouldn't ever arrive here, but if we do, never fail open. */
88         return INT_MAX;
89     } else {
90         /* Otherwise, every controller must have a chance to send an
91          * inactivity probe and reconnect before we fail open, so take the
92          * maximum probe interval and multiply by 3:
93          *
94          *  - The first interval is the idle time before sending an inactivity
95          *    probe.
96          *
97          *  - The second interval is the time allowed for a response to the
98          *    inactivity probe.
99          *
100          *  - The third interval is the time allowed to reconnect after no
101          *    response is received.
102          */
103         int max_probe_interval;
104         size_t i;
105
106         max_probe_interval = 0;
107         for (i = 0; i < fo->n_controllers; i++) {
108             int probe_interval = rconn_get_probe_interval(fo->controllers[i]);
109             max_probe_interval = MAX(max_probe_interval, probe_interval);
110         }
111         return max_probe_interval * 3;
112     }
113 }
114
115 /* Returns the number of seconds for which all controllers have been
116  * disconnected.  */
117 static int
118 failure_duration(const struct fail_open *fo)
119 {
120     int min_failure_duration;
121     size_t i;
122
123     if (!fo->n_controllers) {
124         return 0;
125     }
126
127     min_failure_duration = INT_MAX;
128     for (i = 0; i < fo->n_controllers; i++) {
129         int failure_duration = rconn_failure_duration(fo->controllers[i]);
130         min_failure_duration = MIN(min_failure_duration, failure_duration);
131     }
132     return min_failure_duration;
133 }
134
135 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
136 bool
137 fail_open_is_active(const struct fail_open *fo)
138 {
139     return fo->last_disconn_secs != 0;
140 }
141
142 /* Returns true if at least one controller is connected (regardless of whether
143  * those controllers are believed to have authenticated and accepted this
144  * switch), false if none of them are connected. */
145 static bool
146 any_controller_is_connected(const struct fail_open *fo)
147 {
148     size_t i;
149
150     for (i = 0; i < fo->n_controllers; i++) {
151         if (rconn_is_connected(fo->controllers[i])) {
152             return true;
153         }
154     }
155     return false;
156 }
157
158 /* Returns true if at least one controller is believed to have authenticated
159  * and accepted this switch, false otherwise. */
160 static bool
161 any_controller_is_admitted(const struct fail_open *fo)
162 {
163     size_t i;
164
165     for (i = 0; i < fo->n_controllers; i++) {
166         if (rconn_is_admitted(fo->controllers[i])) {
167             return true;
168         }
169     }
170     return false;
171 }
172
173 static void
174 send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn)
175 {
176     uint8_t mac[ETH_ADDR_LEN];
177     struct ofpbuf *opi;
178     struct ofpbuf b;
179
180     /* Compose ofp_packet_in. */
181     ofpbuf_init(&b, 128);
182     eth_addr_nicira_random(mac);
183     compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
184     opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
185     ofpbuf_uninit(&b);
186
187     /* Send. */
188     rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1);
189 }
190
191 static void
192 send_bogus_packet_ins(struct fail_open *fo)
193 {
194     size_t i;
195
196     for (i = 0; i < fo->n_controllers; i++) {
197         if (rconn_is_connected(fo->controllers[i])) {
198             send_bogus_packet_in(fo, fo->controllers[i]);
199         }
200     }
201 }
202
203 /* Enter fail-open mode if we should be in it. */
204 void
205 fail_open_run(struct fail_open *fo)
206 {
207     int disconn_secs = failure_duration(fo);
208
209     /* Enter fail-open mode if 'fo' is not in it but should be.  */
210     if (disconn_secs >= trigger_duration(fo)) {
211         if (!fail_open_is_active(fo)) {
212             VLOG_WARN("Could not connect to controller (or switch failed "
213                       "controller's post-connection admission control "
214                       "policy) for %d seconds, failing open", disconn_secs);
215             fo->last_disconn_secs = disconn_secs;
216
217             /* Flush all OpenFlow and datapath flows.  We will set up our
218              * fail-open rule from fail_open_flushed() when
219              * ofproto_flush_flows() calls back to us. */
220             ofproto_flush_flows(fo->ofproto);
221         } else if (disconn_secs > fo->last_disconn_secs + 60) {
222             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
223                       "from controller", disconn_secs);
224             fo->last_disconn_secs = disconn_secs;
225         }
226     }
227
228     /* Schedule a bogus packet-in if we're connected and in fail-open. */
229     if (fail_open_is_active(fo)) {
230         if (any_controller_is_connected(fo)) {
231             bool expired = time_msec() >= fo->next_bogus_packet_in;
232             if (expired) {
233                 send_bogus_packet_ins(fo);
234             }
235             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
236                 fo->next_bogus_packet_in = time_msec() + 2000;
237             }
238         } else {
239             fo->next_bogus_packet_in = LLONG_MAX;
240         }
241     }
242
243 }
244
245 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
246  * controller, exits fail open mode. */
247 void
248 fail_open_maybe_recover(struct fail_open *fo)
249 {
250     if (any_controller_is_admitted(fo)) {
251         fail_open_recover(fo);
252     }
253 }
254
255 static void
256 fail_open_recover(struct fail_open *fo)
257 {
258     if (fail_open_is_active(fo)) {
259         flow_t flow;
260
261         VLOG_WARN("No longer in fail-open mode");
262         fo->last_disconn_secs = 0;
263         fo->next_bogus_packet_in = LLONG_MAX;
264
265         memset(&flow, 0, sizeof flow);
266         flow.wildcards = OVSFW_ALL;
267         flow.priority = FAIL_OPEN_PRIORITY;
268         ofproto_delete_flow(fo->ofproto, &flow);
269     }
270 }
271
272 void
273 fail_open_wait(struct fail_open *fo)
274 {
275     if (fo->next_bogus_packet_in != LLONG_MAX) {
276         poll_timer_wait_until(fo->next_bogus_packet_in);
277     }
278 }
279
280 void
281 fail_open_flushed(struct fail_open *fo)
282 {
283     int disconn_secs = failure_duration(fo);
284     bool open = disconn_secs >= trigger_duration(fo);
285     if (open) {
286         union ofp_action action;
287         flow_t flow;
288
289         /* Set up a flow that matches every packet and directs them to
290          * OFPP_NORMAL. */
291         memset(&action, 0, sizeof action);
292         action.type = htons(OFPAT_OUTPUT);
293         action.output.len = htons(sizeof action);
294         action.output.port = htons(OFPP_NORMAL);
295         memset(&flow, 0, sizeof flow);
296         flow.wildcards = OVSFW_ALL;
297         flow.priority = FAIL_OPEN_PRIORITY;
298         ofproto_add_flow(fo->ofproto, &flow, &action, 1, 0);
299     }
300 }
301
302 static void
303 fail_open_status_cb(struct status_reply *sr, void *fo_)
304 {
305     struct fail_open *fo = fo_;
306     int cur_duration = failure_duration(fo);
307     int trigger = trigger_duration(fo);
308
309     status_reply_put(sr, "trigger-duration=%d", trigger);
310     status_reply_put(sr, "current-duration=%d", cur_duration);
311     status_reply_put(sr, "triggered=%s",
312                      cur_duration >= trigger ? "true" : "false");
313 }
314
315 /* Creates and returns a new struct fail_open for 'ofproto', registering switch
316  * status with 'switch_status'.
317  *
318  * The caller should register its set of controllers with
319  * fail_open_set_controllers().  (There should be at least one controller,
320  * otherwise there isn't any point in having the struct fail_open around.) */
321 struct fail_open *
322 fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status)
323 {
324     struct fail_open *fo = xmalloc(sizeof *fo);
325     fo->ofproto = ofproto;
326     fo->controllers = NULL;
327     fo->n_controllers = 0;
328     fo->last_disconn_secs = 0;
329     fo->ss_cat = switch_status_register(switch_status, "fail-open",
330                                         fail_open_status_cb, fo);
331     fo->next_bogus_packet_in = LLONG_MAX;
332     fo->bogus_packet_counter = rconn_packet_counter_create();
333     return fo;
334 }
335
336 /* Registers the 'n' rconns in 'rconns' as connections to the controller for
337  * 'fo'.  The caller must ensure that all of the rconns remain valid until 'fo'
338  * is destroyed or a new set is registered in a subsequent call.
339  *
340  * Takes ownership of the 'rconns' array, but not of the rconns that it points
341  * to (of which the caller retains ownership). */
342 void
343 fail_open_set_controllers(struct fail_open *fo,
344                           struct rconn **rconns, size_t n)
345 {
346     free(fo->controllers);
347     fo->controllers = rconns;
348     fo->n_controllers = n;
349 }
350
351 /* Destroys 'fo'. */
352 void
353 fail_open_destroy(struct fail_open *fo)
354 {
355     if (fo) {
356         fail_open_recover(fo);
357         free(fo->controllers);
358         /* We don't own the rconns behind fo->controllers. */
359         switch_status_unregister(fo->ss_cat);
360         rconn_packet_counter_destroy(fo->bogus_packet_counter);
361         free(fo);
362     }
363 }