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