]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-controller.c
Merge citrix branch into master.
[mirror_ovs.git] / utilities / ovs-controller.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "command-line.h"
27 #include "compiler.h"
28 #include "daemon.h"
29 #include "fault.h"
30 #include "learning-switch.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
34 #include "rconn.h"
35 #include "timeval.h"
36 #include "unixctl.h"
37 #include "util.h"
38 #include "vconn-ssl.h"
39 #include "vconn.h"
40
41 #include "vlog.h"
42 #define THIS_MODULE VLM_controller
43
44 #define MAX_SWITCHES 16
45 #define MAX_LISTENERS 16
46
47 struct switch_ {
48 struct lswitch *lswitch;
49 struct rconn *rconn;
50 };
51
52 /* Learn the ports on which MAC addresses appear? */
53 static bool learn_macs = true;
54
55 /* Set up flows? (If not, every packet is processed at the controller.) */
56 static bool set_up_flows = true;
57
58 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
59 static int max_idle = 60;
60
61 /* --mute: If true, accept connections from switches but do not reply to any
62 * of their messages (for debugging fail-open mode). */
63 static bool mute = false;
64
65 static int do_switching(struct switch_ *);
66 static void new_switch(struct switch_ *, struct vconn *, const char *name);
67 static void parse_options(int argc, char *argv[]);
68 static void usage(void) NO_RETURN;
69
70 int
71 main(int argc, char *argv[])
72 {
73 struct unixctl_server *unixctl;
74 struct switch_ switches[MAX_SWITCHES];
75 struct pvconn *listeners[MAX_LISTENERS];
76 int n_switches, n_listeners;
77 int retval;
78 int i;
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 ovs_fatal(0, "at least one vconn argument required; "
89 "use --help for usage");
90 }
91
92 n_switches = n_listeners = 0;
93 for (i = optind; i < argc; i++) {
94 const char *name = argv[i];
95 struct vconn *vconn;
96 int retval;
97
98 retval = vconn_open(name, OFP_VERSION, &vconn);
99 if (!retval) {
100 if (n_switches >= MAX_SWITCHES) {
101 ovs_fatal(0, "max %d switch connections", n_switches);
102 }
103 new_switch(&switches[n_switches++], vconn, name);
104 continue;
105 } else if (retval == EAFNOSUPPORT) {
106 struct pvconn *pvconn;
107 retval = pvconn_open(name, &pvconn);
108 if (!retval) {
109 if (n_listeners >= MAX_LISTENERS) {
110 ovs_fatal(0, "max %d passive connections", n_listeners);
111 }
112 listeners[n_listeners++] = pvconn;
113 }
114 }
115 if (retval) {
116 VLOG_ERR("%s: connect: %s", name, strerror(retval));
117 }
118 }
119 if (n_switches == 0 && n_listeners == 0) {
120 ovs_fatal(0, "no active or passive switch connections");
121 }
122
123 die_if_already_running();
124 daemonize();
125
126 retval = unixctl_server_create(NULL, &unixctl);
127 if (retval) {
128 ovs_fatal(retval, "Could not listen for unixctl connections");
129 }
130
131 while (n_switches > 0 || n_listeners > 0) {
132 int iteration;
133 int i;
134
135 /* Accept connections on listening vconns. */
136 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
137 struct vconn *new_vconn;
138 int retval;
139
140 retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
141 if (!retval || retval == EAGAIN) {
142 if (!retval) {
143 new_switch(&switches[n_switches++], new_vconn, "tcp");
144 }
145 i++;
146 } else {
147 pvconn_close(listeners[i]);
148 listeners[i] = listeners[--n_listeners];
149 }
150 }
151
152 /* Do some switching work. Limit the number of iterations so that
153 * callbacks registered with the poll loop don't starve. */
154 for (iteration = 0; iteration < 50; iteration++) {
155 bool progress = false;
156 for (i = 0; i < n_switches; ) {
157 struct switch_ *this = &switches[i];
158 int retval = do_switching(this);
159 if (!retval || retval == EAGAIN) {
160 if (!retval) {
161 progress = true;
162 }
163 i++;
164 } else {
165 rconn_destroy(this->rconn);
166 lswitch_destroy(this->lswitch);
167 switches[i] = switches[--n_switches];
168 }
169 }
170 if (!progress) {
171 break;
172 }
173 }
174 for (i = 0; i < n_switches; i++) {
175 struct switch_ *this = &switches[i];
176 lswitch_run(this->lswitch, this->rconn);
177 }
178
179 unixctl_server_run(unixctl);
180
181 /* Wait for something to happen. */
182 if (n_switches < MAX_SWITCHES) {
183 for (i = 0; i < n_listeners; i++) {
184 pvconn_wait(listeners[i]);
185 }
186 }
187 for (i = 0; i < n_switches; i++) {
188 struct switch_ *sw = &switches[i];
189 rconn_run_wait(sw->rconn);
190 rconn_recv_wait(sw->rconn);
191 lswitch_wait(sw->lswitch);
192 }
193 unixctl_server_wait(unixctl);
194 poll_block();
195 }
196
197 return 0;
198 }
199
200 static void
201 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
202 {
203 sw->rconn = rconn_new_from_vconn(name, vconn);
204 sw->lswitch = lswitch_create(sw->rconn, learn_macs,
205 set_up_flows ? max_idle : -1);
206 }
207
208 static int
209 do_switching(struct switch_ *sw)
210 {
211 unsigned int packets_sent;
212 struct ofpbuf *msg;
213
214 packets_sent = rconn_packets_sent(sw->rconn);
215
216 msg = rconn_recv(sw->rconn);
217 if (msg) {
218 if (!mute) {
219 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
220 }
221 ofpbuf_delete(msg);
222 }
223 rconn_run(sw->rconn);
224
225 return (!rconn_is_alive(sw->rconn) ? EOF
226 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
227 : EAGAIN);
228 }
229
230 static void
231 parse_options(int argc, char *argv[])
232 {
233 enum {
234 OPT_MAX_IDLE = UCHAR_MAX + 1,
235 OPT_PEER_CA_CERT,
236 OPT_MUTE,
237 VLOG_OPTION_ENUMS
238 };
239 static struct option long_options[] = {
240 {"hub", no_argument, 0, 'H'},
241 {"noflow", no_argument, 0, 'n'},
242 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
243 {"mute", no_argument, 0, OPT_MUTE},
244 {"help", no_argument, 0, 'h'},
245 {"version", no_argument, 0, 'V'},
246 DAEMON_LONG_OPTIONS,
247 VLOG_LONG_OPTIONS,
248 #ifdef HAVE_OPENSSL
249 VCONN_SSL_LONG_OPTIONS
250 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
251 #endif
252 {0, 0, 0, 0},
253 };
254 char *short_options = long_options_to_short_options(long_options);
255
256 for (;;) {
257 int indexptr;
258 int c;
259
260 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
261 if (c == -1) {
262 break;
263 }
264
265 switch (c) {
266 case 'H':
267 learn_macs = false;
268 break;
269
270 case 'n':
271 set_up_flows = false;
272 break;
273
274 case OPT_MUTE:
275 mute = true;
276 break;
277
278 case OPT_MAX_IDLE:
279 if (!strcmp(optarg, "permanent")) {
280 max_idle = OFP_FLOW_PERMANENT;
281 } else {
282 max_idle = atoi(optarg);
283 if (max_idle < 1 || max_idle > 65535) {
284 ovs_fatal(0, "--max-idle argument must be between 1 and "
285 "65535 or the word 'permanent'");
286 }
287 }
288 break;
289
290 case 'h':
291 usage();
292
293 case 'V':
294 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
295 exit(EXIT_SUCCESS);
296
297 VLOG_OPTION_HANDLERS
298 DAEMON_OPTION_HANDLERS
299
300 #ifdef HAVE_OPENSSL
301 VCONN_SSL_OPTION_HANDLERS
302
303 case OPT_PEER_CA_CERT:
304 vconn_ssl_set_peer_ca_cert_file(optarg);
305 break;
306 #endif
307
308 case '?':
309 exit(EXIT_FAILURE);
310
311 default:
312 abort();
313 }
314 }
315 free(short_options);
316 }
317
318 static void
319 usage(void)
320 {
321 printf("%s: OpenFlow controller\n"
322 "usage: %s [OPTIONS] METHOD\n"
323 "where METHOD is any OpenFlow connection method.\n",
324 program_name, program_name);
325 vconn_usage(true, true, false);
326 daemon_usage();
327 vlog_usage();
328 printf("\nOther options:\n"
329 " -H, --hub act as hub instead of learning switch\n"
330 " -n, --noflow pass traffic, but don't add flows\n"
331 " --max-idle=SECS max idle time for new flows\n"
332 " -h, --help display this help message\n"
333 " -V, --version display version information\n");
334 exit(EXIT_SUCCESS);
335 }