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