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