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