2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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.
24 #include "openflow/openflow.h"
25 #include "openvswitch/ofp-msgs.h"
26 #include "openvswitch/ofp-util.h"
27 #include "openvswitch/ofpbuf.h"
28 #include "openvswitch/vconn.h"
29 #include "openvswitch/vlog.h"
30 #include "poll-loop.h"
36 VLOG_DEFINE_THIS_MODULE(rconn);
38 COVERAGE_DEFINE(rconn_discarded);
39 COVERAGE_DEFINE(rconn_overflow);
40 COVERAGE_DEFINE(rconn_queued);
41 COVERAGE_DEFINE(rconn_sent);
43 /* The connection states have the following meanings:
45 * - S_VOID: No connection information is configured.
47 * - S_BACKOFF: Waiting for a period of time before reconnecting.
49 * - S_CONNECTING: A connection attempt is in progress and has not yet
50 * succeeded or failed.
52 * - S_ACTIVE: A connection has been established and appears to be healthy.
54 * - S_IDLE: A connection has been established but has been idle for some
55 * time. An echo request has been sent, but no reply has yet been
58 * - S_DISCONNECTED: An unreliable connection has disconnected and cannot be
59 * automatically retried.
63 STATE(BACKOFF, 1 << 1) \
64 STATE(CONNECTING, 1 << 2) \
65 STATE(ACTIVE, 1 << 3) \
67 STATE(DISCONNECTED, 1 << 5)
69 #define STATE(NAME, VALUE) S_##NAME = VALUE,
75 state_name(enum state state)
78 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
85 /* A reliable connection to an OpenFlow switch or controller.
87 * See the large comment in rconn.h for more information. */
89 struct ovs_mutex mutex;
95 char *name; /* Human-readable descriptive name. */
96 char *target; /* vconn name, passed to vconn_open(). */
99 struct ovs_list txq; /* Contains "struct ofpbuf"s. */
103 time_t backoff_deadline;
104 time_t last_connected;
105 time_t last_disconnected;
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 n_attempted_connections, n_successful_connections;
123 time_t creation_time;
124 unsigned long int total_time_connected;
126 /* Throughout this file, "probe" is shorthand for "inactivity probe". When
127 * no activity has been observed from the peer for a while, we send out an
128 * echo request as an inactivity probe packet. We should receive back a
131 * "Activity" is defined as either receiving an OpenFlow message from the
132 * peer or successfully sending a message that had been in 'txq'. */
133 int probe_interval; /* Secs of inactivity before sending probe. */
134 time_t last_activity; /* Last time we saw some activity. */
138 /* Messages sent or received are copied to the monitor connections. */
139 #define MAXIMUM_MONITORS 8
140 struct vconn *monitors[MAXIMUM_MONITORS];
143 uint32_t allowed_versions;
146 uint32_t rconn_get_allowed_versions(const struct rconn *rconn)
148 return rconn->allowed_versions;
151 static unsigned int elapsed_in_this_state(const struct rconn *rc)
152 OVS_REQUIRES(rc->mutex);
153 static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
154 static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
155 static void state_transition(struct rconn *rc, enum state)
156 OVS_REQUIRES(rc->mutex);
157 static void rconn_set_target__(struct rconn *rc,
158 const char *target, const char *name)
159 OVS_REQUIRES(rc->mutex);
160 static int rconn_send__(struct rconn *rc, struct ofpbuf *,
161 struct rconn_packet_counter *)
162 OVS_REQUIRES(rc->mutex);
163 static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex);
164 static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex);
165 static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
166 static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex);
167 static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
168 static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex);
169 static void close_monitor(struct rconn *rc, size_t idx, int retval)
170 OVS_REQUIRES(rc->mutex);
171 static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
172 static bool is_connected_state(enum state);
173 static bool is_admitted_msg(const struct ofpbuf *);
174 static bool rconn_logging_connection_attempts__(const struct rconn *rc)
175 OVS_REQUIRES(rc->mutex);
176 static int rconn_get_version__(const struct rconn *rconn)
177 OVS_REQUIRES(rconn->mutex);
179 /* The following prototypes duplicate those in rconn.h, but there we weren't
180 * able to add the OVS_EXCLUDED annotations because the definition of struct
181 * rconn was not visible. */
183 void rconn_set_max_backoff(struct rconn *rc, int max_backoff)
184 OVS_EXCLUDED(rc->mutex);
185 void rconn_connect(struct rconn *rc, const char *target, const char *name)
186 OVS_EXCLUDED(rc->mutex);
187 void rconn_connect_unreliably(struct rconn *rc,
188 struct vconn *vconn, const char *name)
189 OVS_EXCLUDED(rc->mutex);
190 void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
191 void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
192 void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
193 void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
194 struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
195 void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
196 int rconn_send(struct rconn *rc, struct ofpbuf *b,
197 struct rconn_packet_counter *counter)
198 OVS_EXCLUDED(rc->mutex);
199 int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
200 struct rconn_packet_counter *counter,
202 OVS_EXCLUDED(rc->mutex);
203 void rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
204 OVS_EXCLUDED(rc->mutex);
205 void rconn_set_name(struct rconn *rc, const char *new_name)
206 OVS_EXCLUDED(rc->mutex);
207 bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
208 int rconn_failure_duration(const struct rconn *rconn)
209 OVS_EXCLUDED(rconn->mutex);
210 ovs_be16 rconn_get_local_port(const struct rconn *rconn)
211 OVS_EXCLUDED(rconn->mutex);
212 int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
213 unsigned int rconn_count_txqlen(const struct rconn *rc)
214 OVS_EXCLUDED(rc->mutex);
217 /* Creates and returns a new rconn.
219 * 'probe_interval' is a number of seconds. If the interval passes once
220 * without an OpenFlow message being received from the peer, the rconn sends
221 * out an "echo request" message. If the interval passes again without a
222 * message being received, the rconn disconnects and re-connects to the peer.
223 * Setting 'probe_interval' to 0 disables this behavior.
225 * 'max_backoff' is the maximum number of seconds between attempts to connect
226 * to the peer. The actual interval starts at 1 second and doubles on each
227 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
230 * The new rconn is initially unconnected. Use rconn_connect() or
231 * rconn_connect_unreliably() to connect it.
233 * Connections made by the rconn will automatically negotiate an OpenFlow
234 * protocol version acceptable to both peers on the connection. The version
235 * negotiated will be one of those in the 'allowed_versions' bitmap: version
236 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying
237 * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.)
240 rconn_create(int probe_interval, int max_backoff, uint8_t dscp,
241 uint32_t allowed_versions)
243 struct rconn *rc = xzalloc(sizeof *rc);
245 ovs_mutex_init(&rc->mutex);
248 rc->state_entered = time_now();
251 rc->name = xstrdup("void");
252 rc->target = xstrdup("void");
253 rc->reliable = false;
255 ovs_list_init(&rc->txq);
258 rc->max_backoff = max_backoff ? max_backoff : 8;
259 rc->backoff_deadline = TIME_MIN;
260 rc->last_connected = TIME_MIN;
261 rc->last_disconnected = TIME_MIN;
264 rc->probably_admitted = false;
265 rc->last_admitted = time_now();
267 rc->n_attempted_connections = 0;
268 rc->n_successful_connections = 0;
269 rc->creation_time = time_now();
270 rc->total_time_connected = 0;
272 rc->last_activity = time_now();
274 rconn_set_probe_interval(rc, probe_interval);
275 rconn_set_dscp(rc, dscp);
278 rc->allowed_versions = allowed_versions;
284 rconn_set_max_backoff(struct rconn *rc, int max_backoff)
285 OVS_EXCLUDED(rc->mutex)
287 ovs_mutex_lock(&rc->mutex);
288 rc->max_backoff = MAX(1, max_backoff);
289 if (rc->state == S_BACKOFF && rc->backoff > max_backoff) {
290 rc->backoff = max_backoff;
291 if (rc->backoff_deadline > time_now() + max_backoff) {
292 rc->backoff_deadline = time_now() + max_backoff;
295 ovs_mutex_unlock(&rc->mutex);
299 rconn_get_max_backoff(const struct rconn *rc)
301 return rc->max_backoff;
305 rconn_set_dscp(struct rconn *rc, uint8_t dscp)
311 rconn_get_dscp(const struct rconn *rc)
317 rconn_set_probe_interval(struct rconn *rc, int probe_interval)
319 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
323 rconn_get_probe_interval(const struct rconn *rc)
325 return rc->probe_interval;
328 /* Drops any existing connection on 'rc', then sets up 'rc' to connect to
329 * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
330 * target in a form acceptable to vconn_open().
332 * If 'name' is nonnull, then it is used in log messages in place of 'target'.
333 * It should presumably give more information to a human reader than 'target',
334 * but it need not be acceptable to vconn_open(). */
336 rconn_connect(struct rconn *rc, const char *target, const char *name)
337 OVS_EXCLUDED(rc->mutex)
339 ovs_mutex_lock(&rc->mutex);
340 rconn_disconnect__(rc);
341 rconn_set_target__(rc, target, name);
343 if (!stream_or_pstream_needs_probes(target)) {
344 rc->probe_interval = 0;
347 ovs_mutex_unlock(&rc->mutex);
350 /* Drops any existing connection on 'rc', then configures 'rc' to use
351 * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
354 * By default, the target obtained from vconn_get_name(vconn) is used in log
355 * messages. If 'name' is nonnull, then it is used instead. It should
356 * presumably give more information to a human reader than the target, but it
357 * need not be acceptable to vconn_open(). */
359 rconn_connect_unreliably(struct rconn *rc,
360 struct vconn *vconn, const char *name)
361 OVS_EXCLUDED(rc->mutex)
363 ovs_assert(vconn != NULL);
365 ovs_mutex_lock(&rc->mutex);
366 rconn_disconnect__(rc);
367 rconn_set_target__(rc, vconn_get_name(vconn), name);
368 rc->reliable = false;
370 rc->last_connected = time_now();
371 state_transition(rc, S_ACTIVE);
372 ovs_mutex_unlock(&rc->mutex);
375 /* If 'rc' is connected, forces it to drop the connection and reconnect. */
377 rconn_reconnect(struct rconn *rc)
378 OVS_EXCLUDED(rc->mutex)
380 ovs_mutex_lock(&rc->mutex);
381 if (rc->state & (S_ACTIVE | S_IDLE)) {
382 VLOG_INFO("%s: disconnecting", rc->name);
385 ovs_mutex_unlock(&rc->mutex);
389 rconn_disconnect__(struct rconn *rc)
390 OVS_REQUIRES(rc->mutex)
392 if (rc->state != S_VOID) {
394 vconn_close(rc->vconn);
397 rconn_set_target__(rc, "void", NULL);
398 rc->reliable = false;
401 rc->backoff_deadline = TIME_MIN;
403 state_transition(rc, S_VOID);
408 rconn_disconnect(struct rconn *rc)
409 OVS_EXCLUDED(rc->mutex)
411 ovs_mutex_lock(&rc->mutex);
412 rconn_disconnect__(rc);
413 ovs_mutex_unlock(&rc->mutex);
416 /* Disconnects 'rc' and frees the underlying storage. */
418 rconn_destroy(struct rconn *rc)
423 ovs_mutex_lock(&rc->mutex);
426 vconn_close(rc->vconn);
428 ofpbuf_list_delete(&rc->txq);
429 for (i = 0; i < rc->n_monitors; i++) {
430 vconn_close(rc->monitors[i]);
432 ovs_mutex_unlock(&rc->mutex);
433 ovs_mutex_destroy(&rc->mutex);
440 timeout_VOID(const struct rconn *rc OVS_UNUSED)
441 OVS_REQUIRES(rc->mutex)
447 run_VOID(struct rconn *rc OVS_UNUSED)
448 OVS_REQUIRES(rc->mutex)
454 reconnect(struct rconn *rc)
455 OVS_REQUIRES(rc->mutex)
459 if (rconn_logging_connection_attempts__(rc)) {
460 VLOG_INFO("%s: connecting...", rc->name);
462 rc->n_attempted_connections++;
463 retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp,
466 rc->backoff_deadline = time_now() + rc->backoff;
467 state_transition(rc, S_CONNECTING);
469 VLOG_WARN("%s: connection failed (%s)",
470 rc->name, ovs_strerror(retval));
471 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
472 disconnect(rc, retval);
477 timeout_BACKOFF(const struct rconn *rc)
478 OVS_REQUIRES(rc->mutex)
484 run_BACKOFF(struct rconn *rc)
485 OVS_REQUIRES(rc->mutex)
493 timeout_CONNECTING(const struct rconn *rc)
494 OVS_REQUIRES(rc->mutex)
496 return MAX(1, rc->backoff);
500 run_CONNECTING(struct rconn *rc)
501 OVS_REQUIRES(rc->mutex)
503 int retval = vconn_connect(rc->vconn);
505 VLOG_INFO("%s: connected", rc->name);
506 rc->n_successful_connections++;
507 state_transition(rc, S_ACTIVE);
508 rc->last_connected = rc->state_entered;
509 } else if (retval != EAGAIN) {
510 if (rconn_logging_connection_attempts__(rc)) {
511 VLOG_INFO("%s: connection failed (%s)",
512 rc->name, ovs_strerror(retval));
514 disconnect(rc, retval);
515 } else if (timed_out(rc)) {
516 if (rconn_logging_connection_attempts__(rc)) {
517 VLOG_INFO("%s: connection timed out", rc->name);
519 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
520 disconnect(rc, ETIMEDOUT);
525 do_tx_work(struct rconn *rc)
526 OVS_REQUIRES(rc->mutex)
528 if (ovs_list_is_empty(&rc->txq)) {
531 while (!ovs_list_is_empty(&rc->txq)) {
532 int error = try_send(rc);
536 rc->last_activity = time_now();
538 if (ovs_list_is_empty(&rc->txq)) {
539 poll_immediate_wake();
544 timeout_ACTIVE(const struct rconn *rc)
545 OVS_REQUIRES(rc->mutex)
547 if (rc->probe_interval) {
548 unsigned int base = MAX(rc->last_activity, rc->state_entered);
549 unsigned int arg = base + rc->probe_interval - rc->state_entered;
556 run_ACTIVE(struct rconn *rc)
557 OVS_REQUIRES(rc->mutex)
560 unsigned int base = MAX(rc->last_activity, rc->state_entered);
563 VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
564 rc->name, (unsigned int) (time_now() - base));
566 version = rconn_get_version__(rc);
567 ovs_assert(version >= 0 && version <= 0xff);
569 /* Ordering is important here: rconn_send() can transition to BACKOFF,
570 * and we don't want to transition back to IDLE if so, because then we
571 * can end up queuing a packet with vconn == NULL and then *boom*. */
572 state_transition(rc, S_IDLE);
573 rconn_send__(rc, make_echo_request(version), NULL);
581 timeout_IDLE(const struct rconn *rc)
582 OVS_REQUIRES(rc->mutex)
584 return rc->probe_interval;
588 run_IDLE(struct rconn *rc)
589 OVS_REQUIRES(rc->mutex)
592 VLOG_ERR("%s: no response to inactivity probe after %u "
593 "seconds, disconnecting",
594 rc->name, elapsed_in_this_state(rc));
595 disconnect(rc, ETIMEDOUT);
602 timeout_DISCONNECTED(const struct rconn *rc OVS_UNUSED)
603 OVS_REQUIRES(rc->mutex)
609 run_DISCONNECTED(struct rconn *rc OVS_UNUSED)
610 OVS_REQUIRES(rc->mutex)
615 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
616 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
617 * connected, attempts to send packets in the send queue, if any. */
619 rconn_run(struct rconn *rc)
620 OVS_EXCLUDED(rc->mutex)
625 ovs_mutex_lock(&rc->mutex);
629 vconn_run(rc->vconn);
631 error = vconn_get_status(rc->vconn);
633 report_error(rc, error);
634 disconnect(rc, error);
637 for (i = 0; i < rc->n_monitors; ) {
641 vconn_run(rc->monitors[i]);
643 /* Drain any stray message that came in on the monitor connection. */
644 retval = vconn_recv(rc->monitors[i], &msg);
647 } else if (retval != EAGAIN) {
648 close_monitor(rc, i, retval);
655 old_state = rc->state;
657 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
663 } while (rc->state != old_state);
664 ovs_mutex_unlock(&rc->mutex);
667 /* Causes the next call to poll_block() to wake up when rconn_run() should be
670 rconn_run_wait(struct rconn *rc)
671 OVS_EXCLUDED(rc->mutex)
676 ovs_mutex_lock(&rc->mutex);
678 vconn_run_wait(rc->vconn);
679 if ((rc->state & (S_ACTIVE | S_IDLE)) && !ovs_list_is_empty(&rc->txq)) {
680 vconn_wait(rc->vconn, WAIT_SEND);
683 for (i = 0; i < rc->n_monitors; i++) {
684 vconn_run_wait(rc->monitors[i]);
685 vconn_recv_wait(rc->monitors[i]);
689 if (timeo != UINT_MAX) {
690 long long int expires = sat_add(rc->state_entered, timeo);
691 poll_timer_wait_until(expires * 1000);
693 ovs_mutex_unlock(&rc->mutex);
696 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
697 * otherwise, returns a null pointer. The caller is responsible for freeing
698 * the packet (with ofpbuf_delete()). */
700 rconn_recv(struct rconn *rc)
701 OVS_EXCLUDED(rc->mutex)
703 struct ofpbuf *buffer = NULL;
705 ovs_mutex_lock(&rc->mutex);
706 if (rc->state & (S_ACTIVE | S_IDLE)) {
707 int error = vconn_recv(rc->vconn, &buffer);
709 copy_to_monitor(rc, buffer);
710 if (rc->probably_admitted || is_admitted_msg(buffer)
711 || time_now() - rc->last_connected >= 30) {
712 rc->probably_admitted = true;
713 rc->last_admitted = time_now();
715 rc->last_activity = time_now();
716 if (rc->state == S_IDLE) {
717 state_transition(rc, S_ACTIVE);
719 } else if (error != EAGAIN) {
720 report_error(rc, error);
721 disconnect(rc, error);
724 ovs_mutex_unlock(&rc->mutex);
729 /* Causes the next call to poll_block() to wake up when a packet may be ready
730 * to be received by vconn_recv() on 'rc'. */
732 rconn_recv_wait(struct rconn *rc)
733 OVS_EXCLUDED(rc->mutex)
735 ovs_mutex_lock(&rc->mutex);
737 vconn_wait(rc->vconn, WAIT_RECV);
739 ovs_mutex_unlock(&rc->mutex);
743 rconn_send__(struct rconn *rc, struct ofpbuf *b,
744 struct rconn_packet_counter *counter)
745 OVS_REQUIRES(rc->mutex)
747 if (rconn_is_connected(rc)) {
748 COVERAGE_INC(rconn_queued);
749 copy_to_monitor(rc, b);
752 rconn_packet_counter_inc(counter, b->size);
755 /* Reuse 'frame' as a private pointer while 'b' is in txq. */
758 ovs_list_push_back(&rc->txq, &b->list_node);
760 /* If the queue was empty before we added 'b', try to send some
761 * packets. (But if the queue had packets in it, it's because the
762 * vconn is backlogged and there's no point in stuffing more into it
763 * now. We'll get back to that in rconn_run().) */
764 if (rc->txq.next == &b->list_node) {
774 /* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not
775 * currently connected. Takes ownership of 'b'.
777 * If 'counter' is non-null, then 'counter' will be incremented while the
778 * packet is in flight, then decremented when it has been sent (or discarded
779 * due to disconnection). Because 'b' may be sent (or discarded) before this
780 * function returns, the caller may not be able to observe any change in
783 * There is no rconn_send_wait() function: an rconn has a send queue that it
784 * takes care of sending if you call rconn_run(), which will have the side
785 * effect of waking up poll_block(). */
787 rconn_send(struct rconn *rc, struct ofpbuf *b,
788 struct rconn_packet_counter *counter)
789 OVS_EXCLUDED(rc->mutex)
793 ovs_mutex_lock(&rc->mutex);
794 error = rconn_send__(rc, b, counter);
795 ovs_mutex_unlock(&rc->mutex);
800 /* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it
801 * will be decremented when it has been sent (or discarded due to
802 * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already
803 * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
804 * connected. Regardless of return value, 'b' is destroyed.
806 * Because 'b' may be sent (or discarded) before this function returns, the
807 * caller may not be able to observe any change in 'counter'.
809 * There is no rconn_send_wait() function: an rconn has a send queue that it
810 * takes care of sending if you call rconn_run(), which will have the side
811 * effect of waking up poll_block(). */
813 rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
814 struct rconn_packet_counter *counter, int queue_limit)
815 OVS_EXCLUDED(rc->mutex)
819 ovs_mutex_lock(&rc->mutex);
820 if (rconn_packet_counter_n_packets(counter) < queue_limit) {
821 error = rconn_send__(rc, b, counter);
823 COVERAGE_INC(rconn_overflow);
827 ovs_mutex_unlock(&rc->mutex);
832 /* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent
833 * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */
835 rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
836 OVS_EXCLUDED(rc->mutex)
838 ovs_mutex_lock(&rc->mutex);
839 if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) {
840 VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn));
841 rc->monitors[rc->n_monitors++] = vconn;
843 VLOG_DBG("too many monitor connections, discarding %s",
844 vconn_get_name(vconn));
847 ovs_mutex_unlock(&rc->mutex);
850 /* Returns 'rc''s name. This is a name for human consumption, appropriate for
851 * use in log messages. It is not necessarily a name that may be passed
852 * directly to, e.g., vconn_open(). */
854 rconn_get_name(const struct rconn *rc)
859 /* Sets 'rc''s name to 'new_name'. */
861 rconn_set_name(struct rconn *rc, const char *new_name)
862 OVS_EXCLUDED(rc->mutex)
864 ovs_mutex_lock(&rc->mutex);
866 rc->name = xstrdup(new_name);
867 ovs_mutex_unlock(&rc->mutex);
870 /* Returns 'rc''s target. This is intended to be a string that may be passed
871 * directly to, e.g., vconn_open(). */
873 rconn_get_target(const struct rconn *rc)
878 /* Returns true if 'rconn' is connected or in the process of reconnecting,
879 * false if 'rconn' is disconnected and will not reconnect on its own. */
881 rconn_is_alive(const struct rconn *rconn)
883 return rconn->state != S_VOID && rconn->state != S_DISCONNECTED;
886 /* Returns true if 'rconn' is connected, false otherwise. */
888 rconn_is_connected(const struct rconn *rconn)
890 return is_connected_state(rconn->state);
894 rconn_is_admitted__(const struct rconn *rconn)
895 OVS_REQUIRES(rconn->mutex)
897 return (rconn_is_connected(rconn)
898 && rconn->last_admitted >= rconn->last_connected);
901 /* Returns true if 'rconn' is connected and thought to have been accepted by
902 * the peer's admission-control policy. */
904 rconn_is_admitted(const struct rconn *rconn)
905 OVS_EXCLUDED(rconn->mutex)
909 ovs_mutex_lock(&rconn->mutex);
910 admitted = rconn_is_admitted__(rconn);
911 ovs_mutex_unlock(&rconn->mutex);
916 /* Returns 0 if 'rconn' is currently connected and considered to have been
917 * accepted by the peer's admission-control policy, otherwise the number of
918 * seconds since 'rconn' was last in such a state. */
920 rconn_failure_duration(const struct rconn *rconn)
921 OVS_EXCLUDED(rconn->mutex)
925 ovs_mutex_lock(&rconn->mutex);
926 duration = (rconn_is_admitted__(rconn)
928 : time_now() - rconn->last_admitted);
929 ovs_mutex_unlock(&rconn->mutex);
935 rconn_get_version__(const struct rconn *rconn)
936 OVS_REQUIRES(rconn->mutex)
938 return rconn->vconn ? vconn_get_version(rconn->vconn) : -1;
941 /* Returns the OpenFlow version negotiated with the peer, or -1 if there is
942 * currently no connection or if version negotiation is not yet complete. */
944 rconn_get_version(const struct rconn *rconn)
945 OVS_EXCLUDED(rconn->mutex)
949 ovs_mutex_lock(&rconn->mutex);
950 version = rconn_get_version__(rconn);
951 ovs_mutex_unlock(&rconn->mutex);
956 /* Returns a string representing the internal state of 'rc'. The caller must
957 * not modify or free the string. */
959 rconn_get_state(const struct rconn *rc)
961 return state_name(rc->state);
964 /* Returns the time at which the last successful connection was made by
965 * 'rc'. Returns TIME_MIN if never connected. */
967 rconn_get_last_connection(const struct rconn *rc)
969 return rc->last_connected;
972 /* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN
973 * if never disconnected. */
975 rconn_get_last_disconnect(const struct rconn *rc)
977 return rc->last_disconnected;
980 /* Returns 'rc''s current connection sequence number, a number that changes
981 * every time that 'rconn' connects or disconnects. */
983 rconn_get_connection_seqno(const struct rconn *rc)
988 /* Returns a value that explains why 'rc' last disconnected:
990 * - 0 means that the last disconnection was caused by a call to
991 * rconn_disconnect(), or that 'rc' is new and has not yet completed its
992 * initial connection or connection attempt.
994 * - EOF means that the connection was closed in the normal way by the peer.
996 * - A positive integer is an errno value that represents the error.
999 rconn_get_last_error(const struct rconn *rc)
1001 return rc->last_error;
1004 /* Returns the number of messages queued for transmission on 'rc'. */
1006 rconn_count_txqlen(const struct rconn *rc)
1007 OVS_EXCLUDED(rc->mutex)
1011 ovs_mutex_lock(&rc->mutex);
1012 len = ovs_list_size(&rc->txq);
1013 ovs_mutex_unlock(&rc->mutex);
1018 struct rconn_packet_counter *
1019 rconn_packet_counter_create(void)
1021 struct rconn_packet_counter *c = xzalloc(sizeof *c);
1022 ovs_mutex_init(&c->mutex);
1023 ovs_mutex_lock(&c->mutex);
1025 ovs_mutex_unlock(&c->mutex);
1030 rconn_packet_counter_destroy(struct rconn_packet_counter *c)
1035 ovs_mutex_lock(&c->mutex);
1036 ovs_assert(c->ref_cnt > 0);
1037 dead = !--c->ref_cnt && !c->n_packets;
1038 ovs_mutex_unlock(&c->mutex);
1041 ovs_mutex_destroy(&c->mutex);
1048 rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
1050 ovs_mutex_lock(&c->mutex);
1052 c->n_bytes += n_bytes;
1053 ovs_mutex_unlock(&c->mutex);
1057 rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
1061 ovs_mutex_lock(&c->mutex);
1062 ovs_assert(c->n_packets > 0);
1063 ovs_assert(c->n_packets == 1
1064 ? c->n_bytes == n_bytes
1065 : c->n_bytes > n_bytes);
1067 c->n_bytes -= n_bytes;
1068 dead = !c->n_packets && !c->ref_cnt;
1069 ovs_mutex_unlock(&c->mutex);
1072 ovs_mutex_destroy(&c->mutex);
1078 rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
1082 ovs_mutex_lock(&c->mutex);
1084 ovs_mutex_unlock(&c->mutex);
1090 rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
1094 ovs_mutex_lock(&c->mutex);
1096 ovs_mutex_unlock(&c->mutex);
1101 /* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
1102 * is null, 'target' is used. */
1104 rconn_set_target__(struct rconn *rc, const char *target, const char *name)
1105 OVS_REQUIRES(rc->mutex)
1108 rc->name = xstrdup(name ? name : target);
1110 rc->target = xstrdup(target);
1113 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
1114 * otherwise a positive errno value. */
1116 try_send(struct rconn *rc)
1117 OVS_REQUIRES(rc->mutex)
1119 struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next);
1120 unsigned int n_bytes = msg->size;
1121 struct rconn_packet_counter *counter = msg->header;
1124 /* Eagerly remove 'msg' from the txq. We can't remove it from the list
1125 * after sending, if sending is successful, because it is then owned by the
1126 * vconn, which might have freed it already. */
1127 ovs_list_remove(&msg->list_node);
1130 retval = vconn_send(rc->vconn, msg);
1132 msg->header = counter;
1133 ovs_list_push_front(&rc->txq, &msg->list_node);
1134 if (retval != EAGAIN) {
1135 report_error(rc, retval);
1136 disconnect(rc, retval);
1140 COVERAGE_INC(rconn_sent);
1142 rconn_packet_counter_dec(counter, n_bytes);
1147 /* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive
1148 * errno value, or it may be EOF to indicate that the connection was closed
1151 report_error(struct rconn *rc, int error)
1152 OVS_REQUIRES(rc->mutex)
1154 /* On Windows, when a peer terminates without calling a closesocket()
1155 * on socket fd, we get WSAECONNRESET. Don't print warning messages
1159 || error == WSAECONNRESET
1162 /* If 'rc' isn't reliable, then we don't really expect this connection
1163 * to last forever anyway (probably it's a connection that we received
1164 * via accept()), so use DBG level to avoid cluttering the logs. */
1165 enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG;
1166 VLOG(level, "%s: connection closed by peer", rc->name);
1168 VLOG_WARN("%s: connection dropped (%s)",
1169 rc->name, ovs_strerror(error));
1173 /* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last
1176 * - 0 means that this disconnection is due to a request by 'rc''s client,
1177 * not due to any kind of network error.
1179 * - EOF means that the connection was closed in the normal way by the peer.
1181 * - A positive integer is an errno value that represents the error.
1184 disconnect(struct rconn *rc, int error)
1185 OVS_REQUIRES(rc->mutex)
1187 rc->last_error = error;
1189 vconn_close(rc->vconn);
1193 time_t now = time_now();
1195 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
1196 rc->last_disconnected = now;
1200 if (now >= rc->backoff_deadline) {
1202 } else if (rc->backoff < rc->max_backoff / 2) {
1203 rc->backoff = MAX(1, 2 * rc->backoff);
1204 VLOG_INFO("%s: waiting %d seconds before reconnect",
1205 rc->name, rc->backoff);
1207 if (rconn_logging_connection_attempts__(rc)) {
1208 VLOG_INFO("%s: continuing to retry connections in the "
1209 "background but suppressing further logging",
1212 rc->backoff = rc->max_backoff;
1214 rc->backoff_deadline = now + rc->backoff;
1215 state_transition(rc, S_BACKOFF);
1217 rc->last_disconnected = time_now();
1218 state_transition(rc, S_DISCONNECTED);
1222 /* Drops all the packets from 'rc''s send queue and decrements their queue
1225 flush_queue(struct rconn *rc)
1226 OVS_REQUIRES(rc->mutex)
1228 if (ovs_list_is_empty(&rc->txq)) {
1231 while (!ovs_list_is_empty(&rc->txq)) {
1232 struct ofpbuf *b = ofpbuf_from_list(ovs_list_pop_front(&rc->txq));
1233 struct rconn_packet_counter *counter = b->header;
1235 rconn_packet_counter_dec(counter, b->size);
1237 COVERAGE_INC(rconn_discarded);
1240 poll_immediate_wake();
1244 elapsed_in_this_state(const struct rconn *rc)
1245 OVS_REQUIRES(rc->mutex)
1247 return time_now() - rc->state_entered;
1251 timeout(const struct rconn *rc)
1252 OVS_REQUIRES(rc->mutex)
1254 switch (rc->state) {
1255 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
1264 timed_out(const struct rconn *rc)
1265 OVS_REQUIRES(rc->mutex)
1267 return time_now() >= sat_add(rc->state_entered, timeout(rc));
1271 state_transition(struct rconn *rc, enum state state)
1272 OVS_REQUIRES(rc->mutex)
1274 rc->seqno += is_connected_state(rc->state) != is_connected_state(state);
1275 if (is_connected_state(state) && !is_connected_state(rc->state)) {
1276 rc->probably_admitted = false;
1278 if (rconn_is_connected(rc)) {
1279 rc->total_time_connected += elapsed_in_this_state(rc);
1281 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
1283 rc->state_entered = time_now();
1287 close_monitor(struct rconn *rc, size_t idx, int retval)
1288 OVS_REQUIRES(rc->mutex)
1290 VLOG_DBG("%s: closing monitor connection to %s: %s",
1291 rconn_get_name(rc), vconn_get_name(rc->monitors[idx]),
1292 ovs_retval_to_string(retval));
1293 rc->monitors[idx] = rc->monitors[--rc->n_monitors];
1297 copy_to_monitor(struct rconn *rc, const struct ofpbuf *b)
1298 OVS_REQUIRES(rc->mutex)
1300 struct ofpbuf *clone = NULL;
1304 for (i = 0; i < rc->n_monitors; ) {
1305 struct vconn *vconn = rc->monitors[i];
1308 clone = ofpbuf_clone(b);
1310 retval = vconn_send(vconn, clone);
1313 } else if (retval != EAGAIN) {
1314 close_monitor(rc, i, retval);
1319 ofpbuf_delete(clone);
1323 is_connected_state(enum state state)
1325 return (state & (S_ACTIVE | S_IDLE)) != 0;
1328 /* When a switch initially connects to a controller, the controller may spend a
1329 * little time examining the switch, looking at, for example, its datapath ID,
1330 * before it decides whether it is willing to control that switch. At that
1331 * point, it either disconnects or starts controlling the switch.
1333 * This function returns a guess to its caller about whether 'b' is OpenFlow
1334 * message that indicates that the controller has decided to control the
1335 * switch. It returns false if the message is one that a controller typically
1336 * uses to determine whether a switch is admissible, true if the message is one
1337 * that would typically be used only after the controller has admitted the
1340 is_admitted_msg(const struct ofpbuf *b)
1345 error = ofptype_decode(&type, b->data);
1353 case OFPTYPE_ECHO_REQUEST:
1354 case OFPTYPE_ECHO_REPLY:
1355 case OFPTYPE_FEATURES_REQUEST:
1356 case OFPTYPE_FEATURES_REPLY:
1357 case OFPTYPE_GET_CONFIG_REQUEST:
1358 case OFPTYPE_GET_CONFIG_REPLY:
1359 case OFPTYPE_SET_CONFIG:
1360 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1361 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1362 case OFPTYPE_GET_ASYNC_REQUEST:
1363 case OFPTYPE_GET_ASYNC_REPLY:
1364 case OFPTYPE_GROUP_STATS_REQUEST:
1365 case OFPTYPE_GROUP_STATS_REPLY:
1366 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
1367 case OFPTYPE_GROUP_DESC_STATS_REPLY:
1368 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
1369 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
1370 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
1371 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
1372 case OFPTYPE_TABLE_DESC_REQUEST:
1373 case OFPTYPE_TABLE_DESC_REPLY:
1376 case OFPTYPE_PACKET_IN:
1377 case OFPTYPE_FLOW_REMOVED:
1378 case OFPTYPE_PORT_STATUS:
1379 case OFPTYPE_PACKET_OUT:
1380 case OFPTYPE_FLOW_MOD:
1381 case OFPTYPE_GROUP_MOD:
1382 case OFPTYPE_PORT_MOD:
1383 case OFPTYPE_TABLE_MOD:
1384 case OFPTYPE_METER_MOD:
1385 case OFPTYPE_BARRIER_REQUEST:
1386 case OFPTYPE_BARRIER_REPLY:
1387 case OFPTYPE_DESC_STATS_REQUEST:
1388 case OFPTYPE_DESC_STATS_REPLY:
1389 case OFPTYPE_FLOW_STATS_REQUEST:
1390 case OFPTYPE_FLOW_STATS_REPLY:
1391 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1392 case OFPTYPE_AGGREGATE_STATS_REPLY:
1393 case OFPTYPE_TABLE_STATS_REQUEST:
1394 case OFPTYPE_TABLE_STATS_REPLY:
1395 case OFPTYPE_PORT_STATS_REQUEST:
1396 case OFPTYPE_PORT_STATS_REPLY:
1397 case OFPTYPE_QUEUE_STATS_REQUEST:
1398 case OFPTYPE_QUEUE_STATS_REPLY:
1399 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1400 case OFPTYPE_PORT_DESC_STATS_REPLY:
1401 case OFPTYPE_METER_STATS_REQUEST:
1402 case OFPTYPE_METER_STATS_REPLY:
1403 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
1404 case OFPTYPE_METER_CONFIG_STATS_REPLY:
1405 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
1406 case OFPTYPE_METER_FEATURES_STATS_REPLY:
1407 case OFPTYPE_ROLE_REQUEST:
1408 case OFPTYPE_ROLE_REPLY:
1409 case OFPTYPE_ROLE_STATUS:
1410 case OFPTYPE_REQUESTFORWARD:
1411 case OFPTYPE_TABLE_STATUS:
1412 case OFPTYPE_SET_FLOW_FORMAT:
1413 case OFPTYPE_FLOW_MOD_TABLE_ID:
1414 case OFPTYPE_SET_PACKET_IN_FORMAT:
1415 case OFPTYPE_FLOW_AGE:
1416 case OFPTYPE_SET_ASYNC_CONFIG:
1417 case OFPTYPE_SET_CONTROLLER_ID:
1418 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1419 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1420 case OFPTYPE_FLOW_MONITOR_CANCEL:
1421 case OFPTYPE_FLOW_MONITOR_PAUSED:
1422 case OFPTYPE_FLOW_MONITOR_RESUMED:
1423 case OFPTYPE_BUNDLE_CONTROL:
1424 case OFPTYPE_BUNDLE_ADD_MESSAGE:
1425 case OFPTYPE_NXT_TLV_TABLE_MOD:
1426 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
1427 case OFPTYPE_NXT_TLV_TABLE_REPLY:
1428 case OFPTYPE_NXT_RESUME:
1429 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
1430 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
1431 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
1432 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
1438 /* Returns true if 'rc' is currently logging information about connection
1439 * attempts, false if logging should be suppressed because 'rc' hasn't
1440 * successuflly connected in too long. */
1442 rconn_logging_connection_attempts__(const struct rconn *rc)
1443 OVS_REQUIRES(rc->mutex)
1445 return rc->backoff < rc->max_backoff;