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.
37 #include <netinet/in.h>
45 #include "command-line.h"
50 #include "learning-switch.h"
52 #include "mac-learning.h"
56 #include "poll-loop.h"
59 #include "vconn-ssl.h"
61 #include "vlog-socket.h"
64 #define THIS_MODULE VLM_secchan
66 #include "ofp-print.h"
68 static const char *listen_vconn_name;
75 /* Behavior when the connection to the controller fails. */
77 FAIL_OPEN, /* Act as learning switch. */
78 FAIL_CLOSED /* Drop all packets. */
86 struct half halves[2];
89 struct lswitch *lswitch;
92 static struct list relays = LIST_INITIALIZER(&relays);
94 /* Enable the local port? */
95 static int local_port;
97 /* MAC address of local port. */
98 static uint8_t local_mac[ETH_ADDR_LEN];
100 /* MAC learning table for local port. */
101 static struct mac_learning *local_ml;
103 /* -f, --fail: Behavior when the connection to the controller fails. */
104 static enum fail_mode fail_mode = FAIL_OPEN;
106 /* The OpenFlow virtual network device ofX. */
107 static struct netdev *of_device;
109 /* --inactivity-probe: Number of seconds without receiving a message from the
110 controller before sending an inactivity probe. */
111 static int probe_interval = 15;
113 /* --max-idle: Idle time to assign to flows created by learning switch when in
115 static int max_idle = 15;
117 static void parse_options(int argc, char *argv[]);
118 static void usage(void) NO_RETURN;
120 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
121 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
123 static void relay_run(struct relay *);
124 static void relay_wait(struct relay *);
125 static void relay_destroy(struct relay *);
127 static bool local_hook(struct relay *r);
128 static bool failing_open(struct relay *r);
129 static bool fail_open_hook(struct relay *r);
132 main(int argc, char *argv[])
134 struct vconn *listen_vconn;
135 struct relay *controller_relay;
140 set_program_name(argv[0]);
141 register_fault_handlers();
143 parse_options(argc, argv);
145 if (argc - optind != 2) {
147 "need exactly two non-option arguments; use --help for usage");
149 nl_name = argv[optind];
150 if (strncmp(nl_name, "nl:", 3)
151 || strlen(nl_name) < 4
152 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
153 fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
156 if (listen_vconn_name) {
157 retval = vconn_open(listen_vconn_name, &listen_vconn);
158 if (retval && retval != EAGAIN) {
159 fatal(retval, "opening %s", listen_vconn_name);
161 if (!vconn_is_passive(listen_vconn)) {
162 fatal(0, "%s is not a passive vconn", listen_vconn_name);
168 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
169 retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
171 enum netdev_flags flags;
172 retval = netdev_get_flags(of_device, &flags);
174 if (flags & NETDEV_UP) {
178 memcpy(local_mac, netdev_get_etheraddr(of_device),
180 if (netdev_get_in6(of_device, &in6)) {
181 VLOG_WARN("Ignoring IPv6 address on %s device: "
182 "IPv6 not supported", of_name);
184 local_ml = mac_learning_create();
187 error(retval, "Could not get flags for %s device", of_name);
190 error(retval, "Could not open %s device", of_name);
193 retval = vlog_server_listen(NULL, NULL);
195 fatal(retval, "Could not listen for vlog connections");
200 controller_relay = relay_create(rconn_new(argv[optind], 1, 0),
201 rconn_new(argv[optind + 1], 1,
208 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
213 struct vconn *new_remote;
214 retval = vconn_accept(listen_vconn, &new_remote);
216 if (retval != EAGAIN) {
217 VLOG_WARN("accept failed (%s)", strerror(retval));
221 new_management_connection(nl_name, new_remote);
224 failing_open(controller_relay);
226 /* Wait for something to happen. */
227 LIST_FOR_EACH (r, struct relay, node, &relays) {
231 vconn_accept_wait(listen_vconn);
240 new_management_connection(const char *nl_name, struct vconn *new_remote)
242 char *nl_name_without_subscription;
243 struct vconn *new_local;
244 struct rconn *r1, *r2;
247 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
248 * only accept the former syntax in main().
250 * nl:123:0 opens a netlink connection to local datapath 123 without
251 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
253 nl_name_without_subscription = xasprintf("%s:0", nl_name);
254 retval = vconn_open(nl_name_without_subscription, &new_local);
256 VLOG_ERR("could not connect to %s (%s)",
257 nl_name_without_subscription, strerror(retval));
258 vconn_close(new_remote);
259 free(nl_name_without_subscription);
263 /* Add it to the relay list. */
264 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
265 r2 = rconn_new_from_vconn("passive", 1, new_remote);
266 relay_create(r1, r2, true);
268 free(nl_name_without_subscription);
271 static struct relay *
272 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
277 r = xmalloc(sizeof *r);
278 r->halves[HALF_LOCAL].rconn = local;
279 r->halves[HALF_REMOTE].rconn = remote;
280 for (i = 0; i < 2; i++) {
281 r->halves[i].rxbuf = NULL;
283 r->is_mgmt_conn = is_mgmt_conn;
285 list_push_back(&relays, &r->node);
290 relay_run(struct relay *r)
295 for (i = 0; i < 2; i++) {
296 rconn_run(r->halves[i].rconn);
299 /* Limit the number of iterations to prevent other tasks from starving. */
300 for (iteration = 0; iteration < 50; iteration++) {
301 bool progress = false;
302 for (i = 0; i < 2; i++) {
303 struct half *this = &r->halves[i];
304 struct half *peer = &r->halves[!i];
307 this->rxbuf = rconn_recv(this->rconn);
308 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
309 && (local_hook(r) || fail_open_hook(r))) {
310 buffer_delete(this->rxbuf);
316 int retval = rconn_send(peer->rconn, this->rxbuf);
317 if (retval != EAGAIN) {
321 buffer_delete(this->rxbuf);
332 for (i = 0; i < 2; i++) {
333 struct half *this = &r->halves[i];
334 if (!rconn_is_alive(this->rconn)) {
342 relay_wait(struct relay *r)
346 for (i = 0; i < 2; i++) {
347 struct half *this = &r->halves[i];
349 rconn_run_wait(this->rconn);
351 rconn_recv_wait(this->rconn);
357 relay_destroy(struct relay *r)
361 list_remove(&r->node);
362 for (i = 0; i < 2; i++) {
363 struct half *this = &r->halves[i];
364 rconn_destroy(this->rconn);
365 buffer_delete(this->rxbuf);
371 is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
372 struct rconn *controller)
374 static uint32_t ip, last_nonzero_ip;
375 static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
376 static time_t next_refresh = 0;
378 uint32_t last_ip = ip;
380 time_t now = time(0);
382 ip = rconn_get_ip(controller);
383 if (last_ip != ip || !next_refresh || now >= next_refresh) {
386 /* Look up MAC address. */
387 memset(mac, 0, sizeof mac);
389 int retval = netdev_arp_lookup(of_device, ip, mac);
391 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
392 IP_ARGS(&ip), strerror(retval));
395 have_mac = !eth_addr_is_zero(mac);
397 /* Log changes in IP, MAC addresses. */
398 if (ip && ip != last_nonzero_ip) {
399 VLOG_DBG("controller IP address changed from "IP_FMT
400 " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
401 last_nonzero_ip = ip;
403 if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
404 VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
406 ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
407 memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
410 /* Schedule next refresh.
412 * If we have an IP address but not a MAC address, then refresh
413 * quickly, since we probably will get a MAC address soon (via ARP).
414 * Otherwise, we can afford to wait a little while. */
415 next_refresh = now + (!ip || have_mac ? 10 : 1);
417 return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
421 local_hook(struct relay *r)
423 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
424 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
425 struct ofp_packet_in *opi;
426 struct ofp_header *oh;
427 size_t pkt_ofs, pkt_len;
428 struct buffer pkt, *b;
430 uint16_t in_port, out_port;
437 if (oh->type != OFPT_PACKET_IN) {
440 if (msg->size < offsetof (struct ofp_packet_in, data)) {
441 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
445 /* Extract flow data from 'opi' into 'flow'. */
447 in_port = ntohs(opi->in_port);
448 pkt_ofs = offsetof(struct ofp_packet_in, data);
449 pkt_len = ntohs(opi->header.length) - pkt_ofs;
450 pkt.data = opi->data;
452 flow_extract(&pkt, in_port, &flow);
454 /* Deal with local stuff. */
455 if (in_port == OFPP_LOCAL) {
456 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
457 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
458 out_port = OFPP_LOCAL;
459 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
460 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
461 ETH_ADDR_ARGS(flow.dl_src), in_port);
463 } else if (flow.dl_type == htons(ETH_TYPE_ARP)
464 && eth_addr_is_broadcast(flow.dl_dst)
465 && is_controller_mac(flow.dl_src,
466 r->halves[HALF_REMOTE].rconn)) {
467 out_port = OFPP_FLOOD;
473 if (out_port != OFPP_FLOOD) {
474 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
476 if (rconn_force_send(rc, b)) {
481 /* If the switch didn't buffer the packet, we need to send a copy. */
482 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
483 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
484 if (rconn_force_send(rc, b)) {
491 /* Causess 'r' to enter or leave fail-open mode, if appropriate. Returns true
492 * if 'r' is in fail-open fail, false otherwise. */
494 failing_open(struct relay *r)
496 struct rconn *local = r->halves[HALF_LOCAL].rconn;
497 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
498 int disconnected_duration;
500 if (fail_mode == FAIL_CLOSED) {
501 /* We fail closed, so there's never anything to do. */
505 disconnected_duration = rconn_disconnected_duration(remote);
506 if (disconnected_duration < probe_interval * 3) {
507 /* It's not time to fail open yet. */
508 if (r->lswitch && rconn_is_connected(remote)) {
509 /* We're connected, so drop the learning switch. */
510 VLOG_WARN("No longer in fail-open mode");
511 lswitch_destroy(r->lswitch);
518 VLOG_WARN("Could not connect to controller for %d seconds, "
519 "failing open", disconnected_duration);
520 r->lswitch = lswitch_create(local, true, max_idle);
526 fail_open_hook(struct relay *r)
528 if (!failing_open(r)) {
531 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
532 struct rconn *local = r->halves[HALF_LOCAL].rconn;
533 lswitch_process_packet(r->lswitch, local, msg);
540 parse_options(int argc, char *argv[])
542 enum { OPT_INACTIVITY_PROBE = UCHAR_MAX + 1, OPT_MAX_IDLE };
543 static struct option long_options[] = {
544 {"fail", required_argument, 0, 'f'},
545 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
546 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
547 {"listen", required_argument, 0, 'l'},
548 {"detach", no_argument, 0, 'D'},
549 {"pidfile", optional_argument, 0, 'P'},
550 {"verbose", optional_argument, 0, 'v'},
551 {"help", no_argument, 0, 'h'},
552 {"version", no_argument, 0, 'V'},
553 VCONN_SSL_LONG_OPTIONS
556 char *short_options = long_options_to_short_options(long_options);
561 c = getopt_long(argc, argv, short_options, long_options, NULL);
568 if (!strcmp(optarg, "open")) {
569 fail_mode = FAIL_OPEN;
570 } else if (!strcmp(optarg, "closed")) {
571 fail_mode = FAIL_CLOSED;
574 "-f or --fail argument must be \"open\" or \"closed\"");
578 case OPT_INACTIVITY_PROBE:
579 probe_interval = atoi(optarg);
580 if (probe_interval < 5) {
581 fatal(0, "--inactivity-probe argument must be at least 5");
586 if (!strcmp(optarg, "permanent")) {
587 max_idle = OFP_FLOW_PERMANENT;
589 max_idle = atoi(optarg);
590 if (max_idle < 1 || max_idle > 65535) {
591 fatal(0, "--max-idle argument must be between 1 and "
592 "65535 or the word 'permanent'");
602 set_pidfile(optarg ? optarg : "secchan.pid");
606 if (listen_vconn_name) {
607 fatal(0, "-l or --listen may be only specified once");
609 listen_vconn_name = optarg;
616 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
620 vlog_set_verbosity(optarg);
623 VCONN_SSL_OPTION_HANDLERS
638 printf("%s: secure channel, a relay for OpenFlow messages.\n"
639 "usage: %s [OPTIONS] nl:DP_IDX CONTROLLER\n"
640 "where nl:DP_IDX is a datapath that has been added with dpctl\n"
641 "and CONTROLLER is an active OpenFlow connection method.\n",
642 program_name, program_name);
643 vconn_usage(true, true);
644 printf("\nNetworking options:\n"
645 " -f, --fail=open|closed when controller connection fails:\n"
646 " closed: drop all packets\n"
647 " open (default): act as learning switch\n"
648 " --inactivity-probe=SECS time between inactivity probes\n"
649 " --max-idle=SECS max idle for flows set up by secchan\n"
650 " -l, --listen=METHOD allow management connections on METHOD\n"
651 " (a passive OpenFlow connection method)\n"
653 " -D, --detach run in background as daemon\n"
654 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
655 " -v, --verbose=MODULE:FACILITY:LEVEL configure logging levels\n"
656 " -v, --verbose set maximum verbosity level\n"
657 " -h, --help display this help message\n"
658 " -V, --version display version information\n",