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