Crossport lib/svec.[ch] from master branch.
[sliver-openvswitch.git] / udatapath / udatapath.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <limits.h>
38 #include <signal.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "command-line.h"
44 #include "daemon.h"
45 #include "datapath.h"
46 #include "fault.h"
47 #include "openflow/openflow.h"
48 #include "poll-loop.h"
49 #include "queue.h"
50 #include "util.h"
51 #include "rconn.h"
52 #include "timeval.h"
53 #include "vconn.h"
54 #include "dirs.h"
55 #include "vconn-ssl.h"
56 #include "vlog-socket.h"
57
58 #define THIS_MODULE VLM_udatapath
59 #include "vlog.h"
60
61 /* Strings to describe the manufacturer, hardware, and software.  This data 
62  * is queriable through the switch description stats message. */
63 char mfr_desc[DESC_STR_LEN] = "Nicira Networks";
64 char hw_desc[DESC_STR_LEN] = "Reference User-Space Switch";
65 char sw_desc[DESC_STR_LEN] = VERSION BUILDNR;
66 char serial_num[SERIAL_NUM_LEN] = "None";
67
68 static void parse_options(int argc, char *argv[]);
69 static void usage(void) NO_RETURN;
70
71 static struct datapath *dp;
72 static uint64_t dpid = UINT64_MAX;
73 static char *port_list;
74 static char *local_port = "tap:";
75
76 static void add_ports(struct datapath *dp, char *port_list);
77
78 int
79 main(int argc, char *argv[])
80 {
81     int n_listeners;
82     int error;
83     int i;
84
85     set_program_name(argv[0]);
86     register_fault_handlers();
87     time_init();
88     vlog_init();
89     parse_options(argc, argv);
90     signal(SIGPIPE, SIG_IGN);
91
92     if (argc - optind < 1) {
93         ofp_fatal(0, "at least one listener argument is required; "
94                   "use --help for usage");
95     }
96
97     error = dp_new(&dp, dpid);
98
99     n_listeners = 0;
100     for (i = optind; i < argc; i++) {
101         const char *pvconn_name = argv[i];
102         struct pvconn *pvconn;
103         int retval;
104
105         retval = pvconn_open(pvconn_name, &pvconn);
106         if (!retval || retval == EAGAIN) {
107             dp_add_pvconn(dp, pvconn);
108             n_listeners++;
109         } else {
110             ofp_error(retval, "opening %s", pvconn_name);
111         }
112     }
113     if (!n_listeners) {
114         ofp_fatal(0, "could not listen for any connections");
115     }
116
117     if (port_list) {
118         add_ports(dp, port_list);
119     }
120     if (local_port) {
121         error = dp_add_local_port(dp, local_port);
122         if (error) {
123             ofp_fatal(error, "failed to add local port %s", local_port);
124         }
125     }
126
127     error = vlog_server_listen(NULL, NULL);
128     if (error) {
129         ofp_fatal(error, "could not listen for vlog connections");
130     }
131
132     die_if_already_running();
133     daemonize();
134
135     for (;;) {
136         dp_run(dp);
137         dp_wait(dp);
138         poll_block();
139     }
140
141     return 0;
142 }
143
144 static void
145 add_ports(struct datapath *dp, char *port_list)
146 {
147     char *port, *save_ptr;
148
149     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
150      * can cause segfaults here:
151      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
152      * Using ",," instead of the obvious "," works around it. */
153     for (port = strtok_r(port_list, ",,", &save_ptr); port;
154          port = strtok_r(NULL, ",,", &save_ptr)) {
155         int error = dp_add_port(dp, port);
156         if (error) {
157             ofp_fatal(error, "failed to add port %s", port);
158         }
159     }
160 }
161
162 static void
163 parse_options(int argc, char *argv[])
164 {
165     enum {
166         OPT_MFR_DESC = UCHAR_MAX + 1,
167         OPT_HW_DESC,
168         OPT_SW_DESC,
169         OPT_SERIAL_NUM,
170         OPT_BOOTSTRAP_CA_CERT,
171         OPT_NO_LOCAL_PORT
172     };
173
174     static struct option long_options[] = {
175         {"interfaces",  required_argument, 0, 'i'},
176         {"local-port",  required_argument, 0, 'L'},
177         {"no-local-port", no_argument, 0, OPT_NO_LOCAL_PORT},
178         {"datapath-id", required_argument, 0, 'd'},
179         {"verbose",     optional_argument, 0, 'v'},
180         {"help",        no_argument, 0, 'h'},
181         {"version",     no_argument, 0, 'V'},
182         {"mfr-desc",    required_argument, 0, OPT_MFR_DESC},
183         {"hw-desc",     required_argument, 0, OPT_HW_DESC},
184         {"sw-desc",     required_argument, 0, OPT_SW_DESC},
185         {"serial_num",  required_argument, 0, OPT_SERIAL_NUM},
186         DAEMON_LONG_OPTIONS,
187 #ifdef HAVE_OPENSSL
188         VCONN_SSL_LONG_OPTIONS
189         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
190 #endif
191         {0, 0, 0, 0},
192     };
193     char *short_options = long_options_to_short_options(long_options);
194
195     for (;;) {
196         int indexptr;
197         int c;
198
199         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
200         if (c == -1) {
201             break;
202         }
203
204         switch (c) {
205         case 'd':
206             if (strlen(optarg) != 12
207                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
208                 ofp_fatal(0, "argument to -d or --datapath-id must be "
209                           "exactly 12 hex digits");
210             }
211             dpid = strtoll(optarg, NULL, 16);
212             if (!dpid) {
213                 ofp_fatal(0, "argument to -d or --datapath-id must "
214                           "be nonzero");
215             }
216             break;
217
218         case 'h':
219             usage();
220
221         case 'V':
222             printf("%s %s compiled "__DATE__" "__TIME__"\n",
223                    program_name, VERSION BUILDNR);
224             exit(EXIT_SUCCESS);
225
226         case 'v':
227             vlog_set_verbosity(optarg);
228             break;
229
230         case 'i':
231             if (!port_list) {
232                 port_list = optarg;
233             } else {
234                 port_list = xasprintf("%s,%s", port_list, optarg);
235             }
236             break;
237
238         case 'L':
239             local_port = optarg;
240             break;
241
242         case OPT_NO_LOCAL_PORT:
243             local_port = NULL;
244             break;
245
246         case OPT_MFR_DESC:
247             strncpy(mfr_desc, optarg, sizeof mfr_desc);
248             break;
249
250         case OPT_HW_DESC:
251             strncpy(hw_desc, optarg, sizeof hw_desc);
252             break;
253
254         case OPT_SW_DESC:
255             strncpy(sw_desc, optarg, sizeof sw_desc);
256             break;
257
258         case OPT_SERIAL_NUM:
259             strncpy(serial_num, optarg, sizeof serial_num);
260             break;
261
262         DAEMON_OPTION_HANDLERS
263
264 #ifdef HAVE_OPENSSL
265         VCONN_SSL_OPTION_HANDLERS
266
267         case OPT_BOOTSTRAP_CA_CERT:
268             vconn_ssl_set_ca_cert_file(optarg, true);
269             break;
270 #endif
271
272         case '?':
273             exit(EXIT_FAILURE);
274
275         default:
276             abort();
277         }
278     }
279     free(short_options);
280 }
281
282 static void
283 usage(void)
284 {
285     printf("%s: userspace OpenFlow datapath\n"
286            "usage: %s [OPTIONS] LISTEN...\n"
287            "where LISTEN is a passive OpenFlow connection method on which\n"
288            "to listen for incoming connections from the secure channel.\n",
289            program_name, program_name);
290     vconn_usage(false, true, false);
291     printf("\nConfiguration options:\n"
292            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
293            "                          add specified initial switch ports\n"
294            "  -L, --local-port=NETDEV set network device for local port\n"
295            "  --no-local-port         disable local port\n"
296            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
297            "                          (ID must consist of 12 hex digits)\n"
298            "\nOther options:\n"
299            "  -D, --detach            run in background as daemon\n"
300            "  -P, --pidfile[=FILE]    create pidfile (default: %s/udatapath.pid)\n"
301            "  -f, --force             with -P, start even if already running\n"
302            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
303            "  -v, --verbose           set maximum verbosity level\n"
304            "  -h, --help              display this help message\n"
305            "  -V, --version           display version information\n",
306         ofp_rundir);
307     exit(EXIT_SUCCESS);
308 }