ofproto-dpif-xlate: Fix revalidation in execute_controller_action().
[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 "ofpbuf.h"
27 #include "ofp-actions.h"
28 #include "ofp-util.h"
29 #include "ofproto.h"
30 #include "ofproto-provider.h"
31 #include "pktbuf.h"
32 #include "dp-packet.h"
33 #include "poll-loop.h"
34 #include "rconn.h"
35 #include "timeval.h"
36 #include "openvswitch/vconn.h"
37 #include "openvswitch/vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(fail_open);
40
41 /*
42  * Fail-open mode.
43  *
44  * In fail-open mode, the switch detects when the controller cannot be
45  * contacted or when the controller is dropping switch connections because the
46  * switch does not pass its admission control policy.  In those situations the
47  * switch sets up flows itself using the "normal" action.
48  *
49  * There is a little subtlety to implementation, to properly handle the case
50  * where the controller allows switch connections but drops them a few seconds
51  * later for admission control reasons.  Because of this case, we don't want to
52  * just stop setting up flows when we connect to the controller: if we did,
53  * then new flow setup and existing flows would stop during the duration of
54  * connection to the controller, and thus the whole network would go down for
55  * that period of time.
56  *
57  * So, instead, we add some special cases when we are connected to a
58  * controller, but not yet sure that it has admitted us:
59  *
60  *     - We set up flows immediately ourselves, but simultaneously send out an
61  *       OFPT_PACKET_IN to the controller.  We put a special bogus buffer-id in
62  *       these OFPT_PACKET_IN messages so that duplicate packets don't get sent
63  *       out to the network when the controller replies.
64  *
65  *     - We also send out OFPT_PACKET_IN messages for totally bogus packets
66  *       every so often, in case no real new flows are arriving in the network.
67  *
68  *     - We don't flush the flow table at the time we connect, because this
69  *       could cause network stuttering in a switch with lots of flows or very
70  *       high-bandwidth flows by suddenly throwing lots of packets down to
71  *       userspace.
72  */
73
74 struct fail_open {
75     struct ofproto *ofproto;
76     struct connmgr *connmgr;
77     int last_disconn_secs;
78     long long int next_bogus_packet_in;
79     struct rconn_packet_counter *bogus_packet_counter;
80     bool fail_open_active;
81 };
82
83 static void fail_open_recover(struct fail_open *);
84
85 /* Returns the number of seconds of disconnection after which fail-open mode
86  * should activate. */
87 static int
88 trigger_duration(const struct fail_open *fo)
89 {
90     if (!connmgr_has_controllers(fo->connmgr)) {
91         /* Shouldn't ever arrive here, but if we do, never fail open. */
92         return INT_MAX;
93     } else {
94         /* Otherwise, every controller must have a chance to send an
95          * inactivity probe and reconnect before we fail open, so take the
96          * maximum probe interval and multiply by 3:
97          *
98          *  - The first interval is the idle time before sending an inactivity
99          *    probe.
100          *
101          *  - The second interval is the time allowed for a response to the
102          *    inactivity probe.
103          *
104          *  - The third interval is the time allowed to reconnect after no
105          *    response is received.
106          */
107         return connmgr_get_max_probe_interval(fo->connmgr) * 3;
108     }
109 }
110
111 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
112 bool
113 fail_open_is_active(const struct fail_open *fo)
114 {
115     return fo->last_disconn_secs != 0;
116 }
117
118 static void
119 send_bogus_packet_ins(struct fail_open *fo)
120 {
121     struct ofproto_packet_in pin;
122     struct eth_addr mac;
123     struct dp_packet b;
124
125     dp_packet_init(&b, 128);
126     eth_addr_nicira_random(&mac);
127     compose_rarp(&b, mac);
128
129     memset(&pin, 0, sizeof pin);
130     pin.up.packet = dp_packet_data(&b);
131     pin.up.packet_len = dp_packet_size(&b);
132     pin.up.reason = OFPR_NO_MATCH;
133     match_init_catchall(&pin.up.flow_metadata);
134     match_set_in_port(&pin.up.flow_metadata, OFPP_LOCAL);
135     pin.send_len = dp_packet_size(&b);
136     pin.miss_type = OFPROTO_PACKET_IN_NO_MISS;
137     connmgr_send_packet_in(fo->connmgr, &pin);
138
139     dp_packet_uninit(&b);
140 }
141
142 /* Enter fail-open mode if we should be in it. */
143 void
144 fail_open_run(struct fail_open *fo)
145 {
146     int disconn_secs = connmgr_failure_duration(fo->connmgr);
147
148     /* Enter fail-open mode if 'fo' is not in it but should be.  */
149     if (disconn_secs >= trigger_duration(fo)) {
150         if (!fail_open_is_active(fo)) {
151             VLOG_WARN("Could not connect to controller (or switch failed "
152                       "controller's post-connection admission control "
153                       "policy) for %d seconds, failing open", disconn_secs);
154             fo->last_disconn_secs = disconn_secs;
155
156             /* Flush all OpenFlow and datapath flows.  We will set up our
157              * fail-open rule from fail_open_flushed() when
158              * ofproto_flush_flows() calls back to us. */
159             ofproto_flush_flows(fo->ofproto);
160         } else if (disconn_secs > fo->last_disconn_secs + 60) {
161             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
162                       "from controller", disconn_secs);
163             fo->last_disconn_secs = disconn_secs;
164         }
165     }
166
167     /* Schedule a bogus packet-in if we're connected and in fail-open. */
168     if (fail_open_is_active(fo)) {
169         if (connmgr_is_any_controller_connected(fo->connmgr)) {
170             bool expired = time_msec() >= fo->next_bogus_packet_in;
171             if (expired) {
172                 send_bogus_packet_ins(fo);
173             }
174             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
175                 fo->next_bogus_packet_in = time_msec() + 2000;
176             }
177         } else {
178             fo->next_bogus_packet_in = LLONG_MAX;
179         }
180     }
181
182 }
183
184 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
185  * controller, exits fail open mode. */
186 void
187 fail_open_maybe_recover(struct fail_open *fo)
188     OVS_EXCLUDED(ofproto_mutex)
189 {
190     if (fail_open_is_active(fo)
191         && connmgr_is_any_controller_admitted(fo->connmgr)) {
192         fail_open_recover(fo);
193     }
194 }
195
196 static void
197 fail_open_recover(struct fail_open *fo)
198     OVS_EXCLUDED(ofproto_mutex)
199 {
200     struct match match;
201
202     VLOG_WARN("No longer in fail-open mode");
203     fo->last_disconn_secs = 0;
204     fo->next_bogus_packet_in = LLONG_MAX;
205
206     match_init_catchall(&match);
207     ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
208 }
209
210 void
211 fail_open_wait(struct fail_open *fo)
212 {
213     if (fo->next_bogus_packet_in != LLONG_MAX) {
214         poll_timer_wait_until(fo->next_bogus_packet_in);
215     }
216 }
217
218 void
219 fail_open_flushed(struct fail_open *fo)
220     OVS_EXCLUDED(ofproto_mutex)
221 {
222     int disconn_secs = connmgr_failure_duration(fo->connmgr);
223     bool open = disconn_secs >= trigger_duration(fo);
224     if (open) {
225         struct ofpbuf ofpacts;
226         struct match match;
227
228         /* Set up a flow that matches every packet and directs them to
229          * OFPP_NORMAL. */
230         ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
231         ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
232         ofpact_pad(&ofpacts);
233
234         match_init_catchall(&match);
235         ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
236                          ofpacts.data, ofpacts.size);
237
238         ofpbuf_uninit(&ofpacts);
239     }
240     fo->fail_open_active = open;
241 }
242
243 /* Returns the number of fail-open rules currently installed in the flow
244  * table. */
245 int
246 fail_open_count_rules(const struct fail_open *fo)
247 {
248     return fo->fail_open_active != 0;
249 }
250
251 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
252 struct fail_open *
253 fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
254 {
255     struct fail_open *fo = xmalloc(sizeof *fo);
256     fo->ofproto = ofproto;
257     fo->connmgr = mgr;
258     fo->last_disconn_secs = 0;
259     fo->next_bogus_packet_in = LLONG_MAX;
260     fo->bogus_packet_counter = rconn_packet_counter_create();
261     fo->fail_open_active = false;
262     return fo;
263 }
264
265 /* Destroys 'fo'. */
266 void
267 fail_open_destroy(struct fail_open *fo)
268     OVS_EXCLUDED(ofproto_mutex)
269 {
270     if (fo) {
271         if (fail_open_is_active(fo)) {
272             fail_open_recover(fo);
273         }
274         /* We don't own fo->connmgr. */
275         rconn_packet_counter_destroy(fo->bogus_packet_counter);
276         free(fo);
277     }
278 }