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