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