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