ofproto: Remove support for OpenFlow-based management protocol.
[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     /* NetFlow. */
98     struct svec netflow;        /* NetFlow targets. */
99 };
100
101 static void parse_options(int argc, char *argv[], struct ofsettings *);
102 static void usage(void) NO_RETURN;
103
104 int
105 main(int argc, char *argv[])
106 {
107     struct unixctl_server *unixctl;
108     struct ofproto *ofproto;
109     struct ofsettings s;
110     int error;
111     struct dpif *dpif;
112     struct netflow_options nf_options;
113
114     proctitle_init(argc, argv);
115     set_program_name(argv[0]);
116     time_init();
117     vlog_init();
118     parse_options(argc, argv, &s);
119     signal(SIGPIPE, SIG_IGN);
120
121     die_if_already_running();
122     daemonize_start();
123
124     /* Start listening for ovs-appctl requests. */
125     error = unixctl_server_create(NULL, &unixctl);
126     if (error) {
127         exit(EXIT_FAILURE);
128     }
129
130     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
131     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
132
133     error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
134     if (error) {
135         ovs_fatal(error, "could not create datapath");
136     }
137
138     /* Add ports to the datapath if requested by the user. */
139     if (s.ports.n) {
140         const char *port;
141         size_t i;
142         struct netdev *netdev;
143
144         SVEC_FOR_EACH (i, port, &s.ports) {
145             error = netdev_open_default(port, &netdev);
146             if (error) {
147                 ovs_fatal(error, "failed to open %s as a device", port);
148             }
149
150             error = dpif_port_add(dpif, port, 0, NULL);
151             if (error) {
152                 ovs_fatal(error, "failed to add %s as a port", port);
153             }
154         }
155     }
156
157     /* Start OpenFlow processing. */
158     error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
159     if (error) {
160         ovs_fatal(error, "could not initialize openflow switch");
161     }
162     error = ofproto_set_in_band(ofproto, s.in_band);
163     if (error) {
164         ovs_fatal(error, "failed to configure in-band control");
165     }
166     error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
167                                   s.update_resolv_conf);
168     if (error) {
169         ovs_fatal(error, "failed to configure controller discovery");
170     }
171     if (s.datapath_id) {
172         ofproto_set_datapath_id(ofproto, s.datapath_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     dpif_close(dpif);
230
231     return 0;
232 }
233 \f
234 /* User interface. */
235
236 static void
237 parse_options(int argc, char *argv[], struct ofsettings *s)
238 {
239     enum {
240         OPT_DATAPATH_ID = UCHAR_MAX + 1,
241         OPT_MANUFACTURER,
242         OPT_HARDWARE,
243         OPT_SOFTWARE,
244         OPT_SERIAL,
245         OPT_ACCEPT_VCONN,
246         OPT_NO_RESOLV_CONF,
247         OPT_BR_NAME,
248         OPT_FAIL_MODE,
249         OPT_INACTIVITY_PROBE,
250         OPT_MAX_IDLE,
251         OPT_MAX_BACKOFF,
252         OPT_SNOOP,
253         OPT_RATE_LIMIT,
254         OPT_BURST_LIMIT,
255         OPT_BOOTSTRAP_CA_CERT,
256         OPT_STP,
257         OPT_NO_STP,
258         OPT_OUT_OF_BAND,
259         OPT_IN_BAND,
260         OPT_NETFLOW,
261         OPT_MGMT_ID,
262         OPT_PORTS,
263         VLOG_OPTION_ENUMS,
264         LEAK_CHECKER_OPTION_ENUMS
265     };
266     static struct option long_options[] = {
267         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
268         {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
269         {"hardware", required_argument, 0, OPT_HARDWARE},
270         {"software", required_argument, 0, OPT_SOFTWARE},
271         {"serial", required_argument, 0, OPT_SERIAL},
272         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
273         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
274         {"config",      required_argument, 0, 'F'},
275         {"br-name",     required_argument, 0, OPT_BR_NAME},
276         {"fail",        required_argument, 0, OPT_FAIL_MODE},
277         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
278         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
279         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
280         {"listen",      required_argument, 0, 'l'},
281         {"snoop",      required_argument, 0, OPT_SNOOP},
282         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
283         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
284         {"stp",         no_argument, 0, OPT_STP},
285         {"no-stp",      no_argument, 0, OPT_NO_STP},
286         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
287         {"in-band",     no_argument, 0, OPT_IN_BAND},
288         {"netflow",     required_argument, 0, OPT_NETFLOW},
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     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 'l':
441             svec_add(&s->listeners, optarg);
442             break;
443
444         case OPT_SNOOP:
445             svec_add(&s->snoops, optarg);
446             break;
447
448         case OPT_PORTS:
449             svec_split(&s->ports, optarg, ",");
450             break;
451
452         case 'h':
453             usage();
454
455         case 'V':
456             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
457             exit(EXIT_SUCCESS);
458
459         DAEMON_OPTION_HANDLERS
460
461         VLOG_OPTION_HANDLERS
462
463         LEAK_CHECKER_OPTION_HANDLERS
464
465 #ifdef HAVE_OPENSSL
466         STREAM_SSL_OPTION_HANDLERS
467
468         case OPT_BOOTSTRAP_CA_CERT:
469             stream_ssl_set_ca_cert_file(optarg, true);
470             break;
471 #endif
472
473         case '?':
474             exit(EXIT_FAILURE);
475
476         default:
477             abort();
478         }
479     }
480     free(short_options);
481
482     argc -= optind;
483     argv += optind;
484     if (argc < 1 || argc > 2) {
485         ovs_fatal(0, "need one or two non-option arguments; "
486                   "use --help for usage");
487     }
488
489     /* Local and remote vconns. */
490     dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
491
492     s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
493
494     /* Set accept_controller_regex. */
495     if (!s->accept_controller_re) {
496         s->accept_controller_re
497             = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
498     }
499
500     /* Mode of operation. */
501     s->discovery = s->controller_name == NULL;
502     if (s->discovery && !s->in_band) {
503         ovs_fatal(0, "Cannot perform discovery with out-of-band control");
504     }
505
506     /* Rate limiting. */
507     if (s->rate_limit && s->rate_limit < 100) {
508         VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
509     }
510 }
511
512 static void
513 usage(void)
514 {
515     printf("%s: an OpenFlow switch implementation.\n"
516            "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
517            "DATAPATH is a local datapath (e.g. \"dp0\").\n"
518            "CONTROLLER is an active OpenFlow connection method; if it is\n"
519            "omitted, then ovs-openflowd performs controller discovery.\n",
520            program_name, program_name);
521     vconn_usage(true, true, true);
522     printf("\nOpenFlow options:\n"
523            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
524            "                          (ID must consist of 12 hex digits)\n"
525            "  --manufacturer=MFR      Identify manufacturer as MFR\n"
526            "  --hardware=HW           Identify hardware as HW\n"
527            "  --software=SW           Identify software as SW\n"
528            "  --serial=SERIAL         Identify serial number as SERIAL\n"
529            "\nController discovery options:\n"
530            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
531            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
532            "\nNetworking options:\n"
533            "  --fail=open|closed      when controller connection fails:\n"
534            "                            closed: drop all packets\n"
535            "                            open (default): act as learning switch\n"
536            "  --inactivity-probe=SECS time between inactivity probes\n"
537            "  --max-idle=SECS         max idle for flows set up by switch\n"
538            "  --max-backoff=SECS      max time between controller connection\n"
539            "                          attempts (default: 8 seconds)\n"
540            "  -l, --listen=METHOD     allow management connections on METHOD\n"
541            "                          (a passive OpenFlow connection method)\n"
542            "  --snoop=METHOD          allow controller snooping on METHOD\n"
543            "                          (a passive OpenFlow connection method)\n"
544            "  --out-of-band           controller connection is out-of-band\n"
545            "  --netflow=HOST:PORT     configure NetFlow output target\n"
546            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
547            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
548            "  --burst-limit=BURST     limit on packet credit for idle time\n");
549     daemon_usage();
550     vlog_usage();
551     printf("\nOther options:\n"
552            "  -h, --help              display this help message\n"
553            "  -V, --version           display version information\n");
554     leak_checker_usage();
555     exit(EXIT_SUCCESS);
556 }