Move Autoconf's macro definitions into config.h.
[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 <config.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <limits.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "command-line.h"
43 #include "daemon.h"
44 #include "datapath.h"
45 #include "fault.h"
46 #include "openflow.h"
47 #include "poll-loop.h"
48 #include "queue.h"
49 #include "util.h"
50 #include "rconn.h"
51 #include "vconn.h"
52 #include "vconn-ssl.h"
53 #include "vlog-socket.h"
54
55 #define THIS_MODULE VLM_switch
56 #include "vlog.h"
57
58 static void parse_options(int argc, char *argv[]);
59 static void usage(void) NO_RETURN;
60
61 static const char *listen_vconn_name;
62 static struct datapath *dp;
63 static uint64_t dpid = UINT64_MAX;
64 static char *port_list;
65
66 /* --max-backoff: Maximum interval between controller connection attempts, in
67  * seconds. */
68 static int max_backoff = 15;
69
70 static void add_ports(struct datapath *dp, char *port_list);
71
72 int
73 main(int argc, char *argv[])
74 {
75     struct rconn *rconn;
76     int error;
77
78     set_program_name(argv[0]);
79     register_fault_handlers();
80     vlog_init();
81     parse_options(argc, argv);
82
83     if (argc - optind != 1) {
84         fatal(0, "missing controller argument; use --help for usage");
85     }
86
87     rconn = rconn_create(128, 60, max_backoff);
88     error = rconn_connect(rconn, argv[optind]);
89     if (error == EAFNOSUPPORT) {
90         fatal(0, "no support for %s vconn", argv[optind]);
91     }
92     error = dp_new(&dp, dpid, rconn);
93     if (listen_vconn_name) {
94         struct vconn *listen_vconn;
95         int retval;
96         
97         retval = vconn_open(listen_vconn_name, &listen_vconn);
98         if (retval && retval != EAGAIN) {
99             fatal(retval, "opening %s", listen_vconn_name);
100         }
101         if (!vconn_is_passive(listen_vconn)) {
102             fatal(0, "%s is not a passive vconn", listen_vconn_name);
103         }
104         dp_add_listen_vconn(dp, listen_vconn);
105     }
106     if (error) {
107         fatal(error, "could not create datapath");
108     }
109     if (port_list) {
110         add_ports(dp, port_list); 
111     }
112
113     error = vlog_server_listen(NULL, NULL);
114     if (error) {
115         fatal(error, "could not listen for vlog connections");
116     }
117
118     daemonize();
119
120     for (;;) {
121         dp_run(dp);
122         dp_wait(dp);
123         poll_block();
124     }
125
126     return 0;
127 }
128
129 static void
130 add_ports(struct datapath *dp, char *port_list)
131 {
132     char *port, *save_ptr;
133
134     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
135      * can cause segfaults here:
136      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
137      * Using ",," instead of the obvious "," works around it. */
138     for (port = strtok_r(port_list, ",,", &save_ptr); port;
139          port = strtok_r(NULL, ",,", &save_ptr)) {
140         int error = dp_add_port(dp, port);
141         if (error) {
142             fatal(error, "failed to add port %s", port);
143         }
144     }
145 }
146
147 static void
148 parse_options(int argc, char *argv[])
149 {
150     enum {
151         OPT_MAX_BACKOFF = UCHAR_MAX + 1
152     };
153
154     static struct option long_options[] = {
155         {"interfaces",  required_argument, 0, 'i'},
156         {"datapath-id", required_argument, 0, 'd'},
157         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
158         {"listen",      required_argument, 0, 'l'},
159         {"detach",      no_argument, 0, 'D'},
160         {"pidfile",     optional_argument, 0, 'P'},
161         {"verbose",     optional_argument, 0, 'v'},
162         {"help",        no_argument, 0, 'h'},
163         {"version",     no_argument, 0, 'V'},
164         VCONN_SSL_LONG_OPTIONS
165         {0, 0, 0, 0},
166     };
167     char *short_options = long_options_to_short_options(long_options);
168
169     for (;;) {
170         int indexptr;
171         int c;
172
173         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
174         if (c == -1) {
175             break;
176         }
177
178         switch (c) {
179         case 'd':
180             if (strlen(optarg) != 12
181                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
182                 fatal(0, "argument to -d or --datapath-id must be "
183                       "exactly 12 hex digits");
184             }
185             dpid = strtoll(optarg, NULL, 16);
186             if (!dpid) {
187                 fatal(0, "argument to -d or --datapath-id must be nonzero");
188             }
189             break;
190
191         case 'h':
192             usage();
193
194         case 'V':
195             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
196             exit(EXIT_SUCCESS);
197
198         case 'D':
199             set_detach();
200             break;
201
202         case 'P':
203             set_pidfile(optarg ? optarg : "switch.pid");
204             break;
205
206         case 'v':
207             vlog_set_verbosity(optarg);
208             break;
209
210         case 'i':
211             if (!port_list) {
212                 port_list = optarg;
213             } else {
214                 port_list = xasprintf("%s,%s", port_list, optarg);
215             }
216             break;
217
218         case OPT_MAX_BACKOFF:
219             max_backoff = atoi(optarg);
220             if (max_backoff < 1) {
221                 fatal(0, "--max-backoff argument must be at least 1");
222             } else if (max_backoff > 3600) {
223                 max_backoff = 3600;
224             }
225             break;
226
227         case 'l':
228             if (listen_vconn_name) {
229                 fatal(0, "-l or --listen may be only specified once");
230             }
231             listen_vconn_name = optarg;
232             break;
233
234         VCONN_SSL_OPTION_HANDLERS
235
236         case '?':
237             exit(EXIT_FAILURE);
238
239         default:
240             abort();
241         }
242     }
243     free(short_options);
244 }
245
246 static void
247 usage(void)
248 {
249     printf("%s: userspace OpenFlow switch\n"
250            "usage: %s [OPTIONS] CONTROLLER\n"
251            "where CONTROLLER is an active OpenFlow connection method.\n",
252            program_name, program_name);
253     vconn_usage(true, true);
254     printf("\nConfiguration options:\n"
255            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
256            "                          add specified initial switch ports\n"
257            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
258            "                          (ID must consist of 12 hex digits)\n"
259            "  --max-backoff=SECS      max time between controller connection\n"
260            "                          attempts (default: 15 seconds)\n"
261            "  -l, --listen=METHOD     allow management connections on METHOD\n"
262            "                          (a passive OpenFlow connection method)\n"
263            "\nOther options:\n"
264            "  -D, --detach            run in background as daemon\n"
265            "  -P, --pidfile[=FILE]    create pidfile (default: %s/switch.pid)\n"
266            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
267            "  -v, --verbose           set maximum verbosity level\n"
268            "  -h, --help              display this help message\n"
269            "  -V, --version           display version information\n",
270         RUNDIR);
271     exit(EXIT_SUCCESS);
272 }