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