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 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";
68 static void parse_options(int argc, char *argv[]);
69 static void usage(void) NO_RETURN;
71 static const char *listen_pvconn_name;
72 static struct datapath *dp;
73 static uint64_t dpid = UINT64_MAX;
74 static char *port_list;
76 /* --max-backoff: Maximum interval between controller connection attempts, in
78 static int max_backoff = 15;
80 static void add_ports(struct datapath *dp, char *port_list);
83 main(int argc, char *argv[])
88 set_program_name(argv[0]);
89 register_fault_handlers();
92 parse_options(argc, argv);
93 signal(SIGPIPE, SIG_IGN);
95 if (argc - optind != 1) {
96 ofp_fatal(0, "missing controller argument; use --help for usage");
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]);
104 error = dp_new(&dp, dpid, rconn);
105 if (listen_pvconn_name) {
106 struct pvconn *listen_pvconn;
109 retval = pvconn_open(listen_pvconn_name, &listen_pvconn);
110 if (retval && retval != EAGAIN) {
111 ofp_fatal(retval, "opening %s", listen_pvconn_name);
113 dp_add_listen_pvconn(dp, listen_pvconn);
116 ofp_fatal(error, "could not create datapath");
119 add_ports(dp, port_list);
122 error = vlog_server_listen(NULL, NULL);
124 ofp_fatal(error, "could not listen for vlog connections");
127 die_if_already_running();
140 add_ports(struct datapath *dp, char *port_list)
142 char *port, *save_ptr;
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);
152 ofp_fatal(error, "failed to add port %s", port);
158 parse_options(int argc, char *argv[])
161 OPT_MAX_BACKOFF = UCHAR_MAX + 1,
166 OPT_BOOTSTRAP_CA_CERT
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},
183 VCONN_SSL_LONG_OPTIONS
184 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
188 char *short_options = long_options_to_short_options(long_options);
194 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
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");
206 dpid = strtoll(optarg, NULL, 16);
208 ofp_fatal(0, "argument to -d or --datapath-id must "
217 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
221 vlog_set_verbosity(optarg);
228 port_list = xasprintf("%s,%s", port_list, optarg);
232 case OPT_MAX_BACKOFF:
233 max_backoff = atoi(optarg);
234 if (max_backoff < 1) {
235 ofp_fatal(0, "--max-backoff argument must be at least 1");
236 } else if (max_backoff > 3600) {
242 strncpy(mfr_desc, optarg, sizeof mfr_desc);
246 strncpy(hw_desc, optarg, sizeof hw_desc);
250 strncpy(sw_desc, optarg, sizeof sw_desc);
254 strncpy(serial_num, optarg, sizeof serial_num);
258 if (listen_pvconn_name) {
259 ofp_fatal(0, "-l or --listen may be only specified once");
261 listen_pvconn_name = optarg;
264 DAEMON_OPTION_HANDLERS
267 VCONN_SSL_OPTION_HANDLERS
269 case OPT_BOOTSTRAP_CA_CERT:
270 vconn_ssl_set_ca_cert_file(optarg, true);
287 printf("%s: userspace OpenFlow switch\n"
288 "usage: %s [OPTIONS] CONTROLLER\n"
289 "where CONTROLLER is an active OpenFlow connection method.\n",
290 program_name, program_name);
291 vconn_usage(true, true, true);
292 printf("\nConfiguration options:\n"
293 " -i, --interfaces=NETDEV[,NETDEV]...\n"
294 " add specified initial switch ports\n"
295 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
296 " (ID must consist of 12 hex digits)\n"
297 " --max-backoff=SECS max time between controller connection\n"
298 " attempts (default: 15 seconds)\n"
299 " -l, --listen=METHOD allow management connections on METHOD\n"
300 " (a passive OpenFlow connection method)\n");
303 printf("\nOther options:\n"
304 " -h, --help display this help message\n"
305 " -V, --version display version information\n");