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