Merge citrix into master.
[sliver-openvswitch.git] / utilities / ovs-openflowd.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 "dpif.h"
32 #include "fault.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 "svec.h"
43 #include "timeval.h"
44 #include "unixctl.h"
45 #include "util.h"
46 #include "vconn-ssl.h"
47 #include "vconn.h"
48
49 #include "vlog.h"
50 #define THIS_MODULE VLM_openflowd
51
52 /* Behavior when the connection to the controller fails. */
53 enum fail_mode {
54     FAIL_OPEN,                  /* Act as learning switch. */
55     FAIL_CLOSED                 /* Drop all packets. */
56 };
57
58 /* Settings that may be configured by the user. */
59 struct ofsettings {
60     /* Overall mode of operation. */
61     bool discovery;           /* Discover the controller automatically? */
62     bool in_band;             /* Connect to controller in-band? */
63
64     /* Datapath. */
65     uint64_t datapath_id;       /* Datapath ID. */
66     const char *dp_name;        /* Name of local datapath. */
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     /* Remote command execution. */
97     char *command_acl;          /* Command white/blacklist, as shell globs. */
98     char *command_dir;          /* Directory that contains commands. */
99
100     /* Management. */
101     uint64_t mgmt_id;           /* Management ID. */
102
103     /* NetFlow. */
104     struct svec netflow;        /* NetFlow targets. */
105 };
106
107 static void parse_options(int argc, char *argv[], struct ofsettings *);
108 static void usage(void) NO_RETURN;
109
110 int
111 main(int argc, char *argv[])
112 {
113     struct unixctl_server *unixctl;
114     struct ofproto *ofproto;
115     struct ofsettings s;
116     int error;
117
118     set_program_name(argv[0]);
119     register_fault_handlers();
120     time_init();
121     vlog_init();
122     parse_options(argc, argv, &s);
123     signal(SIGPIPE, SIG_IGN);
124
125     die_if_already_running();
126     daemonize();
127
128     /* Start listening for ovs-appctl requests. */
129     error = unixctl_server_create(NULL, &unixctl);
130     if (error) {
131         ovs_fatal(error, "Could not listen for unixctl connections");
132     }
133
134     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
135     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
136
137     /* Start OpenFlow processing. */
138     error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
139     if (error) {
140         ovs_fatal(error, "could not initialize openflow switch");
141     }
142     error = ofproto_set_in_band(ofproto, s.in_band);
143     if (error) {
144         ovs_fatal(error, "failed to configure in-band control");
145     }
146     error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
147                                   s.update_resolv_conf);
148     if (error) {
149         ovs_fatal(error, "failed to configure controller discovery");
150     }
151     if (s.datapath_id) {
152         ofproto_set_datapath_id(ofproto, s.datapath_id);
153     }
154     if (s.mgmt_id) {
155         ofproto_set_mgmt_id(ofproto, s.mgmt_id);
156     }
157     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
158     error = ofproto_set_listeners(ofproto, &s.listeners);
159     if (error) {
160         ovs_fatal(error, "failed to configure management connections");
161     }
162     error = ofproto_set_snoops(ofproto, &s.snoops);
163     if (error) {
164         ovs_fatal(error,
165                   "failed to configure controller snooping connections");
166     }
167     error = ofproto_set_netflow(ofproto, &s.netflow, 0, 0, false);
168     if (error) {
169         ovs_fatal(error, "failed to configure NetFlow collectors");
170     }
171     ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
172     ofproto_set_probe_interval(ofproto, s.probe_interval);
173     ofproto_set_max_backoff(ofproto, s.max_backoff);
174     ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
175     error = ofproto_set_stp(ofproto, s.enable_stp);
176     if (error) {
177         ovs_fatal(error, "failed to configure STP");
178     }
179     error = ofproto_set_remote_execution(ofproto, s.command_acl,
180                                          s.command_dir);
181     if (error) {
182         ovs_fatal(error, "failed to configure remote command execution");
183     }
184     if (!s.discovery) {
185         error = ofproto_set_controller(ofproto, s.controller_name);
186         if (error) {
187             ovs_fatal(error, "failed to configure controller");
188         }
189     }
190
191     while (ofproto_is_alive(ofproto)) {
192         error = ofproto_run(ofproto);
193         if (error) {
194             ovs_fatal(error, "unrecoverable datapath error");
195         }
196         unixctl_server_run(unixctl);
197         dp_run();
198         netdev_run();
199
200         ofproto_wait(ofproto);
201         unixctl_server_wait(unixctl);
202         dp_wait();
203         netdev_wait();
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
500             = vconn_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
501     }
502
503     /* Mode of operation. */
504     s->discovery = s->controller_name == NULL;
505     if (s->discovery && !s->in_band) {
506         ovs_fatal(0, "Cannot perform discovery with out-of-band control");
507     }
508
509     /* Rate limiting. */
510     if (s->rate_limit && s->rate_limit < 100) {
511         VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
512     }
513 }
514
515 static void
516 usage(void)
517 {
518     printf("%s: an OpenFlow switch implementation.\n"
519            "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
520            "DATAPATH is a local datapath (e.g. \"dp0\").\n"
521            "CONTROLLER is an active OpenFlow connection method; if it is\n"
522            "omitted, then ovs-openflowd performs controller discovery.\n",
523            program_name, program_name);
524     vconn_usage(true, true, true);
525     printf("\nOpenFlow options:\n"
526            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
527            "                          (ID must consist of 12 hex digits)\n"
528            "  --mgmt-id=ID            Use ID as the management ID\n"
529            "                          (ID must consist of 12 hex digits)\n"
530            "  --manufacturer=MFR      Identify manufacturer as MFR\n"
531            "  --hardware=HW           Identify hardware as HW\n"
532            "  --software=SW           Identify software as SW\n"
533            "  --serial=SERIAL         Identify serial number as SERIAL\n"
534            "\nController discovery options:\n"
535            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
536            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
537            "\nNetworking options:\n"
538            "  --fail=open|closed      when controller connection fails:\n"
539            "                            closed: drop all packets\n"
540            "                            open (default): act as learning switch\n"
541            "  --inactivity-probe=SECS time between inactivity probes\n"
542            "  --max-idle=SECS         max idle for flows set up by switch\n"
543            "  --max-backoff=SECS      max time between controller connection\n"
544            "                          attempts (default: 8 seconds)\n"
545            "  -l, --listen=METHOD     allow management connections on METHOD\n"
546            "                          (a passive OpenFlow connection method)\n"
547            "  --snoop=METHOD          allow controller snooping on METHOD\n"
548            "                          (a passive OpenFlow connection method)\n"
549            "  --out-of-band           controller connection is out-of-band\n"
550            "  --netflow=HOST:PORT     configure NetFlow output target\n"
551            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
552            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
553            "  --burst-limit=BURST     limit on packet credit for idle time\n"
554            "\nRemote command execution options:\n"
555            "  --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
556            "  --command-dir=DIR       set command dir (default: %s/commands)\n",
557            ovs_pkgdatadir);
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 }