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