rconn: Add new max_backoff argument to rconn_new().
[sliver-openvswitch.git] / secchan / secchan.c
1  /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include <errno.h>
35 #include <getopt.h>
36 #include <inttypes.h>
37 #include <netinet/in.h>
38 #include <poll.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43
44 #include "buffer.h"
45 #include "command-line.h"
46 #include "compiler.h"
47 #include "daemon.h"
48 #include "fault.h"
49 #include "flow.h"
50 #include "learning-switch.h"
51 #include "list.h"
52 #include "mac-learning.h"
53 #include "netdev.h"
54 #include "openflow.h"
55 #include "packets.h"
56 #include "poll-loop.h"
57 #include "rconn.h"
58 #include "util.h"
59 #include "vconn-ssl.h"
60 #include "vconn.h"
61 #include "vlog-socket.h"
62
63 #include "vlog.h"
64 #define THIS_MODULE VLM_secchan
65
66 #include "ofp-print.h"
67
68 static const char *listen_vconn_name;
69
70 struct half {
71     struct rconn *rconn;
72     struct buffer *rxbuf;
73 };
74
75 /* Behavior when the connection to the controller fails. */
76 enum fail_mode {
77     FAIL_OPEN,                  /* Act as learning switch. */
78     FAIL_CLOSED                 /* Drop all packets. */
79 };
80
81 struct relay {
82     struct list node;
83
84 #define HALF_LOCAL 0
85 #define HALF_REMOTE 1
86     struct half halves[2];
87
88     bool is_mgmt_conn;
89     struct lswitch *lswitch;
90 };
91
92 static struct list relays = LIST_INITIALIZER(&relays);
93
94 /* Enable the local port? */
95 static int local_port;
96
97 /* MAC address of local port. */
98 static uint8_t local_mac[ETH_ADDR_LEN];
99
100 /* MAC learning table for local port. */
101 static struct mac_learning *local_ml;
102
103 /* -f, --fail: Behavior when the connection to the controller fails. */
104 static enum fail_mode fail_mode = FAIL_OPEN;
105
106 /* The OpenFlow virtual network device ofX. */
107 static struct netdev *of_device;
108
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;
112
113 /* --max-idle: Idle time to assign to flows created by learning switch when in
114  * fail-open mode. */
115 static int max_idle = 15;
116
117 static void parse_options(int argc, char *argv[]);
118 static void usage(void) NO_RETURN;
119
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,
122                                   bool is_mgmt_conn);
123 static void relay_run(struct relay *);
124 static void relay_wait(struct relay *);
125 static void relay_destroy(struct relay *);
126
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);
130
131 int
132 main(int argc, char *argv[])
133 {
134     struct vconn *listen_vconn;
135     struct relay *controller_relay;
136     const char *nl_name;
137     char of_name[16];
138     int retval;
139
140     set_program_name(argv[0]);
141     register_fault_handlers();
142     vlog_init();
143     parse_options(argc, argv);
144
145     if (argc - optind != 2) {
146         fatal(0,
147               "need exactly two non-option arguments; use --help for usage");
148     }
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);
154     }
155
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);
160         }
161         if (!vconn_is_passive(listen_vconn)) {
162             fatal(0, "%s is not a passive vconn", listen_vconn_name);
163         }
164     } else {
165         listen_vconn = NULL;
166     }
167
168     snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
169     retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
170     if (!retval) {
171         enum netdev_flags flags;
172         retval = netdev_get_flags(of_device, &flags);
173         if (!retval) {
174             if (flags & NETDEV_UP) {
175                 struct in6_addr in6;
176
177                 local_port = true;
178                 memcpy(local_mac, netdev_get_etheraddr(of_device),
179                        ETH_ADDR_LEN);
180                 if (netdev_get_in6(of_device, &in6)) {
181                     VLOG_WARN("Ignoring IPv6 address on %s device: "
182                               "IPv6 not supported", of_name);
183                 }
184                 local_ml = mac_learning_create();
185             }
186         } else {
187             error(retval, "Could not get flags for %s device", of_name);
188         }
189     } else {
190         error(retval, "Could not open %s device", of_name);
191     }
192
193     retval = vlog_server_listen(NULL, NULL);
194     if (retval) {
195         fatal(retval, "Could not listen for vlog connections");
196     }
197
198     daemonize();
199
200     controller_relay = relay_create(rconn_new(argv[optind], 1, 0, 0),
201                                     rconn_new(argv[optind + 1], 1,
202                                               probe_interval, 0),
203                                     false);
204     for (;;) {
205         struct relay *r, *n;
206
207         /* Do work. */
208         LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
209             relay_run(r);
210         }
211         if (listen_vconn) {
212             for (;;) {
213                 struct vconn *new_remote;
214                 retval = vconn_accept(listen_vconn, &new_remote);
215                 if (retval) {
216                     if (retval != EAGAIN) {
217                         VLOG_WARN("accept failed (%s)", strerror(retval));
218                     }
219                     break;
220                 }
221                 new_management_connection(nl_name, new_remote);
222             }
223         }
224         failing_open(controller_relay);
225
226         /* Wait for something to happen. */
227         LIST_FOR_EACH (r, struct relay, node, &relays) {
228             relay_wait(r);
229         }
230         if (listen_vconn) {
231             vconn_accept_wait(listen_vconn);
232         }
233         poll_block();
234     }
235
236     return 0;
237 }
238
239 static void
240 new_management_connection(const char *nl_name, struct vconn *new_remote)
241 {
242     char *nl_name_without_subscription;
243     struct vconn *new_local;
244     struct rconn *r1, *r2;
245     int retval;
246
247     /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.  We
248      * only accept the former syntax in main().
249      *
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
252      * messages.*/
253     nl_name_without_subscription = xasprintf("%s:0", nl_name);
254     retval = vconn_open(nl_name_without_subscription, &new_local);
255     if (retval) {
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);
260         return;
261     }
262
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);
267
268     free(nl_name_without_subscription);
269 }
270
271 static struct relay *
272 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
273 {
274     struct relay *r;
275     int i;
276
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;
282     }
283     r->is_mgmt_conn = is_mgmt_conn;
284     r->lswitch = NULL;
285     list_push_back(&relays, &r->node);
286     return r;
287 }
288
289 static void
290 relay_run(struct relay *r)
291 {
292     int iteration;
293     int i;
294
295     for (i = 0; i < 2; i++) {
296         rconn_run(r->halves[i].rconn);
297     }
298
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];
305
306             if (!this->rxbuf) {
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);
311                     this->rxbuf = NULL;
312                 }
313             }
314
315             if (this->rxbuf) {
316                 int retval = rconn_send(peer->rconn, this->rxbuf);
317                 if (retval != EAGAIN) {
318                     if (!retval) {
319                         progress = true;
320                     } else {
321                         buffer_delete(this->rxbuf);
322                     }
323                     this->rxbuf = NULL;
324                 }
325             }
326         }
327         if (!progress) {
328             break;
329         }
330     }
331
332     for (i = 0; i < 2; i++) {
333         struct half *this = &r->halves[i];
334         if (!rconn_is_alive(this->rconn)) {
335             relay_destroy(r);
336             return;
337         }
338     }
339 }
340
341 static void
342 relay_wait(struct relay *r)
343 {
344     int i;
345
346     for (i = 0; i < 2; i++) {
347         struct half *this = &r->halves[i];
348
349         rconn_run_wait(this->rconn);
350         if (!this->rxbuf) {
351             rconn_recv_wait(this->rconn);
352         }
353     }
354 }
355
356 static void
357 relay_destroy(struct relay *r)
358 {
359     int i;
360
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);
366     }
367     free(r);
368 }
369
370 static bool
371 is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
372                   struct rconn *controller) 
373 {
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;
377
378     uint32_t last_ip = ip;
379
380     time_t now = time(0);
381
382     ip = rconn_get_ip(controller);
383     if (last_ip != ip || !next_refresh || now >= next_refresh) {
384         bool have_mac;
385
386         /* Look up MAC address. */
387         memset(mac, 0, sizeof mac);
388         if (ip) {
389             int retval = netdev_arp_lookup(of_device, ip, mac);
390             if (retval) {
391                 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
392                          IP_ARGS(&ip), strerror(retval));
393             }
394         }
395         have_mac = !eth_addr_is_zero(mac);
396
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;
402         }
403         if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
404             VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
405                      ETH_ADDR_FMT,
406                      ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
407             memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
408         }
409
410         /* Schedule next refresh.
411          *
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);
416     }
417     return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
418 }
419
420 static bool
421 local_hook(struct relay *r)
422 {
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;
429     struct flow flow;
430     uint16_t in_port, out_port;
431
432     if (!local_port) {
433         return false;
434     }
435
436     oh = msg->data;
437     if (oh->type != OFPT_PACKET_IN) {
438         return false;
439     }
440     if (msg->size < offsetof (struct ofp_packet_in, data)) {
441         VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
442         return false;
443     }
444
445     /* Extract flow data from 'opi' into 'flow'. */
446     opi = msg->data;
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;
451     pkt.size = pkt_len;
452     flow_extract(&pkt, in_port, &flow);
453
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);
462         }
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;
468     } else {
469         return false;
470     }
471
472     /* Add new flow. */
473     if (out_port != OFPP_FLOOD) {
474         b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
475                                  max_idle);
476         if (rconn_force_send(rc, b)) {
477             buffer_delete(b);
478         }
479     }
480
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)) {
485             buffer_delete(b);
486         }
487     }
488     return true;
489 }
490
491 /* Causess 'r' to enter or leave fail-open mode, if appropriate.  Returns true
492  * if 'r' is in fail-open fail, false otherwise. */
493 static bool
494 failing_open(struct relay *r)
495 {
496     struct rconn *local = r->halves[HALF_LOCAL].rconn;
497     struct rconn *remote = r->halves[HALF_REMOTE].rconn;
498     int disconnected_duration;
499
500     if (fail_mode == FAIL_CLOSED) {
501         /* We fail closed, so there's never anything to do. */
502         return false;
503     }
504
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);
512             r->lswitch = NULL;
513         }
514         return false;
515     }
516
517     if (!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);
521     }
522     return true;
523 }
524
525 static bool
526 fail_open_hook(struct relay *r)
527 {
528     if (!failing_open(r)) {
529         return false;
530     } else {
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);
534         rconn_run(local);
535         return true;
536     }
537 }
538
539 static void
540 parse_options(int argc, char *argv[]) 
541 {
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
554         {0, 0, 0, 0},
555     };
556     char *short_options = long_options_to_short_options(long_options);
557     
558     for (;;) {
559         int c;
560
561         c = getopt_long(argc, argv, short_options, long_options, NULL);
562         if (c == -1) {
563             break;
564         }
565
566         switch (c) {
567         case 'f':
568             if (!strcmp(optarg, "open")) {
569                 fail_mode = FAIL_OPEN;
570             } else if (!strcmp(optarg, "closed")) {
571                 fail_mode = FAIL_CLOSED;
572             } else {
573                 fatal(0,
574                       "-f or --fail argument must be \"open\" or \"closed\"");
575             }
576             break;
577
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");
582             }
583             break;
584
585         case OPT_MAX_IDLE:
586             if (!strcmp(optarg, "permanent")) {
587                 max_idle = OFP_FLOW_PERMANENT;
588             } else {
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'");
593                 }
594             }
595             break;
596
597         case 'D':
598             set_detach();
599             break;
600
601         case 'P':
602             set_pidfile(optarg ? optarg : "secchan.pid");
603             break;
604
605         case 'l':
606             if (listen_vconn_name) {
607                 fatal(0, "-l or --listen may be only specified once");
608             }
609             listen_vconn_name = optarg;
610             break;
611
612         case 'h':
613             usage();
614
615         case 'V':
616             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
617             exit(EXIT_SUCCESS);
618
619         case 'v':
620             vlog_set_verbosity(optarg);
621             break;
622
623         VCONN_SSL_OPTION_HANDLERS
624
625         case '?':
626             exit(EXIT_FAILURE);
627
628         default:
629             abort();
630         }
631     }
632     free(short_options);
633 }
634
635 static void
636 usage(void)
637 {
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"
652            "\nOther options:\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",
659            RUNDIR);
660     exit(EXIT_SUCCESS);
661 }