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