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