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