Merge "master" into "wdp".
[sliver-openvswitch.git] / utilities / ovs-openflowd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 "leak-checker.h"
32 #include "list.h"
33 #include "netdev.h"
34 #include "ofpbuf.h"
35 #include "ofproto/ofproto.h"
36 #include "ofproto/wdp.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 "svec.h"
43 #include "timeval.h"
44 #include "unixctl.h"
45 #include "util.h"
46 #include "vconn.h"
47 #include "xfif.h"
48
49 #include "vlog.h"
50 #define THIS_MODULE VLM_openflowd
51
52 /* Settings that may be configured by the user. */
53 struct ofsettings {
54     /* Controller configuration. */
55     struct ofproto_controller *controllers;
56     size_t n_controllers;
57
58     /* Datapath. */
59     uint64_t datapath_id;       /* Datapath ID. */
60     char *dp_name;              /* Name of local datapath. */
61     char *dp_type;              /* Type of local datapath. */
62     struct svec ports;          /* Set of ports to add to datapath (if any). */
63
64     /* Description strings. */
65     const char *mfr_desc;       /* Manufacturer. */
66     const char *hw_desc;        /* Hardware. */
67     const char *sw_desc;        /* Software version. */
68     const char *serial_desc;    /* Serial number. */
69     const char *dp_desc;        /* Datapath description. */
70
71     /* Related vconns and network devices. */
72     struct svec listeners;       /* Listen for management connections. */
73     struct svec snoops;          /* Listen for controller snooping conns. */
74
75     /* Failure behavior. */
76     int max_idle;             /* Idle time for flows in fail-open mode. */
77
78     /* Spanning tree protocol. */
79     bool enable_stp;
80
81     /* NetFlow. */
82     struct svec netflow;        /* NetFlow targets. */
83 };
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 xfif *xfif;
96     struct netflow_options nf_options;
97
98     proctitle_init(argc, argv);
99     set_program_name(argv[0]);
100     time_init();
101     vlog_init();
102     parse_options(argc, argv, &s);
103     signal(SIGPIPE, SIG_IGN);
104
105     die_if_already_running();
106     daemonize_start();
107
108     /* Start listening for ovs-appctl requests. */
109     error = unixctl_server_create(NULL, &unixctl);
110     if (error) {
111         exit(EXIT_FAILURE);
112     }
113
114     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
115     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
116
117     error = xfif_create_and_open(s.dp_name, s.dp_type, &xfif);
118     if (error) {
119         ovs_fatal(error, "could not create datapath");
120     }
121
122     /* Add ports to the datapath if requested by the user. */
123     if (s.ports.n) {
124         const char *port;
125         size_t i;
126         struct netdev *netdev;
127
128         SVEC_FOR_EACH (i, port, &s.ports) {
129             error = netdev_open_default(port, &netdev);
130             if (error) {
131                 ovs_fatal(error, "failed to open %s as a device", port);
132             }
133
134             error = xfif_port_add(xfif, port, 0, NULL);
135             if (error) {
136                 ovs_fatal(error, "failed to add %s as a port", port);
137             }
138         }
139     }
140
141     /* Start OpenFlow processing. */
142     error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
143     if (error) {
144         ovs_fatal(error, "could not initialize openflow switch");
145     }
146     if (s.datapath_id) {
147         ofproto_set_datapath_id(ofproto, s.datapath_id);
148     }
149     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
150                      s.serial_desc, s.dp_desc);
151     if (!s.listeners.n) {
152         svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
153                                               ovs_rundir, s.dp_name));
154     } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
155         svec_clear(&s.listeners);
156     }
157     error = ofproto_set_listeners(ofproto, &s.listeners);
158     if (error) {
159         ovs_fatal(error, "failed to configure management connections");
160     }
161     error = ofproto_set_snoops(ofproto, &s.snoops);
162     if (error) {
163         ovs_fatal(error,
164                   "failed to configure controller snooping connections");
165     }
166     memset(&nf_options, 0, sizeof nf_options);
167     nf_options.collectors = s.netflow;
168     error = ofproto_set_netflow(ofproto, &nf_options);
169     if (error) {
170         ovs_fatal(error, "failed to configure NetFlow collectors");
171     }
172     error = ofproto_set_stp(ofproto, s.enable_stp);
173     if (error) {
174         ovs_fatal(error, "failed to configure STP");
175     }
176     ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
177
178     daemonize_complete();
179
180     while (ofproto_is_alive(ofproto)) {
181         error = ofproto_run(ofproto);
182         if (error) {
183             ovs_fatal(error, "unrecoverable datapath error");
184         }
185         unixctl_server_run(unixctl);
186         wdp_run();
187         netdev_run();
188
189         ofproto_wait(ofproto);
190         unixctl_server_wait(unixctl);
191         wdp_wait();
192         netdev_wait();
193         poll_block();
194     }
195
196     xfif_close(xfif);
197
198     return 0;
199 }
200 \f
201 /* User interface. */
202
203 static void
204 parse_options(int argc, char *argv[], struct ofsettings *s)
205 {
206     enum {
207         OPT_DATAPATH_ID = UCHAR_MAX + 1,
208         OPT_MFR_DESC,
209         OPT_HW_DESC,
210         OPT_SW_DESC,
211         OPT_SERIAL_DESC,
212         OPT_DP_DESC,
213         OPT_ACCEPT_VCONN,
214         OPT_NO_RESOLV_CONF,
215         OPT_BR_NAME,
216         OPT_FAIL_MODE,
217         OPT_INACTIVITY_PROBE,
218         OPT_MAX_IDLE,
219         OPT_MAX_BACKOFF,
220         OPT_SNOOP,
221         OPT_RATE_LIMIT,
222         OPT_BURST_LIMIT,
223         OPT_BOOTSTRAP_CA_CERT,
224         OPT_STP,
225         OPT_NO_STP,
226         OPT_OUT_OF_BAND,
227         OPT_IN_BAND,
228         OPT_NETFLOW,
229         OPT_PORTS,
230         VLOG_OPTION_ENUMS,
231         LEAK_CHECKER_OPTION_ENUMS
232     };
233     static struct option long_options[] = {
234         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
235         {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
236         {"hw-desc", required_argument, 0, OPT_HW_DESC},
237         {"sw-desc", required_argument, 0, OPT_SW_DESC},
238         {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
239         {"dp-desc", required_argument, 0, OPT_DP_DESC},
240         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
241         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
242         {"config",      required_argument, 0, 'F'},
243         {"br-name",     required_argument, 0, OPT_BR_NAME},
244         {"fail",        required_argument, 0, OPT_FAIL_MODE},
245         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
246         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
247         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
248         {"listen",      required_argument, 0, 'l'},
249         {"snoop",      required_argument, 0, OPT_SNOOP},
250         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
251         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
252         {"stp",         no_argument, 0, OPT_STP},
253         {"no-stp",      no_argument, 0, OPT_NO_STP},
254         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
255         {"in-band",     no_argument, 0, OPT_IN_BAND},
256         {"netflow",     required_argument, 0, OPT_NETFLOW},
257         {"ports",       required_argument, 0, OPT_PORTS},
258         {"verbose",     optional_argument, 0, 'v'},
259         {"help",        no_argument, 0, 'h'},
260         {"version",     no_argument, 0, 'V'},
261         DAEMON_LONG_OPTIONS,
262         VLOG_LONG_OPTIONS,
263         LEAK_CHECKER_LONG_OPTIONS,
264 #ifdef HAVE_OPENSSL
265         STREAM_SSL_LONG_OPTIONS
266         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
267 #endif
268         {0, 0, 0, 0},
269     };
270     char *short_options = long_options_to_short_options(long_options);
271     struct ofproto_controller controller_opts;
272
273     /* Set defaults that we can figure out before parsing options. */
274     controller_opts.target = NULL;
275     controller_opts.max_backoff = 8;
276     controller_opts.probe_interval = 5;
277     controller_opts.fail = OFPROTO_FAIL_STANDALONE;
278     controller_opts.band = OFPROTO_IN_BAND;
279     controller_opts.accept_re = NULL;
280     controller_opts.update_resolv_conf = true;
281     controller_opts.rate_limit = 0;
282     controller_opts.burst_limit = 0;
283     s->datapath_id = 0;
284     s->mfr_desc = NULL;
285     s->hw_desc = NULL;
286     s->sw_desc = NULL;
287     s->serial_desc = NULL;
288     s->dp_desc = NULL;
289     svec_init(&s->listeners);
290     svec_init(&s->snoops);
291     s->max_idle = 0;
292     s->enable_stp = false;
293     svec_init(&s->netflow);
294     svec_init(&s->ports);
295     for (;;) {
296         int c;
297
298         c = getopt_long(argc, argv, short_options, long_options, NULL);
299         if (c == -1) {
300             break;
301         }
302
303         switch (c) {
304         case OPT_DATAPATH_ID:
305             if (!dpid_from_string(optarg, &s->datapath_id)) {
306                 ovs_fatal(0, "argument to --datapath-id must be "
307                           "exactly 16 hex digits and may not be all-zero");
308             }
309             break;
310
311         case OPT_MFR_DESC:
312             s->mfr_desc = optarg;
313             break;
314
315         case OPT_HW_DESC:
316             s->hw_desc = optarg;
317             break;
318
319         case OPT_SW_DESC:
320             s->sw_desc = optarg;
321             break;
322
323         case OPT_SERIAL_DESC:
324             s->serial_desc = optarg;
325             break;
326
327         case OPT_DP_DESC:
328             s->dp_desc = optarg;
329             break;
330
331         case OPT_ACCEPT_VCONN:
332             controller_opts.accept_re = optarg;
333             break;
334
335         case OPT_NO_RESOLV_CONF:
336             controller_opts.update_resolv_conf = false;
337             break;
338
339         case OPT_FAIL_MODE:
340             if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
341                 controller_opts.fail = OFPROTO_FAIL_STANDALONE;
342             } else if (!strcmp(optarg, "closed")
343                        || !strcmp(optarg, "secure")) {
344                 controller_opts.fail = OFPROTO_FAIL_SECURE;
345             } else {
346                 ovs_fatal(0, "--fail argument must be \"standalone\" "
347                           "or \"secure\"");
348             }
349             break;
350
351         case OPT_INACTIVITY_PROBE:
352             controller_opts.probe_interval = atoi(optarg);
353             if (controller_opts.probe_interval < 5) {
354                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
355             }
356             break;
357
358         case OPT_MAX_IDLE:
359             if (!strcmp(optarg, "permanent")) {
360                 s->max_idle = OFP_FLOW_PERMANENT;
361             } else {
362                 s->max_idle = atoi(optarg);
363                 if (s->max_idle < 1 || s->max_idle > 65535) {
364                     ovs_fatal(0, "--max-idle argument must be between 1 and "
365                               "65535 or the word 'permanent'");
366                 }
367             }
368             break;
369
370         case OPT_MAX_BACKOFF:
371             controller_opts.max_backoff = atoi(optarg);
372             if (controller_opts.max_backoff < 1) {
373                 ovs_fatal(0, "--max-backoff argument must be at least 1");
374             } else if (controller_opts.max_backoff > 3600) {
375                 controller_opts.max_backoff = 3600;
376             }
377             break;
378
379         case OPT_RATE_LIMIT:
380             if (optarg) {
381                 controller_opts.rate_limit = atoi(optarg);
382                 if (controller_opts.rate_limit < 1) {
383                     ovs_fatal(0, "--rate-limit argument must be at least 1");
384                 }
385             } else {
386                 controller_opts.rate_limit = 1000;
387             }
388             break;
389
390         case OPT_BURST_LIMIT:
391             controller_opts.burst_limit = atoi(optarg);
392             if (controller_opts.burst_limit < 1) {
393                 ovs_fatal(0, "--burst-limit argument must be at least 1");
394             }
395             break;
396
397         case OPT_STP:
398             s->enable_stp = true;
399             break;
400
401         case OPT_NO_STP:
402             s->enable_stp = false;
403             break;
404
405         case OPT_OUT_OF_BAND:
406             controller_opts.band = OFPROTO_OUT_OF_BAND;
407             break;
408
409         case OPT_IN_BAND:
410             controller_opts.band = OFPROTO_IN_BAND;
411             break;
412
413         case OPT_NETFLOW:
414             svec_add(&s->netflow, optarg);
415             break;
416
417         case 'l':
418             svec_add(&s->listeners, optarg);
419             break;
420
421         case OPT_SNOOP:
422             svec_add(&s->snoops, optarg);
423             break;
424
425         case OPT_PORTS:
426             svec_split(&s->ports, optarg, ",");
427             break;
428
429         case 'h':
430             usage();
431
432         case 'V':
433             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
434             exit(EXIT_SUCCESS);
435
436         DAEMON_OPTION_HANDLERS
437
438         VLOG_OPTION_HANDLERS
439
440         LEAK_CHECKER_OPTION_HANDLERS
441
442 #ifdef HAVE_OPENSSL
443         STREAM_SSL_OPTION_HANDLERS
444
445         case OPT_BOOTSTRAP_CA_CERT:
446             stream_ssl_set_ca_cert_file(optarg, true);
447             break;
448 #endif
449
450         case '?':
451             exit(EXIT_FAILURE);
452
453         default:
454             abort();
455         }
456     }
457     free(short_options);
458
459     argc -= optind;
460     argv += optind;
461     if (argc < 1 || argc > 2) {
462         ovs_fatal(0, "need one or two non-option arguments; "
463                   "use --help for usage");
464     }
465
466     /* Set accept_controller_regex. */
467     if (!controller_opts.accept_re) {
468         controller_opts.accept_re
469             = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
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     xf_parse_name(argv[0], &s->dp_name, &s->dp_type);
480
481     /* Controllers. */
482     s->n_controllers = argc > 1 ? argc - 1 : 1;
483     s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
484     if (argc > 1) {
485         size_t i;
486
487         for (i = 0; i < s->n_controllers; i++) {
488             s->controllers[i] = controller_opts;
489             s->controllers[i].target = argv[i + 1];
490         }
491     } else {
492         s->controllers[0] = controller_opts;
493         s->controllers[0].target = "discover";
494     }
495
496     /* Sanity check. */
497     if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
498         size_t i;
499
500         for (i = 0; i < s->n_controllers; i++) {
501             if (!strcmp(s->controllers[i].target, "discover")) {
502                 ovs_fatal(0, "Cannot perform discovery with out-of-band "
503                           "control");
504             }
505         }
506     }
507 }
508
509 static void
510 usage(void)
511 {
512     printf("%s: an OpenFlow switch implementation.\n"
513            "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
514            "DATAPATH is a local datapath (e.g. \"dp0\").\n"
515            "Each CONTROLLER is an active OpenFlow connection method.  If\n"
516            "none is given, ovs-openflowd performs controller discovery.\n",
517            program_name, program_name);
518     vconn_usage(true, true, true);
519     printf("\nOpenFlow options:\n"
520            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
521            "                          (ID must consist of 16 hex digits)\n"
522            "  --mfr-desc=MFR          Identify manufacturer as MFR\n"
523            "  --hw-desc=HW            Identify hardware as HW\n"
524            "  --sw-desc=SW            Identify software as SW\n"
525            "  --serial-desc=SERIAL    Identify serial number as SERIAL\n"
526            "  --dp-desc=DP_DESC       Identify dp description as DP_DESC\n"
527            "\nController discovery options:\n"
528            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
529            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
530            "\nNetworking options:\n"
531            "  --fail=open|closed      when controller connection fails:\n"
532            "                            closed: drop all packets\n"
533            "                            open (default): act as learning switch\n"
534            "  --inactivity-probe=SECS time between inactivity probes\n"
535            "  --max-idle=SECS         max idle for flows set up by switch\n"
536            "  --max-backoff=SECS      max time between controller connection\n"
537            "                          attempts (default: 8 seconds)\n"
538            "  -l, --listen=METHOD     allow management connections on METHOD\n"
539            "                          (a passive OpenFlow connection method)\n"
540            "  --snoop=METHOD          allow controller snooping on METHOD\n"
541            "                          (a passive OpenFlow connection method)\n"
542            "  --out-of-band           controller connection is out-of-band\n"
543            "  --netflow=HOST:PORT     configure NetFlow output target\n"
544            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
545            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
546            "  --burst-limit=BURST     limit on packet credit for idle time\n");
547     daemon_usage();
548     vlog_usage();
549     printf("\nOther options:\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 }