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