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 static void parse_options(int argc, char *argv[]);
111 static void usage(void) NO_RETURN;
113 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
114 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
116 static void relay_run(struct relay *);
117 static void relay_wait(struct relay *);
118 static void relay_destroy(struct relay *);
120 static bool local_hook(struct relay *r);
121 static bool fail_open_hook(struct relay *r);
124 main(int argc, char *argv[])
126 struct vconn *listen_vconn;
127 struct netdev *of_device;
132 set_program_name(argv[0]);
133 register_fault_handlers();
135 parse_options(argc, argv);
137 if (argc - optind != 2) {
139 "need exactly two non-option arguments; use --help for usage");
141 nl_name = argv[optind];
142 if (strncmp(nl_name, "nl:", 3)
143 || strlen(nl_name) < 4
144 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
145 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
148 if (listen_vconn_name) {
149 retval = vconn_open(listen_vconn_name, &listen_vconn);
150 if (retval && retval != EAGAIN) {
151 fatal(retval, "opening %s", listen_vconn_name);
153 if (!vconn_is_passive(listen_vconn)) {
154 fatal(0, "%s is not a passive vconn", listen_vconn_name);
160 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
161 retval = netdev_open(of_name, &of_device);
163 enum netdev_flags flags;
164 retval = netdev_get_flags(of_device, &flags);
166 if (flags & NETDEV_UP) {
170 memcpy(local_mac, netdev_get_etheraddr(of_device),
172 if (netdev_get_in6(of_device, &in6)) {
173 VLOG_WARN("Ignoring IPv6 address on %s device: "
174 "IPv6 not supported", of_name);
176 local_ml = mac_learning_create();
179 error(retval, "Could not get flags for %s device", of_name);
181 netdev_close(of_device);
183 error(retval, "Could not open %s device", of_name);
186 retval = vlog_server_listen(NULL, NULL);
188 fatal(retval, "Could not listen for vlog connections");
193 relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1),
199 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
204 struct vconn *new_remote;
205 retval = vconn_accept(listen_vconn, &new_remote);
207 if (retval != EAGAIN) {
208 VLOG_WARN("accept failed (%s)", strerror(retval));
212 new_management_connection(nl_name, new_remote);
216 /* Wait for something to happen. */
217 LIST_FOR_EACH (r, struct relay, node, &relays) {
221 vconn_accept_wait(listen_vconn);
230 new_management_connection(const char *nl_name, struct vconn *new_remote)
232 char *nl_name_without_subscription;
233 struct vconn *new_local;
234 struct rconn *r1, *r2;
237 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
238 * only accept the former syntax in main().
240 * nl:123:0 opens a netlink connection to local datapath 123 without
241 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
243 nl_name_without_subscription = xasprintf("%s:0", nl_name);
244 retval = vconn_open(nl_name_without_subscription, &new_local);
246 VLOG_ERR("could not connect to %s (%s)",
247 nl_name_without_subscription, strerror(retval));
248 vconn_close(new_remote);
249 free(nl_name_without_subscription);
253 /* Add it to the relay list. */
254 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
255 r2 = rconn_new_from_vconn("passive", 1, new_remote);
256 relay_create(r1, r2, true);
258 free(nl_name_without_subscription);
261 static struct relay *
262 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
267 r = xmalloc(sizeof *r);
268 r->halves[HALF_LOCAL].rconn = local;
269 r->halves[HALF_REMOTE].rconn = remote;
270 for (i = 0; i < 2; i++) {
271 r->halves[i].rxbuf = NULL;
273 r->is_mgmt_conn = is_mgmt_conn;
275 list_push_back(&relays, &r->node);
280 relay_run(struct relay *r)
285 for (i = 0; i < 2; i++) {
286 rconn_run(r->halves[i].rconn);
289 /* Limit the number of iterations to prevent other tasks from starving. */
290 for (iteration = 0; iteration < 50; iteration++) {
291 bool progress = false;
292 for (i = 0; i < 2; i++) {
293 struct half *this = &r->halves[i];
294 struct half *peer = &r->halves[!i];
297 this->rxbuf = rconn_recv(this->rconn);
298 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
299 && (local_hook(r) || fail_open_hook(r))) {
300 buffer_delete(this->rxbuf);
306 int retval = rconn_send(peer->rconn, this->rxbuf);
307 if (retval != EAGAIN) {
311 buffer_delete(this->rxbuf);
322 for (i = 0; i < 2; i++) {
323 struct half *this = &r->halves[i];
324 if (!rconn_is_alive(this->rconn)) {
332 relay_wait(struct relay *r)
336 for (i = 0; i < 2; i++) {
337 struct half *this = &r->halves[i];
339 rconn_run_wait(this->rconn);
341 rconn_recv_wait(this->rconn);
347 relay_destroy(struct relay *r)
351 list_remove(&r->node);
352 for (i = 0; i < 2; i++) {
353 struct half *this = &r->halves[i];
354 rconn_destroy(this->rconn);
355 buffer_delete(this->rxbuf);
361 local_hook(struct relay *r)
363 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
364 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
365 struct ofp_packet_in *opi;
366 struct ofp_header *oh;
367 size_t pkt_ofs, pkt_len;
368 struct buffer pkt, *b;
370 uint16_t in_port, out_port;
377 if (oh->type != OFPT_PACKET_IN) {
380 if (msg->size < offsetof (struct ofp_packet_in, data)) {
381 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
385 /* Extract flow data from 'opi' into 'flow'. */
387 in_port = ntohs(opi->in_port);
388 pkt_ofs = offsetof(struct ofp_packet_in, data);
389 pkt_len = ntohs(opi->header.length) - pkt_ofs;
390 pkt.data = opi->data;
392 flow_extract(&pkt, in_port, &flow);
394 /* Deal with local stuff. */
395 if (!rconn_is_connected(r->halves[HALF_REMOTE].rconn)
396 && eth_addr_is_broadcast(flow.dl_dst)) {
397 out_port = OFPP_FLOOD;
398 } else if (in_port == OFPP_LOCAL) {
399 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
400 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
401 out_port = OFPP_LOCAL;
402 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
403 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
404 ETH_ADDR_ARGS(flow.dl_src), in_port);
411 if (out_port != OFPP_FLOOD) {
412 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port);
413 if (rconn_force_send(rc, b)) {
418 /* If the switch didn't buffer the packet, we need to send a copy. */
419 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
420 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
421 if (rconn_force_send(rc, b)) {
429 fail_open_hook(struct relay *r)
431 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
432 struct rconn *local = r->halves[HALF_LOCAL].rconn;
433 struct rconn *remote = r->halves[HALF_REMOTE].rconn;
434 int disconnected_duration;
436 if (fail_mode == FAIL_CLOSED) {
437 /* We fail closed, so there's never anything to do. */
441 disconnected_duration = rconn_disconnected_duration(remote);
442 if (disconnected_duration < fail_open_delay) {
443 /* It's not time to fail open yet. */
444 if (r->lswitch && rconn_is_connected(remote)) {
445 /* We're connected, so drop the learning switch. */
446 VLOG_WARN("No longer in fail-open mode");
447 lswitch_destroy(r->lswitch);
454 VLOG_WARN("Could not connect to controller for %d seconds, "
455 "failing open", disconnected_duration);
456 r->lswitch = lswitch_create(local, true, true);
460 lswitch_process_packet(r->lswitch, local, msg);
466 parse_options(int argc, char *argv[])
468 static struct option long_options[] = {
469 {"fail", required_argument, 0, 'f'},
470 {"fail-open-delay", required_argument, 0, 'd'},
471 {"listen", required_argument, 0, 'l'},
472 {"detach", no_argument, 0, 'D'},
473 {"pidfile", optional_argument, 0, 'P'},
474 {"verbose", optional_argument, 0, 'v'},
475 {"help", no_argument, 0, 'h'},
476 {"version", no_argument, 0, 'V'},
477 VCONN_SSL_LONG_OPTIONS
480 char *short_options = long_options_to_short_options(long_options);
485 c = getopt_long(argc, argv, short_options, long_options, NULL);
492 if (!strcmp(optarg, "open")) {
493 fail_mode = FAIL_OPEN;
494 } else if (!strcmp(optarg, "closed")) {
495 fail_mode = FAIL_CLOSED;
498 "-f or --fail argument must be \"open\" or \"closed\"");
503 fail_open_delay = atoi(optarg);
504 if (fail_open_delay < 1) {
506 "-d or --fail-open-delay argument must be at least 1");
515 set_pidfile(optarg ? optarg : "secchan.pid");
519 if (listen_vconn_name) {
520 fatal(0, "-l or --listen may be only specified once");
522 listen_vconn_name = optarg;
529 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
533 vlog_set_verbosity(optarg);
536 VCONN_SSL_OPTION_HANDLERS
551 printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
552 "usage: %s [OPTIONS] LOCAL REMOTE\n"
553 "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
554 program_name, program_name);
555 vconn_usage(true, true);
556 printf("\nNetworking options:\n"
557 " -f, --fail=open|closed when controller connection fails:\n"
558 " closed: drop all packets\n"
559 " open (default): act as learning switch\n"
560 " -d, --fail-open-delay=SECS number of seconds after which to\n"
561 " fail open if --fail=open (default: 30)\n"
562 " -l, --listen=METHOD allow management connections on METHOD\n"
563 " (a passive OpenFlow connection method)\n"
565 " -D, --detach run in background as daemon\n"
566 " -P, --pidfile[=FILE] create pidfile (default: %s/secchan.pid)\n"
567 " -v, --verbose set maximum verbosity level\n"
568 " -h, --help display this help message\n"
569 " -V, --version display version information\n",