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