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