1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "command-line.h"
29 #include "controller.h"
33 #include "poll-loop.h"
37 #include "vconn-ssl.h"
38 #include "vlog-socket.h"
40 #define THIS_MODULE VLM_switch
43 static void parse_options(int argc, char *argv[]);
44 static void usage(void) NO_RETURN;
46 static bool reliable = true;
47 static struct datapath *dp;
48 static uint64_t dpid = UINT64_MAX;
49 static char *port_list;
51 static void add_ports(struct datapath *dp, char *port_list);
54 main(int argc, char *argv[])
56 struct controller_connection cc;
59 set_program_name(argv[0]);
60 register_fault_handlers();
62 parse_options(argc, argv);
64 if (argc - optind != 1) {
65 fatal(0, "missing controller argument; use --help for usage");
68 controller_init(&cc, argv[optind], reliable);
69 error = dp_new(&dp, dpid, &cc);
71 fatal(error, "could not create datapath");
74 add_ports(dp, port_list);
77 error = vlog_server_listen(NULL, NULL);
79 fatal(error, "could not listen for vlog connections");
83 controller_run(&cc, dp);
94 add_ports(struct datapath *dp, char *port_list)
96 char *port, *save_ptr;
98 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
99 * can cause segfaults here:
100 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
101 * Using ",," instead of the obvious "," works around it. */
102 for (port = strtok_r(port_list, ",,", &save_ptr); port;
103 port = strtok_r(NULL, ",,", &save_ptr)) {
104 int error = dp_add_port(dp, port);
106 fatal(error, "failed to add port %s", port);
112 parse_options(int argc, char *argv[])
114 static struct option long_options[] = {
115 {"interfaces", required_argument, 0, 'i'},
116 {"unreliable", no_argument, 0, 'u'},
117 {"datapath-id", required_argument, 0, 'd'},
118 {"verbose", optional_argument, 0, 'v'},
119 {"help", no_argument, 0, 'h'},
120 {"version", no_argument, 0, 'V'},
122 {"private-key", required_argument, 0, 'p'},
123 {"certificate", required_argument, 0, 'c'},
124 {"ca-cert", required_argument, 0, 'C'},
128 char *short_options = long_options_to_short_options(long_options);
134 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
145 if (strlen(optarg) != 12
146 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
147 fatal(0, "argument to -d or --datapath-id must be "
148 "exactly 12 hex digits");
150 dpid = strtoll(optarg, NULL, 16);
152 fatal(0, "argument to -d or --datapath-id must be nonzero");
160 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
164 vlog_set_verbosity(optarg);
171 port_list = xasprintf("%s,%s", port_list, optarg);
177 vconn_ssl_set_private_key_file(optarg);
181 vconn_ssl_set_certificate_file(optarg);
185 vconn_ssl_set_ca_cert_file(optarg);
202 printf("%s: userspace OpenFlow switch\n"
203 "usage: %s [OPTIONS] CONTROLLER\n"
204 "CONTROLLER must be one of the following:\n"
205 " tcp:HOST[:PORT] PORT (default: %d) on remote TCP HOST\n",
206 program_name, program_name, OFP_TCP_PORT);
208 printf(" ssl:HOST[:PORT] SSL PORT (default: %d) on remote HOST\n"
209 "\nPKI configuration (required to use SSL):\n"
210 " -p, --private-key=FILE file with private key\n"
211 " -c, --certificate=FILE file with certificate for private key\n"
212 " -C, --ca-cert=FILE file with peer CA certificate\n",
216 " -i, --interfaces=NETDEV[,NETDEV]...\n"
217 " add specified initial switch ports\n"
218 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
219 " (ID must consist of 12 hex digits)\n"
220 " -u, --unreliable do not reconnect to controller\n"
221 " -v, --verbose set maximum verbosity level\n"
222 " -h, --help display this help message\n"
223 " -V, --version display version information\n");