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.
43 #include "command-line.h"
49 #include "vconn-ssl.h"
50 #include "vlog-socket.h"
52 #include "poll-loop.h"
56 #define THIS_MODULE VLM_secchan
58 static void parse_options(int argc, char *argv[]);
59 static void usage(void) NO_RETURN;
61 static const char *listen_vconn_name;
70 struct half halves[2];
73 static struct list relays = LIST_INITIALIZER(&relays);
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 *);
82 main(int argc, char *argv[])
84 struct vconn *listen_vconn;
88 set_program_name(argv[0]);
89 register_fault_handlers();
91 parse_options(argc, argv);
93 if (argc - optind != 2) {
95 "need exactly two non-option arguments; use --help for usage");
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);
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);
109 if (!vconn_is_passive(listen_vconn)) {
110 fatal(0, "%s is not a passive vconn", listen_vconn_name);
116 retval = vlog_server_listen(NULL, NULL);
118 fatal(retval, "Could not listen for vlog connections");
121 relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1));
126 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
131 struct vconn *new_remote;
132 retval = vconn_accept(listen_vconn, &new_remote);
134 if (retval != EAGAIN) {
135 VLOG_WARN("accept failed (%s)", strerror(retval));
139 new_management_connection(nl_name, new_remote);
143 /* Wait for something to happen. */
144 LIST_FOR_EACH (r, struct relay, node, &relays) {
148 vconn_accept_wait(listen_vconn);
157 new_management_connection(const char *nl_name, struct vconn *new_remote)
159 char *nl_name_without_subscription;
160 struct vconn *new_local;
161 struct rconn *r1, *r2;
164 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
165 * only accept the former syntax in main().
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
170 nl_name_without_subscription = xasprintf("%s:0", nl_name);
171 retval = vconn_open(nl_name_without_subscription, &new_local);
173 VLOG_ERR("could not connect to %s (%s)",
174 nl_name_without_subscription, strerror(retval));
175 vconn_close(new_remote);
178 free(nl_name_without_subscription);
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);
187 relay_create(struct rconn *a, struct rconn *b)
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;
197 list_push_back(&relays, &r->node);
201 relay_run(struct relay *r)
206 for (i = 0; i < 2; i++) {
207 rconn_run(r->halves[i].rconn);
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];
218 this->rxbuf = rconn_recv(this->rconn);
222 int retval = rconn_send(peer->rconn, this->rxbuf);
223 if (retval != EAGAIN) {
227 buffer_delete(this->rxbuf);
238 for (i = 0; i < 2; i++) {
239 struct half *this = &r->halves[i];
240 if (!rconn_is_alive(this->rconn)) {
248 relay_wait(struct relay *r)
252 for (i = 0; i < 2; i++) {
253 struct half *this = &r->halves[i];
255 rconn_run_wait(this->rconn);
257 rconn_recv_wait(this->rconn);
263 relay_destroy(struct relay *r)
267 list_remove(&r->node);
268 for (i = 0; i < 2; i++) {
269 struct half *this = &r->halves[i];
270 rconn_destroy(this->rconn);
271 buffer_delete(this->rxbuf);
277 parse_options(int argc, char *argv[])
279 static struct option long_options[] = {
280 {"listen", required_argument, 0, 'l'},
281 {"verbose", optional_argument, 0, 'v'},
282 {"help", no_argument, 0, 'h'},
283 {"version", no_argument, 0, 'V'},
284 VCONN_SSL_LONG_OPTIONS
287 char *short_options = long_options_to_short_options(long_options);
292 c = getopt_long(argc, argv, short_options, long_options, NULL);
299 if (listen_vconn_name) {
300 fatal(0, "-l or --listen may be only specified once");
302 listen_vconn_name = optarg;
309 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
313 vlog_set_verbosity(optarg);
316 VCONN_SSL_OPTION_HANDLERS
331 printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
332 "usage: %s [OPTIONS] LOCAL REMOTE\n"
333 "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
334 program_name, program_name);
335 vconn_usage(true, true);
336 printf("\nNetworking options:\n"
337 " -l, --listen=METHOD allow management connections on METHOD\n"
338 " (a passive OpenFlow connection method)\n"
340 " -v, --verbose set maximum verbosity level\n"
341 " -h, --help display this help message\n"
342 " -V, --version display version information\n");