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 /* --inactivity-probe: Number of seconds without receiving a message from the
107 controller before sending an inactivity probe. */
108 static int probe_interval = 15;
110 /* --max-idle: Idle time to assign to flows created by learning switch when in
112 static int max_idle = 15;
114 static void parse_options(int argc, char *argv[]);
115 static void usage(void) NO_RETURN;
117 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
118 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
120 static void relay_run(struct relay *);
121 static void relay_wait(struct relay *);
122 static void relay_destroy(struct relay *);
124 static bool local_hook(struct relay *r);
125 static bool failing_open(struct relay *r);
126 static bool fail_open_hook(struct relay *r);
129 main(int argc, char *argv[])
131 struct vconn *listen_vconn;
132 struct netdev *of_device;
133 struct relay *controller_relay;
138 set_program_name(argv[0]);
139 register_fault_handlers();
141 parse_options(argc, argv);
143 if (argc - optind != 2) {
145 "need exactly two non-option arguments; use --help for usage");
147 nl_name = argv[optind];
148 if (strncmp(nl_name, "nl:", 3)
149 || strlen(nl_name) < 4
150 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
151 fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
154 if (listen_vconn_name) {
155 retval = vconn_open(listen_vconn_name, &listen_vconn);
156 if (retval && retval != EAGAIN) {
157 fatal(retval, "opening %s", listen_vconn_name);
159 if (!vconn_is_passive(listen_vconn)) {
160 fatal(0, "%s is not a passive vconn", listen_vconn_name);
166 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
167 retval = netdev_open(of_name, &of_device);
169 enum netdev_flags flags;
170 retval = netdev_get_flags(of_device, &flags);
172 if (flags & NETDEV_UP) {
176 memcpy(local_mac, netdev_get_etheraddr(of_device),
178 if (netdev_get_in6(of_device, &in6)) {
179 VLOG_WARN("Ignoring IPv6 address on %s device: "
180 "IPv6 not supported", of_name);
182 local_ml = mac_learning_create();
185 error(retval, "Could not get flags for %s device", of_name);
187 netdev_close(of_device);
189 error(retval, "Could not open %s device", of_name);
192 retval = vlog_server_listen(NULL, NULL);
194 fatal(retval, "Could not listen for vlog connections");
199 controller_relay = relay_create(rconn_new(argv[optind], 1, 0),
200 rconn_new(argv[optind + 1], 1,
207 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
212 struct vconn *new_remote;
213 retval = vconn_accept(listen_vconn, &new_remote);
215 if (retval != EAGAIN) {
216 VLOG_WARN("accept failed (%s)", strerror(retval));
220 new_management_connection(nl_name, new_remote);
223 failing_open(controller_relay);
225 /* Wait for something to happen. */
226 LIST_FOR_EACH (r, struct relay, node, &relays) {
230 vconn_accept_wait(listen_vconn);
239 new_management_connection(const char *nl_name, struct vconn *new_remote)
241 char *nl_name_without_subscription;
242 struct vconn *new_local;
243 struct rconn *r1, *r2;
246 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
247 * only accept the former syntax in main().
249 * nl:123:0 opens a netlink connection to local datapath 123 without
250 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
252 nl_name_without_subscription = xasprintf("%s:0", nl_name);
253 retval = vconn_open(nl_name_without_subscription, &new_local);
255 VLOG_ERR("could not connect to %s (%s)",
256 nl_name_without_subscription, strerror(retval));
257 vconn_close(new_remote);
258 free(nl_name_without_subscription);
262 /* Add it to the relay list. */
263 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
264 r2 = rconn_new_from_vconn("passive", 1, new_remote);
265 relay_create(r1, r2, true);
267 free(nl_name_without_subscription);
270 static struct relay *
271 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
276 r = xmalloc(sizeof *r);
277 r->halves[HALF_LOCAL].rconn = local;
278 r->halves[HALF_REMOTE].rconn = remote;
279 for (i = 0; i < 2; i++) {
280 r->halves[i].rxbuf = NULL;
282 r->is_mgmt_conn = is_mgmt_conn;
284 list_push_back(&relays, &r->node);
289 relay_run(struct relay *r)
294 for (i = 0; i < 2; i++) {
295 rconn_run(r->halves[i].rconn);
298 /* Limit the number of iterations to prevent other tasks from starving. */
299 for (iteration = 0; iteration < 50; iteration++) {
300 bool progress = false;
301 for (i = 0; i < 2; i++) {
302 struct half *this = &r->halves[i];
303 struct half *peer = &r->halves[!i];
306 this->rxbuf = rconn_recv(this->rconn);
307 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
308 && (local_hook(r) || fail_open_hook(r))) {
309 buffer_delete(this->rxbuf);
315 int retval = rconn_send(peer->rconn, this->rxbuf);
316 if (retval != EAGAIN) {
320 buffer_delete(this->rxbuf);
331 for (i = 0; i < 2; i++) {
332 struct half *this = &r->halves[i];
333 if (!rconn_is_alive(this->rconn)) {
341 relay_wait(struct relay *r)
345 for (i = 0; i < 2; i++) {
346 struct half *this = &r->halves[i];
348 rconn_run_wait(this->rconn);
350 rconn_recv_wait(this->rconn);
356 relay_destroy(struct relay *r)
360 list_remove(&r->node);
361 for (i = 0; i < 2; i++) {
362 struct half *this = &r->halves[i];
363 rconn_destroy(this->rconn);
364 buffer_delete(this->rxbuf);
370 local_hook(struct relay *r)
372 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
373 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
374 struct ofp_packet_in *opi;
375 struct ofp_header *oh;
376 size_t pkt_ofs, pkt_len;
377 struct buffer pkt, *b;
379 uint16_t in_port, out_port;
386 if (oh->type != OFPT_PACKET_IN) {
389 if (msg->size < offsetof (struct ofp_packet_in, data)) {
390 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
394 /* Extract flow data from 'opi' into 'flow'. */
396 in_port = ntohs(opi->in_port);
397 pkt_ofs = offsetof(struct ofp_packet_in, data);
398 pkt_len = ntohs(opi->header.length) - pkt_ofs;
399 pkt.data = opi->data;
401 flow_extract(&pkt, in_port, &flow);
403 /* Deal with local stuff. */
404 if (in_port == OFPP_LOCAL) {
405 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
406 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
407 out_port = OFPP_LOCAL;
408 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
409 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
410 ETH_ADDR_ARGS(flow.dl_src), in_port);
417 if (out_port != OFPP_FLOOD) {
418 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
420 if (rconn_force_send(rc, b)) {
425 /* If the switch didn't buffer the packet, we need to send a copy. */
426 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
427 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
428 if (rconn_force_send(rc, b)) {
435 /* Causess 'r' to enter or leave fail-open mode, if appropriate. Returns true
436 * if 'r' is in fail-open fail, false otherwise. */
438 failing_open(struct relay *r)
440 struct rconn *local = r->halves[HALF_LOCAL].rconn;
441 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
442 int disconnected_duration;
444 if (fail_mode == FAIL_CLOSED) {
445 /* We fail closed, so there's never anything to do. */
449 disconnected_duration = rconn_disconnected_duration(remote);
450 if (disconnected_duration < probe_interval * 3) {
451 /* It's not time to fail open yet. */
452 if (r->lswitch && rconn_is_connected(remote)) {
453 /* We're connected, so drop the learning switch. */
454 VLOG_WARN("No longer in fail-open mode");
455 lswitch_destroy(r->lswitch);
462 VLOG_WARN("Could not connect to controller for %d seconds, "
463 "failing open", disconnected_duration);
464 r->lswitch = lswitch_create(local, true, max_idle);
470 fail_open_hook(struct relay *r)
472 if (!failing_open(r)) {
475 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
476 struct rconn *local = r->halves[HALF_LOCAL].rconn;
477 lswitch_process_packet(r->lswitch, local, msg);
484 parse_options(int argc, char *argv[])
486 enum { OPT_INACTIVITY_PROBE = UCHAR_MAX + 1, OPT_MAX_IDLE };
487 static struct option long_options[] = {
488 {"fail", required_argument, 0, 'f'},
489 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
490 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
491 {"listen", required_argument, 0, 'l'},
492 {"detach", no_argument, 0, 'D'},
493 {"pidfile", optional_argument, 0, 'P'},
494 {"verbose", optional_argument, 0, 'v'},
495 {"help", no_argument, 0, 'h'},
496 {"version", no_argument, 0, 'V'},
497 VCONN_SSL_LONG_OPTIONS
500 char *short_options = long_options_to_short_options(long_options);
505 c = getopt_long(argc, argv, short_options, long_options, NULL);
512 if (!strcmp(optarg, "open")) {
513 fail_mode = FAIL_OPEN;
514 } else if (!strcmp(optarg, "closed")) {
515 fail_mode = FAIL_CLOSED;
518 "-f or --fail argument must be \"open\" or \"closed\"");
522 case OPT_INACTIVITY_PROBE:
523 probe_interval = atoi(optarg);
524 if (probe_interval < 5) {
525 fatal(0, "--inactivity-probe argument must be at least 5");
530 if (!strcmp(optarg, "permanent")) {
531 max_idle = OFP_FLOW_PERMANENT;
533 max_idle = atoi(optarg);
534 if (max_idle < 1 || max_idle > 65535) {
535 fatal(0, "--max-idle argument must be between 1 and "
536 "65535 or the word 'permanent'");
546 set_pidfile(optarg ? optarg : "secchan.pid");
550 if (listen_vconn_name) {
551 fatal(0, "-l or --listen may be only specified once");
553 listen_vconn_name = optarg;
560 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
564 vlog_set_verbosity(optarg);
567 VCONN_SSL_OPTION_HANDLERS
582 printf("%s: secure channel, a relay for OpenFlow messages.\n"
583 "usage: %s [OPTIONS] nl:DP_IDX CONTROLLER\n"
584 "where nl:DP_IDX is a datapath that has been added with dpctl\n"
585 "and CONTROLLER is an active OpenFlow connection method.\n",
586 program_name, program_name);
587 vconn_usage(true, true);
588 printf("\nNetworking options:\n"
589 " -f, --fail=open|closed when controller connection fails:\n"
590 " closed: drop all packets\n"
591 " open (default): act as learning switch\n"
592 " --inactivity-probe=SECS time between inactivity probes\n"
593 " --max-idle=SECS max idle for flows set up by secchan\n"
594 " -l, --listen=METHOD allow management connections on METHOD\n"
595 " (a passive OpenFlow connection method)\n"
597 " -D, --detach run in background as daemon\n"
598 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
599 " -v, --verbose=MODULE:FACILITY:LEVEL configure logging levels\n"
600 " -v, --verbose set maximum verbosity level\n"
601 " -h, --help display this help message\n"
602 " -V, --version display version information\n",