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