Never free an skb that has been passed to genlmsg_reply().
[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 "secchan.h"
36 #include <assert.h>
37 #include <errno.h>
38 #include <getopt.h>
39 #include <netinet/in.h>
40 #include <stdlib.h>
41 #include <signal.h>
42 #include <string.h>
43
44 #include "command-line.h"
45 #include "compiler.h"
46 #include "daemon.h"
47 #include "dirs.h"
48 #include "discovery.h"
49 #include "executer.h"
50 #include "fail-open.h"
51 #include "fault.h"
52 #include "in-band.h"
53 #include "list.h"
54 #include "ofpbuf.h"
55 #include "openflow/openflow.h"
56 #include "packets.h"
57 #include "port-watcher.h"
58 #include "poll-loop.h"
59 #include "ratelimit.h"
60 #include "rconn.h"
61 #ifdef SUPPORT_SNAT
62 #include "snat.h"
63 #endif
64 #include "stp-secchan.h"
65 #include "status.h"
66 #include "timeval.h"
67 #include "util.h"
68 #include "vconn-ssl.h"
69 #include "vconn.h"
70 #include "vlog-socket.h"
71
72 #include "vlog.h"
73 #define THIS_MODULE VLM_secchan
74
75 struct hook {
76     const struct hook_class *class;
77     void *aux;
78 };
79
80 struct secchan {
81     struct hook *hooks;
82     size_t n_hooks, allocated_hooks;
83 };
84
85 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
86
87 static void parse_options(int argc, char *argv[], struct settings *);
88 static void usage(void) NO_RETURN;
89
90 static struct pvconn *open_passive_vconn(const char *name);
91 static struct vconn *accept_vconn(struct pvconn *pvconn);
92
93 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
94                                   bool is_mgmt_conn);
95 static struct relay *relay_accept(const struct settings *, struct pvconn *);
96 static void relay_run(struct relay *, struct secchan *);
97 static void relay_wait(struct relay *);
98 static void relay_destroy(struct relay *);
99
100 int
101 main(int argc, char *argv[])
102 {
103     struct settings s;
104
105     struct list relays = LIST_INITIALIZER(&relays);
106
107     struct secchan secchan;
108
109     struct pvconn *monitor;
110
111     struct pvconn *listeners[MAX_MGMT];
112     size_t n_listeners;
113
114     struct rconn *local_rconn, *remote_rconn;
115     struct relay *controller_relay;
116     struct discovery *discovery;
117     struct switch_status *switch_status;
118     struct port_watcher *pw;
119     int i;
120     int retval;
121
122     set_program_name(argv[0]);
123     register_fault_handlers();
124     time_init();
125     vlog_init();
126     parse_options(argc, argv, &s);
127     signal(SIGPIPE, SIG_IGN);
128
129     secchan.hooks = NULL;
130     secchan.n_hooks = 0;
131     secchan.allocated_hooks = 0;
132
133     /* Start listening for management and monitoring connections. */
134     n_listeners = 0;
135     for (i = 0; i < s.n_listeners; i++) {
136         listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
137     }
138     monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
139
140     /* Initialize switch status hook. */
141     switch_status_start(&secchan, &s, &switch_status);
142
143     die_if_already_running();
144     daemonize();
145
146     /* Start listening for vlogconf requests. */
147     retval = vlog_server_listen(NULL, NULL);
148     if (retval) {
149         ofp_fatal(retval, "Could not listen for vlog connections");
150     }
151
152     VLOG_WARN("OpenFlow reference implementation version %s", VERSION BUILDNR);
153     VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
154
155     /* Connect to datapath. */
156     if (strncmp(s.dp_name, "nl:", 3) && strncmp(s.dp_name, "unix:", 5)
157         && !s.controller_name) {
158         VLOG_WARN("Controller not specified and datapath is not nl: or "
159                   "unix:.  (Did you forget to specify the datapath?)");
160     }
161     local_rconn = rconn_create(0, s.max_backoff);
162     rconn_connect(local_rconn, s.dp_name);
163     switch_status_register_category(switch_status, "local",
164                                     rconn_status_cb, local_rconn);
165
166     /* Connect to controller. */
167     remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
168     if (s.controller_name) {
169         retval = rconn_connect(remote_rconn, s.controller_name);
170         if (retval == EAFNOSUPPORT) {
171             ofp_fatal(0, "No support for %s vconn", s.controller_name);
172         }
173     }
174     switch_status_register_category(switch_status, "remote",
175                                     rconn_status_cb, remote_rconn);
176
177     /* Start relaying. */
178     controller_relay = relay_create(local_rconn, remote_rconn, false);
179     list_push_back(&relays, &controller_relay->node);
180
181     /* Set up hooks. */
182     port_watcher_start(&secchan, local_rconn, remote_rconn, &pw);
183     discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
184 #ifdef SUPPORT_SNAT
185     snat_start(&secchan, pw);
186 #endif
187     if (s.enable_stp) {
188         stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
189     }
190     if (s.in_band) {
191         in_band_start(&secchan, &s, switch_status, pw, remote_rconn);
192     }
193     if (s.fail_mode == FAIL_OPEN) {
194         fail_open_start(&secchan, &s, switch_status,
195                         local_rconn, remote_rconn);
196     }
197     if (s.rate_limit) {
198         rate_limit_start(&secchan, &s, switch_status,
199                          local_rconn, remote_rconn);
200     }
201     if (s.command_acl[0]) {
202         executer_start(&secchan, &s);
203     }
204
205     for (;;) {
206         struct relay *r, *n;
207         size_t i;
208
209         /* Do work. */
210         LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
211             relay_run(r, &secchan);
212         }
213         for (i = 0; i < n_listeners; i++) {
214             for (;;) {
215                 struct relay *r = relay_accept(&s, listeners[i]);
216                 if (!r) {
217                     break;
218                 }
219                 list_push_back(&relays, &r->node);
220             }
221         }
222         if (monitor) {
223             struct vconn *new = accept_vconn(monitor);
224             if (new) {
225                 rconn_add_monitor(local_rconn, new);
226             }
227         }
228         for (i = 0; i < secchan.n_hooks; i++) {
229             if (secchan.hooks[i].class->periodic_cb) {
230                 secchan.hooks[i].class->periodic_cb(secchan.hooks[i].aux);
231             }
232         }
233         if (s.discovery) {
234             char *controller_name;
235             if (rconn_is_connectivity_questionable(remote_rconn)) {
236                 discovery_question_connectivity(discovery);
237             }
238             if (discovery_run(discovery, &controller_name)) {
239                 if (controller_name) {
240                     rconn_connect(remote_rconn, controller_name);
241                 } else {
242                     rconn_disconnect(remote_rconn);
243                 }
244             }
245         }
246
247         /* Wait for something to happen. */
248         LIST_FOR_EACH (r, struct relay, node, &relays) {
249             relay_wait(r);
250         }
251         for (i = 0; i < n_listeners; i++) {
252             pvconn_wait(listeners[i]);
253         }
254         if (monitor) {
255             pvconn_wait(monitor);
256         }
257         for (i = 0; i < secchan.n_hooks; i++) {
258             if (secchan.hooks[i].class->wait_cb) {
259                 secchan.hooks[i].class->wait_cb(secchan.hooks[i].aux);
260             }
261         }
262         if (discovery) {
263             discovery_wait(discovery);
264         }
265         poll_block();
266     }
267
268     return 0;
269 }
270
271 static struct pvconn *
272 open_passive_vconn(const char *name)
273 {
274     struct pvconn *pvconn;
275     int retval;
276
277     retval = pvconn_open(name, &pvconn);
278     if (retval && retval != EAGAIN) {
279         ofp_fatal(retval, "opening %s", name);
280     }
281     return pvconn;
282 }
283
284 static struct vconn *
285 accept_vconn(struct pvconn *pvconn)
286 {
287     struct vconn *new;
288     int retval;
289
290     retval = pvconn_accept(pvconn, OFP_VERSION, &new);
291     if (retval && retval != EAGAIN) {
292         VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
293     }
294     return new;
295 }
296
297 void
298 add_hook(struct secchan *secchan, const struct hook_class *class, void *aux)
299 {
300     struct hook *hook;
301
302     if (secchan->n_hooks >= secchan->allocated_hooks) {
303         secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
304         secchan->hooks = xrealloc(secchan->hooks,
305                                   (sizeof *secchan->hooks
306                                    * secchan->allocated_hooks));
307     }
308     hook = &secchan->hooks[secchan->n_hooks++];
309     hook->class = class;
310     hook->aux = aux;
311 }
312
313 struct ofp_packet_in *
314 get_ofp_packet_in(struct relay *r)
315 {
316     struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
317     struct ofp_header *oh = msg->data;
318     if (oh->type == OFPT_PACKET_IN) {
319         if (msg->size >= offsetof (struct ofp_packet_in, data)) {
320             return msg->data;
321         } else {
322             VLOG_WARN("packet too short (%zu bytes) for packet_in",
323                       msg->size);
324         }
325     }
326     return NULL;
327 }
328
329 bool
330 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
331                           struct eth_header **ethp)
332 {
333     const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
334     struct ofp_packet_in *opi = get_ofp_packet_in(r);
335     if (opi && ntohs(opi->header.length) >= min_len) {
336         *opip = opi;
337         *ethp = (void *) opi->data;
338         return true;
339     }
340     return false;
341 }
342 \f
343 /* OpenFlow message relaying. */
344
345 static struct relay *
346 relay_accept(const struct settings *s, struct pvconn *pvconn)
347 {
348     struct vconn *new_remote, *new_local;
349     struct rconn *r1, *r2;
350     char *vconn_name;
351     int nl_index;
352     int retval;
353
354     new_remote = accept_vconn(pvconn);
355     if (!new_remote) {
356         return NULL;
357     }
358
359     if (sscanf(s->dp_name, "nl:%d", &nl_index) == 1) {
360         /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
361          * nl:123:0 opens a netlink connection to local datapath 123 without
362          * obtaining a subscription for ofp_packet_in or ofp_flow_expired
363          * messages.  That's what we want here; management connections should
364          * not receive those messages, at least by default. */
365         vconn_name = xasprintf("nl:%d:0", nl_index);
366     } else {
367         /* We don't have a way to specify not to subscribe to those messages
368          * for other transports.  (That's a defect: really this should be in
369          * the OpenFlow protocol, not the Netlink transport). */
370         VLOG_WARN_RL(&rl, "new management connection will receive "
371                      "asynchronous messages");
372         vconn_name = xstrdup(s->dp_name);
373     }
374
375     retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
376     if (retval) {
377         VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
378                     vconn_name, strerror(retval));
379         vconn_close(new_remote);
380         free(vconn_name);
381         return NULL;
382     }
383
384     /* Create and return relay. */
385     r1 = rconn_create(0, 0);
386     rconn_connect_unreliably(r1, vconn_name, new_local);
387     free(vconn_name);
388
389     r2 = rconn_create(0, 0);
390     rconn_connect_unreliably(r2, "passive", new_remote);
391
392     return relay_create(r1, r2, true);
393 }
394
395 static struct relay *
396 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
397 {
398     struct relay *r = xcalloc(1, sizeof *r);
399     r->halves[HALF_LOCAL].rconn = local;
400     r->halves[HALF_REMOTE].rconn = remote;
401     r->is_mgmt_conn = is_mgmt_conn;
402     return r;
403 }
404
405 static bool
406 call_local_packet_cbs(struct secchan *secchan, struct relay *r)
407 {
408     const struct hook *h;
409     for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
410         bool (*cb)(struct relay *, void *aux) = h->class->local_packet_cb;
411         if (cb && (cb)(r, h->aux)) {
412             return true;
413         }
414     }
415     return false;
416 }
417
418 static bool
419 call_remote_packet_cbs(struct secchan *secchan, struct relay *r)
420 {
421     const struct hook *h;
422     for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
423         bool (*cb)(struct relay *, void *aux) = h->class->remote_packet_cb;
424         if (cb && (cb)(r, h->aux)) {
425             return true;
426         }
427     }
428     return false;
429 }
430
431 static void
432 relay_run(struct relay *r, struct secchan *secchan)
433 {
434     int iteration;
435     int i;
436
437     for (i = 0; i < 2; i++) {
438         rconn_run(r->halves[i].rconn);
439     }
440
441     /* Limit the number of iterations to prevent other tasks from starving. */
442     for (iteration = 0; iteration < 50; iteration++) {
443         bool progress = false;
444         for (i = 0; i < 2; i++) {
445             struct half *this = &r->halves[i];
446             struct half *peer = &r->halves[!i];
447
448             if (!this->rxbuf) {
449                 this->rxbuf = rconn_recv(this->rconn);
450                 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
451                     if (i == HALF_LOCAL
452                         ? call_local_packet_cbs(secchan, r)
453                         : call_remote_packet_cbs(secchan, r))
454                     {
455                         ofpbuf_delete(this->rxbuf);
456                         this->rxbuf = NULL;
457                         progress = true;
458                         break;
459                     }
460                 }
461             }
462
463             if (this->rxbuf && !this->n_txq) {
464                 int retval = rconn_send(peer->rconn, this->rxbuf,
465                                         &this->n_txq);
466                 if (retval != EAGAIN) {
467                     if (!retval) {
468                         progress = true;
469                     } else {
470                         ofpbuf_delete(this->rxbuf);
471                     }
472                     this->rxbuf = NULL;
473                 }
474             }
475         }
476         if (!progress) {
477             break;
478         }
479     }
480
481     if (r->is_mgmt_conn) {
482         for (i = 0; i < 2; i++) {
483             struct half *this = &r->halves[i];
484             if (!rconn_is_alive(this->rconn)) {
485                 relay_destroy(r);
486                 return;
487             }
488         }
489     }
490 }
491
492 static void
493 relay_wait(struct relay *r)
494 {
495     int i;
496
497     for (i = 0; i < 2; i++) {
498         struct half *this = &r->halves[i];
499
500         rconn_run_wait(this->rconn);
501         if (!this->rxbuf) {
502             rconn_recv_wait(this->rconn);
503         }
504     }
505 }
506
507 static void
508 relay_destroy(struct relay *r)
509 {
510     int i;
511
512     list_remove(&r->node);
513     for (i = 0; i < 2; i++) {
514         struct half *this = &r->halves[i];
515         rconn_destroy(this->rconn);
516         ofpbuf_delete(this->rxbuf);
517     }
518     free(r);
519 }
520 \f
521 /* User interface. */
522
523 static void
524 parse_options(int argc, char *argv[], struct settings *s)
525 {
526     enum {
527         OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
528         OPT_NO_RESOLV_CONF,
529         OPT_INACTIVITY_PROBE,
530         OPT_MAX_IDLE,
531         OPT_MAX_BACKOFF,
532         OPT_RATE_LIMIT,
533         OPT_BURST_LIMIT,
534         OPT_BOOTSTRAP_CA_CERT,
535         OPT_STP,
536         OPT_NO_STP,
537         OPT_OUT_OF_BAND,
538         OPT_IN_BAND,
539         OPT_COMMAND_ACL,
540         OPT_COMMAND_DIR,
541         VLOG_OPTION_ENUMS
542     };
543     static struct option long_options[] = {
544         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
545         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
546         {"fail",        required_argument, 0, 'F'},
547         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
548         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
549         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
550         {"listen",      required_argument, 0, 'l'},
551         {"monitor",     required_argument, 0, 'm'},
552         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
553         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
554         {"stp",         no_argument, 0, OPT_STP},
555         {"no-stp",      no_argument, 0, OPT_NO_STP},
556         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
557         {"in-band",     no_argument, 0, OPT_IN_BAND},
558         {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
559         {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
560         {"verbose",     optional_argument, 0, 'v'},
561         {"help",        no_argument, 0, 'h'},
562         {"version",     no_argument, 0, 'V'},
563         DAEMON_LONG_OPTIONS,
564         VLOG_LONG_OPTIONS,
565 #ifdef HAVE_OPENSSL
566         VCONN_SSL_LONG_OPTIONS
567         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
568 #endif
569         {0, 0, 0, 0},
570     };
571     char *short_options = long_options_to_short_options(long_options);
572     char *accept_re = NULL;
573     int retval;
574
575     /* Set defaults that we can figure out before parsing options. */
576     s->n_listeners = 0;
577     s->monitor_name = NULL;
578     s->fail_mode = FAIL_OPEN;
579     s->max_idle = 15;
580     s->probe_interval = 15;
581     s->max_backoff = 15;
582     s->update_resolv_conf = true;
583     s->rate_limit = 0;
584     s->burst_limit = 0;
585     s->enable_stp = false;
586     s->in_band = true;
587     s->command_acl = "";
588     s->command_dir = xasprintf("%s/commands", ofp_pkgdatadir);
589     for (;;) {
590         int c;
591
592         c = getopt_long(argc, argv, short_options, long_options, NULL);
593         if (c == -1) {
594             break;
595         }
596
597         switch (c) {
598         case OPT_ACCEPT_VCONN:
599             accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
600             break;
601
602         case OPT_NO_RESOLV_CONF:
603             s->update_resolv_conf = false;
604             break;
605
606         case 'F':
607             if (!strcmp(optarg, "open")) {
608                 s->fail_mode = FAIL_OPEN;
609             } else if (!strcmp(optarg, "closed")) {
610                 s->fail_mode = FAIL_CLOSED;
611             } else {
612                 ofp_fatal(0, "-f or --fail argument must be \"open\" "
613                           "or \"closed\"");
614             }
615             break;
616
617         case OPT_INACTIVITY_PROBE:
618             s->probe_interval = atoi(optarg);
619             if (s->probe_interval < 5) {
620                 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
621             }
622             break;
623
624         case OPT_MAX_IDLE:
625             if (!strcmp(optarg, "permanent")) {
626                 s->max_idle = OFP_FLOW_PERMANENT;
627             } else {
628                 s->max_idle = atoi(optarg);
629                 if (s->max_idle < 1 || s->max_idle > 65535) {
630                     ofp_fatal(0, "--max-idle argument must be between 1 and "
631                               "65535 or the word 'permanent'");
632                 }
633             }
634             break;
635
636         case OPT_MAX_BACKOFF:
637             s->max_backoff = atoi(optarg);
638             if (s->max_backoff < 1) {
639                 ofp_fatal(0, "--max-backoff argument must be at least 1");
640             } else if (s->max_backoff > 3600) {
641                 s->max_backoff = 3600;
642             }
643             break;
644
645         case OPT_RATE_LIMIT:
646             if (optarg) {
647                 s->rate_limit = atoi(optarg);
648                 if (s->rate_limit < 1) {
649                     ofp_fatal(0, "--rate-limit argument must be at least 1");
650                 }
651             } else {
652                 s->rate_limit = 1000;
653             }
654             break;
655
656         case OPT_BURST_LIMIT:
657             s->burst_limit = atoi(optarg);
658             if (s->burst_limit < 1) {
659                 ofp_fatal(0, "--burst-limit argument must be at least 1");
660             }
661             break;
662
663         case OPT_STP:
664             s->enable_stp = true;
665             break;
666
667         case OPT_NO_STP:
668             s->enable_stp = false;
669             break;
670
671         case OPT_OUT_OF_BAND:
672             s->in_band = false;
673             break;
674
675         case OPT_IN_BAND:
676             s->in_band = true;
677             break;
678
679         case OPT_COMMAND_ACL:
680             s->command_acl = (s->command_acl[0]
681                               ? xasprintf("%s,%s", s->command_acl, optarg)
682                               : optarg);
683             break;
684
685         case OPT_COMMAND_DIR:
686             s->command_dir = optarg;
687             break;
688
689         case 'l':
690             if (s->n_listeners >= MAX_MGMT) {
691                 ofp_fatal(0,
692                           "-l or --listen may be specified at most %d times",
693                           MAX_MGMT);
694             }
695             s->listener_names[s->n_listeners++] = optarg;
696             break;
697
698         case 'm':
699             if (s->monitor_name) {
700                 ofp_fatal(0, "-m or --monitor may only be specified once");
701             }
702             s->monitor_name = optarg;
703             break;
704
705         case 'h':
706             usage();
707
708         case 'V':
709             printf("%s %s compiled "__DATE__" "__TIME__"\n",
710                    program_name, VERSION BUILDNR);
711             exit(EXIT_SUCCESS);
712
713         DAEMON_OPTION_HANDLERS
714
715         VLOG_OPTION_HANDLERS
716
717 #ifdef HAVE_OPENSSL
718         VCONN_SSL_OPTION_HANDLERS
719
720         case OPT_BOOTSTRAP_CA_CERT:
721             vconn_ssl_set_ca_cert_file(optarg, true);
722             break;
723 #endif
724
725         case '?':
726             exit(EXIT_FAILURE);
727
728         default:
729             abort();
730         }
731     }
732     free(short_options);
733
734     argc -= optind;
735     argv += optind;
736     if (argc < 1 || argc > 2) {
737         ofp_fatal(0, "need one or two non-option arguments; "
738                   "use --help for usage");
739     }
740
741     /* Local and remote vconns. */
742     s->dp_name = argv[0];
743     s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
744
745     /* Set accept_controller_regex. */
746     if (!accept_re) {
747         accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
748     }
749     retval = regcomp(&s->accept_controller_regex, accept_re,
750                      REG_NOSUB | REG_EXTENDED);
751     if (retval) {
752         size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
753         char *buffer = xmalloc(length);
754         regerror(retval, &s->accept_controller_regex, buffer, length);
755         ofp_fatal(0, "%s: %s", accept_re, buffer);
756     }
757     s->accept_controller_re = accept_re;
758
759     /* Mode of operation. */
760     s->discovery = s->controller_name == NULL;
761     if (s->discovery && !s->in_band) {
762         ofp_fatal(0, "Cannot perform discovery with out-of-band control");
763     }
764
765     /* Rate limiting. */
766     if (s->rate_limit) {
767         if (s->rate_limit < 100) {
768             VLOG_WARN("Rate limit set to unusually low value %d",
769                       s->rate_limit);
770         }
771         if (!s->burst_limit) {
772             s->burst_limit = s->rate_limit / 4;
773         }
774         s->burst_limit = MAX(s->burst_limit, 1);
775         s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
776     }
777 }
778
779 static void
780 usage(void)
781 {
782     printf("%s: secure channel, a relay for OpenFlow messages.\n"
783            "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
784            "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
785            "CONTROLLER is an active OpenFlow connection method; if it is\n"
786            "omitted, then secchan performs controller discovery.\n",
787            program_name, program_name);
788     vconn_usage(true, true, true);
789     printf("\nController discovery options:\n"
790            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
791            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
792            "\nNetworking options:\n"
793            "  -F, --fail=open|closed  when controller connection fails:\n"
794            "                            closed: drop all packets\n"
795            "                            open (default): act as learning switch\n"
796            "  --inactivity-probe=SECS time between inactivity probes\n"
797            "  --max-idle=SECS         max idle for flows set up by secchan\n"
798            "  --max-backoff=SECS      max time between controller connection\n"
799            "                          attempts (default: 15 seconds)\n"
800            "  -l, --listen=METHOD     allow management connections on METHOD\n"
801            "                          (a passive OpenFlow connection method)\n"
802            "  -m, --monitor=METHOD    copy traffic to/from kernel to METHOD\n"
803            "                          (a passive OpenFlow connection method)\n"
804            "  --out-of-band           controller connection is out-of-band\n"
805            "  --stp                   enable 802.1D Spanning Tree Protocol\n"
806            "  --no-stp                disable 802.1D Spanning Tree Protocol\n"
807            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
808            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
809            "  --burst-limit=BURST     limit on packet credit for idle time\n"
810            "\nRemote command execution options:\n"
811            "  --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
812            "  --command-dir=DIR       set command dir (default: %s/commands)\n",
813            ofp_pkgdatadir);
814     daemon_usage();
815     vlog_usage();
816     printf("\nOther options:\n"
817            "  -h, --help              display this help message\n"
818            "  -V, --version           display version information\n");
819     exit(EXIT_SUCCESS);
820 }