8307891ad44a859eb95a61cb4ef708c7d71e7343
[cascardo/ovs.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 "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 "stream-ssl.h"
43 #include "svec.h"
44 #include "timeval.h"
45 #include "unixctl.h"
46 #include "util.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     /* 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 netflow_options nf_options;
115
116     set_program_name(argv[0]);
117     register_fault_handlers();
118     time_init();
119     vlog_init();
120     parse_options(argc, argv, &s);
121     signal(SIGPIPE, SIG_IGN);
122
123     die_if_already_running();
124     daemonize_start();
125
126     /* Start listening for ovs-appctl requests. */
127     error = unixctl_server_create(NULL, &unixctl);
128     if (error) {
129         ovs_fatal(error, "Could not listen for unixctl connections");
130     }
131
132     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
133     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
134
135     /* Create the datapath and add ports to it, if requested by the user. */
136     if (s.ports.n) {
137         struct dpif *dpif;
138         const char *port;
139         size_t i;
140
141         error = dpif_create_and_open(s.dp_name, &dpif);
142         if (error) {
143             ovs_fatal(error, "could not create datapath");
144         }
145
146         SVEC_FOR_EACH (i, port, &s.ports) {
147             error = dpif_port_add(dpif, port, 0, NULL);
148             if (error) {
149                 ovs_fatal(error, "failed to add %s as a port", port);
150             }
151         }
152         dpif_close(dpif);
153     }
154
155     /* Start OpenFlow processing. */
156     error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
157     if (error) {
158         ovs_fatal(error, "could not initialize openflow switch");
159     }
160     error = ofproto_set_in_band(ofproto, s.in_band);
161     if (error) {
162         ovs_fatal(error, "failed to configure in-band control");
163     }
164     error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
165                                   s.update_resolv_conf);
166     if (error) {
167         ovs_fatal(error, "failed to configure controller discovery");
168     }
169     if (s.datapath_id) {
170         ofproto_set_datapath_id(ofproto, s.datapath_id);
171     }
172     if (s.mgmt_id) {
173         ofproto_set_mgmt_id(ofproto, s.mgmt_id);
174     }
175     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
176     if (!s.listeners.n) {
177         svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
178                                               ovs_rundir, s.dp_name));
179     } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
180         svec_clear(&s.listeners);
181     }
182     error = ofproto_set_listeners(ofproto, &s.listeners);
183     if (error) {
184         ovs_fatal(error, "failed to configure management connections");
185     }
186     error = ofproto_set_snoops(ofproto, &s.snoops);
187     if (error) {
188         ovs_fatal(error,
189                   "failed to configure controller snooping connections");
190     }
191     memset(&nf_options, 0, sizeof nf_options);
192     nf_options.collectors = s.netflow;
193     error = ofproto_set_netflow(ofproto, &nf_options);
194     if (error) {
195         ovs_fatal(error, "failed to configure NetFlow collectors");
196     }
197     ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
198     ofproto_set_probe_interval(ofproto, s.probe_interval);
199     ofproto_set_max_backoff(ofproto, s.max_backoff);
200     ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
201     error = ofproto_set_stp(ofproto, s.enable_stp);
202     if (error) {
203         ovs_fatal(error, "failed to configure STP");
204     }
205     if (!s.discovery) {
206         error = ofproto_set_controller(ofproto, s.controller_name);
207         if (error) {
208             ovs_fatal(error, "failed to configure controller");
209         }
210     }
211
212     daemonize_complete();
213
214     while (ofproto_is_alive(ofproto)) {
215         error = ofproto_run(ofproto);
216         if (error) {
217             ovs_fatal(error, "unrecoverable datapath error");
218         }
219         unixctl_server_run(unixctl);
220         dp_run();
221         netdev_run();
222
223         ofproto_wait(ofproto);
224         unixctl_server_wait(unixctl);
225         dp_wait();
226         netdev_wait();
227         poll_block();
228     }
229
230     return 0;
231 }
232 \f
233 /* User interface. */
234
235 static void
236 parse_options(int argc, char *argv[], struct ofsettings *s)
237 {
238     enum {
239         OPT_DATAPATH_ID = UCHAR_MAX + 1,
240         OPT_MANUFACTURER,
241         OPT_HARDWARE,
242         OPT_SOFTWARE,
243         OPT_SERIAL,
244         OPT_ACCEPT_VCONN,
245         OPT_NO_RESOLV_CONF,
246         OPT_BR_NAME,
247         OPT_FAIL_MODE,
248         OPT_INACTIVITY_PROBE,
249         OPT_MAX_IDLE,
250         OPT_MAX_BACKOFF,
251         OPT_SNOOP,
252         OPT_RATE_LIMIT,
253         OPT_BURST_LIMIT,
254         OPT_BOOTSTRAP_CA_CERT,
255         OPT_STP,
256         OPT_NO_STP,
257         OPT_OUT_OF_BAND,
258         OPT_IN_BAND,
259         OPT_NETFLOW,
260         OPT_MGMT_ID,
261         OPT_PORTS,
262         VLOG_OPTION_ENUMS,
263         LEAK_CHECKER_OPTION_ENUMS
264     };
265     static struct option long_options[] = {
266         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
267         {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
268         {"hardware", required_argument, 0, OPT_HARDWARE},
269         {"software", required_argument, 0, OPT_SOFTWARE},
270         {"serial", required_argument, 0, OPT_SERIAL},
271         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
272         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
273         {"config",      required_argument, 0, 'F'},
274         {"br-name",     required_argument, 0, OPT_BR_NAME},
275         {"fail",        required_argument, 0, OPT_FAIL_MODE},
276         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
277         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
278         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
279         {"listen",      required_argument, 0, 'l'},
280         {"snoop",      required_argument, 0, OPT_SNOOP},
281         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
282         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
283         {"stp",         no_argument, 0, OPT_STP},
284         {"no-stp",      no_argument, 0, OPT_NO_STP},
285         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
286         {"in-band",     no_argument, 0, OPT_IN_BAND},
287         {"netflow",     required_argument, 0, OPT_NETFLOW},
288         {"mgmt-id",     required_argument, 0, OPT_MGMT_ID},
289         {"ports",       required_argument, 0, OPT_PORTS},
290         {"verbose",     optional_argument, 0, 'v'},
291         {"help",        no_argument, 0, 'h'},
292         {"version",     no_argument, 0, 'V'},
293         DAEMON_LONG_OPTIONS,
294         VLOG_LONG_OPTIONS,
295         LEAK_CHECKER_LONG_OPTIONS,
296 #ifdef HAVE_OPENSSL
297         STREAM_SSL_LONG_OPTIONS
298         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
299 #endif
300         {0, 0, 0, 0},
301     };
302     char *short_options = long_options_to_short_options(long_options);
303
304     /* Set defaults that we can figure out before parsing options. */
305     s->datapath_id = 0;
306     s->mfr_desc = NULL;
307     s->hw_desc = NULL;
308     s->sw_desc = NULL;
309     s->serial_desc = NULL;
310     svec_init(&s->listeners);
311     svec_init(&s->snoops);
312     s->fail_mode = FAIL_OPEN;
313     s->max_idle = 0;
314     s->probe_interval = 0;
315     s->max_backoff = 8;
316     s->update_resolv_conf = true;
317     s->rate_limit = 0;
318     s->burst_limit = 0;
319     s->accept_controller_re = NULL;
320     s->enable_stp = false;
321     s->in_band = true;
322     svec_init(&s->netflow);
323     s->mgmt_id = 0;
324     svec_init(&s->ports);
325     for (;;) {
326         int c;
327
328         c = getopt_long(argc, argv, short_options, long_options, NULL);
329         if (c == -1) {
330             break;
331         }
332
333         switch (c) {
334         case OPT_DATAPATH_ID:
335             if (!dpid_from_string(optarg, &s->datapath_id)) {
336                 ovs_fatal(0, "argument to --datapath-id must be "
337                           "exactly 12 hex digits and may not be all-zero");
338             }
339             break;
340
341         case OPT_MANUFACTURER:
342             s->mfr_desc = optarg;
343             break;
344
345         case OPT_HARDWARE:
346             s->hw_desc = optarg;
347             break;
348
349         case OPT_SOFTWARE:
350             s->sw_desc = optarg;
351             break;
352
353         case OPT_SERIAL:
354             s->serial_desc = optarg;
355             break;
356
357         case OPT_ACCEPT_VCONN:
358             s->accept_controller_re = optarg;
359             break;
360
361         case OPT_NO_RESOLV_CONF:
362             s->update_resolv_conf = false;
363             break;
364
365         case OPT_FAIL_MODE:
366             if (!strcmp(optarg, "open")) {
367                 s->fail_mode = FAIL_OPEN;
368             } else if (!strcmp(optarg, "closed")) {
369                 s->fail_mode = FAIL_CLOSED;
370             } else {
371                 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
372             }
373             break;
374
375         case OPT_INACTIVITY_PROBE:
376             s->probe_interval = atoi(optarg);
377             if (s->probe_interval < 5) {
378                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
379             }
380             break;
381
382         case OPT_MAX_IDLE:
383             if (!strcmp(optarg, "permanent")) {
384                 s->max_idle = OFP_FLOW_PERMANENT;
385             } else {
386                 s->max_idle = atoi(optarg);
387                 if (s->max_idle < 1 || s->max_idle > 65535) {
388                     ovs_fatal(0, "--max-idle argument must be between 1 and "
389                               "65535 or the word 'permanent'");
390                 }
391             }
392             break;
393
394         case OPT_MAX_BACKOFF:
395             s->max_backoff = atoi(optarg);
396             if (s->max_backoff < 1) {
397                 ovs_fatal(0, "--max-backoff argument must be at least 1");
398             } else if (s->max_backoff > 3600) {
399                 s->max_backoff = 3600;
400             }
401             break;
402
403         case OPT_RATE_LIMIT:
404             if (optarg) {
405                 s->rate_limit = atoi(optarg);
406                 if (s->rate_limit < 1) {
407                     ovs_fatal(0, "--rate-limit argument must be at least 1");
408                 }
409             } else {
410                 s->rate_limit = 1000;
411             }
412             break;
413
414         case OPT_BURST_LIMIT:
415             s->burst_limit = atoi(optarg);
416             if (s->burst_limit < 1) {
417                 ovs_fatal(0, "--burst-limit argument must be at least 1");
418             }
419             break;
420
421         case OPT_STP:
422             s->enable_stp = true;
423             break;
424
425         case OPT_NO_STP:
426             s->enable_stp = false;
427             break;
428
429         case OPT_OUT_OF_BAND:
430             s->in_band = false;
431             break;
432
433         case OPT_IN_BAND:
434             s->in_band = true;
435             break;
436
437         case OPT_NETFLOW:
438             svec_add(&s->netflow, optarg);
439             break;
440
441         case OPT_MGMT_ID:
442             if (strlen(optarg) != 12
443                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
444                 ovs_fatal(0, "argument to --mgmt-id must be "
445                           "exactly 12 hex digits");
446             }
447             s->mgmt_id = strtoll(optarg, NULL, 16);
448             if (!s->mgmt_id) {
449                 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
450             }
451             break;
452
453         case 'l':
454             svec_add(&s->listeners, optarg);
455             break;
456
457         case OPT_SNOOP:
458             svec_add(&s->snoops, optarg);
459             break;
460
461         case OPT_PORTS:
462             svec_split(&s->ports, 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         STREAM_SSL_OPTION_HANDLERS
480
481         case OPT_BOOTSTRAP_CA_CERT:
482             stream_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             = stream_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     daemon_usage();
564     vlog_usage();
565     printf("\nOther options:\n"
566            "  -h, --help              display this help message\n"
567            "  -V, --version           display version information\n");
568     leak_checker_usage();
569     exit(EXIT_SUCCESS);
570 }