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