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