]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
60cb3eb8 | 2 | * Copyright (c) 2008, 2009, 2010 Nicira Networks. |
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 | #include "vconn-provider.h" | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
22 | #include <netinet/in.h> | |
23 | #include <poll.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include "coverage.h" | |
27 | #include "dynamic-string.h" | |
28 | #include "flow.h" | |
29 | #include "ofp-print.h" | |
30 | #include "ofpbuf.h" | |
31 | #include "openflow/nicira-ext.h" | |
32 | #include "openflow/openflow.h" | |
33 | #include "packets.h" | |
34 | #include "poll-loop.h" | |
35 | #include "random.h" | |
36 | #include "util.h" | |
37 | ||
38 | #define THIS_MODULE VLM_vconn | |
39 | #include "vlog.h" | |
40 | ||
41 | /* State of an active vconn.*/ | |
42 | enum vconn_state { | |
43 | /* This is the ordinary progression of states. */ | |
44 | VCS_CONNECTING, /* Underlying vconn is not connected. */ | |
45 | VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */ | |
46 | VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */ | |
47 | VCS_CONNECTED, /* Connection established. */ | |
48 | ||
49 | /* These states are entered only when something goes wrong. */ | |
50 | VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */ | |
51 | VCS_DISCONNECTED /* Connection failed or connection closed. */ | |
52 | }; | |
53 | ||
54 | static struct vconn_class *vconn_classes[] = { | |
55 | &tcp_vconn_class, | |
56 | &unix_vconn_class, | |
57 | #ifdef HAVE_OPENSSL | |
58 | &ssl_vconn_class, | |
59 | #endif | |
60 | }; | |
61 | ||
62 | static struct pvconn_class *pvconn_classes[] = { | |
63 | &ptcp_pvconn_class, | |
64 | &punix_pvconn_class, | |
65 | #ifdef HAVE_OPENSSL | |
66 | &pssl_pvconn_class, | |
67 | #endif | |
68 | }; | |
69 | ||
70 | /* Rate limit for individual OpenFlow messages going over the vconn, output at | |
71 | * DBG level. This is very high because, if these are enabled, it is because | |
72 | * we really need to see them. */ | |
73 | static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
74 | ||
75 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
76 | * in the peer and so there's not much point in showing a lot of them. */ | |
77 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
78 | ||
79 | static int do_recv(struct vconn *, struct ofpbuf **); | |
80 | static int do_send(struct vconn *, struct ofpbuf *); | |
81 | ||
82 | /* Check the validity of the vconn class structures. */ | |
83 | static void | |
84 | check_vconn_classes(void) | |
85 | { | |
86 | #ifndef NDEBUG | |
87 | size_t i; | |
88 | ||
89 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
90 | struct vconn_class *class = vconn_classes[i]; | |
91 | assert(class->name != NULL); | |
92 | assert(class->open != NULL); | |
60cb3eb8 BP |
93 | if (class->close || class->recv || class->send |
94 | || class->run || class->run_wait || class->wait) { | |
064af421 BP |
95 | assert(class->close != NULL); |
96 | assert(class->recv != NULL); | |
97 | assert(class->send != NULL); | |
98 | assert(class->wait != NULL); | |
99 | } else { | |
100 | /* This class delegates to another one. */ | |
101 | } | |
102 | } | |
103 | ||
104 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
105 | struct pvconn_class *class = pvconn_classes[i]; | |
106 | assert(class->name != NULL); | |
107 | assert(class->listen != NULL); | |
108 | if (class->close || class->accept || class->wait) { | |
109 | assert(class->close != NULL); | |
110 | assert(class->accept != NULL); | |
111 | assert(class->wait != NULL); | |
112 | } else { | |
113 | /* This class delegates to another one. */ | |
114 | } | |
115 | } | |
116 | #endif | |
117 | } | |
118 | ||
119 | /* Prints information on active (if 'active') and passive (if 'passive') | |
120 | * connection methods supported by the vconn. If 'bootstrap' is true, also | |
121 | * advertises options to bootstrap the CA certificate. */ | |
122 | void | |
67a4917b | 123 | vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED) |
064af421 BP |
124 | { |
125 | /* Really this should be implemented via callbacks into the vconn | |
126 | * providers, but that seems too heavy-weight to bother with at the | |
127 | * moment. */ | |
128 | ||
129 | printf("\n"); | |
130 | if (active) { | |
131 | printf("Active OpenFlow connection methods:\n"); | |
2b35e147 BP |
132 | printf(" tcp:IP[:PORT] " |
133 | "PORT (default: %d) at remote IP\n", OFP_TCP_PORT); | |
064af421 | 134 | #ifdef HAVE_OPENSSL |
2b35e147 BP |
135 | printf(" ssl:IP[:PORT] " |
136 | "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT); | |
064af421 BP |
137 | #endif |
138 | printf(" unix:FILE Unix domain socket named FILE\n"); | |
139 | } | |
140 | ||
141 | if (passive) { | |
142 | printf("Passive OpenFlow connection methods:\n"); | |
78ff0270 BP |
143 | printf(" ptcp:[PORT][:IP] " |
144 | "listen to TCP PORT (default: %d) on IP\n", | |
064af421 BP |
145 | OFP_TCP_PORT); |
146 | #ifdef HAVE_OPENSSL | |
78ff0270 BP |
147 | printf(" pssl:[PORT][:IP] " |
148 | "listen for SSL on PORT (default: %d) on IP\n", | |
064af421 BP |
149 | OFP_SSL_PORT); |
150 | #endif | |
151 | printf(" punix:FILE " | |
152 | "listen on Unix domain socket FILE\n"); | |
153 | } | |
154 | ||
155 | #ifdef HAVE_OPENSSL | |
156 | printf("PKI configuration (required to use SSL):\n" | |
157 | " -p, --private-key=FILE file with private key\n" | |
158 | " -c, --certificate=FILE file with certificate for private key\n" | |
159 | " -C, --ca-cert=FILE file with peer CA certificate\n"); | |
160 | if (bootstrap) { | |
161 | printf(" --bootstrap-ca-cert=FILE file with peer CA certificate " | |
162 | "to read or create\n"); | |
163 | } | |
164 | #endif | |
165 | } | |
166 | ||
167 | /* Attempts to connect to an OpenFlow device. 'name' is a connection name in | |
168 | * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS | |
169 | * are vconn class-specific. | |
170 | * | |
171 | * The vconn will automatically negotiate an OpenFlow protocol version | |
172 | * acceptable to both peers on the connection. The version negotiated will be | |
173 | * no lower than 'min_version' and no higher than OFP_VERSION. | |
174 | * | |
175 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
176 | * stores a pointer to the new connection in '*vconnp', otherwise a null | |
177 | * pointer. */ | |
178 | int | |
179 | vconn_open(const char *name, int min_version, struct vconn **vconnp) | |
180 | { | |
181 | size_t prefix_len; | |
182 | size_t i; | |
183 | ||
184 | COVERAGE_INC(vconn_open); | |
185 | check_vconn_classes(); | |
186 | ||
187 | *vconnp = NULL; | |
188 | prefix_len = strcspn(name, ":"); | |
189 | if (prefix_len == strlen(name)) { | |
190 | return EAFNOSUPPORT; | |
191 | } | |
192 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
193 | struct vconn_class *class = vconn_classes[i]; | |
194 | if (strlen(class->name) == prefix_len | |
195 | && !memcmp(class->name, name, prefix_len)) { | |
196 | struct vconn *vconn; | |
197 | char *suffix_copy = xstrdup(name + prefix_len + 1); | |
198 | int retval = class->open(name, suffix_copy, &vconn); | |
199 | free(suffix_copy); | |
200 | if (!retval) { | |
201 | assert(vconn->state != VCS_CONNECTING | |
202 | || vconn->class->connect); | |
203 | vconn->min_version = min_version; | |
204 | *vconnp = vconn; | |
205 | } | |
206 | return retval; | |
207 | } | |
208 | } | |
209 | return EAFNOSUPPORT; | |
210 | } | |
211 | ||
60cb3eb8 BP |
212 | /* Allows 'vconn' to perform maintenance activities, such as flushing output |
213 | * buffers. */ | |
214 | void | |
215 | vconn_run(struct vconn *vconn) | |
216 | { | |
217 | if (vconn->class->run) { | |
218 | (vconn->class->run)(vconn); | |
219 | } | |
220 | } | |
221 | ||
222 | /* Arranges for the poll loop to wake up when 'vconn' needs to perform | |
223 | * maintenance activities. */ | |
224 | void | |
225 | vconn_run_wait(struct vconn *vconn) | |
226 | { | |
227 | if (vconn->class->run_wait) { | |
228 | (vconn->class->run_wait)(vconn); | |
229 | } | |
230 | } | |
231 | ||
064af421 BP |
232 | int |
233 | vconn_open_block(const char *name, int min_version, struct vconn **vconnp) | |
234 | { | |
235 | struct vconn *vconn; | |
236 | int error; | |
237 | ||
238 | error = vconn_open(name, min_version, &vconn); | |
239 | while (error == EAGAIN) { | |
60cb3eb8 BP |
240 | vconn_run(vconn); |
241 | vconn_run_wait(vconn); | |
064af421 BP |
242 | vconn_connect_wait(vconn); |
243 | poll_block(); | |
244 | error = vconn_connect(vconn); | |
245 | assert(error != EINPROGRESS); | |
246 | } | |
247 | if (error) { | |
248 | vconn_close(vconn); | |
249 | *vconnp = NULL; | |
250 | } else { | |
251 | *vconnp = vconn; | |
252 | } | |
253 | return error; | |
254 | } | |
255 | ||
256 | /* Closes 'vconn'. */ | |
257 | void | |
258 | vconn_close(struct vconn *vconn) | |
259 | { | |
260 | if (vconn != NULL) { | |
261 | char *name = vconn->name; | |
262 | (vconn->class->close)(vconn); | |
263 | free(name); | |
264 | } | |
265 | } | |
266 | ||
267 | /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */ | |
268 | const char * | |
269 | vconn_get_name(const struct vconn *vconn) | |
270 | { | |
271 | return vconn->name; | |
272 | } | |
273 | ||
274 | /* Returns the IP address of the peer, or 0 if the peer is not connected over | |
275 | * an IP-based protocol or if its IP address is not yet known. */ | |
276 | uint32_t | |
193456d5 | 277 | vconn_get_remote_ip(const struct vconn *vconn) |
064af421 | 278 | { |
193456d5 JP |
279 | return vconn->remote_ip; |
280 | } | |
281 | ||
282 | /* Returns the transport port of the peer, or 0 if the connection does not | |
283 | * contain a port or if the port is not yet known. */ | |
284 | uint16_t | |
285 | vconn_get_remote_port(const struct vconn *vconn) | |
286 | { | |
287 | return vconn->remote_port; | |
288 | } | |
289 | ||
290 | /* Returns the IP address used to connect to the peer, or 0 if the | |
291 | * connection is not an IP-based protocol or if its IP address is not | |
292 | * yet known. */ | |
293 | uint32_t | |
294 | vconn_get_local_ip(const struct vconn *vconn) | |
295 | { | |
296 | return vconn->local_ip; | |
297 | } | |
298 | ||
299 | /* Returns the transport port used to connect to the peer, or 0 if the | |
300 | * connection does not contain a port or if the port is not yet known. */ | |
301 | uint16_t | |
302 | vconn_get_local_port(const struct vconn *vconn) | |
303 | { | |
304 | return vconn->local_port; | |
064af421 BP |
305 | } |
306 | ||
307 | static void | |
308 | vcs_connecting(struct vconn *vconn) | |
309 | { | |
310 | int retval = (vconn->class->connect)(vconn); | |
311 | assert(retval != EINPROGRESS); | |
312 | if (!retval) { | |
313 | vconn->state = VCS_SEND_HELLO; | |
314 | } else if (retval != EAGAIN) { | |
315 | vconn->state = VCS_DISCONNECTED; | |
316 | vconn->error = retval; | |
317 | } | |
318 | } | |
319 | ||
320 | static void | |
321 | vcs_send_hello(struct vconn *vconn) | |
322 | { | |
323 | struct ofpbuf *b; | |
324 | int retval; | |
325 | ||
326 | make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b); | |
327 | retval = do_send(vconn, b); | |
328 | if (!retval) { | |
329 | vconn->state = VCS_RECV_HELLO; | |
330 | } else { | |
331 | ofpbuf_delete(b); | |
332 | if (retval != EAGAIN) { | |
333 | vconn->state = VCS_DISCONNECTED; | |
334 | vconn->error = retval; | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | static void | |
340 | vcs_recv_hello(struct vconn *vconn) | |
341 | { | |
342 | struct ofpbuf *b; | |
343 | int retval; | |
344 | ||
345 | retval = do_recv(vconn, &b); | |
346 | if (!retval) { | |
347 | struct ofp_header *oh = b->data; | |
348 | ||
349 | if (oh->type == OFPT_HELLO) { | |
350 | if (b->size > sizeof *oh) { | |
351 | struct ds msg = DS_EMPTY_INITIALIZER; | |
352 | ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name); | |
353 | ds_put_hex_dump(&msg, b->data, b->size, 0, true); | |
354 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg)); | |
355 | ds_destroy(&msg); | |
356 | } | |
357 | ||
358 | vconn->version = MIN(OFP_VERSION, oh->version); | |
359 | if (vconn->version < vconn->min_version) { | |
360 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
361 | "%s: version negotiation failed: we support " | |
362 | "versions 0x%02x to 0x%02x inclusive but peer " | |
363 | "supports no later than version 0x%02"PRIx8, | |
364 | vconn->name, vconn->min_version, OFP_VERSION, | |
365 | oh->version); | |
366 | vconn->state = VCS_SEND_ERROR; | |
367 | } else { | |
368 | VLOG_DBG("%s: negotiated OpenFlow version 0x%02x " | |
369 | "(we support versions 0x%02x to 0x%02x inclusive, " | |
370 | "peer no later than version 0x%02"PRIx8")", | |
371 | vconn->name, vconn->version, vconn->min_version, | |
372 | OFP_VERSION, oh->version); | |
373 | vconn->state = VCS_CONNECTED; | |
374 | } | |
375 | ofpbuf_delete(b); | |
376 | return; | |
377 | } else { | |
378 | char *s = ofp_to_string(b->data, b->size, 1); | |
379 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
380 | "%s: received message while expecting hello: %s", | |
381 | vconn->name, s); | |
382 | free(s); | |
383 | retval = EPROTO; | |
384 | ofpbuf_delete(b); | |
385 | } | |
386 | } | |
387 | ||
388 | if (retval != EAGAIN) { | |
389 | vconn->state = VCS_DISCONNECTED; | |
b7eae257 | 390 | vconn->error = retval == EOF ? ECONNRESET : retval; |
064af421 BP |
391 | } |
392 | } | |
393 | ||
394 | static void | |
395 | vcs_send_error(struct vconn *vconn) | |
396 | { | |
397 | struct ofp_error_msg *error; | |
398 | struct ofpbuf *b; | |
399 | char s[128]; | |
400 | int retval; | |
401 | ||
402 | snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but " | |
403 | "you support no later than version 0x%02"PRIx8".", | |
404 | vconn->min_version, OFP_VERSION, vconn->version); | |
405 | error = make_openflow(sizeof *error, OFPT_ERROR, &b); | |
406 | error->type = htons(OFPET_HELLO_FAILED); | |
407 | error->code = htons(OFPHFC_INCOMPATIBLE); | |
408 | ofpbuf_put(b, s, strlen(s)); | |
409 | update_openflow_length(b); | |
410 | retval = do_send(vconn, b); | |
411 | if (retval) { | |
412 | ofpbuf_delete(b); | |
413 | } | |
414 | if (retval != EAGAIN) { | |
415 | vconn->state = VCS_DISCONNECTED; | |
416 | vconn->error = retval ? retval : EPROTO; | |
417 | } | |
418 | } | |
419 | ||
420 | /* Tries to complete the connection on 'vconn', which must be an active | |
421 | * vconn. If 'vconn''s connection is complete, returns 0 if the connection | |
422 | * was successful or a positive errno value if it failed. If the | |
423 | * connection is still in progress, returns EAGAIN. */ | |
424 | int | |
425 | vconn_connect(struct vconn *vconn) | |
426 | { | |
427 | enum vconn_state last_state; | |
428 | ||
429 | assert(vconn->min_version >= 0); | |
430 | do { | |
431 | last_state = vconn->state; | |
432 | switch (vconn->state) { | |
433 | case VCS_CONNECTING: | |
434 | vcs_connecting(vconn); | |
435 | break; | |
436 | ||
437 | case VCS_SEND_HELLO: | |
438 | vcs_send_hello(vconn); | |
439 | break; | |
440 | ||
441 | case VCS_RECV_HELLO: | |
442 | vcs_recv_hello(vconn); | |
443 | break; | |
444 | ||
445 | case VCS_CONNECTED: | |
446 | return 0; | |
447 | ||
448 | case VCS_SEND_ERROR: | |
449 | vcs_send_error(vconn); | |
450 | break; | |
451 | ||
452 | case VCS_DISCONNECTED: | |
453 | return vconn->error; | |
454 | ||
455 | default: | |
456 | NOT_REACHED(); | |
457 | } | |
458 | } while (vconn->state != last_state); | |
459 | ||
460 | return EAGAIN; | |
461 | } | |
462 | ||
463 | /* Tries to receive an OpenFlow message from 'vconn', which must be an active | |
464 | * vconn. If successful, stores the received message into '*msgp' and returns | |
465 | * 0. The caller is responsible for destroying the message with | |
466 | * ofpbuf_delete(). On failure, returns a positive errno value and stores a | |
467 | * null pointer into '*msgp'. On normal connection close, returns EOF. | |
468 | * | |
469 | * vconn_recv will not block waiting for a packet to arrive. If no packets | |
470 | * have been received, it returns EAGAIN immediately. */ | |
471 | int | |
472 | vconn_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
473 | { | |
474 | int retval = vconn_connect(vconn); | |
475 | if (!retval) { | |
476 | retval = do_recv(vconn, msgp); | |
477 | } | |
478 | return retval; | |
479 | } | |
480 | ||
481 | static int | |
482 | do_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
483 | { | |
5fe577eb | 484 | int retval = (vconn->class->recv)(vconn, msgp); |
064af421 BP |
485 | if (!retval) { |
486 | struct ofp_header *oh; | |
487 | ||
488 | COVERAGE_INC(vconn_received); | |
489 | if (VLOG_IS_DBG_ENABLED()) { | |
490 | char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1); | |
491 | VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s); | |
492 | free(s); | |
493 | } | |
494 | ||
495 | oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh); | |
496 | if (oh->version != vconn->version | |
497 | && oh->type != OFPT_HELLO | |
498 | && oh->type != OFPT_ERROR | |
499 | && oh->type != OFPT_ECHO_REQUEST | |
500 | && oh->type != OFPT_ECHO_REPLY | |
501 | && oh->type != OFPT_VENDOR) | |
502 | { | |
503 | if (vconn->version < 0) { | |
064af421 BP |
504 | VLOG_ERR_RL(&bad_ofmsg_rl, |
505 | "%s: received OpenFlow message type %"PRIu8" " | |
506 | "before version negotiation complete", | |
507 | vconn->name, oh->type); | |
508 | } else { | |
509 | VLOG_ERR_RL(&bad_ofmsg_rl, | |
510 | "%s: received OpenFlow version 0x%02"PRIx8" " | |
511 | "!= expected %02x", | |
512 | vconn->name, oh->version, vconn->version); | |
513 | } | |
514 | ofpbuf_delete(*msgp); | |
515 | retval = EPROTO; | |
516 | } | |
517 | } | |
518 | if (retval) { | |
519 | *msgp = NULL; | |
520 | } | |
521 | return retval; | |
522 | } | |
523 | ||
524 | /* Tries to queue 'msg' for transmission on 'vconn', which must be an active | |
525 | * vconn. If successful, returns 0, in which case ownership of 'msg' is | |
526 | * transferred to the vconn. Success does not guarantee that 'msg' has been or | |
527 | * ever will be delivered to the peer, only that it has been queued for | |
528 | * transmission. | |
529 | * | |
530 | * Returns a positive errno value on failure, in which case the caller | |
531 | * retains ownership of 'msg'. | |
532 | * | |
533 | * vconn_send will not block. If 'msg' cannot be immediately accepted for | |
534 | * transmission, it returns EAGAIN immediately. */ | |
535 | int | |
536 | vconn_send(struct vconn *vconn, struct ofpbuf *msg) | |
537 | { | |
538 | int retval = vconn_connect(vconn); | |
539 | if (!retval) { | |
540 | retval = do_send(vconn, msg); | |
541 | } | |
542 | return retval; | |
543 | } | |
544 | ||
545 | static int | |
546 | do_send(struct vconn *vconn, struct ofpbuf *msg) | |
547 | { | |
548 | int retval; | |
549 | ||
550 | assert(msg->size >= sizeof(struct ofp_header)); | |
551 | assert(((struct ofp_header *) msg->data)->length == htons(msg->size)); | |
552 | if (!VLOG_IS_DBG_ENABLED()) { | |
553 | COVERAGE_INC(vconn_sent); | |
554 | retval = (vconn->class->send)(vconn, msg); | |
555 | } else { | |
556 | char *s = ofp_to_string(msg->data, msg->size, 1); | |
557 | retval = (vconn->class->send)(vconn, msg); | |
558 | if (retval != EAGAIN) { | |
559 | VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s", | |
560 | vconn->name, strerror(retval), s); | |
561 | } | |
562 | free(s); | |
563 | } | |
564 | return retval; | |
565 | } | |
566 | ||
567 | /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */ | |
568 | int | |
569 | vconn_send_block(struct vconn *vconn, struct ofpbuf *msg) | |
570 | { | |
571 | int retval; | |
572 | while ((retval = vconn_send(vconn, msg)) == EAGAIN) { | |
60cb3eb8 BP |
573 | vconn_run(vconn); |
574 | vconn_run_wait(vconn); | |
064af421 BP |
575 | vconn_send_wait(vconn); |
576 | poll_block(); | |
577 | } | |
578 | return retval; | |
579 | } | |
580 | ||
581 | /* Same as vconn_recv, except that it waits until a message is received. */ | |
582 | int | |
583 | vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp) | |
584 | { | |
585 | int retval; | |
586 | while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) { | |
60cb3eb8 BP |
587 | vconn_run(vconn); |
588 | vconn_run_wait(vconn); | |
064af421 BP |
589 | vconn_recv_wait(vconn); |
590 | poll_block(); | |
591 | } | |
592 | return retval; | |
593 | } | |
594 | ||
595 | /* Waits until a message with a transaction ID matching 'xid' is recived on | |
596 | * 'vconn'. Returns 0 if successful, in which case the reply is stored in | |
597 | * '*replyp' for the caller to examine and free. Otherwise returns a positive | |
598 | * errno value, or EOF, and sets '*replyp' to null. | |
599 | * | |
600 | * 'request' is always destroyed, regardless of the return value. */ | |
601 | int | |
602 | vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp) | |
603 | { | |
604 | for (;;) { | |
605 | uint32_t recv_xid; | |
606 | struct ofpbuf *reply; | |
607 | int error; | |
608 | ||
609 | error = vconn_recv_block(vconn, &reply); | |
610 | if (error) { | |
611 | *replyp = NULL; | |
612 | return error; | |
613 | } | |
614 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
615 | if (xid == recv_xid) { | |
616 | *replyp = reply; | |
617 | return 0; | |
618 | } | |
619 | ||
620 | VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32 | |
621 | " != expected %08"PRIx32, vconn->name, recv_xid, xid); | |
622 | ofpbuf_delete(reply); | |
623 | } | |
624 | } | |
625 | ||
626 | /* Sends 'request' to 'vconn' and blocks until it receives a reply with a | |
627 | * matching transaction ID. Returns 0 if successful, in which case the reply | |
628 | * is stored in '*replyp' for the caller to examine and free. Otherwise | |
629 | * returns a positive errno value, or EOF, and sets '*replyp' to null. | |
630 | * | |
631 | * 'request' is always destroyed, regardless of the return value. */ | |
632 | int | |
633 | vconn_transact(struct vconn *vconn, struct ofpbuf *request, | |
634 | struct ofpbuf **replyp) | |
635 | { | |
636 | uint32_t send_xid = ((struct ofp_header *) request->data)->xid; | |
637 | int error; | |
638 | ||
639 | *replyp = NULL; | |
640 | error = vconn_send_block(vconn, request); | |
641 | if (error) { | |
642 | ofpbuf_delete(request); | |
643 | } | |
644 | return error ? error : vconn_recv_xid(vconn, send_xid, replyp); | |
645 | } | |
646 | ||
647 | void | |
648 | vconn_wait(struct vconn *vconn, enum vconn_wait_type wait) | |
649 | { | |
650 | assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND); | |
651 | ||
652 | switch (vconn->state) { | |
653 | case VCS_CONNECTING: | |
654 | wait = WAIT_CONNECT; | |
655 | break; | |
656 | ||
657 | case VCS_SEND_HELLO: | |
658 | case VCS_SEND_ERROR: | |
659 | wait = WAIT_SEND; | |
660 | break; | |
661 | ||
662 | case VCS_RECV_HELLO: | |
663 | wait = WAIT_RECV; | |
664 | break; | |
665 | ||
666 | case VCS_CONNECTED: | |
667 | break; | |
668 | ||
669 | case VCS_DISCONNECTED: | |
670 | poll_immediate_wake(); | |
671 | return; | |
672 | } | |
673 | (vconn->class->wait)(vconn, wait); | |
674 | } | |
675 | ||
676 | void | |
677 | vconn_connect_wait(struct vconn *vconn) | |
678 | { | |
679 | vconn_wait(vconn, WAIT_CONNECT); | |
680 | } | |
681 | ||
682 | void | |
683 | vconn_recv_wait(struct vconn *vconn) | |
684 | { | |
685 | vconn_wait(vconn, WAIT_RECV); | |
686 | } | |
687 | ||
688 | void | |
689 | vconn_send_wait(struct vconn *vconn) | |
690 | { | |
691 | vconn_wait(vconn, WAIT_SEND); | |
692 | } | |
693 | ||
694 | /* Attempts to start listening for OpenFlow connections. 'name' is a | |
695 | * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn | |
696 | * class's name and ARGS are vconn class-specific. | |
697 | * | |
698 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
699 | * stores a pointer to the new connection in '*pvconnp', otherwise a null | |
700 | * pointer. */ | |
701 | int | |
702 | pvconn_open(const char *name, struct pvconn **pvconnp) | |
703 | { | |
704 | size_t prefix_len; | |
705 | size_t i; | |
706 | ||
707 | check_vconn_classes(); | |
708 | ||
709 | *pvconnp = NULL; | |
710 | prefix_len = strcspn(name, ":"); | |
711 | if (prefix_len == strlen(name)) { | |
712 | return EAFNOSUPPORT; | |
713 | } | |
714 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
715 | struct pvconn_class *class = pvconn_classes[i]; | |
716 | if (strlen(class->name) == prefix_len | |
717 | && !memcmp(class->name, name, prefix_len)) { | |
718 | char *suffix_copy = xstrdup(name + prefix_len + 1); | |
719 | int retval = class->listen(name, suffix_copy, pvconnp); | |
720 | free(suffix_copy); | |
721 | if (retval) { | |
722 | *pvconnp = NULL; | |
723 | } | |
724 | return retval; | |
725 | } | |
726 | } | |
727 | return EAFNOSUPPORT; | |
728 | } | |
729 | ||
730 | /* Returns the name that was used to open 'pvconn'. The caller must not | |
731 | * modify or free the name. */ | |
732 | const char * | |
733 | pvconn_get_name(const struct pvconn *pvconn) | |
734 | { | |
735 | return pvconn->name; | |
736 | } | |
737 | ||
738 | /* Closes 'pvconn'. */ | |
739 | void | |
740 | pvconn_close(struct pvconn *pvconn) | |
741 | { | |
742 | if (pvconn != NULL) { | |
743 | char *name = pvconn->name; | |
744 | (pvconn->class->close)(pvconn); | |
745 | free(name); | |
746 | } | |
747 | } | |
748 | ||
749 | /* Tries to accept a new connection on 'pvconn'. If successful, stores the new | |
750 | * connection in '*new_vconn' and returns 0. Otherwise, returns a positive | |
751 | * errno value. | |
752 | * | |
753 | * The new vconn will automatically negotiate an OpenFlow protocol version | |
754 | * acceptable to both peers on the connection. The version negotiated will be | |
755 | * no lower than 'min_version' and no higher than OFP_VERSION. | |
756 | * | |
757 | * pvconn_accept() will not block waiting for a connection. If no connection | |
758 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
759 | int | |
760 | pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn) | |
761 | { | |
762 | int retval = (pvconn->class->accept)(pvconn, new_vconn); | |
763 | if (retval) { | |
764 | *new_vconn = NULL; | |
765 | } else { | |
766 | assert((*new_vconn)->state != VCS_CONNECTING | |
767 | || (*new_vconn)->class->connect); | |
768 | (*new_vconn)->min_version = min_version; | |
769 | } | |
770 | return retval; | |
771 | } | |
772 | ||
773 | void | |
774 | pvconn_wait(struct pvconn *pvconn) | |
775 | { | |
776 | (pvconn->class->wait)(pvconn); | |
777 | } | |
778 | ||
779 | /* XXX we should really use consecutive xids to avoid probabilistic | |
780 | * failures. */ | |
781 | static inline uint32_t | |
782 | alloc_xid(void) | |
783 | { | |
784 | return random_uint32(); | |
785 | } | |
786 | ||
787 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of | |
788 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
789 | * an arbitrary transaction id. Allocated bytes beyond the header, if any, are | |
790 | * zeroed. | |
791 | * | |
792 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
793 | * needed. | |
794 | * | |
795 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
796 | * message is later extended, the length should be updated with | |
797 | * update_openflow_length() before sending. | |
798 | * | |
799 | * Returns the header. */ | |
800 | void * | |
801 | make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp) | |
802 | { | |
803 | *bufferp = ofpbuf_new(openflow_len); | |
804 | return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp); | |
805 | } | |
806 | ||
807 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of | |
808 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
809 | * transaction id 'xid'. Allocated bytes beyond the header, if any, are | |
810 | * zeroed. | |
811 | * | |
812 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
813 | * needed. | |
814 | * | |
815 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
816 | * message is later extended, the length should be updated with | |
817 | * update_openflow_length() before sending. | |
818 | * | |
819 | * Returns the header. */ | |
820 | void * | |
821 | make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid, | |
822 | struct ofpbuf **bufferp) | |
823 | { | |
824 | *bufferp = ofpbuf_new(openflow_len); | |
825 | return put_openflow_xid(openflow_len, type, xid, *bufferp); | |
826 | } | |
827 | ||
828 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header | |
829 | * with the given 'type' and an arbitrary transaction id. Allocated bytes | |
830 | * beyond the header, if any, are zeroed. | |
831 | * | |
832 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
833 | * message is later extended, the length should be updated with | |
834 | * update_openflow_length() before sending. | |
835 | * | |
836 | * Returns the header. */ | |
837 | void * | |
838 | put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer) | |
839 | { | |
840 | return put_openflow_xid(openflow_len, type, alloc_xid(), buffer); | |
841 | } | |
842 | ||
843 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header | |
844 | * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond | |
845 | * the header, if any, are zeroed. | |
846 | * | |
847 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
848 | * message is later extended, the length should be updated with | |
849 | * update_openflow_length() before sending. | |
850 | * | |
851 | * Returns the header. */ | |
852 | void * | |
853 | put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid, | |
854 | struct ofpbuf *buffer) | |
855 | { | |
856 | struct ofp_header *oh; | |
857 | ||
858 | assert(openflow_len >= sizeof *oh); | |
859 | assert(openflow_len <= UINT16_MAX); | |
860 | ||
861 | oh = ofpbuf_put_uninit(buffer, openflow_len); | |
862 | oh->version = OFP_VERSION; | |
863 | oh->type = type; | |
864 | oh->length = htons(openflow_len); | |
865 | oh->xid = xid; | |
866 | memset(oh + 1, 0, openflow_len - sizeof *oh); | |
867 | return oh; | |
868 | } | |
869 | ||
870 | /* Updates the 'length' field of the OpenFlow message in 'buffer' to | |
871 | * 'buffer->size'. */ | |
872 | void | |
873 | update_openflow_length(struct ofpbuf *buffer) | |
874 | { | |
875 | struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh); | |
876 | oh->length = htons(buffer->size); | |
877 | } | |
878 | ||
879 | struct ofpbuf * | |
880 | make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len) | |
881 | { | |
882 | struct ofp_flow_mod *ofm; | |
883 | size_t size = sizeof *ofm + actions_len; | |
884 | struct ofpbuf *out = ofpbuf_new(size); | |
885 | ofm = ofpbuf_put_zeros(out, sizeof *ofm); | |
886 | ofm->header.version = OFP_VERSION; | |
887 | ofm->header.type = OFPT_FLOW_MOD; | |
888 | ofm->header.length = htons(size); | |
39997502 | 889 | ofm->cookie = 0; |
064af421 BP |
890 | ofm->match.wildcards = htonl(0); |
891 | ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL | |
892 | : flow->in_port); | |
893 | memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src); | |
894 | memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst); | |
895 | ofm->match.dl_vlan = flow->dl_vlan; | |
959a2ecd | 896 | ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp; |
064af421 BP |
897 | ofm->match.dl_type = flow->dl_type; |
898 | ofm->match.nw_src = flow->nw_src; | |
899 | ofm->match.nw_dst = flow->nw_dst; | |
900 | ofm->match.nw_proto = flow->nw_proto; | |
834377ea | 901 | ofm->match.nw_tos = flow->nw_tos; |
064af421 BP |
902 | ofm->match.tp_src = flow->tp_src; |
903 | ofm->match.tp_dst = flow->tp_dst; | |
904 | ofm->command = htons(command); | |
905 | return out; | |
906 | } | |
907 | ||
908 | struct ofpbuf * | |
909 | make_add_flow(const flow_t *flow, uint32_t buffer_id, | |
910 | uint16_t idle_timeout, size_t actions_len) | |
911 | { | |
912 | struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len); | |
913 | struct ofp_flow_mod *ofm = out->data; | |
914 | ofm->idle_timeout = htons(idle_timeout); | |
915 | ofm->hard_timeout = htons(OFP_FLOW_PERMANENT); | |
916 | ofm->buffer_id = htonl(buffer_id); | |
917 | return out; | |
918 | } | |
919 | ||
920 | struct ofpbuf * | |
921 | make_del_flow(const flow_t *flow) | |
922 | { | |
923 | struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0); | |
924 | struct ofp_flow_mod *ofm = out->data; | |
925 | ofm->out_port = htons(OFPP_NONE); | |
926 | return out; | |
927 | } | |
928 | ||
929 | struct ofpbuf * | |
930 | make_add_simple_flow(const flow_t *flow, | |
931 | uint32_t buffer_id, uint16_t out_port, | |
932 | uint16_t idle_timeout) | |
933 | { | |
934 | struct ofp_action_output *oao; | |
935 | struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout, | |
936 | sizeof *oao); | |
937 | oao = ofpbuf_put_zeros(buffer, sizeof *oao); | |
938 | oao->type = htons(OFPAT_OUTPUT); | |
939 | oao->len = htons(sizeof *oao); | |
940 | oao->port = htons(out_port); | |
941 | return buffer; | |
942 | } | |
943 | ||
372179d4 BP |
944 | struct ofpbuf * |
945 | make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason, | |
946 | const struct ofpbuf *payload, int max_send_len) | |
947 | { | |
948 | struct ofp_packet_in *opi; | |
949 | struct ofpbuf *buf; | |
950 | int send_len; | |
951 | ||
952 | send_len = MIN(max_send_len, payload->size); | |
953 | buf = ofpbuf_new(sizeof *opi + send_len); | |
954 | opi = put_openflow_xid(offsetof(struct ofp_packet_in, data), | |
955 | OFPT_PACKET_IN, 0, buf); | |
956 | opi->buffer_id = htonl(buffer_id); | |
957 | opi->total_len = htons(payload->size); | |
958 | opi->in_port = htons(in_port); | |
959 | opi->reason = reason; | |
960 | ofpbuf_put(buf, payload->data, send_len); | |
961 | update_openflow_length(buf); | |
962 | ||
963 | return buf; | |
964 | } | |
965 | ||
064af421 BP |
966 | struct ofpbuf * |
967 | make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id, | |
968 | uint16_t in_port, | |
969 | const struct ofp_action_header *actions, size_t n_actions) | |
970 | { | |
971 | size_t actions_len = n_actions * sizeof *actions; | |
972 | struct ofp_packet_out *opo; | |
973 | size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0); | |
974 | struct ofpbuf *out = ofpbuf_new(size); | |
975 | ||
976 | opo = ofpbuf_put_uninit(out, sizeof *opo); | |
977 | opo->header.version = OFP_VERSION; | |
978 | opo->header.type = OFPT_PACKET_OUT; | |
979 | opo->header.length = htons(size); | |
980 | opo->header.xid = htonl(0); | |
981 | opo->buffer_id = htonl(buffer_id); | |
982 | opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port); | |
983 | opo->actions_len = htons(actions_len); | |
984 | ofpbuf_put(out, actions, actions_len); | |
985 | if (packet) { | |
986 | ofpbuf_put(out, packet->data, packet->size); | |
987 | } | |
988 | return out; | |
989 | } | |
990 | ||
991 | struct ofpbuf * | |
992 | make_unbuffered_packet_out(const struct ofpbuf *packet, | |
993 | uint16_t in_port, uint16_t out_port) | |
994 | { | |
995 | struct ofp_action_output action; | |
996 | action.type = htons(OFPAT_OUTPUT); | |
997 | action.len = htons(sizeof action); | |
998 | action.port = htons(out_port); | |
999 | return make_packet_out(packet, UINT32_MAX, in_port, | |
1000 | (struct ofp_action_header *) &action, 1); | |
1001 | } | |
1002 | ||
1003 | struct ofpbuf * | |
1004 | make_buffered_packet_out(uint32_t buffer_id, | |
1005 | uint16_t in_port, uint16_t out_port) | |
1006 | { | |
1007 | struct ofp_action_output action; | |
1008 | action.type = htons(OFPAT_OUTPUT); | |
1009 | action.len = htons(sizeof action); | |
1010 | action.port = htons(out_port); | |
1011 | return make_packet_out(NULL, buffer_id, in_port, | |
1012 | (struct ofp_action_header *) &action, 1); | |
1013 | } | |
1014 | ||
1015 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ | |
1016 | struct ofpbuf * | |
1017 | make_echo_request(void) | |
1018 | { | |
1019 | struct ofp_header *rq; | |
1020 | struct ofpbuf *out = ofpbuf_new(sizeof *rq); | |
1021 | rq = ofpbuf_put_uninit(out, sizeof *rq); | |
1022 | rq->version = OFP_VERSION; | |
1023 | rq->type = OFPT_ECHO_REQUEST; | |
1024 | rq->length = htons(sizeof *rq); | |
1025 | rq->xid = 0; | |
1026 | return out; | |
1027 | } | |
1028 | ||
1029 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
1030 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
1031 | struct ofpbuf * | |
1032 | make_echo_reply(const struct ofp_header *rq) | |
1033 | { | |
1034 | size_t size = ntohs(rq->length); | |
1035 | struct ofpbuf *out = ofpbuf_new(size); | |
1036 | struct ofp_header *reply = ofpbuf_put(out, rq, size); | |
1037 | reply->type = OFPT_ECHO_REPLY; | |
1038 | return out; | |
1039 | } | |
1040 | ||
1041 | static int | |
1042 | check_message_type(uint8_t got_type, uint8_t want_type) | |
1043 | { | |
1044 | if (got_type != want_type) { | |
1045 | char *want_type_name = ofp_message_type_to_string(want_type); | |
1046 | char *got_type_name = ofp_message_type_to_string(got_type); | |
1047 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
1048 | "received bad message type %s (expected %s)", | |
1049 | got_type_name, want_type_name); | |
1050 | free(want_type_name); | |
1051 | free(got_type_name); | |
1052 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE); | |
1053 | } | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long. | |
1058 | * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced | |
1059 | * with ofp_mkerr()). */ | |
1060 | int | |
1061 | check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size) | |
1062 | { | |
1063 | size_t got_size; | |
1064 | int error; | |
1065 | ||
1066 | error = check_message_type(msg->type, type); | |
1067 | if (error) { | |
1068 | return error; | |
1069 | } | |
1070 | ||
1071 | got_size = ntohs(msg->length); | |
1072 | if (got_size != size) { | |
1073 | char *type_name = ofp_message_type_to_string(type); | |
1074 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
2886875a | 1075 | "received %s message of length %zu (expected %zu)", |
064af421 BP |
1076 | type_name, got_size, size); |
1077 | free(type_name); | |
49bdc010 | 1078 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
064af421 BP |
1079 | } |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a | |
1085 | * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if | |
1086 | * the checks pass, otherwise an OpenFlow error code (produced with | |
1087 | * ofp_mkerr()). | |
1088 | * | |
1089 | * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of | |
1090 | * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when | |
1091 | * successful. */ | |
1092 | int | |
1093 | check_ofp_message_array(const struct ofp_header *msg, uint8_t type, | |
1094 | size_t min_size, size_t array_elt_size, | |
1095 | size_t *n_array_elts) | |
1096 | { | |
1097 | size_t got_size; | |
1098 | int error; | |
1099 | ||
1100 | assert(array_elt_size); | |
1101 | ||
1102 | error = check_message_type(msg->type, type); | |
1103 | if (error) { | |
1104 | return error; | |
1105 | } | |
1106 | ||
1107 | got_size = ntohs(msg->length); | |
1108 | if (got_size < min_size) { | |
1109 | char *type_name = ofp_message_type_to_string(type); | |
2886875a | 1110 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu " |
064af421 BP |
1111 | "(expected at least %zu)", |
1112 | type_name, got_size, min_size); | |
1113 | free(type_name); | |
49bdc010 | 1114 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
064af421 BP |
1115 | } |
1116 | if ((got_size - min_size) % array_elt_size) { | |
1117 | char *type_name = ofp_message_type_to_string(type); | |
1118 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
2886875a | 1119 | "received %s message of bad length %zu: the " |
064af421 BP |
1120 | "excess over %zu (%zu) is not evenly divisible by %zu " |
1121 | "(remainder is %zu)", | |
1122 | type_name, got_size, min_size, got_size - min_size, | |
1123 | array_elt_size, (got_size - min_size) % array_elt_size); | |
1124 | free(type_name); | |
49bdc010 | 1125 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
064af421 BP |
1126 | } |
1127 | if (n_array_elts) { | |
1128 | *n_array_elts = (got_size - min_size) / array_elt_size; | |
1129 | } | |
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | int | |
1134 | check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data, | |
1135 | int *n_actionsp, int max_ports) | |
1136 | { | |
1137 | const struct ofp_packet_out *opo; | |
1138 | unsigned int actions_len, n_actions; | |
1139 | size_t extra; | |
1140 | int error; | |
1141 | ||
1142 | *n_actionsp = 0; | |
1143 | error = check_ofp_message_array(oh, OFPT_PACKET_OUT, | |
1144 | sizeof *opo, 1, &extra); | |
1145 | if (error) { | |
1146 | return error; | |
1147 | } | |
1148 | opo = (const struct ofp_packet_out *) oh; | |
1149 | ||
1150 | actions_len = ntohs(opo->actions_len); | |
1151 | if (actions_len > extra) { | |
2886875a | 1152 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions " |
064af421 BP |
1153 | "but message has room for only %zu bytes", |
1154 | actions_len, extra); | |
49bdc010 | 1155 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
064af421 BP |
1156 | } |
1157 | if (actions_len % sizeof(union ofp_action)) { | |
2886875a | 1158 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, " |
064af421 BP |
1159 | "which is not a multiple of %zu", |
1160 | actions_len, sizeof(union ofp_action)); | |
49bdc010 | 1161 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
064af421 BP |
1162 | } |
1163 | ||
1164 | n_actions = actions_len / sizeof(union ofp_action); | |
1165 | error = validate_actions((const union ofp_action *) opo->actions, | |
1166 | n_actions, max_ports); | |
1167 | if (error) { | |
1168 | return error; | |
1169 | } | |
1170 | ||
1171 | data->data = (void *) &opo->actions[n_actions]; | |
1172 | data->size = extra - actions_len; | |
1173 | *n_actionsp = n_actions; | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | const struct ofp_flow_stats * | |
1178 | flow_stats_first(struct flow_stats_iterator *iter, | |
1179 | const struct ofp_stats_reply *osr) | |
1180 | { | |
1181 | iter->pos = osr->body; | |
1182 | iter->end = osr->body + (ntohs(osr->header.length) | |
1183 | - offsetof(struct ofp_stats_reply, body)); | |
1184 | return flow_stats_next(iter); | |
1185 | } | |
1186 | ||
1187 | const struct ofp_flow_stats * | |
1188 | flow_stats_next(struct flow_stats_iterator *iter) | |
1189 | { | |
1190 | ptrdiff_t bytes_left = iter->end - iter->pos; | |
1191 | const struct ofp_flow_stats *fs; | |
1192 | size_t length; | |
1193 | ||
1194 | if (bytes_left < sizeof *fs) { | |
1195 | if (bytes_left != 0) { | |
1196 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
1197 | "%td leftover bytes in flow stats reply", bytes_left); | |
1198 | } | |
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | fs = (const void *) iter->pos; | |
1203 | length = ntohs(fs->length); | |
1204 | if (length < sizeof *fs) { | |
1205 | VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than " | |
1206 | "min %zu", length, sizeof *fs); | |
1207 | return NULL; | |
1208 | } else if (length > bytes_left) { | |
1209 | VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td " | |
1210 | "bytes left", length, bytes_left); | |
1211 | return NULL; | |
1212 | } else if ((length - sizeof *fs) % sizeof fs->actions[0]) { | |
1213 | VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes " | |
1214 | "left over in final action", length, | |
1215 | (length - sizeof *fs) % sizeof fs->actions[0]); | |
1216 | return NULL; | |
1217 | } | |
1218 | iter->pos += length; | |
1219 | return fs; | |
1220 | } | |
1221 | ||
1222 | /* Alignment of ofp_actions. */ | |
1223 | #define ACTION_ALIGNMENT 8 | |
1224 | ||
1225 | static int | |
1226 | check_action_exact_len(const union ofp_action *a, unsigned int len, | |
1227 | unsigned int required_len) | |
1228 | { | |
1229 | if (len != required_len) { | |
1230 | VLOG_DBG_RL(&bad_ofmsg_rl, | |
1231 | "action %u has invalid length %"PRIu16" (must be %u)\n", | |
1232 | a->type, ntohs(a->header.len), required_len); | |
1233 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1234 | } | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | static int | |
1239 | check_action_port(int port, int max_ports) | |
1240 | { | |
1241 | switch (port) { | |
1242 | case OFPP_IN_PORT: | |
1243 | case OFPP_TABLE: | |
1244 | case OFPP_NORMAL: | |
1245 | case OFPP_FLOOD: | |
1246 | case OFPP_ALL: | |
1247 | case OFPP_CONTROLLER: | |
1248 | case OFPP_LOCAL: | |
1249 | return 0; | |
1250 | ||
1251 | default: | |
1252 | if (port >= 0 && port < max_ports) { | |
1253 | return 0; | |
1254 | } | |
1255 | VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port); | |
1256 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT); | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | static int | |
1261 | check_nicira_action(const union ofp_action *a, unsigned int len) | |
1262 | { | |
1263 | const struct nx_action_header *nah; | |
1264 | ||
1265 | if (len < 16) { | |
1266 | VLOG_DBG_RL(&bad_ofmsg_rl, | |
1267 | "Nicira vendor action only %u bytes", len); | |
1268 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1269 | } | |
1270 | nah = (const struct nx_action_header *) a; | |
1271 | ||
1272 | switch (ntohs(nah->subtype)) { | |
1273 | case NXAST_RESUBMIT: | |
1274 | return check_action_exact_len(a, len, 16); | |
1275 | default: | |
1276 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE); | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | static int | |
1281 | check_action(const union ofp_action *a, unsigned int len, int max_ports) | |
1282 | { | |
1283 | int error; | |
1284 | ||
dc6fe1d7 | 1285 | switch (ntohs(a->type)) { |
064af421 BP |
1286 | case OFPAT_OUTPUT: |
1287 | error = check_action_port(ntohs(a->output.port), max_ports); | |
1288 | if (error) { | |
1289 | return error; | |
1290 | } | |
1291 | return check_action_exact_len(a, len, 8); | |
1292 | ||
1293 | case OFPAT_SET_VLAN_VID: | |
1294 | case OFPAT_SET_VLAN_PCP: | |
1295 | case OFPAT_STRIP_VLAN: | |
1296 | case OFPAT_SET_NW_SRC: | |
1297 | case OFPAT_SET_NW_DST: | |
959a2ecd | 1298 | case OFPAT_SET_NW_TOS: |
064af421 BP |
1299 | case OFPAT_SET_TP_SRC: |
1300 | case OFPAT_SET_TP_DST: | |
1301 | return check_action_exact_len(a, len, 8); | |
1302 | ||
1303 | case OFPAT_SET_DL_SRC: | |
1304 | case OFPAT_SET_DL_DST: | |
1305 | return check_action_exact_len(a, len, 16); | |
1306 | ||
1307 | case OFPAT_VENDOR: | |
1308 | if (a->vendor.vendor == htonl(NX_VENDOR_ID)) { | |
1309 | return check_nicira_action(a, len); | |
1310 | } else { | |
1311 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR); | |
1312 | } | |
1313 | break; | |
1314 | ||
1315 | default: | |
5f21d20e JP |
1316 | VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16, |
1317 | ntohs(a->type)); | |
064af421 BP |
1318 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE); |
1319 | } | |
1320 | ||
1321 | if (!len) { | |
1322 | VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0"); | |
1323 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1324 | } | |
1325 | if (len % ACTION_ALIGNMENT) { | |
1326 | VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple of %d", | |
1327 | len, ACTION_ALIGNMENT); | |
1328 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1329 | } | |
1330 | return 0; | |
1331 | } | |
1332 | ||
1333 | int | |
1334 | validate_actions(const union ofp_action *actions, size_t n_actions, | |
1335 | int max_ports) | |
1336 | { | |
1337 | const union ofp_action *a; | |
1338 | ||
1339 | for (a = actions; a < &actions[n_actions]; ) { | |
1340 | unsigned int len = ntohs(a->header.len); | |
1341 | unsigned int n_slots = len / ACTION_ALIGNMENT; | |
1342 | unsigned int slots_left = &actions[n_actions] - a; | |
1343 | int error; | |
1344 | ||
1345 | if (n_slots > slots_left) { | |
1346 | VLOG_DBG_RL(&bad_ofmsg_rl, | |
2886875a | 1347 | "action requires %u slots but only %u remain", |
064af421 BP |
1348 | n_slots, slots_left); |
1349 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1350 | } | |
1351 | error = check_action(a, len, max_ports); | |
1352 | if (error) { | |
1353 | return error; | |
1354 | } | |
1355 | a += n_slots; | |
1356 | } | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | /* The set of actions must either come from a trusted source or have been | |
1361 | * previously validated with validate_actions(). */ | |
1362 | const union ofp_action * | |
1363 | actions_first(struct actions_iterator *iter, | |
1364 | const union ofp_action *oa, size_t n_actions) | |
1365 | { | |
1366 | iter->pos = oa; | |
1367 | iter->end = oa + n_actions; | |
1368 | return actions_next(iter); | |
1369 | } | |
1370 | ||
1371 | const union ofp_action * | |
1372 | actions_next(struct actions_iterator *iter) | |
1373 | { | |
1374 | if (iter->pos < iter->end) { | |
1375 | const union ofp_action *a = iter->pos; | |
1376 | unsigned int len = ntohs(a->header.len); | |
1377 | iter->pos += len / ACTION_ALIGNMENT; | |
1378 | return a; | |
1379 | } else { | |
1380 | return NULL; | |
1381 | } | |
1382 | } | |
1383 | ||
1384 | void | |
1385 | normalize_match(struct ofp_match *m) | |
1386 | { | |
1387 | enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO }; | |
1388 | enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST }; | |
1389 | uint32_t wc; | |
1390 | ||
1391 | wc = ntohl(m->wildcards) & OFPFW_ALL; | |
1392 | if (wc & OFPFW_DL_TYPE) { | |
1393 | m->dl_type = 0; | |
1394 | ||
8ddb3f37 | 1395 | /* Can't sensibly match on network or transport headers if the |
064af421 BP |
1396 | * data link type is unknown. */ |
1397 | wc |= OFPFW_NW | OFPFW_TP; | |
1398 | m->nw_src = m->nw_dst = m->nw_proto = 0; | |
1399 | m->tp_src = m->tp_dst = 0; | |
1400 | } else if (m->dl_type == htons(ETH_TYPE_IP)) { | |
1401 | if (wc & OFPFW_NW_PROTO) { | |
1402 | m->nw_proto = 0; | |
1403 | ||
8ddb3f37 | 1404 | /* Can't sensibly match on transport headers if the network |
064af421 BP |
1405 | * protocol is unknown. */ |
1406 | wc |= OFPFW_TP; | |
1407 | m->tp_src = m->tp_dst = 0; | |
1408 | } else if (m->nw_proto == IPPROTO_TCP || | |
1409 | m->nw_proto == IPPROTO_UDP || | |
1410 | m->nw_proto == IPPROTO_ICMP) { | |
1411 | if (wc & OFPFW_TP_SRC) { | |
1412 | m->tp_src = 0; | |
1413 | } | |
1414 | if (wc & OFPFW_TP_DST) { | |
1415 | m->tp_dst = 0; | |
1416 | } | |
1417 | } else { | |
1418 | /* Transport layer fields will always be extracted as zeros, so we | |
8ddb3f37 | 1419 | * can do an exact-match on those values. */ |
064af421 BP |
1420 | wc &= ~OFPFW_TP; |
1421 | m->tp_src = m->tp_dst = 0; | |
1422 | } | |
1423 | if (wc & OFPFW_NW_SRC_MASK) { | |
1424 | m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT); | |
1425 | } | |
1426 | if (wc & OFPFW_NW_DST_MASK) { | |
1427 | m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT); | |
1428 | } | |
fb892732 JP |
1429 | } else if (m->dl_type == htons(ETH_TYPE_ARP)) { |
1430 | if (wc & OFPFW_NW_PROTO) { | |
1431 | m->nw_proto = 0; | |
1432 | } | |
1433 | if (wc & OFPFW_NW_SRC_MASK) { | |
1434 | m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT); | |
1435 | } | |
1436 | if (wc & OFPFW_NW_DST_MASK) { | |
1437 | m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT); | |
1438 | } | |
1439 | m->tp_src = m->tp_dst = 0; | |
064af421 BP |
1440 | } else { |
1441 | /* Network and transport layer fields will always be extracted as | |
8ddb3f37 | 1442 | * zeros, so we can do an exact-match on those values. */ |
064af421 BP |
1443 | wc &= ~(OFPFW_NW | OFPFW_TP); |
1444 | m->nw_proto = m->nw_src = m->nw_dst = 0; | |
1445 | m->tp_src = m->tp_dst = 0; | |
1446 | } | |
1447 | if (wc & OFPFW_DL_SRC) { | |
1448 | memset(m->dl_src, 0, sizeof m->dl_src); | |
1449 | } | |
1450 | if (wc & OFPFW_DL_DST) { | |
1451 | memset(m->dl_dst, 0, sizeof m->dl_dst); | |
1452 | } | |
1453 | m->wildcards = htonl(wc); | |
1454 | } | |
1455 | ||
85ab0a02 BP |
1456 | /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'. |
1457 | * The initial connection status, supplied as 'connect_status', is interpreted | |
1458 | * as follows: | |
1459 | * | |
1460 | * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be | |
1461 | * called in the normal fashion. | |
1462 | * | |
1463 | * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect' | |
1464 | * function should be called to complete the connection. | |
1465 | * | |
1466 | * - Other positive errno values indicate that the connection failed with | |
1467 | * the specified error. | |
1468 | * | |
1469 | * After calling this function, vconn_close() must be used to destroy 'vconn', | |
1470 | * otherwise resources will be leaked. | |
1471 | * | |
1472 | * The caller retains ownership of 'name'. */ | |
064af421 BP |
1473 | void |
1474 | vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status, | |
85ab0a02 | 1475 | const char *name) |
064af421 BP |
1476 | { |
1477 | vconn->class = class; | |
1478 | vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING | |
1479 | : !connect_status ? VCS_SEND_HELLO | |
1480 | : VCS_DISCONNECTED); | |
1481 | vconn->error = connect_status; | |
1482 | vconn->version = -1; | |
1483 | vconn->min_version = -1; | |
d7cca867 BP |
1484 | vconn->remote_ip = 0; |
1485 | vconn->remote_port = 0; | |
193456d5 JP |
1486 | vconn->local_ip = 0; |
1487 | vconn->local_port = 0; | |
064af421 | 1488 | vconn->name = xstrdup(name); |
e0668bd1 | 1489 | assert(vconn->state != VCS_CONNECTING || class->connect); |
064af421 BP |
1490 | } |
1491 | ||
d7cca867 BP |
1492 | void |
1493 | vconn_set_remote_ip(struct vconn *vconn, uint32_t ip) | |
1494 | { | |
1495 | vconn->remote_ip = ip; | |
1496 | } | |
1497 | ||
1498 | void | |
1499 | vconn_set_remote_port(struct vconn *vconn, uint16_t port) | |
1500 | { | |
1501 | vconn->remote_port = port; | |
1502 | } | |
1503 | ||
193456d5 JP |
1504 | void |
1505 | vconn_set_local_ip(struct vconn *vconn, uint32_t ip) | |
1506 | { | |
1507 | vconn->local_ip = ip; | |
1508 | } | |
1509 | ||
1510 | void | |
1511 | vconn_set_local_port(struct vconn *vconn, uint16_t port) | |
1512 | { | |
1513 | vconn->local_port = port; | |
1514 | } | |
1515 | ||
064af421 BP |
1516 | void |
1517 | pvconn_init(struct pvconn *pvconn, struct pvconn_class *class, | |
1518 | const char *name) | |
1519 | { | |
1520 | pvconn->class = class; | |
1521 | pvconn->name = xstrdup(name); | |
1522 | } |