]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-controller.c
ovs-vsctl: Update --help message.
[ovs.git] / utilities / ovs-controller.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 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 "learning-switch.h"
30 #include "ofpbuf.h"
31 #include "openflow/openflow.h"
32 #include "poll-loop.h"
33 #include "rconn.h"
34 #include "stream-ssl.h"
35 #include "timeval.h"
36 #include "unixctl.h"
37 #include "util.h"
38 #include "vconn.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_controller
42
43 #define MAX_SWITCHES 16
44 #define MAX_LISTENERS 16
45
46 struct switch_ {
47 struct lswitch *lswitch;
48 struct rconn *rconn;
49 };
50
51 /* Learn the ports on which MAC addresses appear? */
52 static bool learn_macs = true;
53
54 /* Set up flows? (If not, every packet is processed at the controller.) */
55 static bool set_up_flows = true;
56
57 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
58 static bool action_normal = false;
59
60 /* -w, --wildcard: Set up exact match or wildcard flow entries? */
61 static bool exact_flows = true;
62
63 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
64 static int max_idle = 60;
65
66 /* --mute: If true, accept connections from switches but do not reply to any
67 * of their messages (for debugging fail-open mode). */
68 static bool mute = false;
69
70 static int do_switching(struct switch_ *);
71 static void new_switch(struct switch_ *, struct vconn *, const char *name);
72 static void parse_options(int argc, char *argv[]);
73 static void usage(void) NO_RETURN;
74
75 int
76 main(int argc, char *argv[])
77 {
78 struct unixctl_server *unixctl;
79 struct switch_ switches[MAX_SWITCHES];
80 struct pvconn *listeners[MAX_LISTENERS];
81 int n_switches, n_listeners;
82 int retval;
83 int i;
84
85 proctitle_init(argc, argv);
86 set_program_name(argv[0]);
87 time_init();
88 vlog_init();
89 parse_options(argc, argv);
90 signal(SIGPIPE, SIG_IGN);
91
92 if (argc - optind < 1) {
93 ovs_fatal(0, "at least one vconn argument required; "
94 "use --help for usage");
95 }
96
97 n_switches = n_listeners = 0;
98 for (i = optind; i < argc; i++) {
99 const char *name = argv[i];
100 struct vconn *vconn;
101 int retval;
102
103 retval = vconn_open(name, OFP_VERSION, &vconn);
104 if (!retval) {
105 if (n_switches >= MAX_SWITCHES) {
106 ovs_fatal(0, "max %d switch connections", n_switches);
107 }
108 new_switch(&switches[n_switches++], vconn, name);
109 continue;
110 } else if (retval == EAFNOSUPPORT) {
111 struct pvconn *pvconn;
112 retval = pvconn_open(name, &pvconn);
113 if (!retval) {
114 if (n_listeners >= MAX_LISTENERS) {
115 ovs_fatal(0, "max %d passive connections", n_listeners);
116 }
117 listeners[n_listeners++] = pvconn;
118 }
119 }
120 if (retval) {
121 VLOG_ERR("%s: connect: %s", name, strerror(retval));
122 }
123 }
124 if (n_switches == 0 && n_listeners == 0) {
125 ovs_fatal(0, "no active or passive switch connections");
126 }
127
128 die_if_already_running();
129 daemonize_start();
130
131 retval = unixctl_server_create(NULL, &unixctl);
132 if (retval) {
133 exit(EXIT_FAILURE);
134 }
135
136 daemonize_complete();
137
138 while (n_switches > 0 || n_listeners > 0) {
139 int iteration;
140 int i;
141
142 /* Accept connections on listening vconns. */
143 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
144 struct vconn *new_vconn;
145 int retval;
146
147 retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
148 if (!retval || retval == EAGAIN) {
149 if (!retval) {
150 new_switch(&switches[n_switches++], new_vconn, "tcp");
151 }
152 i++;
153 } else {
154 pvconn_close(listeners[i]);
155 listeners[i] = listeners[--n_listeners];
156 }
157 }
158
159 /* Do some switching work. Limit the number of iterations so that
160 * callbacks registered with the poll loop don't starve. */
161 for (iteration = 0; iteration < 50; iteration++) {
162 bool progress = false;
163 for (i = 0; i < n_switches; ) {
164 struct switch_ *this = &switches[i];
165 int retval = do_switching(this);
166 if (!retval || retval == EAGAIN) {
167 if (!retval) {
168 progress = true;
169 }
170 i++;
171 } else {
172 rconn_destroy(this->rconn);
173 lswitch_destroy(this->lswitch);
174 switches[i] = switches[--n_switches];
175 }
176 }
177 if (!progress) {
178 break;
179 }
180 }
181 for (i = 0; i < n_switches; i++) {
182 struct switch_ *this = &switches[i];
183 lswitch_run(this->lswitch, this->rconn);
184 }
185
186 unixctl_server_run(unixctl);
187
188 /* Wait for something to happen. */
189 if (n_switches < MAX_SWITCHES) {
190 for (i = 0; i < n_listeners; i++) {
191 pvconn_wait(listeners[i]);
192 }
193 }
194 for (i = 0; i < n_switches; i++) {
195 struct switch_ *sw = &switches[i];
196 rconn_run_wait(sw->rconn);
197 rconn_recv_wait(sw->rconn);
198 lswitch_wait(sw->lswitch);
199 }
200 unixctl_server_wait(unixctl);
201 poll_block();
202 }
203
204 return 0;
205 }
206
207 static void
208 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
209 {
210 sw->rconn = rconn_new_from_vconn(name, vconn);
211 sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
212 set_up_flows ? max_idle : -1,
213 action_normal);
214 }
215
216 static int
217 do_switching(struct switch_ *sw)
218 {
219 unsigned int packets_sent;
220 struct ofpbuf *msg;
221
222 packets_sent = rconn_packets_sent(sw->rconn);
223
224 msg = rconn_recv(sw->rconn);
225 if (msg) {
226 if (!mute) {
227 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
228 }
229 ofpbuf_delete(msg);
230 }
231 rconn_run(sw->rconn);
232
233 return (!rconn_is_alive(sw->rconn) ? EOF
234 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
235 : EAGAIN);
236 }
237
238 static void
239 parse_options(int argc, char *argv[])
240 {
241 enum {
242 OPT_MAX_IDLE = UCHAR_MAX + 1,
243 OPT_PEER_CA_CERT,
244 OPT_MUTE,
245 VLOG_OPTION_ENUMS
246 };
247 static struct option long_options[] = {
248 {"hub", no_argument, 0, 'H'},
249 {"noflow", no_argument, 0, 'n'},
250 {"normal", no_argument, 0, 'N'},
251 {"wildcard", no_argument, 0, 'w'},
252 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
253 {"mute", no_argument, 0, OPT_MUTE},
254 {"help", no_argument, 0, 'h'},
255 {"version", no_argument, 0, 'V'},
256 DAEMON_LONG_OPTIONS,
257 VLOG_LONG_OPTIONS,
258 #ifdef HAVE_OPENSSL
259 STREAM_SSL_LONG_OPTIONS
260 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
261 #endif
262 {0, 0, 0, 0},
263 };
264 char *short_options = long_options_to_short_options(long_options);
265
266 for (;;) {
267 int indexptr;
268 int c;
269
270 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
271 if (c == -1) {
272 break;
273 }
274
275 switch (c) {
276 case 'H':
277 learn_macs = false;
278 break;
279
280 case 'n':
281 set_up_flows = false;
282 break;
283
284 case OPT_MUTE:
285 mute = true;
286 break;
287
288 case 'N':
289 action_normal = true;
290 break;
291
292 case 'w':
293 exact_flows = false;
294 break;
295
296 case OPT_MAX_IDLE:
297 if (!strcmp(optarg, "permanent")) {
298 max_idle = OFP_FLOW_PERMANENT;
299 } else {
300 max_idle = atoi(optarg);
301 if (max_idle < 1 || max_idle > 65535) {
302 ovs_fatal(0, "--max-idle argument must be between 1 and "
303 "65535 or the word 'permanent'");
304 }
305 }
306 break;
307
308 case 'h':
309 usage();
310
311 case 'V':
312 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
313 exit(EXIT_SUCCESS);
314
315 VLOG_OPTION_HANDLERS
316 DAEMON_OPTION_HANDLERS
317
318 #ifdef HAVE_OPENSSL
319 STREAM_SSL_OPTION_HANDLERS
320
321 case OPT_PEER_CA_CERT:
322 stream_ssl_set_peer_ca_cert_file(optarg);
323 break;
324 #endif
325
326 case '?':
327 exit(EXIT_FAILURE);
328
329 default:
330 abort();
331 }
332 }
333 free(short_options);
334 }
335
336 static void
337 usage(void)
338 {
339 printf("%s: OpenFlow controller\n"
340 "usage: %s [OPTIONS] METHOD\n"
341 "where METHOD is any OpenFlow connection method.\n",
342 program_name, program_name);
343 vconn_usage(true, true, false);
344 daemon_usage();
345 vlog_usage();
346 printf("\nOther options:\n"
347 " -H, --hub act as hub instead of learning switch\n"
348 " -n, --noflow pass traffic, but don't add flows\n"
349 " --max-idle=SECS max idle time for new flows\n"
350 " -N, --normal use OFPAT_NORMAL action\n"
351 " -w, --wildcard use wildcards, not exact-match rules\n"
352 " -h, --help display this help message\n"
353 " -V, --version display version information\n");
354 exit(EXIT_SUCCESS);
355 }