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.
34 #include "controller.h"
38 #include "poll-loop.h"
39 #include "ofp-print.h"
43 #define THIS_MODULE VLM_controller_connection
47 controller_init(struct controller_connection *cc,
48 const char *name, bool reliable)
50 cc->reliable = reliable;
54 cc->backoff_deadline = 0;
59 try_send(struct controller_connection *cc)
62 struct buffer *next = cc->txq.head->next;
63 retval = vconn_send(cc->vconn, cc->txq.head);
67 queue_advance_head(&cc->txq, next);
72 controller_run(struct controller_connection *cc)
75 if (time(0) >= cc->backoff_deadline) {
78 retval = vconn_open(cc->name, &cc->vconn);
80 cc->backoff_deadline = time(0) + cc->backoff;
81 cc->connected = false;
83 VLOG_WARN("%s: connection failed (%s)",
84 cc->name, strerror(retval));
85 controller_disconnect(cc, 0);
88 } else if (!cc->connected) {
89 int error = vconn_connect(cc->vconn);
91 VLOG_WARN("%s: connected", cc->name);
92 if (vconn_is_passive(cc->vconn)) {
93 fatal(0, "%s: passive vconn not supported in switch",
97 } else if (error != EAGAIN) {
98 VLOG_WARN("%s: connection failed (%s)",
99 cc->name, strerror(error));
100 controller_disconnect(cc, 0);
103 while (cc->txq.n > 0) {
104 int error = try_send(cc);
105 if (error == EAGAIN) {
108 controller_disconnect(cc, error);
116 controller_run_wait(struct controller_connection *cc)
120 vconn_wait(cc->vconn, WAIT_SEND);
123 poll_timer_wait((cc->backoff_deadline - time(0)) * 1000);
128 controller_disconnect(struct controller_connection *cc, int error)
130 time_t now = time(0);
134 fatal(0, "%s: connection dropped", cc->name);
138 VLOG_WARN("%s: connection dropped (%s)",
139 cc->name, strerror(error));
140 } else if (error == EOF) {
141 VLOG_WARN("%s: connection closed", cc->name);
143 VLOG_WARN("%s: connection dropped", cc->name);
145 vconn_close(cc->vconn);
147 queue_clear(&cc->txq);
150 if (now >= cc->backoff_deadline) {
153 cc->backoff = MIN(60, MAX(1, 2 * cc->backoff));
154 VLOG_WARN("%s: waiting %d seconds before reconnect\n",
155 cc->name, cc->backoff);
157 cc->backoff_deadline = now + cc->backoff;
161 controller_recv(struct controller_connection *cc)
163 if (cc->vconn && cc->connected) {
164 struct buffer *buffer;
165 int error = vconn_recv(cc->vconn, &buffer);
168 } else if (error != EAGAIN) {
169 controller_disconnect(cc, error);
176 controller_recv_wait(struct controller_connection *cc)
179 vconn_wait(cc->vconn, WAIT_RECV);
184 controller_send(struct controller_connection *cc, struct buffer *b)
187 if (cc->txq.n < 128) {
188 queue_push_tail(&cc->txq, b);
189 if (cc->txq.n == 1) {
193 VLOG_WARN("%s: controller queue overflow", cc->name);