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