2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "openflow/openflow.h"
28 #include "poll-loop.h"
35 VLOG_DEFINE_THIS_MODULE(rconn);
37 COVERAGE_DEFINE(rconn_discarded);
38 COVERAGE_DEFINE(rconn_overflow);
39 COVERAGE_DEFINE(rconn_queued);
40 COVERAGE_DEFINE(rconn_sent);
42 /* The connection states have the following meanings:
44 * - S_VOID: No connection information is configured.
46 * - S_BACKOFF: Waiting for a period of time before reconnecting.
48 * - S_CONNECTING: A connection attempt is in progress and has not yet
49 * succeeded or failed.
51 * - S_ACTIVE: A connection has been established and appears to be healthy.
53 * - S_IDLE: A connection has been established but has been idle for some
54 * time. An echo request has been sent, but no reply has yet been
57 * - S_DISCONNECTED: An unreliable connection has disconnected and cannot be
58 * automatically retried.
62 STATE(BACKOFF, 1 << 1) \
63 STATE(CONNECTING, 1 << 2) \
64 STATE(ACTIVE, 1 << 3) \
66 STATE(DISCONNECTED, 1 << 5)
68 #define STATE(NAME, VALUE) S_##NAME = VALUE,
74 state_name(enum state state)
77 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
84 /* A reliable connection to an OpenFlow switch or controller.
86 * See the large comment in rconn.h for more information. */
88 struct ovs_mutex mutex;
94 char *name; /* Human-readable descriptive name. */
95 char *target; /* vconn name, passed to vconn_open(). */
98 struct list txq; /* Contains "struct ofpbuf"s. */
102 time_t backoff_deadline;
103 time_t last_connected;
104 time_t last_disconnected;
105 unsigned int packets_sent;
109 /* In S_ACTIVE and S_IDLE, probably_admitted reports whether we believe
110 * that the peer has made a (positive) admission control decision on our
111 * connection. If we have not yet been (probably) admitted, then the
112 * connection does not reset the timer used for deciding whether the switch
113 * should go into fail-open mode.
115 * last_admitted reports the last time we believe such a positive admission
116 * control decision was made. */
117 bool probably_admitted;
118 time_t last_admitted;
120 /* These values are simply for statistics reporting, not used directly by
121 * anything internal to the rconn (or ofproto for that matter). */
122 unsigned int packets_received;
123 unsigned int n_attempted_connections, n_successful_connections;
124 time_t creation_time;
125 unsigned long int total_time_connected;
127 /* Throughout this file, "probe" is shorthand for "inactivity probe". When
128 * no activity has been observed from the peer for a while, we send out an
129 * echo request as an inactivity probe packet. We should receive back a
132 * "Activity" is defined as either receiving an OpenFlow message from the
133 * peer or successfully sending a message that had been in 'txq'. */
134 int probe_interval; /* Secs of inactivity before sending probe. */
135 time_t last_activity; /* Last time we saw some activity. */
139 /* Messages sent or received are copied to the monitor connections. */
140 #define MAXIMUM_MONITORS 8
141 struct vconn *monitors[MAXIMUM_MONITORS];
144 uint32_t allowed_versions;
147 uint32_t rconn_get_allowed_versions(const struct rconn *rconn)
149 return rconn->allowed_versions;
152 static unsigned int elapsed_in_this_state(const struct rconn *rc)
153 OVS_REQUIRES(rc->mutex);
154 static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
155 static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
156 static void state_transition(struct rconn *rc, enum state)
157 OVS_REQUIRES(rc->mutex);
158 static void rconn_set_target__(struct rconn *rc,
159 const char *target, const char *name)
160 OVS_REQUIRES(rc->mutex);
161 static int rconn_send__(struct rconn *rc, struct ofpbuf *,
162 struct rconn_packet_counter *)
163 OVS_REQUIRES(rc->mutex);
164 static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex);
165 static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex);
166 static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
167 static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex);
168 static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
169 static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex);
170 static void close_monitor(struct rconn *rc, size_t idx, int retval)
171 OVS_REQUIRES(rc->mutex);
172 static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
173 static bool is_connected_state(enum state);
174 static bool is_admitted_msg(const struct ofpbuf *);
175 static bool rconn_logging_connection_attempts__(const struct rconn *rc)
176 OVS_REQUIRES(rc->mutex);
177 static int rconn_get_version__(const struct rconn *rconn)
178 OVS_REQUIRES(rconn->mutex);
180 /* The following prototypes duplicate those in rconn.h, but there we weren't
181 * able to add the OVS_EXCLUDED annotations because the definition of struct
182 * rconn was not visible. */
184 void rconn_set_max_backoff(struct rconn *rc, int max_backoff)
185 OVS_EXCLUDED(rc->mutex);
186 void rconn_connect(struct rconn *rc, const char *target, const char *name)
187 OVS_EXCLUDED(rc->mutex);
188 void rconn_connect_unreliably(struct rconn *rc,
189 struct vconn *vconn, const char *name)
190 OVS_EXCLUDED(rc->mutex);
191 void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
192 void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
193 void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
194 void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
195 struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
196 void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
197 int rconn_send(struct rconn *rc, struct ofpbuf *b,
198 struct rconn_packet_counter *counter)
199 OVS_EXCLUDED(rc->mutex);
200 int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
201 struct rconn_packet_counter *counter,
203 OVS_EXCLUDED(rc->mutex);
204 void rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
205 OVS_EXCLUDED(rc->mutex);
206 void rconn_set_name(struct rconn *rc, const char *new_name)
207 OVS_EXCLUDED(rc->mutex);
208 bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
209 int rconn_failure_duration(const struct rconn *rconn)
210 OVS_EXCLUDED(rconn->mutex);
211 ovs_be16 rconn_get_local_port(const struct rconn *rconn)
212 OVS_EXCLUDED(rconn->mutex);
213 int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
214 unsigned int rconn_count_txqlen(const struct rconn *rc)
215 OVS_EXCLUDED(rc->mutex);
218 /* Creates and returns a new rconn.
220 * 'probe_interval' is a number of seconds. If the interval passes once
221 * without an OpenFlow message being received from the peer, the rconn sends
222 * out an "echo request" message. If the interval passes again without a
223 * message being received, the rconn disconnects and re-connects to the peer.
224 * Setting 'probe_interval' to 0 disables this behavior.
226 * 'max_backoff' is the maximum number of seconds between attempts to connect
227 * to the peer. The actual interval starts at 1 second and doubles on each
228 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
231 * The new rconn is initially unconnected. Use rconn_connect() or
232 * rconn_connect_unreliably() to connect it.
234 * Connections made by the rconn will automatically negotiate an OpenFlow
235 * protocol version acceptable to both peers on the connection. The version
236 * negotiated will be one of those in the 'allowed_versions' bitmap: version
237 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying
238 * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.)
241 rconn_create(int probe_interval, int max_backoff, uint8_t dscp,
242 uint32_t allowed_versions)
244 struct rconn *rc = xzalloc(sizeof *rc);
246 ovs_mutex_init(&rc->mutex);
249 rc->state_entered = time_now();
252 rc->name = xstrdup("void");
253 rc->target = xstrdup("void");
254 rc->reliable = false;
259 rc->max_backoff = max_backoff ? max_backoff : 8;
260 rc->backoff_deadline = TIME_MIN;
261 rc->last_connected = TIME_MIN;
262 rc->last_disconnected = TIME_MIN;
265 rc->packets_sent = 0;
267 rc->probably_admitted = false;
268 rc->last_admitted = time_now();
270 rc->packets_received = 0;
271 rc->n_attempted_connections = 0;
272 rc->n_successful_connections = 0;
273 rc->creation_time = time_now();
274 rc->total_time_connected = 0;
276 rc->last_activity = time_now();
278 rconn_set_probe_interval(rc, probe_interval);
279 rconn_set_dscp(rc, dscp);
282 rc->allowed_versions = allowed_versions;
288 rconn_set_max_backoff(struct rconn *rc, int max_backoff)
289 OVS_EXCLUDED(rc->mutex)
291 ovs_mutex_lock(&rc->mutex);
292 rc->max_backoff = MAX(1, max_backoff);
293 if (rc->state == S_BACKOFF && rc->backoff > max_backoff) {
294 rc->backoff = max_backoff;
295 if (rc->backoff_deadline > time_now() + max_backoff) {
296 rc->backoff_deadline = time_now() + max_backoff;
299 ovs_mutex_unlock(&rc->mutex);
303 rconn_get_max_backoff(const struct rconn *rc)
305 return rc->max_backoff;
309 rconn_set_dscp(struct rconn *rc, uint8_t dscp)
315 rconn_get_dscp(const struct rconn *rc)
321 rconn_set_probe_interval(struct rconn *rc, int probe_interval)
323 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
327 rconn_get_probe_interval(const struct rconn *rc)
329 return rc->probe_interval;
332 /* Drops any existing connection on 'rc', then sets up 'rc' to connect to
333 * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
334 * target in a form acceptable to vconn_open().
336 * If 'name' is nonnull, then it is used in log messages in place of 'target'.
337 * It should presumably give more information to a human reader than 'target',
338 * but it need not be acceptable to vconn_open(). */
340 rconn_connect(struct rconn *rc, const char *target, const char *name)
341 OVS_EXCLUDED(rc->mutex)
343 ovs_mutex_lock(&rc->mutex);
344 rconn_disconnect__(rc);
345 rconn_set_target__(rc, target, name);
348 ovs_mutex_unlock(&rc->mutex);
351 /* Drops any existing connection on 'rc', then configures 'rc' to use
352 * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
355 * By default, the target obtained from vconn_get_name(vconn) is used in log
356 * messages. If 'name' is nonnull, then it is used instead. It should
357 * presumably give more information to a human reader than the target, but it
358 * need not be acceptable to vconn_open(). */
360 rconn_connect_unreliably(struct rconn *rc,
361 struct vconn *vconn, const char *name)
362 OVS_EXCLUDED(rc->mutex)
364 ovs_assert(vconn != NULL);
366 ovs_mutex_lock(&rc->mutex);
367 rconn_disconnect__(rc);
368 rconn_set_target__(rc, vconn_get_name(vconn), name);
369 rc->reliable = false;
371 rc->last_connected = time_now();
372 state_transition(rc, S_ACTIVE);
373 ovs_mutex_unlock(&rc->mutex);
376 /* If 'rc' is connected, forces it to drop the connection and reconnect. */
378 rconn_reconnect(struct rconn *rc)
379 OVS_EXCLUDED(rc->mutex)
381 ovs_mutex_lock(&rc->mutex);
382 if (rc->state & (S_ACTIVE | S_IDLE)) {
383 VLOG_INFO("%s: disconnecting", rc->name);
386 ovs_mutex_unlock(&rc->mutex);
390 rconn_disconnect__(struct rconn *rc)
391 OVS_REQUIRES(rc->mutex)
393 if (rc->state != S_VOID) {
395 vconn_close(rc->vconn);
398 rconn_set_target__(rc, "void", NULL);
399 rc->reliable = false;
402 rc->backoff_deadline = TIME_MIN;
404 state_transition(rc, S_VOID);
409 rconn_disconnect(struct rconn *rc)
410 OVS_EXCLUDED(rc->mutex)
412 ovs_mutex_lock(&rc->mutex);
413 rconn_disconnect__(rc);
414 ovs_mutex_unlock(&rc->mutex);
417 /* Disconnects 'rc' and frees the underlying storage. */
419 rconn_destroy(struct rconn *rc)
424 ovs_mutex_lock(&rc->mutex);
427 vconn_close(rc->vconn);
429 ofpbuf_list_delete(&rc->txq);
430 for (i = 0; i < rc->n_monitors; i++) {
431 vconn_close(rc->monitors[i]);
433 ovs_mutex_unlock(&rc->mutex);
434 ovs_mutex_destroy(&rc->mutex);
441 timeout_VOID(const struct rconn *rc OVS_UNUSED)
442 OVS_REQUIRES(rc->mutex)
448 run_VOID(struct rconn *rc OVS_UNUSED)
449 OVS_REQUIRES(rc->mutex)
455 reconnect(struct rconn *rc)
456 OVS_REQUIRES(rc->mutex)
460 if (rconn_logging_connection_attempts__(rc)) {
461 VLOG_INFO("%s: connecting...", rc->name);
463 rc->n_attempted_connections++;
464 retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp,
467 rc->backoff_deadline = time_now() + rc->backoff;
468 state_transition(rc, S_CONNECTING);
470 VLOG_WARN("%s: connection failed (%s)",
471 rc->name, ovs_strerror(retval));
472 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
473 disconnect(rc, retval);
478 timeout_BACKOFF(const struct rconn *rc)
479 OVS_REQUIRES(rc->mutex)
485 run_BACKOFF(struct rconn *rc)
486 OVS_REQUIRES(rc->mutex)
494 timeout_CONNECTING(const struct rconn *rc)
495 OVS_REQUIRES(rc->mutex)
497 return MAX(1, rc->backoff);
501 run_CONNECTING(struct rconn *rc)
502 OVS_REQUIRES(rc->mutex)
504 int retval = vconn_connect(rc->vconn);
506 VLOG_INFO("%s: connected", rc->name);
507 rc->n_successful_connections++;
508 state_transition(rc, S_ACTIVE);
509 rc->last_connected = rc->state_entered;
510 } else if (retval != EAGAIN) {
511 if (rconn_logging_connection_attempts__(rc)) {
512 VLOG_INFO("%s: connection failed (%s)",
513 rc->name, ovs_strerror(retval));
515 disconnect(rc, retval);
516 } else if (timed_out(rc)) {
517 if (rconn_logging_connection_attempts__(rc)) {
518 VLOG_INFO("%s: connection timed out", rc->name);
520 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
521 disconnect(rc, ETIMEDOUT);
526 do_tx_work(struct rconn *rc)
527 OVS_REQUIRES(rc->mutex)
529 if (list_is_empty(&rc->txq)) {
532 while (!list_is_empty(&rc->txq)) {
533 int error = try_send(rc);
537 rc->last_activity = time_now();
539 if (list_is_empty(&rc->txq)) {
540 poll_immediate_wake();
545 timeout_ACTIVE(const struct rconn *rc)
546 OVS_REQUIRES(rc->mutex)
548 if (rc->probe_interval) {
549 unsigned int base = MAX(rc->last_activity, rc->state_entered);
550 unsigned int arg = base + rc->probe_interval - rc->state_entered;
557 run_ACTIVE(struct rconn *rc)
558 OVS_REQUIRES(rc->mutex)
561 unsigned int base = MAX(rc->last_activity, rc->state_entered);
564 VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
565 rc->name, (unsigned int) (time_now() - base));
567 version = rconn_get_version__(rc);
568 ovs_assert(version >= 0 && version <= 0xff);
570 /* Ordering is important here: rconn_send() can transition to BACKOFF,
571 * and we don't want to transition back to IDLE if so, because then we
572 * can end up queuing a packet with vconn == NULL and then *boom*. */
573 state_transition(rc, S_IDLE);
574 rconn_send__(rc, make_echo_request(version), NULL);
582 timeout_IDLE(const struct rconn *rc)
583 OVS_REQUIRES(rc->mutex)
585 return rc->probe_interval;
589 run_IDLE(struct rconn *rc)
590 OVS_REQUIRES(rc->mutex)
593 VLOG_ERR("%s: no response to inactivity probe after %u "
594 "seconds, disconnecting",
595 rc->name, elapsed_in_this_state(rc));
596 disconnect(rc, ETIMEDOUT);
603 timeout_DISCONNECTED(const struct rconn *rc OVS_UNUSED)
604 OVS_REQUIRES(rc->mutex)
610 run_DISCONNECTED(struct rconn *rc OVS_UNUSED)
611 OVS_REQUIRES(rc->mutex)
616 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
617 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
618 * connected, attempts to send packets in the send queue, if any. */
620 rconn_run(struct rconn *rc)
621 OVS_EXCLUDED(rc->mutex)
626 ovs_mutex_lock(&rc->mutex);
630 vconn_run(rc->vconn);
632 error = vconn_get_status(rc->vconn);
634 report_error(rc, error);
635 disconnect(rc, error);
638 for (i = 0; i < rc->n_monitors; ) {
642 vconn_run(rc->monitors[i]);
644 /* Drain any stray message that came in on the monitor connection. */
645 retval = vconn_recv(rc->monitors[i], &msg);
648 } else if (retval != EAGAIN) {
649 close_monitor(rc, i, retval);
656 old_state = rc->state;
658 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
664 } while (rc->state != old_state);
665 ovs_mutex_unlock(&rc->mutex);
668 /* Causes the next call to poll_block() to wake up when rconn_run() should be
671 rconn_run_wait(struct rconn *rc)
672 OVS_EXCLUDED(rc->mutex)
677 ovs_mutex_lock(&rc->mutex);
679 vconn_run_wait(rc->vconn);
680 if ((rc->state & (S_ACTIVE | S_IDLE)) && !list_is_empty(&rc->txq)) {
681 vconn_wait(rc->vconn, WAIT_SEND);
684 for (i = 0; i < rc->n_monitors; i++) {
685 vconn_run_wait(rc->monitors[i]);
686 vconn_recv_wait(rc->monitors[i]);
690 if (timeo != UINT_MAX) {
691 long long int expires = sat_add(rc->state_entered, timeo);
692 poll_timer_wait_until(expires * 1000);
694 ovs_mutex_unlock(&rc->mutex);
697 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
698 * otherwise, returns a null pointer. The caller is responsible for freeing
699 * the packet (with ofpbuf_delete()). */
701 rconn_recv(struct rconn *rc)
702 OVS_EXCLUDED(rc->mutex)
704 struct ofpbuf *buffer = NULL;
706 ovs_mutex_lock(&rc->mutex);
707 if (rc->state & (S_ACTIVE | S_IDLE)) {
708 int error = vconn_recv(rc->vconn, &buffer);
710 copy_to_monitor(rc, buffer);
711 if (rc->probably_admitted || is_admitted_msg(buffer)
712 || time_now() - rc->last_connected >= 30) {
713 rc->probably_admitted = true;
714 rc->last_admitted = time_now();
716 rc->last_activity = time_now();
717 rc->packets_received++;
718 if (rc->state == S_IDLE) {
719 state_transition(rc, S_ACTIVE);
721 } else if (error != EAGAIN) {
722 report_error(rc, error);
723 disconnect(rc, error);
726 ovs_mutex_unlock(&rc->mutex);
731 /* Causes the next call to poll_block() to wake up when a packet may be ready
732 * to be received by vconn_recv() on 'rc'. */
734 rconn_recv_wait(struct rconn *rc)
735 OVS_EXCLUDED(rc->mutex)
737 ovs_mutex_lock(&rc->mutex);
739 vconn_wait(rc->vconn, WAIT_RECV);
741 ovs_mutex_unlock(&rc->mutex);
745 rconn_send__(struct rconn *rc, struct ofpbuf *b,
746 struct rconn_packet_counter *counter)
747 OVS_REQUIRES(rc->mutex)
749 if (rconn_is_connected(rc)) {
750 COVERAGE_INC(rconn_queued);
751 copy_to_monitor(rc, b);
754 rconn_packet_counter_inc(counter, ofpbuf_size(b));
757 /* Reuse 'frame' as a private pointer while 'b' is in txq. */
758 ofpbuf_set_frame(b, counter);
760 list_push_back(&rc->txq, &b->list_node);
762 /* If the queue was empty before we added 'b', try to send some
763 * packets. (But if the queue had packets in it, it's because the
764 * vconn is backlogged and there's no point in stuffing more into it
765 * now. We'll get back to that in rconn_run().) */
766 if (rc->txq.next == &b->list_node) {
776 /* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not
777 * currently connected. Takes ownership of 'b'.
779 * If 'counter' is non-null, then 'counter' will be incremented while the
780 * packet is in flight, then decremented when it has been sent (or discarded
781 * due to disconnection). Because 'b' may be sent (or discarded) before this
782 * function returns, the caller may not be able to observe any change in
785 * There is no rconn_send_wait() function: an rconn has a send queue that it
786 * takes care of sending if you call rconn_run(), which will have the side
787 * effect of waking up poll_block(). */
789 rconn_send(struct rconn *rc, struct ofpbuf *b,
790 struct rconn_packet_counter *counter)
791 OVS_EXCLUDED(rc->mutex)
795 ovs_mutex_lock(&rc->mutex);
796 error = rconn_send__(rc, b, counter);
797 ovs_mutex_unlock(&rc->mutex);
802 /* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it
803 * will be decremented when it has been sent (or discarded due to
804 * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already
805 * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
806 * connected. Regardless of return value, 'b' is destroyed.
808 * Because 'b' may be sent (or discarded) before this function returns, the
809 * caller may not be able to observe any change in 'counter'.
811 * There is no rconn_send_wait() function: an rconn has a send queue that it
812 * takes care of sending if you call rconn_run(), which will have the side
813 * effect of waking up poll_block(). */
815 rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
816 struct rconn_packet_counter *counter, int queue_limit)
817 OVS_EXCLUDED(rc->mutex)
821 ovs_mutex_lock(&rc->mutex);
822 if (rconn_packet_counter_n_packets(counter) < queue_limit) {
823 error = rconn_send__(rc, b, counter);
825 COVERAGE_INC(rconn_overflow);
829 ovs_mutex_unlock(&rc->mutex);
834 /* Returns the total number of packets successfully sent on the underlying
835 * vconn. A packet is not counted as sent while it is still queued in the
836 * rconn, only when it has been successfuly passed to the vconn. */
838 rconn_packets_sent(const struct rconn *rc)
840 return rc->packets_sent;
843 /* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent
844 * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */
846 rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
847 OVS_EXCLUDED(rc->mutex)
849 ovs_mutex_lock(&rc->mutex);
850 if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) {
851 VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn));
852 rc->monitors[rc->n_monitors++] = vconn;
854 VLOG_DBG("too many monitor connections, discarding %s",
855 vconn_get_name(vconn));
858 ovs_mutex_unlock(&rc->mutex);
861 /* Returns 'rc''s name. This is a name for human consumption, appropriate for
862 * use in log messages. It is not necessarily a name that may be passed
863 * directly to, e.g., vconn_open(). */
865 rconn_get_name(const struct rconn *rc)
870 /* Sets 'rc''s name to 'new_name'. */
872 rconn_set_name(struct rconn *rc, const char *new_name)
873 OVS_EXCLUDED(rc->mutex)
875 ovs_mutex_lock(&rc->mutex);
877 rc->name = xstrdup(new_name);
878 ovs_mutex_unlock(&rc->mutex);
881 /* Returns 'rc''s target. This is intended to be a string that may be passed
882 * directly to, e.g., vconn_open(). */
884 rconn_get_target(const struct rconn *rc)
889 /* Returns true if 'rconn' is connected or in the process of reconnecting,
890 * false if 'rconn' is disconnected and will not reconnect on its own. */
892 rconn_is_alive(const struct rconn *rconn)
894 return rconn->state != S_VOID && rconn->state != S_DISCONNECTED;
897 /* Returns true if 'rconn' is connected, false otherwise. */
899 rconn_is_connected(const struct rconn *rconn)
901 return is_connected_state(rconn->state);
905 rconn_is_admitted__(const struct rconn *rconn)
906 OVS_REQUIRES(rconn->mutex)
908 return (rconn_is_connected(rconn)
909 && rconn->last_admitted >= rconn->last_connected);
912 /* Returns true if 'rconn' is connected and thought to have been accepted by
913 * the peer's admission-control policy. */
915 rconn_is_admitted(const struct rconn *rconn)
916 OVS_EXCLUDED(rconn->mutex)
920 ovs_mutex_lock(&rconn->mutex);
921 admitted = rconn_is_admitted__(rconn);
922 ovs_mutex_unlock(&rconn->mutex);
927 /* Returns 0 if 'rconn' is currently connected and considered to have been
928 * accepted by the peer's admission-control policy, otherwise the number of
929 * seconds since 'rconn' was last in such a state. */
931 rconn_failure_duration(const struct rconn *rconn)
932 OVS_EXCLUDED(rconn->mutex)
936 ovs_mutex_lock(&rconn->mutex);
937 duration = (rconn_is_admitted__(rconn)
939 : time_now() - rconn->last_admitted);
940 ovs_mutex_unlock(&rconn->mutex);
946 rconn_get_version__(const struct rconn *rconn)
947 OVS_REQUIRES(rconn->mutex)
949 return rconn->vconn ? vconn_get_version(rconn->vconn) : -1;
952 /* Returns the OpenFlow version negotiated with the peer, or -1 if there is
953 * currently no connection or if version negotiation is not yet complete. */
955 rconn_get_version(const struct rconn *rconn)
956 OVS_EXCLUDED(rconn->mutex)
960 ovs_mutex_lock(&rconn->mutex);
961 version = rconn_get_version__(rconn);
962 ovs_mutex_unlock(&rconn->mutex);
967 /* Returns the total number of packets successfully received by the underlying
970 rconn_packets_received(const struct rconn *rc)
972 return rc->packets_received;
975 /* Returns a string representing the internal state of 'rc'. The caller must
976 * not modify or free the string. */
978 rconn_get_state(const struct rconn *rc)
980 return state_name(rc->state);
983 /* Returns the time at which the last successful connection was made by
984 * 'rc'. Returns TIME_MIN if never connected. */
986 rconn_get_last_connection(const struct rconn *rc)
988 return rc->last_connected;
991 /* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN
992 * if never disconnected. */
994 rconn_get_last_disconnect(const struct rconn *rc)
996 return rc->last_disconnected;
999 /* Returns 'rc''s current connection sequence number, a number that changes
1000 * every time that 'rconn' connects or disconnects. */
1002 rconn_get_connection_seqno(const struct rconn *rc)
1007 /* Returns a value that explains why 'rc' last disconnected:
1009 * - 0 means that the last disconnection was caused by a call to
1010 * rconn_disconnect(), or that 'rc' is new and has not yet completed its
1011 * initial connection or connection attempt.
1013 * - EOF means that the connection was closed in the normal way by the peer.
1015 * - A positive integer is an errno value that represents the error.
1018 rconn_get_last_error(const struct rconn *rc)
1020 return rc->last_error;
1023 /* Returns the number of messages queued for transmission on 'rc'. */
1025 rconn_count_txqlen(const struct rconn *rc)
1026 OVS_EXCLUDED(rc->mutex)
1030 ovs_mutex_lock(&rc->mutex);
1031 len = list_size(&rc->txq);
1032 ovs_mutex_unlock(&rc->mutex);
1037 struct rconn_packet_counter *
1038 rconn_packet_counter_create(void)
1040 struct rconn_packet_counter *c = xzalloc(sizeof *c);
1041 ovs_mutex_init(&c->mutex);
1042 ovs_mutex_lock(&c->mutex);
1044 ovs_mutex_unlock(&c->mutex);
1049 rconn_packet_counter_destroy(struct rconn_packet_counter *c)
1054 ovs_mutex_lock(&c->mutex);
1055 ovs_assert(c->ref_cnt > 0);
1056 dead = !--c->ref_cnt && !c->n_packets;
1057 ovs_mutex_unlock(&c->mutex);
1060 ovs_mutex_destroy(&c->mutex);
1067 rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
1069 ovs_mutex_lock(&c->mutex);
1071 c->n_bytes += n_bytes;
1072 ovs_mutex_unlock(&c->mutex);
1076 rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
1080 ovs_mutex_lock(&c->mutex);
1081 ovs_assert(c->n_packets > 0);
1082 ovs_assert(c->n_packets == 1
1083 ? c->n_bytes == n_bytes
1084 : c->n_bytes > n_bytes);
1086 c->n_bytes -= n_bytes;
1087 dead = !c->n_packets && !c->ref_cnt;
1088 ovs_mutex_unlock(&c->mutex);
1091 ovs_mutex_destroy(&c->mutex);
1097 rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
1101 ovs_mutex_lock(&c->mutex);
1103 ovs_mutex_unlock(&c->mutex);
1109 rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
1113 ovs_mutex_lock(&c->mutex);
1115 ovs_mutex_unlock(&c->mutex);
1120 /* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
1121 * is null, 'target' is used. */
1123 rconn_set_target__(struct rconn *rc, const char *target, const char *name)
1124 OVS_REQUIRES(rc->mutex)
1127 rc->name = xstrdup(name ? name : target);
1129 rc->target = xstrdup(target);
1132 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
1133 * otherwise a positive errno value. */
1135 try_send(struct rconn *rc)
1136 OVS_REQUIRES(rc->mutex)
1138 struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next);
1139 unsigned int n_bytes = ofpbuf_size(msg);
1140 struct rconn_packet_counter *counter = msg->frame;
1143 /* Eagerly remove 'msg' from the txq. We can't remove it from the list
1144 * after sending, if sending is successful, because it is then owned by the
1145 * vconn, which might have freed it already. */
1146 list_remove(&msg->list_node);
1147 ofpbuf_set_frame(msg, NULL);
1149 retval = vconn_send(rc->vconn, msg);
1151 ofpbuf_set_frame(msg, counter);
1152 list_push_front(&rc->txq, &msg->list_node);
1153 if (retval != EAGAIN) {
1154 report_error(rc, retval);
1155 disconnect(rc, retval);
1159 COVERAGE_INC(rconn_sent);
1162 rconn_packet_counter_dec(counter, n_bytes);
1167 /* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive
1168 * errno value, or it may be EOF to indicate that the connection was closed
1171 report_error(struct rconn *rc, int error)
1172 OVS_REQUIRES(rc->mutex)
1174 /* On Windows, when a peer terminates without calling a closesocket()
1175 * on socket fd, we get WSAECONNRESET. Don't print warning messages
1179 || error == WSAECONNRESET
1182 /* If 'rc' isn't reliable, then we don't really expect this connection
1183 * to last forever anyway (probably it's a connection that we received
1184 * via accept()), so use DBG level to avoid cluttering the logs. */
1185 enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG;
1186 VLOG(level, "%s: connection closed by peer", rc->name);
1188 VLOG_WARN("%s: connection dropped (%s)",
1189 rc->name, ovs_strerror(error));
1193 /* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last
1196 * - 0 means that this disconnection is due to a request by 'rc''s client,
1197 * not due to any kind of network error.
1199 * - EOF means that the connection was closed in the normal way by the peer.
1201 * - A positive integer is an errno value that represents the error.
1204 disconnect(struct rconn *rc, int error)
1205 OVS_REQUIRES(rc->mutex)
1207 rc->last_error = error;
1209 vconn_close(rc->vconn);
1213 time_t now = time_now();
1215 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
1216 rc->last_disconnected = now;
1220 if (now >= rc->backoff_deadline) {
1222 } else if (rc->backoff < rc->max_backoff / 2) {
1223 rc->backoff = MAX(1, 2 * rc->backoff);
1224 VLOG_INFO("%s: waiting %d seconds before reconnect",
1225 rc->name, rc->backoff);
1227 if (rconn_logging_connection_attempts__(rc)) {
1228 VLOG_INFO("%s: continuing to retry connections in the "
1229 "background but suppressing further logging",
1232 rc->backoff = rc->max_backoff;
1234 rc->backoff_deadline = now + rc->backoff;
1235 state_transition(rc, S_BACKOFF);
1237 rc->last_disconnected = time_now();
1238 state_transition(rc, S_DISCONNECTED);
1242 /* Drops all the packets from 'rc''s send queue and decrements their queue
1245 flush_queue(struct rconn *rc)
1246 OVS_REQUIRES(rc->mutex)
1248 if (list_is_empty(&rc->txq)) {
1251 while (!list_is_empty(&rc->txq)) {
1252 struct ofpbuf *b = ofpbuf_from_list(list_pop_front(&rc->txq));
1253 struct rconn_packet_counter *counter = b->frame;
1255 rconn_packet_counter_dec(counter, ofpbuf_size(b));
1257 COVERAGE_INC(rconn_discarded);
1260 poll_immediate_wake();
1264 elapsed_in_this_state(const struct rconn *rc)
1265 OVS_REQUIRES(rc->mutex)
1267 return time_now() - rc->state_entered;
1271 timeout(const struct rconn *rc)
1272 OVS_REQUIRES(rc->mutex)
1274 switch (rc->state) {
1275 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
1284 timed_out(const struct rconn *rc)
1285 OVS_REQUIRES(rc->mutex)
1287 return time_now() >= sat_add(rc->state_entered, timeout(rc));
1291 state_transition(struct rconn *rc, enum state state)
1292 OVS_REQUIRES(rc->mutex)
1294 rc->seqno += (rc->state == S_ACTIVE) != (state == S_ACTIVE);
1295 if (is_connected_state(state) && !is_connected_state(rc->state)) {
1296 rc->probably_admitted = false;
1298 if (rconn_is_connected(rc)) {
1299 rc->total_time_connected += elapsed_in_this_state(rc);
1301 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
1303 rc->state_entered = time_now();
1307 close_monitor(struct rconn *rc, size_t idx, int retval)
1308 OVS_REQUIRES(rc->mutex)
1310 VLOG_DBG("%s: closing monitor connection to %s: %s",
1311 rconn_get_name(rc), vconn_get_name(rc->monitors[idx]),
1312 ovs_retval_to_string(retval));
1313 rc->monitors[idx] = rc->monitors[--rc->n_monitors];
1317 copy_to_monitor(struct rconn *rc, const struct ofpbuf *b)
1318 OVS_REQUIRES(rc->mutex)
1320 struct ofpbuf *clone = NULL;
1324 for (i = 0; i < rc->n_monitors; ) {
1325 struct vconn *vconn = rc->monitors[i];
1328 clone = ofpbuf_clone(b);
1330 retval = vconn_send(vconn, clone);
1333 } else if (retval != EAGAIN) {
1334 close_monitor(rc, i, retval);
1339 ofpbuf_delete(clone);
1343 is_connected_state(enum state state)
1345 return (state & (S_ACTIVE | S_IDLE)) != 0;
1348 /* When a switch initially connects to a controller, the controller may spend a
1349 * little time examining the switch, looking at, for example, its datapath ID,
1350 * before it decides whether it is willing to control that switch. At that
1351 * point, it either disconnects or starts controlling the switch.
1353 * This function returns a guess to its caller about whether 'b' is OpenFlow
1354 * message that indicates that the controller has decided to control the
1355 * switch. It returns false if the message is one that a controller typically
1356 * uses to determine whether a switch is admissible, true if the message is one
1357 * that would typically be used only after the controller has admitted the
1360 is_admitted_msg(const struct ofpbuf *b)
1365 error = ofptype_decode(&type, ofpbuf_data(b));
1373 case OFPTYPE_ECHO_REQUEST:
1374 case OFPTYPE_ECHO_REPLY:
1375 case OFPTYPE_FEATURES_REQUEST:
1376 case OFPTYPE_FEATURES_REPLY:
1377 case OFPTYPE_GET_CONFIG_REQUEST:
1378 case OFPTYPE_GET_CONFIG_REPLY:
1379 case OFPTYPE_SET_CONFIG:
1380 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1381 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1382 case OFPTYPE_GET_ASYNC_REQUEST:
1383 case OFPTYPE_GET_ASYNC_REPLY:
1384 case OFPTYPE_GROUP_STATS_REQUEST:
1385 case OFPTYPE_GROUP_STATS_REPLY:
1386 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
1387 case OFPTYPE_GROUP_DESC_STATS_REPLY:
1388 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
1389 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
1390 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
1391 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
1392 case OFPTYPE_BUNDLE_CONTROL:
1393 case OFPTYPE_BUNDLE_ADD_MESSAGE:
1396 case OFPTYPE_PACKET_IN:
1397 case OFPTYPE_FLOW_REMOVED:
1398 case OFPTYPE_PORT_STATUS:
1399 case OFPTYPE_PACKET_OUT:
1400 case OFPTYPE_FLOW_MOD:
1401 case OFPTYPE_GROUP_MOD:
1402 case OFPTYPE_PORT_MOD:
1403 case OFPTYPE_TABLE_MOD:
1404 case OFPTYPE_METER_MOD:
1405 case OFPTYPE_BARRIER_REQUEST:
1406 case OFPTYPE_BARRIER_REPLY:
1407 case OFPTYPE_DESC_STATS_REQUEST:
1408 case OFPTYPE_DESC_STATS_REPLY:
1409 case OFPTYPE_FLOW_STATS_REQUEST:
1410 case OFPTYPE_FLOW_STATS_REPLY:
1411 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1412 case OFPTYPE_AGGREGATE_STATS_REPLY:
1413 case OFPTYPE_TABLE_STATS_REQUEST:
1414 case OFPTYPE_TABLE_STATS_REPLY:
1415 case OFPTYPE_PORT_STATS_REQUEST:
1416 case OFPTYPE_PORT_STATS_REPLY:
1417 case OFPTYPE_QUEUE_STATS_REQUEST:
1418 case OFPTYPE_QUEUE_STATS_REPLY:
1419 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1420 case OFPTYPE_PORT_DESC_STATS_REPLY:
1421 case OFPTYPE_METER_STATS_REQUEST:
1422 case OFPTYPE_METER_STATS_REPLY:
1423 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
1424 case OFPTYPE_METER_CONFIG_STATS_REPLY:
1425 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
1426 case OFPTYPE_METER_FEATURES_STATS_REPLY:
1427 case OFPTYPE_ROLE_REQUEST:
1428 case OFPTYPE_ROLE_REPLY:
1429 case OFPTYPE_ROLE_STATUS:
1430 case OFPTYPE_SET_FLOW_FORMAT:
1431 case OFPTYPE_FLOW_MOD_TABLE_ID:
1432 case OFPTYPE_SET_PACKET_IN_FORMAT:
1433 case OFPTYPE_FLOW_AGE:
1434 case OFPTYPE_SET_ASYNC_CONFIG:
1435 case OFPTYPE_SET_CONTROLLER_ID:
1436 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1437 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1438 case OFPTYPE_FLOW_MONITOR_CANCEL:
1439 case OFPTYPE_FLOW_MONITOR_PAUSED:
1440 case OFPTYPE_FLOW_MONITOR_RESUMED:
1446 /* Returns true if 'rc' is currently logging information about connection
1447 * attempts, false if logging should be suppressed because 'rc' hasn't
1448 * successuflly connected in too long. */
1450 rconn_logging_connection_attempts__(const struct rconn *rc)
1451 OVS_REQUIRES(rc->mutex)
1453 return rc->backoff < rc->max_backoff;