Merge remote branch 'repo/master' into stats
[sliver-openvswitch.git] / secchan / secchan.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 <errno.h>
35 #include <getopt.h>
36 #include <poll.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "buffer.h"
43 #include "command-line.h"
44 #include "compiler.h"
45 #include "fault.h"
46 #include "list.h"
47 #include "util.h"
48 #include "rconn.h"
49 #include "vconn-ssl.h"
50 #include "vlog-socket.h"
51 #include "openflow.h"
52 #include "poll-loop.h"
53 #include "vconn.h"
54
55 #include "vlog.h"
56 #define THIS_MODULE VLM_secchan
57
58 static void parse_options(int argc, char *argv[]);
59 static void usage(void) NO_RETURN;
60
61 static const char *listen_vconn_name;
62
63 struct half {
64     struct rconn *rconn;
65     struct buffer *rxbuf;
66 };
67
68 struct relay {
69     struct list node;
70     struct half halves[2];
71 };
72
73 static struct list relays = LIST_INITIALIZER(&relays);
74
75 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
76 static void relay_create(struct rconn *, struct rconn *);
77 static void relay_run(struct relay *);
78 static void relay_wait(struct relay *);
79 static void relay_destroy(struct relay *);
80
81 int
82 main(int argc, char *argv[])
83 {
84     struct vconn *listen_vconn;
85     const char *nl_name;
86     int retval;
87
88     set_program_name(argv[0]);
89     register_fault_handlers();
90     vlog_init();
91     parse_options(argc, argv);
92
93     if (argc - optind != 2) {
94         fatal(0,
95               "need exactly two non-option arguments; use --help for usage");
96     }
97     nl_name = argv[optind];
98     if (strncmp(nl_name, "nl:", 3)
99         || strlen(nl_name) < 4
100         || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
101         fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
102     }
103
104     if (listen_vconn_name) {
105         retval = vconn_open(listen_vconn_name, &listen_vconn);
106         if (retval && retval != EAGAIN) {
107             fatal(retval, "opening %s", listen_vconn_name);
108         }
109         if (!vconn_is_passive(listen_vconn)) {
110             fatal(0, "%s is not a passive vconn", listen_vconn_name);
111         }
112     } else {
113         listen_vconn = NULL;
114     }
115
116     retval = vlog_server_listen(NULL, NULL);
117     if (retval) {
118         fatal(retval, "Could not listen for vlog connections");
119     }
120
121     relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1));
122     for (;;) {
123         struct relay *r, *n;
124
125         /* Do work. */
126         LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
127             relay_run(r);
128         }
129         if (listen_vconn) {
130             for (;;) {
131                 struct vconn *new_remote;
132                 retval = vconn_accept(listen_vconn, &new_remote);
133                 if (retval) {
134                     if (retval != EAGAIN) {
135                         VLOG_WARN("accept failed (%s)", strerror(retval));
136                     }
137                     break;
138                 }
139                 new_management_connection(nl_name, new_remote);
140             }
141         }
142
143         /* Wait for something to happen. */
144         LIST_FOR_EACH (r, struct relay, node, &relays) {
145             relay_wait(r);
146         }
147         if (listen_vconn) {
148             vconn_accept_wait(listen_vconn);
149         }
150         poll_block();
151     }
152
153     return 0;
154 }
155
156 static void
157 new_management_connection(const char *nl_name, struct vconn *new_remote)
158 {
159     char *nl_name_without_subscription;
160     struct vconn *new_local;
161     struct rconn *r1, *r2;
162     int retval;
163
164     /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.  We
165      * only accept the former syntax in main().
166      *
167      * nl:123:0 opens a netlink connection to local datapath 123 without
168      * obtaining a subscription for ofp_packet_in or ofp_flow_expired
169      * messages.*/
170     nl_name_without_subscription = xasprintf("%s:0", nl_name);
171     retval = vconn_open(nl_name_without_subscription, &new_local);
172     if (retval) {
173         VLOG_ERR("could not connect to %s (%s)",
174                  nl_name_without_subscription, strerror(retval));
175         vconn_close(new_remote);
176         return;
177     }
178     free(nl_name_without_subscription);
179
180     /* Add it to the relay list. */
181     r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
182     r2 = rconn_new_from_vconn("passive", 1, new_remote);
183     relay_create(r1, r2);
184 }
185
186 static void
187 relay_create(struct rconn *a, struct rconn *b)
188 {
189     struct relay *r;
190     int i;
191
192     r = xmalloc(sizeof *r);
193     for (i = 0; i < 2; i++) {
194         r->halves[i].rconn = i ? b : a;
195         r->halves[i].rxbuf = NULL;
196     }
197     list_push_back(&relays, &r->node);
198 }
199
200 static void
201 relay_run(struct relay *r)
202 {
203     int iteration;
204     int i;
205
206     for (i = 0; i < 2; i++) {
207         rconn_run(r->halves[i].rconn);
208     }
209
210     /* Limit the number of iterations to prevent other tasks from starving. */
211     for (iteration = 0; iteration < 50; iteration++) {
212         bool progress = false;
213         for (i = 0; i < 2; i++) {
214             struct half *this = &r->halves[i];
215             struct half *peer = &r->halves[!i];
216
217             if (!this->rxbuf) {
218                 this->rxbuf = rconn_recv(this->rconn);
219             }
220
221             if (this->rxbuf) {
222                 int retval = rconn_send(peer->rconn, this->rxbuf);
223                 if (retval != EAGAIN) {
224                     this->rxbuf = NULL;
225                     if (!retval) {
226                         progress = true;
227                     }
228                 }
229             }
230         }
231         if (!progress) {
232             break;
233         }
234     }
235
236     for (i = 0; i < 2; i++) {
237         struct half *this = &r->halves[i];
238         if (!rconn_is_alive(this->rconn)) {
239             relay_destroy(r);
240             return;
241         }
242     }
243 }
244
245 static void
246 relay_wait(struct relay *r)
247 {
248     int i;
249
250     for (i = 0; i < 2; i++) {
251         struct half *this = &r->halves[i];
252
253         rconn_run_wait(this->rconn);
254         if (!this->rxbuf) {
255             rconn_recv_wait(this->rconn);
256         }
257     }
258 }
259
260 static void
261 relay_destroy(struct relay *r)
262 {
263     int i;
264
265     list_remove(&r->node);
266     for (i = 0; i < 2; i++) {
267         struct half *this = &r->halves[i];
268         rconn_destroy(this->rconn);
269         buffer_delete(this->rxbuf);
270     }
271     free(r);
272 }
273
274 static void
275 parse_options(int argc, char *argv[]) 
276 {
277     static struct option long_options[] = {
278         {"listen",      required_argument, 0, 'l'},
279         {"verbose",     optional_argument, 0, 'v'},
280         {"help",        no_argument, 0, 'h'},
281         {"version",     no_argument, 0, 'V'},
282         VCONN_SSL_LONG_OPTIONS
283         {0, 0, 0, 0},
284     };
285     char *short_options = long_options_to_short_options(long_options);
286     
287     for (;;) {
288         int c;
289
290         c = getopt_long(argc, argv, short_options, long_options, NULL);
291         if (c == -1) {
292             break;
293         }
294
295         switch (c) {
296         case 'l':
297             if (listen_vconn_name) {
298                 fatal(0, "-l or --listen may be only specified once");
299             }
300             listen_vconn_name = optarg;
301             break;
302
303         case 'h':
304             usage();
305
306         case 'V':
307             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
308             exit(EXIT_SUCCESS);
309
310         case 'v':
311             vlog_set_verbosity(optarg);
312             break;
313
314         VCONN_SSL_OPTION_HANDLERS
315
316         case '?':
317             exit(EXIT_FAILURE);
318
319         default:
320             abort();
321         }
322     }
323     free(short_options);
324 }
325
326 static void
327 usage(void)
328 {
329     printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
330            "usage: %s [OPTIONS] LOCAL REMOTE\n"
331            "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
332            program_name, program_name);
333     vconn_usage(true, true);
334     printf("\nNetworking options:\n"
335            "  -l, --listen=METHOD     allow management connections on METHOD\n"
336            "                          (a passive OpenFlow connection method)\n"
337            "\nOther options:\n"
338            "  -v, --verbose           set maximum verbosity level\n"
339            "  -h, --help              display this help message\n"
340            "  -V, --version           display version information\n");
341     exit(EXIT_SUCCESS);
342 }