2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009-2010 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2010-04-29 15:36:32 +00:00
|
|
|
#include <sys/types.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <sys/queue.h>
|
2010-04-29 15:36:32 +00:00
|
|
|
#include <sys/socket.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2011-02-02 15:53:09 +00:00
|
|
|
#include <string.h>
|
2011-02-02 15:23:07 +00:00
|
|
|
#include <strings.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
#include "pjdlog.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "proto.h"
|
|
|
|
#include "proto_impl.h"
|
|
|
|
|
|
|
|
#define PROTO_CONN_MAGIC 0x907041c
|
|
|
|
struct proto_conn {
|
2011-03-22 16:21:11 +00:00
|
|
|
int pc_magic;
|
|
|
|
struct proto *pc_proto;
|
|
|
|
void *pc_ctx;
|
|
|
|
int pc_side;
|
2010-02-18 23:16:19 +00:00
|
|
|
#define PROTO_SIDE_CLIENT 0
|
|
|
|
#define PROTO_SIDE_SERVER_LISTEN 1
|
|
|
|
#define PROTO_SIDE_SERVER_WORK 2
|
|
|
|
};
|
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
static TAILQ_HEAD(, proto) protos = TAILQ_HEAD_INITIALIZER(protos);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
void
|
2011-03-22 16:21:11 +00:00
|
|
|
proto_register(struct proto *proto, bool isdefault)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2010-08-05 17:56:41 +00:00
|
|
|
static bool seen_default = false;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-05 17:56:41 +00:00
|
|
|
if (!isdefault)
|
2011-03-22 16:21:11 +00:00
|
|
|
TAILQ_INSERT_HEAD(&protos, proto, prt_next);
|
2010-08-05 17:56:41 +00:00
|
|
|
else {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(!seen_default);
|
2010-08-05 17:56:41 +00:00
|
|
|
seen_default = true;
|
2011-03-22 16:21:11 +00:00
|
|
|
TAILQ_INSERT_TAIL(&protos, proto, prt_next);
|
2010-08-05 17:56:41 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 15:23:07 +00:00
|
|
|
static struct proto_conn *
|
2011-03-22 16:21:11 +00:00
|
|
|
proto_alloc(struct proto *proto, int side)
|
2011-02-02 15:23:07 +00:00
|
|
|
{
|
|
|
|
struct proto_conn *conn;
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(proto != NULL);
|
|
|
|
PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
|
|
|
|
side == PROTO_SIDE_SERVER_LISTEN ||
|
|
|
|
side == PROTO_SIDE_SERVER_WORK);
|
|
|
|
|
|
|
|
conn = malloc(sizeof(*conn));
|
|
|
|
if (conn != NULL) {
|
|
|
|
conn->pc_proto = proto;
|
|
|
|
conn->pc_side = side;
|
|
|
|
conn->pc_magic = PROTO_CONN_MAGIC;
|
|
|
|
}
|
|
|
|
return (conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
proto_free(struct proto_conn *conn)
|
|
|
|
{
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT ||
|
|
|
|
conn->pc_side == PROTO_SIDE_SERVER_LISTEN ||
|
|
|
|
conn->pc_side == PROTO_SIDE_SERVER_WORK);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
|
|
|
|
|
|
|
bzero(conn, sizeof(*conn));
|
|
|
|
free(conn);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static int
|
2011-03-21 08:54:59 +00:00
|
|
|
proto_common_setup(const char *srcaddr, const char *dstaddr,
|
|
|
|
struct proto_conn **connp, int side)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-03-22 16:21:11 +00:00
|
|
|
struct proto *proto;
|
2010-02-18 23:16:19 +00:00
|
|
|
struct proto_conn *conn;
|
|
|
|
void *ctx;
|
|
|
|
int ret;
|
|
|
|
|
2011-02-02 15:23:07 +00:00
|
|
|
PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
|
|
|
|
side == PROTO_SIDE_SERVER_LISTEN);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
TAILQ_FOREACH(proto, &protos, prt_next) {
|
2011-02-02 08:24:26 +00:00
|
|
|
if (side == PROTO_SIDE_CLIENT) {
|
2011-03-22 16:21:11 +00:00
|
|
|
if (proto->prt_client == NULL)
|
2011-02-02 08:24:26 +00:00
|
|
|
ret = -1;
|
|
|
|
else
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = proto->prt_client(srcaddr, dstaddr, &ctx);
|
2011-02-02 08:24:26 +00:00
|
|
|
} else /* if (side == PROTO_SIDE_SERVER_LISTEN) */ {
|
2011-03-22 16:21:11 +00:00
|
|
|
if (proto->prt_server == NULL)
|
2011-02-02 08:24:26 +00:00
|
|
|
ret = -1;
|
|
|
|
else
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = proto->prt_server(dstaddr, &ctx);
|
2011-02-02 08:24:26 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* ret == 0 - success
|
2011-03-21 08:54:59 +00:00
|
|
|
* ret == -1 - dstaddr is not for this protocol
|
2012-01-07 16:09:33 +00:00
|
|
|
* ret > 0 - right protocol, but an error occurred
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
if (ret >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (proto == NULL) {
|
|
|
|
/* Unrecognized address. */
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
2012-01-07 16:09:33 +00:00
|
|
|
/* An error occurred. */
|
2010-02-18 23:16:19 +00:00
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-02-02 15:23:07 +00:00
|
|
|
conn = proto_alloc(proto, side);
|
|
|
|
if (conn == NULL) {
|
2011-03-22 16:21:11 +00:00
|
|
|
if (proto->prt_close != NULL)
|
|
|
|
proto->prt_close(ctx);
|
2011-02-02 15:23:07 +00:00
|
|
|
errno = ENOMEM;
|
|
|
|
return (-1);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
conn->pc_ctx = ctx;
|
|
|
|
*connp = conn;
|
2011-02-02 15:23:07 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-03-21 08:54:59 +00:00
|
|
|
proto_client(const char *srcaddr, const char *dstaddr,
|
|
|
|
struct proto_conn **connp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
|
2011-03-21 08:54:59 +00:00
|
|
|
return (proto_common_setup(srcaddr, dstaddr, connp, PROTO_SIDE_CLIENT));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-02-02 15:42:00 +00:00
|
|
|
proto_connect(struct proto_conn *conn, int timeout)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_connect != NULL);
|
2011-02-02 15:46:28 +00:00
|
|
|
PJDLOG_ASSERT(timeout >= -1);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = conn->pc_proto->prt_connect(conn->pc_ctx, timeout);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-02-02 15:46:28 +00:00
|
|
|
int
|
|
|
|
proto_connect_wait(struct proto_conn *conn, int timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_connect_wait != NULL);
|
2011-02-02 15:46:28 +00:00
|
|
|
PJDLOG_ASSERT(timeout >= 0);
|
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = conn->pc_proto->prt_connect_wait(conn->pc_ctx, timeout);
|
2011-02-02 15:46:28 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
int
|
|
|
|
proto_server(const char *addr, struct proto_conn **connp)
|
|
|
|
{
|
|
|
|
|
2011-03-21 08:54:59 +00:00
|
|
|
return (proto_common_setup(NULL, addr, connp, PROTO_SIDE_SERVER_LISTEN));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
|
|
|
|
{
|
|
|
|
struct proto_conn *newconn;
|
|
|
|
int ret;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_accept != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-02-02 15:23:07 +00:00
|
|
|
newconn = proto_alloc(conn->pc_proto, PROTO_SIDE_SERVER_WORK);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (newconn == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = conn->pc_proto->prt_accept(conn->pc_ctx, &newconn->pc_ctx);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (ret != 0) {
|
2011-02-02 15:23:07 +00:00
|
|
|
proto_free(newconn);
|
2010-02-18 23:16:19 +00:00
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
*newconnp = newconn;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-30 22:26:42 +00:00
|
|
|
proto_send(const struct proto_conn *conn, const void *data, size_t size)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_send != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = conn->pc_proto->prt_send(conn->pc_ctx, data, size, -1);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-30 22:26:42 +00:00
|
|
|
proto_recv(const struct proto_conn *conn, void *data, size_t size)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
ret = conn->pc_proto->prt_recv(conn->pc_ctx, data, size, NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:35:17 +00:00
|
|
|
int
|
2011-02-02 15:53:09 +00:00
|
|
|
proto_connection_send(const struct proto_conn *conn, struct proto_conn *mconn)
|
2011-01-31 18:35:17 +00:00
|
|
|
{
|
2011-02-02 15:53:09 +00:00
|
|
|
const char *protoname;
|
|
|
|
int ret, fd;
|
2011-01-31 18:35:17 +00:00
|
|
|
|
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_send != NULL);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(mconn != NULL);
|
|
|
|
PJDLOG_ASSERT(mconn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(mconn->pc_proto != NULL);
|
|
|
|
fd = proto_descriptor(mconn);
|
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
2011-03-22 16:21:11 +00:00
|
|
|
protoname = mconn->pc_proto->prt_name;
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(protoname != NULL);
|
|
|
|
|
MFC r257155, r257582, r259191, r259192, r259193, r259194, r259195, r259196:
r257155:
Make hastctl list command output current queue sizes.
Reviewed by: pjd
r257582 (pjd):
Correct alignment.
r259191:
For memsync replication, hio_countdown is used not only as an
indication when a request can be moved to done queue, but also for
detecting the current state of memsync request.
This approach has problems, e.g. leaking a request if memsynk ack from
the secondary failed, or racy usage of write_complete, which should be
called only once per write request, but for memsync can be entered by
local_send_thread and ggate_send_thread simultaneously.
So the following approach is implemented instead:
1) Use hio_countdown only for counting components we waiting to
complete, i.e. initially it is always 2 for any replication mode.
2) To distinguish between "memsync ack" and "memsync fin" responses
from the secondary, add and use hio_memsyncacked field.
3) write_complete() in component threads is called only before
releasing hio_countdown (i.e. before the hio may be returned to the
done queue).
4) Add and use hio_writecount refcounter to detect when
write_complete() can be called in memsync case.
Reported by: Pete French petefrench ingresso.co.uk
Tested by: Pete French petefrench ingresso.co.uk
r259192:
Add some macros to make the code more readable (no functional chages).
r259193:
Fix compiler warnings.
r259194:
In remote_send_thread, if sending a request fails don't take the
request back from the receive queue -- it might already be processed
by remote_recv_thread, which lead to crashes like below:
(primary) Unable to receive reply header: Connection reset by peer.
(primary) Unable to send request (Connection reset by peer):
WRITE(954662912, 131072).
(primary) Disconnected from kopusha:7772.
(primary) Increasing localcnt to 1.
(primary) Assertion failed: (old > 0), function refcnt_release,
file refcnt.h, line 62.
Taking the request back was not necessary (it would properly be
processed by the remote_recv_thread) and only complicated things.
r259195:
Send wakeup to threads waiting on empty queue before releasing the
lock to decrease spurious wakeups.
Submitted by: davidxu
r259196:
Check remote protocol version only for the first connection (when it
is actually sent by the remote node).
Otherwise it generated confusing "Negotiated protocol version 1" debug
messages when processing the second connection.
2013-12-28 19:21:22 +00:00
|
|
|
ret = conn->pc_proto->prt_send(conn->pc_ctx,
|
|
|
|
(const unsigned char *)protoname, strlen(protoname) + 1, fd);
|
2011-02-02 15:53:09 +00:00
|
|
|
proto_close(mconn);
|
2011-01-31 18:35:17 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-02-02 15:53:09 +00:00
|
|
|
proto_connection_recv(const struct proto_conn *conn, bool client,
|
|
|
|
struct proto_conn **newconnp)
|
2011-01-31 18:35:17 +00:00
|
|
|
{
|
2011-02-02 15:53:09 +00:00
|
|
|
char protoname[128];
|
2011-03-22 16:21:11 +00:00
|
|
|
struct proto *proto;
|
2011-02-02 15:53:09 +00:00
|
|
|
struct proto_conn *newconn;
|
|
|
|
int ret, fd;
|
2011-01-31 18:35:17 +00:00
|
|
|
|
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(newconnp != NULL);
|
|
|
|
|
|
|
|
bzero(protoname, sizeof(protoname));
|
|
|
|
|
MFC r257155, r257582, r259191, r259192, r259193, r259194, r259195, r259196:
r257155:
Make hastctl list command output current queue sizes.
Reviewed by: pjd
r257582 (pjd):
Correct alignment.
r259191:
For memsync replication, hio_countdown is used not only as an
indication when a request can be moved to done queue, but also for
detecting the current state of memsync request.
This approach has problems, e.g. leaking a request if memsynk ack from
the secondary failed, or racy usage of write_complete, which should be
called only once per write request, but for memsync can be entered by
local_send_thread and ggate_send_thread simultaneously.
So the following approach is implemented instead:
1) Use hio_countdown only for counting components we waiting to
complete, i.e. initially it is always 2 for any replication mode.
2) To distinguish between "memsync ack" and "memsync fin" responses
from the secondary, add and use hio_memsyncacked field.
3) write_complete() in component threads is called only before
releasing hio_countdown (i.e. before the hio may be returned to the
done queue).
4) Add and use hio_writecount refcounter to detect when
write_complete() can be called in memsync case.
Reported by: Pete French petefrench ingresso.co.uk
Tested by: Pete French petefrench ingresso.co.uk
r259192:
Add some macros to make the code more readable (no functional chages).
r259193:
Fix compiler warnings.
r259194:
In remote_send_thread, if sending a request fails don't take the
request back from the receive queue -- it might already be processed
by remote_recv_thread, which lead to crashes like below:
(primary) Unable to receive reply header: Connection reset by peer.
(primary) Unable to send request (Connection reset by peer):
WRITE(954662912, 131072).
(primary) Disconnected from kopusha:7772.
(primary) Increasing localcnt to 1.
(primary) Assertion failed: (old > 0), function refcnt_release,
file refcnt.h, line 62.
Taking the request back was not necessary (it would properly be
processed by the remote_recv_thread) and only complicated things.
r259195:
Send wakeup to threads waiting on empty queue before releasing the
lock to decrease spurious wakeups.
Submitted by: davidxu
r259196:
Check remote protocol version only for the first connection (when it
is actually sent by the remote node).
Otherwise it generated confusing "Negotiated protocol version 1" debug
messages when processing the second connection.
2013-12-28 19:21:22 +00:00
|
|
|
ret = conn->pc_proto->prt_recv(conn->pc_ctx, (unsigned char *)protoname,
|
2011-02-02 15:53:09 +00:00
|
|
|
sizeof(protoname) - 1, &fd);
|
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
2011-01-31 18:35:17 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
TAILQ_FOREACH(proto, &protos, prt_next) {
|
|
|
|
if (strcmp(proto->prt_name, protoname) == 0)
|
2011-02-02 15:53:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (proto == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
newconn = proto_alloc(proto,
|
|
|
|
client ? PROTO_SIDE_CLIENT : PROTO_SIDE_SERVER_WORK);
|
|
|
|
if (newconn == NULL)
|
|
|
|
return (-1);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(newconn->pc_proto->prt_wrap != NULL);
|
|
|
|
ret = newconn->pc_proto->prt_wrap(fd, client, &newconn->pc_ctx);
|
2011-01-31 18:35:17 +00:00
|
|
|
if (ret != 0) {
|
2011-02-02 15:53:09 +00:00
|
|
|
proto_free(newconn);
|
2011-01-31 18:35:17 +00:00
|
|
|
errno = ret;
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-02-02 15:53:09 +00:00
|
|
|
|
|
|
|
*newconnp = newconn;
|
|
|
|
|
2011-01-31 18:35:17 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
int
|
|
|
|
proto_descriptor(const struct proto_conn *conn)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_descriptor != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
return (conn->pc_proto->prt_descriptor(conn->pc_ctx));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
proto_address_match(const struct proto_conn *conn, const char *addr)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_address_match != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
return (conn->pc_proto->prt_address_match(conn->pc_ctx, addr));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_local_address != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
conn->pc_proto->prt_local_address(conn->pc_ctx, addr, size);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_remote_address != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
conn->pc_proto->prt_remote_address(conn->pc_ctx, addr, size);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 15:36:32 +00:00
|
|
|
int
|
|
|
|
proto_timeout(const struct proto_conn *conn, int timeout)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int fd;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2010-04-29 15:36:32 +00:00
|
|
|
|
|
|
|
fd = proto_descriptor(conn);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (fd == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
return (-1);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
void
|
|
|
|
proto_close(struct proto_conn *conn)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(conn != NULL);
|
|
|
|
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
|
|
|
|
PJDLOG_ASSERT(conn->pc_proto != NULL);
|
2011-03-22 16:21:11 +00:00
|
|
|
PJDLOG_ASSERT(conn->pc_proto->prt_close != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-03-22 16:21:11 +00:00
|
|
|
conn->pc_proto->prt_close(conn->pc_ctx);
|
2011-02-02 15:23:07 +00:00
|
|
|
proto_free(conn);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|