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