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 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_IDX\"", 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, 0),
198 rconn_new(argv[optind + 1], 1, probe_interval),
204 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
209 struct vconn *new_remote;
210 retval = vconn_accept(listen_vconn, &new_remote);
212 if (retval != EAGAIN) {
213 VLOG_WARN("accept failed (%s)", strerror(retval));
217 new_management_connection(nl_name, new_remote);
221 /* Wait for something to happen. */
222 LIST_FOR_EACH (r, struct relay, node, &relays) {
226 vconn_accept_wait(listen_vconn);
235 new_management_connection(const char *nl_name, struct vconn *new_remote)
237 char *nl_name_without_subscription;
238 struct vconn *new_local;
239 struct rconn *r1, *r2;
242 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
243 * only accept the former syntax in main().
245 * nl:123:0 opens a netlink connection to local datapath 123 without
246 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
248 nl_name_without_subscription = xasprintf("%s:0", nl_name);
249 retval = vconn_open(nl_name_without_subscription, &new_local);
251 VLOG_ERR("could not connect to %s (%s)",
252 nl_name_without_subscription, strerror(retval));
253 vconn_close(new_remote);
254 free(nl_name_without_subscription);
258 /* Add it to the relay list. */
259 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
260 r2 = rconn_new_from_vconn("passive", 1, new_remote);
261 relay_create(r1, r2, true);
263 free(nl_name_without_subscription);
266 static struct relay *
267 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
272 r = xmalloc(sizeof *r);
273 r->halves[HALF_LOCAL].rconn = local;
274 r->halves[HALF_REMOTE].rconn = remote;
275 for (i = 0; i < 2; i++) {
276 r->halves[i].rxbuf = NULL;
278 r->is_mgmt_conn = is_mgmt_conn;
280 list_push_back(&relays, &r->node);
285 relay_run(struct relay *r)
290 for (i = 0; i < 2; i++) {
291 rconn_run(r->halves[i].rconn);
294 /* Limit the number of iterations to prevent other tasks from starving. */
295 for (iteration = 0; iteration < 50; iteration++) {
296 bool progress = false;
297 for (i = 0; i < 2; i++) {
298 struct half *this = &r->halves[i];
299 struct half *peer = &r->halves[!i];
302 this->rxbuf = rconn_recv(this->rconn);
303 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
304 && (local_hook(r) || fail_open_hook(r))) {
305 buffer_delete(this->rxbuf);
311 int retval = rconn_send(peer->rconn, this->rxbuf);
312 if (retval != EAGAIN) {
316 buffer_delete(this->rxbuf);
327 for (i = 0; i < 2; i++) {
328 struct half *this = &r->halves[i];
329 if (!rconn_is_alive(this->rconn)) {
337 relay_wait(struct relay *r)
341 for (i = 0; i < 2; i++) {
342 struct half *this = &r->halves[i];
344 rconn_run_wait(this->rconn);
346 rconn_recv_wait(this->rconn);
352 relay_destroy(struct relay *r)
356 list_remove(&r->node);
357 for (i = 0; i < 2; i++) {
358 struct half *this = &r->halves[i];
359 rconn_destroy(this->rconn);
360 buffer_delete(this->rxbuf);
366 local_hook(struct relay *r)
368 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
369 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
370 struct ofp_packet_in *opi;
371 struct ofp_header *oh;
372 size_t pkt_ofs, pkt_len;
373 struct buffer pkt, *b;
375 uint16_t in_port, out_port;
382 if (oh->type != OFPT_PACKET_IN) {
385 if (msg->size < offsetof (struct ofp_packet_in, data)) {
386 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
390 /* Extract flow data from 'opi' into 'flow'. */
392 in_port = ntohs(opi->in_port);
393 pkt_ofs = offsetof(struct ofp_packet_in, data);
394 pkt_len = ntohs(opi->header.length) - pkt_ofs;
395 pkt.data = opi->data;
397 flow_extract(&pkt, in_port, &flow);
399 /* Deal with local stuff. */
400 if (in_port == OFPP_LOCAL) {
401 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
402 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
403 out_port = OFPP_LOCAL;
404 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
405 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
406 ETH_ADDR_ARGS(flow.dl_src), in_port);
413 if (out_port != OFPP_FLOOD) {
414 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
416 if (rconn_force_send(rc, b)) {
421 /* If the switch didn't buffer the packet, we need to send a copy. */
422 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
423 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
424 if (rconn_force_send(rc, b)) {
432 fail_open_hook(struct relay *r)
434 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
435 struct rconn *local = r->halves[HALF_LOCAL].rconn;
436 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
437 int disconnected_duration;
439 if (fail_mode == FAIL_CLOSED) {
440 /* We fail closed, so there's never anything to do. */
444 disconnected_duration = rconn_disconnected_duration(remote);
445 if (disconnected_duration < probe_interval * 3) {
446 /* It's not time to fail open yet. */
447 if (r->lswitch && rconn_is_connected(remote)) {
448 /* We're connected, so drop the learning switch. */
449 VLOG_WARN("No longer in fail-open mode");
450 lswitch_destroy(r->lswitch);
457 VLOG_WARN("Could not connect to controller for %d seconds, "
458 "failing open", disconnected_duration);
459 r->lswitch = lswitch_create(local, true, max_idle);
463 lswitch_process_packet(r->lswitch, local, msg);
469 parse_options(int argc, char *argv[])
471 enum { OPT_INACTIVITY_PROBE = UCHAR_MAX + 1, OPT_MAX_IDLE };
472 static struct option long_options[] = {
473 {"fail", required_argument, 0, 'f'},
474 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
475 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
476 {"listen", required_argument, 0, 'l'},
477 {"detach", no_argument, 0, 'D'},
478 {"pidfile", optional_argument, 0, 'P'},
479 {"verbose", optional_argument, 0, 'v'},
480 {"help", no_argument, 0, 'h'},
481 {"version", no_argument, 0, 'V'},
482 VCONN_SSL_LONG_OPTIONS
485 char *short_options = long_options_to_short_options(long_options);
490 c = getopt_long(argc, argv, short_options, long_options, NULL);
497 if (!strcmp(optarg, "open")) {
498 fail_mode = FAIL_OPEN;
499 } else if (!strcmp(optarg, "closed")) {
500 fail_mode = FAIL_CLOSED;
503 "-f or --fail argument must be \"open\" or \"closed\"");
507 case OPT_INACTIVITY_PROBE:
508 probe_interval = atoi(optarg);
509 if (probe_interval < 5) {
510 fatal(0, "--inactivity-probe argument must be at least 5");
515 if (!strcmp(optarg, "permanent")) {
516 max_idle = OFP_FLOW_PERMANENT;
518 max_idle = atoi(optarg);
519 if (max_idle < 1 || max_idle > 65535) {
520 fatal(0, "--max-idle argument must be between 1 and "
521 "65535 or the word 'permanent'");
531 set_pidfile(optarg ? optarg : "secchan.pid");
535 if (listen_vconn_name) {
536 fatal(0, "-l or --listen may be only specified once");
538 listen_vconn_name = optarg;
545 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
549 vlog_set_verbosity(optarg);
552 VCONN_SSL_OPTION_HANDLERS
567 printf("%s: secure channel, a relay for OpenFlow messages.\n"
568 "usage: %s [OPTIONS] nl:DP_IDX CONTROLLER\n"
569 "where nl:DP_IDX is a datapath that has been added with dpctl\n"
570 "and CONTROLLER is an active OpenFlow connection method.\n",
571 program_name, program_name);
572 vconn_usage(true, true);
573 printf("\nNetworking options:\n"
574 " -f, --fail=open|closed when controller connection fails:\n"
575 " closed: drop all packets\n"
576 " open (default): act as learning switch\n"
577 " --inactivity-probe=SECS time between inactivity probes\n"
578 " --max-idle=SECS max idle for flows set up by secchan\n"
579 " -l, --listen=METHOD allow management connections on METHOD\n"
580 " (a passive OpenFlow connection method)\n"
582 " -D, --detach run in background as daemon\n"
583 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
584 " -v, --verbose=MODULE:FACILITY:LEVEL configure logging levels\n"
585 " -v, --verbose set maximum verbosity level\n"
586 " -h, --help display this help message\n"
587 " -V, --version display version information\n",