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