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