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 #include "ofp-print.h"
72 static const char *listen_vconn_name;
79 /* Behavior when the connection to the controller fails. */
81 FAIL_OPEN, /* Act as learning switch. */
82 FAIL_CLOSED /* Drop all packets. */
90 struct half halves[2];
93 struct lswitch *lswitch;
96 static struct list relays = LIST_INITIALIZER(&relays);
98 /* Mode of operation. Note that autodiscovery implies in-band
100 static bool autodiscovery; /* Discover the controller automatically? */
101 static bool in_band; /* Connect to controller in-band? */
103 /* MAC address of local port. */
104 static uint8_t local_mac[ETH_ADDR_LEN];
106 /* MAC learning table for local port. */
107 static struct mac_learning *local_ml;
109 /* Controller vconn name, or null to perform controller autodiscovery. */
110 static char *controller_name = NULL;
112 /* -f, --fail: Behavior when the connection to the controller fails. */
113 static enum fail_mode fail_mode = FAIL_OPEN;
115 /* The OpenFlow virtual network device ofX. */
116 static struct netdev *of_device;
118 /* --inactivity-probe: Number of seconds without receiving a message from the
119 controller before sending an inactivity probe. */
120 static int probe_interval = 15;
122 /* --max-idle: Idle time to assign to flows created by learning switch when in
124 static int max_idle = 15;
126 /* --max-backoff: Maximum interval between controller connection attempts, in
128 static int max_backoff = 15;
130 /* DHCP client, for controller autodiscovery. */
131 static struct dhclient *dhcp;
133 /* --accept-vconn: Regular expression specifying the class of controller vconns
134 * that we will accept during autodiscovery. */
135 static const char *accept_controller_re;
136 static regex_t accept_controller_regex;
138 static void parse_options(int argc, char *argv[]);
139 static void usage(void) NO_RETURN;
141 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
142 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
144 static void relay_run(struct relay *);
145 static void relay_wait(struct relay *);
146 static void relay_destroy(struct relay *);
148 static bool local_hook(struct relay *r);
149 static bool failing_open(struct relay *r);
150 static bool fail_open_hook(struct relay *r);
152 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
153 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
156 main(int argc, char *argv[])
158 struct rconn *local_rconn, *remote_rconn;
159 struct vconn *listen_vconn;
160 struct relay *controller_relay;
165 set_program_name(argv[0]);
166 register_fault_handlers();
168 parse_options(argc, argv);
172 if (argc < 1 || argc > 2) {
173 fatal(0, "need one or two non-option arguments; use --help for usage");
176 if (strncmp(nl_name, "nl:", 3)
177 || strlen(nl_name) < 4
178 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
179 fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
181 controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
182 autodiscovery = controller_name == NULL;
184 if (!accept_controller_re) {
185 accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
187 retval = regcomp(&accept_controller_regex, accept_controller_re,
188 REG_NOSUB | REG_EXTENDED);
190 size_t length = regerror(retval, &accept_controller_regex, NULL, 0);
191 char *buffer = xmalloc(length);
192 regerror(retval, &accept_controller_regex, buffer, length);
193 fatal(0, "%s: %s", accept_controller_re, buffer);
196 if (listen_vconn_name) {
197 retval = vconn_open(listen_vconn_name, &listen_vconn);
198 if (retval && retval != EAGAIN) {
199 fatal(retval, "opening %s", listen_vconn_name);
201 if (!vconn_is_passive(listen_vconn)) {
202 fatal(0, "%s is not a passive vconn", listen_vconn_name);
208 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
209 retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
211 enum netdev_flags flags;
214 retval = netdev_turn_flags_on(of_device, NETDEV_UP, true);
216 fatal(retval, "Could not bring %s device up", of_name);
220 retval = netdev_get_flags(of_device, &flags);
222 if (flags & NETDEV_UP) {
226 memcpy(local_mac, netdev_get_etheraddr(of_device),
228 if (netdev_get_in6(of_device, &in6)) {
229 VLOG_WARN("Ignoring IPv6 address on %s device: "
230 "IPv6 not supported", of_name);
232 local_ml = mac_learning_create();
235 error(retval, "Could not get flags for %s device", of_name);
238 error(retval, "Could not open %s device", of_name);
240 if (autodiscovery && !in_band) {
241 fatal(retval, "In autodiscovery mode but failed to configure "
246 retval = dhclient_create(of_name, modify_dhcp_request,
247 validate_dhcp_offer, NULL, &dhcp);
249 fatal(retval, "Failed to initialize DHCP client");
251 dhclient_init(dhcp, 0);
254 retval = vlog_server_listen(NULL, NULL);
256 fatal(retval, "Could not listen for vlog connections");
261 local_rconn = rconn_create(1, 0, max_backoff);
262 rconn_connect(local_rconn, nl_name);
264 remote_rconn = rconn_create(1, probe_interval, max_backoff);
265 if (controller_name) {
266 retval = rconn_connect(remote_rconn, controller_name);
267 if (retval == EAFNOSUPPORT) {
268 fatal(0, "No support for %s vconn", controller_name);
271 controller_relay = relay_create(local_rconn, remote_rconn, false);
276 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
281 struct vconn *new_remote;
282 retval = vconn_accept(listen_vconn, &new_remote);
284 if (retval != EAGAIN) {
285 VLOG_WARN("accept failed (%s)", strerror(retval));
289 new_management_connection(nl_name, new_remote);
292 if (controller_relay) {
293 /* FIXME: should also fail open when controller_relay is NULL. */
294 failing_open(controller_relay);
297 if (rconn_is_connectivity_questionable(remote_rconn)) {
298 dhclient_force_renew(dhcp, 15);
301 if (dhclient_changed(dhcp)) {
302 free(controller_name);
303 if (dhclient_is_bound(dhcp)) {
304 controller_name = dhcp_msg_get_string(
305 dhclient_get_config(dhcp),
306 DHCP_CODE_OFP_CONTROLLER_VCONN);
307 VLOG_WARN("%s: discovered controller",
309 rconn_connect(remote_rconn, controller_name);
310 } else if (controller_name) {
311 VLOG_WARN("%s: discover controller no longer available",
313 controller_name = NULL;
314 rconn_disconnect(remote_rconn);
319 /* Wait for something to happen. */
320 LIST_FOR_EACH (r, struct relay, node, &relays) {
324 vconn_accept_wait(listen_vconn);
336 new_management_connection(const char *nl_name, struct vconn *new_remote)
338 char *nl_name_without_subscription;
339 struct vconn *new_local;
340 struct rconn *r1, *r2;
343 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
344 * only accept the former syntax in main().
346 * nl:123:0 opens a netlink connection to local datapath 123 without
347 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
349 nl_name_without_subscription = xasprintf("%s:0", nl_name);
350 retval = vconn_open(nl_name_without_subscription, &new_local);
352 VLOG_ERR("could not connect to %s (%s)",
353 nl_name_without_subscription, strerror(retval));
354 vconn_close(new_remote);
355 free(nl_name_without_subscription);
359 /* Add it to the relay list. */
360 r1 = rconn_create(1, 0, 0);
361 rconn_connect_unreliably(r1, nl_name_without_subscription, new_local);
362 r2 = rconn_create(1, 0, 0);
363 rconn_connect_unreliably(r2, "passive", new_remote);
364 relay_create(r1, r2, true);
366 free(nl_name_without_subscription);
369 static struct relay *
370 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
375 r = xmalloc(sizeof *r);
376 r->halves[HALF_LOCAL].rconn = local;
377 r->halves[HALF_REMOTE].rconn = remote;
378 for (i = 0; i < 2; i++) {
379 r->halves[i].rxbuf = NULL;
381 r->is_mgmt_conn = is_mgmt_conn;
383 list_push_back(&relays, &r->node);
388 relay_run(struct relay *r)
393 for (i = 0; i < 2; i++) {
394 rconn_run(r->halves[i].rconn);
397 /* Limit the number of iterations to prevent other tasks from starving. */
398 for (iteration = 0; iteration < 50; iteration++) {
399 bool progress = false;
400 for (i = 0; i < 2; i++) {
401 struct half *this = &r->halves[i];
402 struct half *peer = &r->halves[!i];
405 this->rxbuf = rconn_recv(this->rconn);
406 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
407 && (local_hook(r) || fail_open_hook(r))) {
408 buffer_delete(this->rxbuf);
414 int retval = rconn_send(peer->rconn, this->rxbuf);
415 if (retval != EAGAIN) {
419 buffer_delete(this->rxbuf);
430 if (r->is_mgmt_conn) {
431 for (i = 0; i < 2; i++) {
432 struct half *this = &r->halves[i];
433 if (!rconn_is_alive(this->rconn)) {
442 relay_wait(struct relay *r)
446 for (i = 0; i < 2; i++) {
447 struct half *this = &r->halves[i];
449 rconn_run_wait(this->rconn);
451 rconn_recv_wait(this->rconn);
457 relay_destroy(struct relay *r)
461 list_remove(&r->node);
462 for (i = 0; i < 2; i++) {
463 struct half *this = &r->halves[i];
464 rconn_destroy(this->rconn);
465 buffer_delete(this->rxbuf);
471 queue_tx(struct rconn *rc, struct buffer *b)
473 if (rconn_force_send(rc, b)) {
479 is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
480 struct rconn *controller)
482 static uint32_t ip, last_nonzero_ip;
483 static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
484 static time_t next_refresh = 0;
486 uint32_t last_ip = ip;
488 time_t now = time(0);
490 ip = rconn_get_ip(controller);
491 if (last_ip != ip || !next_refresh || now >= next_refresh) {
494 /* Look up MAC address. */
495 memset(mac, 0, sizeof mac);
497 int retval = netdev_arp_lookup(of_device, ip, mac);
499 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
500 IP_ARGS(&ip), strerror(retval));
503 have_mac = !eth_addr_is_zero(mac);
505 /* Log changes in IP, MAC addresses. */
506 if (ip && ip != last_nonzero_ip) {
507 VLOG_DBG("controller IP address changed from "IP_FMT
508 " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
509 last_nonzero_ip = ip;
511 if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
512 VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
514 ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
515 memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
518 /* Schedule next refresh.
520 * If we have an IP address but not a MAC address, then refresh
521 * quickly, since we probably will get a MAC address soon (via ARP).
522 * Otherwise, we can afford to wait a little while. */
523 next_refresh = now + (!ip || have_mac ? 10 : 1);
525 return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
529 local_hook(struct relay *r)
531 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
532 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
533 struct ofp_packet_in *opi;
534 struct ofp_header *oh;
535 size_t pkt_ofs, pkt_len;
538 uint16_t in_port, out_port;
545 if (oh->type != OFPT_PACKET_IN) {
548 if (msg->size < offsetof (struct ofp_packet_in, data)) {
549 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
553 /* Extract flow data from 'opi' into 'flow'. */
555 in_port = ntohs(opi->in_port);
556 pkt_ofs = offsetof(struct ofp_packet_in, data);
557 pkt_len = ntohs(opi->header.length) - pkt_ofs;
558 pkt.data = opi->data;
560 flow_extract(&pkt, in_port, &flow);
562 /* Deal with local stuff. */
563 if (in_port == OFPP_LOCAL) {
564 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
565 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
566 out_port = OFPP_LOCAL;
567 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
568 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
569 ETH_ADDR_ARGS(flow.dl_src), in_port);
571 } else if (flow.dl_type == htons(ETH_TYPE_ARP)
572 && eth_addr_is_broadcast(flow.dl_dst)
573 && is_controller_mac(flow.dl_src,
574 r->halves[HALF_REMOTE].rconn)) {
575 out_port = OFPP_FLOOD;
580 if (out_port != OFPP_FLOOD) {
581 /* The output port is known, so add a new flow. */
582 queue_tx(rc, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
583 out_port, max_idle));
585 /* If the switch didn't buffer the packet, we need to send a copy. */
586 if (ntohl(opi->buffer_id) == UINT32_MAX) {
587 queue_tx(rc, make_unbuffered_packet_out(&pkt, in_port, out_port));
590 /* We don't know that MAC. Send along the packet without setting up a
593 if (ntohl(opi->buffer_id) == UINT32_MAX) {
594 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
596 b = make_buffered_packet_out(ntohl(opi->buffer_id),
604 /* Causess 'r' to enter or leave fail-open mode, if appropriate. Returns true
605 * if 'r' is in fail-open fail, false otherwise. */
607 failing_open(struct relay *r)
609 struct rconn *local = r->halves[HALF_LOCAL].rconn;
610 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
611 int disconnected_duration;
613 if (fail_mode == FAIL_CLOSED) {
614 /* We fail closed, so there's never anything to do. */
618 disconnected_duration = rconn_disconnected_duration(remote);
619 if (disconnected_duration < probe_interval * 3) {
620 /* It's not time to fail open yet. */
621 if (r->lswitch && rconn_is_connected(remote)) {
622 /* We're connected, so drop the learning switch. */
623 VLOG_WARN("No longer in fail-open mode");
624 lswitch_destroy(r->lswitch);
631 VLOG_WARN("Could not connect to controller for %d seconds, "
632 "failing open", disconnected_duration);
633 r->lswitch = lswitch_create(local, true, max_idle);
639 fail_open_hook(struct relay *r)
641 if (!failing_open(r)) {
644 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
645 struct rconn *local = r->halves[HALF_LOCAL].rconn;
646 lswitch_process_packet(r->lswitch, local, msg);
653 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
655 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
659 validate_dhcp_offer(const struct dhcp_msg *msg, void *aux)
664 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
666 VLOG_WARN("rejecting DHCP offer missing controller vconn");
669 accept = !regexec(&accept_controller_regex, vconn_name, 0, NULL, 0);
675 parse_options(int argc, char *argv[])
678 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
679 OPT_INACTIVITY_PROBE,
683 static struct option long_options[] = {
684 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
685 {"fail", required_argument, 0, 'f'},
686 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
687 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
688 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
689 {"listen", required_argument, 0, 'l'},
690 {"detach", no_argument, 0, 'D'},
691 {"pidfile", optional_argument, 0, 'P'},
692 {"verbose", optional_argument, 0, 'v'},
693 {"help", no_argument, 0, 'h'},
694 {"version", no_argument, 0, 'V'},
695 VCONN_SSL_LONG_OPTIONS
698 char *short_options = long_options_to_short_options(long_options);
703 c = getopt_long(argc, argv, short_options, long_options, NULL);
709 case OPT_ACCEPT_VCONN:
710 accept_controller_re = (optarg[0] == '^'
712 : xasprintf("^%s", optarg));
716 if (!strcmp(optarg, "open")) {
717 fail_mode = FAIL_OPEN;
718 } else if (!strcmp(optarg, "closed")) {
719 fail_mode = FAIL_CLOSED;
722 "-f or --fail argument must be \"open\" or \"closed\"");
726 case OPT_INACTIVITY_PROBE:
727 probe_interval = atoi(optarg);
728 if (probe_interval < 5) {
729 fatal(0, "--inactivity-probe argument must be at least 5");
734 if (!strcmp(optarg, "permanent")) {
735 max_idle = OFP_FLOW_PERMANENT;
737 max_idle = atoi(optarg);
738 if (max_idle < 1 || max_idle > 65535) {
739 fatal(0, "--max-idle argument must be between 1 and "
740 "65535 or the word 'permanent'");
745 case OPT_MAX_BACKOFF:
746 max_backoff = atoi(optarg);
747 if (max_backoff < 1) {
748 fatal(0, "--max-backoff argument must be at least 1");
749 } else if (max_backoff > 3600) {
759 set_pidfile(optarg ? optarg : "secchan.pid");
763 if (listen_vconn_name) {
764 fatal(0, "-l or --listen may be only specified once");
766 listen_vconn_name = optarg;
773 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
777 vlog_set_verbosity(optarg);
780 VCONN_SSL_OPTION_HANDLERS
795 printf("%s: secure channel, a relay for OpenFlow messages.\n"
796 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
797 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
798 "CONTROLLER is an active OpenFlow connection method; if it is\n"
799 "omitted, then secchan performs controller autodiscovery.\n",
800 program_name, program_name);
801 vconn_usage(true, true);
802 printf("\nNetworking options:\n"
803 " --accept-vconn=REGEX accept matching discovered controllers\n"
804 " -f, --fail=open|closed when controller connection fails:\n"
805 " closed: drop all packets\n"
806 " open (default): act as learning switch\n"
807 " --inactivity-probe=SECS time between inactivity probes\n"
808 " --max-idle=SECS max idle for flows set up by secchan\n"
809 " --max-backoff=SECS max time between controller connection\n"
810 " attempts (default: 15 seconds)\n"
811 " -l, --listen=METHOD allow management connections on METHOD\n"
812 " (a passive OpenFlow connection method)\n"
814 " -D, --detach run in background as daemon\n"
815 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
816 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
817 " -v, --verbose set maximum verbosity level\n"
818 " -h, --help display this help message\n"
819 " -V, --version display version information\n",