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