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