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