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