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