Fix typo in comment.
[sliver-openvswitch.git] / lib / rconn.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "rconn.h"
36 #include <assert.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "buffer.h"
42 #include "poll-loop.h"
43 #include "ofp-print.h"
44 #include "sat-math.h"
45 #include "timeval.h"
46 #include "util.h"
47 #include "vconn.h"
48
49 #define THIS_MODULE VLM_rconn
50 #include "vlog.h"
51
52 #define STATES                                  \
53     STATE(VOID, 1 << 0)                         \
54     STATE(BACKOFF, 1 << 1)                      \
55     STATE(CONNECTING, 1 << 2)                   \
56     STATE(ACTIVE, 1 << 3)                       \
57     STATE(IDLE, 1 << 4)
58 enum state {
59 #define STATE(NAME, VALUE) S_##NAME = VALUE,
60     STATES
61 #undef STATE
62 };
63
64 static const char *
65 state_name(enum state state)
66 {
67     switch (state) {
68 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
69         STATES
70 #undef STATE
71     }
72     return "***ERROR***";
73 }
74
75 /* A reliable connection to an OpenFlow switch or controller.
76  *
77  * See the large comment in rconn.h for more information. */
78 struct rconn {
79     enum state state;
80     time_t state_entered;
81
82     struct vconn *vconn;
83     char *name;
84     bool reliable;
85
86     struct queue txq;
87
88     int backoff;
89     int max_backoff;
90     time_t backoff_deadline;
91     time_t last_received;
92     time_t last_connected;
93     unsigned int packets_sent;
94
95     /* These values are simply for statistics reporting, not used directly by
96      * anything internal to the rconn (or the secchan for that matter). */
97     unsigned int packets_received;
98     unsigned int n_attempted_connections, n_successful_connections;
99     time_t creation_time;
100     unsigned long int total_time_connected;
101
102     /* If we can't connect to the peer, it could be for any number of reasons.
103      * Usually, one would assume it is because the peer is not running or
104      * because the network is partitioned.  But it could also be because the
105      * network topology has changed, in which case the upper layer will need to
106      * reassess it (in particular, obtain a new IP address via DHCP and find
107      * the new location of the controller).  We set this flag when we suspect
108      * that this could be the case. */
109     bool questionable_connectivity;
110     time_t last_questioned;
111
112     /* Throughout this file, "probe" is shorthand for "inactivity probe".
113      * When nothing has been received from the peer for a while, we send out
114      * an echo request as an inactivity probe packet.  We should receive back
115      * a response. */
116     int probe_interval;         /* Secs of inactivity before sending probe. */
117 };
118
119 static unsigned int elapsed_in_this_state(const struct rconn *);
120 static unsigned int timeout(const struct rconn *);
121 static bool timed_out(const struct rconn *);
122 static void state_transition(struct rconn *, enum state);
123 static int try_send(struct rconn *);
124 static int reconnect(struct rconn *);
125 static void disconnect(struct rconn *, int error);
126 static void flush_queue(struct rconn *);
127 static void question_connectivity(struct rconn *);
128
129 /* Creates a new rconn, connects it (reliably) to 'name', and returns it. */
130 struct rconn *
131 rconn_new(const char *name, int inactivity_probe_interval, int max_backoff)
132 {
133     struct rconn *rc = rconn_create(inactivity_probe_interval, max_backoff);
134     rconn_connect(rc, name);
135     return rc;
136 }
137
138 /* Creates a new rconn, connects it (unreliably) to 'vconn', and returns it. */
139 struct rconn *
140 rconn_new_from_vconn(const char *name, struct vconn *vconn) 
141 {
142     struct rconn *rc = rconn_create(60, 0);
143     rconn_connect_unreliably(rc, name, vconn);
144     return rc;
145 }
146
147 /* Creates and returns a new rconn.
148  *
149  * 'probe_interval' is a number of seconds.  If the interval passes once
150  * without an OpenFlow message being received from the peer, the rconn sends
151  * out an "echo request" message.  If the interval passes again without a
152  * message being received, the rconn disconnects and re-connects to the peer.
153  * Setting 'probe_interval' to 0 disables this behavior.
154  *
155  * 'max_backoff' is the maximum number of seconds between attempts to connect
156  * to the peer.  The actual interval starts at 1 second and doubles on each
157  * failure until it reaches 'max_backoff'.  If 0 is specified, the default of
158  * 60 seconds is used. */
159 struct rconn *
160 rconn_create(int probe_interval, int max_backoff)
161 {
162     struct rconn *rc = xcalloc(1, sizeof *rc);
163
164     rc->state = S_VOID;
165     rc->state_entered = time_now();
166
167     rc->vconn = NULL;
168     rc->name = xstrdup("void");
169     rc->reliable = false;
170
171     queue_init(&rc->txq);
172
173     rc->backoff = 0;
174     rc->max_backoff = max_backoff ? max_backoff : 60;
175     rc->backoff_deadline = TIME_MIN;
176     rc->last_received = time_now();
177     rc->last_connected = time_now();
178
179     rc->packets_sent = 0;
180
181     rc->packets_received = 0;
182     rc->n_attempted_connections = 0;
183     rc->n_successful_connections = 0;
184     rc->creation_time = time_now();
185     rc->total_time_connected = 0;
186
187     rc->questionable_connectivity = false;
188     rc->last_questioned = time_now();
189
190     rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
191
192     return rc;
193 }
194
195 int
196 rconn_connect(struct rconn *rc, const char *name)
197 {
198     rconn_disconnect(rc);
199     free(rc->name);
200     rc->name = xstrdup(name);
201     rc->reliable = true;
202     return reconnect(rc);
203 }
204
205 void
206 rconn_connect_unreliably(struct rconn *rc,
207                          const char *name, struct vconn *vconn)
208 {
209     assert(vconn != NULL);
210     rconn_disconnect(rc);
211     free(rc->name);
212     rc->name = xstrdup(name);
213     rc->reliable = false;
214     rc->vconn = vconn;
215     rc->last_connected = time_now();
216     state_transition(rc, S_ACTIVE);
217 }
218
219 void
220 rconn_disconnect(struct rconn *rc)
221 {
222     if (rc->vconn) {
223         vconn_close(rc->vconn);
224         rc->vconn = NULL;
225     }
226     free(rc->name);
227     rc->name = xstrdup("void");
228     rc->reliable = false;
229
230     rc->backoff = 0;
231     rc->backoff_deadline = TIME_MIN;
232
233     state_transition(rc, S_VOID);
234 }
235
236 /* Disconnects 'rc' and frees the underlying storage. */
237 void
238 rconn_destroy(struct rconn *rc)
239 {
240     if (rc) {
241         free(rc->name);
242         vconn_close(rc->vconn);
243         flush_queue(rc);
244         queue_destroy(&rc->txq);
245         free(rc);
246     }
247 }
248
249 static unsigned int
250 timeout_VOID(const struct rconn *rc)
251 {
252     return UINT_MAX;
253 }
254
255 static void
256 run_VOID(struct rconn *rc)
257 {
258     /* Nothing to do. */
259 }
260
261 static int
262 reconnect(struct rconn *rc)
263 {
264     int retval;
265
266     VLOG_WARN("%s: connecting...", rc->name);
267     rc->n_attempted_connections++;
268     retval = vconn_open(rc->name, &rc->vconn);
269     if (!retval) {
270         rc->backoff_deadline = time_now() + rc->backoff;
271         state_transition(rc, S_CONNECTING);
272     } else {
273         VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(retval));
274         disconnect(rc, 0);
275     }
276     return retval;
277 }
278
279 static unsigned int
280 timeout_BACKOFF(const struct rconn *rc)
281 {
282     return rc->backoff;
283 }
284
285 static void
286 run_BACKOFF(struct rconn *rc)
287 {
288     if (timed_out(rc)) {
289         reconnect(rc);
290     }
291 }
292
293 static unsigned int
294 timeout_CONNECTING(const struct rconn *rc)
295 {
296     return MAX(1, rc->backoff);
297 }
298
299 static void
300 run_CONNECTING(struct rconn *rc)
301 {
302     int retval = vconn_connect(rc->vconn);
303     if (!retval) {
304         VLOG_WARN("%s: connected", rc->name);
305         rc->n_successful_connections++;
306         if (vconn_is_passive(rc->vconn)) {
307             error(0, "%s: passive vconn not supported", rc->name);
308             state_transition(rc, S_VOID);
309         } else {
310             state_transition(rc, S_ACTIVE);
311             rc->last_connected = rc->state_entered;
312         }
313     } else if (retval != EAGAIN) {
314         VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(retval));
315         disconnect(rc, retval);
316     } else if (timed_out(rc)) {
317         VLOG_WARN("%s: connection timed out", rc->name);
318         rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
319         disconnect(rc, 0);
320     }
321 }
322
323 static void
324 do_tx_work(struct rconn *rc)
325 {
326     if (!rc->txq.n) {
327         return;
328     }
329     while (rc->txq.n > 0) {
330         int error = try_send(rc);
331         if (error) {
332             break;
333         }
334     }
335     if (!rc->txq.n) {
336         poll_immediate_wake();
337     }
338 }
339
340 static unsigned int
341 timeout_ACTIVE(const struct rconn *rc)
342 {
343     if (rc->probe_interval) {
344         unsigned int base = MAX(rc->last_received, rc->state_entered);
345         unsigned int arg = base + rc->probe_interval - rc->state_entered;
346         return arg;
347     }
348     return UINT_MAX;
349 }
350
351 static void
352 run_ACTIVE(struct rconn *rc)
353 {
354     if (timed_out(rc)) {
355         unsigned int base = MAX(rc->last_received, rc->state_entered);
356         rconn_send(rc, make_echo_request(), NULL);
357         VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
358                  rc->name, (unsigned int) (time_now() - base));
359         state_transition(rc, S_IDLE);
360         return;
361     }
362
363     do_tx_work(rc);
364 }
365
366 static unsigned int
367 timeout_IDLE(const struct rconn *rc)
368 {
369     return rc->probe_interval;
370 }
371
372 static void
373 run_IDLE(struct rconn *rc)
374 {
375     if (timed_out(rc)) {
376         question_connectivity(rc);
377         VLOG_ERR("%s: no response to inactivity probe after %u "
378                  "seconds, disconnecting",
379                  rc->name, elapsed_in_this_state(rc));
380         disconnect(rc, 0);
381     } else {
382         do_tx_work(rc);
383     }
384 }
385
386 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
387  * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
388  * connected, attempts to send packets in the send queue, if any. */
389 void
390 rconn_run(struct rconn *rc)
391 {
392     int old_state;
393     do {
394         old_state = rc->state;
395         switch (rc->state) {
396 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
397             STATES
398 #undef STATE
399         default:
400             NOT_REACHED();
401         }
402     } while (rc->state != old_state);
403 }
404
405 /* Causes the next call to poll_block() to wake up when rconn_run() should be
406  * called on 'rc'. */
407 void
408 rconn_run_wait(struct rconn *rc)
409 {
410     unsigned int timeo = timeout(rc);
411     if (timeo != UINT_MAX) {
412         unsigned int expires = sat_add(rc->state_entered, timeo);
413         unsigned int remaining = sat_sub(expires, time_now());
414         poll_timer_wait(sat_mul(remaining, 1000));
415     }
416
417     if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {
418         vconn_wait(rc->vconn, WAIT_SEND);
419     }
420 }
421
422 /* Attempts to receive a packet from 'rc'.  If successful, returns the packet;
423  * otherwise, returns a null pointer.  The caller is responsible for freeing
424  * the packet (with buffer_delete()). */
425 struct buffer *
426 rconn_recv(struct rconn *rc)
427 {
428     if (rc->state & (S_ACTIVE | S_IDLE)) {
429         struct buffer *buffer;
430         int error = vconn_recv(rc->vconn, &buffer);
431         if (!error) {
432             rc->last_received = time_now();
433             rc->packets_received++;
434             if (rc->state == S_IDLE) {
435                 state_transition(rc, S_ACTIVE);
436             }
437             return buffer;
438         } else if (error != EAGAIN) {
439             disconnect(rc, error);
440         }
441     }
442     return NULL;
443 }
444
445 /* Causes the next call to poll_block() to wake up when a packet may be ready
446  * to be received by vconn_recv() on 'rc'.  */
447 void
448 rconn_recv_wait(struct rconn *rc)
449 {
450     if (rc->vconn) {
451         vconn_wait(rc->vconn, WAIT_RECV);
452     }
453 }
454
455 /* Sends 'b' on 'rc'.  Returns 0 if successful (in which case 'b' is
456  * destroyed), or ENOTCONN if 'rc' is not currently connected (in which case
457  * the caller retains ownership of 'b').
458  *
459  * If 'n_queued' is non-null, then '*n_queued' will be incremented while the
460  * packet is in flight, then decremented when it has been sent (or discarded
461  * due to disconnection).  Because 'b' may be sent (or discarded) before this
462  * function returns, the caller may not be able to observe any change in
463  * '*n_queued'.
464  *
465  * There is no rconn_send_wait() function: an rconn has a send queue that it
466  * takes care of sending if you call rconn_run(), which will have the side
467  * effect of waking up poll_block(). */
468 int
469 rconn_send(struct rconn *rc, struct buffer *b, int *n_queued)
470 {
471     if (rconn_is_connected(rc)) {
472         b->private = n_queued;
473         if (n_queued) {
474             ++*n_queued;
475         }
476         queue_push_tail(&rc->txq, b);
477         if (rc->txq.n == 1) {
478             try_send(rc);
479         }
480         return 0;
481     } else {
482         return ENOTCONN;
483     }
484 }
485
486 /* Sends 'b' on 'rc'.  Increments '*n_queued' while the packet is in flight; it
487  * will be decremented when it has been sent (or discarded due to
488  * disconnection).  Returns 0 if successful, EAGAIN if '*n_queued' is already
489  * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
490  * connected.  Regardless of return value, 'b' is destroyed.
491  *
492  * Because 'b' may be sent (or discarded) before this function returns, the
493  * caller may not be able to observe any change in '*n_queued'.
494  *
495  * There is no rconn_send_wait() function: an rconn has a send queue that it
496  * takes care of sending if you call rconn_run(), which will have the side
497  * effect of waking up poll_block(). */
498 int
499 rconn_send_with_limit(struct rconn *rc, struct buffer *b,
500                       int *n_queued, int queue_limit)
501 {
502     int retval;
503     retval = *n_queued >= queue_limit ? EAGAIN : rconn_send(rc, b, n_queued);
504     if (retval) {
505         buffer_delete(b);
506     }
507     return retval;
508 }
509
510 /* Returns the total number of packets successfully sent on the underlying
511  * vconn.  A packet is not counted as sent while it is still queued in the
512  * rconn, only when it has been successfuly passed to the vconn.  */
513 unsigned int
514 rconn_packets_sent(const struct rconn *rc)
515 {
516     return rc->packets_sent;
517 }
518
519 /* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */
520 const char *
521 rconn_get_name(const struct rconn *rc)
522 {
523     return rc->name;
524 }
525
526 /* Returns true if 'rconn' is connected or in the process of reconnecting,
527  * false if 'rconn' is disconnected and will not reconnect on its own. */
528 bool
529 rconn_is_alive(const struct rconn *rconn)
530 {
531     return rconn->state != S_VOID;
532 }
533
534 /* Returns true if 'rconn' is connected, false otherwise. */
535 bool
536 rconn_is_connected(const struct rconn *rconn)
537 {
538     return rconn->state & (S_ACTIVE | S_IDLE);
539 }
540
541 /* Returns 0 if 'rconn' is connected, otherwise the number of seconds that it
542  * has been disconnected. */
543 int
544 rconn_disconnected_duration(const struct rconn *rconn)
545 {
546     return rconn_is_connected(rconn) ? 0 : time_now() - rconn->last_received;
547 }
548
549 /* Returns the IP address of the peer, or 0 if the peer is not connected over
550  * an IP-based protocol or if its IP address is not known. */
551 uint32_t
552 rconn_get_ip(const struct rconn *rconn) 
553 {
554     return rconn->vconn ? vconn_get_ip(rconn->vconn) : 0;
555 }
556
557 /* If 'rconn' can't connect to the peer, it could be for any number of reasons.
558  * Usually, one would assume it is because the peer is not running or because
559  * the network is partitioned.  But it could also be because the network
560  * topology has changed, in which case the upper layer will need to reassess it
561  * (in particular, obtain a new IP address via DHCP and find the new location
562  * of the controller).  When this appears that this might be the case, this
563  * function returns true.  It also clears the questionability flag and prevents
564  * it from being set again for some time. */
565 bool
566 rconn_is_connectivity_questionable(struct rconn *rconn)
567 {
568     bool questionable = rconn->questionable_connectivity;
569     rconn->questionable_connectivity = false;
570     return questionable;
571 }
572
573 /* Returns the total number of packets successfully received by the underlying
574  * vconn.  */
575 unsigned int
576 rconn_packets_received(const struct rconn *rc)
577 {
578     return rc->packets_received;
579 }
580
581 /* Returns a string representing the internal state of 'rc'.  The caller must
582  * not modify or free the string. */
583 const char *
584 rconn_get_state(const struct rconn *rc)
585 {
586     return state_name(rc->state);
587 }
588
589 /* Returns the number of connection attempts made by 'rc', including any
590  * ongoing attempt that has not yet succeeded or failed. */
591 unsigned int
592 rconn_get_attempted_connections(const struct rconn *rc)
593 {
594     return rc->n_attempted_connections;
595 }
596
597 /* Returns the number of successful connection attempts made by 'rc'. */
598 unsigned int
599 rconn_get_successful_connections(const struct rconn *rc)
600 {
601     return rc->n_successful_connections;
602 }
603
604 /* Returns the time at which the last successful connection was made by
605  * 'rc'. */
606 time_t
607 rconn_get_last_connection(const struct rconn *rc)
608 {
609     return rc->last_connected;
610 }
611
612 /* Returns the time at which 'rc' was created. */
613 time_t
614 rconn_get_creation_time(const struct rconn *rc)
615 {
616     return rc->creation_time;
617 }
618
619 /* Returns the approximate number of seconds that 'rc' has been connected. */
620 unsigned long int
621 rconn_get_total_time_connected(const struct rconn *rc)
622 {
623     return (rc->total_time_connected
624             + (rconn_is_connected(rc) ? elapsed_in_this_state(rc) : 0));
625 }
626
627 /* Returns the current amount of backoff, in seconds.  This is the amount of
628  * time after which the rconn will transition from BACKOFF to CONNECTING. */
629 int
630 rconn_get_backoff(const struct rconn *rc)
631 {
632     return rc->backoff;
633 }
634
635 /* Returns the number of seconds spent in this state so far. */
636 unsigned int
637 rconn_get_state_elapsed(const struct rconn *rc)
638 {
639     return elapsed_in_this_state(rc);
640 }
641 \f
642 /* Tries to send a packet from 'rc''s send buffer.  Returns 0 if successful,
643  * otherwise a positive errno value. */
644 static int
645 try_send(struct rconn *rc)
646 {
647     int retval = 0;
648     struct buffer *next = rc->txq.head->next;
649     int *n_queued = rc->txq.head->private;
650     retval = vconn_send(rc->vconn, rc->txq.head);
651     if (retval) {
652         if (retval != EAGAIN) {
653             disconnect(rc, retval);
654         }
655         return retval;
656     }
657     rc->packets_sent++;
658     if (n_queued) {
659         --*n_queued;
660     }
661     queue_advance_head(&rc->txq, next);
662     return 0;
663 }
664
665 /* Disconnects 'rc'.  'error' is used only for logging purposes.  If it is
666  * nonzero, then it should be EOF to indicate the connection was closed by the
667  * peer in a normal fashion or a positive errno value. */
668 static void
669 disconnect(struct rconn *rc, int error)
670 {
671     if (rc->reliable) {
672         time_t now = time_now();
673
674         if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
675             if (error > 0) {
676                 VLOG_WARN("%s: connection dropped (%s)",
677                           rc->name, strerror(error));
678             } else if (error == EOF) {
679                 if (rc->reliable) {
680                     VLOG_WARN("%s: connection closed", rc->name);
681                 }
682             } else {
683                 VLOG_WARN("%s: connection dropped", rc->name);
684             }
685             vconn_close(rc->vconn);
686             rc->vconn = NULL;
687             flush_queue(rc);
688         }
689
690         if (now >= rc->backoff_deadline) {
691             rc->backoff = 1;
692         } else {
693             rc->backoff = MIN(rc->max_backoff, MAX(1, 2 * rc->backoff));
694             VLOG_WARN("%s: waiting %d seconds before reconnect\n",
695                       rc->name, rc->backoff);
696         }
697         rc->backoff_deadline = now + rc->backoff;
698         state_transition(rc, S_BACKOFF);
699         if (now - rc->last_connected > 60) {
700             question_connectivity(rc);
701         }
702     } else {
703         rconn_disconnect(rc);
704     }
705 }
706
707 /* Drops all the packets from 'rc''s send queue and decrements their queue
708  * counts. */
709 static void
710 flush_queue(struct rconn *rc)
711 {
712     if (!rc->txq.n) {
713         return;
714     }
715     while (rc->txq.n > 0) {
716         struct buffer *b = queue_pop_head(&rc->txq);
717         int *n_queued = b->private;
718         if (n_queued) {
719             --*n_queued;
720         }
721         buffer_delete(b);
722     }
723     poll_immediate_wake();
724 }
725
726 static unsigned int
727 elapsed_in_this_state(const struct rconn *rc)
728 {
729     return time_now() - rc->state_entered;
730 }
731
732 static unsigned int
733 timeout(const struct rconn *rc)
734 {
735     switch (rc->state) {
736 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
737         STATES
738 #undef STATE
739     default:
740         NOT_REACHED();
741     }
742 }
743
744 static bool
745 timed_out(const struct rconn *rc)
746 {
747     return time_now() >= sat_add(rc->state_entered, timeout(rc));
748 }
749
750 static void
751 state_transition(struct rconn *rc, enum state state)
752 {
753     if (rconn_is_connected(rc)) {
754         rc->total_time_connected += elapsed_in_this_state(rc);
755     }
756     VLOG_DBG("%s: entering %s", rc->name, state_name(state));
757     rc->state = state;
758     rc->state_entered = time_now();
759 }
760
761 static void
762 question_connectivity(struct rconn *rc) 
763 {
764     time_t now = time_now();
765     if (now - rc->last_questioned > 60) {
766         rc->questionable_connectivity = true;
767         rc->last_questioned = now;
768     }
769 }