xenserver: Add missing argument for fake-iface config
[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
202         ofproto_wait(ofproto);
203         unixctl_server_wait(unixctl);
204         poll_block();
205     }
206
207     return 0;
208 }
209 \f
210 /* User interface. */
211
212 static void
213 parse_options(int argc, char *argv[], struct ofsettings *s)
214 {
215     enum {
216         OPT_DATAPATH_ID = UCHAR_MAX + 1,
217         OPT_MANUFACTURER,
218         OPT_HARDWARE,
219         OPT_SOFTWARE,
220         OPT_SERIAL,
221         OPT_ACCEPT_VCONN,
222         OPT_NO_RESOLV_CONF,
223         OPT_BR_NAME,
224         OPT_FAIL_MODE,
225         OPT_INACTIVITY_PROBE,
226         OPT_MAX_IDLE,
227         OPT_MAX_BACKOFF,
228         OPT_SNOOP,
229         OPT_RATE_LIMIT,
230         OPT_BURST_LIMIT,
231         OPT_BOOTSTRAP_CA_CERT,
232         OPT_STP,
233         OPT_NO_STP,
234         OPT_OUT_OF_BAND,
235         OPT_IN_BAND,
236         OPT_COMMAND_ACL,
237         OPT_COMMAND_DIR,
238         OPT_NETFLOW,
239         OPT_MGMT_ID,
240         VLOG_OPTION_ENUMS,
241         LEAK_CHECKER_OPTION_ENUMS
242     };
243     static struct option long_options[] = {
244         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
245         {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
246         {"hardware", required_argument, 0, OPT_HARDWARE},
247         {"software", required_argument, 0, OPT_SOFTWARE},
248         {"serial", required_argument, 0, OPT_SERIAL},
249         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
250         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
251         {"config",      required_argument, 0, 'F'},
252         {"br-name",     required_argument, 0, OPT_BR_NAME},
253         {"fail",        required_argument, 0, OPT_FAIL_MODE},
254         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
255         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
256         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
257         {"listen",      required_argument, 0, 'l'},
258         {"snoop",      required_argument, 0, OPT_SNOOP},
259         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
260         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
261         {"stp",         no_argument, 0, OPT_STP},
262         {"no-stp",      no_argument, 0, OPT_NO_STP},
263         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
264         {"in-band",     no_argument, 0, OPT_IN_BAND},
265         {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
266         {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
267         {"netflow",     required_argument, 0, OPT_NETFLOW},
268         {"mgmt-id",     required_argument, 0, OPT_MGMT_ID},
269         {"verbose",     optional_argument, 0, 'v'},
270         {"help",        no_argument, 0, 'h'},
271         {"version",     no_argument, 0, 'V'},
272         DAEMON_LONG_OPTIONS,
273         VLOG_LONG_OPTIONS,
274         LEAK_CHECKER_LONG_OPTIONS,
275 #ifdef HAVE_OPENSSL
276         VCONN_SSL_LONG_OPTIONS
277         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
278 #endif
279         {0, 0, 0, 0},
280     };
281     char *short_options = long_options_to_short_options(long_options);
282
283     /* Set defaults that we can figure out before parsing options. */
284     s->datapath_id = 0;
285     s->mfr_desc = NULL;
286     s->hw_desc = NULL;
287     s->sw_desc = NULL;
288     s->serial_desc = NULL;
289     svec_init(&s->listeners);
290     svec_init(&s->snoops);
291     s->fail_mode = FAIL_OPEN;
292     s->max_idle = 0;
293     s->probe_interval = 0;
294     s->max_backoff = 8;
295     s->update_resolv_conf = true;
296     s->rate_limit = 0;
297     s->burst_limit = 0;
298     s->accept_controller_re = NULL;
299     s->enable_stp = false;
300     s->in_band = true;
301     s->command_acl = "";
302     s->command_dir = NULL;
303     svec_init(&s->netflow);
304     s->mgmt_id = 0;
305     for (;;) {
306         int c;
307
308         c = getopt_long(argc, argv, short_options, long_options, NULL);
309         if (c == -1) {
310             break;
311         }
312
313         switch (c) {
314         case OPT_DATAPATH_ID:
315             if (strlen(optarg) != 12
316                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
317                 ovs_fatal(0, "argument to --datapath-id must be "
318                           "exactly 12 hex digits");
319             }
320             s->datapath_id = strtoll(optarg, NULL, 16);
321             if (!s->datapath_id) {
322                 ovs_fatal(0, "argument to --datapath-id must be nonzero");
323             }
324             break;
325
326         case OPT_MANUFACTURER:
327             s->mfr_desc = optarg;
328             break;
329
330         case OPT_HARDWARE:
331             s->hw_desc = optarg;
332             break;
333
334         case OPT_SOFTWARE:
335             s->sw_desc = optarg;
336             break;
337
338         case OPT_SERIAL:
339             s->serial_desc = optarg;
340             break;
341
342         case OPT_ACCEPT_VCONN:
343             s->accept_controller_re = optarg;
344             break;
345
346         case OPT_NO_RESOLV_CONF:
347             s->update_resolv_conf = false;
348             break;
349
350         case OPT_FAIL_MODE:
351             if (!strcmp(optarg, "open")) {
352                 s->fail_mode = FAIL_OPEN;
353             } else if (!strcmp(optarg, "closed")) {
354                 s->fail_mode = FAIL_CLOSED;
355             } else {
356                 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
357             }
358             break;
359
360         case OPT_INACTIVITY_PROBE:
361             s->probe_interval = atoi(optarg);
362             if (s->probe_interval < 5) {
363                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
364             }
365             break;
366
367         case OPT_MAX_IDLE:
368             if (!strcmp(optarg, "permanent")) {
369                 s->max_idle = OFP_FLOW_PERMANENT;
370             } else {
371                 s->max_idle = atoi(optarg);
372                 if (s->max_idle < 1 || s->max_idle > 65535) {
373                     ovs_fatal(0, "--max-idle argument must be between 1 and "
374                               "65535 or the word 'permanent'");
375                 }
376             }
377             break;
378
379         case OPT_MAX_BACKOFF:
380             s->max_backoff = atoi(optarg);
381             if (s->max_backoff < 1) {
382                 ovs_fatal(0, "--max-backoff argument must be at least 1");
383             } else if (s->max_backoff > 3600) {
384                 s->max_backoff = 3600;
385             }
386             break;
387
388         case OPT_RATE_LIMIT:
389             if (optarg) {
390                 s->rate_limit = atoi(optarg);
391                 if (s->rate_limit < 1) {
392                     ovs_fatal(0, "--rate-limit argument must be at least 1");
393                 }
394             } else {
395                 s->rate_limit = 1000;
396             }
397             break;
398
399         case OPT_BURST_LIMIT:
400             s->burst_limit = atoi(optarg);
401             if (s->burst_limit < 1) {
402                 ovs_fatal(0, "--burst-limit argument must be at least 1");
403             }
404             break;
405
406         case OPT_STP:
407             s->enable_stp = true;
408             break;
409
410         case OPT_NO_STP:
411             s->enable_stp = false;
412             break;
413
414         case OPT_OUT_OF_BAND:
415             s->in_band = false;
416             break;
417
418         case OPT_IN_BAND:
419             s->in_band = true;
420             break;
421
422         case OPT_COMMAND_ACL:
423             s->command_acl = (s->command_acl[0]
424                               ? xasprintf("%s,%s", s->command_acl, optarg)
425                               : optarg);
426             break;
427
428         case OPT_COMMAND_DIR:
429             s->command_dir = optarg;
430             break;
431
432         case OPT_NETFLOW:
433             svec_add(&s->netflow, optarg);
434             break;
435
436         case OPT_MGMT_ID:
437             if (strlen(optarg) != 12
438                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
439                 ovs_fatal(0, "argument to --mgmt-id must be "
440                           "exactly 12 hex digits");
441             }
442             s->mgmt_id = strtoll(optarg, NULL, 16);
443             if (!s->mgmt_id) {
444                 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
445             }
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 'h':
457             usage();
458
459         case 'V':
460             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
461             exit(EXIT_SUCCESS);
462
463         DAEMON_OPTION_HANDLERS
464
465         VLOG_OPTION_HANDLERS
466
467         LEAK_CHECKER_OPTION_HANDLERS
468
469 #ifdef HAVE_OPENSSL
470         VCONN_SSL_OPTION_HANDLERS
471
472         case OPT_BOOTSTRAP_CA_CERT:
473             vconn_ssl_set_ca_cert_file(optarg, true);
474             break;
475 #endif
476
477         case '?':
478             exit(EXIT_FAILURE);
479
480         default:
481             abort();
482         }
483     }
484     free(short_options);
485
486     argc -= optind;
487     argv += optind;
488     if (argc < 1 || argc > 2) {
489         ovs_fatal(0, "need one or two non-option arguments; "
490                   "use --help for usage");
491     }
492
493     /* Local and remote vconns. */
494     s->dp_name = argv[0];
495     s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
496
497     /* Set accept_controller_regex. */
498     if (!s->accept_controller_re) {
499         s->accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
500     }
501
502     /* Mode of operation. */
503     s->discovery = s->controller_name == NULL;
504     if (s->discovery && !s->in_band) {
505         ovs_fatal(0, "Cannot perform discovery with out-of-band control");
506     }
507
508     /* Rate limiting. */
509     if (s->rate_limit && s->rate_limit < 100) {
510         VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
511     }
512 }
513
514 static void
515 usage(void)
516 {
517     printf("%s: an OpenFlow switch implementation.\n"
518            "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
519            "DATAPATH is a local datapath (e.g. \"dp0\").\n"
520            "CONTROLLER is an active OpenFlow connection method; if it is\n"
521            "omitted, then secchan performs controller discovery.\n",
522            program_name, program_name);
523     vconn_usage(true, true, true);
524     printf("\nOpenFlow options:\n"
525            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
526            "                          (ID must consist of 12 hex digits)\n"
527            "  --mgmt-id=ID            Use ID as the management ID\n"
528            "                          (ID must consist of 12 hex digits)\n"
529            "  --manufacturer=MFR      Identify manufacturer as MFR\n"
530            "  --hardware=HW           Identify hardware as HW\n"
531            "  --software=SW           Identify software as SW\n"
532            "  --serial=SERIAL         Identify serial number as SERIAL\n"
533            "\nController discovery options:\n"
534            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
535            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
536            "\nNetworking options:\n"
537            "  --fail=open|closed      when controller connection fails:\n"
538            "                            closed: drop all packets\n"
539            "                            open (default): act as learning switch\n"
540            "  --inactivity-probe=SECS time between inactivity probes\n"
541            "  --max-idle=SECS         max idle for flows set up by secchan\n"
542            "  --max-backoff=SECS      max time between controller connection\n"
543            "                          attempts (default: 8 seconds)\n"
544            "  -l, --listen=METHOD     allow management connections on METHOD\n"
545            "                          (a passive OpenFlow connection method)\n"
546            "  --snoop=METHOD          allow controller snooping on METHOD\n"
547            "                          (a passive OpenFlow connection method)\n"
548            "  --out-of-band           controller connection is out-of-band\n"
549            "  --netflow=HOST:PORT     configure NetFlow output target\n"
550            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
551            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
552            "  --burst-limit=BURST     limit on packet credit for idle time\n"
553            "\nRemote command execution options:\n"
554            "  --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
555            "  --command-dir=DIR       set command dir (default: %s/commands)\n",
556            ovs_pkgdatadir);
557     daemon_usage();
558     vlog_usage();
559     printf("\nOther options:\n"
560            "  -h, --help              display this help message\n"
561            "  -V, --version           display version information\n");
562     leak_checker_usage();
563     exit(EXIT_SUCCESS);
564 }