]> git.proxmox.com Git - mirror_ovs.git/blame - lib/rconn.c
tests: windows ovsdb online compact
[mirror_ovs.git] / lib / rconn.c
CommitLineData
064af421 1/*
77ab5fd2 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 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 "rconn.h"
064af421
BP
19#include <errno.h>
20#include <limits.h>
21#include <stdlib.h>
22#include <string.h>
23#include "coverage.h"
d271907f
BW
24#include "openflow/openflow.h"
25#include "openvswitch/ofp-msgs.h"
f4248336 26#include "openvswitch/ofp-util.h"
64c96779 27#include "openvswitch/ofpbuf.h"
d271907f
BW
28#include "openvswitch/vconn.h"
29#include "openvswitch/vlog.h"
064af421
BP
30#include "poll-loop.h"
31#include "sat-math.h"
c1bfdd9d 32#include "stream.h"
064af421
BP
33#include "timeval.h"
34#include "util.h"
064af421 35
d98e6007 36VLOG_DEFINE_THIS_MODULE(rconn);
5136ce49 37
d76f09ea
BP
38COVERAGE_DEFINE(rconn_discarded);
39COVERAGE_DEFINE(rconn_overflow);
40COVERAGE_DEFINE(rconn_queued);
41COVERAGE_DEFINE(rconn_sent);
42
46e2b6c8
BP
43/* The connection states have the following meanings:
44 *
45 * - S_VOID: No connection information is configured.
46 *
47 * - S_BACKOFF: Waiting for a period of time before reconnecting.
48 *
49 * - S_CONNECTING: A connection attempt is in progress and has not yet
50 * succeeded or failed.
51 *
52 * - S_ACTIVE: A connection has been established and appears to be healthy.
53 *
54 * - S_IDLE: A connection has been established but has been idle for some
55 * time. An echo request has been sent, but no reply has yet been
56 * received.
57 *
58 * - S_DISCONNECTED: An unreliable connection has disconnected and cannot be
59 * automatically retried.
60 */
064af421
BP
61#define STATES \
62 STATE(VOID, 1 << 0) \
63 STATE(BACKOFF, 1 << 1) \
64 STATE(CONNECTING, 1 << 2) \
65 STATE(ACTIVE, 1 << 3) \
46e2b6c8
BP
66 STATE(IDLE, 1 << 4) \
67 STATE(DISCONNECTED, 1 << 5)
064af421
BP
68enum state {
69#define STATE(NAME, VALUE) S_##NAME = VALUE,
70 STATES
71#undef STATE
72};
73
74static const char *
75state_name(enum state state)
76{
77 switch (state) {
78#define STATE(NAME, VALUE) case S_##NAME: return #NAME;
79 STATES
80#undef STATE
81 }
82 return "***ERROR***";
83}
84
85/* A reliable connection to an OpenFlow switch or controller.
86 *
87 * See the large comment in rconn.h for more information. */
88struct rconn {
9f5e8906
BP
89 struct ovs_mutex mutex;
90
064af421
BP
91 enum state state;
92 time_t state_entered;
93
94 struct vconn *vconn;
eb15cdbb
BP
95 char *name; /* Human-readable descriptive name. */
96 char *target; /* vconn name, passed to vconn_open(). */
064af421
BP
97 bool reliable;
98
ca6ba700 99 struct ovs_list txq; /* Contains "struct ofpbuf"s. */
064af421
BP
100
101 int backoff;
102 int max_backoff;
103 time_t backoff_deadline;
064af421 104 time_t last_connected;
2cdcb898 105 time_t last_disconnected;
064af421 106 unsigned int seqno;
88a20d6e 107 int last_error;
064af421
BP
108
109 /* In S_ACTIVE and S_IDLE, probably_admitted reports whether we believe
110 * that the peer has made a (positive) admission control decision on our
111 * connection. If we have not yet been (probably) admitted, then the
112 * connection does not reset the timer used for deciding whether the switch
113 * should go into fail-open mode.
114 *
115 * last_admitted reports the last time we believe such a positive admission
116 * control decision was made. */
117 bool probably_admitted;
118 time_t last_admitted;
119
120 /* These values are simply for statistics reporting, not used directly by
8cd4882f 121 * anything internal to the rconn (or ofproto for that matter). */
064af421
BP
122 unsigned int n_attempted_connections, n_successful_connections;
123 time_t creation_time;
124 unsigned long int total_time_connected;
125
133f2dc9
BP
126 /* Throughout this file, "probe" is shorthand for "inactivity probe". When
127 * no activity has been observed from the peer for a while, we send out an
128 * echo request as an inactivity probe packet. We should receive back a
129 * response.
130 *
131 * "Activity" is defined as either receiving an OpenFlow message from the
132 * peer or successfully sending a message that had been in 'txq'. */
064af421 133 int probe_interval; /* Secs of inactivity before sending probe. */
133f2dc9 134 time_t last_activity; /* Last time we saw some activity. */
064af421 135
f125905c 136 uint8_t dscp;
19d1ab55 137
064af421 138 /* Messages sent or received are copied to the monitor connections. */
4f35691d
GS
139#define MAXIMUM_MONITORS 8
140 struct vconn *monitors[MAXIMUM_MONITORS];
064af421 141 size_t n_monitors;
6042457b
SH
142
143 uint32_t allowed_versions;
064af421
BP
144};
145
e182670b
SH
146uint32_t rconn_get_allowed_versions(const struct rconn *rconn)
147{
148 return rconn->allowed_versions;
149}
150
9f5e8906
BP
151static unsigned int elapsed_in_this_state(const struct rconn *rc)
152 OVS_REQUIRES(rc->mutex);
153static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
154static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
155static void state_transition(struct rconn *rc, enum state)
156 OVS_REQUIRES(rc->mutex);
157static void rconn_set_target__(struct rconn *rc,
158 const char *target, const char *name)
159 OVS_REQUIRES(rc->mutex);
160static int rconn_send__(struct rconn *rc, struct ofpbuf *,
161 struct rconn_packet_counter *)
162 OVS_REQUIRES(rc->mutex);
163static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex);
164static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex);
165static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
166static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex);
167static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
168static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex);
169static void close_monitor(struct rconn *rc, size_t idx, int retval)
170 OVS_REQUIRES(rc->mutex);
064af421
BP
171static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
172static bool is_connected_state(enum state);
173static bool is_admitted_msg(const struct ofpbuf *);
9f5e8906
BP
174static bool rconn_logging_connection_attempts__(const struct rconn *rc)
175 OVS_REQUIRES(rc->mutex);
176static int rconn_get_version__(const struct rconn *rconn)
177 OVS_REQUIRES(rconn->mutex);
178
179/* The following prototypes duplicate those in rconn.h, but there we weren't
180 * able to add the OVS_EXCLUDED annotations because the definition of struct
181 * rconn was not visible. */
182
183void rconn_set_max_backoff(struct rconn *rc, int max_backoff)
184 OVS_EXCLUDED(rc->mutex);
185void rconn_connect(struct rconn *rc, const char *target, const char *name)
186 OVS_EXCLUDED(rc->mutex);
187void rconn_connect_unreliably(struct rconn *rc,
188 struct vconn *vconn, const char *name)
189 OVS_EXCLUDED(rc->mutex);
190void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
191void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
192void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
193void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
194struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
195void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
196int rconn_send(struct rconn *rc, struct ofpbuf *b,
197 struct rconn_packet_counter *counter)
198 OVS_EXCLUDED(rc->mutex);
199int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
200 struct rconn_packet_counter *counter,
201 int queue_limit)
202 OVS_EXCLUDED(rc->mutex);
203void rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
204 OVS_EXCLUDED(rc->mutex);
205void rconn_set_name(struct rconn *rc, const char *new_name)
206 OVS_EXCLUDED(rc->mutex);
207bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
208int rconn_failure_duration(const struct rconn *rconn)
209 OVS_EXCLUDED(rconn->mutex);
210ovs_be16 rconn_get_local_port(const struct rconn *rconn)
211 OVS_EXCLUDED(rconn->mutex);
212int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
213unsigned int rconn_count_txqlen(const struct rconn *rc)
214 OVS_EXCLUDED(rc->mutex);
215
064af421 216
064af421
BP
217/* Creates and returns a new rconn.
218 *
219 * 'probe_interval' is a number of seconds. If the interval passes once
220 * without an OpenFlow message being received from the peer, the rconn sends
221 * out an "echo request" message. If the interval passes again without a
222 * message being received, the rconn disconnects and re-connects to the peer.
223 * Setting 'probe_interval' to 0 disables this behavior.
224 *
225 * 'max_backoff' is the maximum number of seconds between attempts to connect
226 * to the peer. The actual interval starts at 1 second and doubles on each
227 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
9794e806
BP
228 * 8 seconds is used.
229 *
230 * The new rconn is initially unconnected. Use rconn_connect() or
6042457b
SH
231 * rconn_connect_unreliably() to connect it.
232 *
233 * Connections made by the rconn will automatically negotiate an OpenFlow
234 * protocol version acceptable to both peers on the connection. The version
5b8ab80c
BP
235 * negotiated will be one of those in the 'allowed_versions' bitmap: version
236 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying
237 * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.)
238 */
064af421 239struct rconn *
6042457b
SH
240rconn_create(int probe_interval, int max_backoff, uint8_t dscp,
241 uint32_t allowed_versions)
064af421 242{
ec6fde61 243 struct rconn *rc = xzalloc(sizeof *rc);
064af421 244
9f5e8906
BP
245 ovs_mutex_init(&rc->mutex);
246
064af421
BP
247 rc->state = S_VOID;
248 rc->state_entered = time_now();
249
250 rc->vconn = NULL;
251 rc->name = xstrdup("void");
eb15cdbb 252 rc->target = xstrdup("void");
064af421
BP
253 rc->reliable = false;
254
417e7e66 255 ovs_list_init(&rc->txq);
064af421
BP
256
257 rc->backoff = 0;
c9aaa877 258 rc->max_backoff = max_backoff ? max_backoff : 8;
064af421 259 rc->backoff_deadline = TIME_MIN;
2cdcb898
AE
260 rc->last_connected = TIME_MIN;
261 rc->last_disconnected = TIME_MIN;
064af421
BP
262 rc->seqno = 0;
263
064af421
BP
264 rc->probably_admitted = false;
265 rc->last_admitted = time_now();
266
064af421
BP
267 rc->n_attempted_connections = 0;
268 rc->n_successful_connections = 0;
269 rc->creation_time = time_now();
270 rc->total_time_connected = 0;
271
133f2dc9
BP
272 rc->last_activity = time_now();
273
064af421 274 rconn_set_probe_interval(rc, probe_interval);
f125905c 275 rconn_set_dscp(rc, dscp);
064af421
BP
276
277 rc->n_monitors = 0;
5b8ab80c 278 rc->allowed_versions = allowed_versions;
064af421
BP
279
280 return rc;
281}
282
283void
284rconn_set_max_backoff(struct rconn *rc, int max_backoff)
9f5e8906 285 OVS_EXCLUDED(rc->mutex)
064af421 286{
9f5e8906 287 ovs_mutex_lock(&rc->mutex);
064af421
BP
288 rc->max_backoff = MAX(1, max_backoff);
289 if (rc->state == S_BACKOFF && rc->backoff > max_backoff) {
290 rc->backoff = max_backoff;
291 if (rc->backoff_deadline > time_now() + max_backoff) {
292 rc->backoff_deadline = time_now() + max_backoff;
293 }
294 }
9f5e8906 295 ovs_mutex_unlock(&rc->mutex);
064af421
BP
296}
297
298int
299rconn_get_max_backoff(const struct rconn *rc)
300{
301 return rc->max_backoff;
302}
303
f125905c
MM
304void
305rconn_set_dscp(struct rconn *rc, uint8_t dscp)
306{
307 rc->dscp = dscp;
308}
309
0442efd9
MM
310uint8_t
311rconn_get_dscp(const struct rconn *rc)
312{
313 return rc->dscp;
314}
315
064af421
BP
316void
317rconn_set_probe_interval(struct rconn *rc, int probe_interval)
318{
319 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
320}
321
322int
323rconn_get_probe_interval(const struct rconn *rc)
324{
325 return rc->probe_interval;
326}
327
eb15cdbb
BP
328/* Drops any existing connection on 'rc', then sets up 'rc' to connect to
329 * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
330 * target in a form acceptable to vconn_open().
331 *
332 * If 'name' is nonnull, then it is used in log messages in place of 'target'.
333 * It should presumably give more information to a human reader than 'target',
334 * but it need not be acceptable to vconn_open(). */
d4cbfb19 335void
eb15cdbb 336rconn_connect(struct rconn *rc, const char *target, const char *name)
9f5e8906 337 OVS_EXCLUDED(rc->mutex)
064af421 338{
9f5e8906
BP
339 ovs_mutex_lock(&rc->mutex);
340 rconn_disconnect__(rc);
eb15cdbb 341 rconn_set_target__(rc, target, name);
064af421 342 rc->reliable = true;
c1bfdd9d 343 if (!stream_or_pstream_needs_probes(target)) {
344 rc->probe_interval = 0;
345 }
d4cbfb19 346 reconnect(rc);
9f5e8906 347 ovs_mutex_unlock(&rc->mutex);
064af421
BP
348}
349
eb15cdbb
BP
350/* Drops any existing connection on 'rc', then configures 'rc' to use
351 * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
352 * own.
353 *
354 * By default, the target obtained from vconn_get_name(vconn) is used in log
355 * messages. If 'name' is nonnull, then it is used instead. It should
356 * presumably give more information to a human reader than the target, but it
357 * need not be acceptable to vconn_open(). */
064af421 358void
eb15cdbb
BP
359rconn_connect_unreliably(struct rconn *rc,
360 struct vconn *vconn, const char *name)
9f5e8906 361 OVS_EXCLUDED(rc->mutex)
064af421 362{
cb22974d 363 ovs_assert(vconn != NULL);
9f5e8906
BP
364
365 ovs_mutex_lock(&rc->mutex);
366 rconn_disconnect__(rc);
eb15cdbb 367 rconn_set_target__(rc, vconn_get_name(vconn), name);
064af421
BP
368 rc->reliable = false;
369 rc->vconn = vconn;
370 rc->last_connected = time_now();
371 state_transition(rc, S_ACTIVE);
9f5e8906 372 ovs_mutex_unlock(&rc->mutex);
064af421
BP
373}
374
375/* If 'rc' is connected, forces it to drop the connection and reconnect. */
376void
377rconn_reconnect(struct rconn *rc)
9f5e8906 378 OVS_EXCLUDED(rc->mutex)
064af421 379{
9f5e8906 380 ovs_mutex_lock(&rc->mutex);
064af421 381 if (rc->state & (S_ACTIVE | S_IDLE)) {
b97ba90b 382 VLOG_INFO("%s: disconnecting", rc->name);
064af421
BP
383 disconnect(rc, 0);
384 }
9f5e8906 385 ovs_mutex_unlock(&rc->mutex);
064af421
BP
386}
387
9f5e8906
BP
388static void
389rconn_disconnect__(struct rconn *rc)
390 OVS_REQUIRES(rc->mutex)
064af421
BP
391{
392 if (rc->state != S_VOID) {
393 if (rc->vconn) {
394 vconn_close(rc->vconn);
395 rc->vconn = NULL;
396 }
eb15cdbb 397 rconn_set_target__(rc, "void", NULL);
064af421
BP
398 rc->reliable = false;
399
400 rc->backoff = 0;
401 rc->backoff_deadline = TIME_MIN;
402
403 state_transition(rc, S_VOID);
404 }
405}
406
9f5e8906
BP
407void
408rconn_disconnect(struct rconn *rc)
409 OVS_EXCLUDED(rc->mutex)
410{
411 ovs_mutex_lock(&rc->mutex);
412 rconn_disconnect__(rc);
413 ovs_mutex_unlock(&rc->mutex);
414}
415
064af421
BP
416/* Disconnects 'rc' and frees the underlying storage. */
417void
418rconn_destroy(struct rconn *rc)
419{
420 if (rc) {
421 size_t i;
422
9f5e8906 423 ovs_mutex_lock(&rc->mutex);
064af421 424 free(rc->name);
eb15cdbb 425 free(rc->target);
064af421
BP
426 vconn_close(rc->vconn);
427 flush_queue(rc);
b3907fbc 428 ofpbuf_list_delete(&rc->txq);
064af421
BP
429 for (i = 0; i < rc->n_monitors; i++) {
430 vconn_close(rc->monitors[i]);
431 }
9f5e8906
BP
432 ovs_mutex_unlock(&rc->mutex);
433 ovs_mutex_destroy(&rc->mutex);
434
064af421
BP
435 free(rc);
436 }
437}
438
439static unsigned int
67a4917b 440timeout_VOID(const struct rconn *rc OVS_UNUSED)
9f5e8906 441 OVS_REQUIRES(rc->mutex)
064af421
BP
442{
443 return UINT_MAX;
444}
445
446static void
67a4917b 447run_VOID(struct rconn *rc OVS_UNUSED)
9f5e8906 448 OVS_REQUIRES(rc->mutex)
064af421
BP
449{
450 /* Nothing to do. */
451}
452
d4cbfb19 453static void
064af421 454reconnect(struct rconn *rc)
9f5e8906 455 OVS_REQUIRES(rc->mutex)
064af421
BP
456{
457 int retval;
458
07c8c80d
BP
459 if (rconn_logging_connection_attempts__(rc)) {
460 VLOG_INFO("%s: connecting...", rc->name);
461 }
064af421 462 rc->n_attempted_connections++;
82c8c53c
BP
463 retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp,
464 &rc->vconn);
064af421
BP
465 if (!retval) {
466 rc->backoff_deadline = time_now() + rc->backoff;
467 state_transition(rc, S_CONNECTING);
468 } else {
10a89ef0
BP
469 VLOG_WARN("%s: connection failed (%s)",
470 rc->name, ovs_strerror(retval));
064af421 471 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
88a20d6e 472 disconnect(rc, retval);
064af421 473 }
064af421
BP
474}
475
476static unsigned int
477timeout_BACKOFF(const struct rconn *rc)
9f5e8906 478 OVS_REQUIRES(rc->mutex)
064af421
BP
479{
480 return rc->backoff;
481}
482
483static void
484run_BACKOFF(struct rconn *rc)
9f5e8906 485 OVS_REQUIRES(rc->mutex)
064af421
BP
486{
487 if (timed_out(rc)) {
488 reconnect(rc);
489 }
490}
491
492static unsigned int
493timeout_CONNECTING(const struct rconn *rc)
9f5e8906 494 OVS_REQUIRES(rc->mutex)
064af421
BP
495{
496 return MAX(1, rc->backoff);
497}
498
499static void
500run_CONNECTING(struct rconn *rc)
9f5e8906 501 OVS_REQUIRES(rc->mutex)
064af421
BP
502{
503 int retval = vconn_connect(rc->vconn);
504 if (!retval) {
505 VLOG_INFO("%s: connected", rc->name);
506 rc->n_successful_connections++;
507 state_transition(rc, S_ACTIVE);
508 rc->last_connected = rc->state_entered;
509 } else if (retval != EAGAIN) {
07c8c80d
BP
510 if (rconn_logging_connection_attempts__(rc)) {
511 VLOG_INFO("%s: connection failed (%s)",
10a89ef0 512 rc->name, ovs_strerror(retval));
07c8c80d 513 }
064af421
BP
514 disconnect(rc, retval);
515 } else if (timed_out(rc)) {
07c8c80d
BP
516 if (rconn_logging_connection_attempts__(rc)) {
517 VLOG_INFO("%s: connection timed out", rc->name);
518 }
064af421 519 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
88a20d6e 520 disconnect(rc, ETIMEDOUT);
064af421
BP
521 }
522}
523
524static void
525do_tx_work(struct rconn *rc)
9f5e8906 526 OVS_REQUIRES(rc->mutex)
064af421 527{
417e7e66 528 if (ovs_list_is_empty(&rc->txq)) {
064af421
BP
529 return;
530 }
417e7e66 531 while (!ovs_list_is_empty(&rc->txq)) {
064af421
BP
532 int error = try_send(rc);
533 if (error) {
534 break;
535 }
133f2dc9 536 rc->last_activity = time_now();
064af421 537 }
417e7e66 538 if (ovs_list_is_empty(&rc->txq)) {
064af421
BP
539 poll_immediate_wake();
540 }
541}
542
543static unsigned int
544timeout_ACTIVE(const struct rconn *rc)
9f5e8906 545 OVS_REQUIRES(rc->mutex)
064af421
BP
546{
547 if (rc->probe_interval) {
133f2dc9 548 unsigned int base = MAX(rc->last_activity, rc->state_entered);
064af421
BP
549 unsigned int arg = base + rc->probe_interval - rc->state_entered;
550 return arg;
551 }
552 return UINT_MAX;
553}
554
555static void
556run_ACTIVE(struct rconn *rc)
9f5e8906 557 OVS_REQUIRES(rc->mutex)
064af421
BP
558{
559 if (timed_out(rc)) {
133f2dc9 560 unsigned int base = MAX(rc->last_activity, rc->state_entered);
1a126c0c
SH
561 int version;
562
064af421
BP
563 VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
564 rc->name, (unsigned int) (time_now() - base));
565
9f5e8906 566 version = rconn_get_version__(rc);
cb22974d 567 ovs_assert(version >= 0 && version <= 0xff);
1a126c0c 568
064af421
BP
569 /* Ordering is important here: rconn_send() can transition to BACKOFF,
570 * and we don't want to transition back to IDLE if so, because then we
571 * can end up queuing a packet with vconn == NULL and then *boom*. */
572 state_transition(rc, S_IDLE);
9f5e8906 573 rconn_send__(rc, make_echo_request(version), NULL);
064af421
BP
574 return;
575 }
576
577 do_tx_work(rc);
578}
579
580static unsigned int
581timeout_IDLE(const struct rconn *rc)
9f5e8906 582 OVS_REQUIRES(rc->mutex)
064af421
BP
583{
584 return rc->probe_interval;
585}
586
587static void
588run_IDLE(struct rconn *rc)
9f5e8906 589 OVS_REQUIRES(rc->mutex)
064af421
BP
590{
591 if (timed_out(rc)) {
064af421
BP
592 VLOG_ERR("%s: no response to inactivity probe after %u "
593 "seconds, disconnecting",
594 rc->name, elapsed_in_this_state(rc));
88a20d6e 595 disconnect(rc, ETIMEDOUT);
064af421
BP
596 } else {
597 do_tx_work(rc);
598 }
599}
600
46e2b6c8
BP
601static unsigned int
602timeout_DISCONNECTED(const struct rconn *rc OVS_UNUSED)
603 OVS_REQUIRES(rc->mutex)
604{
605 return UINT_MAX;
606}
607
608static void
609run_DISCONNECTED(struct rconn *rc OVS_UNUSED)
610 OVS_REQUIRES(rc->mutex)
611{
612 /* Nothing to do. */
613}
614
064af421
BP
615/* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
616 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
617 * connected, attempts to send packets in the send queue, if any. */
618void
619rconn_run(struct rconn *rc)
9f5e8906 620 OVS_EXCLUDED(rc->mutex)
064af421
BP
621{
622 int old_state;
60cb3eb8
BP
623 size_t i;
624
9f5e8906 625 ovs_mutex_lock(&rc->mutex);
60cb3eb8 626 if (rc->vconn) {
accaecc4
BP
627 int error;
628
60cb3eb8 629 vconn_run(rc->vconn);
accaecc4
BP
630
631 error = vconn_get_status(rc->vconn);
632 if (error) {
633 report_error(rc, error);
634 disconnect(rc, error);
635 }
60cb3eb8 636 }
f44c5146
BP
637 for (i = 0; i < rc->n_monitors; ) {
638 struct ofpbuf *msg;
639 int retval;
640
60cb3eb8 641 vconn_run(rc->monitors[i]);
f44c5146
BP
642
643 /* Drain any stray message that came in on the monitor connection. */
644 retval = vconn_recv(rc->monitors[i], &msg);
645 if (!retval) {
646 ofpbuf_delete(msg);
647 } else if (retval != EAGAIN) {
648 close_monitor(rc, i, retval);
649 continue;
650 }
651 i++;
60cb3eb8
BP
652 }
653
064af421
BP
654 do {
655 old_state = rc->state;
656 switch (rc->state) {
657#define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
658 STATES
659#undef STATE
660 default:
428b2edd 661 OVS_NOT_REACHED();
064af421
BP
662 }
663 } while (rc->state != old_state);
9f5e8906 664 ovs_mutex_unlock(&rc->mutex);
064af421
BP
665}
666
667/* Causes the next call to poll_block() to wake up when rconn_run() should be
668 * called on 'rc'. */
669void
670rconn_run_wait(struct rconn *rc)
9f5e8906 671 OVS_EXCLUDED(rc->mutex)
064af421 672{
60cb3eb8
BP
673 unsigned int timeo;
674 size_t i;
675
9f5e8906 676 ovs_mutex_lock(&rc->mutex);
60cb3eb8
BP
677 if (rc->vconn) {
678 vconn_run_wait(rc->vconn);
417e7e66 679 if ((rc->state & (S_ACTIVE | S_IDLE)) && !ovs_list_is_empty(&rc->txq)) {
82c2b79d
BP
680 vconn_wait(rc->vconn, WAIT_SEND);
681 }
60cb3eb8
BP
682 }
683 for (i = 0; i < rc->n_monitors; i++) {
684 vconn_run_wait(rc->monitors[i]);
f44c5146 685 vconn_recv_wait(rc->monitors[i]);
60cb3eb8
BP
686 }
687
688 timeo = timeout(rc);
064af421 689 if (timeo != UINT_MAX) {
7cf8b266
BP
690 long long int expires = sat_add(rc->state_entered, timeo);
691 poll_timer_wait_until(expires * 1000);
064af421 692 }
9f5e8906 693 ovs_mutex_unlock(&rc->mutex);
064af421
BP
694}
695
696/* Attempts to receive a packet from 'rc'. If successful, returns the packet;
697 * otherwise, returns a null pointer. The caller is responsible for freeing
698 * the packet (with ofpbuf_delete()). */
699struct ofpbuf *
700rconn_recv(struct rconn *rc)
9f5e8906 701 OVS_EXCLUDED(rc->mutex)
064af421 702{
9f5e8906
BP
703 struct ofpbuf *buffer = NULL;
704
705 ovs_mutex_lock(&rc->mutex);
064af421 706 if (rc->state & (S_ACTIVE | S_IDLE)) {
064af421
BP
707 int error = vconn_recv(rc->vconn, &buffer);
708 if (!error) {
709 copy_to_monitor(rc, buffer);
7778bd15 710 if (rc->probably_admitted || is_admitted_msg(buffer)
064af421
BP
711 || time_now() - rc->last_connected >= 30) {
712 rc->probably_admitted = true;
713 rc->last_admitted = time_now();
714 }
133f2dc9 715 rc->last_activity = time_now();
064af421
BP
716 if (rc->state == S_IDLE) {
717 state_transition(rc, S_ACTIVE);
718 }
064af421 719 } else if (error != EAGAIN) {
b97ba90b 720 report_error(rc, error);
064af421
BP
721 disconnect(rc, error);
722 }
723 }
9f5e8906
BP
724 ovs_mutex_unlock(&rc->mutex);
725
726 return buffer;
064af421
BP
727}
728
729/* Causes the next call to poll_block() to wake up when a packet may be ready
730 * to be received by vconn_recv() on 'rc'. */
731void
732rconn_recv_wait(struct rconn *rc)
9f5e8906 733 OVS_EXCLUDED(rc->mutex)
064af421 734{
9f5e8906 735 ovs_mutex_lock(&rc->mutex);
064af421
BP
736 if (rc->vconn) {
737 vconn_wait(rc->vconn, WAIT_RECV);
738 }
9f5e8906 739 ovs_mutex_unlock(&rc->mutex);
064af421
BP
740}
741
9f5e8906
BP
742static int
743rconn_send__(struct rconn *rc, struct ofpbuf *b,
064af421 744 struct rconn_packet_counter *counter)
9f5e8906 745 OVS_REQUIRES(rc->mutex)
064af421
BP
746{
747 if (rconn_is_connected(rc)) {
748 COVERAGE_INC(rconn_queued);
749 copy_to_monitor(rc, b);
437d0d22 750
064af421 751 if (counter) {
6fd6ed71 752 rconn_packet_counter_inc(counter, b->size);
064af421 753 }
437d0d22 754
cf3b7538 755 /* Reuse 'frame' as a private pointer while 'b' is in txq. */
6fd6ed71 756 b->header = counter;
437d0d22 757
417e7e66 758 ovs_list_push_back(&rc->txq, &b->list_node);
064af421
BP
759
760 /* If the queue was empty before we added 'b', try to send some
761 * packets. (But if the queue had packets in it, it's because the
762 * vconn is backlogged and there's no point in stuffing more into it
763 * now. We'll get back to that in rconn_run().) */
b3907fbc 764 if (rc->txq.next == &b->list_node) {
064af421
BP
765 try_send(rc);
766 }
767 return 0;
768 } else {
acb9da40 769 ofpbuf_delete(b);
064af421
BP
770 return ENOTCONN;
771 }
772}
773
9f5e8906
BP
774/* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not
775 * currently connected. Takes ownership of 'b'.
776 *
777 * If 'counter' is non-null, then 'counter' will be incremented while the
778 * packet is in flight, then decremented when it has been sent (or discarded
779 * due to disconnection). Because 'b' may be sent (or discarded) before this
780 * function returns, the caller may not be able to observe any change in
781 * 'counter'.
782 *
783 * There is no rconn_send_wait() function: an rconn has a send queue that it
784 * takes care of sending if you call rconn_run(), which will have the side
785 * effect of waking up poll_block(). */
786int
787rconn_send(struct rconn *rc, struct ofpbuf *b,
788 struct rconn_packet_counter *counter)
789 OVS_EXCLUDED(rc->mutex)
790{
791 int error;
792
793 ovs_mutex_lock(&rc->mutex);
794 error = rconn_send__(rc, b, counter);
795 ovs_mutex_unlock(&rc->mutex);
796
797 return error;
798}
799
064af421
BP
800/* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it
801 * will be decremented when it has been sent (or discarded due to
802 * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already
803 * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
804 * connected. Regardless of return value, 'b' is destroyed.
805 *
806 * Because 'b' may be sent (or discarded) before this function returns, the
807 * caller may not be able to observe any change in 'counter'.
808 *
809 * There is no rconn_send_wait() function: an rconn has a send queue that it
810 * takes care of sending if you call rconn_run(), which will have the side
811 * effect of waking up poll_block(). */
812int
813rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
814 struct rconn_packet_counter *counter, int queue_limit)
9f5e8906 815 OVS_EXCLUDED(rc->mutex)
064af421 816{
9f5e8906
BP
817 int error;
818
819 ovs_mutex_lock(&rc->mutex);
a3d1ff00 820 if (rconn_packet_counter_n_packets(counter) < queue_limit) {
9f5e8906 821 error = rconn_send__(rc, b, counter);
b8dddecf 822 } else {
064af421 823 COVERAGE_INC(rconn_overflow);
b8dddecf 824 ofpbuf_delete(b);
9f5e8906 825 error = EAGAIN;
064af421 826 }
9f5e8906
BP
827 ovs_mutex_unlock(&rc->mutex);
828
829 return error;
064af421
BP
830}
831
064af421
BP
832/* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent
833 * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */
834void
835rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
9f5e8906 836 OVS_EXCLUDED(rc->mutex)
064af421 837{
9f5e8906 838 ovs_mutex_lock(&rc->mutex);
064af421
BP
839 if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) {
840 VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn));
841 rc->monitors[rc->n_monitors++] = vconn;
842 } else {
843 VLOG_DBG("too many monitor connections, discarding %s",
844 vconn_get_name(vconn));
845 vconn_close(vconn);
846 }
9f5e8906 847 ovs_mutex_unlock(&rc->mutex);
064af421
BP
848}
849
eb15cdbb
BP
850/* Returns 'rc''s name. This is a name for human consumption, appropriate for
851 * use in log messages. It is not necessarily a name that may be passed
852 * directly to, e.g., vconn_open(). */
064af421
BP
853const char *
854rconn_get_name(const struct rconn *rc)
855{
856 return rc->name;
857}
858
eb15cdbb
BP
859/* Sets 'rc''s name to 'new_name'. */
860void
861rconn_set_name(struct rconn *rc, const char *new_name)
9f5e8906 862 OVS_EXCLUDED(rc->mutex)
eb15cdbb 863{
9f5e8906 864 ovs_mutex_lock(&rc->mutex);
eb15cdbb
BP
865 free(rc->name);
866 rc->name = xstrdup(new_name);
9f5e8906 867 ovs_mutex_unlock(&rc->mutex);
eb15cdbb
BP
868}
869
870/* Returns 'rc''s target. This is intended to be a string that may be passed
871 * directly to, e.g., vconn_open(). */
872const char *
873rconn_get_target(const struct rconn *rc)
874{
875 return rc->target;
876}
877
064af421
BP
878/* Returns true if 'rconn' is connected or in the process of reconnecting,
879 * false if 'rconn' is disconnected and will not reconnect on its own. */
880bool
881rconn_is_alive(const struct rconn *rconn)
882{
46e2b6c8 883 return rconn->state != S_VOID && rconn->state != S_DISCONNECTED;
064af421
BP
884}
885
886/* Returns true if 'rconn' is connected, false otherwise. */
887bool
888rconn_is_connected(const struct rconn *rconn)
889{
890 return is_connected_state(rconn->state);
891}
892
9f5e8906
BP
893static bool
894rconn_is_admitted__(const struct rconn *rconn)
895 OVS_REQUIRES(rconn->mutex)
896{
897 return (rconn_is_connected(rconn)
898 && rconn->last_admitted >= rconn->last_connected);
899}
900
7778bd15
BP
901/* Returns true if 'rconn' is connected and thought to have been accepted by
902 * the peer's admission-control policy. */
903bool
904rconn_is_admitted(const struct rconn *rconn)
9f5e8906 905 OVS_EXCLUDED(rconn->mutex)
7778bd15 906{
9f5e8906
BP
907 bool admitted;
908
909 ovs_mutex_lock(&rconn->mutex);
910 admitted = rconn_is_admitted__(rconn);
911 ovs_mutex_unlock(&rconn->mutex);
912
913 return admitted;
7778bd15
BP
914}
915
916/* Returns 0 if 'rconn' is currently connected and considered to have been
917 * accepted by the peer's admission-control policy, otherwise the number of
918 * seconds since 'rconn' was last in such a state. */
064af421
BP
919int
920rconn_failure_duration(const struct rconn *rconn)
9f5e8906 921 OVS_EXCLUDED(rconn->mutex)
064af421 922{
9f5e8906
BP
923 int duration;
924
925 ovs_mutex_lock(&rconn->mutex);
926 duration = (rconn_is_admitted__(rconn)
927 ? 0
928 : time_now() - rconn->last_admitted);
929 ovs_mutex_unlock(&rconn->mutex);
930
931 return duration;
064af421
BP
932}
933
9f5e8906
BP
934static int
935rconn_get_version__(const struct rconn *rconn)
936 OVS_REQUIRES(rconn->mutex)
937{
938 return rconn->vconn ? vconn_get_version(rconn->vconn) : -1;
064af421
BP
939}
940
27527aa0
BP
941/* Returns the OpenFlow version negotiated with the peer, or -1 if there is
942 * currently no connection or if version negotiation is not yet complete. */
943int
944rconn_get_version(const struct rconn *rconn)
9f5e8906 945 OVS_EXCLUDED(rconn->mutex)
27527aa0 946{
9f5e8906
BP
947 int version;
948
949 ovs_mutex_lock(&rconn->mutex);
950 version = rconn_get_version__(rconn);
951 ovs_mutex_unlock(&rconn->mutex);
952
953 return version;
27527aa0
BP
954}
955
064af421
BP
956/* Returns a string representing the internal state of 'rc'. The caller must
957 * not modify or free the string. */
958const char *
959rconn_get_state(const struct rconn *rc)
960{
961 return state_name(rc->state);
962}
963
064af421 964/* Returns the time at which the last successful connection was made by
2cdcb898 965 * 'rc'. Returns TIME_MIN if never connected. */
064af421
BP
966time_t
967rconn_get_last_connection(const struct rconn *rc)
968{
969 return rc->last_connected;
970}
971
2cdcb898
AE
972/* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN
973 * if never disconnected. */
974time_t
975rconn_get_last_disconnect(const struct rconn *rc)
976{
977 return rc->last_disconnected;
978}
979
064af421
BP
980/* Returns 'rc''s current connection sequence number, a number that changes
981 * every time that 'rconn' connects or disconnects. */
982unsigned int
983rconn_get_connection_seqno(const struct rconn *rc)
984{
985 return rc->seqno;
986}
88a20d6e
BP
987
988/* Returns a value that explains why 'rc' last disconnected:
989 *
990 * - 0 means that the last disconnection was caused by a call to
991 * rconn_disconnect(), or that 'rc' is new and has not yet completed its
992 * initial connection or connection attempt.
993 *
994 * - EOF means that the connection was closed in the normal way by the peer.
995 *
996 * - A positive integer is an errno value that represents the error.
997 */
998int
999rconn_get_last_error(const struct rconn *rc)
1000{
1001 return rc->last_error;
1002}
0d085684
BP
1003
1004/* Returns the number of messages queued for transmission on 'rc'. */
1005unsigned int
1006rconn_count_txqlen(const struct rconn *rc)
9f5e8906 1007 OVS_EXCLUDED(rc->mutex)
0d085684 1008{
9f5e8906
BP
1009 unsigned int len;
1010
1011 ovs_mutex_lock(&rc->mutex);
417e7e66 1012 len = ovs_list_size(&rc->txq);
9f5e8906
BP
1013 ovs_mutex_unlock(&rc->mutex);
1014
1015 return len;
0d085684 1016}
064af421
BP
1017\f
1018struct rconn_packet_counter *
1019rconn_packet_counter_create(void)
1020{
a6441685 1021 struct rconn_packet_counter *c = xzalloc(sizeof *c);
a3d1ff00
BP
1022 ovs_mutex_init(&c->mutex);
1023 ovs_mutex_lock(&c->mutex);
064af421 1024 c->ref_cnt = 1;
a3d1ff00 1025 ovs_mutex_unlock(&c->mutex);
064af421
BP
1026 return c;
1027}
1028
1029void
1030rconn_packet_counter_destroy(struct rconn_packet_counter *c)
1031{
1032 if (c) {
a3d1ff00
BP
1033 bool dead;
1034
1035 ovs_mutex_lock(&c->mutex);
cb22974d 1036 ovs_assert(c->ref_cnt > 0);
a3d1ff00
BP
1037 dead = !--c->ref_cnt && !c->n_packets;
1038 ovs_mutex_unlock(&c->mutex);
1039
1040 if (dead) {
1041 ovs_mutex_destroy(&c->mutex);
064af421
BP
1042 free(c);
1043 }
1044 }
1045}
1046
1047void
a6441685 1048rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
064af421 1049{
a3d1ff00 1050 ovs_mutex_lock(&c->mutex);
a6441685
BP
1051 c->n_packets++;
1052 c->n_bytes += n_bytes;
a3d1ff00 1053 ovs_mutex_unlock(&c->mutex);
064af421
BP
1054}
1055
1056void
a6441685 1057rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
064af421 1058{
a3d1ff00 1059 bool dead = false;
a6441685 1060
a3d1ff00
BP
1061 ovs_mutex_lock(&c->mutex);
1062 ovs_assert(c->n_packets > 0);
1063 ovs_assert(c->n_packets == 1
1064 ? c->n_bytes == n_bytes
1065 : c->n_bytes > n_bytes);
a6441685 1066 c->n_packets--;
a3d1ff00
BP
1067 c->n_bytes -= n_bytes;
1068 dead = !c->n_packets && !c->ref_cnt;
1069 ovs_mutex_unlock(&c->mutex);
1070
1071 if (dead) {
1072 ovs_mutex_destroy(&c->mutex);
1073 free(c);
064af421
BP
1074 }
1075}
a3d1ff00
BP
1076
1077unsigned int
1078rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
1079{
1080 unsigned int n;
1081
1082 ovs_mutex_lock(&c->mutex);
1083 n = c->n_packets;
1084 ovs_mutex_unlock(&c->mutex);
1085
1086 return n;
1087}
1088
1089unsigned int
1090rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
1091{
1092 unsigned int n;
1093
1094 ovs_mutex_lock(&c->mutex);
1095 n = c->n_bytes;
1096 ovs_mutex_unlock(&c->mutex);
1097
1098 return n;
1099}
064af421 1100\f
eb15cdbb 1101/* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
9e7b1b11 1102 * is null, 'target' is used. */
19d1ab55 1103static void
eb15cdbb 1104rconn_set_target__(struct rconn *rc, const char *target, const char *name)
9f5e8906 1105 OVS_REQUIRES(rc->mutex)
19d1ab55
BP
1106{
1107 free(rc->name);
eb15cdbb
BP
1108 rc->name = xstrdup(name ? name : target);
1109 free(rc->target);
1110 rc->target = xstrdup(target);
19d1ab55
BP
1111}
1112
064af421
BP
1113/* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
1114 * otherwise a positive errno value. */
1115static int
1116try_send(struct rconn *rc)
9f5e8906 1117 OVS_REQUIRES(rc->mutex)
064af421 1118{
b3907fbc 1119 struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next);
6fd6ed71
PS
1120 unsigned int n_bytes = msg->size;
1121 struct rconn_packet_counter *counter = msg->header;
b3907fbc
BP
1122 int retval;
1123
1124 /* Eagerly remove 'msg' from the txq. We can't remove it from the list
1125 * after sending, if sending is successful, because it is then owned by the
1126 * vconn, which might have freed it already. */
417e7e66 1127 ovs_list_remove(&msg->list_node);
6fd6ed71 1128 msg->header = NULL;
b3907fbc
BP
1129
1130 retval = vconn_send(rc->vconn, msg);
064af421 1131 if (retval) {
6fd6ed71 1132 msg->header = counter;
417e7e66 1133 ovs_list_push_front(&rc->txq, &msg->list_node);
064af421 1134 if (retval != EAGAIN) {
b97ba90b 1135 report_error(rc, retval);
064af421
BP
1136 disconnect(rc, retval);
1137 }
1138 return retval;
1139 }
1140 COVERAGE_INC(rconn_sent);
064af421 1141 if (counter) {
a6441685 1142 rconn_packet_counter_dec(counter, n_bytes);
064af421 1143 }
064af421
BP
1144 return 0;
1145}
1146
b97ba90b
BP
1147/* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive
1148 * errno value, or it may be EOF to indicate that the connection was closed
1149 * normally. */
064af421 1150static void
b97ba90b 1151report_error(struct rconn *rc, int error)
9f5e8906 1152 OVS_REQUIRES(rc->mutex)
b97ba90b 1153{
055b1668
GS
1154 /* On Windows, when a peer terminates without calling a closesocket()
1155 * on socket fd, we get WSAECONNRESET. Don't print warning messages
1156 * for that case. */
1157 if (error == EOF
1158#ifdef _WIN32
1159 || error == WSAECONNRESET
1160#endif
1161 ) {
b97ba90b
BP
1162 /* If 'rc' isn't reliable, then we don't really expect this connection
1163 * to last forever anyway (probably it's a connection that we received
1164 * via accept()), so use DBG level to avoid cluttering the logs. */
1165 enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG;
1166 VLOG(level, "%s: connection closed by peer", rc->name);
1167 } else {
10a89ef0
BP
1168 VLOG_WARN("%s: connection dropped (%s)",
1169 rc->name, ovs_strerror(error));
b97ba90b
BP
1170 }
1171}
1172
88a20d6e
BP
1173/* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last
1174 * disconnection:
1175 *
1176 * - 0 means that this disconnection is due to a request by 'rc''s client,
1177 * not due to any kind of network error.
1178 *
1179 * - EOF means that the connection was closed in the normal way by the peer.
1180 *
1181 * - A positive integer is an errno value that represents the error.
1182 */
064af421
BP
1183static void
1184disconnect(struct rconn *rc, int error)
9f5e8906 1185 OVS_REQUIRES(rc->mutex)
064af421 1186{
88a20d6e 1187 rc->last_error = error;
46e2b6c8
BP
1188 if (rc->vconn) {
1189 vconn_close(rc->vconn);
1190 rc->vconn = NULL;
1191 }
064af421
BP
1192 if (rc->reliable) {
1193 time_t now = time_now();
1194
1195 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
2cdcb898 1196 rc->last_disconnected = now;
064af421
BP
1197 flush_queue(rc);
1198 }
1199
1200 if (now >= rc->backoff_deadline) {
1201 rc->backoff = 1;
07c8c80d
BP
1202 } else if (rc->backoff < rc->max_backoff / 2) {
1203 rc->backoff = MAX(1, 2 * rc->backoff);
46816c34 1204 VLOG_INFO("%s: waiting %d seconds before reconnect",
064af421 1205 rc->name, rc->backoff);
07c8c80d
BP
1206 } else {
1207 if (rconn_logging_connection_attempts__(rc)) {
1208 VLOG_INFO("%s: continuing to retry connections in the "
1209 "background but suppressing further logging",
1210 rc->name);
1211 }
1212 rc->backoff = rc->max_backoff;
064af421
BP
1213 }
1214 rc->backoff_deadline = now + rc->backoff;
1215 state_transition(rc, S_BACKOFF);
064af421 1216 } else {
2cdcb898 1217 rc->last_disconnected = time_now();
46e2b6c8 1218 state_transition(rc, S_DISCONNECTED);
064af421
BP
1219 }
1220}
1221
1222/* Drops all the packets from 'rc''s send queue and decrements their queue
1223 * counts. */
1224static void
1225flush_queue(struct rconn *rc)
9f5e8906 1226 OVS_REQUIRES(rc->mutex)
064af421 1227{
417e7e66 1228 if (ovs_list_is_empty(&rc->txq)) {
064af421
BP
1229 return;
1230 }
417e7e66
BW
1231 while (!ovs_list_is_empty(&rc->txq)) {
1232 struct ofpbuf *b = ofpbuf_from_list(ovs_list_pop_front(&rc->txq));
6fd6ed71 1233 struct rconn_packet_counter *counter = b->header;
064af421 1234 if (counter) {
6fd6ed71 1235 rconn_packet_counter_dec(counter, b->size);
064af421
BP
1236 }
1237 COVERAGE_INC(rconn_discarded);
1238 ofpbuf_delete(b);
1239 }
1240 poll_immediate_wake();
1241}
1242
1243static unsigned int
1244elapsed_in_this_state(const struct rconn *rc)
9f5e8906 1245 OVS_REQUIRES(rc->mutex)
064af421
BP
1246{
1247 return time_now() - rc->state_entered;
1248}
1249
1250static unsigned int
1251timeout(const struct rconn *rc)
9f5e8906 1252 OVS_REQUIRES(rc->mutex)
064af421
BP
1253{
1254 switch (rc->state) {
1255#define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
1256 STATES
1257#undef STATE
1258 default:
428b2edd 1259 OVS_NOT_REACHED();
064af421
BP
1260 }
1261}
1262
1263static bool
1264timed_out(const struct rconn *rc)
9f5e8906 1265 OVS_REQUIRES(rc->mutex)
064af421
BP
1266{
1267 return time_now() >= sat_add(rc->state_entered, timeout(rc));
1268}
1269
1270static void
1271state_transition(struct rconn *rc, enum state state)
9f5e8906 1272 OVS_REQUIRES(rc->mutex)
064af421 1273{
8f2bc8d7 1274 rc->seqno += is_connected_state(rc->state) != is_connected_state(state);
064af421
BP
1275 if (is_connected_state(state) && !is_connected_state(rc->state)) {
1276 rc->probably_admitted = false;
1277 }
1278 if (rconn_is_connected(rc)) {
1279 rc->total_time_connected += elapsed_in_this_state(rc);
1280 }
1281 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
1282 rc->state = state;
1283 rc->state_entered = time_now();
1284}
1285
5ac7c4dc
BP
1286static void
1287close_monitor(struct rconn *rc, size_t idx, int retval)
9f5e8906 1288 OVS_REQUIRES(rc->mutex)
5ac7c4dc
BP
1289{
1290 VLOG_DBG("%s: closing monitor connection to %s: %s",
1291 rconn_get_name(rc), vconn_get_name(rc->monitors[idx]),
1292 ovs_retval_to_string(retval));
1293 rc->monitors[idx] = rc->monitors[--rc->n_monitors];
1294}
1295
064af421
BP
1296static void
1297copy_to_monitor(struct rconn *rc, const struct ofpbuf *b)
9f5e8906 1298 OVS_REQUIRES(rc->mutex)
064af421
BP
1299{
1300 struct ofpbuf *clone = NULL;
1301 int retval;
1302 size_t i;
1303
1304 for (i = 0; i < rc->n_monitors; ) {
1305 struct vconn *vconn = rc->monitors[i];
1306
1307 if (!clone) {
1308 clone = ofpbuf_clone(b);
1309 }
1310 retval = vconn_send(vconn, clone);
1311 if (!retval) {
1312 clone = NULL;
1313 } else if (retval != EAGAIN) {
5ac7c4dc 1314 close_monitor(rc, i, retval);
064af421
BP
1315 continue;
1316 }
1317 i++;
1318 }
1319 ofpbuf_delete(clone);
1320}
1321
1322static bool
d295e8e9 1323is_connected_state(enum state state)
064af421
BP
1324{
1325 return (state & (S_ACTIVE | S_IDLE)) != 0;
1326}
1327
81196254
BP
1328/* When a switch initially connects to a controller, the controller may spend a
1329 * little time examining the switch, looking at, for example, its datapath ID,
1330 * before it decides whether it is willing to control that switch. At that
1331 * point, it either disconnects or starts controlling the switch.
1332 *
1333 * This function returns a guess to its caller about whether 'b' is OpenFlow
1334 * message that indicates that the controller has decided to control the
1335 * switch. It returns false if the message is one that a controller typically
1336 * uses to determine whether a switch is admissible, true if the message is one
1337 * that would typically be used only after the controller has admitted the
1338 * switch. */
064af421
BP
1339static bool
1340is_admitted_msg(const struct ofpbuf *b)
1341{
982697a4
BP
1342 enum ofptype type;
1343 enum ofperr error;
1344
6fd6ed71 1345 error = ofptype_decode(&type, b->data);
982697a4
BP
1346 if (error) {
1347 return false;
1348 }
1349
1350 switch (type) {
1351 case OFPTYPE_HELLO:
1352 case OFPTYPE_ERROR:
1353 case OFPTYPE_ECHO_REQUEST:
1354 case OFPTYPE_ECHO_REPLY:
1355 case OFPTYPE_FEATURES_REQUEST:
1356 case OFPTYPE_FEATURES_REPLY:
1357 case OFPTYPE_GET_CONFIG_REQUEST:
1358 case OFPTYPE_GET_CONFIG_REPLY:
1359 case OFPTYPE_SET_CONFIG:
c545d38d
JR
1360 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1361 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2e1ae200
JR
1362 case OFPTYPE_GET_ASYNC_REQUEST:
1363 case OFPTYPE_GET_ASYNC_REPLY:
261bd854
BP
1364 case OFPTYPE_GROUP_STATS_REQUEST:
1365 case OFPTYPE_GROUP_STATS_REPLY:
1366 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
1367 case OFPTYPE_GROUP_DESC_STATS_REPLY:
1368 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
1369 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
1370 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
1371 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922
BP
1372 case OFPTYPE_TABLE_DESC_REQUEST:
1373 case OFPTYPE_TABLE_DESC_REPLY:
982697a4
BP
1374 return false;
1375
1376 case OFPTYPE_PACKET_IN:
1377 case OFPTYPE_FLOW_REMOVED:
1378 case OFPTYPE_PORT_STATUS:
1379 case OFPTYPE_PACKET_OUT:
1380 case OFPTYPE_FLOW_MOD:
7395c052 1381 case OFPTYPE_GROUP_MOD:
982697a4 1382 case OFPTYPE_PORT_MOD:
918f2b82 1383 case OFPTYPE_TABLE_MOD:
9cae45dc 1384 case OFPTYPE_METER_MOD:
982697a4
BP
1385 case OFPTYPE_BARRIER_REQUEST:
1386 case OFPTYPE_BARRIER_REPLY:
1387 case OFPTYPE_DESC_STATS_REQUEST:
1388 case OFPTYPE_DESC_STATS_REPLY:
1389 case OFPTYPE_FLOW_STATS_REQUEST:
1390 case OFPTYPE_FLOW_STATS_REPLY:
1391 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1392 case OFPTYPE_AGGREGATE_STATS_REPLY:
1393 case OFPTYPE_TABLE_STATS_REQUEST:
1394 case OFPTYPE_TABLE_STATS_REPLY:
1395 case OFPTYPE_PORT_STATS_REQUEST:
1396 case OFPTYPE_PORT_STATS_REPLY:
1397 case OFPTYPE_QUEUE_STATS_REQUEST:
1398 case OFPTYPE_QUEUE_STATS_REPLY:
1399 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1400 case OFPTYPE_PORT_DESC_STATS_REPLY:
261bd854
BP
1401 case OFPTYPE_METER_STATS_REQUEST:
1402 case OFPTYPE_METER_STATS_REPLY:
1403 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
1404 case OFPTYPE_METER_CONFIG_STATS_REPLY:
1405 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
1406 case OFPTYPE_METER_FEATURES_STATS_REPLY:
982697a4
BP
1407 case OFPTYPE_ROLE_REQUEST:
1408 case OFPTYPE_ROLE_REPLY:
252f3411 1409 case OFPTYPE_ROLE_STATUS:
3c35db62 1410 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 1411 case OFPTYPE_TABLE_STATUS:
982697a4
BP
1412 case OFPTYPE_SET_FLOW_FORMAT:
1413 case OFPTYPE_FLOW_MOD_TABLE_ID:
1414 case OFPTYPE_SET_PACKET_IN_FORMAT:
1415 case OFPTYPE_FLOW_AGE:
1416 case OFPTYPE_SET_ASYNC_CONFIG:
1417 case OFPTYPE_SET_CONTROLLER_ID:
1418 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1419 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1420 case OFPTYPE_FLOW_MONITOR_CANCEL:
1421 case OFPTYPE_FLOW_MONITOR_PAUSED:
1422 case OFPTYPE_FLOW_MONITOR_RESUMED:
b58990a6
JR
1423 case OFPTYPE_BUNDLE_CONTROL:
1424 case OFPTYPE_BUNDLE_ADD_MESSAGE:
4e548ad9
ML
1425 case OFPTYPE_NXT_TLV_TABLE_MOD:
1426 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
1427 case OFPTYPE_NXT_TLV_TABLE_REPLY:
77ab5fd2 1428 case OFPTYPE_NXT_RESUME:
fb8f22c1
BY
1429 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
1430 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
1431 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
1432 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
2a7c4805 1433 case OFPTYPE_CT_FLUSH_ZONE:
982697a4
BP
1434 default:
1435 return true;
1436 }
064af421 1437}
07c8c80d
BP
1438
1439/* Returns true if 'rc' is currently logging information about connection
1440 * attempts, false if logging should be suppressed because 'rc' hasn't
1441 * successuflly connected in too long. */
1442static bool
1443rconn_logging_connection_attempts__(const struct rconn *rc)
9f5e8906 1444 OVS_REQUIRES(rc->mutex)
07c8c80d
BP
1445{
1446 return rc->backoff < rc->max_backoff;
1447}