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