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.
39 #include <netinet/in.h>
44 #include "command-line.h"
47 #include "discovery.h"
48 #include "fail-open.h"
55 #include "port-watcher.h"
56 #include "poll-loop.h"
57 #include "ratelimit.h"
62 #include "stp-secchan.h"
66 #include "vconn-ssl.h"
68 #include "vlog-socket.h"
71 #define THIS_MODULE VLM_secchan
73 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
75 static void parse_options(int argc, char *argv[], struct settings *);
76 static void usage(void) NO_RETURN;
78 static struct pvconn *open_passive_vconn(const char *name);
79 static struct vconn *accept_vconn(struct pvconn *pvconn);
81 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
83 static struct relay *relay_accept(const struct settings *, struct pvconn *);
84 static void relay_run(struct relay *, const struct hook[], size_t n_hooks);
85 static void relay_wait(struct relay *);
86 static void relay_destroy(struct relay *);
89 main(int argc, char *argv[])
93 struct list relays = LIST_INITIALIZER(&relays);
98 struct pvconn *monitor;
100 struct pvconn *listeners[MAX_MGMT];
103 struct rconn *local_rconn, *remote_rconn;
104 struct relay *controller_relay;
105 struct discovery *discovery;
106 struct switch_status *switch_status;
107 struct port_watcher *pw;
111 set_program_name(argv[0]);
112 register_fault_handlers();
115 parse_options(argc, argv, &s);
116 signal(SIGPIPE, SIG_IGN);
118 /* Start listening for management and monitoring connections. */
120 for (i = 0; i < s.n_listeners; i++) {
121 listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
123 monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
125 /* Initialize switch status hook. */
126 hooks[n_hooks++] = switch_status_hook_create(&s, &switch_status);
128 /* Start listening for vlogconf requests. */
129 retval = vlog_server_listen(NULL, NULL);
131 ofp_fatal(retval, "Could not listen for vlog connections");
134 die_if_already_running();
137 VLOG_WARN("OpenFlow reference implementation version %s", VERSION);
138 VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
140 /* Connect to datapath. */
141 local_rconn = rconn_create(0, s.max_backoff);
142 rconn_connect(local_rconn, s.dp_name);
143 switch_status_register_category(switch_status, "local",
144 rconn_status_cb, local_rconn);
146 /* Connect to controller. */
147 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
148 if (s.controller_name) {
149 retval = rconn_connect(remote_rconn, s.controller_name);
150 if (retval == EAFNOSUPPORT) {
151 ofp_fatal(0, "No support for %s vconn", s.controller_name);
154 switch_status_register_category(switch_status, "remote",
155 rconn_status_cb, remote_rconn);
157 /* Start relaying. */
158 controller_relay = relay_create(local_rconn, remote_rconn, false);
159 list_push_back(&relays, &controller_relay->node);
162 hooks[n_hooks++] = port_watcher_create(local_rconn, remote_rconn, &pw);
163 discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
165 hooks[n_hooks++] = snat_hook_create(pw);
168 hooks[n_hooks++] = stp_hook_create(&s, pw, local_rconn, remote_rconn);
171 hooks[n_hooks++] = in_band_hook_create(&s, switch_status, pw,
174 if (s.fail_mode == FAIL_OPEN) {
175 hooks[n_hooks++] = fail_open_hook_create(&s, switch_status,
176 local_rconn, remote_rconn);
179 hooks[n_hooks++] = rate_limit_hook_create(&s, switch_status,
180 local_rconn, remote_rconn);
182 assert(n_hooks <= ARRAY_SIZE(hooks));
189 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
190 relay_run(r, hooks, n_hooks);
192 for (i = 0; i < n_listeners; i++) {
194 struct relay *r = relay_accept(&s, listeners[i]);
198 list_push_back(&relays, &r->node);
202 struct vconn *new = accept_vconn(monitor);
204 rconn_add_monitor(local_rconn, new);
207 for (i = 0; i < n_hooks; i++) {
208 if (hooks[i].periodic_cb) {
209 hooks[i].periodic_cb(hooks[i].aux);
213 char *controller_name;
214 if (rconn_is_connectivity_questionable(remote_rconn)) {
215 discovery_question_connectivity(discovery);
217 if (discovery_run(discovery, &controller_name)) {
218 if (controller_name) {
219 rconn_connect(remote_rconn, controller_name);
221 rconn_disconnect(remote_rconn);
226 /* Wait for something to happen. */
227 LIST_FOR_EACH (r, struct relay, node, &relays) {
230 for (i = 0; i < n_listeners; i++) {
231 pvconn_wait(listeners[i]);
234 pvconn_wait(monitor);
236 for (i = 0; i < n_hooks; i++) {
237 if (hooks[i].wait_cb) {
238 hooks[i].wait_cb(hooks[i].aux);
242 discovery_wait(discovery);
250 static struct pvconn *
251 open_passive_vconn(const char *name)
253 struct pvconn *pvconn;
256 retval = pvconn_open(name, &pvconn);
257 if (retval && retval != EAGAIN) {
258 ofp_fatal(retval, "opening %s", name);
263 static struct vconn *
264 accept_vconn(struct pvconn *pvconn)
269 retval = pvconn_accept(pvconn, OFP_VERSION, &new);
270 if (retval && retval != EAGAIN) {
271 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
277 make_hook(bool (*local_packet_cb)(struct relay *, void *aux),
278 bool (*remote_packet_cb)(struct relay *, void *aux),
279 void (*periodic_cb)(void *aux),
280 void (*wait_cb)(void *aux),
284 h.packet_cb[HALF_LOCAL] = local_packet_cb;
285 h.packet_cb[HALF_REMOTE] = remote_packet_cb;
286 h.periodic_cb = periodic_cb;
292 struct ofp_packet_in *
293 get_ofp_packet_in(struct relay *r)
295 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
296 struct ofp_header *oh = msg->data;
297 if (oh->type == OFPT_PACKET_IN) {
298 if (msg->size >= offsetof (struct ofp_packet_in, data)) {
301 VLOG_WARN("packet too short (%zu bytes) for packet_in",
309 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
310 struct eth_header **ethp)
312 const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
313 struct ofp_packet_in *opi = get_ofp_packet_in(r);
314 if (opi && ntohs(opi->header.length) >= min_len) {
316 *ethp = (void *) opi->data;
322 /* OpenFlow message relaying. */
324 static struct relay *
325 relay_accept(const struct settings *s, struct pvconn *pvconn)
327 struct vconn *new_remote, *new_local;
328 struct rconn *r1, *r2;
333 new_remote = accept_vconn(pvconn);
338 if (sscanf(s->dp_name, "nl:%d", &nl_index) == 1) {
339 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
340 * nl:123:0 opens a netlink connection to local datapath 123 without
341 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
342 * messages. That's what we want here; management connections should
343 * not receive those messages, at least by default. */
344 vconn_name = xasprintf("nl:%d:0", nl_index);
346 /* We don't have a way to specify not to subscribe to those messages
347 * for other transports. (That's a defect: really this should be in
348 * the OpenFlow protocol, not the Netlink transport). */
349 VLOG_WARN_RL(&rl, "new management connection will receive "
350 "asynchronous messages");
351 vconn_name = xstrdup(s->dp_name);
354 retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
356 VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
357 vconn_name, strerror(retval));
358 vconn_close(new_remote);
363 /* Create and return relay. */
364 r1 = rconn_create(0, 0);
365 rconn_connect_unreliably(r1, vconn_name, new_local);
368 r2 = rconn_create(0, 0);
369 rconn_connect_unreliably(r2, "passive", new_remote);
371 return relay_create(r1, r2, true);
374 static struct relay *
375 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
377 struct relay *r = xcalloc(1, sizeof *r);
378 r->halves[HALF_LOCAL].rconn = local;
379 r->halves[HALF_REMOTE].rconn = remote;
380 r->is_mgmt_conn = is_mgmt_conn;
385 relay_run(struct relay *r, const struct hook hooks[], size_t n_hooks)
390 for (i = 0; i < 2; i++) {
391 rconn_run(r->halves[i].rconn);
394 /* Limit the number of iterations to prevent other tasks from starving. */
395 for (iteration = 0; iteration < 50; iteration++) {
396 bool progress = false;
397 for (i = 0; i < 2; i++) {
398 struct half *this = &r->halves[i];
399 struct half *peer = &r->halves[!i];
402 this->rxbuf = rconn_recv(this->rconn);
403 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
404 const struct hook *h;
405 for (h = hooks; h < &hooks[n_hooks]; h++) {
406 if (h->packet_cb[i] && h->packet_cb[i](r, h->aux)) {
407 ofpbuf_delete(this->rxbuf);
416 if (this->rxbuf && !this->n_txq) {
417 int retval = rconn_send(peer->rconn, this->rxbuf,
419 if (retval != EAGAIN) {
423 ofpbuf_delete(this->rxbuf);
434 if (r->is_mgmt_conn) {
435 for (i = 0; i < 2; i++) {
436 struct half *this = &r->halves[i];
437 if (!rconn_is_alive(this->rconn)) {
446 relay_wait(struct relay *r)
450 for (i = 0; i < 2; i++) {
451 struct half *this = &r->halves[i];
453 rconn_run_wait(this->rconn);
455 rconn_recv_wait(this->rconn);
461 relay_destroy(struct relay *r)
465 list_remove(&r->node);
466 for (i = 0; i < 2; i++) {
467 struct half *this = &r->halves[i];
468 rconn_destroy(this->rconn);
469 ofpbuf_delete(this->rxbuf);
474 /* User interface. */
477 parse_options(int argc, char *argv[], struct settings *s)
480 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
482 OPT_INACTIVITY_PROBE,
487 OPT_BOOTSTRAP_CA_CERT,
494 static struct option long_options[] = {
495 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
496 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
497 {"fail", required_argument, 0, 'F'},
498 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
499 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
500 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
501 {"listen", required_argument, 0, 'l'},
502 {"monitor", required_argument, 0, 'm'},
503 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
504 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
505 {"stp", no_argument, 0, OPT_STP},
506 {"no-stp", no_argument, 0, OPT_NO_STP},
507 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
508 {"in-band", no_argument, 0, OPT_IN_BAND},
509 {"verbose", optional_argument, 0, 'v'},
510 {"help", no_argument, 0, 'h'},
511 {"version", no_argument, 0, 'V'},
515 VCONN_SSL_LONG_OPTIONS
516 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
520 char *short_options = long_options_to_short_options(long_options);
521 char *accept_re = NULL;
524 /* Set defaults that we can figure out before parsing options. */
526 s->monitor_name = NULL;
527 s->fail_mode = FAIL_OPEN;
529 s->probe_interval = 15;
531 s->update_resolv_conf = true;
534 s->enable_stp = false;
539 c = getopt_long(argc, argv, short_options, long_options, NULL);
545 case OPT_ACCEPT_VCONN:
546 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
549 case OPT_NO_RESOLV_CONF:
550 s->update_resolv_conf = false;
554 if (!strcmp(optarg, "open")) {
555 s->fail_mode = FAIL_OPEN;
556 } else if (!strcmp(optarg, "closed")) {
557 s->fail_mode = FAIL_CLOSED;
559 ofp_fatal(0, "-f or --fail argument must be \"open\" "
564 case OPT_INACTIVITY_PROBE:
565 s->probe_interval = atoi(optarg);
566 if (s->probe_interval < 5) {
567 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
572 if (!strcmp(optarg, "permanent")) {
573 s->max_idle = OFP_FLOW_PERMANENT;
575 s->max_idle = atoi(optarg);
576 if (s->max_idle < 1 || s->max_idle > 65535) {
577 ofp_fatal(0, "--max-idle argument must be between 1 and "
578 "65535 or the word 'permanent'");
583 case OPT_MAX_BACKOFF:
584 s->max_backoff = atoi(optarg);
585 if (s->max_backoff < 1) {
586 ofp_fatal(0, "--max-backoff argument must be at least 1");
587 } else if (s->max_backoff > 3600) {
588 s->max_backoff = 3600;
594 s->rate_limit = atoi(optarg);
595 if (s->rate_limit < 1) {
596 ofp_fatal(0, "--rate-limit argument must be at least 1");
599 s->rate_limit = 1000;
603 case OPT_BURST_LIMIT:
604 s->burst_limit = atoi(optarg);
605 if (s->burst_limit < 1) {
606 ofp_fatal(0, "--burst-limit argument must be at least 1");
611 s->enable_stp = true;
615 s->enable_stp = false;
618 case OPT_OUT_OF_BAND:
627 if (s->n_listeners >= MAX_MGMT) {
629 "-l or --listen may be specified at most %d times",
632 s->listener_names[s->n_listeners++] = optarg;
636 if (s->monitor_name) {
637 ofp_fatal(0, "-m or --monitor may only be specified once");
639 s->monitor_name = optarg;
646 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
649 DAEMON_OPTION_HANDLERS
654 VCONN_SSL_OPTION_HANDLERS
656 case OPT_BOOTSTRAP_CA_CERT:
657 vconn_ssl_set_ca_cert_file(optarg, true);
672 if (argc < 1 || argc > 2) {
673 ofp_fatal(0, "need one or two non-option arguments; "
674 "use --help for usage");
677 /* Local and remote vconns. */
678 s->dp_name = argv[0];
679 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
681 /* Set accept_controller_regex. */
683 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
685 retval = regcomp(&s->accept_controller_regex, accept_re,
686 REG_NOSUB | REG_EXTENDED);
688 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
689 char *buffer = xmalloc(length);
690 regerror(retval, &s->accept_controller_regex, buffer, length);
691 ofp_fatal(0, "%s: %s", accept_re, buffer);
693 s->accept_controller_re = accept_re;
695 /* Mode of operation. */
696 s->discovery = s->controller_name == NULL;
697 if (s->discovery && !s->in_band) {
698 ofp_fatal(0, "Cannot perform discovery with out-of-band control");
703 if (s->rate_limit < 100) {
704 VLOG_WARN("Rate limit set to unusually low value %d",
707 if (!s->burst_limit) {
708 s->burst_limit = s->rate_limit / 4;
710 s->burst_limit = MAX(s->burst_limit, 1);
711 s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
718 printf("%s: secure channel, a relay for OpenFlow messages.\n"
719 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
720 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
721 "CONTROLLER is an active OpenFlow connection method; if it is\n"
722 "omitted, then secchan performs controller discovery.\n",
723 program_name, program_name);
724 vconn_usage(true, true, true);
725 printf("\nController discovery options:\n"
726 " --accept-vconn=REGEX accept matching discovered controllers\n"
727 " --no-resolv-conf do not update /etc/resolv.conf\n"
728 "\nNetworking options:\n"
729 " -F, --fail=open|closed when controller connection fails:\n"
730 " closed: drop all packets\n"
731 " open (default): act as learning switch\n"
732 " --inactivity-probe=SECS time between inactivity probes\n"
733 " --max-idle=SECS max idle for flows set up by secchan\n"
734 " --max-backoff=SECS max time between controller connection\n"
735 " attempts (default: 15 seconds)\n"
736 " -l, --listen=METHOD allow management connections on METHOD\n"
737 " (a passive OpenFlow connection method)\n"
738 " -m, --monitor=METHOD copy traffic to/from kernel to METHOD\n"
739 " (a passive OpenFlow connection method)\n"
740 " --out-of-band controller connection is out-of-band\n"
741 " --stp enable 802.1D Spanning Tree Protocol\n"
742 " --no-stp disable 802.1D Spanning Tree Protocol\n"
743 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
744 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
745 " --burst-limit=BURST limit on packet credit for idle time\n");
748 printf("\nOther options:\n"
749 " -h, --help display this help message\n"
750 " -V, --version display version information\n");