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