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