]> git.proxmox.com Git - mirror_ovs.git/blame - lib/vconn.c
ovsdb-idl: Remove prototype for function that is not defined or used.
[mirror_ovs.git] / lib / vconn.c
CommitLineData
064af421 1/*
50f96b10 2 * Copyright (c) 2008-2017 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>
b2befd5b 21#include <sys/types.h>
064af421
BP
22#include <netinet/in.h>
23#include <poll.h>
24#include <stdlib.h>
25#include <string.h>
26#include "coverage.h"
b302749b 27#include "fatal-signal.h"
064af421 28#include "flow.h"
064af421
BP
29#include "openflow/nicira-ext.h"
30#include "openflow/openflow.h"
d271907f 31#include "openvswitch/dynamic-string.h"
0d71302e 32#include "openvswitch/ofp-bundle.h"
d271907f
BW
33#include "openvswitch/ofp-errors.h"
34#include "openvswitch/ofp-msgs.h"
25d436fb 35#include "openvswitch/ofp-print.h"
d271907f
BW
36#include "openvswitch/ofp-util.h"
37#include "openvswitch/ofpbuf.h"
38#include "openvswitch/vlog.h"
064af421 39#include "packets.h"
fd016ae3 40#include "openvswitch/poll-loop.h"
064af421
BP
41#include "random.h"
42#include "util.h"
f125905c 43#include "socket-util.h"
064af421 44
d98e6007 45VLOG_DEFINE_THIS_MODULE(vconn);
5136ce49 46
d76f09ea
BP
47COVERAGE_DEFINE(vconn_open);
48COVERAGE_DEFINE(vconn_received);
49COVERAGE_DEFINE(vconn_sent);
50
064af421
BP
51/* State of an active vconn.*/
52enum vconn_state {
53 /* This is the ordinary progression of states. */
54 VCS_CONNECTING, /* Underlying vconn is not connected. */
55 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
56 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
57 VCS_CONNECTED, /* Connection established. */
58
59 /* These states are entered only when something goes wrong. */
60 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
61 VCS_DISCONNECTED /* Connection failed or connection closed. */
62};
63
5b712636 64static const struct vconn_class *vconn_classes[] = {
064af421
BP
65 &tcp_vconn_class,
66 &unix_vconn_class,
67#ifdef HAVE_OPENSSL
68 &ssl_vconn_class,
69#endif
70};
71
5b712636 72static const struct pvconn_class *pvconn_classes[] = {
064af421
BP
73 &ptcp_pvconn_class,
74 &punix_pvconn_class,
75#ifdef HAVE_OPENSSL
76 &pssl_pvconn_class,
77#endif
78};
79
80/* Rate limit for individual OpenFlow messages going over the vconn, output at
81 * DBG level. This is very high because, if these are enabled, it is because
82 * we really need to see them. */
83static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
84
85/* Rate limit for OpenFlow message parse errors. These always indicate a bug
86 * in the peer and so there's not much point in showing a lot of them. */
87static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
88
89static int do_recv(struct vconn *, struct ofpbuf **);
90static int do_send(struct vconn *, struct ofpbuf *);
91
92/* Check the validity of the vconn class structures. */
93static void
94check_vconn_classes(void)
95{
96#ifndef NDEBUG
97 size_t i;
98
99 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
5b712636 100 const struct vconn_class *class = vconn_classes[i];
cb22974d
BP
101 ovs_assert(class->name != NULL);
102 ovs_assert(class->open != NULL);
60cb3eb8
BP
103 if (class->close || class->recv || class->send
104 || class->run || class->run_wait || class->wait) {
cb22974d
BP
105 ovs_assert(class->close != NULL);
106 ovs_assert(class->recv != NULL);
107 ovs_assert(class->send != NULL);
108 ovs_assert(class->wait != NULL);
064af421
BP
109 } else {
110 /* This class delegates to another one. */
111 }
112 }
113
114 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
5b712636 115 const struct pvconn_class *class = pvconn_classes[i];
cb22974d
BP
116 ovs_assert(class->name != NULL);
117 ovs_assert(class->listen != NULL);
064af421 118 if (class->close || class->accept || class->wait) {
cb22974d
BP
119 ovs_assert(class->close != NULL);
120 ovs_assert(class->accept != NULL);
121 ovs_assert(class->wait != NULL);
064af421
BP
122 } else {
123 /* This class delegates to another one. */
124 }
125 }
126#endif
127}
128
129/* Prints information on active (if 'active') and passive (if 'passive')
130 * connection methods supported by the vconn. If 'bootstrap' is true, also
131 * advertises options to bootstrap the CA certificate. */
132void
67a4917b 133vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
064af421
BP
134{
135 /* Really this should be implemented via callbacks into the vconn
136 * providers, but that seems too heavy-weight to bother with at the
137 * moment. */
d295e8e9 138
064af421
BP
139 printf("\n");
140 if (active) {
141 printf("Active OpenFlow connection methods:\n");
771680d9
YS
142 printf(" tcp:HOST[:PORT] "
143 "PORT (default: %d) at remote HOST\n", OFP_PORT);
064af421 144#ifdef HAVE_OPENSSL
771680d9
YS
145 printf(" ssl:HOST[:PORT] "
146 "SSL PORT (default: %d) at remote HOST\n", OFP_PORT);
064af421
BP
147#endif
148 printf(" unix:FILE Unix domain socket named FILE\n");
149 }
150
151 if (passive) {
152 printf("Passive OpenFlow connection methods:\n");
78ff0270
BP
153 printf(" ptcp:[PORT][:IP] "
154 "listen to TCP PORT (default: %d) on IP\n",
d4763d1d 155 OFP_PORT);
064af421 156#ifdef HAVE_OPENSSL
78ff0270
BP
157 printf(" pssl:[PORT][:IP] "
158 "listen for SSL on PORT (default: %d) on IP\n",
d4763d1d 159 OFP_PORT);
064af421
BP
160#endif
161 printf(" punix:FILE "
162 "listen on Unix domain socket FILE\n");
163 }
164
165#ifdef HAVE_OPENSSL
166 printf("PKI configuration (required to use SSL):\n"
167 " -p, --private-key=FILE file with private key\n"
168 " -c, --certificate=FILE file with certificate for private key\n"
169 " -C, --ca-cert=FILE file with peer CA certificate\n");
170 if (bootstrap) {
171 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
172 "to read or create\n");
173 }
174#endif
175}
176
30012c72
BP
177/* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
178 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
179 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
180 * class exists. */
181static int
5b712636 182vconn_lookup_class(const char *name, const struct vconn_class **classp)
30012c72
BP
183{
184 size_t prefix_len;
185
186 prefix_len = strcspn(name, ":");
187 if (name[prefix_len] != '\0') {
188 size_t i;
189
190 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
5b712636 191 const struct vconn_class *class = vconn_classes[i];
30012c72
BP
192 if (strlen(class->name) == prefix_len
193 && !memcmp(class->name, name, prefix_len)) {
194 *classp = class;
195 return 0;
196 }
197 }
198 }
199
200 *classp = NULL;
201 return EAFNOSUPPORT;
202}
203
204/* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
205 * a supported connection type, otherwise EAFNOSUPPORT. */
206int
207vconn_verify_name(const char *name)
208{
5b712636 209 const struct vconn_class *class;
30012c72
BP
210 return vconn_lookup_class(name, &class);
211}
212
064af421
BP
213/* Attempts to connect to an OpenFlow device. 'name' is a connection name in
214 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
215 * are vconn class-specific.
216 *
217 * The vconn will automatically negotiate an OpenFlow protocol version
218 * acceptable to both peers on the connection. The version negotiated will be
7a25bd99
SH
219 * one of those in the 'allowed_versions' bitmap: version 'x' is allowed if
220 * allowed_versions & (1 << x) is nonzero. If 'allowed_versions' is zero, then
221 * OFPUTIL_DEFAULT_VERSIONS are allowed.
064af421
BP
222 *
223 * Returns 0 if successful, otherwise a positive errno value. If successful,
224 * stores a pointer to the new connection in '*vconnp', otherwise a null
225 * pointer. */
226int
82c8c53c
BP
227vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
228 struct vconn **vconnp)
064af421 229{
5b712636 230 const struct vconn_class *class;
30012c72
BP
231 struct vconn *vconn;
232 char *suffix_copy;
233 int error;
064af421
BP
234
235 COVERAGE_INC(vconn_open);
236 check_vconn_classes();
237
7a25bd99
SH
238 if (!allowed_versions) {
239 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
240 }
241
30012c72
BP
242 /* Look up the class. */
243 error = vconn_lookup_class(name, &class);
244 if (!class) {
245 goto error;
064af421 246 }
30012c72
BP
247
248 /* Call class's "open" function. */
249 suffix_copy = xstrdup(strchr(name, ':') + 1);
7a25bd99 250 error = class->open(name, allowed_versions, suffix_copy, &vconn, dscp);
30012c72
BP
251 free(suffix_copy);
252 if (error) {
253 goto error;
064af421 254 }
30012c72
BP
255
256 /* Success. */
a445a8d8 257 ovs_assert(vconn->state != VCS_CONNECTING || vconn->vclass->connect);
30012c72
BP
258 *vconnp = vconn;
259 return 0;
260
261error:
262 *vconnp = NULL;
263 return error;
064af421
BP
264}
265
60cb3eb8
BP
266/* Allows 'vconn' to perform maintenance activities, such as flushing output
267 * buffers. */
268void
269vconn_run(struct vconn *vconn)
270{
48d84b17
BP
271 if (vconn->state == VCS_CONNECTING ||
272 vconn->state == VCS_SEND_HELLO ||
273 vconn->state == VCS_RECV_HELLO) {
274 vconn_connect(vconn);
275 }
276
a445a8d8
AB
277 if (vconn->vclass->run) {
278 (vconn->vclass->run)(vconn);
60cb3eb8
BP
279 }
280}
281
282/* Arranges for the poll loop to wake up when 'vconn' needs to perform
283 * maintenance activities. */
284void
285vconn_run_wait(struct vconn *vconn)
286{
48d84b17
BP
287 if (vconn->state == VCS_CONNECTING ||
288 vconn->state == VCS_SEND_HELLO ||
289 vconn->state == VCS_RECV_HELLO) {
290 vconn_connect_wait(vconn);
291 }
292
a445a8d8
AB
293 if (vconn->vclass->run_wait) {
294 (vconn->vclass->run_wait)(vconn);
60cb3eb8
BP
295 }
296}
297
accaecc4
BP
298/* Returns 0 if 'vconn' is healthy (connecting or connected), a positive errno
299 * value if the connection died abnormally (connection failed or aborted), or
300 * EOF if the connection was closed in a normal way. */
301int
302vconn_get_status(const struct vconn *vconn)
303{
77587e0f 304 return vconn->error == EAGAIN ? 0 : vconn->error;
accaecc4
BP
305}
306
064af421 307int
82c8c53c 308vconn_open_block(const char *name, uint32_t allowed_versions, uint8_t dscp,
04895042 309 long long int timeout, struct vconn **vconnp)
064af421
BP
310{
311 struct vconn *vconn;
312 int error;
313
b302749b
BP
314 fatal_signal_run();
315
82c8c53c 316 error = vconn_open(name, allowed_versions, dscp, &vconn);
b0bfeb3e 317 if (!error) {
04895042 318 error = vconn_connect_block(vconn, timeout);
064af421 319 }
b0bfeb3e 320
064af421
BP
321 if (error) {
322 vconn_close(vconn);
323 *vconnp = NULL;
324 } else {
325 *vconnp = vconn;
326 }
327 return error;
328}
329
330/* Closes 'vconn'. */
331void
332vconn_close(struct vconn *vconn)
333{
334 if (vconn != NULL) {
335 char *name = vconn->name;
a445a8d8 336 (vconn->vclass->close)(vconn);
064af421
BP
337 free(name);
338 }
339}
340
341/* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
342const char *
343vconn_get_name(const struct vconn *vconn)
344{
345 return vconn->name;
346}
347
7a25bd99
SH
348/* Returns the allowed_versions of 'vconn', that is,
349 * the allowed_versions passed to vconn_open(). */
350uint32_t
351vconn_get_allowed_versions(const struct vconn *vconn)
352{
353 return vconn->allowed_versions;
354}
355
e182670b
SH
356/* Sets the allowed_versions of 'vconn', overriding
357 * the allowed_versions passed to vconn_open(). */
358void
359vconn_set_allowed_versions(struct vconn *vconn, uint32_t allowed_versions)
360{
361 vconn->allowed_versions = allowed_versions;
362}
363
27527aa0
BP
364/* Returns the OpenFlow version negotiated with the peer, or -1 if version
365 * negotiation is not yet complete.
366 *
367 * A vconn that has successfully connected (that is, vconn_connect() or
368 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
733c8ed3 369int
27527aa0
BP
370vconn_get_version(const struct vconn *vconn)
371{
733c8ed3 372 return vconn->version ? vconn->version : -1;
27527aa0
BP
373}
374
4766ce7a
BP
375/* By default, a vconn accepts only OpenFlow messages whose version matches the
376 * one negotiated for the connection. A message received with a different
377 * version is an error that causes the vconn to drop the connection.
378 *
379 * This functions allows 'vconn' to accept messages with any OpenFlow version.
380 * This is useful in the special case where 'vconn' is used as an rconn
381 * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is
382 * used as a target for mirroring OpenFlow messages for debugging and
383 * troubleshooting.
384 *
385 * This function should be called after a successful vconn_open() or
386 * pvconn_accept() but before the connection completes, that is, before
387 * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn'
388 * beforehand with an unexpected version will the vconn to drop the
389 * connection. */
390void
391vconn_set_recv_any_version(struct vconn *vconn)
392{
393 vconn->recv_any_version = true;
394}
395
064af421 396static void
d295e8e9 397vcs_connecting(struct vconn *vconn)
064af421 398{
a445a8d8 399 int retval = (vconn->vclass->connect)(vconn);
cb22974d 400 ovs_assert(retval != EINPROGRESS);
064af421
BP
401 if (!retval) {
402 vconn->state = VCS_SEND_HELLO;
403 } else if (retval != EAGAIN) {
404 vconn->state = VCS_DISCONNECTED;
405 vconn->error = retval;
406 }
407}
408
409static void
410vcs_send_hello(struct vconn *vconn)
411{
412 struct ofpbuf *b;
413 int retval;
414
de6c85b0 415 b = ofputil_encode_hello(vconn->allowed_versions);
064af421
BP
416 retval = do_send(vconn, b);
417 if (!retval) {
418 vconn->state = VCS_RECV_HELLO;
419 } else {
420 ofpbuf_delete(b);
421 if (retval != EAGAIN) {
422 vconn->state = VCS_DISCONNECTED;
423 vconn->error = retval;
424 }
425 }
426}
427
7a25bd99
SH
428static char *
429version_bitmap_to_string(uint32_t bitmap)
430{
431 struct ds s;
432
433 ds_init(&s);
434 if (!bitmap) {
435 ds_put_cstr(&s, "no versions");
436 } else if (is_pow2(bitmap)) {
437 ds_put_cstr(&s, "version ");
438 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
439 } else if (is_pow2((bitmap >> 1) + 1)) {
440 ds_put_cstr(&s, "version ");
441 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
6cca5c20 442 ds_put_cstr(&s, " and earlier");
7a25bd99
SH
443 } else {
444 ds_put_cstr(&s, "versions ");
445 ofputil_format_version_bitmap(&s, bitmap);
446 }
447 return ds_steal_cstr(&s);
448}
449
064af421
BP
450static void
451vcs_recv_hello(struct vconn *vconn)
452{
453 struct ofpbuf *b;
454 int retval;
455
456 retval = do_recv(vconn, &b);
457 if (!retval) {
982697a4
BP
458 enum ofptype type;
459 enum ofperr error;
064af421 460
6fd6ed71 461 error = ofptype_decode(&type, b->data);
982697a4 462 if (!error && type == OFPTYPE_HELLO) {
7a25bd99
SH
463 char *peer_s, *local_s;
464 uint32_t common_versions;
465
6fd6ed71 466 if (!ofputil_decode_hello(b->data, &vconn->peer_versions)) {
064af421 467 struct ds msg = DS_EMPTY_INITIALIZER;
de6c85b0
SH
468 ds_put_format(&msg, "%s: unknown data in hello:\n",
469 vconn->name);
6fd6ed71 470 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
064af421
BP
471 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
472 ds_destroy(&msg);
473 }
474
7a25bd99
SH
475 local_s = version_bitmap_to_string(vconn->allowed_versions);
476 peer_s = version_bitmap_to_string(vconn->peer_versions);
477
478 common_versions = vconn->peer_versions & vconn->allowed_versions;
479 if (!common_versions) {
480 vconn->version = leftmost_1bit_idx(vconn->peer_versions);
064af421 481 VLOG_WARN_RL(&bad_ofmsg_rl,
7a25bd99
SH
482 "%s: version negotiation failed (we support "
483 "%s, peer supports %s)",
484 vconn->name, local_s, peer_s);
064af421
BP
485 vconn->state = VCS_SEND_ERROR;
486 } else {
7a25bd99 487 vconn->version = leftmost_1bit_idx(common_versions);
064af421 488 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
7a25bd99
SH
489 "(we support %s, peer supports %s)", vconn->name,
490 vconn->version, local_s, peer_s);
064af421
BP
491 vconn->state = VCS_CONNECTED;
492 }
7a25bd99
SH
493
494 free(local_s);
495 free(peer_s);
496
064af421
BP
497 ofpbuf_delete(b);
498 return;
499 } else {
4bc938cc 500 char *s = ofp_to_string(b->data, b->size, NULL, NULL, 1);
064af421
BP
501 VLOG_WARN_RL(&bad_ofmsg_rl,
502 "%s: received message while expecting hello: %s",
503 vconn->name, s);
504 free(s);
505 retval = EPROTO;
506 ofpbuf_delete(b);
507 }
508 }
509
510 if (retval != EAGAIN) {
511 vconn->state = VCS_DISCONNECTED;
b7eae257 512 vconn->error = retval == EOF ? ECONNRESET : retval;
064af421
BP
513 }
514}
515
516static void
517vcs_send_error(struct vconn *vconn)
518{
064af421
BP
519 struct ofpbuf *b;
520 char s[128];
521 int retval;
7a25bd99
SH
522 char *local_s, *peer_s;
523
524 local_s = version_bitmap_to_string(vconn->allowed_versions);
525 peer_s = version_bitmap_to_string(vconn->peer_versions);
526 snprintf(s, sizeof s, "We support %s, you support %s, no common versions.",
527 local_s, peer_s);
528 free(peer_s);
529 free(local_s);
064af421 530
9b7e2112 531 b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE, vconn->version, s);
064af421
BP
532 retval = do_send(vconn, b);
533 if (retval) {
534 ofpbuf_delete(b);
535 }
536 if (retval != EAGAIN) {
537 vconn->state = VCS_DISCONNECTED;
538 vconn->error = retval ? retval : EPROTO;
539 }
540}
541
294e9fc8
BP
542/* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
543 * complete, returns 0 if the connection was successful or a positive errno
544 * value if it failed. If the connection is still in progress, returns
545 * EAGAIN. */
064af421
BP
546int
547vconn_connect(struct vconn *vconn)
548{
549 enum vconn_state last_state;
550
064af421
BP
551 do {
552 last_state = vconn->state;
553 switch (vconn->state) {
554 case VCS_CONNECTING:
555 vcs_connecting(vconn);
556 break;
557
558 case VCS_SEND_HELLO:
559 vcs_send_hello(vconn);
560 break;
561
562 case VCS_RECV_HELLO:
563 vcs_recv_hello(vconn);
564 break;
565
566 case VCS_CONNECTED:
567 return 0;
568
569 case VCS_SEND_ERROR:
570 vcs_send_error(vconn);
571 break;
572
573 case VCS_DISCONNECTED:
476d2551 574 ovs_assert(vconn->error != 0);
064af421
BP
575 return vconn->error;
576
577 default:
428b2edd 578 OVS_NOT_REACHED();
064af421
BP
579 }
580 } while (vconn->state != last_state);
581
582 return EAGAIN;
583}
584
294e9fc8
BP
585/* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
586 * the received message into '*msgp' and returns 0. The caller is responsible
587 * for destroying the message with ofpbuf_delete(). On failure, returns a
588 * positive errno value and stores a null pointer into '*msgp'. On normal
589 * connection close, returns EOF.
064af421
BP
590 *
591 * vconn_recv will not block waiting for a packet to arrive. If no packets
592 * have been received, it returns EAGAIN immediately. */
593int
594vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
595{
982697a4
BP
596 struct ofpbuf *msg;
597 int retval;
598
599 retval = vconn_connect(vconn);
600 if (!retval) {
601 retval = do_recv(vconn, &msg);
602 }
4766ce7a 603 if (!retval && !vconn->recv_any_version) {
6fd6ed71 604 const struct ofp_header *oh = msg->data;
982697a4
BP
605 if (oh->version != vconn->version) {
606 enum ofptype type;
607
6fd6ed71 608 if (ofptype_decode(&type, msg->data)
982697a4
BP
609 || (type != OFPTYPE_HELLO &&
610 type != OFPTYPE_ERROR &&
611 type != OFPTYPE_ECHO_REQUEST &&
612 type != OFPTYPE_ECHO_REPLY)) {
2b4cca6f
BP
613 struct ofpbuf *reply;
614
982697a4
BP
615 VLOG_ERR_RL(&bad_ofmsg_rl, "%s: received OpenFlow version "
616 "0x%02"PRIx8" != expected %02x",
617 vconn->name, oh->version, vconn->version);
2b4cca6f
BP
618
619 /* Send a "bad version" reply, if we can. */
620 reply = ofperr_encode_reply(OFPERR_OFPBRC_BAD_VERSION, oh);
621 retval = vconn_send(vconn, reply);
622 if (retval) {
623 VLOG_INFO_RL(&bad_ofmsg_rl,
624 "%s: failed to queue error reply (%s)",
625 vconn->name, ovs_strerror(retval));
626 ofpbuf_delete(reply);
627 }
628
629 /* Suppress the received message, as if it had not arrived. */
630 retval = EAGAIN;
982697a4 631 ofpbuf_delete(msg);
982697a4
BP
632 }
633 }
064af421 634 }
982697a4
BP
635
636 *msgp = retval ? NULL : msg;
064af421
BP
637 return retval;
638}
639
640static int
641do_recv(struct vconn *vconn, struct ofpbuf **msgp)
642{
a445a8d8 643 int retval = (vconn->vclass->recv)(vconn, msgp);
064af421 644 if (!retval) {
064af421
BP
645 COVERAGE_INC(vconn_received);
646 if (VLOG_IS_DBG_ENABLED()) {
4bc938cc
BP
647 char *s = ofp_to_string((*msgp)->data, (*msgp)->size,
648 NULL, NULL, 1);
064af421
BP
649 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
650 free(s);
651 }
064af421
BP
652 }
653 return retval;
654}
655
294e9fc8
BP
656/* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
657 * in which case ownership of 'msg' is transferred to the vconn. Success does
658 * not guarantee that 'msg' has been or ever will be delivered to the peer,
659 * only that it has been queued for transmission.
064af421
BP
660 *
661 * Returns a positive errno value on failure, in which case the caller
662 * retains ownership of 'msg'.
663 *
664 * vconn_send will not block. If 'msg' cannot be immediately accepted for
665 * transmission, it returns EAGAIN immediately. */
666int
667vconn_send(struct vconn *vconn, struct ofpbuf *msg)
668{
669 int retval = vconn_connect(vconn);
670 if (!retval) {
671 retval = do_send(vconn, msg);
672 }
673 return retval;
674}
675
676static int
677do_send(struct vconn *vconn, struct ofpbuf *msg)
678{
679 int retval;
680
6fd6ed71 681 ovs_assert(msg->size >= sizeof(struct ofp_header));
982697a4
BP
682
683 ofpmsg_update_length(msg);
064af421
BP
684 if (!VLOG_IS_DBG_ENABLED()) {
685 COVERAGE_INC(vconn_sent);
a445a8d8 686 retval = (vconn->vclass->send)(vconn, msg);
064af421 687 } else {
4bc938cc 688 char *s = ofp_to_string(msg->data, msg->size, NULL, NULL, 1);
a445a8d8 689 retval = (vconn->vclass->send)(vconn, msg);
064af421
BP
690 if (retval != EAGAIN) {
691 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
10a89ef0 692 vconn->name, ovs_strerror(retval), s);
064af421
BP
693 }
694 free(s);
695 }
696 return retval;
697}
698
6d1fb217 699/* Same as vconn_connect(), except that it waits until the connection on
04895042
IM
700 * 'vconn' completes or fails, but no more than 'timeout' milliseconds.
701 * Thus, it will never return EAGAIN. Negative value of 'timeout' means
702 * infinite waiting.*/
6d1fb217 703int
04895042 704vconn_connect_block(struct vconn *vconn, long long int timeout)
6d1fb217 705{
04895042 706 long long int deadline = (timeout >= 0
63e29301 707 ? time_msec() + timeout
04895042 708 : LLONG_MAX);
6d1fb217 709
04895042 710 int error;
6d1fb217 711 while ((error = vconn_connect(vconn)) == EAGAIN) {
04895042
IM
712 if (time_msec() > deadline) {
713 error = ETIMEDOUT;
714 break;
715 }
6d1fb217
BP
716 vconn_run(vconn);
717 vconn_run_wait(vconn);
718 vconn_connect_wait(vconn);
04895042
IM
719 if (deadline != LLONG_MAX) {
720 poll_timer_wait_until(deadline);
721 }
6d1fb217
BP
722 poll_block();
723 }
cb22974d 724 ovs_assert(error != EINPROGRESS);
6d1fb217
BP
725
726 return error;
727}
728
064af421
BP
729/* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
730int
731vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
732{
733 int retval;
b302749b
BP
734
735 fatal_signal_run();
736
064af421 737 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
60cb3eb8
BP
738 vconn_run(vconn);
739 vconn_run_wait(vconn);
064af421
BP
740 vconn_send_wait(vconn);
741 poll_block();
742 }
743 return retval;
744}
745
746/* Same as vconn_recv, except that it waits until a message is received. */
747int
748vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
749{
750 int retval;
b302749b
BP
751
752 fatal_signal_run();
753
064af421 754 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
60cb3eb8
BP
755 vconn_run(vconn);
756 vconn_run_wait(vconn);
064af421
BP
757 vconn_recv_wait(vconn);
758 poll_block();
759 }
760 return retval;
761}
762
db5076ee
JR
763static int
764vconn_recv_xid__(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp,
506c1ddb 765 struct ovs_list *errors)
064af421
BP
766{
767 for (;;) {
4408d18a 768 ovs_be32 recv_xid;
064af421 769 struct ofpbuf *reply;
db5076ee
JR
770 const struct ofp_header *oh;
771 enum ofptype type;
064af421
BP
772 int error;
773
774 error = vconn_recv_block(vconn, &reply);
775 if (error) {
776 *replyp = NULL;
777 return error;
778 }
db5076ee
JR
779 oh = reply->data;
780 recv_xid = oh->xid;
064af421
BP
781 if (xid == recv_xid) {
782 *replyp = reply;
783 return 0;
784 }
785
db5076ee 786 error = ofptype_decode(&type, oh);
df501177 787 if (!error && type == OFPTYPE_ERROR && errors) {
209aa4ad 788 ovs_list_push_back(errors, &reply->list_node);
db5076ee
JR
789 } else {
790 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
791 " != expected %08"PRIx32,
792 vconn->name, ntohl(recv_xid), ntohl(xid));
209aa4ad 793 ofpbuf_delete(reply);
db5076ee 794 }
064af421
BP
795 }
796}
797
db5076ee
JR
798/* Waits until a message with a transaction ID matching 'xid' is received on
799 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
800 * '*replyp' for the caller to examine and free. Otherwise returns a positive
801 * errno value, or EOF, and sets '*replyp' to null.
802 *
803 * 'request' is always destroyed, regardless of the return value. */
804int
805vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp)
806{
807 return vconn_recv_xid__(vconn, xid, replyp, NULL);
808}
809
810static int
811vconn_transact__(struct vconn *vconn, struct ofpbuf *request,
506c1ddb 812 struct ofpbuf **replyp, struct ovs_list *errors)
db5076ee
JR
813{
814 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
815 int error;
816
817 *replyp = NULL;
818 error = vconn_send_block(vconn, request);
819 if (error) {
820 ofpbuf_delete(request);
821 }
506c1ddb 822 return error ? error : vconn_recv_xid__(vconn, send_xid, replyp, errors);
db5076ee
JR
823}
824
064af421
BP
825/* Sends 'request' to 'vconn' and blocks until it receives a reply with a
826 * matching transaction ID. Returns 0 if successful, in which case the reply
827 * is stored in '*replyp' for the caller to examine and free. Otherwise
828 * returns a positive errno value, or EOF, and sets '*replyp' to null.
829 *
33af7dca
BP
830 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
831 * if there is no reply, this function can end up blocking forever (or until
832 * the peer drops the connection).
833 *
064af421
BP
834 * 'request' is always destroyed, regardless of the return value. */
835int
836vconn_transact(struct vconn *vconn, struct ofpbuf *request,
837 struct ofpbuf **replyp)
838{
db5076ee 839 return vconn_transact__(vconn, request, replyp, NULL);
064af421
BP
840}
841
506c1ddb
JR
842static int
843vconn_send_barrier(struct vconn *vconn, ovs_be32 *barrier_xid)
844{
845 struct ofpbuf *barrier;
846 int error;
847
848 /* Send barrier. */
849 barrier = ofputil_encode_barrier_request(vconn_get_version(vconn));
850 *barrier_xid = ((struct ofp_header *) barrier->data)->xid;
851 error = vconn_send_block(vconn, barrier);
852 if (error) {
853 ofpbuf_delete(barrier);
854 }
855 return error;
856}
857
33af7dca
BP
858/* Sends 'request' followed by a barrier request to 'vconn', then blocks until
859 * it receives a reply to the barrier. If successful, stores the reply to
860 * 'request' in '*replyp', if one was received, and otherwise NULL, then
861 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
862 * '*replyp' to null.
863 *
864 * This function is useful for sending an OpenFlow request that doesn't
865 * ordinarily include a reply but might report an error in special
866 * circumstances.
867 *
868 * 'request' is always destroyed, regardless of the return value. */
869int
870vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request,
871 struct ofpbuf **replyp)
872{
873 ovs_be32 request_xid;
874 ovs_be32 barrier_xid;
33af7dca
BP
875 int error;
876
877 *replyp = NULL;
878
879 /* Send request. */
6fd6ed71 880 request_xid = ((struct ofp_header *) request->data)->xid;
33af7dca
BP
881 error = vconn_send_block(vconn, request);
882 if (error) {
883 ofpbuf_delete(request);
884 return error;
885 }
886
887 /* Send barrier. */
506c1ddb 888 error = vconn_send_barrier(vconn, &barrier_xid);
33af7dca 889 if (error) {
33af7dca
BP
890 return error;
891 }
892
893 for (;;) {
894 struct ofpbuf *msg;
895 ovs_be32 msg_xid;
33af7dca
BP
896
897 error = vconn_recv_block(vconn, &msg);
898 if (error) {
899 ofpbuf_delete(*replyp);
900 *replyp = NULL;
901 return error;
902 }
903
6fd6ed71 904 msg_xid = ((struct ofp_header *) msg->data)->xid;
33af7dca
BP
905 if (msg_xid == request_xid) {
906 if (*replyp) {
907 VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with "
908 "xid %08"PRIx32, vconn->name, ntohl(msg_xid));
909 ofpbuf_delete(*replyp);
910 }
911 *replyp = msg;
912 } else {
913 ofpbuf_delete(msg);
914 if (msg_xid == barrier_xid) {
915 return 0;
916 } else {
917 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32
918 " != expected %08"PRIx32" or %08"PRIx32,
919 vconn->name, ntohl(msg_xid),
920 ntohl(request_xid), ntohl(barrier_xid));
921 }
922 }
923 }
924}
925
7f009380
BP
926/* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
927 * All of the requests on 'requests' are always destroyed, regardless of the
928 * return value. */
929int
ca6ba700 930vconn_transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests,
7f009380
BP
931 struct ofpbuf **replyp)
932{
5f03c983 933 struct ofpbuf *request;
7f009380 934
5f03c983 935 LIST_FOR_EACH_POP (request, list_node, requests) {
7f009380
BP
936 int error;
937
7f009380
BP
938 error = vconn_transact_noreply(vconn, request, replyp);
939 if (error || *replyp) {
940 ofpbuf_list_delete(requests);
941 return error;
942 }
943 }
944
945 *replyp = NULL;
946 return 0;
947}
948
4e413ac8
BP
949/* Sends 'requests' (which should be a multipart request) on 'vconn' and waits
950 * for the replies, which are put into 'replies'. Returns 0 if successful,
951 * otherwise an errno value. */
952int
953vconn_transact_multipart(struct vconn *vconn,
954 struct ovs_list *requests,
955 struct ovs_list *replies)
956{
957 struct ofpbuf *rq = ofpbuf_from_list(ovs_list_front(requests));
958 ovs_be32 send_xid = ((struct ofp_header *) rq->data)->xid;
959
960 ovs_list_init(replies);
961
962 /* Send all the requests. */
963 struct ofpbuf *b, *next;
964 LIST_FOR_EACH_SAFE (b, next, list_node, requests) {
965 ovs_list_remove(&b->list_node);
966 int error = vconn_send_block(vconn, b);
967 if (error) {
968 ofpbuf_delete(b);
969 }
970 }
971
972 /* Receive all the replies. */
973 bool more;
974 do {
975 struct ofpbuf *reply;
976 int error = vconn_recv_xid__(vconn, send_xid, &reply, NULL);
977 if (error) {
978 ofpbuf_list_delete(replies);
979 return error;
980 }
981
982 ovs_list_push_back(replies, &reply->list_node);
983 more = ofpmsg_is_stat_reply(reply->data) && ofpmp_more(reply->data);
984 } while (more);
985
986 return 0;
987}
988
d444a914
BP
989static int
990recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
991 struct ofpbuf **replyp,
992 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
993{
994 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
995 struct ofpbuf *reply = *replyp;
996
997 for (;;) {
998 int retval;
999 bool more;
1000
1001 /* Get a flow stats reply message, if we don't already have one. */
1002 if (!reply) {
1003 enum ofptype type;
1004 enum ofperr error;
1005
1006 do {
1007 error = vconn_recv_block(vconn, &reply);
1008 if (error) {
1009 return error;
1010 }
1011 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1012
1013 error = ofptype_decode(&type, reply->data);
1014 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
1015 VLOG_WARN_RL(&rl, "received bad reply: %s",
4bc938cc
BP
1016 ofp_to_string(reply->data, reply->size,
1017 NULL, NULL, 1));
d444a914
BP
1018 return EPROTO;
1019 }
1020 }
1021
1022 /* Pull an individual flow stats reply out of the message. */
1023 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1024 switch (retval) {
1025 case 0:
1026 *replyp = reply;
1027 return 0;
1028
1029 case EOF:
1030 more = ofpmp_more(reply->header);
1031 ofpbuf_delete(reply);
1032 reply = NULL;
1033 if (!more) {
1034 *replyp = NULL;
1035 return EOF;
1036 }
1037 break;
1038
1039 default:
1040 VLOG_WARN_RL(&rl, "parse error in reply (%s)",
1041 ofperr_to_string(retval));
1042 return EPROTO;
1043 }
1044 }
1045}
1046
1047/* Sends 'fsr' to 'vconn', encoding it with the given 'protocol', and then
1048 * waits for, parses, and accumulates all of the replies into '*fsesp' and
1049 * '*n_fsesp'. The caller is responsible for freeing all of the flows.
1050 * Returns 0 if successful, otherwise a positive errno value. */
1051int
1052vconn_dump_flows(struct vconn *vconn,
1053 const struct ofputil_flow_stats_request *fsr,
1054 enum ofputil_protocol protocol,
1055 struct ofputil_flow_stats **fsesp, size_t *n_fsesp)
1056{
1057 struct ofputil_flow_stats *fses = NULL;
1058 size_t n_fses = 0;
1059 size_t allocated_fses = 0;
1060
1061 struct ofpbuf *request = ofputil_encode_flow_stats_request(fsr, protocol);
1062 const struct ofp_header *oh = request->data;
1063 ovs_be32 send_xid = oh->xid;
1064 int error = vconn_send_block(vconn, request);
1065 if (error) {
1066 goto exit;
1067 }
1068
1069 struct ofpbuf *reply = NULL;
1070 struct ofpbuf ofpacts;
1071 ofpbuf_init(&ofpacts, 0);
1072 for (;;) {
1073 if (n_fses >= allocated_fses) {
1074 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1075 }
1076
1077 struct ofputil_flow_stats *fs = &fses[n_fses];
1078 error = recv_flow_stats_reply(vconn, send_xid, &reply, fs, &ofpacts);
1079 if (error) {
1080 if (error == EOF) {
1081 error = 0;
1082 }
1083 break;
1084 }
1085 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1086 n_fses++;
1087 }
1088 ofpbuf_uninit(&ofpacts);
1089 ofpbuf_delete(reply);
1090
1091 if (error) {
1092 for (size_t i = 0; i < n_fses; i++) {
1093 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1094 }
1095 free(fses);
1096
1097 fses = NULL;
1098 n_fses = 0;
1099 }
1100
1101exit:
1102 *fsesp = fses;
1103 *n_fsesp = n_fses;
1104 return error;
1105}
1106
1107
db5076ee
JR
1108static enum ofperr
1109vconn_bundle_reply_validate(struct ofpbuf *reply,
1110 struct ofputil_bundle_ctrl_msg *request,
506c1ddb 1111 struct ovs_list *errors)
db5076ee
JR
1112{
1113 const struct ofp_header *oh;
1114 enum ofptype type;
1115 enum ofperr error;
1116 struct ofputil_bundle_ctrl_msg rbc;
1117
1118 oh = reply->data;
1119 error = ofptype_decode(&type, oh);
1120 if (error) {
1121 return error;
1122 }
1123
1124 if (type == OFPTYPE_ERROR) {
209aa4ad
BP
1125 struct ofpbuf *copy = ofpbuf_clone(reply);
1126 ovs_list_push_back(errors, &copy->list_node);
db5076ee
JR
1127 return ofperr_decode_msg(oh, NULL);
1128 }
1129 if (type != OFPTYPE_BUNDLE_CONTROL) {
1130 return OFPERR_OFPBRC_BAD_TYPE;
1131 }
1132
1133 error = ofputil_decode_bundle_ctrl(oh, &rbc);
1134 if (error) {
1135 return error;
1136 }
1137
1138 if (rbc.bundle_id != request->bundle_id) {
1139 return OFPERR_OFPBFC_BAD_ID;
1140 }
1141
1142 if (rbc.type != request->type + 1) {
1143 return OFPERR_OFPBFC_BAD_TYPE;
1144 }
1145
1146 return 0;
1147}
1148
1149/* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either
1150 * an error or the corresponding bundle control message response.
1151 *
506c1ddb
JR
1152 * 'errors' is a list to which any OpenFlow errors relating to bundle
1153 * processing are appended. Caller is responsible for releasing the memory of
1154 * each node in the list on return.
db5076ee
JR
1155 *
1156 * Returns errno value, or 0 when successful. */
1157static int
1158vconn_bundle_control_transact(struct vconn *vconn,
1159 struct ofputil_bundle_ctrl_msg *bc,
506c1ddb 1160 uint16_t type, struct ovs_list *errors)
db5076ee
JR
1161{
1162 struct ofpbuf *request, *reply;
1163 int error;
1164 enum ofperr ofperr;
1165
1166 bc->type = type;
1167 request = ofputil_encode_bundle_ctrl_request(vconn->version, bc);
1168 ofpmsg_update_length(request);
506c1ddb 1169 error = vconn_transact__(vconn, request, &reply, errors);
db5076ee
JR
1170 if (error) {
1171 return error;
1172 }
1173
506c1ddb 1174 ofperr = vconn_bundle_reply_validate(reply, bc, errors);
db5076ee
JR
1175 ofpbuf_delete(reply);
1176
1177 return ofperr ? EPROTO : 0;
1178}
1179
1180/* Checks if error responses can be received on 'vconn'. */
1181static void
506c1ddb 1182vconn_recv_error(struct vconn *vconn, struct ovs_list *errors)
db5076ee
JR
1183{
1184 int error;
1185
1186 do {
1187 struct ofpbuf *reply;
1188
1189 error = vconn_recv(vconn, &reply);
1190 if (!error) {
1191 const struct ofp_header *oh;
1192 enum ofptype type;
1193 enum ofperr ofperr;
1194
1195 oh = reply->data;
1196 ofperr = ofptype_decode(&type, oh);
1197 if (!ofperr && type == OFPTYPE_ERROR) {
209aa4ad 1198 ovs_list_push_back(errors, &reply->list_node);
db5076ee
JR
1199 } else {
1200 VLOG_DBG_RL(&bad_ofmsg_rl,
1201 "%s: received unexpected reply with xid %08"PRIx32,
1202 vconn->name, ntohl(oh->xid));
209aa4ad 1203 ofpbuf_delete(reply);
db5076ee 1204 }
db5076ee
JR
1205 }
1206 } while (!error);
1207}
1208
506c1ddb
JR
1209/* Sends a barrier and waits for the barrier response and stores any errors
1210 * that are received before the barrier response. */
1211static int
1212vconn_bundle_barrier_transact(struct vconn *vconn, struct ovs_list *errors)
1213{
1214 struct ofpbuf *reply;
1215 ovs_be32 barrier_xid;
1216 int error;
1217
1218 error = vconn_send_barrier(vconn, &barrier_xid);
1219 if (error) {
1220 return error;
1221 }
1222
1223 error = vconn_recv_xid__(vconn, barrier_xid, &reply, errors);
1224 if (error) {
1225 return error;
1226 }
1227 ofpbuf_delete(reply);
1228 return 0;
1229}
1230
db5076ee
JR
1231static int
1232vconn_bundle_add_msg(struct vconn *vconn, struct ofputil_bundle_ctrl_msg *bc,
1233 struct ofpbuf *msg,
506c1ddb 1234 struct ovs_list *errors)
db5076ee
JR
1235{
1236 struct ofputil_bundle_add_msg bam;
1237 struct ofpbuf *request;
1238 int error;
1239
bcf899f7
JR
1240 ofpmsg_update_length(msg);
1241
db5076ee
JR
1242 bam.bundle_id = bc->bundle_id;
1243 bam.flags = bc->flags;
1244 bam.msg = msg->data;
1245
1246 request = ofputil_encode_bundle_add(vconn->version, &bam);
db5076ee
JR
1247
1248 error = vconn_send_block(vconn, request);
1249 if (!error) {
1250 /* Check for an error return, so that the socket buffer does not become
1251 * full of errors. */
506c1ddb 1252 vconn_recv_error(vconn, errors);
db5076ee
JR
1253 }
1254 return error;
1255}
1256
209aa4ad
BP
1257/* Appends ofpbufs for received errors, if any, to 'errors'. The caller must
1258 * free the received errors. */
db5076ee
JR
1259int
1260vconn_bundle_transact(struct vconn *vconn, struct ovs_list *requests,
506c1ddb 1261 uint16_t flags, struct ovs_list *errors)
db5076ee
JR
1262{
1263 struct ofputil_bundle_ctrl_msg bc;
1264 struct ofpbuf *request;
1265 int error;
1266
506c1ddb
JR
1267 ovs_list_init(errors);
1268
db5076ee
JR
1269 memset(&bc, 0, sizeof bc);
1270 bc.flags = flags;
1271 error = vconn_bundle_control_transact(vconn, &bc, OFPBCT_OPEN_REQUEST,
506c1ddb 1272 errors);
db5076ee
JR
1273 if (error) {
1274 return error;
1275 }
1276
1277 LIST_FOR_EACH (request, list_node, requests) {
506c1ddb 1278 error = vconn_bundle_add_msg(vconn, &bc, request, errors);
db5076ee
JR
1279 if (error) {
1280 break;
1281 }
1282 }
1283
506c1ddb
JR
1284 if (!error) {
1285 /* A failing message does not invalidate the bundle, but the message is
1286 * simply not added to the bundle. Since we do not want to commit if
1287 * any of the messages failed, we need to explicitly sync with barrier
1288 * before we issue the commit message. */
1289 error = vconn_bundle_barrier_transact(vconn, errors);
1290 }
1291 if (!error && !ovs_list_is_empty(errors)) {
1292 error = EPROTO;
1293 }
1294
1295 /* Commit only if no errors are received. */
db5076ee
JR
1296 if (!error) {
1297 error = vconn_bundle_control_transact(vconn, &bc,
1298 OFPBCT_COMMIT_REQUEST,
506c1ddb 1299 errors);
db5076ee 1300 } else {
db5076ee 1301 vconn_bundle_control_transact(vconn, &bc, OFPBCT_DISCARD_REQUEST,
506c1ddb 1302 errors);
db5076ee
JR
1303 }
1304 return error;
1305}
1306
064af421
BP
1307void
1308vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
1309{
cb22974d 1310 ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
064af421
BP
1311
1312 switch (vconn->state) {
1313 case VCS_CONNECTING:
1314 wait = WAIT_CONNECT;
1315 break;
1316
1317 case VCS_SEND_HELLO:
1318 case VCS_SEND_ERROR:
1319 wait = WAIT_SEND;
1320 break;
1321
1322 case VCS_RECV_HELLO:
1323 wait = WAIT_RECV;
1324 break;
1325
1326 case VCS_CONNECTED:
1327 break;
1328
1329 case VCS_DISCONNECTED:
1330 poll_immediate_wake();
1331 return;
1332 }
a445a8d8 1333 (vconn->vclass->wait)(vconn, wait);
064af421
BP
1334}
1335
1336void
1337vconn_connect_wait(struct vconn *vconn)
1338{
1339 vconn_wait(vconn, WAIT_CONNECT);
1340}
1341
1342void
1343vconn_recv_wait(struct vconn *vconn)
1344{
1345 vconn_wait(vconn, WAIT_RECV);
1346}
1347
1348void
1349vconn_send_wait(struct vconn *vconn)
1350{
1351 vconn_wait(vconn, WAIT_SEND);
1352}
1353
30012c72
BP
1354/* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
1355 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
1356 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
1357 * class exists. */
1358static int
5b712636 1359pvconn_lookup_class(const char *name, const struct pvconn_class **classp)
30012c72
BP
1360{
1361 size_t prefix_len;
1362
1363 prefix_len = strcspn(name, ":");
1364 if (name[prefix_len] != '\0') {
1365 size_t i;
1366
1367 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
5b712636 1368 const struct pvconn_class *class = pvconn_classes[i];
30012c72
BP
1369 if (strlen(class->name) == prefix_len
1370 && !memcmp(class->name, name, prefix_len)) {
1371 *classp = class;
1372 return 0;
1373 }
1374 }
1375 }
1376
1377 *classp = NULL;
1378 return EAFNOSUPPORT;
1379}
1380
1381/* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
1382 * a supported connection type, otherwise EAFNOSUPPORT. */
1383int
1384pvconn_verify_name(const char *name)
1385{
5b712636 1386 const struct pvconn_class *class;
30012c72
BP
1387 return pvconn_lookup_class(name, &class);
1388}
1389
064af421
BP
1390/* Attempts to start listening for OpenFlow connections. 'name' is a
1391 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
1392 * class's name and ARGS are vconn class-specific.
1393 *
7a25bd99
SH
1394 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1395 * protocol version acceptable to both peers on the connection. The version
1396 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1397 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1398 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1399 *
064af421
BP
1400 * Returns 0 if successful, otherwise a positive errno value. If successful,
1401 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1402 * pointer. */
1403int
82c8c53c
BP
1404pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
1405 struct pvconn **pvconnp)
064af421 1406{
5b712636 1407 const struct pvconn_class *class;
30012c72
BP
1408 struct pvconn *pvconn;
1409 char *suffix_copy;
1410 int error;
064af421
BP
1411
1412 check_vconn_classes();
1413
7a25bd99
SH
1414 if (!allowed_versions) {
1415 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
1416 }
1417
30012c72
BP
1418 /* Look up the class. */
1419 error = pvconn_lookup_class(name, &class);
1420 if (!class) {
1421 goto error;
064af421 1422 }
30012c72
BP
1423
1424 /* Call class's "open" function. */
1425 suffix_copy = xstrdup(strchr(name, ':') + 1);
7a25bd99 1426 error = class->listen(name, allowed_versions, suffix_copy, &pvconn, dscp);
30012c72
BP
1427 free(suffix_copy);
1428 if (error) {
1429 goto error;
064af421 1430 }
30012c72
BP
1431
1432 /* Success. */
1433 *pvconnp = pvconn;
1434 return 0;
1435
1436error:
1437 *pvconnp = NULL;
1438 return error;
064af421
BP
1439}
1440
1441/* Returns the name that was used to open 'pvconn'. The caller must not
1442 * modify or free the name. */
1443const char *
1444pvconn_get_name(const struct pvconn *pvconn)
1445{
1446 return pvconn->name;
1447}
1448
1449/* Closes 'pvconn'. */
1450void
1451pvconn_close(struct pvconn *pvconn)
1452{
1453 if (pvconn != NULL) {
1454 char *name = pvconn->name;
a445a8d8 1455 (pvconn->pvclass->close)(pvconn);
064af421
BP
1456 free(name);
1457 }
1458}
1459
1460/* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1461 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1462 * errno value.
1463 *
064af421
BP
1464 * pvconn_accept() will not block waiting for a connection. If no connection
1465 * is ready to be accepted, it returns EAGAIN immediately. */
1466int
7a25bd99 1467pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn)
064af421 1468{
a445a8d8 1469 int retval = (pvconn->pvclass->accept)(pvconn, new_vconn);
064af421
BP
1470 if (retval) {
1471 *new_vconn = NULL;
1472 } else {
cb22974d 1473 ovs_assert((*new_vconn)->state != VCS_CONNECTING
a445a8d8 1474 || (*new_vconn)->vclass->connect);
064af421
BP
1475 }
1476 return retval;
1477}
1478
1479void
1480pvconn_wait(struct pvconn *pvconn)
1481{
a445a8d8 1482 (pvconn->pvclass->wait)(pvconn);
064af421
BP
1483}
1484
85ab0a02
BP
1485/* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1486 * The initial connection status, supplied as 'connect_status', is interpreted
1487 * as follows:
1488 *
1489 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1490 * called in the normal fashion.
1491 *
1492 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1493 * function should be called to complete the connection.
1494 *
1495 * - Other positive errno values indicate that the connection failed with
1496 * the specified error.
1497 *
1498 * After calling this function, vconn_close() must be used to destroy 'vconn',
1499 * otherwise resources will be leaked.
1500 *
1501 * The caller retains ownership of 'name'. */
064af421 1502void
5b712636
BP
1503vconn_init(struct vconn *vconn, const struct vconn_class *class,
1504 int connect_status, const char *name, uint32_t allowed_versions)
064af421 1505{
267e915f 1506 memset(vconn, 0, sizeof *vconn);
a445a8d8 1507 vconn->vclass = class;
064af421
BP
1508 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1509 : !connect_status ? VCS_SEND_HELLO
1510 : VCS_DISCONNECTED);
1511 vconn->error = connect_status;
7a25bd99 1512 vconn->allowed_versions = allowed_versions;
064af421 1513 vconn->name = xstrdup(name);
cb22974d 1514 ovs_assert(vconn->state != VCS_CONNECTING || class->connect);
064af421
BP
1515}
1516
1517void
5b712636 1518pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class,
7a25bd99 1519 const char *name, uint32_t allowed_versions)
064af421 1520{
a445a8d8 1521 pvconn->pvclass = class;
064af421 1522 pvconn->name = xstrdup(name);
7a25bd99 1523 pvconn->allowed_versions = allowed_versions;
064af421 1524}