1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
43 #include "command-line.h"
48 #include "poll-loop.h"
54 #include "vconn-ssl.h"
55 #include "vlog-socket.h"
57 #define THIS_MODULE VLM_switch
61 /* Strings to describe the manufacturer, hardware, and software. This data
62 * is queriable through the version stats message. */
63 char mfr_desc[VERSION_STR_LEN] = "Nicira Networks";
64 char hw_desc[VERSION_STR_LEN] = "Reference User-Space Switch";
65 char sw_desc[VERSION_STR_LEN] = VERSION;
67 static void parse_options(int argc, char *argv[]);
68 static void usage(void) NO_RETURN;
70 static const char *listen_vconn_name;
71 static struct datapath *dp;
72 static uint64_t dpid = UINT64_MAX;
73 static char *port_list;
75 /* --max-backoff: Maximum interval between controller connection attempts, in
77 static int max_backoff = 15;
79 static void add_ports(struct datapath *dp, char *port_list);
82 main(int argc, char *argv[])
87 set_program_name(argv[0]);
88 register_fault_handlers();
91 parse_options(argc, argv);
92 signal(SIGPIPE, SIG_IGN);
94 if (argc - optind != 1) {
95 fatal(0, "missing controller argument; use --help for usage");
98 rconn = rconn_create(60, max_backoff);
99 error = rconn_connect(rconn, argv[optind]);
100 if (error == EAFNOSUPPORT) {
101 fatal(0, "no support for %s vconn", argv[optind]);
103 error = dp_new(&dp, dpid, rconn);
104 if (listen_vconn_name) {
105 struct vconn *listen_vconn;
108 retval = vconn_open(listen_vconn_name, &listen_vconn);
109 if (retval && retval != EAGAIN) {
110 fatal(retval, "opening %s", listen_vconn_name);
112 if (!vconn_is_passive(listen_vconn)) {
113 fatal(0, "%s is not a passive vconn", listen_vconn_name);
115 dp_add_listen_vconn(dp, listen_vconn);
118 fatal(error, "could not create datapath");
121 add_ports(dp, port_list);
124 error = vlog_server_listen(NULL, NULL);
126 fatal(error, "could not listen for vlog connections");
141 add_ports(struct datapath *dp, char *port_list)
143 char *port, *save_ptr;
145 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
146 * can cause segfaults here:
147 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
148 * Using ",," instead of the obvious "," works around it. */
149 for (port = strtok_r(port_list, ",,", &save_ptr); port;
150 port = strtok_r(NULL, ",,", &save_ptr)) {
151 int error = dp_add_port(dp, port);
153 fatal(error, "failed to add port %s", port);
159 parse_options(int argc, char *argv[])
162 OPT_MAX_BACKOFF = UCHAR_MAX + 1,
168 static struct option long_options[] = {
169 {"interfaces", required_argument, 0, 'i'},
170 {"datapath-id", required_argument, 0, 'd'},
171 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
172 {"listen", required_argument, 0, 'l'},
173 {"detach", no_argument, 0, 'D'},
174 {"pidfile", optional_argument, 0, 'P'},
175 {"verbose", optional_argument, 0, 'v'},
176 {"help", no_argument, 0, 'h'},
177 {"version", no_argument, 0, 'V'},
178 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
179 {"hw-desc", required_argument, 0, OPT_HW_DESC},
180 {"sw-desc", required_argument, 0, OPT_SW_DESC},
181 VCONN_SSL_LONG_OPTIONS
184 char *short_options = long_options_to_short_options(long_options);
190 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
197 if (strlen(optarg) != 12
198 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
199 fatal(0, "argument to -d or --datapath-id must be "
200 "exactly 12 hex digits");
202 dpid = strtoll(optarg, NULL, 16);
204 fatal(0, "argument to -d or --datapath-id must be nonzero");
212 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
224 vlog_set_verbosity(optarg);
231 port_list = xasprintf("%s,%s", port_list, optarg);
235 case OPT_MAX_BACKOFF:
236 max_backoff = atoi(optarg);
237 if (max_backoff < 1) {
238 fatal(0, "--max-backoff argument must be at least 1");
239 } else if (max_backoff > 3600) {
245 strncpy(mfr_desc, optarg, sizeof mfr_desc);
249 strncpy(hw_desc, optarg, sizeof hw_desc);
253 strncpy(sw_desc, optarg, sizeof sw_desc);
257 if (listen_vconn_name) {
258 fatal(0, "-l or --listen may be only specified once");
260 listen_vconn_name = optarg;
263 VCONN_SSL_OPTION_HANDLERS
278 printf("%s: userspace OpenFlow switch\n"
279 "usage: %s [OPTIONS] CONTROLLER\n"
280 "where CONTROLLER is an active OpenFlow connection method.\n",
281 program_name, program_name);
282 vconn_usage(true, true);
283 printf("\nConfiguration options:\n"
284 " -i, --interfaces=NETDEV[,NETDEV]...\n"
285 " add specified initial switch ports\n"
286 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
287 " (ID must consist of 12 hex digits)\n"
288 " --max-backoff=SECS max time between controller connection\n"
289 " attempts (default: 15 seconds)\n"
290 " -l, --listen=METHOD allow management connections on METHOD\n"
291 " (a passive OpenFlow connection method)\n"
293 " -D, --detach run in background as daemon\n"
294 " -P, --pidfile[=FILE] create pidfile (default: %s/switch.pid)\n"
295 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
296 " -v, --verbose set maximum verbosity level\n"
297 " -h, --help display this help message\n"
298 " -V, --version display version information\n",