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 /* -d, --fail-open-delay: Number of seconds after which to fail open, when
107 * fail_mode is FAIL_OPEN. */
108 static int fail_open_delay = 30;
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 fail_open_hook(struct relay *r);
128 main(int argc, char *argv[])
130 struct vconn *listen_vconn;
131 struct netdev *of_device;
136 set_program_name(argv[0]);
137 register_fault_handlers();
139 parse_options(argc, argv);
141 if (argc - optind != 2) {
143 "need exactly two non-option arguments; use --help for usage");
145 nl_name = argv[optind];
146 if (strncmp(nl_name, "nl:", 3)
147 || strlen(nl_name) < 4
148 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
149 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
152 if (listen_vconn_name) {
153 retval = vconn_open(listen_vconn_name, &listen_vconn);
154 if (retval && retval != EAGAIN) {
155 fatal(retval, "opening %s", listen_vconn_name);
157 if (!vconn_is_passive(listen_vconn)) {
158 fatal(0, "%s is not a passive vconn", listen_vconn_name);
164 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
165 retval = netdev_open(of_name, &of_device);
167 enum netdev_flags flags;
168 retval = netdev_get_flags(of_device, &flags);
170 if (flags & NETDEV_UP) {
174 memcpy(local_mac, netdev_get_etheraddr(of_device),
176 if (netdev_get_in6(of_device, &in6)) {
177 VLOG_WARN("Ignoring IPv6 address on %s device: "
178 "IPv6 not supported", of_name);
180 local_ml = mac_learning_create();
183 error(retval, "Could not get flags for %s device", of_name);
185 netdev_close(of_device);
187 error(retval, "Could not open %s device", of_name);
190 retval = vlog_server_listen(NULL, NULL);
192 fatal(retval, "Could not listen for vlog connections");
197 relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1),
203 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
208 struct vconn *new_remote;
209 retval = vconn_accept(listen_vconn, &new_remote);
211 if (retval != EAGAIN) {
212 VLOG_WARN("accept failed (%s)", strerror(retval));
216 new_management_connection(nl_name, new_remote);
220 /* Wait for something to happen. */
221 LIST_FOR_EACH (r, struct relay, node, &relays) {
225 vconn_accept_wait(listen_vconn);
234 new_management_connection(const char *nl_name, struct vconn *new_remote)
236 char *nl_name_without_subscription;
237 struct vconn *new_local;
238 struct rconn *r1, *r2;
241 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
242 * only accept the former syntax in main().
244 * nl:123:0 opens a netlink connection to local datapath 123 without
245 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
247 nl_name_without_subscription = xasprintf("%s:0", nl_name);
248 retval = vconn_open(nl_name_without_subscription, &new_local);
250 VLOG_ERR("could not connect to %s (%s)",
251 nl_name_without_subscription, strerror(retval));
252 vconn_close(new_remote);
253 free(nl_name_without_subscription);
257 /* Add it to the relay list. */
258 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
259 r2 = rconn_new_from_vconn("passive", 1, new_remote);
260 relay_create(r1, r2, true);
262 free(nl_name_without_subscription);
265 static struct relay *
266 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
271 r = xmalloc(sizeof *r);
272 r->halves[HALF_LOCAL].rconn = local;
273 r->halves[HALF_REMOTE].rconn = remote;
274 for (i = 0; i < 2; i++) {
275 r->halves[i].rxbuf = NULL;
277 r->is_mgmt_conn = is_mgmt_conn;
279 list_push_back(&relays, &r->node);
284 relay_run(struct relay *r)
289 for (i = 0; i < 2; i++) {
290 rconn_run(r->halves[i].rconn);
293 /* Limit the number of iterations to prevent other tasks from starving. */
294 for (iteration = 0; iteration < 50; iteration++) {
295 bool progress = false;
296 for (i = 0; i < 2; i++) {
297 struct half *this = &r->halves[i];
298 struct half *peer = &r->halves[!i];
301 this->rxbuf = rconn_recv(this->rconn);
302 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
303 && (local_hook(r) || fail_open_hook(r))) {
304 buffer_delete(this->rxbuf);
310 int retval = rconn_send(peer->rconn, this->rxbuf);
311 if (retval != EAGAIN) {
315 buffer_delete(this->rxbuf);
326 for (i = 0; i < 2; i++) {
327 struct half *this = &r->halves[i];
328 if (!rconn_is_alive(this->rconn)) {
336 relay_wait(struct relay *r)
340 for (i = 0; i < 2; i++) {
341 struct half *this = &r->halves[i];
343 rconn_run_wait(this->rconn);
345 rconn_recv_wait(this->rconn);
351 relay_destroy(struct relay *r)
355 list_remove(&r->node);
356 for (i = 0; i < 2; i++) {
357 struct half *this = &r->halves[i];
358 rconn_destroy(this->rconn);
359 buffer_delete(this->rxbuf);
365 local_hook(struct relay *r)
367 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
368 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
369 struct ofp_packet_in *opi;
370 struct ofp_header *oh;
371 size_t pkt_ofs, pkt_len;
372 struct buffer pkt, *b;
374 uint16_t in_port, out_port;
381 if (oh->type != OFPT_PACKET_IN) {
384 if (msg->size < offsetof (struct ofp_packet_in, data)) {
385 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
389 /* Extract flow data from 'opi' into 'flow'. */
391 in_port = ntohs(opi->in_port);
392 pkt_ofs = offsetof(struct ofp_packet_in, data);
393 pkt_len = ntohs(opi->header.length) - pkt_ofs;
394 pkt.data = opi->data;
396 flow_extract(&pkt, in_port, &flow);
398 /* Deal with local stuff. */
399 if (!rconn_is_connected(r->halves[HALF_REMOTE].rconn)
400 && eth_addr_is_broadcast(flow.dl_dst)) {
401 out_port = OFPP_FLOOD;
402 } else if (in_port == OFPP_LOCAL) {
403 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
404 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
405 out_port = OFPP_LOCAL;
406 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
407 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
408 ETH_ADDR_ARGS(flow.dl_src), in_port);
415 if (out_port != OFPP_FLOOD) {
416 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
418 if (rconn_force_send(rc, b)) {
423 /* If the switch didn't buffer the packet, we need to send a copy. */
424 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
425 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
426 if (rconn_force_send(rc, b)) {
434 fail_open_hook(struct relay *r)
436 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
437 struct rconn *local = r->halves[HALF_LOCAL].rconn;
438 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
439 int disconnected_duration;
441 if (fail_mode == FAIL_CLOSED) {
442 /* We fail closed, so there's never anything to do. */
446 disconnected_duration = rconn_disconnected_duration(remote);
447 if (disconnected_duration < fail_open_delay) {
448 /* It's not time to fail open yet. */
449 if (r->lswitch && rconn_is_connected(remote)) {
450 /* We're connected, so drop the learning switch. */
451 VLOG_WARN("No longer in fail-open mode");
452 lswitch_destroy(r->lswitch);
459 VLOG_WARN("Could not connect to controller for %d seconds, "
460 "failing open", disconnected_duration);
461 r->lswitch = lswitch_create(local, true, max_idle);
465 lswitch_process_packet(r->lswitch, local, msg);
471 parse_options(int argc, char *argv[])
473 enum { OPT_MAX_IDLE = UCHAR_MAX + 1 };
474 static struct option long_options[] = {
475 {"fail", required_argument, 0, 'f'},
476 {"fail-open-delay", required_argument, 0, 'd'},
477 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
478 {"listen", required_argument, 0, 'l'},
479 {"detach", no_argument, 0, 'D'},
480 {"pidfile", optional_argument, 0, 'P'},
481 {"verbose", optional_argument, 0, 'v'},
482 {"help", no_argument, 0, 'h'},
483 {"version", no_argument, 0, 'V'},
484 VCONN_SSL_LONG_OPTIONS
487 char *short_options = long_options_to_short_options(long_options);
492 c = getopt_long(argc, argv, short_options, long_options, NULL);
499 if (!strcmp(optarg, "open")) {
500 fail_mode = FAIL_OPEN;
501 } else if (!strcmp(optarg, "closed")) {
502 fail_mode = FAIL_CLOSED;
505 "-f or --fail argument must be \"open\" or \"closed\"");
510 fail_open_delay = atoi(optarg);
511 if (fail_open_delay < 1) {
513 "-d or --fail-open-delay argument must be at least 1");
518 if (!strcmp(optarg, "permanent")) {
519 max_idle = OFP_FLOW_PERMANENT;
521 max_idle = atoi(optarg);
522 if (max_idle < 1 || max_idle > 65535) {
523 fatal(0, "--max-idle argument must be between 1 and "
524 "65535 or the word 'permanent'");
534 set_pidfile(optarg ? optarg : "secchan.pid");
538 if (listen_vconn_name) {
539 fatal(0, "-l or --listen may be only specified once");
541 listen_vconn_name = optarg;
548 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
552 vlog_set_verbosity(optarg);
555 VCONN_SSL_OPTION_HANDLERS
570 printf("%s: secure channel, a relay for OpenFlow messages.\n"
571 "usage: %s [OPTIONS] nl:DP_IDX CONTROLLER\n"
572 "where nl:DP_IDX is a datapath that has been added with dpctl\n"
573 "and CONTROLLER is an active OpenFlow connection method.\n",
574 program_name, program_name);
575 vconn_usage(true, true);
576 printf("\nNetworking options:\n"
577 " -f, --fail=open|closed when controller connection fails:\n"
578 " closed: drop all packets\n"
579 " open (default): act as learning switch\n"
580 " -d, --fail-open-delay=SECS number of seconds after which to\n"
581 " fail open if --fail=open (default: 30)\n"
582 " --max-idle=SECS max idle for flows set up by secchan\n"
583 " -l, --listen=METHOD allow management connections on METHOD\n"
584 " (a passive OpenFlow connection method)\n"
586 " -D, --detach run in background as daemon\n"
587 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
588 " -v, --verbose=MODULE:FACILITY:LEVEL configure logging levels\n"
589 " -v, --verbose set maximum verbosity level\n"
590 " -h, --help display this help message\n"
591 " -V, --version display version information\n",