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