2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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);
44 STATE(BACKOFF, 1 << 1) \
45 STATE(CONNECTING, 1 << 2) \
46 STATE(ACTIVE, 1 << 3) \
49 #define STATE(NAME, VALUE) S_##NAME = VALUE,
55 state_name(enum state state)
58 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
65 /* A reliable connection to an OpenFlow switch or controller.
67 * See the large comment in rconn.h for more information. */
69 struct ovs_mutex mutex;
75 char *name; /* Human-readable descriptive name. */
76 char *target; /* vconn name, passed to vconn_open(). */
79 struct list txq; /* Contains "struct ofpbuf"s. */
83 time_t backoff_deadline;
84 time_t last_connected;
85 time_t last_disconnected;
86 unsigned int packets_sent;
90 /* In S_ACTIVE and S_IDLE, probably_admitted reports whether we believe
91 * that the peer has made a (positive) admission control decision on our
92 * connection. If we have not yet been (probably) admitted, then the
93 * connection does not reset the timer used for deciding whether the switch
94 * should go into fail-open mode.
96 * last_admitted reports the last time we believe such a positive admission
97 * control decision was made. */
98 bool probably_admitted;
101 /* These values are simply for statistics reporting, not used directly by
102 * anything internal to the rconn (or ofproto for that matter). */
103 unsigned int packets_received;
104 unsigned int n_attempted_connections, n_successful_connections;
105 time_t creation_time;
106 unsigned long int total_time_connected;
108 /* Throughout this file, "probe" is shorthand for "inactivity probe". When
109 * no activity has been observed from the peer for a while, we send out an
110 * echo request as an inactivity probe packet. We should receive back a
113 * "Activity" is defined as either receiving an OpenFlow message from the
114 * peer or successfully sending a message that had been in 'txq'. */
115 int probe_interval; /* Secs of inactivity before sending probe. */
116 time_t last_activity; /* Last time we saw some activity. */
118 /* When we create a vconn we obtain these values, to save them past the end
119 * of the vconn's lifetime. Otherwise, in-band control will only allow
120 * traffic when a vconn is actually open, but it is nice to allow ARP to
121 * complete even between connection attempts, and it is also polite to
122 * allow traffic from other switches to go through to the controller
123 * whether or not we are connected.
125 * We don't cache the local port, because that changes from one connection
126 * attempt to the next. */
127 ovs_be32 local_ip, remote_ip;
128 ovs_be16 remote_port;
131 /* Messages sent or received are copied to the monitor connections. */
132 #define MAX_MONITORS 8
133 struct vconn *monitors[8];
136 uint32_t allowed_versions;
139 uint32_t rconn_get_allowed_versions(const struct rconn *rconn)
141 return rconn->allowed_versions;
144 static unsigned int elapsed_in_this_state(const struct rconn *rc)
145 OVS_REQUIRES(rc->mutex);
146 static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
147 static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex);
148 static void state_transition(struct rconn *rc, enum state)
149 OVS_REQUIRES(rc->mutex);
150 static void rconn_set_target__(struct rconn *rc,
151 const char *target, const char *name)
152 OVS_REQUIRES(rc->mutex);
153 static int rconn_send__(struct rconn *rc, struct ofpbuf *,
154 struct rconn_packet_counter *)
155 OVS_REQUIRES(rc->mutex);
156 static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex);
157 static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex);
158 static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
159 static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex);
160 static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex);
161 static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex);
162 static void close_monitor(struct rconn *rc, size_t idx, int retval)
163 OVS_REQUIRES(rc->mutex);
164 static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
165 static bool is_connected_state(enum state);
166 static bool is_admitted_msg(const struct ofpbuf *);
167 static bool rconn_logging_connection_attempts__(const struct rconn *rc)
168 OVS_REQUIRES(rc->mutex);
169 static int rconn_get_version__(const struct rconn *rconn)
170 OVS_REQUIRES(rconn->mutex);
172 /* The following prototypes duplicate those in rconn.h, but there we weren't
173 * able to add the OVS_EXCLUDED annotations because the definition of struct
174 * rconn was not visible. */
176 void rconn_set_max_backoff(struct rconn *rc, int max_backoff)
177 OVS_EXCLUDED(rc->mutex);
178 void rconn_connect(struct rconn *rc, const char *target, const char *name)
179 OVS_EXCLUDED(rc->mutex);
180 void rconn_connect_unreliably(struct rconn *rc,
181 struct vconn *vconn, const char *name)
182 OVS_EXCLUDED(rc->mutex);
183 void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
184 void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
185 void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
186 void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
187 struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
188 void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex);
189 int rconn_send(struct rconn *rc, struct ofpbuf *b,
190 struct rconn_packet_counter *counter)
191 OVS_EXCLUDED(rc->mutex);
192 int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
193 struct rconn_packet_counter *counter,
195 OVS_EXCLUDED(rc->mutex);
196 void rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
197 OVS_EXCLUDED(rc->mutex);
198 void rconn_set_name(struct rconn *rc, const char *new_name)
199 OVS_EXCLUDED(rc->mutex);
200 bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
201 int rconn_failure_duration(const struct rconn *rconn)
202 OVS_EXCLUDED(rconn->mutex);
203 ovs_be16 rconn_get_local_port(const struct rconn *rconn)
204 OVS_EXCLUDED(rconn->mutex);
205 int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex);
206 unsigned int rconn_count_txqlen(const struct rconn *rc)
207 OVS_EXCLUDED(rc->mutex);
210 /* Creates and returns a new rconn.
212 * 'probe_interval' is a number of seconds. If the interval passes once
213 * without an OpenFlow message being received from the peer, the rconn sends
214 * out an "echo request" message. If the interval passes again without a
215 * message being received, the rconn disconnects and re-connects to the peer.
216 * Setting 'probe_interval' to 0 disables this behavior.
218 * 'max_backoff' is the maximum number of seconds between attempts to connect
219 * to the peer. The actual interval starts at 1 second and doubles on each
220 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
223 * The new rconn is initially unconnected. Use rconn_connect() or
224 * rconn_connect_unreliably() to connect it.
226 * Connections made by the rconn will automatically negotiate an OpenFlow
227 * protocol version acceptable to both peers on the connection. The version
228 * negotiated will be one of those in the 'allowed_versions' bitmap: version
229 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying
230 * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.)
233 rconn_create(int probe_interval, int max_backoff, uint8_t dscp,
234 uint32_t allowed_versions)
236 struct rconn *rc = xzalloc(sizeof *rc);
238 ovs_mutex_init(&rc->mutex);
241 rc->state_entered = time_now();
244 rc->name = xstrdup("void");
245 rc->target = xstrdup("void");
246 rc->reliable = false;
251 rc->max_backoff = max_backoff ? max_backoff : 8;
252 rc->backoff_deadline = TIME_MIN;
253 rc->last_connected = TIME_MIN;
254 rc->last_disconnected = TIME_MIN;
257 rc->packets_sent = 0;
259 rc->probably_admitted = false;
260 rc->last_admitted = time_now();
262 rc->packets_received = 0;
263 rc->n_attempted_connections = 0;
264 rc->n_successful_connections = 0;
265 rc->creation_time = time_now();
266 rc->total_time_connected = 0;
268 rc->last_activity = time_now();
270 rconn_set_probe_interval(rc, probe_interval);
271 rconn_set_dscp(rc, dscp);
274 rc->allowed_versions = allowed_versions;
280 rconn_set_max_backoff(struct rconn *rc, int max_backoff)
281 OVS_EXCLUDED(rc->mutex)
283 ovs_mutex_lock(&rc->mutex);
284 rc->max_backoff = MAX(1, max_backoff);
285 if (rc->state == S_BACKOFF && rc->backoff > max_backoff) {
286 rc->backoff = max_backoff;
287 if (rc->backoff_deadline > time_now() + max_backoff) {
288 rc->backoff_deadline = time_now() + max_backoff;
291 ovs_mutex_unlock(&rc->mutex);
295 rconn_get_max_backoff(const struct rconn *rc)
297 return rc->max_backoff;
301 rconn_set_dscp(struct rconn *rc, uint8_t dscp)
307 rconn_get_dscp(const struct rconn *rc)
313 rconn_set_probe_interval(struct rconn *rc, int probe_interval)
315 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
319 rconn_get_probe_interval(const struct rconn *rc)
321 return rc->probe_interval;
324 /* Drops any existing connection on 'rc', then sets up 'rc' to connect to
325 * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
326 * target in a form acceptable to vconn_open().
328 * If 'name' is nonnull, then it is used in log messages in place of 'target'.
329 * It should presumably give more information to a human reader than 'target',
330 * but it need not be acceptable to vconn_open(). */
332 rconn_connect(struct rconn *rc, const char *target, const char *name)
333 OVS_EXCLUDED(rc->mutex)
335 ovs_mutex_lock(&rc->mutex);
336 rconn_disconnect__(rc);
337 rconn_set_target__(rc, target, name);
340 ovs_mutex_unlock(&rc->mutex);
343 /* Drops any existing connection on 'rc', then configures 'rc' to use
344 * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
347 * By default, the target obtained from vconn_get_name(vconn) is used in log
348 * messages. If 'name' is nonnull, then it is used instead. It should
349 * presumably give more information to a human reader than the target, but it
350 * need not be acceptable to vconn_open(). */
352 rconn_connect_unreliably(struct rconn *rc,
353 struct vconn *vconn, const char *name)
354 OVS_EXCLUDED(rc->mutex)
356 ovs_assert(vconn != NULL);
358 ovs_mutex_lock(&rc->mutex);
359 rconn_disconnect__(rc);
360 rconn_set_target__(rc, vconn_get_name(vconn), name);
361 rc->reliable = false;
363 rc->last_connected = time_now();
364 state_transition(rc, S_ACTIVE);
365 ovs_mutex_unlock(&rc->mutex);
368 /* If 'rc' is connected, forces it to drop the connection and reconnect. */
370 rconn_reconnect(struct rconn *rc)
371 OVS_EXCLUDED(rc->mutex)
373 ovs_mutex_lock(&rc->mutex);
374 if (rc->state & (S_ACTIVE | S_IDLE)) {
375 VLOG_INFO("%s: disconnecting", rc->name);
378 ovs_mutex_unlock(&rc->mutex);
382 rconn_disconnect__(struct rconn *rc)
383 OVS_REQUIRES(rc->mutex)
385 if (rc->state != S_VOID) {
387 vconn_close(rc->vconn);
390 rconn_set_target__(rc, "void", NULL);
391 rc->reliable = false;
394 rc->backoff_deadline = TIME_MIN;
396 state_transition(rc, S_VOID);
401 rconn_disconnect(struct rconn *rc)
402 OVS_EXCLUDED(rc->mutex)
404 ovs_mutex_lock(&rc->mutex);
405 rconn_disconnect__(rc);
406 ovs_mutex_unlock(&rc->mutex);
409 /* Disconnects 'rc' and frees the underlying storage. */
411 rconn_destroy(struct rconn *rc)
416 ovs_mutex_lock(&rc->mutex);
419 vconn_close(rc->vconn);
421 ofpbuf_list_delete(&rc->txq);
422 for (i = 0; i < rc->n_monitors; i++) {
423 vconn_close(rc->monitors[i]);
425 ovs_mutex_unlock(&rc->mutex);
426 ovs_mutex_destroy(&rc->mutex);
433 timeout_VOID(const struct rconn *rc OVS_UNUSED)
434 OVS_REQUIRES(rc->mutex)
440 run_VOID(struct rconn *rc OVS_UNUSED)
441 OVS_REQUIRES(rc->mutex)
447 reconnect(struct rconn *rc)
448 OVS_REQUIRES(rc->mutex)
452 if (rconn_logging_connection_attempts__(rc)) {
453 VLOG_INFO("%s: connecting...", rc->name);
455 rc->n_attempted_connections++;
456 retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp,
459 rc->remote_ip = vconn_get_remote_ip(rc->vconn);
460 rc->local_ip = vconn_get_local_ip(rc->vconn);
461 rc->remote_port = vconn_get_remote_port(rc->vconn);
462 rc->backoff_deadline = time_now() + rc->backoff;
463 state_transition(rc, S_CONNECTING);
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);
597 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
598 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
599 * connected, attempts to send packets in the send queue, if any. */
601 rconn_run(struct rconn *rc)
602 OVS_EXCLUDED(rc->mutex)
607 ovs_mutex_lock(&rc->mutex);
609 vconn_run(rc->vconn);
611 for (i = 0; i < rc->n_monitors; ) {
615 vconn_run(rc->monitors[i]);
617 /* Drain any stray message that came in on the monitor connection. */
618 retval = vconn_recv(rc->monitors[i], &msg);
621 } else if (retval != EAGAIN) {
622 close_monitor(rc, i, retval);
629 old_state = rc->state;
631 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
637 } while (rc->state != old_state);
638 ovs_mutex_unlock(&rc->mutex);
641 /* Causes the next call to poll_block() to wake up when rconn_run() should be
644 rconn_run_wait(struct rconn *rc)
645 OVS_EXCLUDED(rc->mutex)
650 ovs_mutex_lock(&rc->mutex);
652 vconn_run_wait(rc->vconn);
653 if ((rc->state & (S_ACTIVE | S_IDLE)) && !list_is_empty(&rc->txq)) {
654 vconn_wait(rc->vconn, WAIT_SEND);
657 for (i = 0; i < rc->n_monitors; i++) {
658 vconn_run_wait(rc->monitors[i]);
659 vconn_recv_wait(rc->monitors[i]);
663 if (timeo != UINT_MAX) {
664 long long int expires = sat_add(rc->state_entered, timeo);
665 poll_timer_wait_until(expires * 1000);
667 ovs_mutex_unlock(&rc->mutex);
670 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
671 * otherwise, returns a null pointer. The caller is responsible for freeing
672 * the packet (with ofpbuf_delete()). */
674 rconn_recv(struct rconn *rc)
675 OVS_EXCLUDED(rc->mutex)
677 struct ofpbuf *buffer = NULL;
679 ovs_mutex_lock(&rc->mutex);
680 if (rc->state & (S_ACTIVE | S_IDLE)) {
681 int error = vconn_recv(rc->vconn, &buffer);
683 copy_to_monitor(rc, buffer);
684 if (rc->probably_admitted || is_admitted_msg(buffer)
685 || time_now() - rc->last_connected >= 30) {
686 rc->probably_admitted = true;
687 rc->last_admitted = time_now();
689 rc->last_activity = time_now();
690 rc->packets_received++;
691 if (rc->state == S_IDLE) {
692 state_transition(rc, S_ACTIVE);
694 } else if (error != EAGAIN) {
695 report_error(rc, error);
696 disconnect(rc, error);
699 ovs_mutex_unlock(&rc->mutex);
704 /* Causes the next call to poll_block() to wake up when a packet may be ready
705 * to be received by vconn_recv() on 'rc'. */
707 rconn_recv_wait(struct rconn *rc)
708 OVS_EXCLUDED(rc->mutex)
710 ovs_mutex_lock(&rc->mutex);
712 vconn_wait(rc->vconn, WAIT_RECV);
714 ovs_mutex_unlock(&rc->mutex);
718 rconn_send__(struct rconn *rc, struct ofpbuf *b,
719 struct rconn_packet_counter *counter)
720 OVS_REQUIRES(rc->mutex)
722 if (rconn_is_connected(rc)) {
723 COVERAGE_INC(rconn_queued);
724 copy_to_monitor(rc, b);
725 b->private_p = counter;
727 rconn_packet_counter_inc(counter, b->size);
729 list_push_back(&rc->txq, &b->list_node);
731 /* If the queue was empty before we added 'b', try to send some
732 * packets. (But if the queue had packets in it, it's because the
733 * vconn is backlogged and there's no point in stuffing more into it
734 * now. We'll get back to that in rconn_run().) */
735 if (rc->txq.next == &b->list_node) {
745 /* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not
746 * currently connected. Takes ownership of 'b'.
748 * If 'counter' is non-null, then 'counter' will be incremented while the
749 * packet is in flight, then decremented when it has been sent (or discarded
750 * due to disconnection). Because 'b' may be sent (or discarded) before this
751 * function returns, the caller may not be able to observe any change in
754 * There is no rconn_send_wait() function: an rconn has a send queue that it
755 * takes care of sending if you call rconn_run(), which will have the side
756 * effect of waking up poll_block(). */
758 rconn_send(struct rconn *rc, struct ofpbuf *b,
759 struct rconn_packet_counter *counter)
760 OVS_EXCLUDED(rc->mutex)
764 ovs_mutex_lock(&rc->mutex);
765 error = rconn_send__(rc, b, counter);
766 ovs_mutex_unlock(&rc->mutex);
771 /* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it
772 * will be decremented when it has been sent (or discarded due to
773 * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already
774 * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
775 * connected. Regardless of return value, 'b' is destroyed.
777 * Because 'b' may be sent (or discarded) before this function returns, the
778 * caller may not be able to observe any change in 'counter'.
780 * There is no rconn_send_wait() function: an rconn has a send queue that it
781 * takes care of sending if you call rconn_run(), which will have the side
782 * effect of waking up poll_block(). */
784 rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
785 struct rconn_packet_counter *counter, int queue_limit)
786 OVS_EXCLUDED(rc->mutex)
790 ovs_mutex_lock(&rc->mutex);
791 if (rconn_packet_counter_n_packets(counter) < queue_limit) {
792 error = rconn_send__(rc, b, counter);
794 COVERAGE_INC(rconn_overflow);
798 ovs_mutex_unlock(&rc->mutex);
803 /* Returns the total number of packets successfully sent on the underlying
804 * vconn. A packet is not counted as sent while it is still queued in the
805 * rconn, only when it has been successfuly passed to the vconn. */
807 rconn_packets_sent(const struct rconn *rc)
809 return rc->packets_sent;
812 /* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent
813 * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */
815 rconn_add_monitor(struct rconn *rc, struct vconn *vconn)
816 OVS_EXCLUDED(rc->mutex)
818 ovs_mutex_lock(&rc->mutex);
819 if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) {
820 VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn));
821 rc->monitors[rc->n_monitors++] = vconn;
823 VLOG_DBG("too many monitor connections, discarding %s",
824 vconn_get_name(vconn));
827 ovs_mutex_unlock(&rc->mutex);
830 /* Returns 'rc''s name. This is a name for human consumption, appropriate for
831 * use in log messages. It is not necessarily a name that may be passed
832 * directly to, e.g., vconn_open(). */
834 rconn_get_name(const struct rconn *rc)
839 /* Sets 'rc''s name to 'new_name'. */
841 rconn_set_name(struct rconn *rc, const char *new_name)
842 OVS_EXCLUDED(rc->mutex)
844 ovs_mutex_lock(&rc->mutex);
846 rc->name = xstrdup(new_name);
847 ovs_mutex_unlock(&rc->mutex);
850 /* Returns 'rc''s target. This is intended to be a string that may be passed
851 * directly to, e.g., vconn_open(). */
853 rconn_get_target(const struct rconn *rc)
858 /* Returns true if 'rconn' is connected or in the process of reconnecting,
859 * false if 'rconn' is disconnected and will not reconnect on its own. */
861 rconn_is_alive(const struct rconn *rconn)
863 return rconn->state != S_VOID;
866 /* Returns true if 'rconn' is connected, false otherwise. */
868 rconn_is_connected(const struct rconn *rconn)
870 return is_connected_state(rconn->state);
874 rconn_is_admitted__(const struct rconn *rconn)
875 OVS_REQUIRES(rconn->mutex)
877 return (rconn_is_connected(rconn)
878 && rconn->last_admitted >= rconn->last_connected);
881 /* Returns true if 'rconn' is connected and thought to have been accepted by
882 * the peer's admission-control policy. */
884 rconn_is_admitted(const struct rconn *rconn)
885 OVS_EXCLUDED(rconn->mutex)
889 ovs_mutex_lock(&rconn->mutex);
890 admitted = rconn_is_admitted__(rconn);
891 ovs_mutex_unlock(&rconn->mutex);
896 /* Returns 0 if 'rconn' is currently connected and considered to have been
897 * accepted by the peer's admission-control policy, otherwise the number of
898 * seconds since 'rconn' was last in such a state. */
900 rconn_failure_duration(const struct rconn *rconn)
901 OVS_EXCLUDED(rconn->mutex)
905 ovs_mutex_lock(&rconn->mutex);
906 duration = (rconn_is_admitted__(rconn)
908 : time_now() - rconn->last_admitted);
909 ovs_mutex_unlock(&rconn->mutex);
914 /* Returns the IP address of the peer, or 0 if the peer's IP address is not
917 rconn_get_remote_ip(const struct rconn *rconn)
919 return rconn->remote_ip;
922 /* Returns the transport port of the peer, or 0 if the peer's port is not
925 rconn_get_remote_port(const struct rconn *rconn)
927 return rconn->remote_port;
930 /* Returns the IP address used to connect to the peer, or 0 if the
931 * connection is not an IP-based protocol or if its IP address is not
934 rconn_get_local_ip(const struct rconn *rconn)
936 return rconn->local_ip;
939 /* Returns the transport port used to connect to the peer, or 0 if the
940 * connection does not contain a port or if the port is not known. */
942 rconn_get_local_port(const struct rconn *rconn)
943 OVS_EXCLUDED(rconn->mutex)
947 ovs_mutex_lock(&rconn->mutex);
948 port = rconn->vconn ? vconn_get_local_port(rconn->vconn) : 0;
949 ovs_mutex_unlock(&rconn->mutex);
955 rconn_get_version__(const struct rconn *rconn)
956 OVS_REQUIRES(rconn->mutex)
958 return rconn->vconn ? vconn_get_version(rconn->vconn) : -1;
961 /* Returns the OpenFlow version negotiated with the peer, or -1 if there is
962 * currently no connection or if version negotiation is not yet complete. */
964 rconn_get_version(const struct rconn *rconn)
965 OVS_EXCLUDED(rconn->mutex)
969 ovs_mutex_lock(&rconn->mutex);
970 version = rconn_get_version__(rconn);
971 ovs_mutex_unlock(&rconn->mutex);
976 /* Returns the total number of packets successfully received by the underlying
979 rconn_packets_received(const struct rconn *rc)
981 return rc->packets_received;
984 /* Returns a string representing the internal state of 'rc'. The caller must
985 * not modify or free the string. */
987 rconn_get_state(const struct rconn *rc)
989 return state_name(rc->state);
992 /* Returns the time at which the last successful connection was made by
993 * 'rc'. Returns TIME_MIN if never connected. */
995 rconn_get_last_connection(const struct rconn *rc)
997 return rc->last_connected;
1000 /* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN
1001 * if never disconnected. */
1003 rconn_get_last_disconnect(const struct rconn *rc)
1005 return rc->last_disconnected;
1008 /* Returns 'rc''s current connection sequence number, a number that changes
1009 * every time that 'rconn' connects or disconnects. */
1011 rconn_get_connection_seqno(const struct rconn *rc)
1016 /* Returns a value that explains why 'rc' last disconnected:
1018 * - 0 means that the last disconnection was caused by a call to
1019 * rconn_disconnect(), or that 'rc' is new and has not yet completed its
1020 * initial connection or connection attempt.
1022 * - EOF means that the connection was closed in the normal way by the peer.
1024 * - A positive integer is an errno value that represents the error.
1027 rconn_get_last_error(const struct rconn *rc)
1029 return rc->last_error;
1032 /* Returns the number of messages queued for transmission on 'rc'. */
1034 rconn_count_txqlen(const struct rconn *rc)
1035 OVS_EXCLUDED(rc->mutex)
1039 ovs_mutex_lock(&rc->mutex);
1040 len = list_size(&rc->txq);
1041 ovs_mutex_unlock(&rc->mutex);
1046 struct rconn_packet_counter *
1047 rconn_packet_counter_create(void)
1049 struct rconn_packet_counter *c = xzalloc(sizeof *c);
1050 ovs_mutex_init(&c->mutex);
1051 ovs_mutex_lock(&c->mutex);
1053 ovs_mutex_unlock(&c->mutex);
1058 rconn_packet_counter_destroy(struct rconn_packet_counter *c)
1063 ovs_mutex_lock(&c->mutex);
1064 ovs_assert(c->ref_cnt > 0);
1065 dead = !--c->ref_cnt && !c->n_packets;
1066 ovs_mutex_unlock(&c->mutex);
1069 ovs_mutex_destroy(&c->mutex);
1076 rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
1078 ovs_mutex_lock(&c->mutex);
1080 c->n_bytes += n_bytes;
1081 ovs_mutex_unlock(&c->mutex);
1085 rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
1089 ovs_mutex_lock(&c->mutex);
1090 ovs_assert(c->n_packets > 0);
1091 ovs_assert(c->n_packets == 1
1092 ? c->n_bytes == n_bytes
1093 : c->n_bytes > n_bytes);
1095 c->n_bytes -= n_bytes;
1096 dead = !c->n_packets && !c->ref_cnt;
1097 ovs_mutex_unlock(&c->mutex);
1100 ovs_mutex_destroy(&c->mutex);
1106 rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
1110 ovs_mutex_lock(&c->mutex);
1112 ovs_mutex_unlock(&c->mutex);
1118 rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
1122 ovs_mutex_lock(&c->mutex);
1124 ovs_mutex_unlock(&c->mutex);
1129 /* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
1130 * is null, 'target' is used.
1132 * Also, clear out the cached IP address and port information, since changing
1133 * the target also likely changes these values. */
1135 rconn_set_target__(struct rconn *rc, const char *target, const char *name)
1136 OVS_REQUIRES(rc->mutex)
1139 rc->name = xstrdup(name ? name : target);
1141 rc->target = xstrdup(target);
1144 rc->remote_port = 0;
1147 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
1148 * otherwise a positive errno value. */
1150 try_send(struct rconn *rc)
1151 OVS_REQUIRES(rc->mutex)
1153 struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next);
1154 unsigned int n_bytes = msg->size;
1155 struct rconn_packet_counter *counter = msg->private_p;
1158 /* Eagerly remove 'msg' from the txq. We can't remove it from the list
1159 * after sending, if sending is successful, because it is then owned by the
1160 * vconn, which might have freed it already. */
1161 list_remove(&msg->list_node);
1163 retval = vconn_send(rc->vconn, msg);
1165 list_push_front(&rc->txq, &msg->list_node);
1166 if (retval != EAGAIN) {
1167 report_error(rc, retval);
1168 disconnect(rc, retval);
1172 COVERAGE_INC(rconn_sent);
1175 rconn_packet_counter_dec(counter, n_bytes);
1180 /* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive
1181 * errno value, or it may be EOF to indicate that the connection was closed
1184 report_error(struct rconn *rc, int error)
1185 OVS_REQUIRES(rc->mutex)
1188 /* If 'rc' isn't reliable, then we don't really expect this connection
1189 * to last forever anyway (probably it's a connection that we received
1190 * via accept()), so use DBG level to avoid cluttering the logs. */
1191 enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG;
1192 VLOG(level, "%s: connection closed by peer", rc->name);
1194 VLOG_WARN("%s: connection dropped (%s)",
1195 rc->name, ovs_strerror(error));
1199 /* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last
1202 * - 0 means that this disconnection is due to a request by 'rc''s client,
1203 * not due to any kind of network error.
1205 * - EOF means that the connection was closed in the normal way by the peer.
1207 * - A positive integer is an errno value that represents the error.
1210 disconnect(struct rconn *rc, int error)
1211 OVS_REQUIRES(rc->mutex)
1213 rc->last_error = error;
1215 time_t now = time_now();
1217 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
1218 rc->last_disconnected = now;
1219 vconn_close(rc->vconn);
1224 if (now >= rc->backoff_deadline) {
1226 } else if (rc->backoff < rc->max_backoff / 2) {
1227 rc->backoff = MAX(1, 2 * rc->backoff);
1228 VLOG_INFO("%s: waiting %d seconds before reconnect",
1229 rc->name, rc->backoff);
1231 if (rconn_logging_connection_attempts__(rc)) {
1232 VLOG_INFO("%s: continuing to retry connections in the "
1233 "background but suppressing further logging",
1236 rc->backoff = rc->max_backoff;
1238 rc->backoff_deadline = now + rc->backoff;
1239 state_transition(rc, S_BACKOFF);
1241 rc->last_disconnected = time_now();
1242 rconn_disconnect__(rc);
1246 /* Drops all the packets from 'rc''s send queue and decrements their queue
1249 flush_queue(struct rconn *rc)
1250 OVS_REQUIRES(rc->mutex)
1252 if (list_is_empty(&rc->txq)) {
1255 while (!list_is_empty(&rc->txq)) {
1256 struct ofpbuf *b = ofpbuf_from_list(list_pop_front(&rc->txq));
1257 struct rconn_packet_counter *counter = b->private_p;
1259 rconn_packet_counter_dec(counter, b->size);
1261 COVERAGE_INC(rconn_discarded);
1264 poll_immediate_wake();
1268 elapsed_in_this_state(const struct rconn *rc)
1269 OVS_REQUIRES(rc->mutex)
1271 return time_now() - rc->state_entered;
1275 timeout(const struct rconn *rc)
1276 OVS_REQUIRES(rc->mutex)
1278 switch (rc->state) {
1279 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
1288 timed_out(const struct rconn *rc)
1289 OVS_REQUIRES(rc->mutex)
1291 return time_now() >= sat_add(rc->state_entered, timeout(rc));
1295 state_transition(struct rconn *rc, enum state state)
1296 OVS_REQUIRES(rc->mutex)
1298 rc->seqno += (rc->state == S_ACTIVE) != (state == S_ACTIVE);
1299 if (is_connected_state(state) && !is_connected_state(rc->state)) {
1300 rc->probably_admitted = false;
1302 if (rconn_is_connected(rc)) {
1303 rc->total_time_connected += elapsed_in_this_state(rc);
1305 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
1307 rc->state_entered = time_now();
1311 close_monitor(struct rconn *rc, size_t idx, int retval)
1312 OVS_REQUIRES(rc->mutex)
1314 VLOG_DBG("%s: closing monitor connection to %s: %s",
1315 rconn_get_name(rc), vconn_get_name(rc->monitors[idx]),
1316 ovs_retval_to_string(retval));
1317 rc->monitors[idx] = rc->monitors[--rc->n_monitors];
1321 copy_to_monitor(struct rconn *rc, const struct ofpbuf *b)
1322 OVS_REQUIRES(rc->mutex)
1324 struct ofpbuf *clone = NULL;
1328 for (i = 0; i < rc->n_monitors; ) {
1329 struct vconn *vconn = rc->monitors[i];
1332 clone = ofpbuf_clone(b);
1334 retval = vconn_send(vconn, clone);
1337 } else if (retval != EAGAIN) {
1338 close_monitor(rc, i, retval);
1343 ofpbuf_delete(clone);
1347 is_connected_state(enum state state)
1349 return (state & (S_ACTIVE | S_IDLE)) != 0;
1353 is_admitted_msg(const struct ofpbuf *b)
1358 error = ofptype_decode(&type, b->data);
1366 case OFPTYPE_ECHO_REQUEST:
1367 case OFPTYPE_ECHO_REPLY:
1368 case OFPTYPE_FEATURES_REQUEST:
1369 case OFPTYPE_FEATURES_REPLY:
1370 case OFPTYPE_GET_CONFIG_REQUEST:
1371 case OFPTYPE_GET_CONFIG_REPLY:
1372 case OFPTYPE_SET_CONFIG:
1373 /* FIXME: Change the following once they are implemented: */
1374 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1375 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1376 case OFPTYPE_GET_ASYNC_REQUEST:
1377 case OFPTYPE_GET_ASYNC_REPLY:
1378 case OFPTYPE_GROUP_STATS_REQUEST:
1379 case OFPTYPE_GROUP_STATS_REPLY:
1380 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
1381 case OFPTYPE_GROUP_DESC_STATS_REPLY:
1382 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
1383 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
1384 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
1385 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
1388 case OFPTYPE_PACKET_IN:
1389 case OFPTYPE_FLOW_REMOVED:
1390 case OFPTYPE_PORT_STATUS:
1391 case OFPTYPE_PACKET_OUT:
1392 case OFPTYPE_FLOW_MOD:
1393 case OFPTYPE_GROUP_MOD:
1394 case OFPTYPE_PORT_MOD:
1395 case OFPTYPE_TABLE_MOD:
1396 case OFPTYPE_METER_MOD:
1397 case OFPTYPE_BARRIER_REQUEST:
1398 case OFPTYPE_BARRIER_REPLY:
1399 case OFPTYPE_DESC_STATS_REQUEST:
1400 case OFPTYPE_DESC_STATS_REPLY:
1401 case OFPTYPE_FLOW_STATS_REQUEST:
1402 case OFPTYPE_FLOW_STATS_REPLY:
1403 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1404 case OFPTYPE_AGGREGATE_STATS_REPLY:
1405 case OFPTYPE_TABLE_STATS_REQUEST:
1406 case OFPTYPE_TABLE_STATS_REPLY:
1407 case OFPTYPE_PORT_STATS_REQUEST:
1408 case OFPTYPE_PORT_STATS_REPLY:
1409 case OFPTYPE_QUEUE_STATS_REQUEST:
1410 case OFPTYPE_QUEUE_STATS_REPLY:
1411 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1412 case OFPTYPE_PORT_DESC_STATS_REPLY:
1413 case OFPTYPE_METER_STATS_REQUEST:
1414 case OFPTYPE_METER_STATS_REPLY:
1415 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
1416 case OFPTYPE_METER_CONFIG_STATS_REPLY:
1417 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
1418 case OFPTYPE_METER_FEATURES_STATS_REPLY:
1419 case OFPTYPE_ROLE_REQUEST:
1420 case OFPTYPE_ROLE_REPLY:
1421 case OFPTYPE_SET_FLOW_FORMAT:
1422 case OFPTYPE_FLOW_MOD_TABLE_ID:
1423 case OFPTYPE_SET_PACKET_IN_FORMAT:
1424 case OFPTYPE_FLOW_AGE:
1425 case OFPTYPE_SET_ASYNC_CONFIG:
1426 case OFPTYPE_SET_CONTROLLER_ID:
1427 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1428 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1429 case OFPTYPE_FLOW_MONITOR_CANCEL:
1430 case OFPTYPE_FLOW_MONITOR_PAUSED:
1431 case OFPTYPE_FLOW_MONITOR_RESUMED:
1437 /* Returns true if 'rc' is currently logging information about connection
1438 * attempts, false if logging should be suppressed because 'rc' hasn't
1439 * successuflly connected in too long. */
1441 rconn_logging_connection_attempts__(const struct rconn *rc)
1442 OVS_REQUIRES(rc->mutex)
1444 return rc->backoff < rc->max_backoff;