]> git.proxmox.com Git - ovs.git/blame - lib/vconn.c
vconn: Reply with OFPBRC_BAD_VERSION for bad version.
[ovs.git] / lib / vconn.c
CommitLineData
064af421 1/*
82c8c53c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
18#include "vconn-provider.h"
064af421
BP
19#include <errno.h>
20#include <inttypes.h>
21#include <netinet/in.h>
22#include <poll.h>
23#include <stdlib.h>
24#include <string.h>
25#include "coverage.h"
26#include "dynamic-string.h"
b302749b 27#include "fatal-signal.h"
064af421 28#include "flow.h"
90bf1e07 29#include "ofp-errors.h"
982697a4 30#include "ofp-msgs.h"
064af421 31#include "ofp-print.h"
fa37b408 32#include "ofp-util.h"
064af421
BP
33#include "ofpbuf.h"
34#include "openflow/nicira-ext.h"
35#include "openflow/openflow.h"
36#include "packets.h"
37#include "poll-loop.h"
38#include "random.h"
39#include "util.h"
064af421 40#include "vlog.h"
f125905c 41#include "socket-util.h"
064af421 42
d98e6007 43VLOG_DEFINE_THIS_MODULE(vconn);
5136ce49 44
d76f09ea
BP
45COVERAGE_DEFINE(vconn_open);
46COVERAGE_DEFINE(vconn_received);
47COVERAGE_DEFINE(vconn_sent);
48
064af421
BP
49/* State of an active vconn.*/
50enum vconn_state {
51 /* This is the ordinary progression of states. */
52 VCS_CONNECTING, /* Underlying vconn is not connected. */
53 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
54 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
55 VCS_CONNECTED, /* Connection established. */
56
57 /* These states are entered only when something goes wrong. */
58 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
59 VCS_DISCONNECTED /* Connection failed or connection closed. */
60};
61
5b712636 62static const struct vconn_class *vconn_classes[] = {
064af421
BP
63 &tcp_vconn_class,
64 &unix_vconn_class,
65#ifdef HAVE_OPENSSL
66 &ssl_vconn_class,
67#endif
68};
69
5b712636 70static const struct pvconn_class *pvconn_classes[] = {
064af421
BP
71 &ptcp_pvconn_class,
72 &punix_pvconn_class,
73#ifdef HAVE_OPENSSL
74 &pssl_pvconn_class,
75#endif
76};
77
78/* Rate limit for individual OpenFlow messages going over the vconn, output at
79 * DBG level. This is very high because, if these are enabled, it is because
80 * we really need to see them. */
81static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
82
83/* Rate limit for OpenFlow message parse errors. These always indicate a bug
84 * in the peer and so there's not much point in showing a lot of them. */
85static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
86
87static int do_recv(struct vconn *, struct ofpbuf **);
88static int do_send(struct vconn *, struct ofpbuf *);
89
90/* Check the validity of the vconn class structures. */
91static void
92check_vconn_classes(void)
93{
94#ifndef NDEBUG
95 size_t i;
96
97 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
5b712636 98 const struct vconn_class *class = vconn_classes[i];
cb22974d
BP
99 ovs_assert(class->name != NULL);
100 ovs_assert(class->open != NULL);
60cb3eb8
BP
101 if (class->close || class->recv || class->send
102 || class->run || class->run_wait || class->wait) {
cb22974d
BP
103 ovs_assert(class->close != NULL);
104 ovs_assert(class->recv != NULL);
105 ovs_assert(class->send != NULL);
106 ovs_assert(class->wait != NULL);
064af421
BP
107 } else {
108 /* This class delegates to another one. */
109 }
110 }
111
112 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
5b712636 113 const struct pvconn_class *class = pvconn_classes[i];
cb22974d
BP
114 ovs_assert(class->name != NULL);
115 ovs_assert(class->listen != NULL);
064af421 116 if (class->close || class->accept || class->wait) {
cb22974d
BP
117 ovs_assert(class->close != NULL);
118 ovs_assert(class->accept != NULL);
119 ovs_assert(class->wait != NULL);
064af421
BP
120 } else {
121 /* This class delegates to another one. */
122 }
123 }
124#endif
125}
126
127/* Prints information on active (if 'active') and passive (if 'passive')
128 * connection methods supported by the vconn. If 'bootstrap' is true, also
129 * advertises options to bootstrap the CA certificate. */
130void
67a4917b 131vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
064af421
BP
132{
133 /* Really this should be implemented via callbacks into the vconn
134 * providers, but that seems too heavy-weight to bother with at the
135 * moment. */
d295e8e9 136
064af421
BP
137 printf("\n");
138 if (active) {
139 printf("Active OpenFlow connection methods:\n");
6e797088 140 printf(" tcp:IP[:PORT] "
125b0291 141 "PORT (default: %d) at remote IP\n", OFP_OLD_PORT);
064af421 142#ifdef HAVE_OPENSSL
6e797088 143 printf(" ssl:IP[:PORT] "
125b0291 144 "SSL PORT (default: %d) at remote IP\n", OFP_OLD_PORT);
064af421
BP
145#endif
146 printf(" unix:FILE Unix domain socket named FILE\n");
147 }
148
149 if (passive) {
150 printf("Passive OpenFlow connection methods:\n");
78ff0270
BP
151 printf(" ptcp:[PORT][:IP] "
152 "listen to TCP PORT (default: %d) on IP\n",
125b0291 153 OFP_OLD_PORT);
064af421 154#ifdef HAVE_OPENSSL
78ff0270
BP
155 printf(" pssl:[PORT][:IP] "
156 "listen for SSL on PORT (default: %d) on IP\n",
125b0291 157 OFP_OLD_PORT);
064af421
BP
158#endif
159 printf(" punix:FILE "
160 "listen on Unix domain socket FILE\n");
161 }
162
163#ifdef HAVE_OPENSSL
164 printf("PKI configuration (required to use SSL):\n"
165 " -p, --private-key=FILE file with private key\n"
166 " -c, --certificate=FILE file with certificate for private key\n"
167 " -C, --ca-cert=FILE file with peer CA certificate\n");
168 if (bootstrap) {
169 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
170 "to read or create\n");
171 }
172#endif
173}
174
30012c72
BP
175/* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
176 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
177 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
178 * class exists. */
179static int
5b712636 180vconn_lookup_class(const char *name, const struct vconn_class **classp)
30012c72
BP
181{
182 size_t prefix_len;
183
184 prefix_len = strcspn(name, ":");
185 if (name[prefix_len] != '\0') {
186 size_t i;
187
188 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
5b712636 189 const struct vconn_class *class = vconn_classes[i];
30012c72
BP
190 if (strlen(class->name) == prefix_len
191 && !memcmp(class->name, name, prefix_len)) {
192 *classp = class;
193 return 0;
194 }
195 }
196 }
197
198 *classp = NULL;
199 return EAFNOSUPPORT;
200}
201
202/* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
203 * a supported connection type, otherwise EAFNOSUPPORT. */
204int
205vconn_verify_name(const char *name)
206{
5b712636 207 const struct vconn_class *class;
30012c72
BP
208 return vconn_lookup_class(name, &class);
209}
210
064af421
BP
211/* Attempts to connect to an OpenFlow device. 'name' is a connection name in
212 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
213 * are vconn class-specific.
214 *
215 * The vconn will automatically negotiate an OpenFlow protocol version
216 * acceptable to both peers on the connection. The version negotiated will be
7a25bd99
SH
217 * one of those in the 'allowed_versions' bitmap: version 'x' is allowed if
218 * allowed_versions & (1 << x) is nonzero. If 'allowed_versions' is zero, then
219 * OFPUTIL_DEFAULT_VERSIONS are allowed.
064af421
BP
220 *
221 * Returns 0 if successful, otherwise a positive errno value. If successful,
222 * stores a pointer to the new connection in '*vconnp', otherwise a null
223 * pointer. */
224int
82c8c53c
BP
225vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
226 struct vconn **vconnp)
064af421 227{
5b712636 228 const struct vconn_class *class;
30012c72
BP
229 struct vconn *vconn;
230 char *suffix_copy;
231 int error;
064af421
BP
232
233 COVERAGE_INC(vconn_open);
234 check_vconn_classes();
235
7a25bd99
SH
236 if (!allowed_versions) {
237 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
238 }
239
30012c72
BP
240 /* Look up the class. */
241 error = vconn_lookup_class(name, &class);
242 if (!class) {
243 goto error;
064af421 244 }
30012c72
BP
245
246 /* Call class's "open" function. */
247 suffix_copy = xstrdup(strchr(name, ':') + 1);
7a25bd99 248 error = class->open(name, allowed_versions, suffix_copy, &vconn, dscp);
30012c72
BP
249 free(suffix_copy);
250 if (error) {
251 goto error;
064af421 252 }
30012c72
BP
253
254 /* Success. */
cb22974d 255 ovs_assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
30012c72
BP
256 *vconnp = vconn;
257 return 0;
258
259error:
260 *vconnp = NULL;
261 return error;
064af421
BP
262}
263
60cb3eb8
BP
264/* Allows 'vconn' to perform maintenance activities, such as flushing output
265 * buffers. */
266void
267vconn_run(struct vconn *vconn)
268{
48d84b17
BP
269 if (vconn->state == VCS_CONNECTING ||
270 vconn->state == VCS_SEND_HELLO ||
271 vconn->state == VCS_RECV_HELLO) {
272 vconn_connect(vconn);
273 }
274
60cb3eb8
BP
275 if (vconn->class->run) {
276 (vconn->class->run)(vconn);
277 }
278}
279
280/* Arranges for the poll loop to wake up when 'vconn' needs to perform
281 * maintenance activities. */
282void
283vconn_run_wait(struct vconn *vconn)
284{
48d84b17
BP
285 if (vconn->state == VCS_CONNECTING ||
286 vconn->state == VCS_SEND_HELLO ||
287 vconn->state == VCS_RECV_HELLO) {
288 vconn_connect_wait(vconn);
289 }
290
60cb3eb8
BP
291 if (vconn->class->run_wait) {
292 (vconn->class->run_wait)(vconn);
293 }
294}
295
064af421 296int
82c8c53c 297vconn_open_block(const char *name, uint32_t allowed_versions, uint8_t dscp,
2e3fa633 298 struct vconn **vconnp)
064af421
BP
299{
300 struct vconn *vconn;
301 int error;
302
b302749b
BP
303 fatal_signal_run();
304
82c8c53c 305 error = vconn_open(name, allowed_versions, dscp, &vconn);
b0bfeb3e 306 if (!error) {
6d1fb217 307 error = vconn_connect_block(vconn);
064af421 308 }
b0bfeb3e 309
064af421
BP
310 if (error) {
311 vconn_close(vconn);
312 *vconnp = NULL;
313 } else {
314 *vconnp = vconn;
315 }
316 return error;
317}
318
319/* Closes 'vconn'. */
320void
321vconn_close(struct vconn *vconn)
322{
323 if (vconn != NULL) {
324 char *name = vconn->name;
325 (vconn->class->close)(vconn);
326 free(name);
327 }
328}
329
330/* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
331const char *
332vconn_get_name(const struct vconn *vconn)
333{
334 return vconn->name;
335}
336
7a25bd99
SH
337/* Returns the allowed_versions of 'vconn', that is,
338 * the allowed_versions passed to vconn_open(). */
339uint32_t
340vconn_get_allowed_versions(const struct vconn *vconn)
341{
342 return vconn->allowed_versions;
343}
344
e182670b
SH
345/* Sets the allowed_versions of 'vconn', overriding
346 * the allowed_versions passed to vconn_open(). */
347void
348vconn_set_allowed_versions(struct vconn *vconn, uint32_t allowed_versions)
349{
350 vconn->allowed_versions = allowed_versions;
351}
352
064af421
BP
353/* Returns the IP address of the peer, or 0 if the peer is not connected over
354 * an IP-based protocol or if its IP address is not yet known. */
4408d18a 355ovs_be32
d295e8e9 356vconn_get_remote_ip(const struct vconn *vconn)
064af421 357{
193456d5
JP
358 return vconn->remote_ip;
359}
360
d295e8e9 361/* Returns the transport port of the peer, or 0 if the connection does not
193456d5 362 * contain a port or if the port is not yet known. */
4408d18a 363ovs_be16
d295e8e9 364vconn_get_remote_port(const struct vconn *vconn)
193456d5
JP
365{
366 return vconn->remote_port;
367}
368
d295e8e9
JP
369/* Returns the IP address used to connect to the peer, or 0 if the
370 * connection is not an IP-based protocol or if its IP address is not
193456d5 371 * yet known. */
4408d18a 372ovs_be32
d295e8e9 373vconn_get_local_ip(const struct vconn *vconn)
193456d5
JP
374{
375 return vconn->local_ip;
376}
377
d295e8e9 378/* Returns the transport port used to connect to the peer, or 0 if the
193456d5 379 * connection does not contain a port or if the port is not yet known. */
4408d18a 380ovs_be16
d295e8e9 381vconn_get_local_port(const struct vconn *vconn)
193456d5
JP
382{
383 return vconn->local_port;
064af421
BP
384}
385
27527aa0
BP
386/* Returns the OpenFlow version negotiated with the peer, or -1 if version
387 * negotiation is not yet complete.
388 *
389 * A vconn that has successfully connected (that is, vconn_connect() or
390 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
733c8ed3 391int
27527aa0
BP
392vconn_get_version(const struct vconn *vconn)
393{
733c8ed3 394 return vconn->version ? vconn->version : -1;
27527aa0
BP
395}
396
4766ce7a
BP
397/* By default, a vconn accepts only OpenFlow messages whose version matches the
398 * one negotiated for the connection. A message received with a different
399 * version is an error that causes the vconn to drop the connection.
400 *
401 * This functions allows 'vconn' to accept messages with any OpenFlow version.
402 * This is useful in the special case where 'vconn' is used as an rconn
403 * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is
404 * used as a target for mirroring OpenFlow messages for debugging and
405 * troubleshooting.
406 *
407 * This function should be called after a successful vconn_open() or
408 * pvconn_accept() but before the connection completes, that is, before
409 * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn'
410 * beforehand with an unexpected version will the vconn to drop the
411 * connection. */
412void
413vconn_set_recv_any_version(struct vconn *vconn)
414{
415 vconn->recv_any_version = true;
416}
417
064af421 418static void
d295e8e9 419vcs_connecting(struct vconn *vconn)
064af421
BP
420{
421 int retval = (vconn->class->connect)(vconn);
cb22974d 422 ovs_assert(retval != EINPROGRESS);
064af421
BP
423 if (!retval) {
424 vconn->state = VCS_SEND_HELLO;
425 } else if (retval != EAGAIN) {
426 vconn->state = VCS_DISCONNECTED;
427 vconn->error = retval;
428 }
429}
430
431static void
432vcs_send_hello(struct vconn *vconn)
433{
434 struct ofpbuf *b;
435 int retval;
436
de6c85b0 437 b = ofputil_encode_hello(vconn->allowed_versions);
064af421
BP
438 retval = do_send(vconn, b);
439 if (!retval) {
440 vconn->state = VCS_RECV_HELLO;
441 } else {
442 ofpbuf_delete(b);
443 if (retval != EAGAIN) {
444 vconn->state = VCS_DISCONNECTED;
445 vconn->error = retval;
446 }
447 }
448}
449
7a25bd99
SH
450static char *
451version_bitmap_to_string(uint32_t bitmap)
452{
453 struct ds s;
454
455 ds_init(&s);
456 if (!bitmap) {
457 ds_put_cstr(&s, "no versions");
458 } else if (is_pow2(bitmap)) {
459 ds_put_cstr(&s, "version ");
460 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
461 } else if (is_pow2((bitmap >> 1) + 1)) {
462 ds_put_cstr(&s, "version ");
463 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
6cca5c20 464 ds_put_cstr(&s, " and earlier");
7a25bd99
SH
465 } else {
466 ds_put_cstr(&s, "versions ");
467 ofputil_format_version_bitmap(&s, bitmap);
468 }
469 return ds_steal_cstr(&s);
470}
471
064af421
BP
472static void
473vcs_recv_hello(struct vconn *vconn)
474{
475 struct ofpbuf *b;
476 int retval;
477
478 retval = do_recv(vconn, &b);
479 if (!retval) {
982697a4
BP
480 enum ofptype type;
481 enum ofperr error;
064af421 482
982697a4
BP
483 error = ofptype_decode(&type, b->data);
484 if (!error && type == OFPTYPE_HELLO) {
7a25bd99
SH
485 char *peer_s, *local_s;
486 uint32_t common_versions;
487
de6c85b0 488 if (!ofputil_decode_hello(b->data, &vconn->peer_versions)) {
064af421 489 struct ds msg = DS_EMPTY_INITIALIZER;
de6c85b0
SH
490 ds_put_format(&msg, "%s: unknown data in hello:\n",
491 vconn->name);
064af421
BP
492 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
493 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
494 ds_destroy(&msg);
495 }
496
7a25bd99
SH
497 local_s = version_bitmap_to_string(vconn->allowed_versions);
498 peer_s = version_bitmap_to_string(vconn->peer_versions);
499
500 common_versions = vconn->peer_versions & vconn->allowed_versions;
501 if (!common_versions) {
502 vconn->version = leftmost_1bit_idx(vconn->peer_versions);
064af421 503 VLOG_WARN_RL(&bad_ofmsg_rl,
7a25bd99
SH
504 "%s: version negotiation failed (we support "
505 "%s, peer supports %s)",
506 vconn->name, local_s, peer_s);
064af421
BP
507 vconn->state = VCS_SEND_ERROR;
508 } else {
7a25bd99 509 vconn->version = leftmost_1bit_idx(common_versions);
064af421 510 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
7a25bd99
SH
511 "(we support %s, peer supports %s)", vconn->name,
512 vconn->version, local_s, peer_s);
064af421
BP
513 vconn->state = VCS_CONNECTED;
514 }
7a25bd99
SH
515
516 free(local_s);
517 free(peer_s);
518
064af421
BP
519 ofpbuf_delete(b);
520 return;
521 } else {
522 char *s = ofp_to_string(b->data, b->size, 1);
523 VLOG_WARN_RL(&bad_ofmsg_rl,
524 "%s: received message while expecting hello: %s",
525 vconn->name, s);
526 free(s);
527 retval = EPROTO;
528 ofpbuf_delete(b);
529 }
530 }
531
532 if (retval != EAGAIN) {
533 vconn->state = VCS_DISCONNECTED;
b7eae257 534 vconn->error = retval == EOF ? ECONNRESET : retval;
064af421
BP
535 }
536}
537
538static void
539vcs_send_error(struct vconn *vconn)
540{
064af421
BP
541 struct ofpbuf *b;
542 char s[128];
543 int retval;
7a25bd99
SH
544 char *local_s, *peer_s;
545
546 local_s = version_bitmap_to_string(vconn->allowed_versions);
547 peer_s = version_bitmap_to_string(vconn->peer_versions);
548 snprintf(s, sizeof s, "We support %s, you support %s, no common versions.",
549 local_s, peer_s);
550 free(peer_s);
551 free(local_s);
064af421 552
9b7e2112 553 b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE, vconn->version, s);
064af421
BP
554 retval = do_send(vconn, b);
555 if (retval) {
556 ofpbuf_delete(b);
557 }
558 if (retval != EAGAIN) {
559 vconn->state = VCS_DISCONNECTED;
560 vconn->error = retval ? retval : EPROTO;
561 }
562}
563
294e9fc8
BP
564/* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
565 * complete, returns 0 if the connection was successful or a positive errno
566 * value if it failed. If the connection is still in progress, returns
567 * EAGAIN. */
064af421
BP
568int
569vconn_connect(struct vconn *vconn)
570{
571 enum vconn_state last_state;
572
064af421
BP
573 do {
574 last_state = vconn->state;
575 switch (vconn->state) {
576 case VCS_CONNECTING:
577 vcs_connecting(vconn);
578 break;
579
580 case VCS_SEND_HELLO:
581 vcs_send_hello(vconn);
582 break;
583
584 case VCS_RECV_HELLO:
585 vcs_recv_hello(vconn);
586 break;
587
588 case VCS_CONNECTED:
589 return 0;
590
591 case VCS_SEND_ERROR:
592 vcs_send_error(vconn);
593 break;
594
595 case VCS_DISCONNECTED:
596 return vconn->error;
597
598 default:
599 NOT_REACHED();
600 }
601 } while (vconn->state != last_state);
602
603 return EAGAIN;
604}
605
294e9fc8
BP
606/* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
607 * the received message into '*msgp' and returns 0. The caller is responsible
608 * for destroying the message with ofpbuf_delete(). On failure, returns a
609 * positive errno value and stores a null pointer into '*msgp'. On normal
610 * connection close, returns EOF.
064af421
BP
611 *
612 * vconn_recv will not block waiting for a packet to arrive. If no packets
613 * have been received, it returns EAGAIN immediately. */
614int
615vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
616{
982697a4
BP
617 struct ofpbuf *msg;
618 int retval;
619
620 retval = vconn_connect(vconn);
621 if (!retval) {
622 retval = do_recv(vconn, &msg);
623 }
4766ce7a 624 if (!retval && !vconn->recv_any_version) {
982697a4
BP
625 const struct ofp_header *oh = msg->data;
626 if (oh->version != vconn->version) {
627 enum ofptype type;
628
629 if (ofptype_decode(&type, msg->data)
630 || (type != OFPTYPE_HELLO &&
631 type != OFPTYPE_ERROR &&
632 type != OFPTYPE_ECHO_REQUEST &&
633 type != OFPTYPE_ECHO_REPLY)) {
2b4cca6f
BP
634 struct ofpbuf *reply;
635
982697a4
BP
636 VLOG_ERR_RL(&bad_ofmsg_rl, "%s: received OpenFlow version "
637 "0x%02"PRIx8" != expected %02x",
638 vconn->name, oh->version, vconn->version);
2b4cca6f
BP
639
640 /* Send a "bad version" reply, if we can. */
641 reply = ofperr_encode_reply(OFPERR_OFPBRC_BAD_VERSION, oh);
642 retval = vconn_send(vconn, reply);
643 if (retval) {
644 VLOG_INFO_RL(&bad_ofmsg_rl,
645 "%s: failed to queue error reply (%s)",
646 vconn->name, ovs_strerror(retval));
647 ofpbuf_delete(reply);
648 }
649
650 /* Suppress the received message, as if it had not arrived. */
651 retval = EAGAIN;
982697a4 652 ofpbuf_delete(msg);
982697a4
BP
653 }
654 }
064af421 655 }
982697a4
BP
656
657 *msgp = retval ? NULL : msg;
064af421
BP
658 return retval;
659}
660
661static int
662do_recv(struct vconn *vconn, struct ofpbuf **msgp)
663{
5fe577eb 664 int retval = (vconn->class->recv)(vconn, msgp);
064af421 665 if (!retval) {
064af421
BP
666 COVERAGE_INC(vconn_received);
667 if (VLOG_IS_DBG_ENABLED()) {
668 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
669 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
670 free(s);
671 }
064af421
BP
672 }
673 return retval;
674}
675
294e9fc8
BP
676/* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
677 * in which case ownership of 'msg' is transferred to the vconn. Success does
678 * not guarantee that 'msg' has been or ever will be delivered to the peer,
679 * only that it has been queued for transmission.
064af421
BP
680 *
681 * Returns a positive errno value on failure, in which case the caller
682 * retains ownership of 'msg'.
683 *
684 * vconn_send will not block. If 'msg' cannot be immediately accepted for
685 * transmission, it returns EAGAIN immediately. */
686int
687vconn_send(struct vconn *vconn, struct ofpbuf *msg)
688{
689 int retval = vconn_connect(vconn);
690 if (!retval) {
691 retval = do_send(vconn, msg);
692 }
693 return retval;
694}
695
696static int
697do_send(struct vconn *vconn, struct ofpbuf *msg)
698{
699 int retval;
700
cb22974d 701 ovs_assert(msg->size >= sizeof(struct ofp_header));
982697a4
BP
702
703 ofpmsg_update_length(msg);
064af421
BP
704 if (!VLOG_IS_DBG_ENABLED()) {
705 COVERAGE_INC(vconn_sent);
706 retval = (vconn->class->send)(vconn, msg);
707 } else {
708 char *s = ofp_to_string(msg->data, msg->size, 1);
709 retval = (vconn->class->send)(vconn, msg);
710 if (retval != EAGAIN) {
711 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
10a89ef0 712 vconn->name, ovs_strerror(retval), s);
064af421
BP
713 }
714 free(s);
715 }
716 return retval;
717}
718
6d1fb217
BP
719/* Same as vconn_connect(), except that it waits until the connection on
720 * 'vconn' completes or fails. Thus, it will never return EAGAIN. */
721int
722vconn_connect_block(struct vconn *vconn)
723{
724 int error;
725
726 while ((error = vconn_connect(vconn)) == EAGAIN) {
727 vconn_run(vconn);
728 vconn_run_wait(vconn);
729 vconn_connect_wait(vconn);
730 poll_block();
731 }
cb22974d 732 ovs_assert(error != EINPROGRESS);
6d1fb217
BP
733
734 return error;
735}
736
064af421
BP
737/* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
738int
739vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
740{
741 int retval;
b302749b
BP
742
743 fatal_signal_run();
744
064af421 745 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
60cb3eb8
BP
746 vconn_run(vconn);
747 vconn_run_wait(vconn);
064af421
BP
748 vconn_send_wait(vconn);
749 poll_block();
750 }
751 return retval;
752}
753
754/* Same as vconn_recv, except that it waits until a message is received. */
755int
756vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
757{
758 int retval;
b302749b
BP
759
760 fatal_signal_run();
761
064af421 762 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
60cb3eb8
BP
763 vconn_run(vconn);
764 vconn_run_wait(vconn);
064af421
BP
765 vconn_recv_wait(vconn);
766 poll_block();
767 }
768 return retval;
769}
770
ec9f40dc 771/* Waits until a message with a transaction ID matching 'xid' is received on
064af421
BP
772 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
773 * '*replyp' for the caller to examine and free. Otherwise returns a positive
774 * errno value, or EOF, and sets '*replyp' to null.
775 *
776 * 'request' is always destroyed, regardless of the return value. */
777int
4408d18a 778vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp)
064af421
BP
779{
780 for (;;) {
4408d18a 781 ovs_be32 recv_xid;
064af421
BP
782 struct ofpbuf *reply;
783 int error;
784
785 error = vconn_recv_block(vconn, &reply);
786 if (error) {
787 *replyp = NULL;
788 return error;
789 }
790 recv_xid = ((struct ofp_header *) reply->data)->xid;
791 if (xid == recv_xid) {
792 *replyp = reply;
793 return 0;
794 }
795
796 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
44381c1b
BP
797 " != expected %08"PRIx32,
798 vconn->name, ntohl(recv_xid), ntohl(xid));
064af421
BP
799 ofpbuf_delete(reply);
800 }
801}
802
803/* Sends 'request' to 'vconn' and blocks until it receives a reply with a
804 * matching transaction ID. Returns 0 if successful, in which case the reply
805 * is stored in '*replyp' for the caller to examine and free. Otherwise
806 * returns a positive errno value, or EOF, and sets '*replyp' to null.
807 *
33af7dca
BP
808 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
809 * if there is no reply, this function can end up blocking forever (or until
810 * the peer drops the connection).
811 *
064af421
BP
812 * 'request' is always destroyed, regardless of the return value. */
813int
814vconn_transact(struct vconn *vconn, struct ofpbuf *request,
815 struct ofpbuf **replyp)
816{
4408d18a 817 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
064af421
BP
818 int error;
819
820 *replyp = NULL;
821 error = vconn_send_block(vconn, request);
822 if (error) {
823 ofpbuf_delete(request);
824 }
825 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
826}
827
33af7dca
BP
828/* Sends 'request' followed by a barrier request to 'vconn', then blocks until
829 * it receives a reply to the barrier. If successful, stores the reply to
830 * 'request' in '*replyp', if one was received, and otherwise NULL, then
831 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
832 * '*replyp' to null.
833 *
834 * This function is useful for sending an OpenFlow request that doesn't
835 * ordinarily include a reply but might report an error in special
836 * circumstances.
837 *
838 * 'request' is always destroyed, regardless of the return value. */
839int
840vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request,
841 struct ofpbuf **replyp)
842{
843 ovs_be32 request_xid;
844 ovs_be32 barrier_xid;
845 struct ofpbuf *barrier;
846 int error;
847
848 *replyp = NULL;
849
850 /* Send request. */
851 request_xid = ((struct ofp_header *) request->data)->xid;
852 error = vconn_send_block(vconn, request);
853 if (error) {
854 ofpbuf_delete(request);
855 return error;
856 }
857
858 /* Send barrier. */
a0ae0b6e 859 barrier = ofputil_encode_barrier_request(vconn_get_version(vconn));
33af7dca
BP
860 barrier_xid = ((struct ofp_header *) barrier->data)->xid;
861 error = vconn_send_block(vconn, barrier);
862 if (error) {
863 ofpbuf_delete(barrier);
864 return error;
865 }
866
867 for (;;) {
868 struct ofpbuf *msg;
869 ovs_be32 msg_xid;
870 int error;
871
872 error = vconn_recv_block(vconn, &msg);
873 if (error) {
874 ofpbuf_delete(*replyp);
875 *replyp = NULL;
876 return error;
877 }
878
879 msg_xid = ((struct ofp_header *) msg->data)->xid;
880 if (msg_xid == request_xid) {
881 if (*replyp) {
882 VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with "
883 "xid %08"PRIx32, vconn->name, ntohl(msg_xid));
884 ofpbuf_delete(*replyp);
885 }
886 *replyp = msg;
887 } else {
888 ofpbuf_delete(msg);
889 if (msg_xid == barrier_xid) {
890 return 0;
891 } else {
892 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32
893 " != expected %08"PRIx32" or %08"PRIx32,
894 vconn->name, ntohl(msg_xid),
895 ntohl(request_xid), ntohl(barrier_xid));
896 }
897 }
898 }
899}
900
7f009380
BP
901/* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
902 * All of the requests on 'requests' are always destroyed, regardless of the
903 * return value. */
904int
905vconn_transact_multiple_noreply(struct vconn *vconn, struct list *requests,
906 struct ofpbuf **replyp)
907{
908 struct ofpbuf *request, *next;
909
910 LIST_FOR_EACH_SAFE (request, next, list_node, requests) {
911 int error;
912
913 list_remove(&request->list_node);
914
915 error = vconn_transact_noreply(vconn, request, replyp);
916 if (error || *replyp) {
917 ofpbuf_list_delete(requests);
918 return error;
919 }
920 }
921
922 *replyp = NULL;
923 return 0;
924}
925
064af421
BP
926void
927vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
928{
cb22974d 929 ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
064af421
BP
930
931 switch (vconn->state) {
932 case VCS_CONNECTING:
933 wait = WAIT_CONNECT;
934 break;
935
936 case VCS_SEND_HELLO:
937 case VCS_SEND_ERROR:
938 wait = WAIT_SEND;
939 break;
940
941 case VCS_RECV_HELLO:
942 wait = WAIT_RECV;
943 break;
944
945 case VCS_CONNECTED:
946 break;
947
948 case VCS_DISCONNECTED:
949 poll_immediate_wake();
950 return;
951 }
952 (vconn->class->wait)(vconn, wait);
953}
954
955void
956vconn_connect_wait(struct vconn *vconn)
957{
958 vconn_wait(vconn, WAIT_CONNECT);
959}
960
961void
962vconn_recv_wait(struct vconn *vconn)
963{
964 vconn_wait(vconn, WAIT_RECV);
965}
966
967void
968vconn_send_wait(struct vconn *vconn)
969{
970 vconn_wait(vconn, WAIT_SEND);
971}
972
30012c72
BP
973/* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
974 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
975 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
976 * class exists. */
977static int
5b712636 978pvconn_lookup_class(const char *name, const struct pvconn_class **classp)
30012c72
BP
979{
980 size_t prefix_len;
981
982 prefix_len = strcspn(name, ":");
983 if (name[prefix_len] != '\0') {
984 size_t i;
985
986 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
5b712636 987 const struct pvconn_class *class = pvconn_classes[i];
30012c72
BP
988 if (strlen(class->name) == prefix_len
989 && !memcmp(class->name, name, prefix_len)) {
990 *classp = class;
991 return 0;
992 }
993 }
994 }
995
996 *classp = NULL;
997 return EAFNOSUPPORT;
998}
999
1000/* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
1001 * a supported connection type, otherwise EAFNOSUPPORT. */
1002int
1003pvconn_verify_name(const char *name)
1004{
5b712636 1005 const struct pvconn_class *class;
30012c72
BP
1006 return pvconn_lookup_class(name, &class);
1007}
1008
064af421
BP
1009/* Attempts to start listening for OpenFlow connections. 'name' is a
1010 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
1011 * class's name and ARGS are vconn class-specific.
1012 *
7a25bd99
SH
1013 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1014 * protocol version acceptable to both peers on the connection. The version
1015 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1016 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1017 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1018 *
064af421
BP
1019 * Returns 0 if successful, otherwise a positive errno value. If successful,
1020 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1021 * pointer. */
1022int
82c8c53c
BP
1023pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
1024 struct pvconn **pvconnp)
064af421 1025{
5b712636 1026 const struct pvconn_class *class;
30012c72
BP
1027 struct pvconn *pvconn;
1028 char *suffix_copy;
1029 int error;
064af421
BP
1030
1031 check_vconn_classes();
1032
7a25bd99
SH
1033 if (!allowed_versions) {
1034 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
1035 }
1036
30012c72
BP
1037 /* Look up the class. */
1038 error = pvconn_lookup_class(name, &class);
1039 if (!class) {
1040 goto error;
064af421 1041 }
30012c72
BP
1042
1043 /* Call class's "open" function. */
1044 suffix_copy = xstrdup(strchr(name, ':') + 1);
7a25bd99 1045 error = class->listen(name, allowed_versions, suffix_copy, &pvconn, dscp);
30012c72
BP
1046 free(suffix_copy);
1047 if (error) {
1048 goto error;
064af421 1049 }
30012c72
BP
1050
1051 /* Success. */
1052 *pvconnp = pvconn;
1053 return 0;
1054
1055error:
1056 *pvconnp = NULL;
1057 return error;
064af421
BP
1058}
1059
1060/* Returns the name that was used to open 'pvconn'. The caller must not
1061 * modify or free the name. */
1062const char *
1063pvconn_get_name(const struct pvconn *pvconn)
1064{
1065 return pvconn->name;
1066}
1067
1068/* Closes 'pvconn'. */
1069void
1070pvconn_close(struct pvconn *pvconn)
1071{
1072 if (pvconn != NULL) {
1073 char *name = pvconn->name;
1074 (pvconn->class->close)(pvconn);
1075 free(name);
1076 }
1077}
1078
1079/* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1080 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1081 * errno value.
1082 *
1083 * The new vconn will automatically negotiate an OpenFlow protocol version
1084 * acceptable to both peers on the connection. The version negotiated will be
7a25bd99 1085 * no lower than 'min_version' and no higher than 'max_version'.
064af421
BP
1086 *
1087 * pvconn_accept() will not block waiting for a connection. If no connection
1088 * is ready to be accepted, it returns EAGAIN immediately. */
1089int
7a25bd99 1090pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn)
064af421
BP
1091{
1092 int retval = (pvconn->class->accept)(pvconn, new_vconn);
1093 if (retval) {
1094 *new_vconn = NULL;
1095 } else {
cb22974d
BP
1096 ovs_assert((*new_vconn)->state != VCS_CONNECTING
1097 || (*new_vconn)->class->connect);
064af421
BP
1098 }
1099 return retval;
1100}
1101
1102void
1103pvconn_wait(struct pvconn *pvconn)
1104{
1105 (pvconn->class->wait)(pvconn);
1106}
1107
85ab0a02
BP
1108/* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1109 * The initial connection status, supplied as 'connect_status', is interpreted
1110 * as follows:
1111 *
1112 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1113 * called in the normal fashion.
1114 *
1115 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1116 * function should be called to complete the connection.
1117 *
1118 * - Other positive errno values indicate that the connection failed with
1119 * the specified error.
1120 *
1121 * After calling this function, vconn_close() must be used to destroy 'vconn',
1122 * otherwise resources will be leaked.
1123 *
1124 * The caller retains ownership of 'name'. */
064af421 1125void
5b712636
BP
1126vconn_init(struct vconn *vconn, const struct vconn_class *class,
1127 int connect_status, const char *name, uint32_t allowed_versions)
064af421 1128{
267e915f 1129 memset(vconn, 0, sizeof *vconn);
064af421
BP
1130 vconn->class = class;
1131 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1132 : !connect_status ? VCS_SEND_HELLO
1133 : VCS_DISCONNECTED);
1134 vconn->error = connect_status;
7a25bd99 1135 vconn->allowed_versions = allowed_versions;
064af421 1136 vconn->name = xstrdup(name);
cb22974d 1137 ovs_assert(vconn->state != VCS_CONNECTING || class->connect);
064af421
BP
1138}
1139
d7cca867 1140void
4408d18a 1141vconn_set_remote_ip(struct vconn *vconn, ovs_be32 ip)
d7cca867
BP
1142{
1143 vconn->remote_ip = ip;
1144}
1145
1146void
4408d18a 1147vconn_set_remote_port(struct vconn *vconn, ovs_be16 port)
d7cca867
BP
1148{
1149 vconn->remote_port = port;
1150}
1151
d295e8e9 1152void
4408d18a 1153vconn_set_local_ip(struct vconn *vconn, ovs_be32 ip)
193456d5
JP
1154{
1155 vconn->local_ip = ip;
1156}
1157
d295e8e9 1158void
4408d18a 1159vconn_set_local_port(struct vconn *vconn, ovs_be16 port)
193456d5
JP
1160{
1161 vconn->local_port = port;
1162}
1163
064af421 1164void
5b712636 1165pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class,
7a25bd99 1166 const char *name, uint32_t allowed_versions)
064af421
BP
1167{
1168 pvconn->class = class;
1169 pvconn->name = xstrdup(name);
7a25bd99 1170 pvconn->allowed_versions = allowed_versions;
064af421 1171}