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