]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-controller.c
New function ovs_strerror() as a thread-safe replacement for strerror().
[mirror_ovs.git] / utilities / ovs-controller.c
CommitLineData
064af421 1/*
82c8c53c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 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>
882c2399 24#include <stdio.h>
064af421
BP
25#include <string.h>
26
27#include "command-line.h"
28#include "compiler.h"
29#include "daemon.h"
064af421 30#include "learning-switch.h"
09913dfd 31#include "ofp-parse.h"
be721d87 32#include "ofp-version-opt.h"
064af421
BP
33#include "ofpbuf.h"
34#include "openflow/openflow.h"
35#include "poll-loop.h"
36#include "rconn.h"
44bac24b 37#include "simap.h"
fe55ad15 38#include "stream-ssl.h"
064af421
BP
39#include "timeval.h"
40#include "unixctl.h"
41#include "util.h"
064af421 42#include "vconn.h"
064af421 43#include "vlog.h"
f125905c 44#include "socket-util.h"
7a25bd99 45#include "ofp-util.h"
5136ce49 46
d98e6007 47VLOG_DEFINE_THIS_MODULE(controller);
064af421
BP
48
49#define MAX_SWITCHES 16
50#define MAX_LISTENERS 16
51
52struct switch_ {
53 struct lswitch *lswitch;
064af421
BP
54};
55
d4cdc6b4 56/* -H, --hub: Learn the ports on which MAC addresses appear? */
064af421
BP
57static bool learn_macs = true;
58
d4cdc6b4
BP
59/* -n, --noflow: Set up flows? (If not, every packet is processed at the
60 * controller.) */
d6fbec6d 61static bool set_up_flows = true;
064af421 62
9af9e2e8
JT
63/* -N, --normal: Use "NORMAL" action instead of explicit port? */
64static bool action_normal = false;
65
eec25dc1 66/* -w, --wildcard: 0 to disable wildcard flow entries, an OFPFW10_* bitmask to
7286b1e1
BP
67 * enable specific wildcards, or UINT32_MAX to use the default wildcards. */
68static uint32_t wildcards = 0;
9af9e2e8 69
064af421
BP
70/* --max-idle: Maximum idle time, in seconds, before flows expire. */
71static int max_idle = 60;
72
7778bd15
BP
73/* --mute: If true, accept connections from switches but do not reply to any
74 * of their messages (for debugging fail-open mode). */
75static bool mute = false;
76
d4cdc6b4
BP
77/* -q, --queue: default OpenFlow queue, none if UINT32_MAX. */
78static uint32_t default_queue = UINT32_MAX;
79
44bac24b
BP
80/* -Q, --port-queue: map from port name to port number. */
81static struct simap port_queues = SIMAP_INITIALIZER(&port_queues);
611e9a35 82
27527aa0
BP
83/* --with-flows: Flows to send to switch. */
84static struct ofputil_flow_mod *default_flows;
85static size_t n_default_flows;
882c2399 86
b66bdf30
BP
87/* --unixctl: Name of unixctl socket, or null to use the default. */
88static char *unixctl_path = NULL;
89
58bdd092 90static void new_switch(struct switch_ *, struct vconn *);
064af421
BP
91static void parse_options(int argc, char *argv[]);
92static void usage(void) NO_RETURN;
93
94int
95main(int argc, char *argv[])
96{
97 struct unixctl_server *unixctl;
98 struct switch_ switches[MAX_SWITCHES];
99 struct pvconn *listeners[MAX_LISTENERS];
100 int n_switches, n_listeners;
101 int retval;
102 int i;
103
40f0707c 104 proctitle_init(argc, argv);
064af421 105 set_program_name(argv[0]);
064af421
BP
106 parse_options(argc, argv);
107 signal(SIGPIPE, SIG_IGN);
108
109 if (argc - optind < 1) {
110 ovs_fatal(0, "at least one vconn argument required; "
111 "use --help for usage");
112 }
113
114 n_switches = n_listeners = 0;
115 for (i = optind; i < argc; i++) {
116 const char *name = argv[i];
117 struct vconn *vconn;
064af421 118
82c8c53c
BP
119 retval = vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT,
120 &vconn);
064af421
BP
121 if (!retval) {
122 if (n_switches >= MAX_SWITCHES) {
123 ovs_fatal(0, "max %d switch connections", n_switches);
124 }
58bdd092 125 new_switch(&switches[n_switches++], vconn);
064af421
BP
126 continue;
127 } else if (retval == EAFNOSUPPORT) {
128 struct pvconn *pvconn;
be721d87 129 retval = pvconn_open(name, get_allowed_ofp_versions(),
82c8c53c 130 DSCP_DEFAULT, &pvconn);
064af421
BP
131 if (!retval) {
132 if (n_listeners >= MAX_LISTENERS) {
133 ovs_fatal(0, "max %d passive connections", n_listeners);
134 }
135 listeners[n_listeners++] = pvconn;
136 }
137 }
138 if (retval) {
139 VLOG_ERR("%s: connect: %s", name, strerror(retval));
140 }
141 }
142 if (n_switches == 0 && n_listeners == 0) {
143 ovs_fatal(0, "no active or passive switch connections");
144 }
145
95440284 146 daemonize_start();
064af421 147
b66bdf30 148 retval = unixctl_server_create(unixctl_path, &unixctl);
064af421 149 if (retval) {
4d12270a 150 exit(EXIT_FAILURE);
064af421
BP
151 }
152
95440284
BP
153 daemonize_complete();
154
064af421 155 while (n_switches > 0 || n_listeners > 0) {
064af421
BP
156 /* Accept connections on listening vconns. */
157 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
158 struct vconn *new_vconn;
064af421 159
7a25bd99 160 retval = pvconn_accept(listeners[i], &new_vconn);
064af421
BP
161 if (!retval || retval == EAGAIN) {
162 if (!retval) {
58bdd092 163 new_switch(&switches[n_switches++], new_vconn);
064af421
BP
164 }
165 i++;
166 } else {
167 pvconn_close(listeners[i]);
168 listeners[i] = listeners[--n_listeners];
169 }
170 }
171
002c3f17
BP
172 /* Do some switching work. . */
173 for (i = 0; i < n_switches; ) {
064af421 174 struct switch_ *this = &switches[i];
ba186119 175 lswitch_run(this->lswitch);
002c3f17
BP
176 if (lswitch_is_alive(this->lswitch)) {
177 i++;
178 } else {
179 lswitch_destroy(this->lswitch);
180 switches[i] = switches[--n_switches];
181 }
064af421
BP
182 }
183
184 unixctl_server_run(unixctl);
185
186 /* Wait for something to happen. */
187 if (n_switches < MAX_SWITCHES) {
188 for (i = 0; i < n_listeners; i++) {
189 pvconn_wait(listeners[i]);
190 }
191 }
192 for (i = 0; i < n_switches; i++) {
193 struct switch_ *sw = &switches[i];
064af421
BP
194 lswitch_wait(sw->lswitch);
195 }
196 unixctl_server_wait(unixctl);
197 poll_block();
198 }
199
200 return 0;
201}
202
203static void
58bdd092 204new_switch(struct switch_ *sw, struct vconn *vconn)
064af421 205{
ad67e568 206 struct lswitch_config cfg;
002c3f17 207 struct rconn *rconn;
ad67e568 208
be721d87 209 rconn = rconn_create(60, 0, DSCP_DEFAULT, get_allowed_ofp_versions());
002c3f17 210 rconn_connect_unreliably(rconn, vconn, NULL);
882c2399 211
ad67e568
BP
212 cfg.mode = (action_normal ? LSW_NORMAL
213 : learn_macs ? LSW_LEARN
214 : LSW_FLOOD);
7286b1e1 215 cfg.wildcards = wildcards;
ad67e568 216 cfg.max_idle = set_up_flows ? max_idle : -1;
27527aa0
BP
217 cfg.default_flows = default_flows;
218 cfg.n_default_flows = n_default_flows;
d4cdc6b4
BP
219 cfg.default_queue = default_queue;
220 cfg.port_queues = &port_queues;
002c3f17
BP
221 cfg.mute = mute;
222 sw->lswitch = lswitch_create(rconn, &cfg);
064af421
BP
223}
224
d4cdc6b4
BP
225static void
226add_port_queue(char *s)
227{
228 char *save_ptr = NULL;
229 char *port_name;
230 char *queue_id;
231
232 port_name = strtok_r(s, ":", &save_ptr);
233 queue_id = strtok_r(NULL, "", &save_ptr);
234 if (!queue_id) {
235 ovs_fatal(0, "argument to -Q or --port-queue should take the form "
236 "\"<port-name>:<queue-id>\"");
237 }
238
44bac24b 239 if (!simap_put(&port_queues, port_name, atoi(queue_id))) {
d4cdc6b4
BP
240 ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
241 "be unique");
242 }
243}
244
064af421
BP
245static void
246parse_options(int argc, char *argv[])
247{
248 enum {
249 OPT_MAX_IDLE = UCHAR_MAX + 1,
250 OPT_PEER_CA_CERT,
7778bd15 251 OPT_MUTE,
882c2399 252 OPT_WITH_FLOWS,
b66bdf30 253 OPT_UNIXCTL,
8274ae95 254 VLOG_OPTION_ENUMS,
be721d87
SH
255 DAEMON_OPTION_ENUMS,
256 OFP_VERSION_OPTION_ENUMS
064af421 257 };
07fc4ed3 258 static const struct option long_options[] = {
e3c17733
BP
259 {"hub", no_argument, NULL, 'H'},
260 {"noflow", no_argument, NULL, 'n'},
261 {"normal", no_argument, NULL, 'N'},
7286b1e1 262 {"wildcards", optional_argument, NULL, 'w'},
e3c17733
BP
263 {"max-idle", required_argument, NULL, OPT_MAX_IDLE},
264 {"mute", no_argument, NULL, OPT_MUTE},
265 {"queue", required_argument, NULL, 'q'},
266 {"port-queue", required_argument, NULL, 'Q'},
267 {"with-flows", required_argument, NULL, OPT_WITH_FLOWS},
268 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
269 {"help", no_argument, NULL, 'h'},
064af421 270 DAEMON_LONG_OPTIONS,
be721d87 271 OFP_VERSION_LONG_OPTIONS,
064af421 272 VLOG_LONG_OPTIONS,
bf8f2167 273 STREAM_SSL_LONG_OPTIONS,
e3c17733
BP
274 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
275 {NULL, 0, NULL, 0},
064af421
BP
276 };
277 char *short_options = long_options_to_short_options(long_options);
278
279 for (;;) {
280 int indexptr;
281 int c;
282
283 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
284 if (c == -1) {
285 break;
286 }
287
288 switch (c) {
289 case 'H':
290 learn_macs = false;
291 break;
292
293 case 'n':
d6fbec6d 294 set_up_flows = false;
064af421
BP
295 break;
296
7778bd15
BP
297 case OPT_MUTE:
298 mute = true;
299 break;
300
9af9e2e8
JT
301 case 'N':
302 action_normal = true;
303 break;
304
305 case 'w':
7286b1e1 306 wildcards = optarg ? strtol(optarg, NULL, 16) : UINT32_MAX;
9af9e2e8
JT
307 break;
308
064af421
BP
309 case OPT_MAX_IDLE:
310 if (!strcmp(optarg, "permanent")) {
311 max_idle = OFP_FLOW_PERMANENT;
312 } else {
313 max_idle = atoi(optarg);
314 if (max_idle < 1 || max_idle > 65535) {
315 ovs_fatal(0, "--max-idle argument must be between 1 and "
316 "65535 or the word 'permanent'");
317 }
318 }
319 break;
320
611e9a35 321 case 'q':
d4cdc6b4
BP
322 default_queue = atoi(optarg);
323 break;
324
325 case 'Q':
326 add_port_queue(optarg);
611e9a35
BP
327 break;
328
882c2399 329 case OPT_WITH_FLOWS:
27527aa0
BP
330 parse_ofp_flow_mod_file(optarg, OFPFC_ADD, &default_flows,
331 &n_default_flows);
882c2399
JP
332 break;
333
b66bdf30
BP
334 case OPT_UNIXCTL:
335 unixctl_path = optarg;
336 break;
337
064af421
BP
338 case 'h':
339 usage();
340
064af421 341 VLOG_OPTION_HANDLERS
be721d87 342 OFP_VERSION_OPTION_HANDLERS
064af421
BP
343 DAEMON_OPTION_HANDLERS
344
fe55ad15 345 STREAM_SSL_OPTION_HANDLERS
064af421
BP
346
347 case OPT_PEER_CA_CERT:
fe55ad15 348 stream_ssl_set_peer_ca_cert_file(optarg);
064af421 349 break;
064af421
BP
350
351 case '?':
352 exit(EXIT_FAILURE);
353
354 default:
355 abort();
356 }
357 }
358 free(short_options);
d4cdc6b4 359
44bac24b 360 if (!simap_is_empty(&port_queues) || default_queue != UINT32_MAX) {
d4cdc6b4
BP
361 if (action_normal) {
362 ovs_error(0, "queue IDs are incompatible with -N or --normal; "
363 "not using OFPP_NORMAL");
364 action_normal = false;
365 }
366
367 if (!learn_macs) {
368 ovs_error(0, "queue IDs are incompatible with -H or --hub; "
369 "not acting as hub");
370 learn_macs = true;
371 }
372 }
064af421
BP
373}
374
375static void
376usage(void)
377{
378 printf("%s: OpenFlow controller\n"
379 "usage: %s [OPTIONS] METHOD\n"
380 "where METHOD is any OpenFlow connection method.\n",
381 program_name, program_name);
382 vconn_usage(true, true, false);
383 daemon_usage();
be721d87 384 ofp_version_usage();
064af421
BP
385 vlog_usage();
386 printf("\nOther options:\n"
387 " -H, --hub act as hub instead of learning switch\n"
388 " -n, --noflow pass traffic, but don't add flows\n"
389 " --max-idle=SECS max idle time for new flows\n"
d4cdc6b4 390 " -N, --normal use OFPP_NORMAL action\n"
7286b1e1 391 " -w, --wildcards[=MASK] wildcard (specified) bits in flows\n"
d4cdc6b4
BP
392 " -q, --queue=QUEUE-ID OpenFlow queue ID to use for output\n"
393 " -Q PORT-NAME:QUEUE-ID use QUEUE-ID for frames from PORT-NAME\n"
882c2399 394 " --with-flows FILE use the flows from FILE\n"
b66bdf30 395 " --unixctl=SOCKET override default control socket name\n"
064af421
BP
396 " -h, --help display this help message\n"
397 " -V, --version display version information\n");
398 exit(EXIT_SUCCESS);
399}