Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ofproto / fail-open.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "connmgr.h"
23 #include "flow.h"
24 #include "mac-learning.h"
25 #include "odp-util.h"
26 #include "ofp-util.h"
27 #include "ofpbuf.h"
28 #include "ofproto.h"
29 #include "pktbuf.h"
30 #include "poll-loop.h"
31 #include "rconn.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 connmgr *connmgr;
74     int last_disconn_secs;
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 (!connmgr_has_controllers(fo->connmgr)) {
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         return connmgr_get_max_probe_interval(fo->connmgr) * 3;
104     }
105 }
106
107 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
108 bool
109 fail_open_is_active(const struct fail_open *fo)
110 {
111     return fo->last_disconn_secs != 0;
112 }
113
114 static void
115 send_bogus_packet_ins(struct fail_open *fo)
116 {
117     uint8_t mac[ETH_ADDR_LEN];
118     struct ofpbuf *opi;
119     struct ofpbuf b;
120
121     /* Compose ofp_packet_in. */
122     ofpbuf_init(&b, 128);
123     eth_addr_nicira_random(mac);
124     compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
125     opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
126     ofpbuf_uninit(&b);
127
128     /* Send. */
129     connmgr_broadcast(fo->connmgr, opi);
130 }
131
132 /* Enter fail-open mode if we should be in it. */
133 void
134 fail_open_run(struct fail_open *fo)
135 {
136     int disconn_secs = connmgr_failure_duration(fo->connmgr);
137
138     /* Enter fail-open mode if 'fo' is not in it but should be.  */
139     if (disconn_secs >= trigger_duration(fo)) {
140         if (!fail_open_is_active(fo)) {
141             VLOG_WARN("Could not connect to controller (or switch failed "
142                       "controller's post-connection admission control "
143                       "policy) for %d seconds, failing open", disconn_secs);
144             fo->last_disconn_secs = disconn_secs;
145
146             /* Flush all OpenFlow and datapath flows.  We will set up our
147              * fail-open rule from fail_open_flushed() when
148              * ofproto_flush_flows() calls back to us. */
149             ofproto_flush_flows(fo->ofproto);
150         } else if (disconn_secs > fo->last_disconn_secs + 60) {
151             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
152                       "from controller", disconn_secs);
153             fo->last_disconn_secs = disconn_secs;
154         }
155     }
156
157     /* Schedule a bogus packet-in if we're connected and in fail-open. */
158     if (fail_open_is_active(fo)) {
159         if (connmgr_is_any_controller_connected(fo->connmgr)) {
160             bool expired = time_msec() >= fo->next_bogus_packet_in;
161             if (expired) {
162                 send_bogus_packet_ins(fo);
163             }
164             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
165                 fo->next_bogus_packet_in = time_msec() + 2000;
166             }
167         } else {
168             fo->next_bogus_packet_in = LLONG_MAX;
169         }
170     }
171
172 }
173
174 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
175  * controller, exits fail open mode. */
176 void
177 fail_open_maybe_recover(struct fail_open *fo)
178 {
179     if (fail_open_is_active(fo)
180         && connmgr_is_any_controller_admitted(fo->connmgr)) {
181         fail_open_recover(fo);
182     }
183 }
184
185 static void
186 fail_open_recover(struct fail_open *fo)
187 {
188     struct cls_rule rule;
189
190     VLOG_WARN("No longer in fail-open mode");
191     fo->last_disconn_secs = 0;
192     fo->next_bogus_packet_in = LLONG_MAX;
193
194     cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
195     ofproto_delete_flow(fo->ofproto, &rule);
196 }
197
198 void
199 fail_open_wait(struct fail_open *fo)
200 {
201     if (fo->next_bogus_packet_in != LLONG_MAX) {
202         poll_timer_wait_until(fo->next_bogus_packet_in);
203     }
204 }
205
206 void
207 fail_open_flushed(struct fail_open *fo)
208 {
209     int disconn_secs = connmgr_failure_duration(fo->connmgr);
210     bool open = disconn_secs >= trigger_duration(fo);
211     if (open) {
212         union ofp_action action;
213         struct cls_rule rule;
214
215         /* Set up a flow that matches every packet and directs them to
216          * OFPP_NORMAL. */
217         memset(&action, 0, sizeof action);
218         action.type = htons(OFPAT_OUTPUT);
219         action.output.len = htons(sizeof action);
220         action.output.port = htons(OFPP_NORMAL);
221
222         cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
223         ofproto_add_flow(fo->ofproto, &rule, &action, 1);
224     }
225 }
226
227 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
228 struct fail_open *
229 fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
230 {
231     struct fail_open *fo = xmalloc(sizeof *fo);
232     fo->ofproto = ofproto;
233     fo->connmgr = mgr;
234     fo->last_disconn_secs = 0;
235     fo->next_bogus_packet_in = LLONG_MAX;
236     fo->bogus_packet_counter = rconn_packet_counter_create();
237     return fo;
238 }
239
240 /* Destroys 'fo'. */
241 void
242 fail_open_destroy(struct fail_open *fo)
243 {
244     if (fo) {
245         if (fail_open_is_active(fo)) {
246             fail_open_recover(fo);
247         }
248         /* We don't own fo->connmgr. */
249         rconn_packet_counter_destroy(fo->bogus_packet_counter);
250         free(fo);
251     }
252 }