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