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