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