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