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