1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 #include "controller.h"
27 #include "poll-loop.h"
28 #include "ofp-print.h"
32 #define THIS_MODULE VLM_controller_connection
36 controller_init(struct controller_connection *cc,
37 const char *name, bool reliable)
39 cc->reliable = reliable;
43 cc->backoff_deadline = 0;
48 try_send(struct controller_connection *cc)
51 struct buffer *next = cc->txq.head->next;
52 retval = vconn_send(cc->vconn, cc->txq.head);
56 queue_advance_head(&cc->txq, next);
61 controller_run(struct controller_connection *cc, struct datapath *dp)
64 if (time(0) >= cc->backoff_deadline) {
67 retval = vconn_open(cc->name, &cc->vconn);
69 cc->backoff_deadline = time(0) + cc->backoff;
70 cc->connected = false;
72 VLOG_WARN("%s: connection failed (%s)",
73 cc->name, strerror(retval));
74 controller_disconnect(cc, 0);
77 } else if (!cc->connected) {
78 int error = vconn_connect(cc->vconn);
80 VLOG_WARN("%s: connected", cc->name);
81 if (vconn_is_passive(cc->vconn)) {
82 fatal(0, "%s: passive vconn not supported in switch",
86 } else if (error != EAGAIN) {
87 VLOG_WARN("%s: connection failed (%s)",
88 cc->name, strerror(error));
89 controller_disconnect(cc, 0);
94 for (iterations = 0; iterations < 50; iterations++) {
95 struct buffer *buffer;
96 int error = vconn_recv(cc->vconn, &buffer);
98 fwd_control_input(dp, buffer->data, buffer->size);
99 buffer_delete(buffer);
100 } else if (error == EAGAIN) {
103 controller_disconnect(cc, error);
108 while (cc->txq.n > 0) {
109 int error = try_send(cc);
110 if (error == EAGAIN) {
113 controller_disconnect(cc, error);
121 controller_disconnect(struct controller_connection *cc, int error)
123 time_t now = time(0);
127 fatal(0, "%s: connection dropped", cc->name);
131 VLOG_WARN("%s: connection dropped (%s)",
132 cc->name, strerror(error));
133 } else if (error == EOF) {
134 VLOG_WARN("%s: connection closed", cc->name);
136 VLOG_WARN("%s: connection dropped", cc->name);
138 vconn_close(cc->vconn);
140 queue_clear(&cc->txq);
143 if (now >= cc->backoff_deadline) {
146 cc->backoff = MIN(60, MAX(1, 2 * cc->backoff));
147 VLOG_WARN("%s: waiting %d seconds before reconnect\n",
148 cc->name, cc->backoff);
150 cc->backoff_deadline = now + cc->backoff;
154 controller_wait(struct controller_connection *cc)
157 vconn_wait(cc->vconn, WAIT_RECV);
159 vconn_wait(cc->vconn, WAIT_SEND);
162 poll_timer_wait((cc->backoff_deadline - time(0)) * 1000);
167 controller_send(struct controller_connection *cc, struct buffer *b)
170 if (cc->txq.n < 128) {
171 queue_push_tail(&cc->txq, b);
172 if (cc->txq.n == 1) {
176 VLOG_WARN("%s: controller queue overflow", cc->name);