cc9e3bc4f8d61652cb3ddeee9d6b3f11f92eab71
[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 <config.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <inttypes.h>
38 #include <netinet/in.h>
39 #include <poll.h>
40 #include <regex.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "buffer.h"
47 #include "command-line.h"
48 #include "compiler.h"
49 #include "daemon.h"
50 #include "dhcp.h"
51 #include "dhcp-client.h"
52 #include "fault.h"
53 #include "flow.h"
54 #include "learning-switch.h"
55 #include "list.h"
56 #include "mac-learning.h"
57 #include "netdev.h"
58 #include "openflow.h"
59 #include "packets.h"
60 #include "poll-loop.h"
61 #include "rconn.h"
62 #include "util.h"
63 #include "vconn-ssl.h"
64 #include "vconn.h"
65 #include "vlog-socket.h"
66
67 #include "vlog.h"
68 #define THIS_MODULE VLM_secchan
69
70 #include "ofp-print.h"
71
72 static const char *listen_vconn_name;
73
74 struct half {
75     struct rconn *rconn;
76     struct buffer *rxbuf;
77 };
78
79 /* Behavior when the connection to the controller fails. */
80 enum fail_mode {
81     FAIL_OPEN,                  /* Act as learning switch. */
82     FAIL_CLOSED                 /* Drop all packets. */
83 };
84
85 struct relay {
86     struct list node;
87
88 #define HALF_LOCAL 0
89 #define HALF_REMOTE 1
90     struct half halves[2];
91
92     bool is_mgmt_conn;
93     struct lswitch *lswitch;
94 };
95
96 static struct list relays = LIST_INITIALIZER(&relays);
97
98 /* Mode of operation.  Note that autodiscovery implies in-band
99  * communication. */
100 static bool autodiscovery;      /* Discover the controller automatically? */
101 static bool in_band;            /* Connect to controller in-band? */
102
103 /* MAC address of local port. */
104 static uint8_t local_mac[ETH_ADDR_LEN];
105
106 /* MAC learning table for local port. */
107 static struct mac_learning *local_ml;
108
109 /* Controller vconn name, or null to perform controller autodiscovery. */
110 static char *controller_name = NULL;
111
112 /* -f, --fail: Behavior when the connection to the controller fails. */
113 static enum fail_mode fail_mode = FAIL_OPEN;
114
115 /* The OpenFlow virtual network device ofX. */
116 static struct netdev *of_device;
117
118 /* --inactivity-probe: Number of seconds without receiving a message from the
119    controller before sending an inactivity probe. */
120 static int probe_interval = 15;
121
122 /* --max-idle: Idle time to assign to flows created by learning switch when in
123  * fail-open mode. */
124 static int max_idle = 15;
125
126 /* --max-backoff: Maximum interval between controller connection attempts, in
127  * seconds. */
128 static int max_backoff = 15;
129
130 /* DHCP client, for controller autodiscovery. */
131 static struct dhclient *dhcp;
132
133 /* --accept-vconn: Regular expression specifying the class of controller vconns
134  * that we will accept during autodiscovery. */
135 static const char *accept_controller_re;
136 static regex_t accept_controller_regex;
137
138 static void parse_options(int argc, char *argv[]);
139 static void usage(void) NO_RETURN;
140
141 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
142 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
143                                   bool is_mgmt_conn);
144 static void relay_run(struct relay *);
145 static void relay_wait(struct relay *);
146 static void relay_destroy(struct relay *);
147
148 static bool local_hook(struct relay *r);
149 static bool failing_open(struct relay *r);
150 static bool fail_open_hook(struct relay *r);
151
152 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
153 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
154
155 int
156 main(int argc, char *argv[])
157 {
158     struct rconn *local_rconn, *remote_rconn;
159     struct vconn *listen_vconn;
160     struct relay *controller_relay;
161     const char *nl_name;
162     char of_name[16];
163     int retval;
164
165     set_program_name(argv[0]);
166     register_fault_handlers();
167     vlog_init();
168     parse_options(argc, argv);
169
170     argc -= optind;
171     argv += optind;
172     if (argc < 1 || argc > 2) {
173         fatal(0, "need one or two non-option arguments; use --help for usage");
174     }
175     nl_name = argv[0];
176     if (strncmp(nl_name, "nl:", 3)
177         || strlen(nl_name) < 4
178         || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
179         fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
180     }
181     controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
182     autodiscovery = controller_name == NULL;
183
184     if (!accept_controller_re) {
185         accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
186     }
187     retval = regcomp(&accept_controller_regex, accept_controller_re,
188                      REG_NOSUB | REG_EXTENDED);
189     if (retval) {
190         size_t length = regerror(retval, &accept_controller_regex, NULL, 0);
191         char *buffer = xmalloc(length);
192         regerror(retval, &accept_controller_regex, buffer, length);
193         fatal(0, "%s: %s", accept_controller_re, buffer);
194     }
195
196     if (listen_vconn_name) {
197         retval = vconn_open(listen_vconn_name, &listen_vconn);
198         if (retval && retval != EAGAIN) {
199             fatal(retval, "opening %s", listen_vconn_name);
200         }
201         if (!vconn_is_passive(listen_vconn)) {
202             fatal(0, "%s is not a passive vconn", listen_vconn_name);
203         }
204     } else {
205         listen_vconn = NULL;
206     }
207
208     snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
209     retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
210     if (!retval) {
211         enum netdev_flags flags;
212
213         if (autodiscovery) {
214             retval = netdev_turn_flags_on(of_device, NETDEV_UP, true);
215             if (retval) {
216                 fatal(retval, "Could not bring %s device up", of_name);
217             }
218         }
219
220         retval = netdev_get_flags(of_device, &flags);
221         if (!retval) {
222             if (flags & NETDEV_UP) {
223                 struct in6_addr in6;
224
225                 in_band = true;
226                 memcpy(local_mac, netdev_get_etheraddr(of_device),
227                        ETH_ADDR_LEN);
228                 if (netdev_get_in6(of_device, &in6)) {
229                     VLOG_WARN("Ignoring IPv6 address on %s device: "
230                               "IPv6 not supported", of_name);
231                 }
232                 local_ml = mac_learning_create();
233             } 
234         } else {
235             error(retval, "Could not get flags for %s device", of_name);
236         }
237     } else {
238         error(retval, "Could not open %s device", of_name);
239     }
240     if (autodiscovery && !in_band) {
241         fatal(retval, "In autodiscovery mode but failed to configure "
242               "in-band control");
243     }
244
245     if (autodiscovery) {
246         retval = dhclient_create(of_name, modify_dhcp_request,
247                                  validate_dhcp_offer, NULL, &dhcp);
248         if (retval) {
249             fatal(retval, "Failed to initialize DHCP client");
250         }
251         dhclient_init(dhcp, 0);
252     }
253
254     retval = vlog_server_listen(NULL, NULL);
255     if (retval) {
256         fatal(retval, "Could not listen for vlog connections");
257     }
258
259     daemonize();
260
261     local_rconn = rconn_create(1, 0, max_backoff);
262     rconn_connect(local_rconn, nl_name);
263
264     remote_rconn = rconn_create(1, probe_interval, max_backoff);
265     if (controller_name) {
266         retval = rconn_connect(remote_rconn, controller_name);
267         if (retval == EAFNOSUPPORT) {
268             fatal(0, "No support for %s vconn", controller_name);
269         }
270     }
271     controller_relay = relay_create(local_rconn, remote_rconn, false);
272     for (;;) {
273         struct relay *r, *n;
274
275         /* Do work. */
276         LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
277             relay_run(r);
278         }
279         if (listen_vconn) {
280             for (;;) {
281                 struct vconn *new_remote;
282                 retval = vconn_accept(listen_vconn, &new_remote);
283                 if (retval) {
284                     if (retval != EAGAIN) {
285                         VLOG_WARN("accept failed (%s)", strerror(retval));
286                     }
287                     break;
288                 }
289                 new_management_connection(nl_name, new_remote);
290             }
291         }
292         if (controller_relay) {
293             /* FIXME: should also fail open when controller_relay is NULL. */
294             failing_open(controller_relay); 
295         }
296         if (dhcp) {
297             if (rconn_is_connectivity_questionable(remote_rconn)) {
298                 dhclient_force_renew(dhcp, 15);
299             }
300             dhclient_run(dhcp);
301             if (dhclient_changed(dhcp)) {
302                 free(controller_name);
303                 if (dhclient_is_bound(dhcp)) {
304                     controller_name = dhcp_msg_get_string(
305                         dhclient_get_config(dhcp),
306                         DHCP_CODE_OFP_CONTROLLER_VCONN);
307                     VLOG_WARN("%s: discovered controller",
308                               controller_name);
309                     rconn_connect(remote_rconn, controller_name);
310                 } else if (controller_name) {
311                     VLOG_WARN("%s: discover controller no longer available",
312                               controller_name);
313                     controller_name = NULL;
314                     rconn_disconnect(remote_rconn);
315                 }
316             }
317         }
318
319         /* Wait for something to happen. */
320         LIST_FOR_EACH (r, struct relay, node, &relays) {
321             relay_wait(r);
322         }
323         if (listen_vconn) {
324             vconn_accept_wait(listen_vconn);
325         }
326         if (dhcp) {
327             dhclient_wait(dhcp);
328         }
329         poll_block();
330     }
331
332     return 0;
333 }
334
335 static void
336 new_management_connection(const char *nl_name, struct vconn *new_remote)
337 {
338     char *nl_name_without_subscription;
339     struct vconn *new_local;
340     struct rconn *r1, *r2;
341     int retval;
342
343     /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.  We
344      * only accept the former syntax in main().
345      *
346      * nl:123:0 opens a netlink connection to local datapath 123 without
347      * obtaining a subscription for ofp_packet_in or ofp_flow_expired
348      * messages.*/
349     nl_name_without_subscription = xasprintf("%s:0", nl_name);
350     retval = vconn_open(nl_name_without_subscription, &new_local);
351     if (retval) {
352         VLOG_ERR("could not connect to %s (%s)",
353                  nl_name_without_subscription, strerror(retval));
354         vconn_close(new_remote);
355         free(nl_name_without_subscription);
356         return;
357     }
358
359     /* Add it to the relay list. */
360     r1 = rconn_create(1, 0, 0);
361     rconn_connect_unreliably(r1, nl_name_without_subscription, new_local);
362     r2 = rconn_create(1, 0, 0);
363     rconn_connect_unreliably(r2, "passive", new_remote);
364     relay_create(r1, r2, true);
365
366     free(nl_name_without_subscription);
367 }
368
369 static struct relay *
370 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
371 {
372     struct relay *r;
373     int i;
374
375     r = xmalloc(sizeof *r);
376     r->halves[HALF_LOCAL].rconn = local;
377     r->halves[HALF_REMOTE].rconn = remote;
378     for (i = 0; i < 2; i++) {
379         r->halves[i].rxbuf = NULL;
380     }
381     r->is_mgmt_conn = is_mgmt_conn;
382     r->lswitch = NULL;
383     list_push_back(&relays, &r->node);
384     return r;
385 }
386
387 static void
388 relay_run(struct relay *r)
389 {
390     int iteration;
391     int i;
392
393     for (i = 0; i < 2; i++) {
394         rconn_run(r->halves[i].rconn);
395     }
396
397     /* Limit the number of iterations to prevent other tasks from starving. */
398     for (iteration = 0; iteration < 50; iteration++) {
399         bool progress = false;
400         for (i = 0; i < 2; i++) {
401             struct half *this = &r->halves[i];
402             struct half *peer = &r->halves[!i];
403
404             if (!this->rxbuf) {
405                 this->rxbuf = rconn_recv(this->rconn);
406                 if (this->rxbuf && !r->is_mgmt_conn && i == HALF_LOCAL
407                     && (local_hook(r) || fail_open_hook(r))) {
408                     buffer_delete(this->rxbuf);
409                     this->rxbuf = NULL;
410                 }
411             }
412
413             if (this->rxbuf) {
414                 int retval = rconn_send(peer->rconn, this->rxbuf);
415                 if (retval != EAGAIN) {
416                     if (!retval) {
417                         progress = true;
418                     } else {
419                         buffer_delete(this->rxbuf);
420                     }
421                     this->rxbuf = NULL;
422                 }
423             }
424         }
425         if (!progress) {
426             break;
427         }
428     }
429
430     if (r->is_mgmt_conn) {
431         for (i = 0; i < 2; i++) {
432             struct half *this = &r->halves[i];
433             if (!rconn_is_alive(this->rconn)) {
434                 relay_destroy(r);
435                 return;
436             }
437         }
438     }
439 }
440
441 static void
442 relay_wait(struct relay *r)
443 {
444     int i;
445
446     for (i = 0; i < 2; i++) {
447         struct half *this = &r->halves[i];
448
449         rconn_run_wait(this->rconn);
450         if (!this->rxbuf) {
451             rconn_recv_wait(this->rconn);
452         }
453     }
454 }
455
456 static void
457 relay_destroy(struct relay *r)
458 {
459     int i;
460
461     list_remove(&r->node);
462     for (i = 0; i < 2; i++) {
463         struct half *this = &r->halves[i];
464         rconn_destroy(this->rconn);
465         buffer_delete(this->rxbuf);
466     }
467     free(r);
468 }
469
470 static void
471 queue_tx(struct rconn *rc, struct buffer *b)
472 {
473     if (rconn_force_send(rc, b)) {
474         buffer_delete(b);
475     }
476 }
477
478 static bool
479 is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
480                   struct rconn *controller) 
481 {
482     static uint32_t ip, last_nonzero_ip;
483     static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
484     static time_t next_refresh = 0;
485
486     uint32_t last_ip = ip;
487
488     time_t now = time(0);
489
490     ip = rconn_get_ip(controller);
491     if (last_ip != ip || !next_refresh || now >= next_refresh) {
492         bool have_mac;
493
494         /* Look up MAC address. */
495         memset(mac, 0, sizeof mac);
496         if (ip) {
497             int retval = netdev_arp_lookup(of_device, ip, mac);
498             if (retval) {
499                 VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
500                          IP_ARGS(&ip), strerror(retval));
501             }
502         }
503         have_mac = !eth_addr_is_zero(mac);
504
505         /* Log changes in IP, MAC addresses. */
506         if (ip && ip != last_nonzero_ip) {
507             VLOG_DBG("controller IP address changed from "IP_FMT
508                      " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
509             last_nonzero_ip = ip;
510         }
511         if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
512             VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
513                      ETH_ADDR_FMT,
514                      ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
515             memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
516         }
517
518         /* Schedule next refresh.
519          *
520          * If we have an IP address but not a MAC address, then refresh
521          * quickly, since we probably will get a MAC address soon (via ARP).
522          * Otherwise, we can afford to wait a little while. */
523         next_refresh = now + (!ip || have_mac ? 10 : 1);
524     }
525     return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
526 }
527
528 static bool
529 local_hook(struct relay *r)
530 {
531     struct rconn *rc = r->halves[HALF_LOCAL].rconn;
532     struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
533     struct ofp_packet_in *opi;
534     struct ofp_header *oh;
535     size_t pkt_ofs, pkt_len;
536     struct buffer pkt;
537     struct flow flow;
538     uint16_t in_port, out_port;
539
540     if (!in_band) {
541         return false;
542     }
543
544     oh = msg->data;
545     if (oh->type != OFPT_PACKET_IN) {
546         return false;
547     }
548     if (msg->size < offsetof (struct ofp_packet_in, data)) {
549         VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
550         return false;
551     }
552
553     /* Extract flow data from 'opi' into 'flow'. */
554     opi = msg->data;
555     in_port = ntohs(opi->in_port);
556     pkt_ofs = offsetof(struct ofp_packet_in, data);
557     pkt_len = ntohs(opi->header.length) - pkt_ofs;
558     pkt.data = opi->data;
559     pkt.size = pkt_len;
560     flow_extract(&pkt, in_port, &flow);
561
562     /* Deal with local stuff. */
563     if (in_port == OFPP_LOCAL) {
564         out_port = mac_learning_lookup(local_ml, flow.dl_dst);
565     } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
566         out_port = OFPP_LOCAL;
567         if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
568             VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
569                      ETH_ADDR_ARGS(flow.dl_src), in_port);
570         }
571     } else if (flow.dl_type == htons(ETH_TYPE_ARP)
572                && eth_addr_is_broadcast(flow.dl_dst)
573                && is_controller_mac(flow.dl_src,
574                                     r->halves[HALF_REMOTE].rconn)) {
575         out_port = OFPP_FLOOD;
576     } else {
577         return false;
578     }
579
580     if (out_port != OFPP_FLOOD) {
581         /* The output port is known, so add a new flow. */
582         queue_tx(rc, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
583                                           out_port, max_idle));
584
585         /* If the switch didn't buffer the packet, we need to send a copy. */
586         if (ntohl(opi->buffer_id) == UINT32_MAX) {
587             queue_tx(rc, make_unbuffered_packet_out(&pkt, in_port, out_port));
588         }
589     } else {
590         /* We don't know that MAC.  Send along the packet without setting up a
591          * flow. */
592         struct buffer *b;
593         if (ntohl(opi->buffer_id) == UINT32_MAX) {
594             b = make_unbuffered_packet_out(&pkt, in_port, out_port);
595         } else {
596             b = make_buffered_packet_out(ntohl(opi->buffer_id),
597                                          in_port, out_port);
598         }
599         queue_tx(rc, b);
600     }
601     return true;
602 }
603
604 /* Causess 'r' to enter or leave fail-open mode, if appropriate.  Returns true
605  * if 'r' is in fail-open fail, false otherwise. */
606 static bool
607 failing_open(struct relay *r)
608 {
609     struct rconn *local = r->halves[HALF_LOCAL].rconn;
610     struct rconn *remote = r->halves[HALF_REMOTE].rconn;
611     int disconnected_duration;
612
613     if (fail_mode == FAIL_CLOSED) {
614         /* We fail closed, so there's never anything to do. */
615         return false;
616     }
617
618     disconnected_duration = rconn_disconnected_duration(remote);
619     if (disconnected_duration < probe_interval * 3) {
620         /* It's not time to fail open yet. */
621         if (r->lswitch && rconn_is_connected(remote)) {
622             /* We're connected, so drop the learning switch. */
623             VLOG_WARN("No longer in fail-open mode");
624             lswitch_destroy(r->lswitch);
625             r->lswitch = NULL;
626         }
627         return false;
628     }
629
630     if (!r->lswitch) {
631         VLOG_WARN("Could not connect to controller for %d seconds, "
632                   "failing open", disconnected_duration);
633         r->lswitch = lswitch_create(local, true, max_idle);
634     }
635     return true;
636 }
637
638 static bool
639 fail_open_hook(struct relay *r)
640 {
641     if (!failing_open(r)) {
642         return false;
643     } else {
644         struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
645         struct rconn *local = r->halves[HALF_LOCAL].rconn;
646         lswitch_process_packet(r->lswitch, local, msg);
647         rconn_run(local);
648         return true;
649     }
650 }
651
652 static void
653 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
654 {
655     dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
656 }
657
658 static bool
659 validate_dhcp_offer(const struct dhcp_msg *msg, void *aux)
660 {
661     char *vconn_name;
662     bool accept;
663
664     vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
665     if (!vconn_name) {
666         VLOG_WARN("rejecting DHCP offer missing controller vconn");
667         return false;
668     }
669     accept = !regexec(&accept_controller_regex, vconn_name, 0, NULL, 0);
670     free(vconn_name);
671     return accept;
672 }
673
674 static void
675 parse_options(int argc, char *argv[]) 
676 {
677     enum {
678         OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
679         OPT_INACTIVITY_PROBE,
680         OPT_MAX_IDLE,
681         OPT_MAX_BACKOFF
682     };
683     static struct option long_options[] = {
684         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
685         {"fail",        required_argument, 0, 'f'},
686         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
687         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
688         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
689         {"listen",      required_argument, 0, 'l'},
690         {"detach",      no_argument, 0, 'D'},
691         {"pidfile",     optional_argument, 0, 'P'},
692         {"verbose",     optional_argument, 0, 'v'},
693         {"help",        no_argument, 0, 'h'},
694         {"version",     no_argument, 0, 'V'},
695         VCONN_SSL_LONG_OPTIONS
696         {0, 0, 0, 0},
697     };
698     char *short_options = long_options_to_short_options(long_options);
699     
700     for (;;) {
701         int c;
702
703         c = getopt_long(argc, argv, short_options, long_options, NULL);
704         if (c == -1) {
705             break;
706         }
707
708         switch (c) {
709         case OPT_ACCEPT_VCONN:
710             accept_controller_re = (optarg[0] == '^'
711                                     ? optarg
712                                     : xasprintf("^%s", optarg));
713             break;
714
715         case 'f':
716             if (!strcmp(optarg, "open")) {
717                 fail_mode = FAIL_OPEN;
718             } else if (!strcmp(optarg, "closed")) {
719                 fail_mode = FAIL_CLOSED;
720             } else {
721                 fatal(0,
722                       "-f or --fail argument must be \"open\" or \"closed\"");
723             }
724             break;
725
726         case OPT_INACTIVITY_PROBE:
727             probe_interval = atoi(optarg);
728             if (probe_interval < 5) {
729                 fatal(0, "--inactivity-probe argument must be at least 5");
730             }
731             break;
732
733         case OPT_MAX_IDLE:
734             if (!strcmp(optarg, "permanent")) {
735                 max_idle = OFP_FLOW_PERMANENT;
736             } else {
737                 max_idle = atoi(optarg);
738                 if (max_idle < 1 || max_idle > 65535) {
739                     fatal(0, "--max-idle argument must be between 1 and "
740                           "65535 or the word 'permanent'");
741                 }
742             }
743             break;
744
745         case OPT_MAX_BACKOFF:
746             max_backoff = atoi(optarg);
747             if (max_backoff < 1) {
748                 fatal(0, "--max-backoff argument must be at least 1");
749             } else if (max_backoff > 3600) {
750                 max_backoff = 3600;
751             }
752             break;
753
754         case 'D':
755             set_detach();
756             break;
757
758         case 'P':
759             set_pidfile(optarg ? optarg : "secchan.pid");
760             break;
761
762         case 'l':
763             if (listen_vconn_name) {
764                 fatal(0, "-l or --listen may be only specified once");
765             }
766             listen_vconn_name = optarg;
767             break;
768
769         case 'h':
770             usage();
771
772         case 'V':
773             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
774             exit(EXIT_SUCCESS);
775
776         case 'v':
777             vlog_set_verbosity(optarg);
778             break;
779
780         VCONN_SSL_OPTION_HANDLERS
781
782         case '?':
783             exit(EXIT_FAILURE);
784
785         default:
786             abort();
787         }
788     }
789     free(short_options);
790 }
791
792 static void
793 usage(void)
794 {
795     printf("%s: secure channel, a relay for OpenFlow messages.\n"
796            "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
797            "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
798            "CONTROLLER is an active OpenFlow connection method; if it is\n"
799            "omitted, then secchan performs controller autodiscovery.\n",
800            program_name, program_name);
801     vconn_usage(true, true);
802     printf("\nNetworking options:\n"
803            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
804            "  -f, --fail=open|closed  when controller connection fails:\n"
805            "                            closed: drop all packets\n"
806            "                            open (default): act as learning switch\n"
807            "  --inactivity-probe=SECS time between inactivity probes\n"
808            "  --max-idle=SECS         max idle for flows set up by secchan\n"
809            "  --max-backoff=SECS      max time between controller connection\n"
810            "                          attempts (default: 15 seconds)\n"
811            "  -l, --listen=METHOD     allow management connections on METHOD\n"
812            "                          (a passive OpenFlow connection method)\n"
813            "\nOther options:\n"
814            "  -D, --detach            run in background as daemon\n"
815            "  -P, --pidfile[=FILE]    create pidfile (default: %s/secchan.pid)\n"
816            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
817            "  -v, --verbose           set maximum verbosity level\n"
818            "  -h, --help              display this help message\n"
819            "  -V, --version           display version information\n",
820            RUNDIR);
821     exit(EXIT_SUCCESS);
822 }