9d307db9f538515288f76ea0cccc53857108734d
[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.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_vconn_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         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         fatal(0, "no support for %s vconn", argv[optind]);
103     }
104     error = dp_new(&dp, dpid, rconn);
105     if (listen_vconn_name) {
106         struct vconn *listen_vconn;
107         int retval;
108         
109         retval = vconn_open(listen_vconn_name, &listen_vconn);
110         if (retval && retval != EAGAIN) {
111             fatal(retval, "opening %s", listen_vconn_name);
112         }
113         if (!vconn_is_passive(listen_vconn)) {
114             fatal(0, "%s is not a passive vconn", listen_vconn_name);
115         }
116         dp_add_listen_vconn(dp, listen_vconn);
117     }
118     if (error) {
119         fatal(error, "could not create datapath");
120     }
121     if (port_list) {
122         add_ports(dp, port_list); 
123     }
124
125     error = vlog_server_listen(NULL, NULL);
126     if (error) {
127         fatal(error, "could not listen for vlog connections");
128     }
129
130     die_if_already_running();
131     daemonize();
132
133     for (;;) {
134         dp_run(dp);
135         dp_wait(dp);
136         poll_block();
137     }
138
139     return 0;
140 }
141
142 static void
143 add_ports(struct datapath *dp, char *port_list)
144 {
145     char *port, *save_ptr;
146
147     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
148      * can cause segfaults here:
149      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
150      * Using ",," instead of the obvious "," works around it. */
151     for (port = strtok_r(port_list, ",,", &save_ptr); port;
152          port = strtok_r(NULL, ",,", &save_ptr)) {
153         int error = dp_add_port(dp, port);
154         if (error) {
155             fatal(error, "failed to add port %s", port);
156         }
157     }
158 }
159
160 static void
161 parse_options(int argc, char *argv[])
162 {
163     enum {
164         OPT_MAX_BACKOFF = UCHAR_MAX + 1,
165         OPT_MFR_DESC,
166         OPT_HW_DESC,
167         OPT_SW_DESC,
168         OPT_SERIAL_NUM
169     };
170
171     static struct option long_options[] = {
172         {"interfaces",  required_argument, 0, 'i'},
173         {"datapath-id", required_argument, 0, 'd'},
174         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
175         {"listen",      required_argument, 0, 'l'},
176         {"detach",      no_argument, 0, 'D'},
177         {"pidfile",     optional_argument, 0, 'P'},
178         {"force",       no_argument, 0, 'f'},
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         VCONN_SSL_LONG_OPTIONS
187         {0, 0, 0, 0},
188     };
189     char *short_options = long_options_to_short_options(long_options);
190
191     for (;;) {
192         int indexptr;
193         int c;
194
195         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
196         if (c == -1) {
197             break;
198         }
199
200         switch (c) {
201         case 'd':
202             if (strlen(optarg) != 12
203                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
204                 fatal(0, "argument to -d or --datapath-id must be "
205                       "exactly 12 hex digits");
206             }
207             dpid = strtoll(optarg, NULL, 16);
208             if (!dpid) {
209                 fatal(0, "argument to -d or --datapath-id must be nonzero");
210             }
211             break;
212
213         case 'h':
214             usage();
215
216         case 'V':
217             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
218             exit(EXIT_SUCCESS);
219
220         case 'D':
221             set_detach();
222             break;
223
224         case 'P':
225             set_pidfile(optarg);
226             break;
227
228         case 'f':
229             ignore_existing_pidfile();
230             break;
231
232         case 'v':
233             vlog_set_verbosity(optarg);
234             break;
235
236         case 'i':
237             if (!port_list) {
238                 port_list = optarg;
239             } else {
240                 port_list = xasprintf("%s,%s", port_list, optarg);
241             }
242             break;
243
244         case OPT_MAX_BACKOFF:
245             max_backoff = atoi(optarg);
246             if (max_backoff < 1) {
247                 fatal(0, "--max-backoff argument must be at least 1");
248             } else if (max_backoff > 3600) {
249                 max_backoff = 3600;
250             }
251             break;
252
253         case OPT_MFR_DESC:
254             strncpy(mfr_desc, optarg, sizeof mfr_desc);
255             break;
256
257         case OPT_HW_DESC:
258             strncpy(hw_desc, optarg, sizeof hw_desc);
259             break;
260
261         case OPT_SW_DESC:
262             strncpy(sw_desc, optarg, sizeof sw_desc);
263             break;
264
265         case OPT_SERIAL_NUM:
266             strncpy(serial_num, optarg, sizeof serial_num);
267             break;
268
269         case 'l':
270             if (listen_vconn_name) {
271                 fatal(0, "-l or --listen may be only specified once");
272             }
273             listen_vconn_name = optarg;
274             break;
275
276         VCONN_SSL_OPTION_HANDLERS
277
278         case '?':
279             exit(EXIT_FAILURE);
280
281         default:
282             abort();
283         }
284     }
285     free(short_options);
286 }
287
288 static void
289 usage(void)
290 {
291     printf("%s: userspace OpenFlow switch\n"
292            "usage: %s [OPTIONS] CONTROLLER\n"
293            "where CONTROLLER is an active OpenFlow connection method.\n",
294            program_name, program_name);
295     vconn_usage(true, true);
296     printf("\nConfiguration options:\n"
297            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
298            "                          add specified initial switch ports\n"
299            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
300            "                          (ID must consist of 12 hex digits)\n"
301            "  --max-backoff=SECS      max time between controller connection\n"
302            "                          attempts (default: 15 seconds)\n"
303            "  -l, --listen=METHOD     allow management connections on METHOD\n"
304            "                          (a passive OpenFlow connection method)\n"
305            "\nOther options:\n"
306            "  -D, --detach            run in background as daemon\n"
307            "  -P, --pidfile[=FILE]    create pidfile (default: %s/switch.pid)\n"
308            "  -f, --force             with -P, start even if already running\n"
309            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
310            "  -v, --verbose           set maximum verbosity level\n"
311            "  -h, --help              display this help message\n"
312            "  -V, --version           display version information\n",
313         RUNDIR);
314     exit(EXIT_SUCCESS);
315 }