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