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