75c7d1c8075baaaee3262ce1733b2b8e45102141
[cascardo/ovs.git] / ofproto / fail-open.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
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 "connmgr.h"
23 #include "flow.h"
24 #include "mac-learning.h"
25 #include "odp-util.h"
26 #include "ofp-actions.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "ofproto.h"
30 #include "ofproto-provider.h"
31 #include "pktbuf.h"
32 #include "poll-loop.h"
33 #include "rconn.h"
34 #include "timeval.h"
35 #include "openvswitch/vconn.h"
36 #include "openvswitch/vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(fail_open);
39
40 /*
41  * Fail-open mode.
42  *
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.
47  *
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.
55  *
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:
58  *
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.
63  *
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.
66  *
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
70  *       userspace.
71  */
72
73 struct fail_open {
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;
79     bool fail_open_active;
80 };
81
82 static void fail_open_recover(struct fail_open *);
83
84 /* Returns the number of seconds of disconnection after which fail-open mode
85  * should activate. */
86 static int
87 trigger_duration(const struct fail_open *fo)
88 {
89     if (!connmgr_has_controllers(fo->connmgr)) {
90         /* Shouldn't ever arrive here, but if we do, never fail open. */
91         return INT_MAX;
92     } else {
93         /* Otherwise, every controller must have a chance to send an
94          * inactivity probe and reconnect before we fail open, so take the
95          * maximum probe interval and multiply by 3:
96          *
97          *  - The first interval is the idle time before sending an inactivity
98          *    probe.
99          *
100          *  - The second interval is the time allowed for a response to the
101          *    inactivity probe.
102          *
103          *  - The third interval is the time allowed to reconnect after no
104          *    response is received.
105          */
106         return connmgr_get_max_probe_interval(fo->connmgr) * 3;
107     }
108 }
109
110 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
111 bool
112 fail_open_is_active(const struct fail_open *fo)
113 {
114     return fo->last_disconn_secs != 0;
115 }
116
117 static void
118 send_bogus_packet_ins(struct fail_open *fo)
119 {
120     struct ofproto_packet_in pin;
121     uint8_t mac[ETH_ADDR_LEN];
122     struct ofpbuf b;
123
124     ofpbuf_init(&b, 128);
125     eth_addr_nicira_random(mac);
126     compose_rarp(&b, mac);
127
128     memset(&pin, 0, sizeof pin);
129     pin.up.packet = ofpbuf_data(&b);
130     pin.up.packet_len = ofpbuf_size(&b);
131     pin.up.reason = OFPR_NO_MATCH;
132     pin.up.fmd.in_port = OFPP_LOCAL;
133     pin.send_len = ofpbuf_size(&b);
134     pin.miss_type = OFPROTO_PACKET_IN_NO_MISS;
135     connmgr_send_packet_in(fo->connmgr, &pin);
136
137     ofpbuf_uninit(&b);
138 }
139
140 /* Enter fail-open mode if we should be in it. */
141 void
142 fail_open_run(struct fail_open *fo)
143 {
144     int disconn_secs = connmgr_failure_duration(fo->connmgr);
145
146     /* Enter fail-open mode if 'fo' is not in it but should be.  */
147     if (disconn_secs >= trigger_duration(fo)) {
148         if (!fail_open_is_active(fo)) {
149             VLOG_WARN("Could not connect to controller (or switch failed "
150                       "controller's post-connection admission control "
151                       "policy) for %d seconds, failing open", disconn_secs);
152             fo->last_disconn_secs = disconn_secs;
153
154             /* Flush all OpenFlow and datapath flows.  We will set up our
155              * fail-open rule from fail_open_flushed() when
156              * ofproto_flush_flows() calls back to us. */
157             ofproto_flush_flows(fo->ofproto);
158         } else if (disconn_secs > fo->last_disconn_secs + 60) {
159             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
160                       "from controller", disconn_secs);
161             fo->last_disconn_secs = disconn_secs;
162         }
163     }
164
165     /* Schedule a bogus packet-in if we're connected and in fail-open. */
166     if (fail_open_is_active(fo)) {
167         if (connmgr_is_any_controller_connected(fo->connmgr)) {
168             bool expired = time_msec() >= fo->next_bogus_packet_in;
169             if (expired) {
170                 send_bogus_packet_ins(fo);
171             }
172             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
173                 fo->next_bogus_packet_in = time_msec() + 2000;
174             }
175         } else {
176             fo->next_bogus_packet_in = LLONG_MAX;
177         }
178     }
179
180 }
181
182 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
183  * controller, exits fail open mode. */
184 void
185 fail_open_maybe_recover(struct fail_open *fo)
186     OVS_EXCLUDED(ofproto_mutex)
187 {
188     if (fail_open_is_active(fo)
189         && connmgr_is_any_controller_admitted(fo->connmgr)) {
190         fail_open_recover(fo);
191     }
192 }
193
194 static void
195 fail_open_recover(struct fail_open *fo)
196     OVS_EXCLUDED(ofproto_mutex)
197 {
198     struct match match;
199
200     VLOG_WARN("No longer in fail-open mode");
201     fo->last_disconn_secs = 0;
202     fo->next_bogus_packet_in = LLONG_MAX;
203
204     match_init_catchall(&match);
205     ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
206 }
207
208 void
209 fail_open_wait(struct fail_open *fo)
210 {
211     if (fo->next_bogus_packet_in != LLONG_MAX) {
212         poll_timer_wait_until(fo->next_bogus_packet_in);
213     }
214 }
215
216 void
217 fail_open_flushed(struct fail_open *fo)
218     OVS_EXCLUDED(ofproto_mutex)
219 {
220     int disconn_secs = connmgr_failure_duration(fo->connmgr);
221     bool open = disconn_secs >= trigger_duration(fo);
222     if (open) {
223         struct ofpbuf ofpacts;
224         struct match match;
225
226         /* Set up a flow that matches every packet and directs them to
227          * OFPP_NORMAL. */
228         ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
229         ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
230         ofpact_pad(&ofpacts);
231
232         match_init_catchall(&match);
233         ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
234                          ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts));
235
236         ofpbuf_uninit(&ofpacts);
237     }
238     fo->fail_open_active = open;
239 }
240
241 /* Returns the number of fail-open rules currently installed in the flow
242  * table. */
243 int
244 fail_open_count_rules(const struct fail_open *fo)
245 {
246     return fo->fail_open_active != 0;
247 }
248
249 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
250 struct fail_open *
251 fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
252 {
253     struct fail_open *fo = xmalloc(sizeof *fo);
254     fo->ofproto = ofproto;
255     fo->connmgr = mgr;
256     fo->last_disconn_secs = 0;
257     fo->next_bogus_packet_in = LLONG_MAX;
258     fo->bogus_packet_counter = rconn_packet_counter_create();
259     fo->fail_open_active = false;
260     return fo;
261 }
262
263 /* Destroys 'fo'. */
264 void
265 fail_open_destroy(struct fail_open *fo)
266     OVS_EXCLUDED(ofproto_mutex)
267 {
268     if (fo) {
269         if (fail_open_is_active(fo)) {
270             fail_open_recover(fo);
271         }
272         /* We don't own fo->connmgr. */
273         rconn_packet_counter_destroy(fo->bogus_packet_counter);
274         free(fo);
275     }
276 }