]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/fail-open.c
ovs-sandbox: Add note about OVN to initial output.
[mirror_ovs.git] / ofproto / fail-open.c
CommitLineData
064af421 1/*
9bfe9334 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "fail-open.h"
19#include <inttypes.h>
20#include <stdlib.h>
cf3fad8a 21#include "classifier.h"
19a87e36 22#include "connmgr.h"
064af421
BP
23#include "flow.h"
24#include "mac-learning.h"
25#include "odp-util.h"
cf62fa4c 26#include "ofpbuf.h"
f25d0cf3 27#include "ofp-actions.h"
fa37b408 28#include "ofp-util.h"
064af421 29#include "ofproto.h"
5bee6e26 30#include "ofproto-provider.h"
7778bd15 31#include "pktbuf.h"
cf62fa4c 32#include "dp-packet.h"
7778bd15 33#include "poll-loop.h"
064af421 34#include "rconn.h"
064af421 35#include "timeval.h"
4a1f523f 36#include "openvswitch/vconn.h"
e6211adc 37#include "openvswitch/vlog.h"
064af421 38
d98e6007 39VLOG_DEFINE_THIS_MODULE(fail_open);
5136ce49 40
7778bd15
BP
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 *
4a2026dd
BP
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:
7778bd15
BP
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
064af421
BP
74struct fail_open {
75 struct ofproto *ofproto;
19a87e36 76 struct connmgr *connmgr;
064af421 77 int last_disconn_secs;
7778bd15
BP
78 long long int next_bogus_packet_in;
79 struct rconn_packet_counter *bogus_packet_counter;
3fbbcba7 80 bool fail_open_active;
064af421
BP
81};
82
2f6d3445
BP
83static void fail_open_recover(struct fail_open *);
84
76ce9432
BP
85/* Returns the number of seconds of disconnection after which fail-open mode
86 * should activate. */
87static int
88trigger_duration(const struct fail_open *fo)
064af421 89{
19a87e36 90 if (!connmgr_has_controllers(fo->connmgr)) {
76ce9432
BP
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 */
19a87e36 107 return connmgr_get_max_probe_interval(fo->connmgr) * 3;
76ce9432 108 }
7778bd15
BP
109}
110
111/* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
112bool
113fail_open_is_active(const struct fail_open *fo)
114{
115 return fo->last_disconn_secs != 0;
116}
064af421 117
7778bd15 118static void
19a87e36 119send_bogus_packet_ins(struct fail_open *fo)
7778bd15 120{
74ff3298 121 struct eth_addr mac;
cf62fa4c 122 struct dp_packet b;
064af421 123
cf62fa4c 124 dp_packet_init(&b, 128);
74ff3298 125 eth_addr_nicira_random(&mac);
2ea838ac 126 compose_rarp(&b, mac);
7778bd15 127
a2b53dec
BP
128 struct ofproto_async_msg am = {
129 .oam = OAM_PACKET_IN,
130 .pin = {
131 .up = {
77ab5fd2
BP
132 .public = {
133 .packet = dp_packet_data(&b),
134 .packet_len = dp_packet_size(&b),
135 .flow_metadata = MATCH_CATCHALL_INITIALIZER,
136 .flow_metadata.flow.in_port.ofp_port = OFPP_LOCAL,
137 .flow_metadata.wc.masks.in_port.ofp_port
a2b53dec 138 = u16_to_ofp(UINT16_MAX),
77ab5fd2
BP
139 .reason = OFPR_NO_MATCH,
140 .cookie = OVS_BE64_MAX,
141 },
a2b53dec
BP
142 },
143 .max_len = UINT16_MAX,
144 }
9bfe9334 145 };
a2b53dec 146 connmgr_send_async_msg(fo->connmgr, &am);
0b0517ef 147
cf62fa4c 148 dp_packet_uninit(&b);
7778bd15
BP
149}
150
76ce9432 151/* Enter fail-open mode if we should be in it. */
7778bd15
BP
152void
153fail_open_run(struct fail_open *fo)
154{
19a87e36 155 int disconn_secs = connmgr_failure_duration(fo->connmgr);
76ce9432 156
7778bd15 157 /* Enter fail-open mode if 'fo' is not in it but should be. */
76ce9432 158 if (disconn_secs >= trigger_duration(fo)) {
7778bd15 159 if (!fail_open_is_active(fo)) {
5ec6690c
JP
160 VLOG_WARN("Could not connect to controller (or switch failed "
161 "controller's post-connection admission control "
162 "policy) for %d seconds, failing open", disconn_secs);
064af421
BP
163 fo->last_disconn_secs = disconn_secs;
164
165 /* Flush all OpenFlow and datapath flows. We will set up our
166 * fail-open rule from fail_open_flushed() when
167 * ofproto_flush_flows() calls back to us. */
168 ofproto_flush_flows(fo->ofproto);
7778bd15
BP
169 } else if (disconn_secs > fo->last_disconn_secs + 60) {
170 VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
171 "from controller", disconn_secs);
172 fo->last_disconn_secs = disconn_secs;
064af421 173 }
064af421 174 }
7778bd15
BP
175
176 /* Schedule a bogus packet-in if we're connected and in fail-open. */
177 if (fail_open_is_active(fo)) {
19a87e36 178 if (connmgr_is_any_controller_connected(fo->connmgr)) {
7778bd15
BP
179 bool expired = time_msec() >= fo->next_bogus_packet_in;
180 if (expired) {
76ce9432 181 send_bogus_packet_ins(fo);
7778bd15
BP
182 }
183 if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
184 fo->next_bogus_packet_in = time_msec() + 2000;
185 }
186 } else {
187 fo->next_bogus_packet_in = LLONG_MAX;
188 }
189 }
190
064af421
BP
191}
192
7778bd15
BP
193/* If 'fo' is currently in fail-open mode and its rconn has connected to the
194 * controller, exits fail open mode. */
064af421 195void
7778bd15 196fail_open_maybe_recover(struct fail_open *fo)
feb8a80b 197 OVS_EXCLUDED(ofproto_mutex)
064af421 198{
19a87e36
BP
199 if (fail_open_is_active(fo)
200 && connmgr_is_any_controller_admitted(fo->connmgr)) {
2f6d3445
BP
201 fail_open_recover(fo);
202 }
203}
204
205static void
206fail_open_recover(struct fail_open *fo)
feb8a80b 207 OVS_EXCLUDED(ofproto_mutex)
2f6d3445 208{
81a76618 209 struct match match;
7778bd15 210
19a87e36
BP
211 VLOG_WARN("No longer in fail-open mode");
212 fo->last_disconn_secs = 0;
213 fo->next_bogus_packet_in = LLONG_MAX;
7778bd15 214
81a76618
BP
215 match_init_catchall(&match);
216 ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
7778bd15
BP
217}
218
219void
220fail_open_wait(struct fail_open *fo)
221{
222 if (fo->next_bogus_packet_in != LLONG_MAX) {
7cf8b266 223 poll_timer_wait_until(fo->next_bogus_packet_in);
7778bd15 224 }
064af421
BP
225}
226
227void
228fail_open_flushed(struct fail_open *fo)
15aaf599 229 OVS_EXCLUDED(ofproto_mutex)
064af421 230{
19a87e36 231 int disconn_secs = connmgr_failure_duration(fo->connmgr);
76ce9432 232 bool open = disconn_secs >= trigger_duration(fo);
064af421 233 if (open) {
f25d0cf3 234 struct ofpbuf ofpacts;
81a76618 235 struct match match;
064af421
BP
236
237 /* Set up a flow that matches every packet and directs them to
238 * OFPP_NORMAL. */
f25d0cf3
BP
239 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
240 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
cf3fad8a 241
81a76618
BP
242 match_init_catchall(&match);
243 ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
6fd6ed71 244 ofpacts.data, ofpacts.size);
f25d0cf3
BP
245
246 ofpbuf_uninit(&ofpacts);
064af421 247 }
3fbbcba7
BP
248 fo->fail_open_active = open;
249}
250
251/* Returns the number of fail-open rules currently installed in the flow
252 * table. */
253int
254fail_open_count_rules(const struct fail_open *fo)
255{
256 return fo->fail_open_active != 0;
064af421
BP
257}
258
19a87e36 259/* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
064af421 260struct fail_open *
19a87e36 261fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
064af421
BP
262{
263 struct fail_open *fo = xmalloc(sizeof *fo);
264 fo->ofproto = ofproto;
19a87e36 265 fo->connmgr = mgr;
064af421 266 fo->last_disconn_secs = 0;
7778bd15
BP
267 fo->next_bogus_packet_in = LLONG_MAX;
268 fo->bogus_packet_counter = rconn_packet_counter_create();
3fbbcba7 269 fo->fail_open_active = false;
064af421
BP
270 return fo;
271}
272
76ce9432 273/* Destroys 'fo'. */
064af421
BP
274void
275fail_open_destroy(struct fail_open *fo)
feb8a80b 276 OVS_EXCLUDED(ofproto_mutex)
064af421
BP
277{
278 if (fo) {
19a87e36
BP
279 if (fail_open_is_active(fo)) {
280 fail_open_recover(fo);
281 }
282 /* We don't own fo->connmgr. */
7778bd15 283 rconn_packet_counter_destroy(fo->bogus_packet_counter);
064af421
BP
284 free(fo);
285 }
286}