Better abstract controller_connection.
[openvswitch] / 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 <errno.h>
35 #include <getopt.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "command-line.h"
41 #include "controller.h"
42 #include "datapath.h"
43 #include "fault.h"
44 #include "forward.h"
45 #include "openflow.h"
46 #include "poll-loop.h"
47 #include "queue.h"
48 #include "util.h"
49 #include "vconn.h"
50 #include "vconn-ssl.h"
51 #include "vlog-socket.h"
52
53 #define THIS_MODULE VLM_switch
54 #include "vlog.h"
55
56 static void parse_options(int argc, char *argv[]);
57 static void usage(void) NO_RETURN;
58
59 static bool reliable = true;
60 static struct datapath *dp;
61 static uint64_t dpid = UINT64_MAX;
62 static char *port_list;
63
64 static void add_ports(struct datapath *dp, char *port_list);
65
66 int
67 main(int argc, char *argv[])
68 {
69     struct controller_connection *cc;
70     int error;
71
72     set_program_name(argv[0]);
73     register_fault_handlers();
74     vlog_init();
75     parse_options(argc, argv);
76
77     if (argc - optind != 1) {
78         fatal(0, "missing controller argument; use --help for usage");
79     }
80
81     cc = controller_new(argv[optind], reliable);
82     error = dp_new(&dp, dpid, cc);
83     if (error) {
84         fatal(error, "could not create datapath");
85     }
86     if (port_list) {
87         add_ports(dp, port_list); 
88     }
89
90     error = vlog_server_listen(NULL, NULL);
91     if (error) {
92         fatal(error, "could not listen for vlog connections");
93     }
94
95     for (;;) {
96         dp_run(dp);
97         fwd_run(dp);
98         controller_run(cc);
99         
100         dp_wait(dp);
101         fwd_run_wait(dp);
102         controller_run_wait(cc);
103         poll_block();
104     }
105
106     return 0;
107 }
108
109 static void
110 add_ports(struct datapath *dp, char *port_list)
111 {
112     char *port, *save_ptr;
113
114     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
115      * can cause segfaults here:
116      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
117      * Using ",," instead of the obvious "," works around it. */
118     for (port = strtok_r(port_list, ",,", &save_ptr); port;
119          port = strtok_r(NULL, ",,", &save_ptr)) {
120         int error = dp_add_port(dp, port);
121         if (error) {
122             fatal(error, "failed to add port %s", port);
123         }
124     }
125 }
126
127 static void
128 parse_options(int argc, char *argv[])
129 {
130     static struct option long_options[] = {
131         {"interfaces",  required_argument, 0, 'i'},
132         {"unreliable",  no_argument, 0, 'u'},
133         {"datapath-id", required_argument, 0, 'd'},
134         {"verbose",     optional_argument, 0, 'v'},
135         {"help",        no_argument, 0, 'h'},
136         {"version",     no_argument, 0, 'V'},
137 #ifdef HAVE_OPENSSL
138         {"private-key", required_argument, 0, 'p'},
139         {"certificate", required_argument, 0, 'c'},
140         {"ca-cert",     required_argument, 0, 'C'},
141 #endif
142         {0, 0, 0, 0},
143     };
144     char *short_options = long_options_to_short_options(long_options);
145
146     for (;;) {
147         int indexptr;
148         int c;
149
150         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
151         if (c == -1) {
152             break;
153         }
154
155         switch (c) {
156         case 'u':
157             reliable = false;
158             break;
159
160         case 'd':
161             if (strlen(optarg) != 12
162                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
163                 fatal(0, "argument to -d or --datapath-id must be "
164                       "exactly 12 hex digits");
165             }
166             dpid = strtoll(optarg, NULL, 16);
167             if (!dpid) {
168                 fatal(0, "argument to -d or --datapath-id must be nonzero");
169             }
170             break;
171
172         case 'h':
173             usage();
174
175         case 'V':
176             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
177             exit(EXIT_SUCCESS);
178
179         case 'v':
180             vlog_set_verbosity(optarg);
181             break;
182
183         case 'i':
184             if (!port_list) {
185                 port_list = optarg;
186             } else {
187                 port_list = xasprintf("%s,%s", port_list, optarg);
188             }
189             break;
190
191 #ifdef HAVE_OPENSSL
192         case 'p':
193             vconn_ssl_set_private_key_file(optarg);
194             break;
195
196         case 'c':
197             vconn_ssl_set_certificate_file(optarg);
198             break;
199
200         case 'C':
201             vconn_ssl_set_ca_cert_file(optarg);
202             break;
203 #endif
204
205         case '?':
206             exit(EXIT_FAILURE);
207
208         default:
209             abort();
210         }
211     }
212     free(short_options);
213 }
214
215 static void
216 usage(void)
217 {
218     printf("%s: userspace OpenFlow switch\n"
219            "usage: %s [OPTIONS] CONTROLLER\n"
220            "CONTROLLER must be one of the following:\n"
221            "  tcp:HOST[:PORT]         PORT (default: %d) on remote TCP HOST\n",
222            program_name, program_name, OFP_TCP_PORT);
223 #ifdef HAVE_OPENSSL
224     printf("  ssl:HOST[:PORT]         SSL PORT (default: %d) on remote HOST\n"
225            "\nPKI configuration (required to use SSL):\n"
226            "  -p, --private-key=FILE  file with private key\n"
227            "  -c, --certificate=FILE  file with certificate for private key\n"
228            "  -C, --ca-cert=FILE      file with peer CA certificate\n",
229            OFP_SSL_PORT);
230 #endif
231     printf("Options:\n"
232            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
233            "                          add specified initial switch ports\n"
234            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
235            "                          (ID must consist of 12 hex digits)\n"
236            "  -u, --unreliable        do not reconnect to controller\n"
237            "  -v, --verbose           set maximum verbosity level\n"
238            "  -h, --help              display this help message\n"
239            "  -V, --version           display version information\n");
240     exit(EXIT_SUCCESS);
241 }