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