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