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