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