1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
42 #include "poll-loop.h"
43 #include "ofp-print.h"
48 #define THIS_MODULE VLM_rconn
53 STATE(BACKOFF, 1 << 1) \
54 STATE(CONNECTING, 1 << 2) \
55 STATE(ACTIVE, 1 << 3) \
58 #define STATE(NAME, VALUE) S_##NAME = VALUE,
64 state_name(enum state state)
67 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
74 /* A reliable connection to an OpenFlow switch or controller.
76 * See the large comment in rconn.h for more information. */
89 time_t backoff_deadline;
91 time_t last_connected;
92 unsigned int packets_sent;
94 /* These values are simply for statistics reporting, not used directly by
95 * anything internal to the rconn (or the secchan for that matter). */
96 unsigned int packets_received;
97 unsigned int n_attempted_connections, n_successful_connections;
99 unsigned long int total_time_connected;
101 /* If we can't connect to the peer, it could be for any number of reasons.
102 * Usually, one would assume it is because the peer is not running or
103 * because the network is partitioned. But it could also be because the
104 * network topology has changed, in which case the upper layer will need to
105 * reassess it (in particular, obtain a new IP address via DHCP and find
106 * the new location of the controller). We set this flag when we suspect
107 * that this could be the case. */
108 bool questionable_connectivity;
109 time_t last_questioned;
111 /* Throughout this file, "probe" is shorthand for "inactivity probe".
112 * When nothing has been received from the peer for a while, we send out
113 * an echo request as an inactivity probe packet. We should receive back
115 int probe_interval; /* Secs of inactivity before sending probe. */
118 static unsigned int sat_add(unsigned int x, unsigned int y);
119 static unsigned int sat_sub(unsigned int x, unsigned int y);
120 static unsigned int sat_mul(unsigned int x, unsigned int y);
121 static unsigned int elapsed_in_this_state(const struct rconn *);
122 static unsigned int timeout(const struct rconn *);
123 static bool timed_out(const struct rconn *);
124 static void state_transition(struct rconn *, enum state);
125 static int try_send(struct rconn *);
126 static int reconnect(struct rconn *);
127 static void disconnect(struct rconn *, int error);
128 static void flush_queue(struct rconn *);
129 static void question_connectivity(struct rconn *);
131 /* Creates a new rconn, connects it (reliably) to 'name', and returns it. */
133 rconn_new(const char *name, int inactivity_probe_interval, int max_backoff)
135 struct rconn *rc = rconn_create(inactivity_probe_interval, max_backoff);
136 rconn_connect(rc, name);
140 /* Creates a new rconn, connects it (unreliably) to 'vconn', and returns it. */
142 rconn_new_from_vconn(const char *name, struct vconn *vconn)
144 struct rconn *rc = rconn_create(60, 0);
145 rconn_connect_unreliably(rc, name, vconn);
149 /* Creates and returns a new rconn.
151 * 'probe_interval' is a number of seconds. If the interval passes once
152 * without an OpenFlow message being received from the peer, the rconn sends
153 * out an "echo request" message. If the interval passes again without a
154 * message being received, the rconn disconnects and re-connects to the peer.
155 * Setting 'probe_interval' to 0 disables this behavior.
157 * 'max_backoff' is the maximum number of seconds between attempts to connect
158 * to the peer. The actual interval starts at 1 second and doubles on each
159 * failure until it reaches 'max_backoff'. If 0 is specified, the default of
160 * 60 seconds is used. */
162 rconn_create(int probe_interval, int max_backoff)
164 struct rconn *rc = xcalloc(1, sizeof *rc);
167 rc->state_entered = time_now();
170 rc->name = xstrdup("void");
171 rc->reliable = false;
173 queue_init(&rc->txq);
176 rc->max_backoff = max_backoff ? max_backoff : 60;
177 rc->backoff_deadline = TIME_MIN;
178 rc->last_received = time_now();
179 rc->last_connected = time_now();
181 rc->packets_sent = 0;
183 rc->packets_received = 0;
184 rc->n_attempted_connections = 0;
185 rc->n_successful_connections = 0;
186 rc->creation_time = time_now();
187 rc->total_time_connected = 0;
189 rc->questionable_connectivity = false;
190 rc->last_questioned = time_now();
192 rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0;
198 rconn_connect(struct rconn *rc, const char *name)
200 rconn_disconnect(rc);
202 rc->name = xstrdup(name);
204 return reconnect(rc);
208 rconn_connect_unreliably(struct rconn *rc,
209 const char *name, struct vconn *vconn)
211 assert(vconn != NULL);
212 rconn_disconnect(rc);
214 rc->name = xstrdup(name);
215 rc->reliable = false;
217 rc->last_connected = time_now();
218 state_transition(rc, S_ACTIVE);
222 rconn_disconnect(struct rconn *rc)
225 vconn_close(rc->vconn);
229 rc->name = xstrdup("void");
230 rc->reliable = false;
233 rc->backoff_deadline = TIME_MIN;
235 state_transition(rc, S_VOID);
238 /* Disconnects 'rc' and frees the underlying storage. */
240 rconn_destroy(struct rconn *rc)
244 vconn_close(rc->vconn);
246 queue_destroy(&rc->txq);
252 timeout_VOID(const struct rconn *rc)
258 run_VOID(struct rconn *rc)
264 reconnect(struct rconn *rc)
268 VLOG_WARN("%s: connecting...", rc->name);
269 rc->n_attempted_connections++;
270 retval = vconn_open(rc->name, &rc->vconn);
272 rc->backoff_deadline = time_now() + rc->backoff;
273 state_transition(rc, S_CONNECTING);
275 VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(retval));
282 timeout_BACKOFF(const struct rconn *rc)
288 run_BACKOFF(struct rconn *rc)
296 timeout_CONNECTING(const struct rconn *rc)
298 return MAX(1, rc->backoff);
302 run_CONNECTING(struct rconn *rc)
304 int retval = vconn_connect(rc->vconn);
306 VLOG_WARN("%s: connected", rc->name);
307 rc->n_successful_connections++;
308 if (vconn_is_passive(rc->vconn)) {
309 error(0, "%s: passive vconn not supported", rc->name);
310 state_transition(rc, S_VOID);
312 state_transition(rc, S_ACTIVE);
313 rc->last_connected = rc->state_entered;
315 } else if (retval != EAGAIN) {
316 VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(retval));
317 disconnect(rc, retval);
318 } else if (timed_out(rc)) {
319 VLOG_WARN("%s: connection timed out", rc->name);
320 rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
326 do_tx_work(struct rconn *rc)
331 while (rc->txq.n > 0) {
332 int error = try_send(rc);
338 poll_immediate_wake();
343 timeout_ACTIVE(const struct rconn *rc)
345 if (rc->probe_interval) {
346 unsigned int base = MAX(rc->last_received, rc->state_entered);
347 unsigned int arg = base + rc->probe_interval - rc->state_entered;
354 run_ACTIVE(struct rconn *rc)
357 unsigned int base = MAX(rc->last_received, rc->state_entered);
358 rconn_send(rc, make_echo_request(), NULL);
359 VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
360 rc->name, (unsigned int) (time_now() - base));
361 state_transition(rc, S_IDLE);
369 timeout_IDLE(const struct rconn *rc)
371 return rc->probe_interval;
375 run_IDLE(struct rconn *rc)
378 question_connectivity(rc);
379 VLOG_ERR("%s: no response to inactivity probe after %u "
380 "seconds, disconnecting",
381 rc->name, elapsed_in_this_state(rc));
388 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
389 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
390 * connected, attempts to send packets in the send queue, if any. */
392 rconn_run(struct rconn *rc)
396 old_state = rc->state;
398 #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break;
404 } while (rc->state != old_state);
407 /* Causes the next call to poll_block() to wake up when rconn_run() should be
410 rconn_run_wait(struct rconn *rc)
412 unsigned int timeo = timeout(rc);
413 if (timeo != UINT_MAX) {
414 unsigned int expires = sat_add(rc->state_entered, timeo);
415 unsigned int remaining = sat_sub(expires, time_now());
416 poll_timer_wait(sat_mul(remaining, 1000));
419 if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {
420 vconn_wait(rc->vconn, WAIT_SEND);
424 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
425 * otherwise, returns a null pointer. The caller is responsible for freeing
426 * the packet (with buffer_delete()). */
428 rconn_recv(struct rconn *rc)
430 if (rc->state & (S_ACTIVE | S_IDLE)) {
431 struct buffer *buffer;
432 int error = vconn_recv(rc->vconn, &buffer);
434 rc->last_received = time_now();
435 rc->packets_received++;
436 if (rc->state == S_IDLE) {
437 state_transition(rc, S_ACTIVE);
440 } else if (error != EAGAIN) {
441 disconnect(rc, error);
447 /* Causes the next call to poll_block() to wake up when a packet may be ready
448 * to be received by vconn_recv() on 'rc'. */
450 rconn_recv_wait(struct rconn *rc)
453 vconn_wait(rc->vconn, WAIT_RECV);
457 /* Sends 'b' on 'rc'. Returns 0 if successful (in which case 'b' is
458 * destroyed), or ENOTCONN if 'rc' is not currently connected (in which case
459 * the caller retains ownership of 'b').
461 * If 'n_queued' is non-null, then '*n_queued' will be incremented while the
462 * packet is in flight, then decremented when it has been sent (or discarded
463 * due to disconnection). Because 'b' may be sent (or discarded) before this
464 * function returns, the caller may not be able to observe any change in
467 * There is no rconn_send_wait() function: an rconn has a send queue that it
468 * takes care of sending if you call rconn_run(), which will have the side
469 * effect of waking up poll_block(). */
471 rconn_send(struct rconn *rc, struct buffer *b, int *n_queued)
473 if (rconn_is_connected(rc)) {
474 b->private = n_queued;
478 queue_push_tail(&rc->txq, b);
479 if (rc->txq.n == 1) {
488 /* Sends 'b' on 'rc'. Increments '*n_queued' while the packet is in flight; it
489 * will be decremented when it has been sent (or discarded due to
490 * disconnection). Returns 0 if successful, EAGAIN if '*n_queued' is already
491 * at least as large of 'queue_limit', or ENOTCONN if 'rc' is not currently
492 * connected. Regardless of return value, 'b' is destroyed.
494 * Because 'b' may be sent (or discarded) before this function returns, the
495 * caller may not be able to observe any change in '*n_queued'.
497 * There is no rconn_send_wait() function: an rconn has a send queue that it
498 * takes care of sending if you call rconn_run(), which will have the side
499 * effect of waking up poll_block(). */
501 rconn_send_with_limit(struct rconn *rc, struct buffer *b,
502 int *n_queued, int queue_limit)
505 retval = *n_queued >= queue_limit ? EAGAIN : rconn_send(rc, b, n_queued);
512 /* Returns the total number of packets successfully sent on the underlying
513 * vconn. A packet is not counted as sent while it is still queued in the
514 * rconn, only when it has been successfuly passed to the vconn. */
516 rconn_packets_sent(const struct rconn *rc)
518 return rc->packets_sent;
521 /* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */
523 rconn_get_name(const struct rconn *rc)
528 /* Returns true if 'rconn' is connected or in the process of reconnecting,
529 * false if 'rconn' is disconnected and will not reconnect on its own. */
531 rconn_is_alive(const struct rconn *rconn)
533 return rconn->state != S_VOID;
536 /* Returns true if 'rconn' is connected, false otherwise. */
538 rconn_is_connected(const struct rconn *rconn)
540 return rconn->state & (S_ACTIVE | S_IDLE);
543 /* Returns 0 if 'rconn' is connected, otherwise the number of seconds that it
544 * has been disconnected. */
546 rconn_disconnected_duration(const struct rconn *rconn)
548 return rconn_is_connected(rconn) ? 0 : time_now() - rconn->last_received;
551 /* Returns the IP address of the peer, or 0 if the peer is not connected over
552 * an IP-based protocol or if its IP address is not known. */
554 rconn_get_ip(const struct rconn *rconn)
556 return rconn->vconn ? vconn_get_ip(rconn->vconn) : 0;
559 /* If 'rconn' can't connect to the peer, it could be for any number of reasons.
560 * Usually, one would assume it is because the peer is not running or because
561 * the network is partitioned. But it could also be because the network
562 * topology has changed, in which case the upper layer will need to reassess it
563 * (in particular, obtain a new IP address via DHCP and find the new location
564 * of the controller). When this appears that this might be the case, this
565 * function returns true. It also clears the questionability flag and prevents
566 * it from being set again for some time. */
568 rconn_is_connectivity_questionable(struct rconn *rconn)
570 bool questionable = rconn->questionable_connectivity;
571 rconn->questionable_connectivity = false;
575 /* Returns the total number of packets successfully received by the underlying
578 rconn_packets_received(const struct rconn *rc)
580 return rc->packets_received;
583 /* Returns a string representing the internal state of 'rc'. The caller must
584 * not modify or free the string. */
586 rconn_get_state(const struct rconn *rc)
588 return state_name(rc->state);
591 /* Returns the number of connection attempts made by 'rc', including any
592 * ongoing attempt that has not yet succeeded or failed. */
594 rconn_get_attempted_connections(const struct rconn *rc)
596 return rc->n_attempted_connections;
599 /* Returns the number of successful connection attempts made by 'rc'. */
601 rconn_get_successful_connections(const struct rconn *rc)
603 return rc->n_successful_connections;
606 /* Returns the time at which the last successful connection was made by
609 rconn_get_last_connection(const struct rconn *rc)
611 return rc->last_connected;
614 /* Returns the time at which 'rc' was created. */
616 rconn_get_creation_time(const struct rconn *rc)
618 return rc->creation_time;
621 /* Returns the approximate number of seconds that 'rc' has been connected. */
623 rconn_get_total_time_connected(const struct rconn *rc)
625 return (rc->total_time_connected
626 + (rconn_is_connected(rc) ? elapsed_in_this_state(rc) : 0));
629 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
630 * otherwise a positive errno value. */
632 try_send(struct rconn *rc)
635 struct buffer *next = rc->txq.head->next;
636 int *n_queued = rc->txq.head->private;
637 retval = vconn_send(rc->vconn, rc->txq.head);
639 if (retval != EAGAIN) {
640 disconnect(rc, retval);
648 queue_advance_head(&rc->txq, next);
652 /* Disconnects 'rc'. 'error' is used only for logging purposes. If it is
653 * nonzero, then it should be EOF to indicate the connection was closed by the
654 * peer in a normal fashion or a positive errno value. */
656 disconnect(struct rconn *rc, int error)
659 time_t now = time_now();
661 if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
663 VLOG_WARN("%s: connection dropped (%s)",
664 rc->name, strerror(error));
665 } else if (error == EOF) {
667 VLOG_WARN("%s: connection closed", rc->name);
670 VLOG_WARN("%s: connection dropped", rc->name);
672 vconn_close(rc->vconn);
677 if (now >= rc->backoff_deadline) {
680 rc->backoff = MIN(rc->max_backoff, MAX(1, 2 * rc->backoff));
681 VLOG_WARN("%s: waiting %d seconds before reconnect\n",
682 rc->name, rc->backoff);
684 rc->backoff_deadline = now + rc->backoff;
685 state_transition(rc, S_BACKOFF);
686 if (now - rc->last_connected > 60) {
687 question_connectivity(rc);
690 rconn_disconnect(rc);
694 /* Drops all the packets from 'rc''s send queue and decrements their queue
697 flush_queue(struct rconn *rc)
702 while (rc->txq.n > 0) {
703 struct buffer *b = queue_pop_head(&rc->txq);
704 int *n_queued = b->private;
710 poll_immediate_wake();
714 elapsed_in_this_state(const struct rconn *rc)
716 return time_now() - rc->state_entered;
720 timeout(const struct rconn *rc)
723 #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc);
732 timed_out(const struct rconn *rc)
734 return time_now() >= sat_add(rc->state_entered, timeout(rc));
738 state_transition(struct rconn *rc, enum state state)
740 if (rconn_is_connected(rc)) {
741 rc->total_time_connected += elapsed_in_this_state(rc);
743 VLOG_DBG("%s: entering %s", rc->name, state_name(state));
745 rc->state_entered = time_now();
749 sat_add(unsigned int x, unsigned int y)
751 return x + y >= x ? x + y : UINT_MAX;
755 sat_sub(unsigned int x, unsigned int y)
757 return x >= y ? x - y : 0;
761 sat_mul(unsigned int x, unsigned int y)
764 return x <= UINT_MAX / y ? x * y : UINT_MAX;
768 question_connectivity(struct rconn *rc)
770 time_t now = time_now();
771 if (now - rc->last_questioned > 60) {
772 rc->questionable_connectivity = true;
773 rc->last_questioned = now;