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>
48 #include "command-line.h"
52 #include "dhcp-client.h"
53 #include "dynamic-string.h"
56 #include "learning-switch.h"
58 #include "mac-learning.h"
62 #include "poll-loop.h"
66 #include "vconn-ssl.h"
68 #include "vlog-socket.h"
71 #define THIS_MODULE VLM_secchan
73 /* Behavior when the connection to the controller fails. */
75 FAIL_OPEN, /* Act as learning switch. */
76 FAIL_CLOSED /* Drop all packets. */
79 /* Settings that may be configured by the user. */
81 /* Overall mode of operation. */
82 bool discovery; /* Discover the controller automatically? */
83 bool in_band; /* Connect to controller in-band? */
85 /* Related vconns and network devices. */
86 const char *nl_name; /* Local datapath (must be "nl:" vconn). */
87 char *of_name; /* ofX network device name. */
88 const char *controller_name; /* Controller (if not discovery mode). */
89 const char *listen_vconn_name; /* Listens for mgmt connections. */
91 /* Failure behavior. */
92 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
93 int max_idle; /* Idle time for flows in fail-open mode. */
94 int probe_interval; /* # seconds idle before sending echo request. */
95 int max_backoff; /* Max # seconds between connection attempts. */
97 /* Discovery behavior. */
98 regex_t accept_controller_regex; /* Controller vconns to accept. */
99 const char *accept_controller_re; /* String version of regex. */
100 bool update_resolv_conf; /* Update /etc/resolv.conf? */
105 struct buffer *rxbuf;
106 int n_txq; /* No. of packets queued for tx on 'rconn'. */
113 #define HALF_REMOTE 1
114 struct half halves[2];
120 bool (*packet_cb)(struct relay *, int half, void *aux);
121 void (*periodic_cb)(void *aux);
125 static void parse_options(int argc, char *argv[], struct settings *);
126 static void usage(void) NO_RETURN;
128 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
130 static struct relay *relay_accept(const struct settings *, struct vconn *);
131 static void relay_run(struct relay *, const struct hook[], size_t n_hooks);
132 static void relay_wait(struct relay *);
133 static void relay_destroy(struct relay *);
135 static struct hook make_hook(bool (*packet_cb)(struct relay *, int, void *),
136 void (*periodic_cb)(void *),
139 static struct discovery *discovery_init(const struct settings *);
140 static void discovery_question_connectivity(struct discovery *);
141 static bool discovery_run(struct discovery *, char **controller_name);
142 static void discovery_wait(struct discovery *);
144 static struct hook in_band_hook_create(const struct settings *);
145 static struct hook fail_open_hook_create(const struct settings *,
147 struct rconn *remote);
149 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
150 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
153 main(int argc, char *argv[])
157 struct list relays = LIST_INITIALIZER(&relays);
159 struct hook hooks[3];
162 struct rconn *local_rconn, *remote_rconn;
163 struct vconn *listen_vconn;
164 struct relay *controller_relay;
165 struct discovery *discovery;
168 set_program_name(argv[0]);
169 register_fault_handlers();
172 parse_options(argc, argv, &s);
174 /* Start listening for management connections. */
175 if (s.listen_vconn_name) {
176 retval = vconn_open(s.listen_vconn_name, &listen_vconn);
177 if (retval && retval != EAGAIN) {
178 fatal(retval, "opening %s", s.listen_vconn_name);
180 if (!vconn_is_passive(listen_vconn)) {
181 fatal(0, "%s is not a passive vconn", s.listen_vconn_name);
187 /* Start controller discovery. */
188 discovery = s.discovery ? discovery_init(&s) : NULL;
190 /* Start listening for vlogconf requests. */
191 retval = vlog_server_listen(NULL, NULL);
193 fatal(retval, "Could not listen for vlog connections");
198 /* Connect to datapath. */
199 local_rconn = rconn_create(0, s.max_backoff);
200 rconn_connect(local_rconn, s.nl_name);
202 /* Connect to controller. */
203 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
204 if (s.controller_name) {
205 retval = rconn_connect(remote_rconn, s.controller_name);
206 if (retval == EAFNOSUPPORT) {
207 fatal(0, "No support for %s vconn", s.controller_name);
211 /* Start relaying. */
212 controller_relay = relay_create(local_rconn, remote_rconn, false);
213 list_push_back(&relays, &controller_relay->node);
218 hooks[n_hooks++] = in_band_hook_create(&s);
220 if (s.fail_mode == FAIL_OPEN) {
221 hooks[n_hooks++] = fail_open_hook_create(&s,
222 local_rconn, remote_rconn);
224 assert(n_hooks <= ARRAY_SIZE(hooks));
231 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
232 relay_run(r, hooks, n_hooks);
236 struct relay *r = relay_accept(&s, listen_vconn);
240 list_push_back(&relays, &r->node);
243 for (i = 0; i < n_hooks; i++) {
244 if (hooks[i].periodic_cb) {
245 hooks[i].periodic_cb(hooks[i].aux);
249 char *controller_name;
250 if (rconn_is_connectivity_questionable(remote_rconn)) {
251 discovery_question_connectivity(discovery);
253 if (discovery_run(discovery, &controller_name)) {
254 if (controller_name) {
255 rconn_connect(remote_rconn, controller_name);
257 rconn_disconnect(remote_rconn);
262 /* Wait for something to happen. */
263 LIST_FOR_EACH (r, struct relay, node, &relays) {
267 vconn_accept_wait(listen_vconn);
270 discovery_wait(discovery);
279 make_hook(bool (*packet_cb)(struct relay *, int half, void *aux),
280 void (*periodic_cb)(void *aux),
284 h.packet_cb = packet_cb;
285 h.periodic_cb = periodic_cb;
290 /* OpenFlow message relaying. */
292 static struct relay *
293 relay_accept(const struct settings *s, struct vconn *listen_vconn)
295 struct vconn *new_remote, *new_local;
296 char *nl_name_without_subscription;
297 struct rconn *r1, *r2;
300 retval = vconn_accept(listen_vconn, &new_remote);
302 if (retval != EAGAIN) {
303 VLOG_WARN("accept failed (%s)", strerror(retval));
308 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
309 * only accept the former syntax in main().
311 * nl:123:0 opens a netlink connection to local datapath 123 without
312 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
314 nl_name_without_subscription = xasprintf("%s:0", s->nl_name);
315 retval = vconn_open(nl_name_without_subscription, &new_local);
317 VLOG_ERR("could not connect to %s (%s)",
318 nl_name_without_subscription, strerror(retval));
319 vconn_close(new_remote);
320 free(nl_name_without_subscription);
324 /* Create and return relay. */
325 r1 = rconn_create(0, 0);
326 rconn_connect_unreliably(r1, nl_name_without_subscription, new_local);
327 free(nl_name_without_subscription);
329 r2 = rconn_create(0, 0);
330 rconn_connect_unreliably(r2, "passive", new_remote);
332 return relay_create(r1, r2, true);
335 static struct relay *
336 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
338 struct relay *r = xcalloc(1, sizeof *r);
339 r->halves[HALF_LOCAL].rconn = local;
340 r->halves[HALF_REMOTE].rconn = remote;
341 r->is_mgmt_conn = is_mgmt_conn;
346 relay_run(struct relay *r, const struct hook hooks[], size_t n_hooks)
351 for (i = 0; i < 2; i++) {
352 rconn_run(r->halves[i].rconn);
355 /* Limit the number of iterations to prevent other tasks from starving. */
356 for (iteration = 0; iteration < 50; iteration++) {
357 bool progress = false;
358 for (i = 0; i < 2; i++) {
359 struct half *this = &r->halves[i];
360 struct half *peer = &r->halves[!i];
363 this->rxbuf = rconn_recv(this->rconn);
365 const struct hook *h;
366 for (h = hooks; h < &hooks[n_hooks]; h++) {
367 if (h->packet_cb(r, i, h->aux)) {
368 buffer_delete(this->rxbuf);
377 if (this->rxbuf && !this->n_txq) {
378 int retval = rconn_send(peer->rconn, this->rxbuf,
380 if (retval != EAGAIN) {
384 buffer_delete(this->rxbuf);
395 if (r->is_mgmt_conn) {
396 for (i = 0; i < 2; i++) {
397 struct half *this = &r->halves[i];
398 if (!rconn_is_alive(this->rconn)) {
407 relay_wait(struct relay *r)
411 for (i = 0; i < 2; i++) {
412 struct half *this = &r->halves[i];
414 rconn_run_wait(this->rconn);
416 rconn_recv_wait(this->rconn);
422 relay_destroy(struct relay *r)
426 list_remove(&r->node);
427 for (i = 0; i < 2; i++) {
428 struct half *this = &r->halves[i];
429 rconn_destroy(this->rconn);
430 buffer_delete(this->rxbuf);
435 /* In-band control. */
437 struct in_band_data {
438 const struct settings *s;
439 struct mac_learning *ml;
440 struct netdev *of_device;
441 uint8_t mac[ETH_ADDR_LEN];
446 queue_tx(struct rconn *rc, struct in_band_data *in_band, struct buffer *b)
448 rconn_send_with_limit(rc, b, &in_band->n_queued, 10);
451 static const uint8_t *
452 get_controller_mac(struct netdev *netdev, struct rconn *controller)
454 static uint32_t ip, last_nonzero_ip;
455 static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
456 static time_t next_refresh = 0;
458 uint32_t last_ip = ip;
460 time_t now = time_now();
462 ip = rconn_get_ip(controller);
463 if (last_ip != ip || !next_refresh || now >= next_refresh) {
466 /* Look up MAC address. */
467 memset(mac, 0, sizeof mac);
469 int retval = netdev_arp_lookup(netdev, ip, mac);
471 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
472 IP_ARGS(&ip), strerror(retval));
475 have_mac = !eth_addr_is_zero(mac);
477 /* Log changes in IP, MAC addresses. */
478 if (ip && ip != last_nonzero_ip) {
479 VLOG_DBG("controller IP address changed from "IP_FMT
480 " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
481 last_nonzero_ip = ip;
483 if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
484 VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
486 ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
487 memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
490 /* Schedule next refresh.
492 * If we have an IP address but not a MAC address, then refresh
493 * quickly, since we probably will get a MAC address soon (via ARP).
494 * Otherwise, we can afford to wait a little while. */
495 next_refresh = now + (!ip || have_mac ? 10 : 1);
497 return !eth_addr_is_zero(mac) ? mac : NULL;
501 is_controller_mac(const uint8_t mac[ETH_ADDR_LEN],
502 const uint8_t *controller_mac)
504 return controller_mac && eth_addr_equals(mac, controller_mac);
508 in_band_packet_cb(struct relay *r, int half, void *in_band_)
510 struct in_band_data *in_band = in_band_;
511 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
512 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
513 struct ofp_packet_in *opi;
514 struct ofp_header *oh;
515 size_t pkt_ofs, pkt_len;
518 uint16_t in_port, out_port;
519 const uint8_t *controller_mac;
521 if (half != HALF_LOCAL || r->is_mgmt_conn) {
526 if (oh->type != OFPT_PACKET_IN) {
529 if (msg->size < offsetof (struct ofp_packet_in, data)) {
530 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
534 /* Extract flow data from 'opi' into 'flow'. */
536 in_port = ntohs(opi->in_port);
537 pkt_ofs = offsetof(struct ofp_packet_in, data);
538 pkt_len = ntohs(opi->header.length) - pkt_ofs;
539 pkt.data = opi->data;
541 flow_extract(&pkt, in_port, &flow);
543 /* Deal with local stuff. */
544 controller_mac = get_controller_mac(in_band->of_device,
545 r->halves[HALF_REMOTE].rconn);
546 if (in_port == OFPP_LOCAL) {
547 /* Sent by secure channel. */
548 out_port = mac_learning_lookup(in_band->ml, flow.dl_dst);
549 } else if (eth_addr_equals(flow.dl_dst, in_band->mac)) {
550 /* Sent to secure channel. */
551 out_port = OFPP_LOCAL;
552 if (mac_learning_learn(in_band->ml, flow.dl_src, in_port)) {
553 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
554 ETH_ADDR_ARGS(flow.dl_src), in_port);
556 } else if (flow.dl_type == htons(ETH_TYPE_ARP)
557 && eth_addr_is_broadcast(flow.dl_dst)
558 && is_controller_mac(flow.dl_src, controller_mac)) {
559 /* ARP sent by controller. */
560 out_port = OFPP_FLOOD;
561 } else if (is_controller_mac(flow.dl_dst, controller_mac)
562 && in_port == mac_learning_lookup(in_band->ml,
564 /* Drop controller traffic that arrives on the controller port. */
565 queue_tx(rc, in_band, make_add_flow(&flow, ntohl(opi->buffer_id),
566 in_band->s->max_idle, 0));
572 if (out_port != OFPP_FLOOD) {
573 /* The output port is known, so add a new flow. */
574 queue_tx(rc, in_band,
575 make_add_simple_flow(&flow, ntohl(opi->buffer_id),
576 out_port, in_band->s->max_idle));
578 /* If the switch didn't buffer the packet, we need to send a copy. */
579 if (ntohl(opi->buffer_id) == UINT32_MAX) {
580 queue_tx(rc, in_band,
581 make_unbuffered_packet_out(&pkt, in_port, out_port));
584 /* We don't know that MAC. Send along the packet without setting up a
587 if (ntohl(opi->buffer_id) == UINT32_MAX) {
588 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
590 b = make_buffered_packet_out(ntohl(opi->buffer_id),
593 queue_tx(rc, in_band, b);
599 in_band_hook_create(const struct settings *s)
601 struct in_band_data *in_band;
604 in_band = xcalloc(1, sizeof *in_band);
606 in_band->ml = mac_learning_create();
607 retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE,
608 &in_band->of_device);
610 fatal(retval, "Could not open %s device", s->of_name);
612 memcpy(in_band->mac, netdev_get_etheraddr(in_band->of_device),
615 return make_hook(in_band_packet_cb, NULL, in_band);
618 /* Fail open support. */
620 struct fail_open_data {
621 const struct settings *s;
622 struct rconn *local_rconn;
623 struct rconn *remote_rconn;
624 struct lswitch *lswitch;
625 int last_disconn_secs;
628 /* Causes 'r' to enter or leave fail-open mode, if appropriate. */
630 fail_open_periodic_cb(void *fail_open_)
632 struct fail_open_data *fail_open = fail_open_;
636 disconn_secs = rconn_disconnected_duration(fail_open->remote_rconn);
637 open = disconn_secs >= fail_open->s->probe_interval * 3;
638 if (open != (fail_open->lswitch != NULL)) {
640 VLOG_WARN("No longer in fail-open mode");
641 lswitch_destroy(fail_open->lswitch);
642 fail_open->lswitch = NULL;
644 VLOG_WARN("Could not connect to controller for %d seconds, "
645 "failing open", disconn_secs);
646 fail_open->lswitch = lswitch_create(fail_open->local_rconn, true,
647 fail_open->s->max_idle);
648 fail_open->last_disconn_secs = disconn_secs;
650 } else if (open && disconn_secs > fail_open->last_disconn_secs + 60) {
651 VLOG_WARN("Still in fail-open mode after %d seconds disconnected "
652 "from controller", disconn_secs);
653 fail_open->last_disconn_secs = disconn_secs;
658 fail_open_packet_cb(struct relay *r, int half, void *fail_open_)
660 struct fail_open_data *fail_open = fail_open_;
661 if (half != HALF_LOCAL || r->is_mgmt_conn || !fail_open->lswitch) {
664 lswitch_process_packet(fail_open->lswitch, fail_open->local_rconn,
665 r->halves[HALF_LOCAL].rxbuf);
666 rconn_run(fail_open->local_rconn);
672 fail_open_hook_create(const struct settings *s, struct rconn *local_rconn,
673 struct rconn *remote_rconn)
675 struct fail_open_data *fail_open = xmalloc(sizeof *fail_open);
677 fail_open->local_rconn = local_rconn;
678 fail_open->remote_rconn = remote_rconn;
679 fail_open->lswitch = NULL;
680 return make_hook(fail_open_packet_cb, fail_open_periodic_cb, fail_open);
683 /* Controller discovery. */
687 const struct settings *s;
688 struct dhclient *dhcp;
689 bool ever_successful;
692 static struct discovery *
693 discovery_init(const struct settings *s)
695 struct netdev *netdev;
697 struct dhclient *dhcp;
700 /* Bring ofX network device up. */
701 retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE, &netdev);
703 fatal(retval, "Could not open %s device", s->of_name);
705 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
707 fatal(retval, "Could not bring %s device up", s->of_name);
709 netdev_close(netdev);
711 /* Initialize DHCP client. */
712 retval = dhclient_create(s->of_name, modify_dhcp_request,
713 validate_dhcp_offer, (void *) s, &dhcp);
715 fatal(retval, "Failed to initialize DHCP client");
717 dhclient_init(dhcp, 0);
719 d = xmalloc(sizeof *d);
722 d->ever_successful = false;
727 discovery_question_connectivity(struct discovery *d)
729 dhclient_force_renew(d->dhcp, 15);
733 discovery_run(struct discovery *d, char **controller_name)
735 dhclient_run(d->dhcp);
736 if (!dhclient_changed(d->dhcp)) {
740 dhclient_configure_netdev(d->dhcp);
741 if (d->s->update_resolv_conf) {
742 dhclient_update_resolv_conf(d->dhcp);
745 if (dhclient_is_bound(d->dhcp)) {
746 *controller_name = dhcp_msg_get_string(dhclient_get_config(d->dhcp),
747 DHCP_CODE_OFP_CONTROLLER_VCONN);
748 VLOG_WARN("%s: discovered controller", *controller_name);
749 d->ever_successful = true;
750 } else if (controller_name) {
751 *controller_name = NULL;
752 if (d->ever_successful) {
753 VLOG_WARN("discovered controller no longer available");
760 discovery_wait(struct discovery *d)
762 dhclient_wait(d->dhcp);
766 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
768 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
772 validate_dhcp_offer(const struct dhcp_msg *msg, void *s_)
774 const struct settings *s = s_;
778 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
780 VLOG_WARN("rejecting DHCP offer missing controller vconn");
783 accept = !regexec(&s->accept_controller_regex, vconn_name, 0, NULL, 0);
785 VLOG_WARN("rejecting controller vconn that fails to match %s",
786 s->accept_controller_re);
792 /* User interface. */
795 parse_options(int argc, char *argv[], struct settings *s)
798 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
800 OPT_INACTIVITY_PROBE,
804 static struct option long_options[] = {
805 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
806 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
807 {"fail", required_argument, 0, 'f'},
808 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
809 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
810 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
811 {"listen", required_argument, 0, 'l'},
812 {"detach", no_argument, 0, 'D'},
813 {"pidfile", optional_argument, 0, 'P'},
814 {"verbose", optional_argument, 0, 'v'},
815 {"help", no_argument, 0, 'h'},
816 {"version", no_argument, 0, 'V'},
817 VCONN_SSL_LONG_OPTIONS
820 char *short_options = long_options_to_short_options(long_options);
821 char *accept_re = NULL;
824 /* Set defaults that we can figure out before parsing options. */
825 s->listen_vconn_name = NULL;
826 s->fail_mode = FAIL_OPEN;
828 s->probe_interval = 15;
830 s->update_resolv_conf = true;
834 c = getopt_long(argc, argv, short_options, long_options, NULL);
840 case OPT_ACCEPT_VCONN:
841 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
844 case OPT_NO_RESOLV_CONF:
845 s->update_resolv_conf = false;
849 if (!strcmp(optarg, "open")) {
850 s->fail_mode = FAIL_OPEN;
851 } else if (!strcmp(optarg, "closed")) {
852 s->fail_mode = FAIL_CLOSED;
855 "-f or --fail argument must be \"open\" or \"closed\"");
859 case OPT_INACTIVITY_PROBE:
860 s->probe_interval = atoi(optarg);
861 if (s->probe_interval < 5) {
862 fatal(0, "--inactivity-probe argument must be at least 5");
867 if (!strcmp(optarg, "permanent")) {
868 s->max_idle = OFP_FLOW_PERMANENT;
870 s->max_idle = atoi(optarg);
871 if (s->max_idle < 1 || s->max_idle > 65535) {
872 fatal(0, "--max-idle argument must be between 1 and "
873 "65535 or the word 'permanent'");
878 case OPT_MAX_BACKOFF:
879 s->max_backoff = atoi(optarg);
880 if (s->max_backoff < 1) {
881 fatal(0, "--max-backoff argument must be at least 1");
882 } else if (s->max_backoff > 3600) {
883 s->max_backoff = 3600;
896 if (s->listen_vconn_name) {
897 fatal(0, "-l or --listen may be only specified once");
899 s->listen_vconn_name = optarg;
906 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
910 vlog_set_verbosity(optarg);
913 VCONN_SSL_OPTION_HANDLERS
926 if (argc < 1 || argc > 2) {
927 fatal(0, "need one or two non-option arguments; use --help for usage");
930 /* Local and remote vconns. */
931 s->nl_name = argv[0];
932 if (strncmp(s->nl_name, "nl:", 3)
933 || strlen(s->nl_name) < 4
934 || s->nl_name[strspn(s->nl_name + 3, "0123456789") + 3]) {
935 fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", s->nl_name);
937 s->of_name = xasprintf("of%s", s->nl_name + 3);
938 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
940 /* Set accept_controller_regex. */
942 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
944 retval = regcomp(&s->accept_controller_regex, accept_re,
945 REG_NOSUB | REG_EXTENDED);
947 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
948 char *buffer = xmalloc(length);
949 regerror(retval, &s->accept_controller_regex, buffer, length);
950 fatal(0, "%s: %s", accept_re, buffer);
952 s->accept_controller_re = accept_re;
954 /* Mode of operation. */
955 s->discovery = s->controller_name == NULL;
959 enum netdev_flags flags;
960 struct netdev *netdev;
962 retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE, &netdev);
964 fatal(retval, "Could not open %s device", s->of_name);
967 retval = netdev_get_flags(netdev, &flags);
969 fatal(retval, "Could not get flags for %s device", s->of_name);
972 s->in_band = (flags & NETDEV_UP) != 0;
973 if (s->in_band && netdev_get_in6(netdev, NULL)) {
974 VLOG_WARN("Ignoring IPv6 address on %s device: IPv6 not supported",
978 netdev_close(netdev);
985 printf("%s: secure channel, a relay for OpenFlow messages.\n"
986 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
987 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
988 "CONTROLLER is an active OpenFlow connection method; if it is\n"
989 "omitted, then secchan performs controller discovery.\n",
990 program_name, program_name);
991 vconn_usage(true, true);
992 printf("\nController discovery options:\n"
993 " --accept-vconn=REGEX accept matching discovered controllers\n"
994 " --no-resolv-conf do not update /etc/resolv.conf\n"
995 "\nNetworking options:\n"
996 " -f, --fail=open|closed when controller connection fails:\n"
997 " closed: drop all packets\n"
998 " open (default): act as learning switch\n"
999 " --inactivity-probe=SECS time between inactivity probes\n"
1000 " --max-idle=SECS max idle for flows set up by secchan\n"
1001 " --max-backoff=SECS max time between controller connection\n"
1002 " attempts (default: 15 seconds)\n"
1003 " -l, --listen=METHOD allow management connections on METHOD\n"
1004 " (a passive OpenFlow connection method)\n"
1005 "\nOther options:\n"
1006 " -D, --detach run in background as daemon\n"
1007 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
1008 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
1009 " -v, --verbose set maximum verbosity level\n"
1010 " -h, --help display this help message\n"
1011 " -V, --version display version information\n",