d8152311e90d4975595c9587bebaa976915d1d5f
[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 "poll-loop.h"
39 #include "ofp-print.h"
40 #include "util.h"
41 #include "vconn.h"
42
43 #define THIS_MODULE VLM_controller_connection
44 #include "vlog.h"
45
46 struct controller_connection {
47     bool reliable;
48     const char *name;
49     struct vconn *vconn;
50     bool connected;
51     struct queue txq;
52     time_t backoff_deadline;
53     int backoff;
54 };
55
56 struct controller_connection *
57 controller_new(const char *name, bool reliable)
58 {
59     struct controller_connection *cc = xmalloc(sizeof *cc);
60     cc->reliable = reliable;
61     cc->name = name;
62     cc->vconn = NULL;
63     queue_init(&cc->txq);
64     cc->backoff_deadline = 0;
65     cc->backoff = 0;
66     return cc;
67 }
68
69 static int
70 try_send(struct controller_connection *cc)
71 {
72     int retval = 0;
73     struct buffer *next = cc->txq.head->next;
74     retval = vconn_send(cc->vconn, cc->txq.head);
75     if (retval) {
76         return retval;
77     }
78     queue_advance_head(&cc->txq, next);
79     return 0;
80 }
81
82 void
83 controller_run(struct controller_connection *cc)
84 {
85     if (!cc->vconn) {
86         if (time(0) >= cc->backoff_deadline) {
87             int retval;
88
89             retval = vconn_open(cc->name, &cc->vconn);
90             if (!retval) {
91                 cc->backoff_deadline = time(0) + cc->backoff;
92                 cc->connected = false;
93             } else {
94                 VLOG_WARN("%s: connection failed (%s)",
95                           cc->name, strerror(retval)); 
96                 controller_disconnect(cc, 0);
97             }
98         }
99     } else if (!cc->connected) {
100         int error = vconn_connect(cc->vconn);
101         if (!error) {
102             VLOG_WARN("%s: connected", cc->name);
103             if (vconn_is_passive(cc->vconn)) {
104                 fatal(0, "%s: passive vconn not supported in switch",
105                       cc->name);
106             }
107             cc->connected = true;
108         } else if (error != EAGAIN) {
109             VLOG_WARN("%s: connection failed (%s)",
110                       cc->name, strerror(error));
111             controller_disconnect(cc, 0);
112         }
113     } else {
114         while (cc->txq.n > 0) {
115             int error = try_send(cc);
116             if (error == EAGAIN) {
117                 break;
118             } else if (error) {
119                 controller_disconnect(cc, error);
120                 return;
121             }
122         }
123     }
124 }
125
126 void
127 controller_run_wait(struct controller_connection *cc) 
128 {
129     if (cc->vconn) {
130         if (cc->txq.n) {
131             vconn_wait(cc->vconn, WAIT_SEND);
132         }
133     } else {
134         poll_timer_wait((cc->backoff_deadline - time(0)) * 1000);
135     }
136 }
137
138 void
139 controller_disconnect(struct controller_connection *cc, int error) 
140 {
141     time_t now = time(0);
142     
143     if (cc->vconn) {
144         if (!cc->reliable) {
145             fatal(0, "%s: connection dropped", cc->name);
146         }
147
148         if (error > 0) {
149             VLOG_WARN("%s: connection dropped (%s)",
150                       cc->name, strerror(error)); 
151         } else if (error == EOF) { 
152             VLOG_WARN("%s: connection closed", cc->name); 
153         } else {
154             VLOG_WARN("%s: connection dropped", cc->name); 
155         }
156         vconn_close(cc->vconn);
157         cc->vconn = NULL;
158         queue_clear(&cc->txq);
159     }
160
161     if (now >= cc->backoff_deadline) {
162         cc->backoff = 1;
163     } else {
164         cc->backoff = MIN(60, MAX(1, 2 * cc->backoff));
165         VLOG_WARN("%s: waiting %d seconds before reconnect\n",
166                   cc->name, cc->backoff);
167     }
168     cc->backoff_deadline = now + cc->backoff;
169 }
170
171 struct buffer *
172 controller_recv(struct controller_connection *cc)
173 {
174     if (cc->vconn && cc->connected) {
175         struct buffer *buffer;
176         int error = vconn_recv(cc->vconn, &buffer);
177         if (!error) {
178             return buffer;
179         } else if (error != EAGAIN) {
180             controller_disconnect(cc, error); 
181         }
182     }
183     return NULL;
184 }
185
186 void
187 controller_recv_wait(struct controller_connection *cc) 
188 {
189     if (cc->vconn) {
190         vconn_wait(cc->vconn, WAIT_RECV);
191     }
192 }
193
194 void
195 controller_send(struct controller_connection *cc, struct buffer *b) 
196 {
197     if (cc->vconn) {
198         if (cc->txq.n < 128) {
199             queue_push_tail(&cc->txq, b);
200             if (cc->txq.n == 1) {
201                 try_send(cc);
202             }
203         } else {
204             VLOG_WARN("%s: controller queue overflow", cc->name);
205             buffer_delete(b);
206         } 
207     }
208 }