2d2446e137c61231ae85f212a96f2ad9582c6ccc
[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 "dummy.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "netdev.h"
35 #include "ofpbuf.h"
36 #include "ofproto/ofproto.h"
37 #include "openflow/openflow.h"
38 #include "packets.h"
39 #include "poll-loop.h"
40 #include "rconn.h"
41 #include "stream-ssl.h"
42 #include "timeval.h"
43 #include "unixctl.h"
44 #include "util.h"
45 #include "vconn.h"
46 #include "vlog.h"
47
48 VLOG_DEFINE_THIS_MODULE(openflowd);
49
50 /* Settings that may be configured by the user. */
51 struct ofsettings {
52     const char *unixctl_path;   /* File name for unixctl socket. */
53
54     /* Controller configuration. */
55     struct ofproto_controller *controllers;
56     size_t n_controllers;
57     enum ofproto_fail_mode fail_mode;
58     bool run_forever;           /* Continue running even with no controller? */
59
60     /* Datapath. */
61     uint64_t datapath_id;       /* Datapath ID. */
62     char *dp_name;              /* Name of local datapath. */
63     char *dp_type;              /* Type of local datapath. */
64     struct sset ports;          /* Set of ports to add to datapath (if any). */
65
66     /* Description strings. */
67     const char *mfr_desc;       /* Manufacturer. */
68     const char *hw_desc;        /* Hardware. */
69     const char *sw_desc;        /* Software version. */
70     const char *serial_desc;    /* Serial number. */
71     const char *dp_desc;        /* Datapath description. */
72
73     /* Related vconns and network devices. */
74     struct sset snoops;          /* Listen for controller snooping conns. */
75
76     /* Failure behavior. */
77     int max_idle;             /* Idle time for flows in fail-open mode. */
78
79     /* NetFlow. */
80     struct sset netflow;        /* NetFlow targets. */
81 };
82
83 static unixctl_cb_func ovs_openflowd_exit;
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 netflow_options nf_options;
96     const char *port;
97     bool exiting;
98
99     proctitle_init(argc, argv);
100     set_program_name(argv[0]);
101     parse_options(argc, argv, &s);
102     signal(SIGPIPE, SIG_IGN);
103
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     unixctl_command_register("exit", ovs_openflowd_exit, &exiting);
113
114     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
115     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
116
117     error = ofproto_create(s.dp_name, s.dp_type, &ofproto);
118     if (error) {
119         VLOG_FATAL("could not initialize OpenFlow switch (%s)",
120                    strerror(error));
121     }
122
123     /* Add ports to the datapath if requested by the user. */
124     SSET_FOR_EACH (port, &s.ports) {
125         struct netdev *netdev;
126
127         error = netdev_open_default(port, &netdev);
128         if (error) {
129             VLOG_FATAL("%s: failed to open network device (%s)",
130                        port, strerror(error));
131         }
132
133         error = ofproto_port_add(ofproto, netdev, NULL);
134         if (error) {
135             VLOG_FATAL("failed to add %s as a port (%s)",
136                        port, strerror(error));
137         }
138
139         netdev_close(netdev);
140     }
141
142     /* Configure OpenFlow switch. */
143     if (s.datapath_id) {
144         ofproto_set_datapath_id(ofproto, s.datapath_id);
145     }
146     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
147                      s.serial_desc, s.dp_desc);
148     error = ofproto_set_snoops(ofproto, &s.snoops);
149     if (error) {
150         VLOG_FATAL("failed to configure controller snooping connections (%s)",
151                    strerror(error));
152     }
153     memset(&nf_options, 0, sizeof nf_options);
154     nf_options.collectors = s.netflow;
155     error = ofproto_set_netflow(ofproto, &nf_options);
156     if (error) {
157         VLOG_FATAL("failed to configure NetFlow collectors (%s)",
158                    strerror(error));
159     }
160     ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
161     ofproto_set_fail_mode(ofproto, s.fail_mode);
162
163     daemonize_complete();
164
165     exiting = false;
166     while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
167         error = ofproto_run(ofproto);
168         if (error) {
169             VLOG_FATAL("unrecoverable datapath error (%s)", strerror(error));
170         }
171         unixctl_server_run(unixctl);
172         netdev_run();
173
174         ofproto_wait(ofproto);
175         unixctl_server_wait(unixctl);
176         netdev_wait();
177         if (exiting) {
178             poll_immediate_wake();
179         }
180         poll_block();
181     }
182
183     ofproto_destroy(ofproto);
184
185     return 0;
186 }
187
188 static void
189 ovs_openflowd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
190                    void *exiting_)
191 {
192     bool *exiting = exiting_;
193     *exiting = true;
194     unixctl_command_reply(conn, 200, NULL);
195 }
196 \f
197 /* User interface. */
198
199 /* Breaks 'ports' apart at commas and adds each resulting word to 'ports'. */
200 static void
201 parse_ports(const char *s_, struct sset *ports)
202 {
203     char *s = xstrdup(s_);
204     char *save_ptr = NULL;
205     char *token;
206
207     for (token = strtok_r(s, ",", &save_ptr); token != NULL;
208          token = strtok_r(NULL, ",", &save_ptr)) {
209         sset_add(ports, token);
210     }
211     free(s);
212 }
213
214 static void
215 parse_options(int argc, char *argv[], struct ofsettings *s)
216 {
217     enum {
218         OPT_DATAPATH_ID = UCHAR_MAX + 1,
219         OPT_MFR_DESC,
220         OPT_HW_DESC,
221         OPT_SW_DESC,
222         OPT_SERIAL_DESC,
223         OPT_DP_DESC,
224         OPT_BR_NAME,
225         OPT_FAIL_MODE,
226         OPT_INACTIVITY_PROBE,
227         OPT_MAX_IDLE,
228         OPT_MAX_BACKOFF,
229         OPT_SNOOP,
230         OPT_RATE_LIMIT,
231         OPT_BURST_LIMIT,
232         OPT_BOOTSTRAP_CA_CERT,
233         OPT_OUT_OF_BAND,
234         OPT_IN_BAND,
235         OPT_NETFLOW,
236         OPT_PORTS,
237         OPT_UNIXCTL,
238         OPT_ENABLE_DUMMY,
239         VLOG_OPTION_ENUMS,
240         LEAK_CHECKER_OPTION_ENUMS,
241         DAEMON_OPTION_ENUMS
242     };
243     static struct option long_options[] = {
244         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
245         {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
246         {"hw-desc", required_argument, 0, OPT_HW_DESC},
247         {"sw-desc", required_argument, 0, OPT_SW_DESC},
248         {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
249         {"dp-desc", required_argument, 0, OPT_DP_DESC},
250         {"config",      required_argument, 0, 'F'},
251         {"br-name",     required_argument, 0, OPT_BR_NAME},
252         {"fail",        required_argument, 0, OPT_FAIL_MODE},
253         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
254         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
255         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
256         {"listen",      required_argument, 0, 'l'},
257         {"snoop",      required_argument, 0, OPT_SNOOP},
258         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
259         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
260         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
261         {"in-band",     no_argument, 0, OPT_IN_BAND},
262         {"netflow",     required_argument, 0, OPT_NETFLOW},
263         {"ports",       required_argument, 0, OPT_PORTS},
264         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
265         {"enable-dummy", no_argument, 0, OPT_ENABLE_DUMMY},
266         {"verbose",     optional_argument, 0, 'v'},
267         {"help",        no_argument, 0, 'h'},
268         {"version",     no_argument, 0, 'V'},
269         DAEMON_LONG_OPTIONS,
270         VLOG_LONG_OPTIONS,
271         LEAK_CHECKER_LONG_OPTIONS,
272 #ifdef HAVE_OPENSSL
273         STREAM_SSL_LONG_OPTIONS
274         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
275 #endif
276         {0, 0, 0, 0},
277     };
278     char *short_options = long_options_to_short_options(long_options);
279     struct ofproto_controller controller_opts;
280     struct sset controllers;
281     const char *name;
282     int i;
283
284     /* Set defaults that we can figure out before parsing options. */
285     controller_opts.target = NULL;
286     controller_opts.max_backoff = 8;
287     controller_opts.probe_interval = 5;
288     controller_opts.band = OFPROTO_IN_BAND;
289     controller_opts.rate_limit = 0;
290     controller_opts.burst_limit = 0;
291     s->unixctl_path = NULL;
292     s->fail_mode = OFPROTO_FAIL_STANDALONE;
293     s->datapath_id = 0;
294     s->mfr_desc = NULL;
295     s->hw_desc = NULL;
296     s->sw_desc = NULL;
297     s->serial_desc = NULL;
298     s->dp_desc = NULL;
299     sset_init(&controllers);
300     sset_init(&s->snoops);
301     s->max_idle = 0;
302     sset_init(&s->netflow);
303     sset_init(&s->ports);
304     for (;;) {
305         int c;
306
307         c = getopt_long(argc, argv, short_options, long_options, NULL);
308         if (c == -1) {
309             break;
310         }
311
312         switch (c) {
313         case OPT_DATAPATH_ID:
314             if (!dpid_from_string(optarg, &s->datapath_id)) {
315                 VLOG_FATAL("argument to --datapath-id must be exactly 16 hex "
316                            "digits and may not be all-zero");
317             }
318             break;
319
320         case OPT_MFR_DESC:
321             s->mfr_desc = optarg;
322             break;
323
324         case OPT_HW_DESC:
325             s->hw_desc = optarg;
326             break;
327
328         case OPT_SW_DESC:
329             s->sw_desc = optarg;
330             break;
331
332         case OPT_SERIAL_DESC:
333             s->serial_desc = optarg;
334             break;
335
336         case OPT_DP_DESC:
337             s->dp_desc = optarg;
338             break;
339
340         case OPT_FAIL_MODE:
341             if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
342                 s->fail_mode = OFPROTO_FAIL_STANDALONE;
343             } else if (!strcmp(optarg, "closed")
344                        || !strcmp(optarg, "secure")) {
345                 s->fail_mode = OFPROTO_FAIL_SECURE;
346             } else {
347                 VLOG_FATAL("--fail argument must be \"standalone\" "
348                            "or \"secure\"");
349             }
350             break;
351
352         case OPT_INACTIVITY_PROBE:
353             controller_opts.probe_interval = atoi(optarg);
354             if (controller_opts.probe_interval < 5) {
355                 VLOG_FATAL("--inactivity-probe argument must be at least 5");
356             }
357             break;
358
359         case OPT_MAX_IDLE:
360             if (!strcmp(optarg, "permanent")) {
361                 s->max_idle = OFP_FLOW_PERMANENT;
362             } else {
363                 s->max_idle = atoi(optarg);
364                 if (s->max_idle < 1 || s->max_idle > 65535) {
365                     VLOG_FATAL("--max-idle argument must be between 1 and "
366                                "65535 or the word 'permanent'");
367                 }
368             }
369             break;
370
371         case OPT_MAX_BACKOFF:
372             controller_opts.max_backoff = atoi(optarg);
373             if (controller_opts.max_backoff < 1) {
374                 VLOG_FATAL("--max-backoff argument must be at least 1");
375             } else if (controller_opts.max_backoff > 3600) {
376                 controller_opts.max_backoff = 3600;
377             }
378             break;
379
380         case OPT_RATE_LIMIT:
381             if (optarg) {
382                 controller_opts.rate_limit = atoi(optarg);
383                 if (controller_opts.rate_limit < 1) {
384                     VLOG_FATAL("--rate-limit argument must be at least 1");
385                 }
386             } else {
387                 controller_opts.rate_limit = 1000;
388             }
389             break;
390
391         case OPT_BURST_LIMIT:
392             controller_opts.burst_limit = atoi(optarg);
393             if (controller_opts.burst_limit < 1) {
394                 VLOG_FATAL("--burst-limit argument must be at least 1");
395             }
396             break;
397
398         case OPT_OUT_OF_BAND:
399             controller_opts.band = OFPROTO_OUT_OF_BAND;
400             break;
401
402         case OPT_IN_BAND:
403             controller_opts.band = OFPROTO_IN_BAND;
404             break;
405
406         case OPT_NETFLOW:
407             sset_add(&s->netflow, optarg);
408             break;
409
410         case 'l':
411             sset_add(&controllers, optarg);
412             break;
413
414         case OPT_SNOOP:
415             sset_add(&s->snoops, optarg);
416             break;
417
418         case OPT_PORTS:
419             parse_ports(optarg, &s->ports);
420             break;
421
422         case OPT_UNIXCTL:
423             s->unixctl_path = optarg;
424             break;
425
426         case OPT_ENABLE_DUMMY:
427             dummy_enable();
428             break;
429
430         case 'h':
431             usage();
432
433         case 'V':
434             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
435             exit(EXIT_SUCCESS);
436
437         DAEMON_OPTION_HANDLERS
438
439         VLOG_OPTION_HANDLERS
440
441         LEAK_CHECKER_OPTION_HANDLERS
442
443 #ifdef HAVE_OPENSSL
444         STREAM_SSL_OPTION_HANDLERS
445
446         case OPT_BOOTSTRAP_CA_CERT:
447             stream_ssl_set_ca_cert_file(optarg, true);
448             break;
449 #endif
450
451         case '?':
452             exit(EXIT_FAILURE);
453
454         default:
455             abort();
456         }
457     }
458     free(short_options);
459
460     argc -= optind;
461     argv += optind;
462     if (argc < 2) {
463         VLOG_FATAL("need at least two non-option arguments; "
464                    "use --help for usage");
465     }
466
467     /* Rate limiting. */
468     if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
469         VLOG_WARN("Rate limit set to unusually low value %d",
470                   controller_opts.rate_limit);
471     }
472
473     /* Local vconns. */
474     ofproto_parse_name(argv[0], &s->dp_name, &s->dp_type);
475
476     /* Figure out controller names. */
477     s->run_forever = false;
478     if (sset_is_empty(&controllers)) {
479         sset_add_and_free(&controllers, xasprintf("punix:%s/%s.mgmt",
480                                                   ovs_rundir(), s->dp_name));
481     }
482     for (i = 1; i < argc; i++) {
483         if (!strcmp(argv[i], "none")) {
484             s->run_forever = true;
485         } else {
486             sset_add(&controllers, argv[i]);
487         }
488     }
489
490     /* Set up controllers. */
491     s->n_controllers = sset_count(&controllers);
492     s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
493     i = 0;
494     SSET_FOR_EACH (name, &controllers) {
495         s->controllers[i] = controller_opts;
496         s->controllers[i].target = xstrdup(name);
497         i++;
498     }
499     sset_destroy(&controllers);
500 }
501
502 static void
503 usage(void)
504 {
505     printf("%s: an OpenFlow switch implementation.\n"
506            "usage: %s [OPTIONS] [TYPE@]DATAPATH CONTROLLER...\n"
507            "where DATAPATH is a local datapath (e.g. \"dp0\")\n"
508            "optionally with an explicit TYPE (default: \"system\").\n"
509            "Each CONTROLLER is an active OpenFlow connection method.\n",
510            program_name, program_name);
511     vconn_usage(true, true, true);
512     printf("\nOpenFlow options:\n"
513            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
514            "                          (ID must consist of 16 hex digits)\n"
515            "  --mfr-desc=MFR          Identify manufacturer as MFR\n"
516            "  --hw-desc=HW            Identify hardware as HW\n"
517            "  --sw-desc=SW            Identify software as SW\n"
518            "  --serial-desc=SERIAL    Identify serial number as SERIAL\n"
519            "  --dp-desc=DP_DESC       Identify dp description as DP_DESC\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 }