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