2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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"
32 #include "openvswitch/vconn.h"
33 #include "openvswitch/vlog.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 ovs_list txq; /* Contains "struct ofpbuf"s. */
102 time_t backoff_deadline;
103 time_t last_connected;
104 time_t last_disconnected;
108 /* In S_ACTIVE and S_IDLE, probably_admitted reports whether we believe
109 * that the peer has made a (positive) admission control decision on our
110 * connection. If we have not yet been (probably) admitted, then the
111 * connection does not reset the timer used for deciding whether the switch
112 * should go into fail-open mode.
114 * last_admitted reports the last time we believe such a positive admission
115 * control decision was made. */
116 bool probably_admitted;
117 time_t last_admitted;
119 /* These values are simply for statistics reporting, not used directly by
120 * anything internal to the rconn (or ofproto for that matter). */
121 unsigned int n_attempted_connections, n_successful_connections;
122 time_t creation_time;
123 unsigned long int total_time_connected;
125 /* Throughout this file, "probe" is shorthand for "inactivity probe". When
126 * no activity has been observed from the peer for a while, we send out an
127 * echo request as an inactivity probe packet. We should receive back a
130 * "Activity" is defined as either receiving an OpenFlow message from the
131 * peer or successfully sending a message that had been in 'txq'. */
132 int probe_interval; /* Secs of inactivity before sending probe. */
133 time_t last_activity; /* Last time we saw some activity. */
137 /* Messages sent or received are copied to the monitor connections. */
138 #define MAXIMUM_MONITORS 8
139 struct vconn *monitors[MAXIMUM_MONITORS];
142 uint32_t allowed_versions;
145 uint32_t rconn_get_allowed_versions(const struct rconn *rconn)
147 return rconn->allowed_versions;
150 static unsigned int elapsed_in_this_state(const struct rconn *rc)
151 OVS_REQUIRES(rc->mutex);
152 static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
153 static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
154 static void state_transition(struct rconn *rc, enum state)
155 OVS_REQUIRES(rc->mutex);
156 static void rconn_set_target__(struct rconn *rc,
157 const char *target, const char *name)
158 OVS_REQUIRES(rc->mutex);
159 static int rconn_send__(struct rconn *rc, struct ofpbuf *,
160 struct rconn_packet_counter *)
161 OVS_REQUIRES(rc->mutex);
162 static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex);
163 static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex);
164 static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
165 static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex);
166 static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
167 static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex);
168 static void close_monitor(struct rconn *rc, size_t idx, int retval)
169 OVS_REQUIRES(rc->mutex);
170 static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
171 static bool is_connected_state(enum state);
172 static bool is_admitted_msg(const struct ofpbuf *);
173 static bool rconn_logging_connection_attempts__(const struct rconn *rc)
174 OVS_REQUIRES(rc->mutex);
175 static int rconn_get_version__(const struct rconn *rconn)
176 OVS_REQUIRES(rconn->mutex);
178 /* The following prototypes duplicate those in rconn.h, but there we weren't
179 * able to add the OVS_EXCLUDED annotations because the definition of struct
180 * rconn was not visible. */
182 void rconn_set_max_backoff(struct rconn *rc, int max_backoff)
183 OVS_EXCLUDED(rc->mutex);
184 void rconn_connect(struct rconn *rc, const char *target, const char *name)
185 OVS_EXCLUDED(rc->mutex);
186 void rconn_connect_unreliably(struct rconn *rc,
187 struct vconn *vconn, const char *name)
188 OVS_EXCLUDED(rc->mutex);
189 void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
190 void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
191 void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
192 void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
193 struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
194 void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
195 int rconn_send(struct rconn *rc, struct ofpbuf *b,
196 struct rconn_packet_counter *counter)
197 OVS_EXCLUDED(rc->mutex);
198 int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
199 struct rconn_packet_counter *counter,
201 OVS_EXCLUDED(rc->mutex);
202 void rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
203 OVS_EXCLUDED(rc->mutex);
204 void rconn_set_name(struct rconn *rc, const char *new_name)
205 OVS_EXCLUDED(rc->mutex);
206 bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
207 int rconn_failure_duration(const struct rconn *rconn)
208 OVS_EXCLUDED(rconn->mutex);
209 ovs_be16 rconn_get_local_port(const struct rconn *rconn)
210 OVS_EXCLUDED(rconn->mutex);
211 int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
212 unsigned int rconn_count_txqlen(const struct rconn *rc)
213 OVS_EXCLUDED(rc->mutex);
216 /* Creates and returns a new rconn.
218 * 'probe_interval' is a number of seconds. If the interval passes once
219 * without an OpenFlow message being received from the peer, the rconn sends
220 * out an "echo request" message. If the interval passes again without a
221 * message being received, the rconn disconnects and re-connects to the peer.
222 * Setting 'probe_interval' to 0 disables this behavior.
224 * 'max_backoff' is the maximum number of seconds between attempts to connect
225 * to the peer. The actual interval starts at 1 second and doubles on each
226 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
229 * The new rconn is initially unconnected. Use rconn_connect() or
230 * rconn_connect_unreliably() to connect it.
232 * Connections made by the rconn will automatically negotiate an OpenFlow
233 * protocol version acceptable to both peers on the connection. The version
234 * negotiated will be one of those in the 'allowed_versions' bitmap: version
235 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying
236 * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.)
239 rconn_create(int probe_interval, int max_backoff, uint8_t dscp,
240 uint32_t allowed_versions)
242 struct rconn *rc = xzalloc(sizeof *rc);
244 ovs_mutex_init(&rc->mutex);
247 rc->state_entered = time_now();
250 rc->name = xstrdup("void");
251 rc->target = xstrdup("void");
252 rc->reliable = false;
257 rc->max_backoff = max_backoff ? max_backoff : 8;
258 rc->backoff_deadline = TIME_MIN;
259 rc->last_connected = TIME_MIN;
260 rc->last_disconnected = TIME_MIN;
263 rc->probably_admitted = false;
264 rc->last_admitted = time_now();
266 rc->n_attempted_connections = 0;
267 rc->n_successful_connections = 0;
268 rc->creation_time = time_now();
269 rc->total_time_connected = 0;
271 rc->last_activity = time_now();
273 rconn_set_probe_interval(rc, probe_interval);
274 rconn_set_dscp(rc, dscp);
277 rc->allowed_versions = allowed_versions;
283 rconn_set_max_backoff(struct rconn *rc, int max_backoff)
284 OVS_EXCLUDED(rc->mutex)
286 ovs_mutex_lock(&rc->mutex);
287 rc->max_backoff = MAX(1, max_backoff);
288 if (rc->state == S_BACKOFF && rc->backoff > max_backoff) {
289 rc->backoff = max_backoff;
290 if (rc->backoff_deadline > time_now() + max_backoff) {
291 rc->backoff_deadline = time_now() + max_backoff;
294 ovs_mutex_unlock(&rc->mutex);
298 rconn_get_max_backoff(const struct rconn *rc)
300 return rc->max_backoff;
304 rconn_set_dscp(struct rconn *rc, uint8_t dscp)
310 rconn_get_dscp(const struct rconn *rc)
316 rconn_set_probe_interval(struct rconn *rc, int probe_interval)
318 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
322 rconn_get_probe_interval(const struct rconn *rc)
324 return rc->probe_interval;
327 /* Drops any existing connection on 'rc', then sets up 'rc' to connect to
328 * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
329 * target in a form acceptable to vconn_open().
331 * If 'name' is nonnull, then it is used in log messages in place of 'target'.
332 * It should presumably give more information to a human reader than 'target',
333 * but it need not be acceptable to vconn_open(). */
335 rconn_connect(struct rconn *rc, const char *target, const char *name)
336 OVS_EXCLUDED(rc->mutex)
338 ovs_mutex_lock(&rc->mutex);
339 rconn_disconnect__(rc);
340 rconn_set_target__(rc, target, name);
343 ovs_mutex_unlock(&rc->mutex);
346 /* Drops any existing connection on 'rc', then configures 'rc' to use
347 * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
350 * By default, the target obtained from vconn_get_name(vconn) is used in log
351 * messages. If 'name' is nonnull, then it is used instead. It should
352 * presumably give more information to a human reader than the target, but it
353 * need not be acceptable to vconn_open(). */
355 rconn_connect_unreliably(struct rconn *rc,
356 struct vconn *vconn, const char *name)
357 OVS_EXCLUDED(rc->mutex)
359 ovs_assert(vconn != NULL);
361 ovs_mutex_lock(&rc->mutex);
362 rconn_disconnect__(rc);
363 rconn_set_target__(rc, vconn_get_name(vconn), name);
364 rc->reliable = false;
366 rc->last_connected = time_now();
367 state_transition(rc, S_ACTIVE);
368 ovs_mutex_unlock(&rc->mutex);
371 /* If 'rc' is connected, forces it to drop the connection and reconnect. */
373 rconn_reconnect(struct rconn *rc)
374 OVS_EXCLUDED(rc->mutex)
376 ovs_mutex_lock(&rc->mutex);
377 if (rc->state & (S_ACTIVE | S_IDLE)) {
378 VLOG_INFO("%s: disconnecting", rc->name);
381 ovs_mutex_unlock(&rc->mutex);
385 rconn_disconnect__(struct rconn *rc)
386 OVS_REQUIRES(rc->mutex)
388 if (rc->state != S_VOID) {
390 vconn_close(rc->vconn);
393 rconn_set_target__(rc, "void", NULL);
394 rc->reliable = false;
397 rc->backoff_deadline = TIME_MIN;
399 state_transition(rc, S_VOID);
404 rconn_disconnect(struct rconn *rc)
405 OVS_EXCLUDED(rc->mutex)
407 ovs_mutex_lock(&rc->mutex);
408 rconn_disconnect__(rc);
409 ovs_mutex_unlock(&rc->mutex);
412 /* Disconnects 'rc' and frees the underlying storage. */
414 rconn_destroy(struct rconn *rc)
419 ovs_mutex_lock(&rc->mutex);
422 vconn_close(rc->vconn);
424 ofpbuf_list_delete(&rc->txq);
425 for (i = 0; i < rc->n_monitors; i++) {
426 vconn_close(rc->monitors[i]);
428 ovs_mutex_unlock(&rc->mutex);
429 ovs_mutex_destroy(&rc->mutex);
436 timeout_VOID(const struct rconn *rc OVS_UNUSED)
437 OVS_REQUIRES(rc->mutex)
443 run_VOID(struct rconn *rc OVS_UNUSED)
444 OVS_REQUIRES(rc->mutex)
450 reconnect(struct rconn *rc)
451 OVS_REQUIRES(rc->mutex)
455 if (rconn_logging_connection_attempts__(rc)) {
456 VLOG_INFO("%s: connecting...", rc->name);
458 rc->n_attempted_connections++;
459 retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp,
462 rc->backoff_deadline = time_now() + rc->backoff;
463 state_transition(rc, S_CONNECTING);
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);
473 timeout_BACKOFF(const struct rconn *rc)
474 OVS_REQUIRES(rc->mutex)
480 run_BACKOFF(struct rconn *rc)
481 OVS_REQUIRES(rc->mutex)
489 timeout_CONNECTING(const struct rconn *rc)
490 OVS_REQUIRES(rc->mutex)
492 return MAX(1, rc->backoff);
496 run_CONNECTING(struct rconn *rc)
497 OVS_REQUIRES(rc->mutex)
499 int retval = vconn_connect(rc->vconn);
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));
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);
515 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
516 disconnect(rc, ETIMEDOUT);
521 do_tx_work(struct rconn *rc)
522 OVS_REQUIRES(rc->mutex)
524 if (list_is_empty(&rc->txq)) {
527 while (!list_is_empty(&rc->txq)) {
528 int error = try_send(rc);
532 rc->last_activity = time_now();
534 if (list_is_empty(&rc->txq)) {
535 poll_immediate_wake();
540 timeout_ACTIVE(const struct rconn *rc)
541 OVS_REQUIRES(rc->mutex)
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;
552 run_ACTIVE(struct rconn *rc)
553 OVS_REQUIRES(rc->mutex)
556 unsigned int base = MAX(rc->last_activity, rc->state_entered);
559 VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
560 rc->name, (unsigned int) (time_now() - base));
562 version = rconn_get_version__(rc);
563 ovs_assert(version >= 0 && version <= 0xff);
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);
577 timeout_IDLE(const struct rconn *rc)
578 OVS_REQUIRES(rc->mutex)
580 return rc->probe_interval;
584 run_IDLE(struct rconn *rc)
585 OVS_REQUIRES(rc->mutex)
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);
598 timeout_DISCONNECTED(const struct rconn *rc OVS_UNUSED)
599 OVS_REQUIRES(rc->mutex)
605 run_DISCONNECTED(struct rconn *rc OVS_UNUSED)
606 OVS_REQUIRES(rc->mutex)
611 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
612 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
613 * connected, attempts to send packets in the send queue, if any. */
615 rconn_run(struct rconn *rc)
616 OVS_EXCLUDED(rc->mutex)
621 ovs_mutex_lock(&rc->mutex);
625 vconn_run(rc->vconn);
627 error = vconn_get_status(rc->vconn);
629 report_error(rc, error);
630 disconnect(rc, error);
633 for (i = 0; i < rc->n_monitors; ) {
637 vconn_run(rc->monitors[i]);
639 /* Drain any stray message that came in on the monitor connection. */
640 retval = vconn_recv(rc->monitors[i], &msg);
643 } else if (retval != EAGAIN) {
644 close_monitor(rc, i, retval);
651 old_state = rc->state;
653 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
659 } while (rc->state != old_state);
660 ovs_mutex_unlock(&rc->mutex);
663 /* Causes the next call to poll_block() to wake up when rconn_run() should be
666 rconn_run_wait(struct rconn *rc)
667 OVS_EXCLUDED(rc->mutex)
672 ovs_mutex_lock(&rc->mutex);
674 vconn_run_wait(rc->vconn);
675 if ((rc->state & (S_ACTIVE | S_IDLE)) && !list_is_empty(&rc->txq)) {
676 vconn_wait(rc->vconn, WAIT_SEND);
679 for (i = 0; i < rc->n_monitors; i++) {
680 vconn_run_wait(rc->monitors[i]);
681 vconn_recv_wait(rc->monitors[i]);
685 if (timeo != UINT_MAX) {
686 long long int expires = sat_add(rc->state_entered, timeo);
687 poll_timer_wait_until(expires * 1000);
689 ovs_mutex_unlock(&rc->mutex);
692 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
693 * otherwise, returns a null pointer. The caller is responsible for freeing
694 * the packet (with ofpbuf_delete()). */
696 rconn_recv(struct rconn *rc)
697 OVS_EXCLUDED(rc->mutex)
699 struct ofpbuf *buffer = NULL;
701 ovs_mutex_lock(&rc->mutex);
702 if (rc->state & (S_ACTIVE | S_IDLE)) {
703 int error = vconn_recv(rc->vconn, &buffer);
705 copy_to_monitor(rc, buffer);
706 if (rc->probably_admitted || is_admitted_msg(buffer)
707 || time_now() - rc->last_connected >= 30) {
708 rc->probably_admitted = true;
709 rc->last_admitted = time_now();
711 rc->last_activity = time_now();
712 if (rc->state == S_IDLE) {
713 state_transition(rc, S_ACTIVE);
715 } else if (error != EAGAIN) {
716 report_error(rc, error);
717 disconnect(rc, error);
720 ovs_mutex_unlock(&rc->mutex);
725 /* Causes the next call to poll_block() to wake up when a packet may be ready
726 * to be received by vconn_recv() on 'rc'. */
728 rconn_recv_wait(struct rconn *rc)
729 OVS_EXCLUDED(rc->mutex)
731 ovs_mutex_lock(&rc->mutex);
733 vconn_wait(rc->vconn, WAIT_RECV);
735 ovs_mutex_unlock(&rc->mutex);
739 rconn_send__(struct rconn *rc, struct ofpbuf *b,
740 struct rconn_packet_counter *counter)
741 OVS_REQUIRES(rc->mutex)
743 if (rconn_is_connected(rc)) {
744 COVERAGE_INC(rconn_queued);
745 copy_to_monitor(rc, b);
748 rconn_packet_counter_inc(counter, b->size);
751 /* Reuse 'frame' as a private pointer while 'b' is in txq. */
754 list_push_back(&rc->txq, &b->list_node);
756 /* If the queue was empty before we added 'b', try to send some
757 * packets. (But if the queue had packets in it, it's because the
758 * vconn is backlogged and there's no point in stuffing more into it
759 * now. We'll get back to that in rconn_run().) */
760 if (rc->txq.next == &b->list_node) {
770 /* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not
771 * currently connected. Takes ownership of 'b'.
773 * If 'counter' is non-null, then 'counter' will be incremented while the
774 * packet is in flight, then decremented when it has been sent (or discarded
775 * due to disconnection). Because 'b' may be sent (or discarded) before this
776 * function returns, the caller may not be able to observe any change in
779 * There is no rconn_send_wait() function: an rconn has a send queue that it
780 * takes care of sending if you call rconn_run(), which will have the side
781 * effect of waking up poll_block(). */
783 rconn_send(struct rconn *rc, struct ofpbuf *b,
784 struct rconn_packet_counter *counter)
785 OVS_EXCLUDED(rc->mutex)
789 ovs_mutex_lock(&rc->mutex);
790 error = rconn_send__(rc, b, counter);
791 ovs_mutex_unlock(&rc->mutex);
796 /* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it
797 * will be decremented when it has been sent (or discarded due to
798 * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already
799 * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
800 * connected. Regardless of return value, 'b' is destroyed.
802 * Because 'b' may be sent (or discarded) before this function returns, the
803 * caller may not be able to observe any change in 'counter'.
805 * There is no rconn_send_wait() function: an rconn has a send queue that it
806 * takes care of sending if you call rconn_run(), which will have the side
807 * effect of waking up poll_block(). */
809 rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
810 struct rconn_packet_counter *counter, int queue_limit)
811 OVS_EXCLUDED(rc->mutex)
815 ovs_mutex_lock(&rc->mutex);
816 if (rconn_packet_counter_n_packets(counter) < queue_limit) {
817 error = rconn_send__(rc, b, counter);
819 COVERAGE_INC(rconn_overflow);
823 ovs_mutex_unlock(&rc->mutex);
828 /* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent
829 * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */
831 rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
832 OVS_EXCLUDED(rc->mutex)
834 ovs_mutex_lock(&rc->mutex);
835 if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) {
836 VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn));
837 rc->monitors[rc->n_monitors++] = vconn;
839 VLOG_DBG("too many monitor connections, discarding %s",
840 vconn_get_name(vconn));
843 ovs_mutex_unlock(&rc->mutex);
846 /* Returns 'rc''s name. This is a name for human consumption, appropriate for
847 * use in log messages. It is not necessarily a name that may be passed
848 * directly to, e.g., vconn_open(). */
850 rconn_get_name(const struct rconn *rc)
855 /* Sets 'rc''s name to 'new_name'. */
857 rconn_set_name(struct rconn *rc, const char *new_name)
858 OVS_EXCLUDED(rc->mutex)
860 ovs_mutex_lock(&rc->mutex);
862 rc->name = xstrdup(new_name);
863 ovs_mutex_unlock(&rc->mutex);
866 /* Returns 'rc''s target. This is intended to be a string that may be passed
867 * directly to, e.g., vconn_open(). */
869 rconn_get_target(const struct rconn *rc)
874 /* Returns true if 'rconn' is connected or in the process of reconnecting,
875 * false if 'rconn' is disconnected and will not reconnect on its own. */
877 rconn_is_alive(const struct rconn *rconn)
879 return rconn->state != S_VOID && rconn->state != S_DISCONNECTED;
882 /* Returns true if 'rconn' is connected, false otherwise. */
884 rconn_is_connected(const struct rconn *rconn)
886 return is_connected_state(rconn->state);
890 rconn_is_admitted__(const struct rconn *rconn)
891 OVS_REQUIRES(rconn->mutex)
893 return (rconn_is_connected(rconn)
894 && rconn->last_admitted >= rconn->last_connected);
897 /* Returns true if 'rconn' is connected and thought to have been accepted by
898 * the peer's admission-control policy. */
900 rconn_is_admitted(const struct rconn *rconn)
901 OVS_EXCLUDED(rconn->mutex)
905 ovs_mutex_lock(&rconn->mutex);
906 admitted = rconn_is_admitted__(rconn);
907 ovs_mutex_unlock(&rconn->mutex);
912 /* Returns 0 if 'rconn' is currently connected and considered to have been
913 * accepted by the peer's admission-control policy, otherwise the number of
914 * seconds since 'rconn' was last in such a state. */
916 rconn_failure_duration(const struct rconn *rconn)
917 OVS_EXCLUDED(rconn->mutex)
921 ovs_mutex_lock(&rconn->mutex);
922 duration = (rconn_is_admitted__(rconn)
924 : time_now() - rconn->last_admitted);
925 ovs_mutex_unlock(&rconn->mutex);
931 rconn_get_version__(const struct rconn *rconn)
932 OVS_REQUIRES(rconn->mutex)
934 return rconn->vconn ? vconn_get_version(rconn->vconn) : -1;
937 /* Returns the OpenFlow version negotiated with the peer, or -1 if there is
938 * currently no connection or if version negotiation is not yet complete. */
940 rconn_get_version(const struct rconn *rconn)
941 OVS_EXCLUDED(rconn->mutex)
945 ovs_mutex_lock(&rconn->mutex);
946 version = rconn_get_version__(rconn);
947 ovs_mutex_unlock(&rconn->mutex);
952 /* Returns a string representing the internal state of 'rc'. The caller must
953 * not modify or free the string. */
955 rconn_get_state(const struct rconn *rc)
957 return state_name(rc->state);
960 /* Returns the time at which the last successful connection was made by
961 * 'rc'. Returns TIME_MIN if never connected. */
963 rconn_get_last_connection(const struct rconn *rc)
965 return rc->last_connected;
968 /* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN
969 * if never disconnected. */
971 rconn_get_last_disconnect(const struct rconn *rc)
973 return rc->last_disconnected;
976 /* Returns 'rc''s current connection sequence number, a number that changes
977 * every time that 'rconn' connects or disconnects. */
979 rconn_get_connection_seqno(const struct rconn *rc)
984 /* Returns a value that explains why 'rc' last disconnected:
986 * - 0 means that the last disconnection was caused by a call to
987 * rconn_disconnect(), or that 'rc' is new and has not yet completed its
988 * initial connection or connection attempt.
990 * - EOF means that the connection was closed in the normal way by the peer.
992 * - A positive integer is an errno value that represents the error.
995 rconn_get_last_error(const struct rconn *rc)
997 return rc->last_error;
1000 /* Returns the number of messages queued for transmission on 'rc'. */
1002 rconn_count_txqlen(const struct rconn *rc)
1003 OVS_EXCLUDED(rc->mutex)
1007 ovs_mutex_lock(&rc->mutex);
1008 len = list_size(&rc->txq);
1009 ovs_mutex_unlock(&rc->mutex);
1014 struct rconn_packet_counter *
1015 rconn_packet_counter_create(void)
1017 struct rconn_packet_counter *c = xzalloc(sizeof *c);
1018 ovs_mutex_init(&c->mutex);
1019 ovs_mutex_lock(&c->mutex);
1021 ovs_mutex_unlock(&c->mutex);
1026 rconn_packet_counter_destroy(struct rconn_packet_counter *c)
1031 ovs_mutex_lock(&c->mutex);
1032 ovs_assert(c->ref_cnt > 0);
1033 dead = !--c->ref_cnt && !c->n_packets;
1034 ovs_mutex_unlock(&c->mutex);
1037 ovs_mutex_destroy(&c->mutex);
1044 rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
1046 ovs_mutex_lock(&c->mutex);
1048 c->n_bytes += n_bytes;
1049 ovs_mutex_unlock(&c->mutex);
1053 rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
1057 ovs_mutex_lock(&c->mutex);
1058 ovs_assert(c->n_packets > 0);
1059 ovs_assert(c->n_packets == 1
1060 ? c->n_bytes == n_bytes
1061 : c->n_bytes > n_bytes);
1063 c->n_bytes -= n_bytes;
1064 dead = !c->n_packets && !c->ref_cnt;
1065 ovs_mutex_unlock(&c->mutex);
1068 ovs_mutex_destroy(&c->mutex);
1074 rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
1078 ovs_mutex_lock(&c->mutex);
1080 ovs_mutex_unlock(&c->mutex);
1086 rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
1090 ovs_mutex_lock(&c->mutex);
1092 ovs_mutex_unlock(&c->mutex);
1097 /* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
1098 * is null, 'target' is used. */
1100 rconn_set_target__(struct rconn *rc, const char *target, const char *name)
1101 OVS_REQUIRES(rc->mutex)
1104 rc->name = xstrdup(name ? name : target);
1106 rc->target = xstrdup(target);
1109 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
1110 * otherwise a positive errno value. */
1112 try_send(struct rconn *rc)
1113 OVS_REQUIRES(rc->mutex)
1115 struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next);
1116 unsigned int n_bytes = msg->size;
1117 struct rconn_packet_counter *counter = msg->header;
1120 /* Eagerly remove 'msg' from the txq. We can't remove it from the list
1121 * after sending, if sending is successful, because it is then owned by the
1122 * vconn, which might have freed it already. */
1123 list_remove(&msg->list_node);
1126 retval = vconn_send(rc->vconn, msg);
1128 msg->header = counter;
1129 list_push_front(&rc->txq, &msg->list_node);
1130 if (retval != EAGAIN) {
1131 report_error(rc, retval);
1132 disconnect(rc, retval);
1136 COVERAGE_INC(rconn_sent);
1138 rconn_packet_counter_dec(counter, n_bytes);
1143 /* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive
1144 * errno value, or it may be EOF to indicate that the connection was closed
1147 report_error(struct rconn *rc, int error)
1148 OVS_REQUIRES(rc->mutex)
1150 /* On Windows, when a peer terminates without calling a closesocket()
1151 * on socket fd, we get WSAECONNRESET. Don't print warning messages
1155 || error == WSAECONNRESET
1158 /* If 'rc' isn't reliable, then we don't really expect this connection
1159 * to last forever anyway (probably it's a connection that we received
1160 * via accept()), so use DBG level to avoid cluttering the logs. */
1161 enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG;
1162 VLOG(level, "%s: connection closed by peer", rc->name);
1164 VLOG_WARN("%s: connection dropped (%s)",
1165 rc->name, ovs_strerror(error));
1169 /* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last
1172 * - 0 means that this disconnection is due to a request by 'rc''s client,
1173 * not due to any kind of network error.
1175 * - EOF means that the connection was closed in the normal way by the peer.
1177 * - A positive integer is an errno value that represents the error.
1180 disconnect(struct rconn *rc, int error)
1181 OVS_REQUIRES(rc->mutex)
1183 rc->last_error = error;
1185 vconn_close(rc->vconn);
1189 time_t now = time_now();
1191 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
1192 rc->last_disconnected = now;
1196 if (now >= rc->backoff_deadline) {
1198 } else if (rc->backoff < rc->max_backoff / 2) {
1199 rc->backoff = MAX(1, 2 * rc->backoff);
1200 VLOG_INFO("%s: waiting %d seconds before reconnect",
1201 rc->name, rc->backoff);
1203 if (rconn_logging_connection_attempts__(rc)) {
1204 VLOG_INFO("%s: continuing to retry connections in the "
1205 "background but suppressing further logging",
1208 rc->backoff = rc->max_backoff;
1210 rc->backoff_deadline = now + rc->backoff;
1211 state_transition(rc, S_BACKOFF);
1213 rc->last_disconnected = time_now();
1214 state_transition(rc, S_DISCONNECTED);
1218 /* Drops all the packets from 'rc''s send queue and decrements their queue
1221 flush_queue(struct rconn *rc)
1222 OVS_REQUIRES(rc->mutex)
1224 if (list_is_empty(&rc->txq)) {
1227 while (!list_is_empty(&rc->txq)) {
1228 struct ofpbuf *b = ofpbuf_from_list(list_pop_front(&rc->txq));
1229 struct rconn_packet_counter *counter = b->header;
1231 rconn_packet_counter_dec(counter, b->size);
1233 COVERAGE_INC(rconn_discarded);
1236 poll_immediate_wake();
1240 elapsed_in_this_state(const struct rconn *rc)
1241 OVS_REQUIRES(rc->mutex)
1243 return time_now() - rc->state_entered;
1247 timeout(const struct rconn *rc)
1248 OVS_REQUIRES(rc->mutex)
1250 switch (rc->state) {
1251 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
1260 timed_out(const struct rconn *rc)
1261 OVS_REQUIRES(rc->mutex)
1263 return time_now() >= sat_add(rc->state_entered, timeout(rc));
1267 state_transition(struct rconn *rc, enum state state)
1268 OVS_REQUIRES(rc->mutex)
1270 rc->seqno += (rc->state == S_ACTIVE) != (state == S_ACTIVE);
1271 if (is_connected_state(state) && !is_connected_state(rc->state)) {
1272 rc->probably_admitted = false;
1274 if (rconn_is_connected(rc)) {
1275 rc->total_time_connected += elapsed_in_this_state(rc);
1277 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
1279 rc->state_entered = time_now();
1283 close_monitor(struct rconn *rc, size_t idx, int retval)
1284 OVS_REQUIRES(rc->mutex)
1286 VLOG_DBG("%s: closing monitor connection to %s: %s",
1287 rconn_get_name(rc), vconn_get_name(rc->monitors[idx]),
1288 ovs_retval_to_string(retval));
1289 rc->monitors[idx] = rc->monitors[--rc->n_monitors];
1293 copy_to_monitor(struct rconn *rc, const struct ofpbuf *b)
1294 OVS_REQUIRES(rc->mutex)
1296 struct ofpbuf *clone = NULL;
1300 for (i = 0; i < rc->n_monitors; ) {
1301 struct vconn *vconn = rc->monitors[i];
1304 clone = ofpbuf_clone(b);
1306 retval = vconn_send(vconn, clone);
1309 } else if (retval != EAGAIN) {
1310 close_monitor(rc, i, retval);
1315 ofpbuf_delete(clone);
1319 is_connected_state(enum state state)
1321 return (state & (S_ACTIVE | S_IDLE)) != 0;
1324 /* When a switch initially connects to a controller, the controller may spend a
1325 * little time examining the switch, looking at, for example, its datapath ID,
1326 * before it decides whether it is willing to control that switch. At that
1327 * point, it either disconnects or starts controlling the switch.
1329 * This function returns a guess to its caller about whether 'b' is OpenFlow
1330 * message that indicates that the controller has decided to control the
1331 * switch. It returns false if the message is one that a controller typically
1332 * uses to determine whether a switch is admissible, true if the message is one
1333 * that would typically be used only after the controller has admitted the
1336 is_admitted_msg(const struct ofpbuf *b)
1341 error = ofptype_decode(&type, b->data);
1349 case OFPTYPE_ECHO_REQUEST:
1350 case OFPTYPE_ECHO_REPLY:
1351 case OFPTYPE_FEATURES_REQUEST:
1352 case OFPTYPE_FEATURES_REPLY:
1353 case OFPTYPE_GET_CONFIG_REQUEST:
1354 case OFPTYPE_GET_CONFIG_REPLY:
1355 case OFPTYPE_SET_CONFIG:
1356 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1357 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1358 case OFPTYPE_GET_ASYNC_REQUEST:
1359 case OFPTYPE_GET_ASYNC_REPLY:
1360 case OFPTYPE_GROUP_STATS_REQUEST:
1361 case OFPTYPE_GROUP_STATS_REPLY:
1362 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
1363 case OFPTYPE_GROUP_DESC_STATS_REPLY:
1364 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
1365 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
1366 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
1367 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
1370 case OFPTYPE_PACKET_IN:
1371 case OFPTYPE_FLOW_REMOVED:
1372 case OFPTYPE_PORT_STATUS:
1373 case OFPTYPE_PACKET_OUT:
1374 case OFPTYPE_FLOW_MOD:
1375 case OFPTYPE_GROUP_MOD:
1376 case OFPTYPE_PORT_MOD:
1377 case OFPTYPE_TABLE_MOD:
1378 case OFPTYPE_METER_MOD:
1379 case OFPTYPE_BARRIER_REQUEST:
1380 case OFPTYPE_BARRIER_REPLY:
1381 case OFPTYPE_DESC_STATS_REQUEST:
1382 case OFPTYPE_DESC_STATS_REPLY:
1383 case OFPTYPE_FLOW_STATS_REQUEST:
1384 case OFPTYPE_FLOW_STATS_REPLY:
1385 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1386 case OFPTYPE_AGGREGATE_STATS_REPLY:
1387 case OFPTYPE_TABLE_STATS_REQUEST:
1388 case OFPTYPE_TABLE_STATS_REPLY:
1389 case OFPTYPE_PORT_STATS_REQUEST:
1390 case OFPTYPE_PORT_STATS_REPLY:
1391 case OFPTYPE_QUEUE_STATS_REQUEST:
1392 case OFPTYPE_QUEUE_STATS_REPLY:
1393 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1394 case OFPTYPE_PORT_DESC_STATS_REPLY:
1395 case OFPTYPE_METER_STATS_REQUEST:
1396 case OFPTYPE_METER_STATS_REPLY:
1397 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
1398 case OFPTYPE_METER_CONFIG_STATS_REPLY:
1399 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
1400 case OFPTYPE_METER_FEATURES_STATS_REPLY:
1401 case OFPTYPE_ROLE_REQUEST:
1402 case OFPTYPE_ROLE_REPLY:
1403 case OFPTYPE_ROLE_STATUS:
1404 case OFPTYPE_SET_FLOW_FORMAT:
1405 case OFPTYPE_FLOW_MOD_TABLE_ID:
1406 case OFPTYPE_SET_PACKET_IN_FORMAT:
1407 case OFPTYPE_FLOW_AGE:
1408 case OFPTYPE_SET_ASYNC_CONFIG:
1409 case OFPTYPE_SET_CONTROLLER_ID:
1410 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1411 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1412 case OFPTYPE_FLOW_MONITOR_CANCEL:
1413 case OFPTYPE_FLOW_MONITOR_PAUSED:
1414 case OFPTYPE_FLOW_MONITOR_RESUMED:
1415 case OFPTYPE_BUNDLE_CONTROL:
1416 case OFPTYPE_BUNDLE_ADD_MESSAGE:
1422 /* Returns true if 'rc' is currently logging information about connection
1423 * attempts, false if logging should be suppressed because 'rc' hasn't
1424 * successuflly connected in too long. */
1426 rconn_logging_connection_attempts__(const struct rconn *rc)
1427 OVS_REQUIRES(rc->mutex)
1429 return rc->backoff < rc->max_backoff;