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.
38 #include <netinet/in.h>
47 #include "command-line.h"
51 #include "dhcp-client.h"
54 #include "learning-switch.h"
56 #include "mac-learning.h"
60 #include "poll-loop.h"
63 #include "vconn-ssl.h"
65 #include "vlog-socket.h"
68 #define THIS_MODULE VLM_secchan
70 static const char *listen_vconn_name;
77 /* Behavior when the connection to the controller fails. */
79 FAIL_OPEN, /* Act as learning switch. */
80 FAIL_CLOSED /* Drop all packets. */
88 struct half halves[2];
91 struct lswitch *lswitch;
94 static struct list relays = LIST_INITIALIZER(&relays);
96 /* Mode of operation. Note that autodiscovery implies in-band
98 static bool autodiscovery; /* Discover the controller automatically? */
99 static bool in_band; /* Connect to controller in-band? */
101 /* MAC address of local port. */
102 static uint8_t local_mac[ETH_ADDR_LEN];
104 /* MAC learning table for local port. */
105 static struct mac_learning *local_ml;
107 /* Controller vconn name, or null to perform controller autodiscovery. */
108 static char *controller_name = NULL;
110 /* -f, --fail: Behavior when the connection to the controller fails. */
111 static enum fail_mode fail_mode = FAIL_OPEN;
113 /* The OpenFlow virtual network device ofX. */
114 static struct netdev *of_device;
116 /* --inactivity-probe: Number of seconds without receiving a message from the
117 controller before sending an inactivity probe. */
118 static int probe_interval = 15;
120 /* --max-idle: Idle time to assign to flows created by learning switch when in
122 static int max_idle = 15;
124 /* --max-backoff: Maximum interval between controller connection attempts, in
126 static int max_backoff = 15;
128 /* DHCP client, for controller autodiscovery. */
129 static struct dhclient *dhcp;
131 /* --accept-vconn: Regular expression specifying the class of controller vconns
132 * that we will accept during autodiscovery. */
133 static const char *accept_controller_re;
134 static regex_t accept_controller_regex;
136 /* --no-resolv-conf: Update resolv.conf upon successful controller
138 static bool update_resolv_conf = true;
140 static void parse_options(int argc, char *argv[]);
141 static void usage(void) NO_RETURN;
143 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
144 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
146 static void relay_run(struct relay *);
147 static void relay_wait(struct relay *);
148 static void relay_destroy(struct relay *);
150 static bool local_hook(struct relay *r);
151 static bool failing_open(struct relay *r);
152 static bool fail_open_hook(struct relay *r);
154 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
155 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
158 main(int argc, char *argv[])
160 struct rconn *local_rconn, *remote_rconn;
161 struct vconn *listen_vconn;
162 struct relay *controller_relay;
167 set_program_name(argv[0]);
168 register_fault_handlers();
170 parse_options(argc, argv);
174 if (argc < 1 || argc > 2) {
175 fatal(0, "need one or two non-option arguments; use --help for usage");
178 if (strncmp(nl_name, "nl:", 3)
179 || strlen(nl_name) < 4
180 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
181 fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
183 controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
184 autodiscovery = controller_name == NULL;
186 if (!accept_controller_re) {
187 accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
189 retval = regcomp(&accept_controller_regex, accept_controller_re,
190 REG_NOSUB | REG_EXTENDED);
192 size_t length = regerror(retval, &accept_controller_regex, NULL, 0);
193 char *buffer = xmalloc(length);
194 regerror(retval, &accept_controller_regex, buffer, length);
195 fatal(0, "%s: %s", accept_controller_re, buffer);
198 if (listen_vconn_name) {
199 retval = vconn_open(listen_vconn_name, &listen_vconn);
200 if (retval && retval != EAGAIN) {
201 fatal(retval, "opening %s", listen_vconn_name);
203 if (!vconn_is_passive(listen_vconn)) {
204 fatal(0, "%s is not a passive vconn", listen_vconn_name);
210 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
211 retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
213 enum netdev_flags flags;
216 retval = netdev_turn_flags_on(of_device, NETDEV_UP, true);
218 fatal(retval, "Could not bring %s device up", of_name);
222 retval = netdev_get_flags(of_device, &flags);
224 if (flags & NETDEV_UP) {
228 memcpy(local_mac, netdev_get_etheraddr(of_device),
230 if (netdev_get_in6(of_device, &in6)) {
231 VLOG_WARN("Ignoring IPv6 address on %s device: "
232 "IPv6 not supported", of_name);
234 local_ml = mac_learning_create();
237 error(retval, "Could not get flags for %s device", of_name);
240 error(retval, "Could not open %s device", of_name);
242 if (autodiscovery && !in_band) {
243 fatal(retval, "In autodiscovery mode but failed to configure "
248 retval = dhclient_create(of_name, modify_dhcp_request,
249 validate_dhcp_offer, NULL, &dhcp);
251 fatal(retval, "Failed to initialize DHCP client");
253 dhclient_init(dhcp, 0);
256 retval = vlog_server_listen(NULL, NULL);
258 fatal(retval, "Could not listen for vlog connections");
263 local_rconn = rconn_create(1, 0, max_backoff);
264 rconn_connect(local_rconn, nl_name);
266 remote_rconn = rconn_create(1, probe_interval, max_backoff);
267 if (controller_name) {
268 retval = rconn_connect(remote_rconn, controller_name);
269 if (retval == EAFNOSUPPORT) {
270 fatal(0, "No support for %s vconn", controller_name);
273 controller_relay = relay_create(local_rconn, remote_rconn, false);
278 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
283 struct vconn *new_remote;
284 retval = vconn_accept(listen_vconn, &new_remote);
286 if (retval != EAGAIN) {
287 VLOG_WARN("accept failed (%s)", strerror(retval));
291 new_management_connection(nl_name, new_remote);
294 failing_open(controller_relay);
296 if (rconn_is_connectivity_questionable(remote_rconn)) {
297 dhclient_force_renew(dhcp, 15);
300 if (dhclient_changed(dhcp)) {
301 dhclient_configure_netdev(dhcp);
302 if (update_resolv_conf) {
303 dhclient_update_resolv_conf(dhcp);
306 if (dhclient_is_bound(dhcp)) {
307 free(controller_name);
308 controller_name = dhcp_msg_get_string(
309 dhclient_get_config(dhcp),
310 DHCP_CODE_OFP_CONTROLLER_VCONN);
311 VLOG_WARN("%s: discovered controller",
313 rconn_connect(remote_rconn, controller_name);
314 } else if (controller_name) {
315 VLOG_WARN("%s: discover controller no longer available",
317 free(controller_name);
318 controller_name = NULL;
319 rconn_disconnect(remote_rconn);
324 /* Wait for something to happen. */
325 LIST_FOR_EACH (r, struct relay, node, &relays) {
329 vconn_accept_wait(listen_vconn);
341 new_management_connection(const char *nl_name, struct vconn *new_remote)
343 char *nl_name_without_subscription;
344 struct vconn *new_local;
345 struct rconn *r1, *r2;
348 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
349 * only accept the former syntax in main().
351 * nl:123:0 opens a netlink connection to local datapath 123 without
352 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
354 nl_name_without_subscription = xasprintf("%s:0", nl_name);
355 retval = vconn_open(nl_name_without_subscription, &new_local);
357 VLOG_ERR("could not connect to %s (%s)",
358 nl_name_without_subscription, strerror(retval));
359 vconn_close(new_remote);
360 free(nl_name_without_subscription);
364 /* Add it to the relay list. */
365 r1 = rconn_create(1, 0, 0);
366 rconn_connect_unreliably(r1, nl_name_without_subscription, new_local);
367 r2 = rconn_create(1, 0, 0);
368 rconn_connect_unreliably(r2, "passive", new_remote);
369 relay_create(r1, r2, true);
371 free(nl_name_without_subscription);
374 static struct relay *
375 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
380 r = xmalloc(sizeof *r);
381 r->halves[HALF_LOCAL].rconn = local;
382 r->halves[HALF_REMOTE].rconn = remote;
383 for (i = 0; i < 2; i++) {
384 r->halves[i].rxbuf = NULL;
386 r->is_mgmt_conn = is_mgmt_conn;
388 list_push_back(&relays, &r->node);
393 relay_run(struct relay *r)
398 for (i = 0; i < 2; i++) {
399 rconn_run(r->halves[i].rconn);
402 /* Limit the number of iterations to prevent other tasks from starving. */
403 for (iteration = 0; iteration < 50; iteration++) {
404 bool progress = false;
405 for (i = 0; i < 2; i++) {
406 struct half *this = &r->halves[i];
407 struct half *peer = &r->halves[!i];
410 this->rxbuf = rconn_recv(this->rconn);
411 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
412 && (local_hook(r) || fail_open_hook(r))) {
413 buffer_delete(this->rxbuf);
419 int retval = rconn_send(peer->rconn, this->rxbuf);
420 if (retval != EAGAIN) {
424 buffer_delete(this->rxbuf);
435 if (r->is_mgmt_conn) {
436 for (i = 0; i < 2; i++) {
437 struct half *this = &r->halves[i];
438 if (!rconn_is_alive(this->rconn)) {
447 relay_wait(struct relay *r)
451 for (i = 0; i < 2; i++) {
452 struct half *this = &r->halves[i];
454 rconn_run_wait(this->rconn);
456 rconn_recv_wait(this->rconn);
462 relay_destroy(struct relay *r)
466 list_remove(&r->node);
467 for (i = 0; i < 2; i++) {
468 struct half *this = &r->halves[i];
469 rconn_destroy(this->rconn);
470 buffer_delete(this->rxbuf);
476 queue_tx(struct rconn *rc, struct buffer *b)
478 if (rconn_force_send(rc, b)) {
484 is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
485 struct rconn *controller)
487 static uint32_t ip, last_nonzero_ip;
488 static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
489 static time_t next_refresh = 0;
491 uint32_t last_ip = ip;
493 time_t now = time(0);
495 ip = rconn_get_ip(controller);
496 if (last_ip != ip || !next_refresh || now >= next_refresh) {
499 /* Look up MAC address. */
500 memset(mac, 0, sizeof mac);
502 int retval = netdev_arp_lookup(of_device, ip, mac);
504 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
505 IP_ARGS(&ip), strerror(retval));
508 have_mac = !eth_addr_is_zero(mac);
510 /* Log changes in IP, MAC addresses. */
511 if (ip && ip != last_nonzero_ip) {
512 VLOG_DBG("controller IP address changed from "IP_FMT
513 " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
514 last_nonzero_ip = ip;
516 if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
517 VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
519 ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
520 memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
523 /* Schedule next refresh.
525 * If we have an IP address but not a MAC address, then refresh
526 * quickly, since we probably will get a MAC address soon (via ARP).
527 * Otherwise, we can afford to wait a little while. */
528 next_refresh = now + (!ip || have_mac ? 10 : 1);
530 return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
534 local_hook(struct relay *r)
536 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
537 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
538 struct ofp_packet_in *opi;
539 struct ofp_header *oh;
540 size_t pkt_ofs, pkt_len;
543 uint16_t in_port, out_port;
550 if (oh->type != OFPT_PACKET_IN) {
553 if (msg->size < offsetof (struct ofp_packet_in, data)) {
554 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
558 /* Extract flow data from 'opi' into 'flow'. */
560 in_port = ntohs(opi->in_port);
561 pkt_ofs = offsetof(struct ofp_packet_in, data);
562 pkt_len = ntohs(opi->header.length) - pkt_ofs;
563 pkt.data = opi->data;
565 flow_extract(&pkt, in_port, &flow);
567 /* Deal with local stuff. */
568 if (in_port == OFPP_LOCAL) {
569 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
570 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
571 out_port = OFPP_LOCAL;
572 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
573 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
574 ETH_ADDR_ARGS(flow.dl_src), in_port);
576 } else if (flow.dl_type == htons(ETH_TYPE_ARP)
577 && eth_addr_is_broadcast(flow.dl_dst)
578 && is_controller_mac(flow.dl_src,
579 r->halves[HALF_REMOTE].rconn)) {
580 out_port = OFPP_FLOOD;
585 if (out_port != OFPP_FLOOD) {
586 /* The output port is known, so add a new flow. */
587 queue_tx(rc, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
588 out_port, max_idle));
590 /* If the switch didn't buffer the packet, we need to send a copy. */
591 if (ntohl(opi->buffer_id) == UINT32_MAX) {
592 queue_tx(rc, make_unbuffered_packet_out(&pkt, in_port, out_port));
595 /* We don't know that MAC. Send along the packet without setting up a
598 if (ntohl(opi->buffer_id) == UINT32_MAX) {
599 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
601 b = make_buffered_packet_out(ntohl(opi->buffer_id),
609 /* Causes 'r' to enter or leave fail-open mode, if appropriate. Returns true
610 * if 'r' is in fail-open fail, false otherwise. */
612 failing_open(struct relay *r)
614 struct rconn *local = r->halves[HALF_LOCAL].rconn;
615 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
616 int disconnected_duration;
618 if (fail_mode == FAIL_CLOSED) {
619 /* We fail closed, so there's never anything to do. */
623 disconnected_duration = rconn_disconnected_duration(remote);
624 if (disconnected_duration < probe_interval * 3) {
625 /* It's not time to fail open yet. */
626 if (r->lswitch && rconn_is_connected(remote)) {
627 /* We're connected, so drop the learning switch. */
628 VLOG_WARN("No longer in fail-open mode");
629 lswitch_destroy(r->lswitch);
636 VLOG_WARN("Could not connect to controller for %d seconds, "
637 "failing open", disconnected_duration);
638 r->lswitch = lswitch_create(local, true, max_idle);
644 fail_open_hook(struct relay *r)
646 if (!failing_open(r)) {
649 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
650 struct rconn *local = r->halves[HALF_LOCAL].rconn;
651 lswitch_process_packet(r->lswitch, local, msg);
658 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
660 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
664 validate_dhcp_offer(const struct dhcp_msg *msg, void *aux)
669 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
671 VLOG_WARN("rejecting DHCP offer missing controller vconn");
674 accept = !regexec(&accept_controller_regex, vconn_name, 0, NULL, 0);
676 VLOG_WARN("rejecting controller vconn that fails to match %s",
677 accept_controller_re);
684 parse_options(int argc, char *argv[])
687 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
689 OPT_INACTIVITY_PROBE,
693 static struct option long_options[] = {
694 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
695 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
696 {"fail", required_argument, 0, 'f'},
697 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
698 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
699 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
700 {"listen", required_argument, 0, 'l'},
701 {"detach", no_argument, 0, 'D'},
702 {"pidfile", optional_argument, 0, 'P'},
703 {"verbose", optional_argument, 0, 'v'},
704 {"help", no_argument, 0, 'h'},
705 {"version", no_argument, 0, 'V'},
706 VCONN_SSL_LONG_OPTIONS
709 char *short_options = long_options_to_short_options(long_options);
714 c = getopt_long(argc, argv, short_options, long_options, NULL);
720 case OPT_ACCEPT_VCONN:
721 accept_controller_re = (optarg[0] == '^'
723 : xasprintf("^%s", optarg));
726 case OPT_NO_RESOLV_CONF:
727 update_resolv_conf = false;
731 if (!strcmp(optarg, "open")) {
732 fail_mode = FAIL_OPEN;
733 } else if (!strcmp(optarg, "closed")) {
734 fail_mode = FAIL_CLOSED;
737 "-f or --fail argument must be \"open\" or \"closed\"");
741 case OPT_INACTIVITY_PROBE:
742 probe_interval = atoi(optarg);
743 if (probe_interval < 5) {
744 fatal(0, "--inactivity-probe argument must be at least 5");
749 if (!strcmp(optarg, "permanent")) {
750 max_idle = OFP_FLOW_PERMANENT;
752 max_idle = atoi(optarg);
753 if (max_idle < 1 || max_idle > 65535) {
754 fatal(0, "--max-idle argument must be between 1 and "
755 "65535 or the word 'permanent'");
760 case OPT_MAX_BACKOFF:
761 max_backoff = atoi(optarg);
762 if (max_backoff < 1) {
763 fatal(0, "--max-backoff argument must be at least 1");
764 } else if (max_backoff > 3600) {
778 if (listen_vconn_name) {
779 fatal(0, "-l or --listen may be only specified once");
781 listen_vconn_name = optarg;
788 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
792 vlog_set_verbosity(optarg);
795 VCONN_SSL_OPTION_HANDLERS
810 printf("%s: secure channel, a relay for OpenFlow messages.\n"
811 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
812 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
813 "CONTROLLER is an active OpenFlow connection method; if it is\n"
814 "omitted, then secchan performs controller autodiscovery.\n",
815 program_name, program_name);
816 vconn_usage(true, true);
817 printf("\nController discovery options:\n"
818 " --accept-vconn=REGEX accept matching discovered controllers\n"
819 " --no-resolv-conf do not update /etc/resolv.conf\n"
820 "\nNetworking options:\n"
821 " -f, --fail=open|closed when controller connection fails:\n"
822 " closed: drop all packets\n"
823 " open (default): act as learning switch\n"
824 " --inactivity-probe=SECS time between inactivity probes\n"
825 " --max-idle=SECS max idle for flows set up by secchan\n"
826 " --max-backoff=SECS max time between controller connection\n"
827 " attempts (default: 15 seconds)\n"
828 " -l, --listen=METHOD allow management connections on METHOD\n"
829 " (a passive OpenFlow connection method)\n"
831 " -D, --detach run in background as daemon\n"
832 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
833 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
834 " -v, --verbose set maximum verbosity level\n"
835 " -h, --help display this help message\n"
836 " -V, --version display version information\n",