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