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