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