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