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