Fix "make dist" by adding forgotten files to sources lists.
[sliver-openvswitch.git] / switch / switch.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 "vconn-ssl.h"
55 #include "vlog-socket.h"
56
57 #define THIS_MODULE VLM_switch
58 #include "vlog.h"
59
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;
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 const char *listen_pvconn_name;
72 static struct datapath *dp;
73 static uint64_t dpid = UINT64_MAX;
74 static char *port_list;
75
76 /* --max-backoff: Maximum interval between controller connection attempts, in
77  * seconds. */
78 static int max_backoff = 15;
79
80 static void add_ports(struct datapath *dp, char *port_list);
81
82 int
83 main(int argc, char *argv[])
84 {
85     struct rconn *rconn;
86     int error;
87
88     set_program_name(argv[0]);
89     register_fault_handlers();
90     time_init();
91     vlog_init();
92     parse_options(argc, argv);
93     signal(SIGPIPE, SIG_IGN);
94
95     if (argc - optind != 1) {
96         ofp_fatal(0, "missing controller argument; use --help for usage");
97     }
98
99     rconn = rconn_create(60, max_backoff);
100     error = rconn_connect(rconn, argv[optind]);
101     if (error == EAFNOSUPPORT) {
102         ofp_fatal(0, "no support for %s vconn", argv[optind]);
103     }
104     error = dp_new(&dp, dpid, rconn);
105     if (listen_pvconn_name) {
106         struct pvconn *listen_pvconn;
107         int retval;
108
109         retval = pvconn_open(listen_pvconn_name, &listen_pvconn);
110         if (retval && retval != EAGAIN) {
111             ofp_fatal(retval, "opening %s", listen_pvconn_name);
112         }
113         dp_add_listen_pvconn(dp, listen_pvconn);
114     }
115     if (error) {
116         ofp_fatal(error, "could not create datapath");
117     }
118     if (port_list) {
119         add_ports(dp, port_list); 
120     }
121
122     die_if_already_running();
123     daemonize();
124
125     error = vlog_server_listen(NULL, NULL);
126     if (error) {
127         ofp_fatal(error, "could not listen for vlog connections");
128     }
129
130     for (;;) {
131         dp_run(dp);
132         dp_wait(dp);
133         poll_block();
134     }
135
136     return 0;
137 }
138
139 static void
140 add_ports(struct datapath *dp, char *port_list)
141 {
142     char *port, *save_ptr;
143
144     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
145      * can cause segfaults here:
146      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
147      * Using ",," instead of the obvious "," works around it. */
148     for (port = strtok_r(port_list, ",,", &save_ptr); port;
149          port = strtok_r(NULL, ",,", &save_ptr)) {
150         int error = dp_add_port(dp, port);
151         if (error) {
152             ofp_fatal(error, "failed to add port %s", port);
153         }
154     }
155 }
156
157 static void
158 parse_options(int argc, char *argv[])
159 {
160     enum {
161         OPT_MAX_BACKOFF = UCHAR_MAX + 1,
162         OPT_MFR_DESC,
163         OPT_HW_DESC,
164         OPT_SW_DESC,
165         OPT_SERIAL_NUM,
166         OPT_BOOTSTRAP_CA_CERT
167     };
168
169     static struct option long_options[] = {
170         {"interfaces",  required_argument, 0, 'i'},
171         {"datapath-id", required_argument, 0, 'd'},
172         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
173         {"listen",      required_argument, 0, 'l'},
174         {"verbose",     optional_argument, 0, 'v'},
175         {"help",        no_argument, 0, 'h'},
176         {"version",     no_argument, 0, 'V'},
177         {"mfr-desc",    required_argument, 0, OPT_MFR_DESC},
178         {"hw-desc",     required_argument, 0, OPT_HW_DESC},
179         {"sw-desc",     required_argument, 0, OPT_SW_DESC},
180         {"serial_num",  required_argument, 0, OPT_SERIAL_NUM},
181         DAEMON_LONG_OPTIONS,
182 #ifdef HAVE_OPENSSL
183         VCONN_SSL_LONG_OPTIONS
184         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
185 #endif
186         {0, 0, 0, 0},
187     };
188     char *short_options = long_options_to_short_options(long_options);
189
190     for (;;) {
191         int indexptr;
192         int c;
193
194         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
195         if (c == -1) {
196             break;
197         }
198
199         switch (c) {
200         case 'd':
201             if (strlen(optarg) != 12
202                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
203                 ofp_fatal(0, "argument to -d or --datapath-id must be "
204                           "exactly 12 hex digits");
205             }
206             dpid = strtoll(optarg, NULL, 16);
207             if (!dpid) {
208                 ofp_fatal(0, "argument to -d or --datapath-id must "
209                           "be nonzero");
210             }
211             break;
212
213         case 'h':
214             usage();
215
216         case 'V':
217             printf("%s %s compiled "__DATE__" "__TIME__"\n",
218                    program_name, VERSION BUILDNR);
219             exit(EXIT_SUCCESS);
220
221         case 'v':
222             vlog_set_verbosity(optarg);
223             break;
224
225         case 'i':
226             if (!port_list) {
227                 port_list = optarg;
228             } else {
229                 port_list = xasprintf("%s,%s", port_list, optarg);
230             }
231             break;
232
233         case OPT_MAX_BACKOFF:
234             max_backoff = atoi(optarg);
235             if (max_backoff < 1) {
236                 ofp_fatal(0, "--max-backoff argument must be at least 1");
237             } else if (max_backoff > 3600) {
238                 max_backoff = 3600;
239             }
240             break;
241
242         case OPT_MFR_DESC:
243             strncpy(mfr_desc, optarg, sizeof mfr_desc);
244             break;
245
246         case OPT_HW_DESC:
247             strncpy(hw_desc, optarg, sizeof hw_desc);
248             break;
249
250         case OPT_SW_DESC:
251             strncpy(sw_desc, optarg, sizeof sw_desc);
252             break;
253
254         case OPT_SERIAL_NUM:
255             strncpy(serial_num, optarg, sizeof serial_num);
256             break;
257
258         case 'l':
259             if (listen_pvconn_name) {
260                 ofp_fatal(0, "-l or --listen may be only specified once");
261             }
262             listen_pvconn_name = optarg;
263             break;
264
265         DAEMON_OPTION_HANDLERS
266
267 #ifdef HAVE_OPENSSL
268         VCONN_SSL_OPTION_HANDLERS
269
270         case OPT_BOOTSTRAP_CA_CERT:
271             vconn_ssl_set_ca_cert_file(optarg, true);
272             break;
273 #endif
274
275         case '?':
276             exit(EXIT_FAILURE);
277
278         default:
279             abort();
280         }
281     }
282     free(short_options);
283 }
284
285 static void
286 usage(void)
287 {
288     printf("%s: userspace OpenFlow switch\n"
289            "usage: %s [OPTIONS] CONTROLLER\n"
290            "where CONTROLLER is an active OpenFlow connection method.\n",
291            program_name, program_name);
292     vconn_usage(true, true, true);
293     printf("\nConfiguration options:\n"
294            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
295            "                          add specified initial switch ports\n"
296            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
297            "                          (ID must consist of 12 hex digits)\n"
298            "  --max-backoff=SECS      max time between controller connection\n"
299            "                          attempts (default: 15 seconds)\n"
300            "  -l, --listen=METHOD     allow management connections on METHOD\n"
301            "                          (a passive OpenFlow connection method)\n");
302     daemon_usage();
303     vlog_usage();
304     printf("\nOther options:\n"
305            "  -h, --help              display this help message\n"
306            "  -V, --version           display version information\n");
307     exit(EXIT_SUCCESS);
308 }