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