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