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