daemon: Define daemon options enums the same way as other option enums.
[sliver-openvswitch.git] / utilities / ovs-openflowd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26
27 #include "command-line.h"
28 #include "compiler.h"
29 #include "daemon.h"
30 #include "dirs.h"
31 #include "dpif.h"
32 #include "dummy.h"
33 #include "leak-checker.h"
34 #include "list.h"
35 #include "netdev.h"
36 #include "ofpbuf.h"
37 #include "ofproto/ofproto.h"
38 #include "openflow/openflow.h"
39 #include "packets.h"
40 #include "poll-loop.h"
41 #include "rconn.h"
42 #include "stream-ssl.h"
43 #include "svec.h"
44 #include "timeval.h"
45 #include "unixctl.h"
46 #include "util.h"
47 #include "vconn.h"
48 #include "vlog.h"
49
50 VLOG_DEFINE_THIS_MODULE(openflowd);
51
52 /* Settings that may be configured by the user. */
53 struct ofsettings {
54     const char *unixctl_path;   /* File name for unixctl socket. */
55
56     /* Controller configuration. */
57     struct ofproto_controller *controllers;
58     size_t n_controllers;
59     enum ofproto_fail_mode fail_mode;
60     bool run_forever;           /* Continue running even with no controller? */
61
62     /* Datapath. */
63     uint64_t datapath_id;       /* Datapath ID. */
64     char *dp_name;              /* Name of local datapath. */
65     char *dp_type;              /* Type of local datapath. */
66     struct svec ports;          /* Set of ports to add to datapath (if any). */
67
68     /* Description strings. */
69     const char *mfr_desc;       /* Manufacturer. */
70     const char *hw_desc;        /* Hardware. */
71     const char *sw_desc;        /* Software version. */
72     const char *serial_desc;    /* Serial number. */
73     const char *dp_desc;        /* Datapath description. */
74
75     /* Related vconns and network devices. */
76     struct svec snoops;          /* Listen for controller snooping conns. */
77
78     /* Failure behavior. */
79     int max_idle;             /* Idle time for flows in fail-open mode. */
80
81     /* NetFlow. */
82     struct svec netflow;        /* NetFlow targets. */
83 };
84
85 static unixctl_cb_func ovs_openflowd_exit;
86
87 static void parse_options(int argc, char *argv[], struct ofsettings *);
88 static void usage(void) NO_RETURN;
89
90 int
91 main(int argc, char *argv[])
92 {
93     struct unixctl_server *unixctl;
94     struct ofproto *ofproto;
95     struct ofsettings s;
96     int error;
97     struct dpif *dpif;
98     struct netflow_options nf_options;
99     bool exiting;
100
101     proctitle_init(argc, argv);
102     set_program_name(argv[0]);
103     parse_options(argc, argv, &s);
104     signal(SIGPIPE, SIG_IGN);
105
106     die_if_already_running();
107     daemonize_start();
108
109     /* Start listening for ovs-appctl requests. */
110     error = unixctl_server_create(s.unixctl_path, &unixctl);
111     if (error) {
112         exit(EXIT_FAILURE);
113     }
114
115     unixctl_command_register("exit", ovs_openflowd_exit, &exiting);
116
117     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
118     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
119
120     error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
121     if (error) {
122         ovs_fatal(error, "could not create datapath");
123     }
124
125     /* Add ports to the datapath if requested by the user. */
126     if (s.ports.n) {
127         const char *port;
128         size_t i;
129
130         SVEC_FOR_EACH (i, port, &s.ports) {
131             struct netdev *netdev;
132
133             error = netdev_open_default(port, &netdev);
134             if (error) {
135                 ovs_fatal(error, "%s: failed to open network device", port);
136             }
137
138             error = dpif_port_add(dpif, netdev, NULL);
139             if (error) {
140                 ovs_fatal(error, "failed to add %s as a port", port);
141             }
142
143             netdev_close(netdev);
144         }
145     }
146
147     /* Start OpenFlow processing. */
148     error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
149     if (error) {
150         ovs_fatal(error, "could not initialize openflow switch");
151     }
152     if (s.datapath_id) {
153         ofproto_set_datapath_id(ofproto, s.datapath_id);
154     }
155     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
156                      s.serial_desc, s.dp_desc);
157     error = ofproto_set_snoops(ofproto, &s.snoops);
158     if (error) {
159         ovs_fatal(error,
160                   "failed to configure controller snooping connections");
161     }
162     memset(&nf_options, 0, sizeof nf_options);
163     nf_options.collectors = s.netflow;
164     error = ofproto_set_netflow(ofproto, &nf_options);
165     if (error) {
166         ovs_fatal(error, "failed to configure NetFlow collectors");
167     }
168     ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
169     ofproto_set_fail_mode(ofproto, s.fail_mode);
170
171     daemonize_complete();
172
173     exiting = false;
174     while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
175         error = ofproto_run(ofproto);
176         if (error) {
177             ovs_fatal(error, "unrecoverable datapath error");
178         }
179         unixctl_server_run(unixctl);
180         dp_run();
181         netdev_run();
182
183         ofproto_wait(ofproto);
184         unixctl_server_wait(unixctl);
185         dp_wait();
186         netdev_wait();
187         if (exiting) {
188             poll_immediate_wake();
189         }
190         poll_block();
191     }
192
193     dpif_close(dpif);
194
195     return 0;
196 }
197
198 static void
199 ovs_openflowd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
200                    void *exiting_)
201 {
202     bool *exiting = exiting_;
203     *exiting = true;
204     unixctl_command_reply(conn, 200, NULL);
205 }
206 \f
207 /* User interface. */
208
209 static void
210 parse_options(int argc, char *argv[], struct ofsettings *s)
211 {
212     enum {
213         OPT_DATAPATH_ID = UCHAR_MAX + 1,
214         OPT_MFR_DESC,
215         OPT_HW_DESC,
216         OPT_SW_DESC,
217         OPT_SERIAL_DESC,
218         OPT_DP_DESC,
219         OPT_ACCEPT_VCONN,
220         OPT_NO_RESOLV_CONF,
221         OPT_BR_NAME,
222         OPT_FAIL_MODE,
223         OPT_INACTIVITY_PROBE,
224         OPT_MAX_IDLE,
225         OPT_MAX_BACKOFF,
226         OPT_SNOOP,
227         OPT_RATE_LIMIT,
228         OPT_BURST_LIMIT,
229         OPT_BOOTSTRAP_CA_CERT,
230         OPT_OUT_OF_BAND,
231         OPT_IN_BAND,
232         OPT_NETFLOW,
233         OPT_PORTS,
234         OPT_UNIXCTL,
235         OPT_ENABLE_DUMMY,
236         VLOG_OPTION_ENUMS,
237         LEAK_CHECKER_OPTION_ENUMS,
238         DAEMON_OPTION_ENUMS
239     };
240     static struct option long_options[] = {
241         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
242         {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
243         {"hw-desc", required_argument, 0, OPT_HW_DESC},
244         {"sw-desc", required_argument, 0, OPT_SW_DESC},
245         {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
246         {"dp-desc", required_argument, 0, OPT_DP_DESC},
247         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
248         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
249         {"config",      required_argument, 0, 'F'},
250         {"br-name",     required_argument, 0, OPT_BR_NAME},
251         {"fail",        required_argument, 0, OPT_FAIL_MODE},
252         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
253         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
254         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
255         {"listen",      required_argument, 0, 'l'},
256         {"snoop",      required_argument, 0, OPT_SNOOP},
257         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
258         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
259         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
260         {"in-band",     no_argument, 0, OPT_IN_BAND},
261         {"netflow",     required_argument, 0, OPT_NETFLOW},
262         {"ports",       required_argument, 0, OPT_PORTS},
263         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
264         {"enable-dummy", no_argument, 0, OPT_ENABLE_DUMMY},
265         {"verbose",     optional_argument, 0, 'v'},
266         {"help",        no_argument, 0, 'h'},
267         {"version",     no_argument, 0, 'V'},
268         DAEMON_LONG_OPTIONS,
269         VLOG_LONG_OPTIONS,
270         LEAK_CHECKER_LONG_OPTIONS,
271 #ifdef HAVE_OPENSSL
272         STREAM_SSL_LONG_OPTIONS
273         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
274 #endif
275         {0, 0, 0, 0},
276     };
277     char *short_options = long_options_to_short_options(long_options);
278     struct ofproto_controller controller_opts;
279     struct svec controllers;
280     int i;
281
282     /* Set defaults that we can figure out before parsing options. */
283     controller_opts.target = NULL;
284     controller_opts.max_backoff = 8;
285     controller_opts.probe_interval = 5;
286     controller_opts.band = OFPROTO_IN_BAND;
287     controller_opts.accept_re = NULL;
288     controller_opts.update_resolv_conf = true;
289     controller_opts.rate_limit = 0;
290     controller_opts.burst_limit = 0;
291     s->unixctl_path = NULL;
292     s->fail_mode = OFPROTO_FAIL_STANDALONE;
293     s->datapath_id = 0;
294     s->mfr_desc = NULL;
295     s->hw_desc = NULL;
296     s->sw_desc = NULL;
297     s->serial_desc = NULL;
298     s->dp_desc = NULL;
299     svec_init(&controllers);
300     svec_init(&s->snoops);
301     s->max_idle = 0;
302     svec_init(&s->netflow);
303     svec_init(&s->ports);
304     for (;;) {
305         int c;
306
307         c = getopt_long(argc, argv, short_options, long_options, NULL);
308         if (c == -1) {
309             break;
310         }
311
312         switch (c) {
313         case OPT_DATAPATH_ID:
314             if (!dpid_from_string(optarg, &s->datapath_id)) {
315                 ovs_fatal(0, "argument to --datapath-id must be "
316                           "exactly 16 hex digits and may not be all-zero");
317             }
318             break;
319
320         case OPT_MFR_DESC:
321             s->mfr_desc = optarg;
322             break;
323
324         case OPT_HW_DESC:
325             s->hw_desc = optarg;
326             break;
327
328         case OPT_SW_DESC:
329             s->sw_desc = optarg;
330             break;
331
332         case OPT_SERIAL_DESC:
333             s->serial_desc = optarg;
334             break;
335
336         case OPT_DP_DESC:
337             s->dp_desc = optarg;
338             break;
339
340         case OPT_ACCEPT_VCONN:
341             controller_opts.accept_re = optarg;
342             break;
343
344         case OPT_NO_RESOLV_CONF:
345             controller_opts.update_resolv_conf = false;
346             break;
347
348         case OPT_FAIL_MODE:
349             if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
350                 s->fail_mode = OFPROTO_FAIL_STANDALONE;
351             } else if (!strcmp(optarg, "closed")
352                        || !strcmp(optarg, "secure")) {
353                 s->fail_mode = OFPROTO_FAIL_SECURE;
354             } else {
355                 ovs_fatal(0, "--fail argument must be \"standalone\" "
356                           "or \"secure\"");
357             }
358             break;
359
360         case OPT_INACTIVITY_PROBE:
361             controller_opts.probe_interval = atoi(optarg);
362             if (controller_opts.probe_interval < 5) {
363                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
364             }
365             break;
366
367         case OPT_MAX_IDLE:
368             if (!strcmp(optarg, "permanent")) {
369                 s->max_idle = OFP_FLOW_PERMANENT;
370             } else {
371                 s->max_idle = atoi(optarg);
372                 if (s->max_idle < 1 || s->max_idle > 65535) {
373                     ovs_fatal(0, "--max-idle argument must be between 1 and "
374                               "65535 or the word 'permanent'");
375                 }
376             }
377             break;
378
379         case OPT_MAX_BACKOFF:
380             controller_opts.max_backoff = atoi(optarg);
381             if (controller_opts.max_backoff < 1) {
382                 ovs_fatal(0, "--max-backoff argument must be at least 1");
383             } else if (controller_opts.max_backoff > 3600) {
384                 controller_opts.max_backoff = 3600;
385             }
386             break;
387
388         case OPT_RATE_LIMIT:
389             if (optarg) {
390                 controller_opts.rate_limit = atoi(optarg);
391                 if (controller_opts.rate_limit < 1) {
392                     ovs_fatal(0, "--rate-limit argument must be at least 1");
393                 }
394             } else {
395                 controller_opts.rate_limit = 1000;
396             }
397             break;
398
399         case OPT_BURST_LIMIT:
400             controller_opts.burst_limit = atoi(optarg);
401             if (controller_opts.burst_limit < 1) {
402                 ovs_fatal(0, "--burst-limit argument must be at least 1");
403             }
404             break;
405
406         case OPT_OUT_OF_BAND:
407             controller_opts.band = OFPROTO_OUT_OF_BAND;
408             break;
409
410         case OPT_IN_BAND:
411             controller_opts.band = OFPROTO_IN_BAND;
412             break;
413
414         case OPT_NETFLOW:
415             svec_add(&s->netflow, optarg);
416             break;
417
418         case 'l':
419             svec_add(&controllers, optarg);
420             break;
421
422         case OPT_SNOOP:
423             svec_add(&s->snoops, optarg);
424             break;
425
426         case OPT_PORTS:
427             svec_split(&s->ports, optarg, ",");
428             break;
429
430         case OPT_UNIXCTL:
431             s->unixctl_path = optarg;
432             break;
433
434         case OPT_ENABLE_DUMMY:
435             dummy_enable();
436             break;
437
438         case 'h':
439             usage();
440
441         case 'V':
442             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
443             exit(EXIT_SUCCESS);
444
445         DAEMON_OPTION_HANDLERS
446
447         VLOG_OPTION_HANDLERS
448
449         LEAK_CHECKER_OPTION_HANDLERS
450
451 #ifdef HAVE_OPENSSL
452         STREAM_SSL_OPTION_HANDLERS
453
454         case OPT_BOOTSTRAP_CA_CERT:
455             stream_ssl_set_ca_cert_file(optarg, true);
456             break;
457 #endif
458
459         case '?':
460             exit(EXIT_FAILURE);
461
462         default:
463             abort();
464         }
465     }
466     free(short_options);
467
468     argc -= optind;
469     argv += optind;
470     if (argc < 1) {
471         ovs_fatal(0, "need at least one non-option arguments; "
472                   "use --help for usage");
473     }
474
475     /* Set accept_controller_regex. */
476     if (!controller_opts.accept_re) {
477         controller_opts.accept_re
478             = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
479     }
480
481     /* Rate limiting. */
482     if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
483         VLOG_WARN("Rate limit set to unusually low value %d",
484                   controller_opts.rate_limit);
485     }
486
487     /* Local vconns. */
488     dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
489
490     /* Figure out controller names. */
491     s->run_forever = false;
492     if (!controllers.n) {
493         svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
494                                                 ovs_rundir(), s->dp_name));
495     }
496     for (i = 1; i < argc; i++) {
497         if (!strcmp(argv[i], "none")) {
498             s->run_forever = true;
499         } else {
500             svec_add(&controllers, argv[i]);
501         }
502     }
503     if (argc < 2) {
504         svec_add(&controllers, "discover");
505     }
506
507     /* Set up controllers. */
508     s->n_controllers = controllers.n;
509     s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
510     for (i = 0; i < s->n_controllers; i++) {
511         s->controllers[i] = controller_opts;
512         s->controllers[i].target = controllers.names[i];
513     }
514
515     /* Sanity check. */
516     if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
517         for (i = 0; i < s->n_controllers; i++) {
518             if (!strcmp(s->controllers[i].target, "discover")) {
519                 ovs_fatal(0, "Cannot perform discovery with out-of-band "
520                           "control");
521             }
522         }
523     }
524 }
525
526 static void
527 usage(void)
528 {
529     printf("%s: an OpenFlow switch implementation.\n"
530            "usage: %s [OPTIONS] [TYPE@]DATAPATH [CONTROLLER...]\n"
531            "where DATAPATH is a local datapath (e.g. \"dp0\")\n"
532            "optionally with an explicit TYPE (default: \"system\").\n"
533            "Each CONTROLLER is an active OpenFlow connection method.  If\n"
534            "none is given, ovs-openflowd performs controller discovery.\n",
535            program_name, program_name);
536     vconn_usage(true, true, true);
537     printf("\nOpenFlow options:\n"
538            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
539            "                          (ID must consist of 16 hex digits)\n"
540            "  --mfr-desc=MFR          Identify manufacturer as MFR\n"
541            "  --hw-desc=HW            Identify hardware as HW\n"
542            "  --sw-desc=SW            Identify software as SW\n"
543            "  --serial-desc=SERIAL    Identify serial number as SERIAL\n"
544            "  --dp-desc=DP_DESC       Identify dp description as DP_DESC\n"
545            "\nController discovery options:\n"
546            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
547            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
548            "\nNetworking options:\n"
549            "  --fail=open|closed      when controller connection fails:\n"
550            "                            closed: drop all packets\n"
551            "                            open (default): act as learning switch\n"
552            "  --inactivity-probe=SECS time between inactivity probes\n"
553            "  --max-idle=SECS         max idle for flows set up by switch\n"
554            "  --max-backoff=SECS      max time between controller connection\n"
555            "                          attempts (default: 8 seconds)\n"
556            "  -l, --listen=METHOD     allow management connections on METHOD\n"
557            "                          (a passive OpenFlow connection method)\n"
558            "  --snoop=METHOD          allow controller snooping on METHOD\n"
559            "                          (a passive OpenFlow connection method)\n"
560            "  --out-of-band           controller connection is out-of-band\n"
561            "  --netflow=HOST:PORT     configure NetFlow output target\n"
562            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
563            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
564            "  --burst-limit=BURST     limit on packet credit for idle time\n");
565     daemon_usage();
566     vlog_usage();
567     printf("\nOther options:\n"
568            "  --unixctl=SOCKET        override default control socket name\n"
569            "  -h, --help              display this help message\n"
570            "  -V, --version           display version information\n");
571     leak_checker_usage();
572     exit(EXIT_SUCCESS);
573 }