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