Update copyright on all non-GPL files
[openvswitch] / switch / controller.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include "controller.h"
35 #include <errno.h>
36 #include <string.h>
37 #include "buffer.h"
38 #include "forward.h"
39 #include "poll-loop.h"
40 #include "ofp-print.h"
41 #include "util.h"
42 #include "vconn.h"
43
44 #define THIS_MODULE VLM_controller_connection
45 #include "vlog.h"
46
47 void
48 controller_init(struct controller_connection *cc,
49                 const char *name, bool reliable)
50 {
51     cc->reliable = reliable;
52     cc->name = name;
53     cc->vconn = NULL;
54     queue_init(&cc->txq);
55     cc->backoff_deadline = 0;
56     cc->backoff = 0;
57 }
58
59 static int
60 try_send(struct controller_connection *cc)
61 {
62     int retval = 0;
63     struct buffer *next = cc->txq.head->next;
64     retval = vconn_send(cc->vconn, cc->txq.head);
65     if (retval) {
66         return retval;
67     }
68     queue_advance_head(&cc->txq, next);
69     return 0;
70 }
71
72 void
73 controller_run(struct controller_connection *cc, struct datapath *dp)
74 {
75     if (!cc->vconn) {
76         if (time(0) >= cc->backoff_deadline) {
77             int retval;
78
79             retval = vconn_open(cc->name, &cc->vconn);
80             if (!retval) {
81                 cc->backoff_deadline = time(0) + cc->backoff;
82                 cc->connected = false;
83             } else {
84                 VLOG_WARN("%s: connection failed (%s)",
85                           cc->name, strerror(retval)); 
86                 controller_disconnect(cc, 0);
87             }
88         }
89     } else if (!cc->connected) {
90         int error = vconn_connect(cc->vconn);
91         if (!error) {
92             VLOG_WARN("%s: connected", cc->name);
93             if (vconn_is_passive(cc->vconn)) {
94                 fatal(0, "%s: passive vconn not supported in switch",
95                       cc->name);
96             }
97             cc->connected = true;
98         } else if (error != EAGAIN) {
99             VLOG_WARN("%s: connection failed (%s)",
100                       cc->name, strerror(error));
101             controller_disconnect(cc, 0);
102         }
103     } else {
104         int iterations;
105
106         for (iterations = 0; iterations < 50; iterations++) {
107             struct buffer *buffer;
108             int error = vconn_recv(cc->vconn, &buffer);
109             if (!error) {
110                 fwd_control_input(dp, buffer->data, buffer->size);
111                 buffer_delete(buffer);
112             } else if (error == EAGAIN) {
113                 break;
114             } else {
115                 controller_disconnect(cc, error);
116                 return;
117             }
118         }
119
120         while (cc->txq.n > 0) {
121             int error = try_send(cc);
122             if (error == EAGAIN) {
123                 break;
124             } else if (error) {
125                 controller_disconnect(cc, error);
126                 return;
127             }
128         } 
129     }
130 }
131
132 void
133 controller_disconnect(struct controller_connection *cc, int error) 
134 {
135     time_t now = time(0);
136     
137     if (cc->vconn) {
138         if (!cc->reliable) {
139             fatal(0, "%s: connection dropped", cc->name);
140         }
141
142         if (error > 0) {
143             VLOG_WARN("%s: connection dropped (%s)",
144                       cc->name, strerror(error)); 
145         } else if (error == EOF) { 
146             VLOG_WARN("%s: connection closed", cc->name); 
147         } else {
148             VLOG_WARN("%s: connection dropped", cc->name); 
149         }
150         vconn_close(cc->vconn);
151         cc->vconn = NULL;
152         queue_clear(&cc->txq);
153     }
154
155     if (now >= cc->backoff_deadline) {
156         cc->backoff = 1;
157     } else {
158         cc->backoff = MIN(60, MAX(1, 2 * cc->backoff));
159         VLOG_WARN("%s: waiting %d seconds before reconnect\n",
160                   cc->name, cc->backoff);
161     }
162     cc->backoff_deadline = now + cc->backoff;
163 }
164
165 void
166 controller_wait(struct controller_connection *cc) 
167 {
168     if (cc->vconn) {
169         vconn_wait(cc->vconn, WAIT_RECV);
170         if (cc->txq.n) {
171             vconn_wait(cc->vconn, WAIT_SEND);
172         }
173     } else {
174         poll_timer_wait((cc->backoff_deadline - time(0)) * 1000);
175     }
176 }
177
178 void
179 controller_send(struct controller_connection *cc, struct buffer *b) 
180 {
181     if (cc->vconn) {
182         if (cc->txq.n < 128) {
183             queue_push_tail(&cc->txq, b);
184             if (cc->txq.n == 1) {
185                 try_send(cc);
186             }
187         } else {
188             VLOG_WARN("%s: controller queue overflow", cc->name);
189             buffer_delete(b);
190         } 
191     }
192 }