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