diff options
Diffstat (limited to 'rpc/rpc-transport')
| -rw-r--r-- | rpc/rpc-transport/rdma/src/Makefile.am | 10 | ||||
| -rw-r--r-- | rpc/rpc-transport/rdma/src/name.c | 263 | ||||
| -rw-r--r-- | rpc/rpc-transport/rdma/src/name.h | 28 | ||||
| -rw-r--r-- | rpc/rpc-transport/rdma/src/rdma.c | 4622 | ||||
| -rw-r--r-- | rpc/rpc-transport/rdma/src/rdma.h | 344 | ||||
| -rw-r--r-- | rpc/rpc-transport/socket/src/Makefile.am | 10 | ||||
| -rw-r--r-- | rpc/rpc-transport/socket/src/name.c | 180 | ||||
| -rw-r--r-- | rpc/rpc-transport/socket/src/name.h | 19 | ||||
| -rw-r--r-- | rpc/rpc-transport/socket/src/socket.c | 2618 | ||||
| -rw-r--r-- | rpc/rpc-transport/socket/src/socket.h | 149 |
10 files changed, 4467 insertions, 3776 deletions
diff --git a/rpc/rpc-transport/rdma/src/Makefile.am b/rpc/rpc-transport/rdma/src/Makefile.am index bc888b175..2bf7cf238 100644 --- a/rpc/rpc-transport/rdma/src/Makefile.am +++ b/rpc/rpc-transport/rdma/src/Makefile.am @@ -3,18 +3,20 @@ transport_LTLIBRARIES = rdma.la transportdir = $(libdir)/glusterfs/$(PACKAGE_VERSION)/rpc-transport -rdma_la_LDFLAGS = -module -avoidversion +rdma_la_LDFLAGS = -module -avoid-version rdma_la_SOURCES = rdma.c name.c rdma_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la \ - -libverbs + -libverbs -lrdmacm noinst_HEADERS = rdma.h name.h -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src/ \ -I$(top_srcdir)/xlators/protocol/lib/src/ -shared -nostartfiles $(GF_CFLAGS) -AM_CFLAGS = -fPIC -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -Wall -D$(GF_HOST_OS) \ +AM_CPPFLAGS = $(GF_CPPFLAGS) \ -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src/ \ - -I$(top_srcdir)/xlators/protocol/lib/src -shared -nostartfiles $(GF_CFLAGS) + -I$(top_srcdir)/rpc/xdr/src + +AM_CFLAGS = -Wall $(GF_CFLAGS) CLEANFILES = *~ diff --git a/rpc/rpc-transport/rdma/src/name.c b/rpc/rpc-transport/rdma/src/name.c index ae5c5d356..c57428ad6 100644 --- a/rpc/rpc-transport/rdma/src/name.c +++ b/rpc/rpc-transport/rdma/src/name.c @@ -1,20 +1,11 @@ /* - Copyright (c) 2008-2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #include <sys/types.h> @@ -22,12 +13,7 @@ #include <errno.h> #include <netdb.h> #include <string.h> - -#ifdef CLIENT_PORT_CEILING -#undef CLIENT_PORT_CEILING -#endif - -#define CLIENT_PORT_CEILING 1024 +#include <rdma/rdma_cma.h> #ifndef AF_INET_SDP #define AF_INET_SDP 27 @@ -35,37 +21,54 @@ #include "rpc-transport.h" #include "rdma.h" +#include "common-utils.h" + int32_t -gf_resolve_ip6 (const char *hostname, - uint16_t port, - int family, - void **dnscache, +gf_resolve_ip6 (const char *hostname, + uint16_t port, + int family, + void **dnscache, struct addrinfo **addr_info); static int32_t -af_inet_bind_to_port_lt_ceiling (int fd, struct sockaddr *sockaddr, +af_inet_bind_to_port_lt_ceiling (struct rdma_cm_id *cm_id, + struct sockaddr *sockaddr, socklen_t sockaddr_len, int ceiling) { - int32_t ret = -1; - /* struct sockaddr_in sin = {0, }; */ - uint16_t port = ceiling - 1; + int32_t ret = -1; + uint16_t port = ceiling - 1; + // by default assume none of the ports are blocked and all are available + gf_boolean_t ports[1024] = {_gf_false,}; + int i = 0; + + ret = gf_process_reserved_ports (ports); + if (ret != 0) { + for (i = 0; i < 1024; i++) + ports[i] = _gf_false; + } while (port) { switch (sockaddr->sa_family) { case AF_INET6: - ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons (port); + ((struct sockaddr_in6 *)sockaddr)->sin6_port + = htons (port); break; case AF_INET_SDP: case AF_INET: - ((struct sockaddr_in *)sockaddr)->sin_port = htons (port); + ((struct sockaddr_in *)sockaddr)->sin_port + = htons (port); break; } - - ret = bind (fd, sockaddr, sockaddr_len); + // ignore the reserved ports + if (ports[port] == _gf_true) { + port--; + continue; + } + ret = rdma_bind_addr (cm_id, sockaddr); if (ret == 0) break; @@ -79,23 +82,22 @@ af_inet_bind_to_port_lt_ceiling (int fd, struct sockaddr *sockaddr, return ret; } +#if 0 static int32_t -af_unix_client_bind (rpc_transport_t *this, - struct sockaddr *sockaddr, - socklen_t sockaddr_len, - int sock) +af_unix_client_bind (rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t sockaddr_len, struct rdma_cm_id *cm_id) { data_t *path_data = NULL; struct sockaddr_un *addr = NULL; int32_t ret = -1; - path_data = dict_get (this->options, + path_data = dict_get (this->options, "transport.rdma.bind-path"); if (path_data) { char *path = data_to_str (path_data); if (!path || strlen (path) > UNIX_PATH_MAX) { gf_log (this->name, GF_LOG_DEBUG, - "transport.rdma.bind-path not specfied " + "transport.rdma.bind-path not specified " "for unix socket, letting connect to assign " "default value"); goto err; @@ -106,7 +108,7 @@ af_unix_client_bind (rpc_transport_t *this, ret = bind (sock, (struct sockaddr *)addr, sockaddr_len); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, - "cannot bind to unix-domain socket %d (%s)", + "cannot bind to unix-domain socket %d (%s)", sock, strerror (errno)); goto err; } @@ -115,30 +117,31 @@ af_unix_client_bind (rpc_transport_t *this, err: return ret; } +#endif static int32_t client_fill_address_family (rpc_transport_t *this, struct sockaddr *sockaddr) { data_t *address_family_data = NULL; - address_family_data = dict_get (this->options, + address_family_data = dict_get (this->options, "transport.address-family"); if (!address_family_data) { data_t *remote_host_data = NULL, *connect_path_data = NULL; remote_host_data = dict_get (this->options, "remote-host"); - connect_path_data = dict_get (this->options, + connect_path_data = dict_get (this->options, "transport.rdma.connect-path"); - if (!(remote_host_data || connect_path_data) || + if (!(remote_host_data || connect_path_data) || (remote_host_data && connect_path_data)) { gf_log (this->name, GF_LOG_ERROR, "address-family not specified and not able to " "determine the same from other options " - "(remote-host:%s and connect-path:%s)", - data_to_str (remote_host_data), + "(remote-host:%s and connect-path:%s)", + data_to_str (remote_host_data), data_to_str (connect_path_data)); return -1; - } + } if (remote_host_data) { gf_log (this->name, GF_LOG_DEBUG, @@ -162,13 +165,11 @@ client_fill_address_family (rpc_transport_t *this, struct sockaddr *sockaddr) sockaddr->sa_family = AF_INET6; } else if (!strcasecmp (address_family, "inet-sdp")) { sockaddr->sa_family = AF_INET_SDP; - } else if (!strcasecmp (address_family, "inet/inet6") - || !strcasecmp (address_family, "inet6/inet")) { - sockaddr->sa_family = AF_UNSPEC; } else { gf_log (this->name, GF_LOG_ERROR, - "unknown address-family (%s) specified", + "unknown address-family (%s) specified", address_family); + sockaddr->sa_family = AF_UNSPEC; return -1; } } @@ -177,8 +178,8 @@ client_fill_address_family (rpc_transport_t *this, struct sockaddr *sockaddr) } static int32_t -af_inet_client_get_remote_sockaddr (rpc_transport_t *this, - struct sockaddr *sockaddr, +af_inet_client_get_remote_sockaddr (rpc_transport_t *this, + struct sockaddr *sockaddr, socklen_t *sockaddr_len, int16_t remote_port) { @@ -193,7 +194,7 @@ af_inet_client_get_remote_sockaddr (rpc_transport_t *this, if (remote_host_data == NULL) { gf_log (this->name, GF_LOG_ERROR, - "option remote-host missing in volume %s", + "option remote-host missing in volume %s", this->name); ret = -1; goto err; @@ -203,7 +204,7 @@ af_inet_client_get_remote_sockaddr (rpc_transport_t *this, if (remote_host == NULL) { gf_log (this->name, GF_LOG_ERROR, - "option remote-host has data NULL in volume %s", + "option remote-host has data NULL in volume %s", this->name); ret = -1; goto err; @@ -238,7 +239,7 @@ af_inet_client_get_remote_sockaddr (rpc_transport_t *this, /* TODO: gf_resolve is a blocking call. kick in some non blocking dns techniques */ ret = gf_resolve_ip6 (remote_host, remote_port, - sockaddr->sa_family, + sockaddr->sa_family, &this->dnscache, &addr_info); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, @@ -254,8 +255,8 @@ err: } static int32_t -af_unix_client_get_remote_sockaddr (rpc_transport_t *this, - struct sockaddr *sockaddr, +af_unix_client_get_remote_sockaddr (rpc_transport_t *this, + struct sockaddr *sockaddr, socklen_t *sockaddr_len) { struct sockaddr_un *sockaddr_un = NULL; @@ -263,7 +264,7 @@ af_unix_client_get_remote_sockaddr (rpc_transport_t *this, data_t *connect_path_data = NULL; int32_t ret = 0; - connect_path_data = dict_get (this->options, + connect_path_data = dict_get (this->options, "transport.rdma.connect-path"); if (!connect_path_data) { gf_log (this->name, GF_LOG_ERROR, @@ -311,7 +312,7 @@ af_unix_server_get_local_sockaddr (rpc_transport_t *this, struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; - listen_path_data = dict_get (this->options, + listen_path_data = dict_get (this->options, "transport.rdma.listen-path"); if (!listen_path_data) { gf_log (this->name, GF_LOG_ERROR, @@ -342,9 +343,9 @@ err: return ret; } -static int32_t -af_inet_server_get_local_sockaddr (rpc_transport_t *this, - struct sockaddr *addr, +static int32_t +af_inet_server_get_local_sockaddr (rpc_transport_t *this, + struct sockaddr *addr, socklen_t *addr_len) { struct addrinfo hints, *res = 0; @@ -360,31 +361,31 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, listen_host_data = dict_get (options, "transport.rdma.bind-address"); - if (listen_port_data) - { + if (listen_port_data) { listen_port = data_to_uint16 (listen_port_data); } else { - if (addr->sa_family == AF_INET6) { - struct sockaddr_in6 *in = (struct sockaddr_in6 *) addr; - in->sin6_addr = in6addr_any; - in->sin6_port = htons(listen_port); - *addr_len = sizeof(struct sockaddr_in6); + listen_port = GF_DEFAULT_RDMA_LISTEN_PORT; + + if (addr->sa_family == AF_INET6) { + struct sockaddr_in6 *in = (struct sockaddr_in6 *) addr; + in->sin6_addr = in6addr_any; + in->sin6_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in6); goto out; - } else if (addr->sa_family == AF_INET) { - struct sockaddr_in *in = (struct sockaddr_in *) addr; - in->sin_addr.s_addr = htonl(INADDR_ANY); - in->sin_port = htons(listen_port); - *addr_len = sizeof(struct sockaddr_in); + } else if (addr->sa_family == AF_INET) { + struct sockaddr_in *in = (struct sockaddr_in *) addr; + in->sin_addr.s_addr = htonl(INADDR_ANY); + in->sin_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in); goto out; - } - } + } + } if (listen_port == (uint16_t) -1) listen_port = GF_DEFAULT_RDMA_LISTEN_PORT; - if (listen_host_data) - { + if (listen_host_data) { listen_host = data_to_str (listen_host_data); } @@ -398,9 +399,8 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, ret = getaddrinfo(listen_host, service, &hints, &res); if (ret != 0) { - gf_log (this->name, - GF_LOG_ERROR, - "getaddrinfo failed for host %s, service %s (%s)", + gf_log (this->name, GF_LOG_ERROR, + "getaddrinfo failed for host %s, service %s (%s)", listen_host, service, gai_strerror (ret)); ret = -1; goto out; @@ -416,10 +416,8 @@ out: } int32_t -gf_rdma_client_bind (rpc_transport_t *this, - struct sockaddr *sockaddr, - socklen_t *sockaddr_len, - int sock) +gf_rdma_client_bind (rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t *sockaddr_len, struct rdma_cm_id *cm_id) { int ret = 0; @@ -431,22 +429,24 @@ gf_rdma_client_bind (rpc_transport_t *this, *sockaddr_len = sizeof (struct sockaddr_in); case AF_INET6: - ret = af_inet_bind_to_port_lt_ceiling (sock, sockaddr, - *sockaddr_len, - CLIENT_PORT_CEILING); + ret = af_inet_bind_to_port_lt_ceiling (cm_id, sockaddr, + *sockaddr_len, + GF_CLIENT_PORT_CEILING); if (ret == -1) { gf_log (this->name, GF_LOG_WARNING, - "cannot bind inet socket (%d) to port " - "less than %d (%s)", - sock, CLIENT_PORT_CEILING, strerror (errno)); + "cannot bind rdma_cm_id to port " + "less than %d (%s)", GF_CLIENT_PORT_CEILING, + strerror (errno)); ret = 0; } break; case AF_UNIX: *sockaddr_len = sizeof (struct sockaddr_un); - ret = af_unix_client_bind (this, (struct sockaddr *)sockaddr, +#if 0 + ret = af_unix_client_bind (this, (struct sockaddr *)sockaddr, *sockaddr_len, sock); +#endif break; default: @@ -473,7 +473,7 @@ gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this, ret = -1; goto err; } - + switch (sockaddr->sa_family) { case AF_INET_SDP: @@ -483,7 +483,7 @@ gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this, case AF_INET: case AF_INET6: case AF_UNSPEC: - ret = af_inet_client_get_remote_sockaddr (this, + ret = af_inet_client_get_remote_sockaddr (this, sockaddr, sockaddr_len, remote_port); @@ -495,8 +495,8 @@ gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this, break; case AF_UNIX: - ret = af_unix_client_get_remote_sockaddr (this, - sockaddr, + ret = af_unix_client_get_remote_sockaddr (this, + sockaddr, sockaddr_len); break; @@ -505,21 +505,21 @@ gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this, "unknown address-family %d", sockaddr->sa_family); ret = -1; } - + err: return ret; } int32_t gf_rdma_server_get_local_sockaddr (rpc_transport_t *this, - struct sockaddr *addr, - socklen_t *addr_len) + struct sockaddr *addr, + socklen_t *addr_len) { data_t *address_family_data = NULL; int32_t ret = 0; char is_inet_sdp = 0; - address_family_data = dict_get (this->options, + address_family_data = dict_get (this->options, "transport.address-family"); if (address_family_data) { char *address_family = NULL; @@ -533,21 +533,19 @@ gf_rdma_server_get_local_sockaddr (rpc_transport_t *this, addr->sa_family = AF_INET_SDP; } else if (!strcasecmp (address_family, "unix")) { addr->sa_family = AF_UNIX; - } else if (!strcasecmp (address_family, "inet/inet6") - || !strcasecmp (address_family, "inet6/inet")) { - addr->sa_family = AF_UNSPEC; } else { gf_log (this->name, GF_LOG_ERROR, - "unknown address family (%s) specified", + "unknown address family (%s) specified", address_family); + addr->sa_family = AF_UNSPEC; ret = -1; goto err; } } else { gf_log (this->name, GF_LOG_DEBUG, "option address-family not specified, defaulting " - "to inet/inet6"); - addr->sa_family = AF_UNSPEC; + "to inet"); + addr->sa_family = AF_INET; } switch (addr->sa_family) @@ -574,60 +572,53 @@ err: return ret; } -int32_t -fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *addr, +int32_t +fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *addr, int32_t addr_len, char *identifier) { int32_t ret = 0, tmpaddr_len = 0; char service[NI_MAXSERV], host[NI_MAXHOST]; - struct sockaddr_storage tmpaddr; + union gf_sock_union sock_union; - memset (&tmpaddr, 0, sizeof (tmpaddr)); - tmpaddr = *addr; + memset (&sock_union, 0, sizeof (sock_union)); + sock_union.storage = *addr; tmpaddr_len = addr_len; - if (((struct sockaddr *) &tmpaddr)->sa_family == AF_INET6) { + if (sock_union.sa.sa_family == AF_INET6) { int32_t one_to_four, four_to_eight, twelve_to_sixteen; int16_t eight_to_ten, ten_to_twelve; - + one_to_four = four_to_eight = twelve_to_sixteen = 0; eight_to_ten = ten_to_twelve = 0; - - one_to_four = ((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr.s6_addr32[0]; - four_to_eight = ((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr.s6_addr32[1]; + + one_to_four = sock_union.sin6.sin6_addr.s6_addr32[0]; + four_to_eight = sock_union.sin6.sin6_addr.s6_addr32[1]; #ifdef GF_SOLARIS_HOST_OS - eight_to_ten = S6_ADDR16(((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr)[4]; + eight_to_ten = S6_ADDR16(sock_union.sin6.sin6_addr)[4]; #else - eight_to_ten = ((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr.s6_addr16[4]; + eight_to_ten = sock_union.sin6.sin6_addr.s6_addr16[4]; #endif #ifdef GF_SOLARIS_HOST_OS - ten_to_twelve = S6_ADDR16(((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr)[5]; + ten_to_twelve = S6_ADDR16(sock_union.sin6.sin6_addr)[5]; #else - ten_to_twelve = ((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr.s6_addr16[5]; + ten_to_twelve = sock_union.sin6.sin6_addr.s6_addr16[5]; #endif - twelve_to_sixteen = ((struct sockaddr_in6 *) - &tmpaddr)->sin6_addr.s6_addr32[3]; + twelve_to_sixteen = sock_union.sin6.sin6_addr.s6_addr32[3]; /* ipv4 mapped ipv6 address has bits 0-80: 0 bits 80-96: 0xffff - bits 96-128: ipv4 address + bits 96-128: ipv4 address */ - + if (one_to_four == 0 && four_to_eight == 0 && eight_to_ten == 0 && ten_to_twelve == -1) { - struct sockaddr_in *in_ptr = (struct sockaddr_in *)&tmpaddr; - memset (&tmpaddr, 0, sizeof (tmpaddr)); - + struct sockaddr_in *in_ptr = &sock_union.sin; + memset (&sock_union, 0, sizeof (sock_union)); + in_ptr->sin_family = AF_INET; in_ptr->sin_port = ((struct sockaddr_in6 *)addr)->sin6_port; in_ptr->sin_addr.s_addr = twelve_to_sixteen; @@ -635,7 +626,7 @@ fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *add } } - ret = getnameinfo ((struct sockaddr *) &tmpaddr, + ret = getnameinfo (&sock_union.sa, tmpaddr_len, host, sizeof (host), service, sizeof (service), @@ -666,8 +657,8 @@ gf_rdma_get_transport_identifiers (rpc_transport_t *this) case AF_INET: case AF_INET6: { - ret = fill_inet6_inet_identifiers (this, - &this->myinfo.sockaddr, + ret = fill_inet6_inet_identifiers (this, + &this->myinfo.sockaddr, this->myinfo.sockaddr_len, this->myinfo.identifier); if (ret == -1) { @@ -705,7 +696,7 @@ gf_rdma_get_transport_identifiers (rpc_transport_t *this) break; default: - gf_log (this->name, GF_LOG_ERROR, + gf_log (this->name, GF_LOG_ERROR, "unknown address family (%d)", ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family); ret = -1; diff --git a/rpc/rpc-transport/rdma/src/name.h b/rpc/rpc-transport/rdma/src/name.h index adde7788e..742fc5fc3 100644 --- a/rpc/rpc-transport/rdma/src/name.h +++ b/rpc/rpc-transport/rdma/src/name.h @@ -1,35 +1,23 @@ /* - Copyright (c) 2008-2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #ifndef _IB_VERBS_NAME_H #define _IB_VERBS_NAME_H -#include <sys/socket.h> -#include <sys/un.h> +#include <rdma/rdma_cma.h> #include "compat.h" int32_t -gf_rdma_client_bind (rpc_transport_t *this, - struct sockaddr *sockaddr, - socklen_t *sockaddr_len, - int sock); +gf_rdma_client_bind (rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t *sockaddr_len, struct rdma_cm_id *cm_id); int32_t gf_rdma_client_get_remote_sockaddr (rpc_transport_t *this, diff --git a/rpc/rpc-transport/rdma/src/rdma.c b/rpc/rpc-transport/rdma/src/rdma.c index e18702ab7..6e6099a98 100644 --- a/rpc/rpc-transport/rdma/src/rdma.c +++ b/rpc/rpc-transport/rdma/src/rdma.c @@ -1,23 +1,13 @@ /* - Copyright (c) 2006-2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ - #ifndef _CONFIG_H #define _CONFIG_H #include "config.h" @@ -30,119 +20,47 @@ #include "name.h" #include "byte-order.h" #include "xlator.h" +#include "xdr-rpc.h" #include <signal.h> -#define RDMA_LOG_NAME "rpc-transport/rdma" +#define GF_RDMA_LOG_NAME "rpc-transport/rdma" static int32_t -__rdma_ioq_churn (rdma_peer_t *peer); +__gf_rdma_ioq_churn (gf_rdma_peer_t *peer); -rdma_post_t * -rdma_post_ref (rdma_post_t *post); +gf_rdma_post_t * +gf_rdma_post_ref (gf_rdma_post_t *post); int -rdma_post_unref (rdma_post_t *post); - -int32_t -gf_resolve_ip6 (const char *hostname, - uint16_t port, - int family, - void **dnscache, - struct addrinfo **addr_info); - -static uint16_t -rdma_get_local_lid (struct ibv_context *context, - int32_t port) -{ - struct ibv_port_attr attr; +gf_rdma_post_unref (gf_rdma_post_t *post); - if (ibv_query_port (context, port, &attr)) - return 0; +static void * +gf_rdma_send_completion_proc (void *data); - return attr.lid; -} +static void * +gf_rdma_recv_completion_proc (void *data); -static const char * -get_port_state_str(enum ibv_port_state pstate) -{ - switch (pstate) { - case IBV_PORT_DOWN: return "PORT_DOWN"; - case IBV_PORT_INIT: return "PORT_INIT"; - case IBV_PORT_ARMED: return "PORT_ARMED"; - case IBV_PORT_ACTIVE: return "PORT_ACTIVE"; - case IBV_PORT_ACTIVE_DEFER: return "PORT_ACTIVE_DEFER"; - default: return "invalid state"; - } -} +void * +gf_rdma_async_event_thread (void *context); static int32_t -ib_check_active_port (struct ibv_context *ctx, uint8_t port) -{ - struct ibv_port_attr port_attr; - - int32_t ret = 0; - const char *state_str = NULL; - - if (!ctx) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "Error in supplied context"); - return -1; - } - - ret = ibv_query_port (ctx, port, &port_attr); - - if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "Failed to query port %u properties", port); - return -1; - } - - state_str = get_port_state_str (port_attr.state); - gf_log (RDMA_LOG_NAME, GF_LOG_TRACE, - "Infiniband PORT: (%u) STATE: (%s)", - port, state_str); - - if (port_attr.state == IBV_PORT_ACTIVE) - return 0; - - return -1; -} +gf_rdma_create_qp (rpc_transport_t *this); static int32_t -ib_get_active_port (struct ibv_context *ib_ctx) -{ - struct ibv_device_attr ib_device_attr; - - int32_t ret = -1; - uint8_t ib_port = 0; +__gf_rdma_teardown (rpc_transport_t *this); - if (!ib_ctx) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "Error in supplied context"); - return -1; - } - if (ibv_query_device (ib_ctx, &ib_device_attr)) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "Failed to query device properties"); - return -1; - } +static int32_t +gf_rdma_teardown (rpc_transport_t *this); - for (ib_port = 1; ib_port <= ib_device_attr.phys_port_cnt; ++ib_port) { - ret = ib_check_active_port (ib_ctx, ib_port); - if (ret == 0) - return ib_port; +static int32_t +gf_rdma_disconnect (rpc_transport_t *this); - gf_log (RDMA_LOG_NAME, GF_LOG_TRACE, - "Port:(%u) not active", ib_port); - continue; - } - return ret; -} +static void +gf_rdma_cm_handle_disconnect (rpc_transport_t *this); static void -rdma_put_post (rdma_queue_t *queue, - rdma_post_t *post) +gf_rdma_put_post (gf_rdma_queue_t *queue, gf_rdma_post_t *post) { post->ctx.is_request = 0; @@ -167,16 +85,16 @@ rdma_put_post (rdma_queue_t *queue, } -static rdma_post_t * -rdma_new_post (rdma_device_t *device, int32_t len, rdma_post_type_t type) +static gf_rdma_post_t * +gf_rdma_new_post (rpc_transport_t *this, gf_rdma_device_t *device, int32_t len, + gf_rdma_post_type_t type) { - rdma_post_t *post = NULL; - int ret = -1; + gf_rdma_post_t *post = NULL; + int ret = -1; - post = (rdma_post_t *) GF_CALLOC (1, sizeof (*post), - gf_common_mt_rdma_post_t); + post = (gf_rdma_post_t *) GF_CALLOC (1, sizeof (*post), + gf_common_mt_rdma_post_t); if (post == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } @@ -186,7 +104,7 @@ rdma_new_post (rdma_device_t *device, int32_t len, rdma_post_type_t type) post->buf = valloc (len); if (!post->buf) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); + gf_log_nomem (GF_RDMA_LOG_NAME, GF_LOG_ERROR, len); goto out; } @@ -195,8 +113,9 @@ rdma_new_post (rdma_device_t *device, int32_t len, rdma_post_type_t type) post->buf_size, IBV_ACCESS_LOCAL_WRITE); if (!post->mr) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "memory registration failed"); + gf_log (this->name, GF_LOG_WARNING, + "memory registration failed (%s)", + strerror (errno)); goto out; } @@ -206,9 +125,7 @@ rdma_new_post (rdma_device_t *device, int32_t len, rdma_post_type_t type) ret = 0; out: if (ret != 0) { - if (post->buf != NULL) { - free (post->buf); - } + free (post->buf); GF_FREE (post); post = NULL; @@ -218,17 +135,17 @@ out: } -static rdma_post_t * -rdma_get_post (rdma_queue_t *queue) +static gf_rdma_post_t * +gf_rdma_get_post (gf_rdma_queue_t *queue) { - rdma_post_t *post; + gf_rdma_post_t *post = NULL; pthread_mutex_lock (&queue->lock); { post = queue->passive_posts.next; if (post == &queue->passive_posts) post = NULL; - + if (post) { if (post->prev) post->prev->next = post->next; @@ -248,7 +165,7 @@ rdma_get_post (rdma_queue_t *queue) } void -rdma_destroy_post (rdma_post_t *post) +gf_rdma_destroy_post (gf_rdma_post_t *post) { ibv_dereg_mr (post->mr); free (post->buf); @@ -257,10 +174,12 @@ rdma_destroy_post (rdma_post_t *post) static int32_t -__rdma_quota_get (rdma_peer_t *peer) +__gf_rdma_quota_get (gf_rdma_peer_t *peer) { - int32_t ret = -1; - rdma_private_t *priv = peer->trans->private; + int32_t ret = -1; + gf_rdma_private_t *priv = NULL; + + priv = peer->trans->private; if (priv->connected && peer->quota > 0) { ret = peer->quota--; @@ -269,25 +188,9 @@ __rdma_quota_get (rdma_peer_t *peer) return ret; } -/* - static int32_t - rdma_quota_get (rdma_peer_t *peer) - { - int32_t ret = -1; - rdma_private_t *priv = peer->trans->private; - - pthread_mutex_lock (&priv->write_mutex); - { - ret = __rdma_quota_get (peer); - } - pthread_mutex_unlock (&priv->write_mutex); - return ret; - } -*/ - -static void -__rdma_ioq_entry_free (rdma_ioq_t *entry) +static void +__gf_rdma_ioq_entry_free (gf_rdma_ioq_t *entry) { list_del_init (&entry->list); @@ -300,48 +203,920 @@ __rdma_ioq_entry_free (rdma_ioq_t *entry) iobref_unref (entry->msg.request.rsp_iobref); entry->msg.request.rsp_iobref = NULL; } - /* TODO: use mem-pool */ - mem_put (entry->pool, entry); + + mem_put (entry); } static void -__rdma_ioq_flush (rdma_peer_t *peer) +__gf_rdma_ioq_flush (gf_rdma_peer_t *peer) { - rdma_ioq_t *entry = NULL, *dummy = NULL; + gf_rdma_ioq_t *entry = NULL, *dummy = NULL; list_for_each_entry_safe (entry, dummy, &peer->ioq, list) { - __rdma_ioq_entry_free (entry); + __gf_rdma_ioq_entry_free (entry); } } static int32_t -__rdma_disconnect (rpc_transport_t *this) +__gf_rdma_disconnect (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - int32_t ret = 0; + gf_rdma_private_t *priv = NULL; - if (priv->connected || priv->tcp_connected) { - fcntl (priv->sock, F_SETFL, O_NONBLOCK); - if (shutdown (priv->sock, SHUT_RDWR) != 0) { - gf_log (RDMA_LOG_NAME, - GF_LOG_DEBUG, - "shutdown () - error: %s", - strerror (errno)); - ret = -errno; - priv->tcp_connected = 0; + priv = this->private; + + if (priv->connected) { + rdma_disconnect (priv->peer.cm_id); + } + + return 0; +} + + +static void +gf_rdma_queue_init (gf_rdma_queue_t *queue) +{ + pthread_mutex_init (&queue->lock, NULL); + + queue->active_posts.next = &queue->active_posts; + queue->active_posts.prev = &queue->active_posts; + queue->passive_posts.next = &queue->passive_posts; + queue->passive_posts.prev = &queue->passive_posts; +} + + +static void +__gf_rdma_destroy_queue (gf_rdma_post_t *post) +{ + gf_rdma_post_t *tmp = NULL; + + while (post->next != post) { + tmp = post->next; + + post->next = post->next->next; + post->next->prev = post; + + gf_rdma_destroy_post (tmp); + } +} + + +static void +gf_rdma_destroy_queue (gf_rdma_queue_t *queue) +{ + if (queue == NULL) { + goto out; + } + + pthread_mutex_lock (&queue->lock); + { + if (queue->passive_count > 0) { + __gf_rdma_destroy_queue (&queue->passive_posts); + queue->passive_count = 0; + } + + if (queue->active_count > 0) { + __gf_rdma_destroy_queue (&queue->active_posts); + queue->active_count = 0; + } + } + pthread_mutex_unlock (&queue->lock); + +out: + return; +} + + +static void +gf_rdma_destroy_posts (rpc_transport_t *this) +{ + gf_rdma_device_t *device = NULL; + gf_rdma_private_t *priv = NULL; + + if (this == NULL) { + goto out; + } + + priv = this->private; + device = priv->device; + + gf_rdma_destroy_queue (&device->sendq); + gf_rdma_destroy_queue (&device->recvq); + +out: + return; +} + + +static int32_t +__gf_rdma_create_posts (rpc_transport_t *this, int32_t count, int32_t size, + gf_rdma_queue_t *q, gf_rdma_post_type_t type) +{ + int32_t i = 0; + int32_t ret = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + + priv = this->private; + device = priv->device; + + for (i=0 ; i<count ; i++) { + gf_rdma_post_t *post = NULL; + + post = gf_rdma_new_post (this, device, size + 2048, type); + if (!post) { + gf_log (this->name, GF_LOG_ERROR, + "post creation failed"); + ret = -1; + break; + } + + gf_rdma_put_post (q, post); + } + return ret; +} + + +static int32_t +gf_rdma_post_recv (struct ibv_srq *srq, + gf_rdma_post_t *post) +{ + struct ibv_sge list = { + .addr = (unsigned long) post->buf, + .length = post->buf_size, + .lkey = post->mr->lkey + }; + + struct ibv_recv_wr wr = { + .wr_id = (unsigned long) post, + .sg_list = &list, + .num_sge = 1, + }, *bad_wr; + + gf_rdma_post_ref (post); + + return ibv_post_srq_recv (srq, &wr, &bad_wr); +} + + +static int32_t +gf_rdma_create_posts (rpc_transport_t *this) +{ + int32_t i = 0, ret = 0; + gf_rdma_post_t *post = NULL; + gf_rdma_private_t *priv = NULL; + gf_rdma_options_t *options = NULL; + gf_rdma_device_t *device = NULL; + + priv = this->private; + options = &priv->options; + device = priv->device; + + ret = __gf_rdma_create_posts (this, options->send_count, + options->send_size, + &device->sendq, GF_RDMA_SEND_POST); + if (!ret) + ret = __gf_rdma_create_posts (this, options->recv_count, + options->recv_size, + &device->recvq, + GF_RDMA_RECV_POST); + + if (!ret) { + for (i=0 ; i<options->recv_count ; i++) { + post = gf_rdma_get_post (&device->recvq); + if (gf_rdma_post_recv (device->srq, post) != 0) { + ret = -1; + break; + } + } + } + + if (ret) + gf_rdma_destroy_posts (this); + + return ret; +} + + +static void +gf_rdma_destroy_cq (rpc_transport_t *this) +{ + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + + priv = this->private; + device = priv->device; + + if (device->recv_cq) + ibv_destroy_cq (device->recv_cq); + device->recv_cq = NULL; + + if (device->send_cq) + ibv_destroy_cq (device->send_cq); + device->send_cq = NULL; + + return; +} + + +static int32_t +gf_rdma_create_cq (rpc_transport_t *this) +{ + gf_rdma_private_t *priv = NULL; + gf_rdma_options_t *options = NULL; + gf_rdma_device_t *device = NULL; + uint64_t send_cqe = 0; + int32_t ret = 0; + struct ibv_device_attr device_attr = {{0}, }; + + priv = this->private; + options = &priv->options; + device = priv->device; + + device->recv_cq = ibv_create_cq (priv->device->context, + options->recv_count * 2, + device, + device->recv_chan, + 0); + if (!device->recv_cq) { + gf_log (this->name, GF_LOG_ERROR, + "creation of CQ for device %s failed", + device->device_name); + ret = -1; + goto out; + } else if (ibv_req_notify_cq (device->recv_cq, 0)) { + gf_log (this->name, GF_LOG_ERROR, + "ibv_req_notify_cq on recv CQ of device %s failed", + device->device_name); + ret = -1; + goto out; + } + + do { + ret = ibv_query_device (priv->device->context, &device_attr); + if (ret != 0) { + gf_log (this->name, GF_LOG_ERROR, + "ibv_query_device on %s returned %d (%s)", + priv->device->device_name, ret, + (ret > 0) ? strerror (ret) : ""); + ret = -1; + goto out; + } + + send_cqe = options->send_count * 128; + send_cqe = (send_cqe > device_attr.max_cqe) + ? device_attr.max_cqe : send_cqe; + + /* TODO: make send_cq size dynamically adaptive */ + device->send_cq = ibv_create_cq (priv->device->context, + send_cqe, device, + device->send_chan, 0); + if (!device->send_cq) { + gf_log (this->name, GF_LOG_ERROR, + "creation of send_cq for device %s failed", + device->device_name); + ret = -1; + goto out; + } + + if (ibv_req_notify_cq (device->send_cq, 0)) { + gf_log (this->name, GF_LOG_ERROR, + "ibv_req_notify_cq on send_cq for device %s" + " failed", device->device_name); + ret = -1; + goto out; + } + } while (0); + +out: + if (ret != 0) + gf_rdma_destroy_cq (this); + + return ret; +} + + +static gf_rdma_device_t * +gf_rdma_get_device (rpc_transport_t *this, struct ibv_context *ibctx, + char *device_name) +{ + glusterfs_ctx_t *ctx = NULL; + gf_rdma_private_t *priv = NULL; + gf_rdma_options_t *options = NULL; + int32_t ret = 0; + int32_t i = 0; + gf_rdma_device_t *trav = NULL, *device = NULL; + gf_rdma_ctx_t *rdma_ctx = NULL; + + priv = this->private; + options = &priv->options; + ctx = this->ctx; + rdma_ctx = ctx->ib; + + trav = rdma_ctx->device; + + while (trav) { + if (!strcmp (trav->device_name, device_name)) + break; + trav = trav->next; + } + + if (!trav) { + trav = GF_CALLOC (1, sizeof (*trav), + gf_common_mt_rdma_device_t); + if (trav == NULL) { + goto out; + } + + priv->device = trav; + trav->context = ibctx; + + trav->request_ctx_pool + = mem_pool_new (gf_rdma_request_context_t, + GF_RDMA_POOL_SIZE); + if (trav->request_ctx_pool == NULL) { + goto out; + } + + trav->ioq_pool + = mem_pool_new (gf_rdma_ioq_t, GF_RDMA_POOL_SIZE); + if (trav->ioq_pool == NULL) { + goto out; + } + + trav->reply_info_pool = mem_pool_new (gf_rdma_reply_info_t, + GF_RDMA_POOL_SIZE); + if (trav->reply_info_pool == NULL) { + goto out; + } + + trav->device_name = gf_strdup (device_name); + + trav->next = rdma_ctx->device; + rdma_ctx->device = trav; + + trav->send_chan = ibv_create_comp_channel (trav->context); + if (!trav->send_chan) { + gf_log (this->name, GF_LOG_ERROR, + "could not create send completion channel for " + "device (%s)", device_name); + goto out; + } + + trav->recv_chan = ibv_create_comp_channel (trav->context); + if (!trav->recv_chan) { + gf_log (this->name, GF_LOG_ERROR, + "could not create recv completion channel for " + "device (%s)", device_name); + + /* TODO: cleanup current mess */ + goto out; + } + + if (gf_rdma_create_cq (this) < 0) { + gf_log (this->name, GF_LOG_ERROR, + "could not create CQ for device (%s)", + device_name); + goto out; + } + + /* protection domain */ + trav->pd = ibv_alloc_pd (trav->context); + + if (!trav->pd) { + gf_log (this->name, GF_LOG_ERROR, + "could not allocate protection domain for " + "device (%s)", device_name); + goto out; + } + + struct ibv_srq_init_attr attr = { + .attr = { + .max_wr = options->recv_count, + .max_sge = 1, + .srq_limit = 10 + } + }; + trav->srq = ibv_create_srq (trav->pd, &attr); + + if (!trav->srq) { + gf_log (this->name, GF_LOG_ERROR, + "could not create SRQ for device (%s)", + device_name); + goto out; + } + + /* queue init */ + gf_rdma_queue_init (&trav->sendq); + gf_rdma_queue_init (&trav->recvq); + + if (gf_rdma_create_posts (this) < 0) { + gf_log (this->name, GF_LOG_ERROR, + "could not allocate posts for device (%s)", + device_name); + goto out; + } + + /* completion threads */ + ret = gf_thread_create (&trav->send_thread, NULL, + gf_rdma_send_completion_proc, + trav->send_chan); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "could not create send completion thread for " + "device (%s)", device_name); + goto out; + } + + ret = gf_thread_create (&trav->recv_thread, NULL, + gf_rdma_recv_completion_proc, + trav->recv_chan); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "could not create recv completion thread " + "for device (%s)", device_name); + return NULL; + } + + ret = gf_thread_create (&trav->async_event_thread, NULL, + gf_rdma_async_event_thread, + ibctx); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "could not create async_event_thread"); + return NULL; + } + + /* qpreg */ + pthread_mutex_init (&trav->qpreg.lock, NULL); + for (i=0; i<42; i++) { + trav->qpreg.ents[i].next = &trav->qpreg.ents[i]; + trav->qpreg.ents[i].prev = &trav->qpreg.ents[i]; + } + } + + device = trav; + trav = NULL; +out: + + if (trav != NULL) { + gf_rdma_destroy_posts (this); + mem_pool_destroy (trav->ioq_pool); + mem_pool_destroy (trav->request_ctx_pool); + mem_pool_destroy (trav->reply_info_pool); + ibv_dealloc_pd (trav->pd); + gf_rdma_destroy_cq (this); + ibv_destroy_comp_channel (trav->recv_chan); + ibv_destroy_comp_channel (trav->send_chan); + GF_FREE ((char *)trav->device_name); + GF_FREE (trav); + } + + return device; +} + + +static rpc_transport_t * +gf_rdma_transport_new (rpc_transport_t *listener, struct rdma_cm_id *cm_id) +{ + gf_rdma_private_t *listener_priv = NULL, *priv = NULL; + rpc_transport_t *this = NULL, *new = NULL; + gf_rdma_options_t *options = NULL; + char *device_name = NULL; + + listener_priv = listener->private; + + this = GF_CALLOC (1, sizeof (rpc_transport_t), + gf_common_mt_rpc_transport_t); + if (this == NULL) { + goto out; + } + + this->listener = listener; + + priv = GF_CALLOC (1, sizeof (gf_rdma_private_t), + gf_common_mt_rdma_private_t); + if (priv == NULL) { + goto out; + } + + this->private = priv; + priv->options = listener_priv->options; + + priv->listener = listener; + priv->entity = GF_RDMA_SERVER; + + options = &priv->options; + + this->ops = listener->ops; + this->init = listener->init; + this->fini = listener->fini; + this->ctx = listener->ctx; + this->name = gf_strdup (listener->name); + this->notify = listener->notify; + this->mydata = listener->mydata; + + this->myinfo.sockaddr_len = sizeof (cm_id->route.addr.src_addr); + memcpy (&this->myinfo.sockaddr, &cm_id->route.addr.src_addr, + this->myinfo.sockaddr_len); + + this->peerinfo.sockaddr_len = sizeof (cm_id->route.addr.dst_addr); + memcpy (&this->peerinfo.sockaddr, &cm_id->route.addr.dst_addr, + this->peerinfo.sockaddr_len); + + priv->peer.trans = this; + gf_rdma_get_transport_identifiers (this); + + device_name = (char *)ibv_get_device_name (cm_id->verbs->device); + if (device_name == NULL) { + gf_log (listener->name, GF_LOG_WARNING, + "cannot get device name (peer:%s me:%s)", + this->peerinfo.identifier, this->myinfo.identifier); + goto out; + } + + priv->device = gf_rdma_get_device (this, cm_id->verbs, + device_name); + if (priv->device == NULL) { + gf_log (listener->name, GF_LOG_WARNING, + "cannot get infiniband device %s (peer:%s me:%s)", + device_name, this->peerinfo.identifier, + this->myinfo.identifier); + goto out; + } + + priv->peer.send_count = options->send_count; + priv->peer.recv_count = options->recv_count; + priv->peer.send_size = options->send_size; + priv->peer.recv_size = options->recv_size; + priv->peer.cm_id = cm_id; + INIT_LIST_HEAD (&priv->peer.ioq); + + pthread_mutex_init (&priv->write_mutex, NULL); + pthread_mutex_init (&priv->recv_mutex, NULL); + + cm_id->context = this; + + new = rpc_transport_ref (this); + this = NULL; +out: + if (this != NULL) { + if (this->private != NULL) { + GF_FREE (this->private); + } + + if (this->name != NULL) { + GF_FREE (this->name); + } + + GF_FREE (this); + } + + return new; +} + + +static int +gf_rdma_cm_handle_connect_request (struct rdma_cm_event *event) +{ + int ret = -1; + rpc_transport_t *this = NULL, *listener = NULL; + struct rdma_cm_id *child_cm_id = NULL, *listener_cm_id = NULL; + struct rdma_conn_param conn_param = {0, }; + gf_rdma_private_t *priv = NULL; + gf_rdma_options_t *options = NULL; + + child_cm_id = event->id; + listener_cm_id = event->listen_id; + + listener = listener_cm_id->context; + priv = listener->private; + options = &priv->options; + + this = gf_rdma_transport_new (listener, child_cm_id); + if (this == NULL) { + gf_log (listener->name, GF_LOG_WARNING, + "could not create a transport for incoming connection" + " (me.name:%s me.identifier:%s)", listener->name, + listener->myinfo.identifier); + rdma_destroy_id (child_cm_id); + goto out; + } + + gf_log (listener->name, GF_LOG_TRACE, + "got a connect request (me:%s peer:%s)", + listener->myinfo.identifier, this->peerinfo.identifier); + + ret = gf_rdma_create_qp (this); + if (ret < 0) { + gf_log (listener->name, GF_LOG_WARNING, + "could not create QP (peer:%s me:%s)", + this->peerinfo.identifier, this->myinfo.identifier); + gf_rdma_cm_handle_disconnect (this); + goto out; + } + + conn_param.responder_resources = 1; + conn_param.initiator_depth = 1; + conn_param.retry_count = options->attr_retry_cnt; + conn_param.rnr_retry_count = options->attr_rnr_retry; + + ret = rdma_accept(child_cm_id, &conn_param); + if (ret < 0) { + gf_log (listener->name, GF_LOG_WARNING, "rdma_accept failed " + "peer:%s me:%s (%s)", this->peerinfo.identifier, + this->myinfo.identifier, strerror (errno)); + gf_rdma_cm_handle_disconnect (this); + goto out; + } + + ret = 0; + +out: + return ret; +} + + +static int +gf_rdma_cm_handle_route_resolved (struct rdma_cm_event *event) +{ + struct rdma_conn_param conn_param = {0, }; + int ret = 0; + rpc_transport_t *this = NULL; + gf_rdma_private_t *priv = NULL; + gf_rdma_peer_t *peer = NULL; + gf_rdma_options_t *options = NULL; + + if (event == NULL) { + goto out; + } + + this = event->id->context; + + priv = this->private; + peer = &priv->peer; + options = &priv->options; + + ret = gf_rdma_create_qp (this); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "could not create QP (peer:%s me:%s)", + this->peerinfo.identifier, this->myinfo.identifier); + gf_rdma_cm_handle_disconnect (this); + goto out; + } + + memset(&conn_param, 0, sizeof conn_param); + conn_param.responder_resources = 1; + conn_param.initiator_depth = 1; + conn_param.retry_count = options->attr_retry_cnt; + conn_param.rnr_retry_count = options->attr_rnr_retry; + + ret = rdma_connect(peer->cm_id, &conn_param); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "rdma_connect failed (%s)", strerror (errno)); + gf_rdma_cm_handle_disconnect (this); + goto out; + } + + gf_log (this->name, GF_LOG_TRACE, "route resolved (me:%s peer:%s)", + this->myinfo.identifier, this->peerinfo.identifier); + + ret = 0; +out: + return ret; +} + + +static int +gf_rdma_cm_handle_addr_resolved (struct rdma_cm_event *event) +{ + rpc_transport_t *this = NULL; + gf_rdma_peer_t *peer = NULL; + gf_rdma_private_t *priv = NULL; + int ret = 0; + + this = event->id->context; + + priv = this->private; + peer = &priv->peer; + + GF_ASSERT (peer->cm_id == event->id); + + this->myinfo.sockaddr_len = sizeof (peer->cm_id->route.addr.src_addr); + memcpy (&this->myinfo.sockaddr, &peer->cm_id->route.addr.src_addr, + this->myinfo.sockaddr_len); + + this->peerinfo.sockaddr_len = sizeof (peer->cm_id->route.addr.dst_addr); + memcpy (&this->peerinfo.sockaddr, &peer->cm_id->route.addr.dst_addr, + this->peerinfo.sockaddr_len); + + gf_rdma_get_transport_identifiers (this); + + ret = rdma_resolve_route(peer->cm_id, 2000); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "rdma_resolve_route failed (me:%s peer:%s) (%s)", + this->myinfo.identifier, this->peerinfo.identifier, + strerror (errno)); + gf_rdma_cm_handle_disconnect (this); + } + + gf_log (this->name, GF_LOG_TRACE, "Address resolved (me:%s peer:%s)", + this->myinfo.identifier, this->peerinfo.identifier); + + return ret; +} + + +static void +gf_rdma_cm_handle_disconnect (rpc_transport_t *this) +{ + gf_rdma_private_t *priv = NULL; + char need_unref = 0, connected = 0; + + priv = this->private; + gf_log (this->name, GF_LOG_DEBUG, + "peer disconnected, cleaning up"); + + pthread_mutex_lock (&priv->write_mutex); + { + if (priv->peer.cm_id != NULL) { + need_unref = 1; + connected = priv->connected; + priv->connected = 0; } + + __gf_rdma_teardown (this); } - + pthread_mutex_unlock (&priv->write_mutex); + + if (connected) { + rpc_transport_notify (this, RPC_TRANSPORT_DISCONNECT, this); + } + + if (need_unref) + rpc_transport_unref (this); + +} + + +static int +gf_rdma_cm_handle_event_established (struct rdma_cm_event *event) +{ + rpc_transport_t *this = NULL; + gf_rdma_private_t *priv = NULL; + struct rdma_cm_id *cm_id = NULL; + int ret = 0; + + cm_id = event->id; + this = cm_id->context; + priv = this->private; + + priv->connected = 1; + + pthread_mutex_lock (&priv->write_mutex); + { + priv->peer.quota = 1; + priv->peer.quota_set = 0; + } + pthread_mutex_unlock (&priv->write_mutex); + + if (priv->entity == GF_RDMA_CLIENT) { + ret = rpc_transport_notify (this, RPC_TRANSPORT_CONNECT, this); + + } else if (priv->entity == GF_RDMA_SERVER) { + ret = rpc_transport_notify (priv->listener, + RPC_TRANSPORT_ACCEPT, this); + } + + if (ret < 0) { + gf_rdma_disconnect (this); + } + + gf_log (this->name, GF_LOG_TRACE, + "recieved event RDMA_CM_EVENT_ESTABLISHED (me:%s peer:%s)", + this->myinfo.identifier, this->peerinfo.identifier); + return ret; } +static int +gf_rdma_cm_handle_event_error (rpc_transport_t *this) +{ + gf_rdma_private_t *priv = NULL; + + priv = this->private; + + if (priv->entity != GF_RDMA_SERVER_LISTENER) { + gf_rdma_cm_handle_disconnect (this); + } + + return 0; +} + + +static int +gf_rdma_cm_handle_device_removal (struct rdma_cm_event *event) +{ + return 0; +} + + +static void * +gf_rdma_cm_event_handler (void *data) +{ + struct rdma_cm_event *event = NULL; + int ret = 0; + rpc_transport_t *this = NULL; + struct rdma_event_channel *event_channel = NULL; + + event_channel = data; + + while (1) { + ret = rdma_get_cm_event (event_channel, &event); + if (ret != 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma_cm_get_event failed (%s)", + strerror (errno)); + break; + } + + switch (event->event) { + case RDMA_CM_EVENT_ADDR_RESOLVED: + gf_rdma_cm_handle_addr_resolved (event); + break; + + case RDMA_CM_EVENT_ROUTE_RESOLVED: + gf_rdma_cm_handle_route_resolved (event); + break; + + case RDMA_CM_EVENT_CONNECT_REQUEST: + gf_rdma_cm_handle_connect_request (event); + break; + + case RDMA_CM_EVENT_ESTABLISHED: + gf_rdma_cm_handle_event_established (event); + break; + + case RDMA_CM_EVENT_ADDR_ERROR: + case RDMA_CM_EVENT_ROUTE_ERROR: + case RDMA_CM_EVENT_CONNECT_ERROR: + case RDMA_CM_EVENT_UNREACHABLE: + case RDMA_CM_EVENT_REJECTED: + this = event->id->context; + + gf_log (this->name, GF_LOG_WARNING, + "cma event %s, error %d (me:%s peer:%s)\n", + rdma_event_str(event->event), event->status, + this->myinfo.identifier, + this->peerinfo.identifier); + + rdma_ack_cm_event (event); + event = NULL; + + gf_rdma_cm_handle_event_error (this); + continue; + + case RDMA_CM_EVENT_DISCONNECTED: + this = event->id->context; + + gf_log (this->name, GF_LOG_DEBUG, + "recieved disconnect (me:%s peer:%s)\n", + this->myinfo.identifier, + this->peerinfo.identifier); + + rdma_ack_cm_event (event); + event = NULL; + + gf_rdma_cm_handle_disconnect (this); + continue; + + case RDMA_CM_EVENT_DEVICE_REMOVAL: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "device removed"); + gf_rdma_cm_handle_device_removal (event); + break; + + default: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "unhandled event: %s, ignoring", + rdma_event_str(event->event)); + break; + } + + rdma_ack_cm_event (event); + } + + return NULL; +} + + static int32_t -rdma_post_send (struct ibv_qp *qp, - rdma_post_t *post, - int32_t len) +gf_rdma_post_send (struct ibv_qp *qp, gf_rdma_post_t *post, int32_t len) { struct ibv_sge list = { .addr = (unsigned long) post->buf, @@ -364,61 +1139,64 @@ rdma_post_send (struct ibv_qp *qp, } int -__rdma_encode_error(rdma_peer_t *peer, rdma_reply_info_t *reply_info, - struct iovec *rpchdr, uint32_t *ptr, - rdma_errcode_t err) +__gf_rdma_encode_error(gf_rdma_peer_t *peer, gf_rdma_reply_info_t *reply_info, + struct iovec *rpchdr, gf_rdma_header_t *hdr, + gf_rdma_errcode_t err) { - uint32_t *startp = NULL; struct rpc_msg *rpc_msg = NULL; - startp = ptr; if (reply_info != NULL) { - *ptr++ = hton32(reply_info->rm_xid); + hdr->rm_xid = hton32(reply_info->rm_xid); } else { rpc_msg = rpchdr[0].iov_base; /* assume rpchdr contains * only one vector. * (which is true) */ - *ptr++ = rpc_msg->rm_xid; + hdr->rm_xid = rpc_msg->rm_xid; } - *ptr++ = hton32(RDMA_VERSION); - *ptr++ = hton32(peer->send_count); - *ptr++ = hton32(RDMA_ERROR); - *ptr++ = hton32(err); - if (err == ERR_VERS) { - *ptr++ = hton32(RDMA_VERSION); - *ptr++ = hton32(RDMA_VERSION); - } + hdr->rm_vers = hton32(GF_RDMA_VERSION); + hdr->rm_credit = hton32(peer->send_count); + hdr->rm_type = hton32(GF_RDMA_ERROR); + hdr->rm_body.rm_error.rm_type = hton32(err); + if (err == ERR_VERS) { + hdr->rm_body.rm_error.rm_version.gf_rdma_vers_low + = hton32(GF_RDMA_VERSION); + hdr->rm_body.rm_error.rm_version.gf_rdma_vers_high + = hton32(GF_RDMA_VERSION); + } - return (int)((unsigned long)ptr - (unsigned long)startp); + return sizeof (*hdr); } int32_t -__rdma_send_error (rdma_peer_t *peer, rdma_ioq_t *entry, rdma_post_t *post, - rdma_reply_info_t *reply_info, rdma_errcode_t err) +__gf_rdma_send_error (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post, gf_rdma_reply_info_t *reply_info, + gf_rdma_errcode_t err) { - int32_t ret = -1, len; + int32_t ret = -1, len = 0; - len = __rdma_encode_error (peer, reply_info, entry->rpchdr, - (uint32_t *)post->buf, err); + len = __gf_rdma_encode_error (peer, reply_info, entry->rpchdr, + (gf_rdma_header_t *)post->buf, err); if (len == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, + "encode error returned -1"); goto out; } - rdma_post_ref (post); + gf_rdma_post_ref (post); - ret = rdma_post_send (peer->qp, post, len); + ret = gf_rdma_post_send (peer->qp, post, len); if (!ret) { ret = len; } else { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "rdma_post_send (to %s) failed with ret = %d (%s)", + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "gf_rdma_post_send (to %s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); - rdma_post_unref (post); - __rdma_disconnect (peer->trans); + gf_rdma_post_unref (post); + __gf_rdma_disconnect (peer->trans); ret = -1; } @@ -428,23 +1206,24 @@ out: int32_t -__rdma_create_read_chunks_from_vector (rdma_peer_t *peer, - rdma_read_chunk_t **readch_ptr, - int32_t *pos, struct iovec *vector, - int count, - rdma_request_context_t *request_ctx) +__gf_rdma_create_read_chunks_from_vector (gf_rdma_peer_t *peer, + gf_rdma_read_chunk_t **readch_ptr, + int32_t *pos, struct iovec *vector, + int count, + gf_rdma_request_context_t *request_ctx) { - int i = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - struct ibv_mr *mr = NULL; - rdma_read_chunk_t *readch = NULL; - int32_t ret = -1; - - if ((peer == NULL) || (readch_ptr == NULL) || (*readch_ptr == NULL) - || (request_ctx == NULL) || (vector == NULL)) { - goto out; - } + int i = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + struct ibv_mr *mr = NULL; + gf_rdma_read_chunk_t *readch = NULL; + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, readch_ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, *readch_ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, request_ctx, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, vector, out); priv = peer->trans->private; device = priv->device; @@ -458,8 +1237,10 @@ __rdma_create_read_chunks_from_vector (rdma_peer_t *peer, vector[i].iov_len, IBV_ACCESS_REMOTE_READ); if (!mr) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "memory registration failed"); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "memory registration failed (%s) (peer:%s)", + strerror (errno), + peer->trans->peerinfo.identifier); goto out; } @@ -484,76 +1265,72 @@ out: int32_t -__rdma_create_read_chunks (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_chunktype_t type, uint32_t **ptr, - rdma_request_context_t *request_ctx) +__gf_rdma_create_read_chunks (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_chunktype_t type, uint32_t **ptr, + gf_rdma_request_context_t *request_ctx) { int32_t ret = -1; - rdma_device_t *device = NULL; - rdma_private_t *priv = NULL; int pos = 0; - if ((peer == NULL) || (entry == NULL) || (ptr == NULL) - || (*ptr == NULL) || (request_ctx == NULL)) { - goto out; - } - - priv = peer->trans->private; - device = priv->device; + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, entry, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, *ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, request_ctx, out); request_ctx->iobref = iobref_ref (entry->iobref); - if (type == rdma_areadch) { + if (type == gf_rdma_areadch) { pos = 0; - ret = __rdma_create_read_chunks_from_vector (peer, - (rdma_read_chunk_t **)ptr, - &pos, - entry->rpchdr, - entry->rpchdr_count, - request_ctx); + ret = __gf_rdma_create_read_chunks_from_vector (peer, + (gf_rdma_read_chunk_t **)ptr, + &pos, + entry->rpchdr, + entry->rpchdr_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "cannot create read chunks from vector, " + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot create read chunks from vector " "entry->rpchdr"); goto out; } - ret = __rdma_create_read_chunks_from_vector (peer, - (rdma_read_chunk_t **)ptr, - &pos, - entry->proghdr, - entry->proghdr_count, - request_ctx); + ret = __gf_rdma_create_read_chunks_from_vector (peer, + (gf_rdma_read_chunk_t **)ptr, + &pos, + entry->proghdr, + entry->proghdr_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "cannot create read chunks from vector, " + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot create read chunks from vector " "entry->proghdr"); } if (entry->prog_payload_count != 0) { - ret = __rdma_create_read_chunks_from_vector (peer, - (rdma_read_chunk_t **)ptr, - &pos, - entry->prog_payload, - entry->prog_payload_count, - request_ctx); + ret = __gf_rdma_create_read_chunks_from_vector (peer, + (gf_rdma_read_chunk_t **)ptr, + &pos, + entry->prog_payload, + entry->prog_payload_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "cannot create read chunks from vector," + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot create read chunks from vector" " entry->prog_payload"); } } } else { pos = iov_length (entry->rpchdr, entry->rpchdr_count); - ret = __rdma_create_read_chunks_from_vector (peer, - (rdma_read_chunk_t **)ptr, - &pos, - entry->prog_payload, - entry->prog_payload_count, - request_ctx); + ret = __gf_rdma_create_read_chunks_from_vector (peer, + (gf_rdma_read_chunk_t **)ptr, + &pos, + entry->prog_payload, + entry->prog_payload_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "cannot create read chunks from vector, " + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot create read chunks from vector " "entry->prog_payload"); } } @@ -567,36 +1344,39 @@ out: int32_t -__rdma_create_write_chunks_from_vector (rdma_peer_t *peer, - rdma_write_chunk_t **writech_ptr, - struct iovec *vector, int count, - rdma_request_context_t *request_ctx) +__gf_rdma_create_write_chunks_from_vector (gf_rdma_peer_t *peer, + gf_rdma_write_chunk_t **writech_ptr, + struct iovec *vector, int count, + gf_rdma_request_context_t *request_ctx) { - int i = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - struct ibv_mr *mr = NULL; - rdma_write_chunk_t *writech = NULL; - int32_t ret = -1; - - if ((peer == NULL) || (writech_ptr == NULL) || (*writech_ptr == NULL) - || (request_ctx == NULL) || (vector == NULL)) { - goto out; - } + int i = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + struct ibv_mr *mr = NULL; + gf_rdma_write_chunk_t *writech = NULL; + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, writech_ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, *writech_ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, request_ctx, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, vector, out); writech = *writech_ptr; priv = peer->trans->private; device = priv->device; - + for (i = 0; i < count; i++) { mr = ibv_reg_mr (device->pd, vector[i].iov_base, vector[i].iov_len, IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_LOCAL_WRITE); if (!mr) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "memory registration failed"); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "memory registration failed (%s) (peer:%s)", + strerror (errno), + peer->trans->peerinfo.identifier); goto out; } @@ -619,22 +1399,23 @@ out: int32_t -__rdma_create_write_chunks (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_chunktype_t chunk_type, uint32_t **ptr, - rdma_request_context_t *request_ctx) +__gf_rdma_create_write_chunks (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_chunktype_t chunk_type, uint32_t **ptr, + gf_rdma_request_context_t *request_ctx) { - int32_t ret = -1; - rdma_write_array_t *warray = NULL; + int32_t ret = -1; + gf_rdma_write_array_t *warray = NULL; - if ((peer == NULL) || (entry == NULL) || (ptr == NULL) - || (*ptr == NULL) || (request_ctx == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, *ptr, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, request_ctx, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, entry, out); - if ((chunk_type == rdma_replych) + if ((chunk_type == gf_rdma_replych) && ((entry->msg.request.rsphdr_count != 1) || (entry->msg.request.rsphdr_vec[0].iov_base == NULL))) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, (entry->msg.request.rsphdr_count == 1) ? "chunktype specified as reply chunk but the vector " "specifying the buffer to be used for holding reply" @@ -645,32 +1426,32 @@ __rdma_create_write_chunks (rdma_peer_t *peer, rdma_ioq_t *entry, } /* - if ((chunk_type == rdma_writech) - && ((entry->msg.request.rsphdr_count == 0) - || (entry->msg.request.rsphdr_vec[0].iov_base == NULL))) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "vector specifying buffer to hold the program's reply " - "header should also be provided when buffers are " - "provided for holding the program's payload in reply"); - goto out; - } + if ((chunk_type == gf_rdma_writech) + && ((entry->msg.request.rsphdr_count == 0) + || (entry->msg.request.rsphdr_vec[0].iov_base == NULL))) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, + "vector specifying buffer to hold the program's reply " + "header should also be provided when buffers are " + "provided for holding the program's payload in reply"); + goto out; + } */ - if (chunk_type == rdma_writech) { - warray = (rdma_write_array_t *)*ptr; + if (chunk_type == gf_rdma_writech) { + warray = (gf_rdma_write_array_t *)*ptr; warray->wc_discrim = hton32 (1); warray->wc_nchunks = hton32 (entry->msg.request.rsp_payload_count); *ptr = (uint32_t *)&warray->wc_array[0]; - ret = __rdma_create_write_chunks_from_vector (peer, - (rdma_write_chunk_t **)ptr, - entry->msg.request.rsp_payload, - entry->msg.request.rsp_payload_count, - request_ctx); + ret = __gf_rdma_create_write_chunks_from_vector (peer, + (gf_rdma_write_chunk_t **)ptr, + entry->msg.request.rsp_payload, + entry->msg.request.rsp_payload_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "cannot create write chunks from vector " "entry->rpc_payload"); goto out; @@ -684,23 +1465,23 @@ __rdma_create_write_chunks (rdma_peer_t *peer, rdma_ioq_t *entry, **ptr = 0; *ptr = *ptr + 1; } else { - /* no write chunklist */ + /* no write chunklist */ **ptr = 0; *ptr = *ptr + 1; - warray = (rdma_write_array_t *)*ptr; + warray = (gf_rdma_write_array_t *)*ptr; warray->wc_discrim = hton32 (1); warray->wc_nchunks = hton32 (entry->msg.request.rsphdr_count); *ptr = (uint32_t *)&warray->wc_array[0]; - - ret = __rdma_create_write_chunks_from_vector (peer, - (rdma_write_chunk_t **)ptr, - entry->msg.request.rsphdr_vec, - entry->msg.request.rsphdr_count, - request_ctx); + + ret = __gf_rdma_create_write_chunks_from_vector (peer, + (gf_rdma_write_chunk_t **)ptr, + entry->msg.request.rsphdr_vec, + entry->msg.request.rsphdr_count, + request_ctx); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "cannot create write chunks from vector " "entry->rpchdr"); goto out; @@ -716,8 +1497,8 @@ out: } -inline void -__rdma_deregister_mr (struct ibv_mr **mr, int count) +static inline void +__gf_rdma_deregister_mr (struct ibv_mr **mr, int count) { int i = 0; @@ -735,15 +1516,15 @@ out: static int32_t -__rdma_quota_put (rdma_peer_t *peer) +__gf_rdma_quota_put (gf_rdma_peer_t *peer) { - int32_t ret; + int32_t ret = 0; peer->quota++; ret = peer->quota; if (!list_empty (&peer->ioq)) { - ret = __rdma_ioq_churn (peer); + ret = __gf_rdma_ioq_churn (peer); } return ret; @@ -751,14 +1532,15 @@ __rdma_quota_put (rdma_peer_t *peer) static int32_t -rdma_quota_put (rdma_peer_t *peer) +gf_rdma_quota_put (gf_rdma_peer_t *peer) { - int32_t ret; - rdma_private_t *priv = peer->trans->private; + int32_t ret = 0; + gf_rdma_private_t *priv = NULL; + priv = peer->trans->private; pthread_mutex_lock (&priv->write_mutex); { - ret = __rdma_quota_put (peer); + ret = __gf_rdma_quota_put (peer); } pthread_mutex_unlock (&priv->write_mutex); @@ -768,11 +1550,11 @@ rdma_quota_put (rdma_peer_t *peer) /* to be called with priv->mutex held */ void -__rdma_request_context_destroy (rdma_request_context_t *context) +__gf_rdma_request_context_destroy (gf_rdma_request_context_t *context) { - rdma_peer_t *peer = NULL; - rdma_private_t *priv = NULL; - int32_t ret = 0; + gf_rdma_peer_t *peer = NULL; + gf_rdma_private_t *priv = NULL; + int32_t ret = 0; if (context == NULL) { goto out; @@ -780,18 +1562,18 @@ __rdma_request_context_destroy (rdma_request_context_t *context) peer = context->peer; - __rdma_deregister_mr (context->mr, context->mr_count); + __gf_rdma_deregister_mr (context->mr, context->mr_count); priv = peer->trans->private; if (priv->connected) { - ret = __rdma_quota_put (peer); + ret = __gf_rdma_quota_put (peer); if (ret < 0) { gf_log ("rdma", GF_LOG_DEBUG, "failed to send " "message"); - mem_put (context->pool, context); - __rdma_disconnect (peer->trans); + mem_put (context); + __gf_rdma_disconnect (peer->trans); goto out; } } @@ -806,22 +1588,21 @@ __rdma_request_context_destroy (rdma_request_context_t *context) context->rsp_iobref = NULL; } - mem_put (context->pool, context); + mem_put (context); out: return; } - void -rdma_post_context_destroy (rdma_post_context_t *ctx) +gf_rdma_post_context_destroy (gf_rdma_post_context_t *ctx) { if (ctx == NULL) { goto out; } - __rdma_deregister_mr (ctx->mr, ctx->mr_count); + __gf_rdma_deregister_mr (ctx->mr, ctx->mr_count); if (ctx->iobref != NULL) { iobref_unref (ctx->iobref); @@ -837,30 +1618,8 @@ out: } -static int32_t -rdma_post_recv (struct ibv_srq *srq, - rdma_post_t *post) -{ - struct ibv_sge list = { - .addr = (unsigned long) post->buf, - .length = post->buf_size, - .lkey = post->mr->lkey - }; - - struct ibv_recv_wr wr = { - .wr_id = (unsigned long) post, - .sg_list = &list, - .num_sge = 1, - }, *bad_wr; - - rdma_post_ref (post); - - return ibv_post_srq_recv (srq, &wr, &bad_wr); -} - - int -rdma_post_unref (rdma_post_t *post) +gf_rdma_post_unref (gf_rdma_post_t *post) { int refcount = -1; @@ -875,11 +1634,11 @@ rdma_post_unref (rdma_post_t *post) pthread_mutex_unlock (&post->lock); if (refcount == 0) { - rdma_post_context_destroy (&post->ctx); - if (post->type == RDMA_SEND_POST) { - rdma_put_post (&post->device->sendq, post); + gf_rdma_post_context_destroy (&post->ctx); + if (post->type == GF_RDMA_SEND_POST) { + gf_rdma_put_post (&post->device->sendq, post); } else { - rdma_post_recv (post->device->srq, post); + gf_rdma_post_recv (post->device->srq, post); } } out: @@ -888,7 +1647,7 @@ out: int -rdma_post_get_refcount (rdma_post_t *post) +gf_rdma_post_get_refcount (gf_rdma_post_t *post) { int refcount = -1; @@ -906,8 +1665,8 @@ out: return refcount; } -rdma_post_t * -rdma_post_ref (rdma_post_t *post) +gf_rdma_post_t * +gf_rdma_post_ref (gf_rdma_post_t *post) { if (post == NULL) { goto out; @@ -918,38 +1677,39 @@ rdma_post_ref (rdma_post_t *post) post->refcount++; } pthread_mutex_unlock (&post->lock); - + out: return post; } int32_t -__rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_post_t *post) +__gf_rdma_ioq_churn_request (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post) { - rdma_chunktype_t rtype = rdma_noch, wtype = rdma_noch; - uint64_t send_size = 0; - rdma_header_t *hdr = NULL; - struct rpc_msg *rpc_msg = NULL; - uint32_t *chunkptr = NULL; - char *buf = NULL; - int32_t ret = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - int chunk_count = 0; - rdma_request_context_t *request_ctx = NULL; - uint32_t prog_payload_length = 0, len = 0; - struct rpc_req *rpc_req = NULL; - - if ((peer == NULL) || (entry == NULL) || (post == NULL)) { - goto out; - } + gf_rdma_chunktype_t rtype = gf_rdma_noch; + gf_rdma_chunktype_t wtype = gf_rdma_noch; + uint64_t send_size = 0; + gf_rdma_header_t *hdr = NULL; + struct rpc_msg *rpc_msg = NULL; + uint32_t *chunkptr = NULL; + char *buf = NULL; + int32_t ret = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + int chunk_count = 0; + gf_rdma_request_context_t *request_ctx = NULL; + uint32_t prog_payload_length = 0, len = 0; + struct rpc_req *rpc_req = NULL; + + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, entry, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, post, out); if ((entry->msg.request.rsphdr_count != 0) && (entry->msg.request.rsp_payload_count != 0)) { ret = -1; - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "both write-chunklist and reply-chunk cannot be " "present"); goto out; @@ -959,7 +1719,7 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, priv = peer->trans->private; device = priv->device; - hdr = (rdma_header_t *)post->buf; + hdr = (gf_rdma_header_t *)post->buf; send_size = iov_length (entry->rpchdr, entry->rpchdr_count) + iov_length (entry->proghdr, entry->proghdr_count) @@ -972,52 +1732,51 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, } if (send_size > GLUSTERFS_RDMA_INLINE_THRESHOLD) { - rtype = rdma_areadch; + rtype = gf_rdma_areadch; } else if ((send_size + prog_payload_length) < GLUSTERFS_RDMA_INLINE_THRESHOLD) { - rtype = rdma_noch; + rtype = gf_rdma_noch; } else if (entry->prog_payload_count != 0) { - rtype = rdma_readch; + rtype = gf_rdma_readch; } if (entry->msg.request.rsphdr_count != 0) { - wtype = rdma_replych; + wtype = gf_rdma_replych; } else if (entry->msg.request.rsp_payload_count != 0) { - wtype = rdma_writech; + wtype = gf_rdma_writech; } - if (rtype == rdma_readch) { + if (rtype == gf_rdma_readch) { chunk_count += entry->prog_payload_count; - } else if (rtype == rdma_areadch) { + } else if (rtype == gf_rdma_areadch) { chunk_count += entry->rpchdr_count; chunk_count += entry->proghdr_count; } - if (wtype == rdma_writech) { + if (wtype == gf_rdma_writech) { chunk_count += entry->msg.request.rsp_payload_count; - } else if (wtype == rdma_replych) { + } else if (wtype == gf_rdma_replych) { chunk_count += entry->msg.request.rsphdr_count; } - if (chunk_count > RDMA_MAX_SEGMENTS) { + if (chunk_count > GF_RDMA_MAX_SEGMENTS) { ret = -1; - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "chunk count(%d) exceeding maximum allowed RDMA " - "segment count(%d)", chunk_count, RDMA_MAX_SEGMENTS); + "segment count(%d)", chunk_count, GF_RDMA_MAX_SEGMENTS); goto out; } - if ((wtype != rdma_noch) || (rtype != rdma_noch)) { - request_ctx = mem_get (priv->device->request_ctx_pool); + if ((wtype != gf_rdma_noch) || (rtype != gf_rdma_noch)) { + request_ctx = mem_get (device->request_ctx_pool); if (request_ctx == NULL) { ret = -1; - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } memset (request_ctx, 0, sizeof (*request_ctx)); - request_ctx->pool = priv->device->request_ctx_pool; + request_ctx->pool = device->request_ctx_pool; request_ctx->peer = peer; entry->msg.request.rpc_req->conn_private = request_ctx; @@ -1034,20 +1793,21 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, * since rpc_msg->rm_xid is already * hton32ed value of actual xid */ - hdr->rm_vers = hton32 (RDMA_VERSION); + hdr->rm_vers = hton32 (GF_RDMA_VERSION); hdr->rm_credit = hton32 (peer->send_count); - if (rtype == rdma_areadch) { - hdr->rm_type = hton32 (RDMA_NOMSG); + if (rtype == gf_rdma_areadch) { + hdr->rm_type = hton32 (GF_RDMA_NOMSG); } else { - hdr->rm_type = hton32 (RDMA_MSG); + hdr->rm_type = hton32 (GF_RDMA_MSG); } chunkptr = &hdr->rm_body.rm_chunks[0]; - if (rtype != rdma_noch) { - ret = __rdma_create_read_chunks (peer, entry, rtype, &chunkptr, - request_ctx); + if (rtype != gf_rdma_noch) { + ret = __gf_rdma_create_read_chunks (peer, entry, rtype, + &chunkptr, + request_ctx); if (ret != 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "creation of read chunks failed"); goto out; } @@ -1055,11 +1815,12 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, *chunkptr++ = 0; /* no read chunks */ } - if (wtype != rdma_noch) { - ret = __rdma_create_write_chunks (peer, entry, wtype, &chunkptr, - request_ctx); + if (wtype != gf_rdma_noch) { + ret = __gf_rdma_create_write_chunks (peer, entry, wtype, + &chunkptr, + request_ctx); if (ret != 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "creation of write/reply chunk failed"); goto out; } @@ -1070,14 +1831,14 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, buf = (char *)chunkptr; - if (rtype != rdma_areadch) { + if (rtype != gf_rdma_areadch) { iov_unload (buf, entry->rpchdr, entry->rpchdr_count); buf += iov_length (entry->rpchdr, entry->rpchdr_count); iov_unload (buf, entry->proghdr, entry->proghdr_count); buf += iov_length (entry->proghdr, entry->proghdr_count); - if (rtype != rdma_readch) { + if (rtype != gf_rdma_readch) { iov_unload (buf, entry->prog_payload, entry->prog_payload_count); buf += iov_length (entry->prog_payload, @@ -1086,19 +1847,19 @@ __rdma_ioq_churn_request (rdma_peer_t *peer, rdma_ioq_t *entry, } len = buf - post->buf; - - rdma_post_ref (post); - ret = rdma_post_send (peer->qp, post, len); + gf_rdma_post_ref (post); + + ret = gf_rdma_post_send (peer->qp, post, len); if (!ret) { ret = len; } else { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "rdma_post_send (to %s) failed with ret = %d (%s)", + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "gf_rdma_post_send (to %s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); - rdma_post_unref (post); - __rdma_disconnect (peer->trans); + gf_rdma_post_unref (post); + __gf_rdma_disconnect (peer->trans); ret = -1; } @@ -1107,7 +1868,7 @@ out: rpc_req = entry->msg.request.rpc_req; if (request_ctx != NULL) { - __rdma_request_context_destroy (rpc_req->conn_private); + __gf_rdma_request_context_destroy (rpc_req->conn_private); } rpc_req->conn_private = NULL; @@ -1117,9 +1878,9 @@ out: } -inline void -__rdma_fill_reply_header (rdma_header_t *header, struct iovec *rpchdr, - rdma_reply_info_t *reply_info, int credits) +static inline void +__gf_rdma_fill_reply_header (gf_rdma_header_t *header, struct iovec *rpchdr, + gf_rdma_reply_info_t *reply_info, int credits) { struct rpc_msg *rpc_msg = NULL; @@ -1127,14 +1888,14 @@ __rdma_fill_reply_header (rdma_header_t *header, struct iovec *rpchdr, header->rm_xid = hton32 (reply_info->rm_xid); } else { rpc_msg = rpchdr[0].iov_base; /* assume rpchdr contains - * only one vector. - * (which is true) - */ + * only one vector. + * (which is true) + */ header->rm_xid = rpc_msg->rm_xid; } - header->rm_type = hton32 (RDMA_MSG); - header->rm_vers = hton32 (RDMA_VERSION); + header->rm_type = hton32 (GF_RDMA_MSG); + header->rm_vers = hton32 (GF_RDMA_VERSION); header->rm_credit = hton32 (credits); header->rm_body.rm_chunks[0] = 0; /* no read chunks */ @@ -1146,36 +1907,36 @@ __rdma_fill_reply_header (rdma_header_t *header, struct iovec *rpchdr, int32_t -__rdma_send_reply_inline (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_post_t *post, rdma_reply_info_t *reply_info) +__gf_rdma_send_reply_inline (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post, + gf_rdma_reply_info_t *reply_info) { - rdma_header_t *header = NULL; - int32_t send_size = 0, ret = 0; - char *buf = NULL; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - - priv = peer->trans->private; - device = priv->device; + gf_rdma_header_t *header = NULL; + int32_t send_size = 0, ret = 0; + char *buf = NULL; send_size = iov_length (entry->rpchdr, entry->rpchdr_count) + iov_length (entry->proghdr, entry->proghdr_count) + iov_length (entry->prog_payload, entry->prog_payload_count) - + sizeof (rdma_header_t); /* - * remember, no chunklists in the - * reply - */ + + sizeof (gf_rdma_header_t); /* + * remember, no chunklists in the + * reply + */ if (send_size > GLUSTERFS_RDMA_INLINE_THRESHOLD) { - ret = __rdma_send_error (peer, entry, post, reply_info, - ERR_CHUNK); + ret = __gf_rdma_send_error (peer, entry, post, reply_info, + ERR_CHUNK); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "msg size (%d) is greater than maximum size " + "of msg that can be sent inlined (%d)", + send_size, GLUSTERFS_RDMA_INLINE_THRESHOLD); goto out; } - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; - __rdma_fill_reply_header (header, entry->rpchdr, reply_info, - peer->send_count); + __gf_rdma_fill_reply_header (header, entry->rpchdr, reply_info, + peer->send_count); buf = (char *)&header->rm_body.rm_chunks[3]; @@ -1188,7 +1949,7 @@ __rdma_send_reply_inline (rdma_peer_t *peer, rdma_ioq_t *entry, iov_unload (buf, entry->proghdr, entry->proghdr_count); buf += iov_length (entry->proghdr, entry->proghdr_count); } - + if (entry->prog_payload_count != 0) { iov_unload (buf, entry->prog_payload, entry->prog_payload_count); @@ -1196,18 +1957,18 @@ __rdma_send_reply_inline (rdma_peer_t *peer, rdma_ioq_t *entry, entry->prog_payload_count); } - rdma_post_ref (post); + gf_rdma_post_ref (post); - ret = rdma_post_send (peer->qp, post, (buf - post->buf)); + ret = gf_rdma_post_send (peer->qp, post, (buf - post->buf)); if (!ret) { ret = send_size; } else { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "rdma_post_send (to %s) failed with ret = %d (%s)", + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "posting send (to %s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); - rdma_post_unref (post); - __rdma_disconnect (peer->trans); + gf_rdma_post_unref (post); + __gf_rdma_disconnect (peer->trans); ret = -1; } @@ -1217,17 +1978,18 @@ out: int32_t -__rdma_reply_encode_write_chunks (rdma_peer_t *peer, uint32_t payload_size, - rdma_post_t *post, - rdma_reply_info_t *reply_info, - uint32_t **ptr) +__gf_rdma_reply_encode_write_chunks (gf_rdma_peer_t *peer, + uint32_t payload_size, + gf_rdma_post_t *post, + gf_rdma_reply_info_t *reply_info, + uint32_t **ptr) { - uint32_t chunk_size = 0; - int32_t ret = -1; - rdma_write_array_t *target_array = NULL; - int i = 0; + uint32_t chunk_size = 0; + int32_t ret = -1; + gf_rdma_write_array_t *target_array = NULL; + int i = 0; - target_array = (rdma_write_array_t *)*ptr; + target_array = (gf_rdma_write_array_t *)*ptr; for (i = 0; i < reply_info->wc_array->wc_nchunks; i++) { chunk_size += @@ -1235,7 +1997,7 @@ __rdma_reply_encode_write_chunks (rdma_peer_t *peer, uint32_t payload_size, } if (chunk_size < payload_size) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, "length of payload (%d) is exceeding the total " "write chunk length (%d)", payload_size, chunk_size); goto out; @@ -1250,7 +2012,7 @@ __rdma_reply_encode_write_chunks (rdma_peer_t *peer, uint32_t payload_size, target_array->wc_array[i].wc_target.rs_length = hton32 (min (payload_size, - reply_info->wc_array->wc_array[i].wc_target.rs_length)); + reply_info->wc_array->wc_array[i].wc_target.rs_length)); } target_array->wc_nchunks = hton32 (i); @@ -1266,18 +2028,17 @@ out: inline int32_t -__rdma_register_local_mr_for_rdma (rdma_peer_t *peer, - struct iovec *vector, int count, - rdma_post_context_t *ctx) +__gf_rdma_register_local_mr_for_rdma (gf_rdma_peer_t *peer, + struct iovec *vector, int count, + gf_rdma_post_context_t *ctx) { - int i = 0; - int32_t ret = -1; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; + int i = 0; + int32_t ret = -1; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; - if ((ctx == NULL) || (vector == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, ctx, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, vector, out); priv = peer->trans->private; device = priv->device; @@ -1298,6 +2059,9 @@ __rdma_register_local_mr_for_rdma (rdma_peer_t *peer, vector[i].iov_len, IBV_ACCESS_LOCAL_WRITE); if (ctx->mr[ctx->mr_count] == NULL) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "registering memory for IBV_ACCESS_LOCAL_WRITE " + "failed (%s)", strerror (errno)); goto out; } @@ -1313,15 +2077,15 @@ out: * 2. modifies vec */ int32_t -__rdma_write (rdma_peer_t *peer, rdma_post_t *post, struct iovec *vec, - uint32_t xfer_len, int *idx, rdma_write_chunk_t *writech) +__gf_rdma_write (gf_rdma_peer_t *peer, gf_rdma_post_t *post, struct iovec *vec, + uint32_t xfer_len, int *idx, gf_rdma_write_chunk_t *writech) { - int size = 0, num_sge = 0, i = 0; - int32_t ret = -1; - struct ibv_sge *sg_list = NULL; - struct ibv_send_wr wr = { - .opcode = IBV_WR_RDMA_WRITE, - .send_flags = IBV_SEND_SIGNALED, + int size = 0, num_sge = 0, i = 0; + int32_t ret = -1; + struct ibv_sge *sg_list = NULL; + struct ibv_send_wr wr = { + .opcode = IBV_WR_RDMA_WRITE, + .send_flags = IBV_SEND_SIGNALED, }, *bad_wr; if ((peer == NULL) || (writech == NULL) || (idx == NULL) @@ -1335,9 +2099,9 @@ __rdma_write (rdma_peer_t *peer, rdma_post_t *post, struct iovec *vec, num_sge = i - *idx; - sg_list = GF_CALLOC (num_sge, sizeof (struct ibv_sge), gf_common_mt_sge); + sg_list = GF_CALLOC (num_sge, sizeof (struct ibv_sge), + gf_common_mt_sge); if (sg_list == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); ret = -1; goto out; } @@ -1362,13 +2126,14 @@ __rdma_write (rdma_peer_t *peer, rdma_post_t *post, struct iovec *vec, wr.sg_list = sg_list; wr.num_sge = num_sge; - wr.wr_id = (unsigned long) rdma_post_ref (post); + wr.wr_id = (unsigned long) gf_rdma_post_ref (post); wr.wr.rdma.rkey = writech->wc_target.rs_handle; wr.wr.rdma.remote_addr = writech->wc_target.rs_offset; ret = ibv_post_send(peer->qp, &wr, &bad_wr); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, "rdma write to " + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma write to " "client (%s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); @@ -1382,14 +2147,15 @@ out: int32_t -__rdma_do_rdma_write (rdma_peer_t *peer, rdma_post_t *post, - struct iovec *vector, int count, struct iobref *iobref, - rdma_reply_info_t *reply_info) +__gf_rdma_do_gf_rdma_write (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + struct iovec *vector, int count, + struct iobref *iobref, + gf_rdma_reply_info_t *reply_info) { - int i = 0, payload_idx = 0; - uint32_t payload_size = 0, xfer_len = 0; - int32_t ret = -1; - + int i = 0, payload_idx = 0; + uint32_t payload_size = 0, xfer_len = 0; + int32_t ret = -1; + if (count != 0) { payload_size = iov_length (vector, count); } @@ -1399,9 +2165,11 @@ __rdma_do_rdma_write (rdma_peer_t *peer, rdma_post_t *post, goto out; } - ret = __rdma_register_local_mr_for_rdma (peer, vector, count, - &post->ctx); + ret = __gf_rdma_register_local_mr_for_rdma (peer, vector, count, + &post->ctx); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "registering memory region for rdma failed"); goto out; } @@ -1413,9 +2181,13 @@ __rdma_do_rdma_write (rdma_peer_t *peer, rdma_post_t *post, xfer_len = min (payload_size, reply_info->wc_array->wc_array[i].wc_target.rs_length); - ret = __rdma_write (peer, post, vector, xfer_len, &payload_idx, - &reply_info->wc_array->wc_array[i]); + ret = __gf_rdma_write (peer, post, vector, xfer_len, + &payload_idx, + &reply_info->wc_array->wc_array[i]); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma write to client (%s) failed", + peer->trans->peerinfo.identifier); goto out; } @@ -1430,44 +2202,41 @@ out: int32_t -__rdma_send_reply_type_nomsg (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_post_t *post, rdma_reply_info_t *reply_info) +__gf_rdma_send_reply_type_nomsg (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post, + gf_rdma_reply_info_t *reply_info) { - rdma_header_t *header = NULL; - char *buf = NULL; - uint32_t payload_size = 0; - int count = 0, i = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - int32_t ret = 0; - struct iovec vector[MAX_IOVEC]; + gf_rdma_header_t *header = NULL; + char *buf = NULL; + uint32_t payload_size = 0; + int count = 0, i = 0; + int32_t ret = 0; + struct iovec vector[MAX_IOVEC]; - priv = peer->trans->private; - device = priv->device; - - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; - __rdma_fill_reply_header (header, entry->rpchdr, reply_info, - peer->send_count); + __gf_rdma_fill_reply_header (header, entry->rpchdr, reply_info, + peer->send_count); - header->rm_type = hton32 (RDMA_NOMSG); + header->rm_type = hton32 (GF_RDMA_NOMSG); payload_size = iov_length (entry->rpchdr, entry->rpchdr_count) + iov_length (entry->proghdr, entry->proghdr_count); /* encode reply chunklist */ buf = (char *)&header->rm_body.rm_chunks[2]; - ret = __rdma_reply_encode_write_chunks (peer, payload_size, post, - reply_info, (uint32_t **)&buf); + ret = __gf_rdma_reply_encode_write_chunks (peer, payload_size, post, + reply_info, + (uint32_t **)&buf); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "encoding write chunks failed"); - ret = __rdma_send_error (peer, entry, post, reply_info, - ERR_CHUNK); + ret = __gf_rdma_send_error (peer, entry, post, reply_info, + ERR_CHUNK); goto out; } - rdma_post_ref (post); + gf_rdma_post_ref (post); for (i = 0; i < entry->rpchdr_count; i++) { vector[count++] = entry->rpchdr[i]; @@ -1477,21 +2246,24 @@ __rdma_send_reply_type_nomsg (rdma_peer_t *peer, rdma_ioq_t *entry, vector[count++] = entry->proghdr[i]; } - ret = __rdma_do_rdma_write (peer, post, vector, count, entry->iobref, - reply_info); + ret = __gf_rdma_do_gf_rdma_write (peer, post, vector, count, + entry->iobref, reply_info); if (ret == -1) { - rdma_post_unref (post); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma write to peer (%s) failed", + peer->trans->peerinfo.identifier); + gf_rdma_post_unref (post); goto out; } - ret = rdma_post_send (peer->qp, post, (buf - post->buf)); + ret = gf_rdma_post_send (peer->qp, post, (buf - post->buf)); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "rdma_post_send to client (%s) failed with " - "ret = %d (%s)", peer->trans->peerinfo.identifier, ret, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "posting a send request to client (%s) failed with " + "ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); ret = -1; - rdma_post_unref (post); + gf_rdma_post_unref (post); } else { ret = payload_size; } @@ -1502,65 +2274,64 @@ out: int32_t -__rdma_send_reply_type_msg (rdma_peer_t *peer, rdma_ioq_t *entry, - rdma_post_t *post, rdma_reply_info_t *reply_info) +__gf_rdma_send_reply_type_msg (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post, + gf_rdma_reply_info_t *reply_info) { - rdma_header_t *header = NULL; - int32_t send_size = 0, ret = 0; - char *ptr = NULL; - uint32_t payload_size = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - - priv = peer->trans->private; - device = priv->device; + gf_rdma_header_t *header = NULL; + int32_t send_size = 0, ret = 0; + char *ptr = NULL; + uint32_t payload_size = 0; send_size = iov_length (entry->rpchdr, entry->rpchdr_count) + iov_length (entry->proghdr, entry->proghdr_count) + GLUSTERFS_RDMA_MAX_HEADER_SIZE; if (send_size > GLUSTERFS_RDMA_INLINE_THRESHOLD) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "client has provided only write chunks, but the " "combined size of rpc and program header (%d) is " "exceeding the size of msg that can be sent using " "RDMA send (%d)", send_size, GLUSTERFS_RDMA_INLINE_THRESHOLD); - ret = __rdma_send_error (peer, entry, post, reply_info, - ERR_CHUNK); + ret = __gf_rdma_send_error (peer, entry, post, reply_info, + ERR_CHUNK); goto out; } - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; - __rdma_fill_reply_header (header, entry->rpchdr, reply_info, - peer->send_count); + __gf_rdma_fill_reply_header (header, entry->rpchdr, reply_info, + peer->send_count); payload_size = iov_length (entry->prog_payload, entry->prog_payload_count); ptr = (char *)&header->rm_body.rm_chunks[1]; - ret = __rdma_reply_encode_write_chunks (peer, payload_size, post, - reply_info, (uint32_t **)&ptr); + ret = __gf_rdma_reply_encode_write_chunks (peer, payload_size, post, + reply_info, + (uint32_t **)&ptr); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "encoding write chunks failed"); - ret = __rdma_send_error (peer, entry, post, reply_info, - ERR_CHUNK); + ret = __gf_rdma_send_error (peer, entry, post, reply_info, + ERR_CHUNK); goto out; } *(uint32_t *)ptr = 0; /* terminate reply chunklist */ ptr += sizeof (uint32_t); - rdma_post_ref (post); + gf_rdma_post_ref (post); - ret = __rdma_do_rdma_write (peer, post, entry->prog_payload, - entry->prog_payload_count, entry->iobref, - reply_info); + ret = __gf_rdma_do_gf_rdma_write (peer, post, entry->prog_payload, + entry->prog_payload_count, + entry->iobref, reply_info); if (ret == -1) { - rdma_post_unref (post); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "rdma write to peer " + "(%s) failed", peer->trans->peerinfo.identifier); + gf_rdma_post_unref (post); goto out; } @@ -1570,13 +2341,13 @@ __rdma_send_reply_type_msg (rdma_peer_t *peer, rdma_ioq_t *entry, iov_unload (ptr, entry->proghdr, entry->proghdr_count); ptr += iov_length (entry->proghdr, entry->proghdr_count); - ret = rdma_post_send (peer->qp, post, (ptr - post->buf)); + ret = gf_rdma_post_send (peer->qp, post, (ptr - post->buf)); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "rdma send to client (%s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); - rdma_post_unref (post); + gf_rdma_post_unref (post); ret = -1; } else { ret = send_size + payload_size; @@ -1588,7 +2359,7 @@ out: void -rdma_reply_info_destroy (rdma_reply_info_t *reply_info) +gf_rdma_reply_info_destroy (gf_rdma_reply_info_t *reply_info) { if (reply_info == NULL) { goto out; @@ -1599,23 +2370,22 @@ rdma_reply_info_destroy (rdma_reply_info_t *reply_info) reply_info->wc_array = NULL; } - mem_put (reply_info->pool, reply_info); + mem_put (reply_info); out: return; } -rdma_reply_info_t * -rdma_reply_info_alloc (rdma_peer_t *peer) +gf_rdma_reply_info_t * +gf_rdma_reply_info_alloc (gf_rdma_peer_t *peer) { - rdma_reply_info_t *reply_info = NULL; - rdma_private_t *priv = NULL; + gf_rdma_reply_info_t *reply_info = NULL; + gf_rdma_private_t *priv = NULL; priv = peer->trans->private; reply_info = mem_get (priv->device->reply_info_pool); if (reply_info == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } @@ -1628,15 +2398,16 @@ out: int32_t -__rdma_ioq_churn_reply (rdma_peer_t *peer, rdma_ioq_t *entry, rdma_post_t *post) +__gf_rdma_ioq_churn_reply (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry, + gf_rdma_post_t *post) { - rdma_reply_info_t *reply_info = NULL; - int32_t ret = -1; - rdma_chunktype_t type = rdma_noch; + gf_rdma_reply_info_t *reply_info = NULL; + int32_t ret = -1; + gf_rdma_chunktype_t type = gf_rdma_noch; - if ((peer == NULL) || (entry == NULL) || (post == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, peer, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, entry, out); + GF_VALIDATE_OR_GOTO (GF_RDMA_LOG_NAME, post, out); reply_info = entry->msg.reply_info; if (reply_info != NULL) { @@ -1644,29 +2415,47 @@ __rdma_ioq_churn_reply (rdma_peer_t *peer, rdma_ioq_t *entry, rdma_post_t *post) } switch (type) { - case rdma_noch: - ret = __rdma_send_reply_inline (peer, entry, post, reply_info); + case gf_rdma_noch: + ret = __gf_rdma_send_reply_inline (peer, entry, post, + reply_info); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "failed to send reply to peer (%s) as an " + "inlined rdma msg", + peer->trans->peerinfo.identifier); + } break; - case rdma_replych: - ret = __rdma_send_reply_type_nomsg (peer, entry, post, - reply_info); + case gf_rdma_replych: + ret = __gf_rdma_send_reply_type_nomsg (peer, entry, post, + reply_info); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "failed to send reply to peer (%s) as " + "RDMA_NOMSG", peer->trans->peerinfo.identifier); + } break; - case rdma_writech: - ret = __rdma_send_reply_type_msg (peer, entry, post, - reply_info); + case gf_rdma_writech: + ret = __gf_rdma_send_reply_type_msg (peer, entry, post, + reply_info); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "failed to send reply with write chunks " + "to peer (%s)", + peer->trans->peerinfo.identifier); + } break; default: - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "invalid chunktype (%d) specified for sending reply", - type); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "invalid chunktype (%d) specified for sending reply " + " (peer:%s)", type, peer->trans->peerinfo.identifier); break; } if (reply_info != NULL) { - rdma_reply_info_destroy (reply_info); + gf_rdma_reply_info_destroy (reply_info); } out: return ret; @@ -1674,40 +2463,54 @@ out: int32_t -__rdma_ioq_churn_entry (rdma_peer_t *peer, rdma_ioq_t *entry) +__gf_rdma_ioq_churn_entry (gf_rdma_peer_t *peer, gf_rdma_ioq_t *entry) { - int32_t ret = 0, quota = 0; - rdma_private_t *priv = NULL; - rdma_device_t *device = NULL; - rdma_options_t *options = NULL; - rdma_post_t *post = NULL; + int32_t ret = 0, quota = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + gf_rdma_options_t *options = NULL; + gf_rdma_post_t *post = NULL; priv = peer->trans->private; options = &priv->options; device = priv->device; - quota = __rdma_quota_get (peer); + quota = __gf_rdma_quota_get (peer); if (quota > 0) { - post = rdma_get_post (&device->sendq); - if (post == NULL) { - post = rdma_new_post (device, - (options->send_size + 2048), - RDMA_SEND_POST); + post = gf_rdma_get_post (&device->sendq); + if (post == NULL) { + post = gf_rdma_new_post (peer->trans, device, + (options->send_size + 2048), + GF_RDMA_SEND_POST); } if (post == NULL) { ret = -1; + gf_log_callingfn (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "not able to get a post to send msg"); goto out; } if (entry->is_request) { - ret = __rdma_ioq_churn_request (peer, entry, post); + ret = __gf_rdma_ioq_churn_request (peer, entry, post); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "failed to process request ioq entry " + "to peer(%s)", + peer->trans->peerinfo.identifier); + } } else { - ret = __rdma_ioq_churn_reply (peer, entry, post); + ret = __gf_rdma_ioq_churn_reply (peer, entry, post); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "failed to process reply ioq entry " + "to peer (%s)", + peer->trans->peerinfo.identifier); + } } if (ret != 0) { - __rdma_ioq_entry_free (entry); + __gf_rdma_ioq_entry_free (entry); } } else { ret = 0; @@ -1719,17 +2522,17 @@ out: static int32_t -__rdma_ioq_churn (rdma_peer_t *peer) +__gf_rdma_ioq_churn (gf_rdma_peer_t *peer) { - rdma_ioq_t *entry = NULL; - int32_t ret = 0; + gf_rdma_ioq_t *entry = NULL; + int32_t ret = 0; while (!list_empty (&peer->ioq)) { /* pick next entry */ entry = peer->ioq_next; - ret = __rdma_ioq_churn_entry (peer, entry); + ret = __gf_rdma_ioq_churn_entry (peer, entry); if (ret <= 0) break; @@ -1737,7 +2540,7 @@ __rdma_ioq_churn (rdma_peer_t *peer) /* list_for_each_entry_safe (entry, dummy, &peer->ioq, list) { - ret = __rdma_ioq_churn_entry (peer, entry); + ret = __gf_rdma_ioq_churn_entry (peer, entry); if (ret <= 0) { break; } @@ -1749,28 +2552,35 @@ __rdma_ioq_churn (rdma_peer_t *peer) static int32_t -rdma_writev (rpc_transport_t *this, - rdma_ioq_t *entry) +gf_rdma_writev (rpc_transport_t *this, gf_rdma_ioq_t *entry) { - int32_t ret = 0, need_append = 1; - rdma_private_t *priv = this->private; - rdma_peer_t *peer = NULL; + int32_t ret = 0, need_append = 1; + gf_rdma_private_t *priv = NULL; + gf_rdma_peer_t *peer = NULL; + priv = this->private; pthread_mutex_lock (&priv->write_mutex); { if (!priv->connected) { - gf_log (this->name, GF_LOG_DEBUG, - "rdma is not connected to post a " - "send request"); + gf_log (this->name, GF_LOG_WARNING, + "rdma is not connected to peer (%s)", + this->peerinfo.identifier); ret = -1; goto unlock; } peer = &priv->peer; if (list_empty (&peer->ioq)) { - ret = __rdma_ioq_churn_entry (peer, entry); + ret = __gf_rdma_ioq_churn_entry (peer, entry); if (ret != 0) { need_append = 0; + + if (ret < 0) { + gf_log (this->name, GF_LOG_WARNING, + "processing ioq entry destined " + "to (%s) failed", + this->peerinfo.identifier); + } } } @@ -1784,23 +2594,22 @@ unlock: } -rdma_ioq_t * -rdma_ioq_new (rpc_transport_t *this, rpc_transport_data_t *data) +gf_rdma_ioq_t * +gf_rdma_ioq_new (rpc_transport_t *this, rpc_transport_data_t *data) { - rdma_ioq_t *entry = NULL; - int count = 0, i = 0; - rpc_transport_msg_t *msg = NULL; - rdma_private_t *priv = NULL; + gf_rdma_ioq_t *entry = NULL; + int count = 0, i = 0; + rpc_transport_msg_t *msg = NULL; + gf_rdma_private_t *priv = NULL; if ((data == NULL) || (this == NULL)) { goto out; } priv = this->private; - /* TODO: use mem-pool */ + entry = mem_get (priv->device->ioq_pool); if (entry == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } memset (entry, 0, sizeof (*entry)); @@ -1821,7 +2630,7 @@ rdma_ioq_new (rpc_transport_t *this, rpc_transport_data_t *data) if (data->data.req.rsp.rsp_payload_count != 0) { for (i = 0; i < data->data.req.rsp.rsp_payload_count; i++) { - entry->msg.request.rsp_payload[i] + entry->msg.request.rsp_payload[i] = data->data.req.rsp.rsp_payload[i]; } @@ -1876,12 +2685,11 @@ out: int32_t -rdma_submit_request (rpc_transport_t *this, - rpc_transport_req_t *req) +gf_rdma_submit_request (rpc_transport_t *this, rpc_transport_req_t *req) { - int32_t ret = 0; - rdma_ioq_t *entry = NULL; - rpc_transport_data_t data = {0, }; + int32_t ret = 0; + gf_rdma_ioq_t *entry = NULL; + rpc_transport_data_t data = {0, }; if (req == NULL) { goto out; @@ -1890,16 +2698,22 @@ rdma_submit_request (rpc_transport_t *this, data.is_request = 1; data.data.req = *req; - entry = rdma_ioq_new (this, &data); + entry = gf_rdma_ioq_new (this, &data); if (entry == NULL) { + gf_log (this->name, GF_LOG_WARNING, + "getting a new ioq entry failed (peer:%s)", + this->peerinfo.identifier); goto out; } - ret = rdma_writev (this, entry); + ret = gf_rdma_writev (this, entry); if (ret > 0) { ret = 0; } else if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "sending request to peer (%s) failed", + this->peerinfo.identifier); rpc_transport_disconnect (this); } @@ -1908,11 +2722,11 @@ out: } int32_t -rdma_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply) +gf_rdma_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply) { - int32_t ret = 0; - rdma_ioq_t *entry = NULL; - rpc_transport_data_t data = {0, }; + int32_t ret = 0; + gf_rdma_ioq_t *entry = NULL; + rpc_transport_data_t data = {0, }; if (reply == NULL) { goto out; @@ -1920,15 +2734,21 @@ rdma_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply) data.data.reply = *reply; - entry = rdma_ioq_new (this, &data); + entry = gf_rdma_ioq_new (this, &data); if (entry == NULL) { + gf_log (this->name, GF_LOG_WARNING, + "getting a new ioq entry failed (peer:%s)", + this->peerinfo.identifier); goto out; } - ret = rdma_writev (this, entry); + ret = gf_rdma_writev (this, entry); if (ret > 0) { ret = 0; } else if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "sending request to peer (%s) failed", + this->peerinfo.identifier); rpc_transport_disconnect (this); } @@ -1936,196 +2756,15 @@ out: return ret; } -#if 0 -static int -rdma_receive (rpc_transport_t *this, char **hdr_p, size_t *hdrlen_p, - struct iobuf **iobuf_p) -{ - rdma_private_t *priv = this->private; - /* TODO: return error if !priv->connected, check with locks */ - /* TODO: boundry checks for data_ptr/offset */ - char *copy_from = NULL; - rdma_header_t *header = NULL; - uint32_t size1, size2, data_len = 0; - char *hdr = NULL; - struct iobuf *iobuf = NULL; - int32_t ret = 0; - - pthread_mutex_lock (&priv->recv_mutex); - { -/* - while (!priv->data_ptr) - pthread_cond_wait (&priv->recv_cond, &priv->recv_mutex); -*/ - - copy_from = priv->data_ptr + priv->data_offset; - - priv->data_ptr = NULL; - data_len = priv->data_len; - pthread_cond_broadcast (&priv->recv_cond); - } - pthread_mutex_unlock (&priv->recv_mutex); - - header = (rdma_header_t *)copy_from; - if (strcmp (header->colonO, ":O")) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "%s: corrupt header received", this->name); - ret = -1; - goto err; - } - - size1 = ntoh32 (header->size1); - size2 = ntoh32 (header->size2); - - if (data_len != (size1 + size2 + sizeof (*header))) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "%s: sizeof data read from transport is not equal " - "to the size specified in the header", - this->name); - ret = -1; - goto err; - } - - copy_from += sizeof (*header); - - if (size1) { - hdr = GF_CALLOC (1, size1, gf_common_mt_char); - if (!hdr) { - gf_log (this->name, GF_LOG_ERROR, - "unable to allocate header for peer %s", - this->peerinfo.identifier); - ret = -ENOMEM; - goto err; - } - memcpy (hdr, copy_from, size1); - copy_from += size1; - *hdr_p = hdr; - } - *hdrlen_p = size1; - - if (size2) { - iobuf = iobuf_get (this->ctx->iobuf_pool); - if (!iobuf) { - gf_log (this->name, GF_LOG_ERROR, - "unable to allocate IO buffer for peer %s", - this->peerinfo.identifier); - ret = -ENOMEM; - goto err; - } - memcpy (iobuf->ptr, copy_from, size2); - *iobuf_p = iobuf; - } - -err: - return ret; -} -#endif - - -static void -rdma_destroy_cq (rpc_transport_t *this) -{ - rdma_private_t *priv = this->private; - rdma_device_t *device = priv->device; - - if (device->recv_cq) - ibv_destroy_cq (device->recv_cq); - device->recv_cq = NULL; - - if (device->send_cq) - ibv_destroy_cq (device->send_cq); - device->send_cq = NULL; - - return; -} - - -static int32_t -rdma_create_cq (rpc_transport_t *this) -{ - rdma_private_t *priv = NULL; - rdma_options_t *options = NULL; - rdma_device_t *device = NULL; - uint64_t send_cqe = 0; - int32_t ret = 0; - struct ibv_device_attr device_attr = {{0}, }; - - priv = this->private; - options = &priv->options; - device = priv->device; - - device->recv_cq = ibv_create_cq (priv->device->context, - options->recv_count * 2, - device, - device->recv_chan, - 0); - if (!device->recv_cq) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: creation of CQ for device %s failed", - this->name, device->device_name); - ret = -1; - goto out; - } else if (ibv_req_notify_cq (device->recv_cq, 0)) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: ibv_req_notify_cq on recv CQ of device %s failed", - this->name, device->device_name); - ret = -1; - goto out; - } - - do { - ret = ibv_query_device (priv->device->context, &device_attr); - if (ret != 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: ibv_query_device on %s returned %d (%s)", - this->name, priv->device->device_name, ret, - (ret > 0) ? strerror (ret) : ""); - ret = -1; - goto out; - } - - send_cqe = options->send_count * 128; - send_cqe = (send_cqe > device_attr.max_cqe) - ? device_attr.max_cqe : send_cqe; - - /* TODO: make send_cq size dynamically adaptive */ - device->send_cq = ibv_create_cq (priv->device->context, - send_cqe, device, - device->send_chan, 0); - if (!device->send_cq) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: creation of send_cq for device %s failed", - this->name, device->device_name); - ret = -1; - goto out; - } - - if (ibv_req_notify_cq (device->send_cq, 0)) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: ibv_req_notify_cq on send_cq for device %s" - " failed", this->name, device->device_name); - ret = -1; - goto out; - } - } while (0); - -out: - if (ret != 0) - rdma_destroy_cq (this); - - return ret; -} - static int -rdma_register_peer (rdma_device_t *device, - int32_t qp_num, - rdma_peer_t *peer) +gf_rdma_register_peer (gf_rdma_device_t *device, int32_t qp_num, + gf_rdma_peer_t *peer) { - struct _qpent *ent = NULL; - rdma_qpreg_t *qpreg = NULL; - int32_t hash = 0; - int ret = -1; + struct _qpent *ent = NULL; + gf_rdma_qpreg_t *qpreg = NULL; + int32_t hash = 0; + int ret = -1; qpreg = &device->qpreg; hash = qp_num % 42; @@ -2145,7 +2784,6 @@ rdma_register_peer (rdma_device_t *device, ent = (struct _qpent *) GF_CALLOC (1, sizeof (*ent), gf_common_mt_qpent); if (ent == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto unlock; } @@ -2167,37 +2805,41 @@ unlock: static void -rdma_unregister_peer (rdma_device_t *device, - int32_t qp_num) +gf_rdma_unregister_peer (gf_rdma_device_t *device, int32_t qp_num) { - struct _qpent *ent; - rdma_qpreg_t *qpreg = &device->qpreg; - int32_t hash = qp_num % 42; + struct _qpent *ent = NULL; + gf_rdma_qpreg_t *qpreg = NULL; + int32_t hash = 0; + + qpreg = &device->qpreg; + hash = qp_num % 42; pthread_mutex_lock (&qpreg->lock); - ent = qpreg->ents[hash].next; - while ((ent != &qpreg->ents[hash]) && (ent->qp_num != qp_num)) - ent = ent->next; - if (ent->qp_num != qp_num) { - pthread_mutex_unlock (&qpreg->lock); - return; - } - ent->prev->next = ent->next; - ent->next->prev = ent->prev; - /* TODO: unref reg->peer */ - GF_FREE (ent); - qpreg->count--; + { + ent = qpreg->ents[hash].next; + while ((ent != &qpreg->ents[hash]) && (ent->qp_num != qp_num)) + ent = ent->next; + if (ent->qp_num != qp_num) { + pthread_mutex_unlock (&qpreg->lock); + return; + } + ent->prev->next = ent->next; + ent->next->prev = ent->prev; + /* TODO: unref reg->peer */ + GF_FREE (ent); + qpreg->count--; + } pthread_mutex_unlock (&qpreg->lock); } -static rdma_peer_t * -__rdma_lookup_peer (rdma_device_t *device, int32_t qp_num) +static gf_rdma_peer_t * +__gf_rdma_lookup_peer (gf_rdma_device_t *device, int32_t qp_num) { - struct _qpent *ent = NULL; - rdma_peer_t *peer = NULL; - rdma_qpreg_t *qpreg = NULL; - int32_t hash = 0; + struct _qpent *ent = NULL; + gf_rdma_peer_t *peer = NULL; + gf_rdma_qpreg_t *qpreg = NULL; + int32_t hash = 0; qpreg = &device->qpreg; hash = qp_num % 42; @@ -2212,34 +2854,16 @@ __rdma_lookup_peer (rdma_device_t *device, int32_t qp_num) return peer; } -/* - static rdma_peer_t * - rdma_lookup_peer (rdma_device_t *device, - int32_t qp_num) - { - rdma_qpreg_t *qpreg = NULL; - rdma_peer_t *peer = NULL; - - qpreg = &device->qpreg; - pthread_mutex_lock (&qpreg->lock); - { - peer = __rdma_lookup_peer (device, qp_num); - } - pthread_mutex_unlock (&qpreg->lock); - - return peer; - } -*/ - static void -__rdma_destroy_qp (rpc_transport_t *this) +__gf_rdma_destroy_qp (rpc_transport_t *this) { - rdma_private_t *priv = this->private; + gf_rdma_private_t *priv = NULL; + priv = this->private; if (priv->peer.qp) { - rdma_unregister_peer (priv->device, priv->peer.qp->qp_num); - ibv_destroy_qp (priv->peer.qp); + gf_rdma_unregister_peer (priv->device, priv->peer.qp->qp_num); + rdma_destroy_qp (priv->peer.cm_id); } priv->peer.qp = NULL; @@ -2248,15 +2872,38 @@ __rdma_destroy_qp (rpc_transport_t *this) static int32_t -rdma_create_qp (rpc_transport_t *this) +gf_rdma_create_qp (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - rdma_options_t *options = &priv->options; - rdma_device_t *device = priv->device; - int32_t ret = 0; - rdma_peer_t *peer; + gf_rdma_private_t *priv = NULL; + gf_rdma_device_t *device = NULL; + int32_t ret = 0; + gf_rdma_peer_t *peer = NULL; + char *device_name = NULL; + + priv = this->private; peer = &priv->peer; + + device_name = (char *)ibv_get_device_name (peer->cm_id->verbs->device); + if (device_name == NULL) { + ret = -1; + gf_log (this->name, GF_LOG_WARNING, "cannot get device_name"); + goto out; + } + + device = gf_rdma_get_device (this, peer->cm_id->verbs, + device_name); + if (device == NULL) { + ret = -1; + gf_log (this->name, GF_LOG_WARNING, "cannot get device for " + "device %s", device_name); + goto out; + } + + if (priv->device == NULL) { + priv->device = device; + } + struct ibv_qp_init_attr init_attr = { .send_cq = device->send_cq, .recv_cq = device->recv_cq, @@ -2269,334 +2916,74 @@ rdma_create_qp (rpc_transport_t *this) }, .qp_type = IBV_QPT_RC }; - - struct ibv_qp_attr attr = { - .qp_state = IBV_QPS_INIT, - .pkey_index = 0, - .port_num = options->port, - .qp_access_flags - = IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE - }; - - peer->qp = ibv_create_qp (device->pd, &init_attr); - if (!peer->qp) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "%s: could not create QP", - this->name); - ret = -1; - goto out; - } else if (ibv_modify_qp (peer->qp, &attr, - IBV_QP_STATE | - IBV_QP_PKEY_INDEX | - IBV_QP_PORT | - IBV_QP_ACCESS_FLAGS)) { - gf_log (RDMA_LOG_NAME, - GF_LOG_ERROR, - "%s: failed to modify QP to INIT state", - this->name); + + ret = rdma_create_qp(peer->cm_id, device->pd, &init_attr); + if (ret != 0) { + gf_log (peer->trans->name, GF_LOG_CRITICAL, + "%s: could not create QP (%s)", this->name, + strerror (errno)); ret = -1; goto out; } - peer->local_lid = rdma_get_local_lid (device->context, - options->port); - peer->local_qpn = peer->qp->qp_num; - peer->local_psn = lrand48 () & 0xffffff; + peer->qp = peer->cm_id->qp; - ret = rdma_register_peer (device, peer->qp->qp_num, peer); + ret = gf_rdma_register_peer (device, peer->qp->qp_num, peer); out: if (ret == -1) - __rdma_destroy_qp (this); + __gf_rdma_destroy_qp (this); return ret; } -static void -rdma_destroy_posts (rpc_transport_t *this) -{ - -} - - static int32_t -__rdma_create_posts (rpc_transport_t *this, int32_t count, int32_t size, - rdma_queue_t *q, rdma_post_type_t type) +__gf_rdma_teardown (rpc_transport_t *this) { - int32_t i; - int32_t ret = 0; - rdma_private_t *priv = this->private; - rdma_device_t *device = priv->device; + gf_rdma_private_t *priv = NULL; + gf_rdma_peer_t *peer = NULL; - for (i=0 ; i<count ; i++) { - rdma_post_t *post; - - post = rdma_new_post (device, size + 2048, type); - if (!post) { - gf_log (RDMA_LOG_NAME, - GF_LOG_ERROR, - "%s: post creation failed", - this->name); - ret = -1; - break; - } + priv = this->private; + peer = &priv->peer; - rdma_put_post (q, post); + if (peer->cm_id->qp != NULL) { + __gf_rdma_destroy_qp (this); } - return ret; -} - - -static int32_t -rdma_create_posts (rpc_transport_t *this) -{ - int32_t i, ret; - rdma_post_t *post = NULL; - rdma_private_t *priv = this->private; - rdma_options_t *options = &priv->options; - rdma_device_t *device = priv->device; - - ret = __rdma_create_posts (this, options->send_count, - options->send_size, - &device->sendq, RDMA_SEND_POST); - if (!ret) - ret = __rdma_create_posts (this, options->recv_count, - options->recv_size, - &device->recvq, RDMA_RECV_POST); - if (!ret) { - for (i=0 ; i<options->recv_count ; i++) { - post = rdma_get_post (&device->recvq); - if (rdma_post_recv (device->srq, post) != 0) { - ret = -1; - break; - } - } + if (!list_empty (&priv->peer.ioq)) { + __gf_rdma_ioq_flush (peer); } - if (ret) - rdma_destroy_posts (this); - - return ret; -} - - -static int32_t -rdma_connect_qp (rpc_transport_t *this) -{ - rdma_private_t *priv = this->private; - rdma_options_t *options = &priv->options; - struct ibv_qp_attr attr = { - .qp_state = IBV_QPS_RTR, - .path_mtu = options->mtu, - .dest_qp_num = priv->peer.remote_qpn, - .rq_psn = priv->peer.remote_psn, - .max_dest_rd_atomic = 1, - .min_rnr_timer = 12, - .qp_access_flags - = IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE, - .ah_attr = { - .is_global = 0, - .dlid = priv->peer.remote_lid, - .sl = 0, - .src_path_bits = 0, - .port_num = options->port - } - }; - if (ibv_modify_qp (priv->peer.qp, &attr, - IBV_QP_STATE | - IBV_QP_AV | - IBV_QP_PATH_MTU | - IBV_QP_DEST_QPN | - IBV_QP_RQ_PSN | - IBV_QP_MAX_DEST_RD_ATOMIC | - IBV_QP_MIN_RNR_TIMER)) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "Failed to modify QP to RTR\n"); - return -1; - } - - /* TODO: make timeout and retry_cnt configurable from options */ - attr.qp_state = IBV_QPS_RTS; - attr.timeout = 14; - attr.retry_cnt = 7; - attr.rnr_retry = 7; - attr.sq_psn = priv->peer.local_psn; - attr.max_rd_atomic = 1; - if (ibv_modify_qp (priv->peer.qp, &attr, - IBV_QP_STATE | - IBV_QP_TIMEOUT | - IBV_QP_RETRY_CNT | - IBV_QP_RNR_RETRY | - IBV_QP_SQ_PSN | - IBV_QP_MAX_QP_RD_ATOMIC)) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "Failed to modify QP to RTS\n"); - return -1; + if (peer->cm_id != NULL) { + rdma_destroy_id (peer->cm_id); + peer->cm_id = NULL; } + /* TODO: decrement cq size */ return 0; } + static int32_t -__rdma_teardown (rpc_transport_t *this) +gf_rdma_teardown (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - - __rdma_destroy_qp (this); + int32_t ret = 0; + gf_rdma_private_t *priv = NULL; - if (!list_empty (&priv->peer.ioq)) { - __rdma_ioq_flush (&priv->peer); + if (this == NULL) { + goto out; } - /* TODO: decrement cq size */ - return 0; -} - -/* - * return value: - * 0 = success (completed) - * -1 = error - * > 0 = incomplete - */ - -static int -__tcp_rwv (rpc_transport_t *this, struct iovec *vector, int count, - struct iovec **pending_vector, int *pending_count, - int write) -{ - rdma_private_t *priv = NULL; - int sock = -1; - int ret = -1; - struct iovec *opvector = vector; - int opcount = count; - int moved = 0; - priv = this->private; - sock = priv->sock; - while (opcount) + pthread_mutex_lock (&priv->write_mutex); { - if (write) - { - ret = writev (sock, opvector, opcount); - - if (ret == 0 || (ret == -1 && errno == EAGAIN)) - { - /* done for now */ - break; - } - } - else - { - ret = readv (sock, opvector, opcount); - - if (ret == -1 && errno == EAGAIN) - { - /* done for now */ - break; - } - } - - if (ret == 0) - { - gf_log (this->name, GF_LOG_DEBUG, - "EOF from peer %s", this->peerinfo.identifier); - opcount = -1; - errno = ENOTCONN; - break; - } - - if (ret == -1) - { - if (errno == EINTR) - continue; - - gf_log (this->name, GF_LOG_DEBUG, - "%s failed (%s)", write ? "writev" : "readv", - strerror (errno)); - if (write && !priv->connected && - (errno == ECONNREFUSED)) - gf_log (this->name, GF_LOG_ERROR, - "possible mismatch of 'rpc-transport-type'" - " in protocol server and client. " - "check volume file"); - opcount = -1; - break; - } - - moved = 0; - - while (moved < ret) - { - if ((ret - moved) >= opvector[0].iov_len) - { - moved += opvector[0].iov_len; - opvector++; - opcount--; - } - else - { - opvector[0].iov_len -= (ret - moved); - opvector[0].iov_base += (ret - moved); - moved += (ret - moved); - } - while (opcount && !opvector[0].iov_len) - { - opvector++; - opcount--; - } - } - } - - if (pending_vector) - *pending_vector = opvector; - - if (pending_count) - *pending_count = opcount; - - return opcount; -} - - -static int -__tcp_readv (rpc_transport_t *this, struct iovec *vector, int count, - struct iovec **pending_vector, int *pending_count) -{ - int ret = -1; - - ret = __tcp_rwv (this, vector, count, - pending_vector, pending_count, 0); - - return ret; -} - - -static int -__tcp_writev (rpc_transport_t *this, struct iovec *vector, int count, - struct iovec **pending_vector, int *pending_count) -{ - int ret = -1; - rdma_private_t *priv = this->private; - - ret = __tcp_rwv (this, vector, count, pending_vector, - pending_count, 1); - - if (ret > 0) { - /* TODO: Avoid multiple calls when socket is already - registered for POLLOUT */ - priv->idx = event_select_on (this->ctx->event_pool, - priv->sock, priv->idx, -1, 1); - } else if (ret == 0) { - priv->idx = event_select_on (this->ctx->event_pool, - priv->sock, - priv->idx, -1, 0); + ret = __gf_rdma_teardown (this); } + pthread_mutex_unlock (&priv->write_mutex); +out: return ret; } @@ -2608,12 +2995,12 @@ __tcp_writev (rpc_transport_t *this, struct iovec *vector, int count, * event is sent to upper layers. */ int32_t -rdma_get_write_chunklist (char **ptr, rdma_write_array_t **write_ary) +gf_rdma_get_write_chunklist (char **ptr, gf_rdma_write_array_t **write_ary) { - rdma_write_array_t *from = NULL, *to = NULL; - int32_t ret = -1, size = 0, i = 0; + gf_rdma_write_array_t *from = NULL, *to = NULL; + int32_t ret = -1, size = 0, i = 0; - from = (rdma_write_array_t *) *ptr; + from = (gf_rdma_write_array_t *) *ptr; if (from->wc_discrim == 0) { ret = 0; goto out; @@ -2622,11 +3009,10 @@ rdma_get_write_chunklist (char **ptr, rdma_write_array_t **write_ary) from->wc_nchunks = ntoh32 (from->wc_nchunks); size = sizeof (*from) - + (sizeof (rdma_write_chunk_t) * from->wc_nchunks); + + (sizeof (gf_rdma_write_chunk_t) * from->wc_nchunks); to = GF_CALLOC (1, size, gf_common_mt_char); if (to == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); ret = -1; goto out; } @@ -2657,13 +3043,13 @@ out: * rdma-reads and hence readchunk-list can point to memory held by post. */ int32_t -rdma_get_read_chunklist (char **ptr, rdma_read_chunk_t **readch) +gf_rdma_get_read_chunklist (char **ptr, gf_rdma_read_chunk_t **readch) { - int32_t ret = -1; - rdma_read_chunk_t *chunk = NULL; - int i = 0; + int32_t ret = -1; + gf_rdma_read_chunk_t *chunk = NULL; + int i = 0; - chunk = (rdma_read_chunk_t *)*ptr; + chunk = (gf_rdma_read_chunk_t *)*ptr; if (chunk[0].rc_discrim == 0) { ret = 0; goto out; @@ -2689,49 +3075,52 @@ out: inline int32_t -rdma_decode_error_msg (rdma_peer_t *peer, rdma_post_t *post, - size_t bytes_in_post) +gf_rdma_decode_error_msg (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + size_t bytes_in_post) { - rdma_header_t *header = NULL; - struct iobuf *iobuf = NULL; - struct iobref *iobref = NULL; - int32_t ret = -1; - - header = (rdma_header_t *)post->buf; + gf_rdma_header_t *header = NULL; + struct iobuf *iobuf = NULL; + struct iobref *iobref = NULL; + int32_t ret = -1; + struct rpc_msg rpc_msg = {0, }; + + header = (gf_rdma_header_t *)post->buf; header->rm_body.rm_error.rm_type = ntoh32 (header->rm_body.rm_error.rm_type); if (header->rm_body.rm_error.rm_type == ERR_VERS) { - header->rm_body.rm_error.rm_version.rdma_vers_low = - ntoh32 (header->rm_body.rm_error.rm_version.rdma_vers_low); - header->rm_body.rm_error.rm_version.rdma_vers_high = - ntoh32 (header->rm_body.rm_error.rm_version.rdma_vers_high); + header->rm_body.rm_error.rm_version.gf_rdma_vers_low = + ntoh32 (header->rm_body.rm_error.rm_version.gf_rdma_vers_low); + header->rm_body.rm_error.rm_version.gf_rdma_vers_high = + ntoh32 (header->rm_body.rm_error.rm_version.gf_rdma_vers_high); } - iobuf = iobuf_get (peer->trans->ctx->iobuf_pool); + rpc_msg.rm_xid = header->rm_xid; + rpc_msg.rm_direction = REPLY; + rpc_msg.rm_reply.rp_stat = MSG_DENIED; + + iobuf = iobuf_get2 (peer->trans->ctx->iobuf_pool, bytes_in_post); if (iobuf == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); ret = -1; goto out; } post->ctx.iobref = iobref = iobref_new (); if (iobref == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); ret = -1; goto out; } iobref_add (iobref, iobuf); iobuf_unref (iobuf); - /* - * FIXME: construct an appropriate rpc-msg here, what is being sent - * to rpc is not correct. - */ - post->ctx.vector[0].iov_base = iobuf_ptr (iobuf); - post->ctx.vector[0].iov_len = bytes_in_post; - - memcpy (post->ctx.vector[0].iov_base, (char *)post->buf, - post->ctx.vector[0].iov_len); + + ret = rpc_reply_to_xdr (&rpc_msg, iobuf_ptr (iobuf), + iobuf_pagesize (iobuf), &post->ctx.vector[0]); + if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "Failed to create RPC reply"); + goto out; + } + post->ctx.count = 1; iobuf = NULL; @@ -2753,30 +3142,34 @@ out: int32_t -rdma_decode_msg (rdma_peer_t *peer, rdma_post_t *post, - rdma_read_chunk_t **readch, size_t bytes_in_post) +gf_rdma_decode_msg (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + gf_rdma_read_chunk_t **readch, size_t bytes_in_post) { - int32_t ret = -1; - rdma_header_t *header = NULL; - rdma_reply_info_t *reply_info = NULL; - char *ptr = NULL; - rdma_write_array_t *write_ary = NULL; - size_t header_len = 0; + int32_t ret = -1; + gf_rdma_header_t *header = NULL; + gf_rdma_reply_info_t *reply_info = NULL; + char *ptr = NULL; + gf_rdma_write_array_t *write_ary = NULL; + size_t header_len = 0; - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; ptr = (char *)&header->rm_body.rm_chunks[0]; - ret = rdma_get_read_chunklist (&ptr, readch); + ret = gf_rdma_get_read_chunklist (&ptr, readch); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot get read chunklist from msg"); goto out; } /* skip terminator of read-chunklist */ ptr = ptr + sizeof (uint32_t); - ret = rdma_get_write_chunklist (&ptr, &write_ary); + ret = gf_rdma_get_write_chunklist (&ptr, &write_ary); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot get write chunklist from msg"); goto out; } @@ -2784,29 +3177,35 @@ rdma_decode_msg (rdma_peer_t *peer, rdma_post_t *post, ptr = ptr + sizeof (uint32_t); if (write_ary != NULL) { - reply_info = rdma_reply_info_alloc (peer); + reply_info = gf_rdma_reply_info_alloc (peer); if (reply_info == NULL) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "reply_info_alloc failed"); ret = -1; goto out; } - reply_info->type = rdma_writech; + reply_info->type = gf_rdma_writech; reply_info->wc_array = write_ary; reply_info->rm_xid = header->rm_xid; } else { - ret = rdma_get_write_chunklist (&ptr, &write_ary); + ret = gf_rdma_get_write_chunklist (&ptr, &write_ary); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot get reply chunklist from msg"); goto out; } if (write_ary != NULL) { - reply_info = rdma_reply_info_alloc (peer); + reply_info = gf_rdma_reply_info_alloc (peer); if (reply_info == NULL) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "reply_info_alloc_failed"); ret = -1; goto out; } - reply_info->type = rdma_replych; + reply_info->type = gf_rdma_replych; reply_info->wc_array = write_ary; reply_info->rm_xid = header->rm_xid; } @@ -2814,17 +3213,18 @@ rdma_decode_msg (rdma_peer_t *peer, rdma_post_t *post, /* skip terminator of reply chunk */ ptr = ptr + sizeof (uint32_t); - if (header->rm_type != RDMA_NOMSG) { - post->ctx.hdr_iobuf = iobuf_get (peer->trans->ctx->iobuf_pool); + if (header->rm_type != GF_RDMA_NOMSG) { + header_len = (long)ptr - (long)post->buf; + post->ctx.vector[0].iov_len = (bytes_in_post - header_len); + + post->ctx.hdr_iobuf = iobuf_get2 (peer->trans->ctx->iobuf_pool, + (bytes_in_post - header_len)); if (post->ctx.hdr_iobuf == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); ret = -1; goto out; } - header_len = (long)ptr - (long)post->buf; post->ctx.vector[0].iov_base = iobuf_ptr (post->ctx.hdr_iobuf); - post->ctx.vector[0].iov_len = bytes_in_post - header_len; memcpy (post->ctx.vector[0].iov_base, ptr, post->ctx.vector[0].iov_len); post->ctx.count = 1; @@ -2838,9 +3238,7 @@ out: *readch = NULL; } - if (write_ary != NULL) { - GF_FREE (write_ary); - } + GF_FREE (write_ary); } return ret; @@ -2849,13 +3247,13 @@ out: /* Assumes only one of either write-chunklist or a reply chunk is present */ int32_t -rdma_decode_header (rdma_peer_t *peer, rdma_post_t *post, - rdma_read_chunk_t **readch, size_t bytes_in_post) +gf_rdma_decode_header (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + gf_rdma_read_chunk_t **readch, size_t bytes_in_post) { - int32_t ret = -1; - rdma_header_t *header = NULL; + int32_t ret = -1; + gf_rdma_header_t *header = NULL; - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; header->rm_xid = ntoh32 (header->rm_xid); header->rm_vers = ntoh32 (header->rm_vers); @@ -2863,31 +3261,39 @@ rdma_decode_header (rdma_peer_t *peer, rdma_post_t *post, header->rm_type = ntoh32 (header->rm_type); switch (header->rm_type) { - case RDMA_MSG: - case RDMA_NOMSG: - ret = rdma_decode_msg (peer, post, readch, bytes_in_post); + case GF_RDMA_MSG: + case GF_RDMA_NOMSG: + ret = gf_rdma_decode_msg (peer, post, readch, bytes_in_post); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot decode msg of type (%d)", + header->rm_type); + } + break; - case RDMA_MSGP: - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "rdma msg of msg-type RDMA_MSGP should not have been " - "recieved"); + case GF_RDMA_MSGP: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma msg of msg-type GF_RDMA_MSGP should not have " + "been received"); ret = -1; break; - case RDMA_DONE: - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "rdma msg of msg-type RDMA_DONE should not have been " - "recieved"); + case GF_RDMA_DONE: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma msg of msg-type GF_RDMA_DONE should not have " + "been received"); ret = -1; break; - case RDMA_ERROR: - /* ret = rdma_decode_error_msg (peer, post, bytes_in_post); */ + case GF_RDMA_ERROR: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "received a msg of type RDMA_ERROR"); + ret = gf_rdma_decode_error_msg (peer, post, bytes_in_post); break; default: - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "unknown rdma msg-type (%d)", header->rm_type); } @@ -2896,15 +3302,17 @@ rdma_decode_header (rdma_peer_t *peer, rdma_post_t *post, int32_t -__rdma_read (rdma_peer_t *peer, rdma_post_t *post, struct iovec *to, - rdma_read_chunk_t *readch) +__gf_rdma_read (gf_rdma_peer_t *peer, gf_rdma_post_t *post, struct iovec *to, + gf_rdma_read_chunk_t *readch) { - int32_t ret = -1; + int32_t ret = -1; struct ibv_sge list = {0, }; - struct ibv_send_wr wr = {0, }, *bad_wr = NULL; + struct ibv_send_wr wr = {0, }, *bad_wr = NULL; - ret = __rdma_register_local_mr_for_rdma (peer, to, 1, &post->ctx); + ret = __gf_rdma_register_local_mr_for_rdma (peer, to, 1, &post->ctx); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "registering local memory for rdma read failed"); goto out; } @@ -2912,7 +3320,7 @@ __rdma_read (rdma_peer_t *peer, rdma_post_t *post, struct iovec *to, list.length = to->iov_len; list.lkey = post->ctx.mr[post->ctx.mr_count - 1]->lkey; - wr.wr_id = (unsigned long) rdma_post_ref (post); + wr.wr_id = (unsigned long) gf_rdma_post_ref (post); wr.sg_list = &list; wr.num_sge = 1; wr.opcode = IBV_WR_RDMA_READ; @@ -2922,12 +3330,13 @@ __rdma_read (rdma_peer_t *peer, rdma_post_t *post, struct iovec *to, ret = ibv_post_send (peer->qp, &wr, &bad_wr); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, "rdma read from client " + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma read from client " "(%s) failed with ret = %d (%s)", peer->trans->peerinfo.identifier, ret, (ret > 0) ? strerror (ret) : ""); ret = -1; - rdma_post_unref (post); + gf_rdma_post_unref (post); } out: return ret; @@ -2935,13 +3344,14 @@ out: int32_t -rdma_do_reads (rdma_peer_t *peer, rdma_post_t *post, rdma_read_chunk_t *readch) +gf_rdma_do_reads (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + gf_rdma_read_chunk_t *readch) { - int32_t ret = -1, i = 0, count = 0; - size_t size = 0; - char *ptr = NULL; - struct iobuf *iobuf = NULL; - rdma_private_t *priv = NULL; + int32_t ret = -1, i = 0, count = 0; + size_t size = 0; + char *ptr = NULL; + struct iobuf *iobuf = NULL; + gf_rdma_private_t *priv = NULL; priv = peer->trans->private; @@ -2950,26 +3360,16 @@ rdma_do_reads (rdma_peer_t *peer, rdma_post_t *post, rdma_read_chunk_t *readch) } if (i == 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "message type specified as rdma-read but there are no " "rdma read-chunks present"); goto out; } - post->ctx.rdma_reads = i; + post->ctx.gf_rdma_reads = i; - if (size > peer->trans->ctx->page_size) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "total size of rdma-read (%lu) is greater than " - "page-size (%lu). This is not supported till variable " - "sized iobufs are implemented", (unsigned long)size, - (unsigned long)peer->trans->ctx->page_size); - goto out; - } - - iobuf = iobuf_get (peer->trans->ctx->iobuf_pool); + iobuf = iobuf_get2 (peer->trans->ctx->iobuf_pool, size); if (iobuf == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } @@ -2977,7 +3377,6 @@ rdma_do_reads (rdma_peer_t *peer, rdma_post_t *post, rdma_read_chunk_t *readch) post->ctx.iobref = iobref_new (); if (post->ctx.iobref == NULL) { iobuf_unref (iobuf); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } } @@ -2991,6 +3390,10 @@ rdma_do_reads (rdma_peer_t *peer, rdma_post_t *post, rdma_read_chunk_t *readch) pthread_mutex_lock (&priv->write_mutex); { if (!priv->connected) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "transport not connected to peer (%s), " + "not doing rdma reads", + peer->trans->peerinfo.identifier); goto unlock; } @@ -3000,10 +3403,13 @@ rdma_do_reads (rdma_peer_t *peer, rdma_post_t *post, rdma_read_chunk_t *readch) post->ctx.vector[count].iov_len = readch[i].rc_target.rs_length; - ret = __rdma_read (peer, post, - &post->ctx.vector[count], - &readch[i]); + ret = __gf_rdma_read (peer, post, + &post->ctx.vector[count], + &readch[i]); if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma read from peer (%s) failed", + peer->trans->peerinfo.identifier); goto unlock; } @@ -3027,16 +3433,16 @@ out: int32_t -rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) +gf_rdma_pollin_notify (gf_rdma_peer_t *peer, gf_rdma_post_t *post) { - int32_t ret = -1; - enum msg_type msg_type = 0; - struct rpc_req *rpc_req = NULL; - rdma_request_context_t *request_context = NULL; - rpc_request_info_t request_info = {0, }; - rdma_private_t *priv = NULL; - uint32_t *ptr = NULL; - rpc_transport_pollin_t *pollin = NULL; + int32_t ret = -1; + enum msg_type msg_type = 0; + struct rpc_req *rpc_req = NULL; + gf_rdma_request_context_t *request_context = NULL; + rpc_request_info_t request_info = {0, }; + gf_rdma_private_t *priv = NULL; + uint32_t *ptr = NULL; + rpc_transport_pollin_t *pollin = NULL; if ((peer == NULL) || (post == NULL)) { goto out; @@ -3045,16 +3451,16 @@ rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) if (post->ctx.iobref == NULL) { post->ctx.iobref = iobref_new (); if (post->ctx.iobref == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } - /* handling the case where both hdr and payload of - * GF_FOP_READ_CBK were recieved in a single iobuf + /* handling the case where both hdr and payload of + * GF_FOP_READ_CBK were received in a single iobuf * because of server sending entire msg as inline without * doing rdma writes. */ - iobref_add (post->ctx.iobref, post->ctx.hdr_iobuf); + if (post->ctx.hdr_iobuf) + iobref_add (post->ctx.iobref, post->ctx.hdr_iobuf); } pollin = rpc_transport_pollin_alloc (peer->trans, @@ -3064,7 +3470,6 @@ rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) post->ctx.iobref, post->ctx.reply_info); if (pollin == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); goto out; } @@ -3078,15 +3483,15 @@ rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) RPC_TRANSPORT_MAP_XID_REQUEST, &request_info); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, "cannot get request information from rpc " - "layer"); + "layer"); goto out; } rpc_req = request_info.rpc_req; if (rpc_req == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, "rpc request structure not found"); ret = -1; goto out; @@ -3099,11 +3504,11 @@ rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) if (request_context != NULL) { pthread_mutex_lock (&priv->write_mutex); { - __rdma_request_context_destroy (request_context); + __gf_rdma_request_context_destroy (request_context); } pthread_mutex_unlock (&priv->write_mutex); } else { - rdma_quota_put (peer); + gf_rdma_quota_put (peer); } pollin->is_reply = 1; @@ -3111,6 +3516,10 @@ rdma_pollin_notify (rdma_peer_t *peer, rdma_post_t *post) ret = rpc_transport_notify (peer->trans, RPC_TRANSPORT_MSG_RECEIVED, pollin); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "transport_notify failed"); + } out: if (pollin != NULL) { @@ -3123,19 +3532,19 @@ out: int32_t -rdma_recv_reply (rdma_peer_t *peer, rdma_post_t *post) +gf_rdma_recv_reply (gf_rdma_peer_t *peer, gf_rdma_post_t *post) { - int32_t ret = -1; - rdma_header_t *header = NULL; - rdma_reply_info_t *reply_info = NULL; - rdma_write_array_t *wc_array = NULL; - int i = 0; - uint32_t *ptr = NULL; - rdma_request_context_t *ctx = NULL; - rpc_request_info_t request_info = {0, }; - struct rpc_req *rpc_req = NULL; - - header = (rdma_header_t *)post->buf; + int32_t ret = -1; + gf_rdma_header_t *header = NULL; + gf_rdma_reply_info_t *reply_info = NULL; + gf_rdma_write_array_t *wc_array = NULL; + int i = 0; + uint32_t *ptr = NULL; + gf_rdma_request_context_t *ctx = NULL; + rpc_request_info_t request_info = {0, }; + struct rpc_req *rpc_req = NULL; + + header = (gf_rdma_header_t *)post->buf; reply_info = post->ctx.reply_info; /* no write chunklist, just notify upper layers */ @@ -3146,7 +3555,7 @@ rdma_recv_reply (rdma_peer_t *peer, rdma_post_t *post) wc_array = reply_info->wc_array; - if (header->rm_type == RDMA_NOMSG) { + if (header->rm_type == GF_RDMA_NOMSG) { post->ctx.vector[0].iov_base = (void *)(long)wc_array->wc_array[0].wc_target.rs_offset; post->ctx.vector[0].iov_len @@ -3160,7 +3569,7 @@ rdma_recv_reply (rdma_peer_t *peer, rdma_post_t *post) post->ctx.vector[i + 1].iov_len = wc_array->wc_array[i].wc_target.rs_length; } - + post->ctx.count += wc_array->wc_nchunks; } @@ -3171,32 +3580,36 @@ rdma_recv_reply (rdma_peer_t *peer, rdma_post_t *post) RPC_TRANSPORT_MAP_XID_REQUEST, &request_info); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "cannot get request information from rpc " - "layer"); + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "cannot get request information (peer:%s) from rpc " + "layer", peer->trans->peerinfo.identifier); goto out; } rpc_req = request_info.rpc_req; if (rpc_req == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "rpc request structure not found"); ret = -1; goto out; } ctx = rpc_req->conn_private; - if (post->ctx.iobref == NULL) { + if ((post->ctx.iobref == NULL) && ctx->rsp_iobref) { post->ctx.iobref = iobref_ref (ctx->rsp_iobref); } ret = 0; - rdma_reply_info_destroy (reply_info); + gf_rdma_reply_info_destroy (reply_info); out: if (ret == 0) { - ret = rdma_pollin_notify (peer, post); + ret = gf_rdma_pollin_notify (peer, post); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "pollin notify failed"); + } } return ret; @@ -3204,17 +3617,22 @@ out: inline int32_t -rdma_recv_request (rdma_peer_t *peer, rdma_post_t *post, - rdma_read_chunk_t *readch) +gf_rdma_recv_request (gf_rdma_peer_t *peer, gf_rdma_post_t *post, + gf_rdma_read_chunk_t *readch) { int32_t ret = -1; - + if (readch != NULL) { - ret = rdma_do_reads (peer, post, readch); + ret = gf_rdma_do_reads (peer, post, readch); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma read from peer (%s) failed", + peer->trans->peerinfo.identifier); + } } else { - ret = rdma_pollin_notify (peer, post); + ret = gf_rdma_pollin_notify (peer, post); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "pollin notification failed"); } } @@ -3223,38 +3641,59 @@ rdma_recv_request (rdma_peer_t *peer, rdma_post_t *post, } void -rdma_process_recv (rdma_peer_t *peer, struct ibv_wc *wc) +gf_rdma_process_recv (gf_rdma_peer_t *peer, struct ibv_wc *wc) { - rdma_post_t *post = NULL; - rdma_read_chunk_t *readch = NULL; - int ret = -1; - uint32_t *ptr = NULL; - enum msg_type msg_type = 0; - rdma_header_t *header = NULL; - - post = (rdma_post_t *) (long) wc->wr_id; + gf_rdma_post_t *post = NULL; + gf_rdma_read_chunk_t *readch = NULL; + int ret = -1; + uint32_t *ptr = NULL; + enum msg_type msg_type = 0; + gf_rdma_header_t *header = NULL; + gf_rdma_private_t *priv = NULL; + + post = (gf_rdma_post_t *) (long) wc->wr_id; if (post == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "no post found in successful work completion element"); goto out; } - ret = rdma_decode_header (peer, post, &readch, wc->byte_len); + ret = gf_rdma_decode_header (peer, post, &readch, wc->byte_len); if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "decoding of header failed"); goto out; } - header = (rdma_header_t *)post->buf; + header = (gf_rdma_header_t *)post->buf; + + priv = peer->trans->private; + + pthread_mutex_lock (&priv->write_mutex); + { + if (!priv->peer.quota_set) { + priv->peer.quota_set = 1; + + /* Initially peer.quota is set to 1 as per RFC 5666. We + * have to account for the quota used while sending + * first msg (which may or may not be returned to pool + * at this point) while deriving peer.quota from + * header->rm_credit. Hence the arithmatic below, + * instead of directly setting it to header->rm_credit. + */ + priv->peer.quota = header->rm_credit + - ( 1 - priv->peer.quota); + } + } + pthread_mutex_unlock (&priv->write_mutex); switch (header->rm_type) { - case RDMA_MSG: + case GF_RDMA_MSG: ptr = (uint32_t *)post->ctx.vector[0].iov_base; msg_type = ntoh32 (*(ptr + 1)); break; - case RDMA_NOMSG: + case GF_RDMA_NOMSG: if (readch != NULL) { msg_type = CALL; } else { @@ -3262,31 +3701,49 @@ rdma_process_recv (rdma_peer_t *peer, struct ibv_wc *wc) } break; - case RDMA_ERROR: - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "an error has happened while transmission of msg, " - "disconnecting the transport"); - rpc_transport_disconnect (peer->trans); - goto out; - -/* ret = rdma_pollin_notify (peer, post); - if (ret == -1) { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "pollin notification failed"); - } - goto out; -*/ + case GF_RDMA_ERROR: + if (header->rm_body.rm_error.rm_type == ERR_CHUNK) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "peer (%s), couldn't encode or decode the msg " + "properly or write chunks were not provided " + "for replies that were bigger than " + "RDMA_INLINE_THRESHOLD (%d)", + peer->trans->peerinfo.identifier, + GLUSTERFS_RDMA_INLINE_THRESHOLD); + ret = gf_rdma_pollin_notify (peer, post); + if (ret == -1) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, + "pollin notification failed"); + } + goto out; + } else { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, + "an error has happened while transmission of " + "msg, disconnecting the transport"); + ret = -1; + goto out; + } default: - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "invalid rdma msg-type (%d)", header->rm_type); - break; + goto out; } if (msg_type == CALL) { - ret = rdma_recv_request (peer, post, readch); + ret = gf_rdma_recv_request (peer, post, readch); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "receiving a request from peer (%s) failed", + peer->trans->peerinfo.identifier); + } } else { - ret = rdma_recv_reply (peer, post); + ret = gf_rdma_recv_reply (peer, post); + if (ret < 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "receiving a reply from peer (%s) failed", + peer->trans->peerinfo.identifier); + } } out: @@ -3297,14 +3754,50 @@ out: return; } +void * +gf_rdma_async_event_thread (void *context) +{ + struct ibv_async_event event; + int ret; + + while (1) { + do { + ret = ibv_get_async_event((struct ibv_context *)context, + &event); + + if (ret && errno != EINTR) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "Error getting event (%s)", + strerror (errno)); + } + } while(ret && errno == EINTR); + + switch (event.event_type) { + case IBV_EVENT_SRQ_LIMIT_REACHED: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "recieved srq_limit reached"); + break; + + default: + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, + "event (%d) recieved", event.event_type); + break; + } + + ibv_ack_async_event(&event); + } + + return 0; +} + static void * -rdma_recv_completion_proc (void *data) +gf_rdma_recv_completion_proc (void *data) { struct ibv_comp_channel *chan = NULL; - rdma_device_t *device = NULL;; - rdma_post_t *post = NULL; - rdma_peer_t *peer = NULL; + gf_rdma_device_t *device = NULL;; + gf_rdma_post_t *post = NULL; + gf_rdma_peer_t *peer = NULL; struct ibv_cq *event_cq = NULL; struct ibv_wc wc = {0, }; void *event_ctx = NULL; @@ -3315,36 +3808,36 @@ rdma_recv_completion_proc (void *data) while (1) { ret = ibv_get_cq_event (chan, &event_cq, &event_ctx); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_get_cq_event failed, terminating recv " "thread %d (%d)", ret, errno); continue; } device = event_ctx; - + ret = ibv_req_notify_cq (event_cq, 0); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_req_notify_cq on %s failed, terminating " "recv thread: %d (%d)", device->device_name, ret, errno); continue; } - device = (rdma_device_t *) event_ctx; + device = (gf_rdma_device_t *) event_ctx; while ((ret = ibv_poll_cq (event_cq, 1, &wc)) > 0) { - post = (rdma_post_t *) (long) wc.wr_id; + post = (gf_rdma_post_t *) (long) wc.wr_id; pthread_mutex_lock (&device->qpreg.lock); { - peer = __rdma_lookup_peer (device, - wc.qp_num); + peer = __gf_rdma_lookup_peer (device, + wc.qp_num); /* * keep a refcount on transport so that it - * doesnot get freed because of some error + * does not get freed because of some error * indicated by wc.status till we are done * with usage of peer and thereby that of trans. */ @@ -3355,7 +3848,7 @@ rdma_recv_completion_proc (void *data) pthread_mutex_unlock (&device->qpreg.lock); if (wc.status != IBV_WC_SUCCESS) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "recv work request on `%s' returned " "error (%d)", device->device_name, wc.status); @@ -3365,26 +3858,26 @@ rdma_recv_completion_proc (void *data) } if (post) { - rdma_post_unref (post); + gf_rdma_post_unref (post); } continue; } if (peer) { - rdma_process_recv (peer, &wc); + gf_rdma_process_recv (peer, &wc); rpc_transport_unref (peer->trans); } else { - gf_log (RDMA_LOG_NAME, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, "could not lookup peer for qp_num: %d", wc.qp_num); } - rdma_post_unref (post); + gf_rdma_post_unref (post); } - + if (ret < 0) { - gf_log (RDMA_LOG_NAME, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_poll_cq on `%s' returned error " "(ret = %d, errno = %d)", @@ -3399,11 +3892,11 @@ rdma_recv_completion_proc (void *data) void -rdma_handle_failed_send_completion (rdma_peer_t *peer, struct ibv_wc *wc) +gf_rdma_handle_failed_send_completion (gf_rdma_peer_t *peer, struct ibv_wc *wc) { - rdma_post_t *post = NULL; - rdma_device_t *device = NULL; - rdma_private_t *priv = NULL; + gf_rdma_post_t *post = NULL; + gf_rdma_device_t *device = NULL; + gf_rdma_private_t *priv = NULL; if (peer != NULL) { priv = peer->trans->private; @@ -3413,13 +3906,13 @@ rdma_handle_failed_send_completion (rdma_peer_t *peer, struct ibv_wc *wc) } - post = (rdma_post_t *) (long) wc->wr_id; + post = (gf_rdma_post_t *) (long) wc->wr_id; - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "send work request on `%s' returned error " "wc.status = %d, wc.vendor_err = %d, post->buf = %p, " "wc.byte_len = %d, post->reused = %d", - (device != NULL) ? device->device_name : NULL, wc->status, + (device != NULL) ? device->device_name : NULL, wc->status, wc->vendor_err, post->buf, wc->byte_len, post->reused); if (wc->status == IBV_WC_RETRY_EXC_ERR) { @@ -3440,20 +3933,22 @@ rdma_handle_failed_send_completion (rdma_peer_t *peer, struct ibv_wc *wc) void -rdma_handle_successful_send_completion (rdma_peer_t *peer, struct ibv_wc *wc) +gf_rdma_handle_successful_send_completion (gf_rdma_peer_t *peer, + struct ibv_wc *wc) { - rdma_post_t *post = NULL; - int reads = 0, ret = 0; + gf_rdma_post_t *post = NULL; + int reads = 0, ret = 0; + gf_rdma_header_t *header = NULL; if (wc->opcode != IBV_WC_RDMA_READ) { goto out; } - post = (rdma_post_t *)(long) wc->wr_id; + post = (gf_rdma_post_t *)(long) wc->wr_id; pthread_mutex_lock (&post->lock); { - reads = --post->ctx.rdma_reads; + reads = --post->ctx.gf_rdma_reads; } pthread_mutex_unlock (&post->lock); @@ -3462,8 +3957,15 @@ rdma_handle_successful_send_completion (rdma_peer_t *peer, struct ibv_wc *wc) goto out; } - ret = rdma_pollin_notify (peer, post); - if (ret == -1) { + header = (gf_rdma_header_t *)post->buf; + + if (header->rm_type == GF_RDMA_NOMSG) { + post->ctx.count = 1; + post->ctx.vector[0].iov_len += post->ctx.vector[1].iov_len; + } + + ret = gf_rdma_pollin_notify (peer, post); + if ((ret == -1) && (peer != NULL)) { rpc_transport_disconnect (peer->trans); } @@ -3473,33 +3975,33 @@ out: static void * -rdma_send_completion_proc (void *data) +gf_rdma_send_completion_proc (void *data) { struct ibv_comp_channel *chan = NULL; - rdma_post_t *post = NULL; - rdma_peer_t *peer = NULL; + gf_rdma_post_t *post = NULL; + gf_rdma_peer_t *peer = NULL; struct ibv_cq *event_cq = NULL; void *event_ctx = NULL; - rdma_device_t *device = NULL; + gf_rdma_device_t *device = NULL; struct ibv_wc wc = {0, }; char is_request = 0; int32_t ret = 0, quota_ret = 0; - + chan = data; while (1) { ret = ibv_get_cq_event (chan, &event_cq, &event_ctx); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_get_cq_event on failed, terminating " "send thread: %d (%d)", ret, errno); continue; } - + device = event_ctx; ret = ibv_req_notify_cq (event_cq, 0); if (ret) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_req_notify_cq on %s failed, terminating " "send thread: %d (%d)", device->device_name, ret, errno); @@ -3507,11 +4009,11 @@ rdma_send_completion_proc (void *data) } while ((ret = ibv_poll_cq (event_cq, 1, &wc)) > 0) { - post = (rdma_post_t *) (long) wc.wr_id; + post = (gf_rdma_post_t *) (long) wc.wr_id; pthread_mutex_lock (&device->qpreg.lock); { - peer = __rdma_lookup_peer (device, wc.qp_num); + peer = __gf_rdma_lookup_peer (device, wc.qp_num); /* * keep a refcount on transport so that it @@ -3526,32 +4028,32 @@ rdma_send_completion_proc (void *data) pthread_mutex_unlock (&device->qpreg.lock); if (wc.status != IBV_WC_SUCCESS) { - rdma_handle_failed_send_completion (peer, &wc); + gf_rdma_handle_failed_send_completion (peer, &wc); } else { - rdma_handle_successful_send_completion (peer, - &wc); + gf_rdma_handle_successful_send_completion (peer, + &wc); } if (post) { is_request = post->ctx.is_request; - ret = rdma_post_unref (post); + ret = gf_rdma_post_unref (post); if ((ret == 0) && (wc.status == IBV_WC_SUCCESS) && !is_request - && (post->type == RDMA_SEND_POST) + && (post->type == GF_RDMA_SEND_POST) && (peer != NULL)) { - /* An RDMA_RECV_POST can end up in - * rdma_send_completion_proc for + /* An GF_RDMA_RECV_POST can end up in + * gf_rdma_send_completion_proc for * rdma-reads, and we do not take - * quota for getting an RDMA_RECV_POST. + * quota for getting an GF_RDMA_RECV_POST. */ - /* + /* * if it is request, quota is returned * after reply has come. */ - quota_ret = rdma_quota_put (peer); + quota_ret = gf_rdma_quota_put (peer); if (quota_ret < 0) { gf_log ("rdma", GF_LOG_DEBUG, "failed to send " @@ -3563,21 +4065,21 @@ rdma_send_completion_proc (void *data) if (peer) { rpc_transport_unref (peer->trans); } else { - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_DEBUG, "could not lookup peer for qp_num: %d", wc.qp_num); } } if (ret < 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_ERROR, "ibv_poll_cq on `%s' returned error (ret = %d," " errno = %d)", device->device_name, ret, errno); continue; } - ibv_ack_cq_events (event_cq, 1); + ibv_ack_cq_events (event_cq, 1); } return NULL; @@ -3585,19 +4087,24 @@ rdma_send_completion_proc (void *data) static void -rdma_options_init (rpc_transport_t *this) +gf_rdma_options_init (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - rdma_options_t *options = &priv->options; - int32_t mtu; - data_t *temp; + gf_rdma_private_t *priv = NULL; + gf_rdma_options_t *options = NULL; + int32_t mtu = 0; + data_t *temp = NULL; /* TODO: validate arguments from options below */ + priv = this->private; + options = &priv->options; options->send_size = GLUSTERFS_RDMA_INLINE_THRESHOLD;/*this->ctx->page_size * 4; 512 KB*/ options->recv_size = GLUSTERFS_RDMA_INLINE_THRESHOLD;/*this->ctx->page_size * 4; 512 KB*/ options->send_count = 4096; options->recv_count = 4096; + options->attr_timeout = GF_RDMA_TIMEOUT; + options->attr_retry_cnt = GF_RDMA_RETRY_CNT; + options->attr_rnr_retry = GF_RDMA_RNR_RETRY; temp = dict_get (this->options, "transport.rdma.work-request-send-count"); @@ -3607,7 +4114,22 @@ rdma_options_init (rpc_transport_t *this) temp = dict_get (this->options, "transport.rdma.work-request-recv-count"); if (temp) - options->recv_count = data_to_int32 (temp); + options->recv_count = data_to_int32 (temp); + + temp = dict_get (this->options, "transport.rdma.attr-timeout"); + + if (temp) + options->attr_timeout = data_to_uint8 (temp); + + temp = dict_get (this->options, "transport.rdma.attr-retry-cnt"); + + if (temp) + options->attr_retry_cnt = data_to_uint8 (temp); + + temp = dict_get (this->options, "transport.rdma.attr-rnr-retry"); + + if (temp) + options->attr_rnr_retry = data_to_uint8 (temp); options->port = 1; temp = dict_get (this->options, @@ -3633,12 +4155,12 @@ rdma_options_init (rpc_transport_t *this) break; default: if (temp) - gf_log (RDMA_LOG_NAME, GF_LOG_WARNING, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, "%s: unrecognized MTU value '%s', defaulting " "to '2048'", this->name, data_to_str (temp)); else - gf_log (RDMA_LOG_NAME, GF_LOG_TRACE, + gf_log (GF_RDMA_LOG_NAME, GF_LOG_TRACE, "%s: defaulting MTU to '2048'", this->name); options->mtu = IBV_MTU_2048; @@ -3653,1117 +4175,250 @@ rdma_options_init (rpc_transport_t *this) return; } -static void -rdma_queue_init (rdma_queue_t *queue) -{ - pthread_mutex_init (&queue->lock, NULL); - - queue->active_posts.next = &queue->active_posts; - queue->active_posts.prev = &queue->active_posts; - queue->passive_posts.next = &queue->passive_posts; - queue->passive_posts.prev = &queue->passive_posts; -} - -static rdma_device_t * -rdma_get_device (rpc_transport_t *this, - struct ibv_context *ibctx) +gf_rdma_ctx_t * +__gf_rdma_ctx_create (void) { - glusterfs_ctx_t *ctx = NULL; - rdma_private_t *priv = NULL; - rdma_options_t *options = NULL; - char *device_name = NULL; - uint32_t port = 0; - uint8_t active_port = 0; - int32_t ret = 0; - int32_t i = 0; - - rdma_device_t *trav = NULL; + gf_rdma_ctx_t *rdma_ctx = NULL; + int ret = -1; - priv = this->private; - options = &priv->options; - device_name = priv->options.device_name; - ctx = this->ctx; - trav = ctx->ib; - port = priv->options.port; - - while (trav) { - if ((!strcmp (trav->device_name, device_name)) && - (trav->port == port)) - break; - trav = trav->next; + rdma_ctx = GF_CALLOC (1, sizeof (*rdma_ctx), gf_common_mt_char); + if (rdma_ctx == NULL) { + goto out; } - if (!trav) { - - trav = GF_CALLOC (1, sizeof (*trav), - gf_common_mt_rdma_device_t); - if (trav == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - return NULL; - } - - priv->device = trav; - - trav->context = ibctx; - - ret = ib_get_active_port (trav->context); - - if (ret < 0) { - if (!port) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "Failed to find any active ports and " - "none specified in volume file," - " exiting"); - GF_FREE (trav); - return NULL; - } - } - - trav->request_ctx_pool = mem_pool_new (rdma_request_context_t, - RDMA_POOL_SIZE); - if (trav->request_ctx_pool == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - return NULL; - } - - trav->ioq_pool = mem_pool_new (rdma_ioq_t, RDMA_POOL_SIZE); - if (trav->ioq_pool == NULL) { - mem_pool_destroy (trav->request_ctx_pool); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - return NULL; - } - - trav->reply_info_pool = mem_pool_new (rdma_reply_info_t, - RDMA_POOL_SIZE); - if (trav->reply_info_pool == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->ioq_pool); - return NULL; - } - - - active_port = ret; - - if (port) { - ret = ib_check_active_port (trav->context, port); - if (ret < 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_WARNING, - "On device %s: provided port:%u is " - "found to be offline, continuing to " - "use the same port", device_name, port); - } - } else { - priv->options.port = active_port; - port = active_port; - gf_log (RDMA_LOG_NAME, GF_LOG_TRACE, - "Port unspecified in volume file using active " - "port: %u", port); - } - - trav->device_name = gf_strdup (device_name); - trav->port = port; - - trav->next = ctx->ib; - ctx->ib = trav; - - trav->send_chan = ibv_create_comp_channel (trav->context); - if (!trav->send_chan) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not create send completion channel", - device_name); - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - return NULL; - } - - trav->recv_chan = ibv_create_comp_channel (trav->context); - if (!trav->recv_chan) { - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "could not create recv completion channel"); - /* TODO: cleanup current mess */ - return NULL; - } - - if (rdma_create_cq (this) < 0) { - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not create CQ", - this->name); - return NULL; - } - - /* protection domain */ - trav->pd = ibv_alloc_pd (trav->context); - - if (!trav->pd) { - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - rdma_destroy_cq (this); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not allocate protection domain", - this->name); - return NULL; - } - - struct ibv_srq_init_attr attr = { - .attr = { - .max_wr = options->recv_count, - .max_sge = 1 - } - }; - trav->srq = ibv_create_srq (trav->pd, &attr); - - if (!trav->srq) { - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_dealloc_pd (trav->pd); - rdma_destroy_cq (this); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not create SRQ", - this->name); - return NULL; - } + rdma_ctx->rdma_cm_event_channel = rdma_create_event_channel (); + if (rdma_ctx->rdma_cm_event_channel == NULL) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "rdma_cm event channel creation failed (%s)", + strerror (errno)); + goto out; + } - /* queue init */ - rdma_queue_init (&trav->sendq); - rdma_queue_init (&trav->recvq); - - if (rdma_create_posts (this) < 0) { - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_dealloc_pd (trav->pd); - rdma_destroy_cq (this); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not allocate posts", - this->name); - return NULL; - } + ret = gf_thread_create (&rdma_ctx->rdma_cm_thread, NULL, + gf_rdma_cm_event_handler, + rdma_ctx->rdma_cm_event_channel); + if (ret != 0) { + gf_log (GF_RDMA_LOG_NAME, GF_LOG_WARNING, + "creation of thread to handle rdma-cm events " + "failed (%s)", strerror (ret)); + goto out; + } - /* completion threads */ - ret = pthread_create (&trav->send_thread, - NULL, - rdma_send_completion_proc, - trav->send_chan); - if (ret) { - rdma_destroy_posts (this); - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_dealloc_pd (trav->pd); - rdma_destroy_cq (this); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "could not create send completion thread"); - return NULL; +out: + if (ret < 0) { + if (rdma_ctx->rdma_cm_event_channel != NULL) { + rdma_destroy_event_channel (rdma_ctx->rdma_cm_event_channel); } - ret = pthread_create (&trav->recv_thread, - NULL, - rdma_recv_completion_proc, - trav->recv_chan); - if (ret) { - rdma_destroy_posts (this); - mem_pool_destroy (trav->ioq_pool); - mem_pool_destroy (trav->request_ctx_pool); - mem_pool_destroy (trav->reply_info_pool); - ibv_dealloc_pd (trav->pd); - rdma_destroy_cq (this); - ibv_destroy_comp_channel (trav->recv_chan); - ibv_destroy_comp_channel (trav->send_chan); - GF_FREE ((char *)trav->device_name); - GF_FREE (trav); - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "could not create recv completion thread"); - return NULL; - } - - /* qpreg */ - pthread_mutex_init (&trav->qpreg.lock, NULL); - for (i=0; i<42; i++) { - trav->qpreg.ents[i].next = &trav->qpreg.ents[i]; - trav->qpreg.ents[i].prev = &trav->qpreg.ents[i]; - } + GF_FREE (rdma_ctx); + rdma_ctx = NULL; } - return trav; + + return rdma_ctx; } -static int32_t -rdma_init (rpc_transport_t *this) +static int32_t +gf_rdma_init (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - rdma_options_t *options = &priv->options; - struct ibv_device **dev_list; - struct ibv_context *ib_ctx = NULL; - int32_t ret = 0; + gf_rdma_private_t *priv = NULL; + int32_t ret = 0; + glusterfs_ctx_t *ctx = NULL; + gf_rdma_options_t *options = NULL; - rdma_options_init (this); + ctx= this->ctx; - { - dev_list = ibv_get_device_list (NULL); - - if (!dev_list) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "Failed to get IB devices"); - ret = -1; - goto cleanup; - } - - if (!*dev_list) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "No IB devices found"); - ret = -1; - goto cleanup; - } - - if (!options->device_name) { - if (*dev_list) { - options->device_name = - gf_strdup (ibv_get_device_name (*dev_list)); - } else { - gf_log (RDMA_LOG_NAME, GF_LOG_CRITICAL, - "IB device list is empty. Check for " - "'ib_uverbs' module"); - return -1; - goto cleanup; - } - } - - while (*dev_list) { - if (!strcmp (ibv_get_device_name (*dev_list), - options->device_name)) { - ib_ctx = ibv_open_device (*dev_list); - - if (!ib_ctx) { - gf_log (RDMA_LOG_NAME, - GF_LOG_ERROR, - "Failed to get infiniband" - "device context"); - ret = -1; - goto cleanup; - } - break; - } - ++dev_list; - } + priv = this->private; - priv->device = rdma_get_device (this, ib_ctx); + ibv_fork_init (); + gf_rdma_options_init (this); - if (!priv->device) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "could not create rdma device for %s", - options->device_name); - ret = -1; - goto cleanup; - } - } + options = &priv->options; + priv->peer.send_count = options->send_count; + priv->peer.recv_count = options->recv_count; + priv->peer.send_size = options->send_size; + priv->peer.recv_size = options->recv_size; priv->peer.trans = this; INIT_LIST_HEAD (&priv->peer.ioq); - - pthread_mutex_init (&priv->read_mutex, NULL); + pthread_mutex_init (&priv->write_mutex, NULL); pthread_mutex_init (&priv->recv_mutex, NULL); pthread_cond_init (&priv->recv_cond, NULL); -cleanup: - if (-1 == ret) { - if (ib_ctx) - ibv_close_device (ib_ctx); - } - - if (dev_list) - ibv_free_device_list (dev_list); - - return ret; -} - - -static int32_t -rdma_disconnect (rpc_transport_t *this) -{ - rdma_private_t *priv = this->private; - int32_t ret = 0; - - pthread_mutex_lock (&priv->write_mutex); - { - ret = __rdma_disconnect (this); - } - pthread_mutex_unlock (&priv->write_mutex); - - return ret; -} - - -static int32_t -__tcp_connect_finish (int fd) -{ - int ret = -1; - int optval = 0; - socklen_t optlen = sizeof (int); - - ret = getsockopt (fd, SOL_SOCKET, SO_ERROR, - (void *)&optval, &optlen); - - if (ret == 0 && optval) - { - errno = optval; - ret = -1; - } - - return ret; -} - -static inline void -rdma_fill_handshake_data (char *buf, struct rdma_nbio *nbio, - rdma_private_t *priv) -{ - sprintf (buf, - "QP1:RECV_BLKSIZE=%08x:SEND_BLKSIZE=%08x\n" - "QP1:LID=%04x:QPN=%06x:PSN=%06x\n", - priv->peer.recv_size, - priv->peer.send_size, - priv->peer.local_lid, - priv->peer.local_qpn, - priv->peer.local_psn); - - nbio->vector.iov_base = buf; - nbio->vector.iov_len = strlen (buf) + 1; - nbio->count = 1; - return; -} - -static inline void -rdma_fill_handshake_ack (char *buf, struct rdma_nbio *nbio) -{ - sprintf (buf, "DONE\n"); - nbio->vector.iov_base = buf; - nbio->vector.iov_len = strlen (buf) + 1; - nbio->count = 1; - return; -} - -static int -rdma_handshake_pollin (rpc_transport_t *this) -{ - int ret = 0; - rdma_private_t *priv = this->private; - char *buf = priv->handshake.incoming.buf; - int32_t recv_buf_size, send_buf_size; - socklen_t sock_len; - - if (priv->handshake.incoming.state == RDMA_HANDSHAKE_COMPLETE) { - return -1; - } - - pthread_mutex_lock (&priv->write_mutex); + pthread_mutex_lock (&ctx->lock); { - while (priv->handshake.incoming.state != RDMA_HANDSHAKE_COMPLETE) - { - switch (priv->handshake.incoming.state) - { - case RDMA_HANDSHAKE_START: - buf = priv->handshake.incoming.buf = GF_CALLOC (1, 256, gf_common_mt_char); - rdma_fill_handshake_data (buf, &priv->handshake.incoming, priv); - buf[0] = 0; - priv->handshake.incoming.state = RDMA_HANDSHAKE_RECEIVING_DATA; - break; - - case RDMA_HANDSHAKE_RECEIVING_DATA: - ret = __tcp_readv (this, - &priv->handshake.incoming.vector, - priv->handshake.incoming.count, - &priv->handshake.incoming.pending_vector, - &priv->handshake.incoming.pending_count); - if (ret == -1) { - goto unlock; - } - - if (ret > 0) { - gf_log (this->name, GF_LOG_TRACE, - "partial header read on NB socket. continue later"); - goto unlock; - } - - if (!ret) { - priv->handshake.incoming.state = RDMA_HANDSHAKE_RECEIVED_DATA; - } - break; - - case RDMA_HANDSHAKE_RECEIVED_DATA: - ret = sscanf (buf, - "QP1:RECV_BLKSIZE=%08x:SEND_BLKSIZE=%08x\n" - "QP1:LID=%04x:QPN=%06x:PSN=%06x\n", - &recv_buf_size, - &send_buf_size, - &priv->peer.remote_lid, - &priv->peer.remote_qpn, - &priv->peer.remote_psn); - - if ((ret != 5) && (strncmp (buf, "QP1:", 4))) { - gf_log (RDMA_LOG_NAME, - GF_LOG_CRITICAL, - "%s: remote-host(%s)'s " - "transport type is different", - this->name, - this->peerinfo.identifier); - ret = -1; - goto unlock; - } - - if (recv_buf_size < priv->peer.recv_size) - priv->peer.recv_size = recv_buf_size; - if (send_buf_size < priv->peer.send_size) - priv->peer.send_size = send_buf_size; - - gf_log (RDMA_LOG_NAME, GF_LOG_TRACE, - "%s: transacted recv_size=%d " - "send_size=%d", - this->name, priv->peer.recv_size, - priv->peer.send_size); - - priv->peer.quota = priv->peer.send_count; - - if (rdma_connect_qp (this)) { - gf_log (RDMA_LOG_NAME, - GF_LOG_ERROR, - "%s: failed to connect with " - "remote QP", this->name); - ret = -1; - goto unlock; - } - rdma_fill_handshake_ack (buf, &priv->handshake.incoming); - buf[0] = 0; - priv->handshake.incoming.state = RDMA_HANDSHAKE_RECEIVING_ACK; - break; - - case RDMA_HANDSHAKE_RECEIVING_ACK: - ret = __tcp_readv (this, - &priv->handshake.incoming.vector, - priv->handshake.incoming.count, - &priv->handshake.incoming.pending_vector, - &priv->handshake.incoming.pending_count); - if (ret == -1) { - goto unlock; - } - - if (ret > 0) { - gf_log (this->name, GF_LOG_TRACE, - "partial header read on NB " - "socket. continue later"); - goto unlock; - } - - if (!ret) { - priv->handshake.incoming.state = RDMA_HANDSHAKE_RECEIVED_ACK; - } - break; - - case RDMA_HANDSHAKE_RECEIVED_ACK: - if (strncmp (buf, "DONE", 4)) { - gf_log (RDMA_LOG_NAME, - GF_LOG_DEBUG, - "%s: handshake-3 did not " - "return 'DONE' (%s)", - this->name, buf); - ret = -1; - goto unlock; - } - ret = 0; - priv->connected = 1; - sock_len = sizeof (struct sockaddr_storage); - getpeername (priv->sock, - (struct sockaddr *) &this->peerinfo.sockaddr, - &sock_len); - - GF_FREE (priv->handshake.incoming.buf); - priv->handshake.incoming.buf = NULL; - priv->handshake.incoming.state = RDMA_HANDSHAKE_COMPLETE; + if (ctx->ib == NULL) { + ctx->ib = __gf_rdma_ctx_create (); + if (ctx->ib == NULL) { + ret = -1; } } } -unlock: - pthread_mutex_unlock (&priv->write_mutex); - - if (ret == -1) { - rpc_transport_disconnect (this); - } else { - ret = 0; - } - - - if (!ret && priv->connected) { - if (priv->is_server) { - ret = rpc_transport_notify (priv->listener, - RPC_TRANSPORT_ACCEPT, - this); - } else { - ret = rpc_transport_notify (this, RPC_TRANSPORT_CONNECT, - this); - } - } + pthread_mutex_unlock (&ctx->lock); return ret; } -static int -rdma_handshake_pollout (rpc_transport_t *this) -{ - rdma_private_t *priv = this->private; - char *buf = priv->handshake.outgoing.buf; - int32_t ret = 0; - - if (priv->handshake.outgoing.state == RDMA_HANDSHAKE_COMPLETE) { - return 0; - } - - pthread_mutex_unlock (&priv->write_mutex); - { - while (priv->handshake.outgoing.state != RDMA_HANDSHAKE_COMPLETE) - { - switch (priv->handshake.outgoing.state) - { - case RDMA_HANDSHAKE_START: - buf = priv->handshake.outgoing.buf = GF_CALLOC (1, 256, gf_common_mt_char); - rdma_fill_handshake_data (buf, &priv->handshake.outgoing, priv); - priv->handshake.outgoing.state = RDMA_HANDSHAKE_SENDING_DATA; - break; - - case RDMA_HANDSHAKE_SENDING_DATA: - ret = __tcp_writev (this, - &priv->handshake.outgoing.vector, - priv->handshake.outgoing.count, - &priv->handshake.outgoing.pending_vector, - &priv->handshake.outgoing.pending_count); - if (ret == -1) { - goto unlock; - } - - if (ret > 0) { - gf_log (this->name, GF_LOG_TRACE, - "partial header read on NB socket. continue later"); - goto unlock; - } - - if (!ret) { - priv->handshake.outgoing.state = RDMA_HANDSHAKE_SENT_DATA; - } - break; - - case RDMA_HANDSHAKE_SENT_DATA: - rdma_fill_handshake_ack (buf, &priv->handshake.outgoing); - priv->handshake.outgoing.state = RDMA_HANDSHAKE_SENDING_ACK; - break; - - case RDMA_HANDSHAKE_SENDING_ACK: - ret = __tcp_writev (this, - &priv->handshake.outgoing.vector, - priv->handshake.outgoing.count, - &priv->handshake.outgoing.pending_vector, - &priv->handshake.outgoing.pending_count); - - if (ret == -1) { - goto unlock; - } - - if (ret > 0) { - gf_log (this->name, GF_LOG_TRACE, - "partial header read on NB " - "socket. continue later"); - goto unlock; - } - - if (!ret) { - GF_FREE (priv->handshake.outgoing.buf); - priv->handshake.outgoing.buf = NULL; - priv->handshake.outgoing.state = RDMA_HANDSHAKE_COMPLETE; - } - break; - } - } - } -unlock: - pthread_mutex_unlock (&priv->write_mutex); - - if (ret == -1) { - rpc_transport_disconnect (this); - } else { - ret = 0; - } - - return ret; -} -static int -rdma_handshake_pollerr (rpc_transport_t *this) +static int32_t +gf_rdma_disconnect (rpc_transport_t *this) { - rdma_private_t *priv = this->private; - int32_t ret = 0; - char need_unref = 0, connected = 0; - - gf_log (RDMA_LOG_NAME, GF_LOG_DEBUG, - "%s: peer disconnected, cleaning up", - this->name); - - pthread_mutex_lock (&priv->write_mutex); - { - __rdma_teardown (this); - - connected = priv->connected; - if (priv->sock != -1) { - event_unregister (this->ctx->event_pool, - priv->sock, priv->idx); - need_unref = 1; - - if (close (priv->sock) != 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "close () - error: %s", - strerror (errno)); - ret = -errno; - } - priv->tcp_connected = priv->connected = 0; - priv->sock = -1; - } - - if (priv->handshake.incoming.buf) { - GF_FREE (priv->handshake.incoming.buf); - priv->handshake.incoming.buf = NULL; - } - - priv->handshake.incoming.state = RDMA_HANDSHAKE_START; - - if (priv->handshake.outgoing.buf) { - GF_FREE (priv->handshake.outgoing.buf); - priv->handshake.outgoing.buf = NULL; - } - - priv->handshake.outgoing.state = RDMA_HANDSHAKE_START; - } - pthread_mutex_unlock (&priv->write_mutex); + gf_rdma_private_t *priv = NULL; + int32_t ret = 0; - if (connected) { - rpc_transport_notify (this, RPC_TRANSPORT_DISCONNECT, this); - } - - if (need_unref) - rpc_transport_unref (this); - - return 0; -} - - -static int -tcp_connect_finish (rpc_transport_t *this) -{ - rdma_private_t *priv = this->private; - int error = 0, ret = 0; + priv = this->private; + gf_log_callingfn (this->name, GF_LOG_WARNING, + "disconnect called (peer:%s)", + this->peerinfo.identifier); pthread_mutex_lock (&priv->write_mutex); { - ret = __tcp_connect_finish (priv->sock); - - if (!ret) { - this->myinfo.sockaddr_len = - sizeof (this->myinfo.sockaddr); - ret = getsockname (priv->sock, - (struct sockaddr *)&this->myinfo.sockaddr, - &this->myinfo.sockaddr_len); - if (ret == -1) - { - gf_log (this->name, GF_LOG_ERROR, - "getsockname on new client-socket %d " - "failed (%s)", - priv->sock, strerror (errno)); - close (priv->sock); - error = 1; - goto unlock; - } - - gf_rdma_get_transport_identifiers (this); - priv->tcp_connected = 1; - } - - if (ret == -1 && errno != EINPROGRESS) { - gf_log (this->name, GF_LOG_ERROR, - "tcp connect to %s failed (%s)", - this->peerinfo.identifier, strerror (errno)); - error = 1; - } + ret = __gf_rdma_disconnect (this); } -unlock: pthread_mutex_unlock (&priv->write_mutex); - if (error) { - rpc_transport_disconnect (this); - } - return ret; } -static int -rdma_event_handler (int fd, int idx, void *data, - int poll_in, int poll_out, int poll_err) -{ - rpc_transport_t *this = data; - rdma_private_t *priv = this->private; - rdma_options_t *options = NULL; - int ret = 0; - - if (!priv->tcp_connected) { - ret = tcp_connect_finish (this); - if (priv->tcp_connected) { - options = &priv->options; - - priv->peer.send_count = options->send_count; - priv->peer.recv_count = options->recv_count; - priv->peer.send_size = options->send_size; - priv->peer.recv_size = options->recv_size; - - if ((ret = rdma_create_qp (this)) < 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not create QP", - this->name); - rpc_transport_disconnect (this); - } - } - } - - if (!ret && poll_out && priv->tcp_connected) { - ret = rdma_handshake_pollout (this); - } - - if (!ret && poll_in && priv->tcp_connected) { - if (priv->handshake.incoming.state == RDMA_HANDSHAKE_COMPLETE) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: pollin received on tcp socket (peer: %s) " - "after handshake is complete", - this->name, this->peerinfo.identifier); - rdma_handshake_pollerr (this); - return 0; - } - ret = rdma_handshake_pollin (this); - } - - if (ret < 0 || poll_err) { - ret = rdma_handshake_pollerr (this); - } - - return 0; -} - -static int -__tcp_nonblock (int fd) -{ - int flags = 0; - int ret = -1; - - flags = fcntl (fd, F_GETFL); - - if (flags != -1) - ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK); - - return ret; -} static int32_t -rdma_connect (struct rpc_transport *this, int port) +gf_rdma_connect (struct rpc_transport *this, int port) { - dict_t *options = this->options; - - rdma_private_t *priv = this->private; - - int32_t ret = 0; - gf_boolean_t non_blocking = 1; - struct sockaddr_storage sockaddr; - socklen_t sockaddr_len = 0; + gf_rdma_private_t *priv = NULL; + int32_t ret = 0; + union gf_sock_union sock_union = {{0, }, }; + socklen_t sockaddr_len = 0; + gf_rdma_peer_t *peer = NULL; + gf_rdma_ctx_t *rdma_ctx = NULL; + gf_boolean_t connected = _gf_false; - if (priv->connected) { - return 0; - } + priv = this->private; - if (dict_get (options, "non-blocking-io")) { - char *nb_connect = data_to_str (dict_get (this->options, - "non-blocking-io")); - - if (gf_string2boolean (nb_connect, &non_blocking) == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'non-blocking-io' takes only boolean " - "options, not taking any action"); - non_blocking = 1; - } - } + peer = &priv->peer; + + rpc_transport_ref (this); ret = gf_rdma_client_get_remote_sockaddr (this, - (struct sockaddr *)&sockaddr, + &sock_union.sa, &sockaddr_len, port); if (ret != 0) { gf_log (this->name, GF_LOG_DEBUG, "cannot get remote address to connect"); - return ret; + goto out; } + rdma_ctx = this->ctx->ib; + pthread_mutex_lock (&priv->write_mutex); { - if (priv->sock != -1) { - ret = 0; + if (peer->cm_id != NULL) { + ret = -1; + errno = EINPROGRESS; + connected = _gf_true; goto unlock; } - - priv->sock = socket (((struct sockaddr *)&sockaddr)->sa_family, - SOCK_STREAM, 0); - - if (priv->sock == -1) { + + priv->entity = GF_RDMA_CLIENT; + + ret = rdma_create_id (rdma_ctx->rdma_cm_event_channel, + &peer->cm_id, this, RDMA_PS_TCP); + if (ret != 0) { gf_log (this->name, GF_LOG_ERROR, - "socket () - error: %s", strerror (errno)); + "creation of rdma_cm_id failed (%s)", + strerror (errno)); ret = -errno; goto unlock; } - gf_log (this->name, GF_LOG_TRACE, - "socket fd = %d", priv->sock); - - memcpy (&this->peerinfo.sockaddr, &sockaddr, sockaddr_len); + memcpy (&this->peerinfo.sockaddr, &sock_union.storage, + sockaddr_len); this->peerinfo.sockaddr_len = sockaddr_len; if (port > 0) - ((struct sockaddr_in *) (&sockaddr))->sin_port - = htons (port); + sock_union.sin.sin_port = htons (port); - ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family = + ((struct sockaddr *) &this->myinfo.sockaddr)->sa_family = ((struct sockaddr *)&this->peerinfo.sockaddr)->sa_family; - if (non_blocking) - { - ret = __tcp_nonblock (priv->sock); - - if (ret == -1) - { - gf_log (this->name, GF_LOG_ERROR, - "could not set socket %d to non " - "blocking mode (%s)", - priv->sock, strerror (errno)); - close (priv->sock); - priv->sock = -1; - goto unlock; - } - } - ret = gf_rdma_client_bind (this, (struct sockaddr *)&this->myinfo.sockaddr, - &this->myinfo.sockaddr_len, priv->sock); - if (ret == -1) - { + &this->myinfo.sockaddr_len, + peer->cm_id); + if (ret != 0) { gf_log (this->name, GF_LOG_WARNING, "client bind failed: %s", strerror (errno)); - close (priv->sock); - priv->sock = -1; goto unlock; } - ret = connect (priv->sock, - (struct sockaddr *)&this->peerinfo.sockaddr, - this->peerinfo.sockaddr_len); - if (ret == -1 && errno != EINPROGRESS) - { - gf_log (this->name, GF_LOG_ERROR, - "connection attempt failed (%s)", + ret = rdma_resolve_addr (peer->cm_id, NULL, &sock_union.sa, + 2000); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "rdma_resolve_addr failed (%s)", strerror (errno)); - close (priv->sock); - priv->sock = -1; goto unlock; } - priv->tcp_connected = priv->connected = 0; - - rpc_transport_ref (this); - - priv->handshake.incoming.state = RDMA_HANDSHAKE_START; - priv->handshake.outgoing.state = RDMA_HANDSHAKE_START; - - priv->idx = event_register (this->ctx->event_pool, - priv->sock, rdma_event_handler, - this, 1, 1); + priv->connected = 0; } unlock: pthread_mutex_unlock (&priv->write_mutex); - return ret; -} - -static int -rdma_server_event_handler (int fd, int idx, void *data, - int poll_in, int poll_out, int poll_err) -{ - int32_t main_sock = -1; - rpc_transport_t *this, *trans = data; - rdma_private_t *priv = NULL; - rdma_private_t *trans_priv = (rdma_private_t *) trans->private; - rdma_options_t *options = NULL; - - if (!poll_in) { - return 0; - } - - this = GF_CALLOC (1, sizeof (rpc_transport_t), - gf_common_mt_rpc_transport_t); - if (this == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - return -1; - } - - this->listener = trans; - - priv = GF_CALLOC (1, sizeof (rdma_private_t), - gf_common_mt_rdma_private_t); - if (priv == NULL) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, "out of memory"); - GF_FREE (priv); - return -1; - } - this->private = priv; - /* Copy all the rdma related values in priv, from trans_priv - as other than QP, all the values remain same */ - priv->device = trans_priv->device; - priv->options = trans_priv->options; - priv->is_server = 1; - priv->listener = trans; - - options = &priv->options; +out: + if (ret != 0) { + if (!connected) { + gf_rdma_teardown (this); + } - this->ops = trans->ops; - this->init = trans->init; - this->fini = trans->fini; - this->ctx = trans->ctx; - this->name = gf_strdup (trans->name); - this->notify = trans->notify; - this->mydata = trans->mydata; - - memcpy (&this->myinfo.sockaddr, &trans->myinfo.sockaddr, - trans->myinfo.sockaddr_len); - this->myinfo.sockaddr_len = trans->myinfo.sockaddr_len; - - main_sock = (trans_priv)->sock; - this->peerinfo.sockaddr_len = sizeof (this->peerinfo.sockaddr); - priv->sock = accept (main_sock, - (struct sockaddr *)&this->peerinfo.sockaddr, - &this->peerinfo.sockaddr_len); - if (priv->sock == -1) { - gf_log ("rdma/server", GF_LOG_ERROR, - "accept() failed: %s", - strerror (errno)); - GF_FREE (this->private); - GF_FREE (this); - return -1; + rpc_transport_unref (this); } - priv->peer.trans = this; - rpc_transport_ref (this); - - gf_rdma_get_transport_identifiers (this); + return ret; +} - priv->tcp_connected = 1; - priv->handshake.incoming.state = RDMA_HANDSHAKE_START; - priv->handshake.outgoing.state = RDMA_HANDSHAKE_START; - priv->peer.send_count = options->send_count; - priv->peer.recv_count = options->recv_count; - priv->peer.send_size = options->send_size; - priv->peer.recv_size = options->recv_size; - INIT_LIST_HEAD (&priv->peer.ioq); +static int32_t +gf_rdma_listen (rpc_transport_t *this) +{ + union gf_sock_union sock_union = {{0, }, }; + socklen_t sockaddr_len = 0; + gf_rdma_private_t *priv = NULL; + gf_rdma_peer_t *peer = NULL; + int ret = 0; + gf_rdma_ctx_t *rdma_ctx = NULL; + char service[NI_MAXSERV], host[NI_MAXHOST]; - if (rdma_create_qp (this) < 0) { - gf_log (RDMA_LOG_NAME, GF_LOG_ERROR, - "%s: could not create QP", - this->name); - rpc_transport_disconnect (this); - return -1; - } + priv = this->private; + peer = &priv->peer; - priv->idx = event_register (this->ctx->event_pool, priv->sock, - rdma_event_handler, this, 1, 1); + priv->entity = GF_RDMA_SERVER_LISTENER; - pthread_mutex_init (&priv->read_mutex, NULL); - pthread_mutex_init (&priv->write_mutex, NULL); - pthread_mutex_init (&priv->recv_mutex, NULL); - /* pthread_cond_init (&priv->recv_cond, NULL); */ - return 0; -} + rdma_ctx = this->ctx->ib; -static int32_t -rdma_listen (rpc_transport_t *this) -{ - struct sockaddr_storage sockaddr; - socklen_t sockaddr_len; - rdma_private_t *priv = this->private; - int opt = 1, ret = 0; - char service[NI_MAXSERV], host[NI_MAXHOST]; - - memset (&sockaddr, 0, sizeof (sockaddr)); - ret = gf_rdma_server_get_local_sockaddr (this, - (struct sockaddr *)&sockaddr, + ret = gf_rdma_server_get_local_sockaddr (this, &sock_union.sa, &sockaddr_len); if (ret != 0) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log (this->name, GF_LOG_WARNING, "cannot find network address of server to bind to"); goto err; } - priv->sock = socket (((struct sockaddr *)&sockaddr)->sa_family, - SOCK_STREAM, 0); - if (priv->sock == -1) { - gf_log ("rdma/server", GF_LOG_CRITICAL, - "init: failed to create socket, error: %s", + ret = rdma_create_id (rdma_ctx->rdma_cm_event_channel, + &peer->cm_id, this, RDMA_PS_TCP); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "creation of rdma_cm_id failed (%s)", strerror (errno)); - GF_FREE (this->private); - ret = -1; goto err; } - memcpy (&this->myinfo.sockaddr, &sockaddr, sockaddr_len); + memcpy (&this->myinfo.sockaddr, &sock_union.storage, + sockaddr_len); this->myinfo.sockaddr_len = sockaddr_len; - ret = getnameinfo ((struct sockaddr *)&this->myinfo.sockaddr, - this->myinfo.sockaddr_len, - host, sizeof (host), + ret = getnameinfo ((struct sockaddr *)&this->myinfo.sockaddr, + this->myinfo.sockaddr_len, host, sizeof (host), service, sizeof (service), NI_NUMERICHOST); if (ret != 0) { @@ -4771,57 +4426,58 @@ rdma_listen (rpc_transport_t *this) "getnameinfo failed (%s)", gai_strerror (ret)); goto err; } + sprintf (this->myinfo.identifier, "%s:%s", host, service); - - setsockopt (priv->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)); - if (bind (priv->sock, - (struct sockaddr *)&sockaddr, - sockaddr_len) != 0) { - ret = -1; - gf_log ("rdma/server", GF_LOG_ERROR, - "init: failed to bind to socket for %s (%s)", - this->myinfo.identifier, strerror (errno)); + + ret = rdma_bind_addr (peer->cm_id, &sock_union.sa); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "rdma_bind_addr failed (%s)", strerror (errno)); goto err; } - if (listen (priv->sock, 10) != 0) { - gf_log ("rdma/server", GF_LOG_ERROR, - "init: listen () failed on socket for %s (%s)", - this->myinfo.identifier, strerror (errno)); - ret = -1; + ret = rdma_listen (peer->cm_id, 10); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "rdma_listen failed (%s)", strerror (errno)); goto err; } - /* Register the main socket */ - priv->idx = event_register (this->ctx->event_pool, priv->sock, - rdma_server_event_handler, - rpc_transport_ref (this), 1, 0); + rpc_transport_ref (this); + ret = 0; err: + if (ret < 0) { + if (peer->cm_id != NULL) { + rdma_destroy_id (peer->cm_id); + peer->cm_id = NULL; + } + } + return ret; } + struct rpc_transport_ops tops = { - .submit_request = rdma_submit_request, - .submit_reply = rdma_submit_reply, - .connect = rdma_connect, - .disconnect = rdma_disconnect, - .listen = rdma_listen, + .submit_request = gf_rdma_submit_request, + .submit_reply = gf_rdma_submit_reply, + .connect = gf_rdma_connect, + .disconnect = gf_rdma_disconnect, + .listen = gf_rdma_listen, }; int32_t init (rpc_transport_t *this) { - rdma_private_t *priv = NULL; + gf_rdma_private_t *priv = NULL; priv = GF_CALLOC (1, sizeof (*priv), gf_common_mt_rdma_private_t); if (!priv) return -1; this->private = priv; - priv->sock = -1; - if (rdma_init (this)) { + if (gf_rdma_init (this)) { gf_log (this->name, GF_LOG_ERROR, "Failed to initialize IB Device"); return -1; @@ -4834,19 +4490,15 @@ void fini (struct rpc_transport *this) { /* TODO: verify this function does graceful finish */ - rdma_private_t *priv = this->private; + gf_rdma_private_t *priv = NULL; + + priv = this->private; + this->private = NULL; if (priv) { pthread_mutex_destroy (&priv->recv_mutex); pthread_mutex_destroy (&priv->write_mutex); - pthread_mutex_destroy (&priv->read_mutex); - - /* pthread_cond_destroy (&priv->recv_cond); */ - if (priv->sock != -1) { - event_unregister (this->ctx->event_pool, - priv->sock, priv->idx); - } gf_log (this->name, GF_LOG_TRACE, "called fini on transport: %p", this); @@ -4858,51 +4510,63 @@ fini (struct rpc_transport *this) /* TODO: expand each option */ struct volume_options options[] = { { .key = {"transport.rdma.port", - "rdma-port"}, + "rdma-port"}, .type = GF_OPTION_TYPE_INT, .min = 1, .max = 4, .description = "check the option by 'ibv_devinfo'" }, { .key = {"transport.rdma.mtu", - "rdma-mtu"}, + "rdma-mtu"}, .type = GF_OPTION_TYPE_INT, }, { .key = {"transport.rdma.device-name", - "rdma-device-name"}, + "rdma-device-name"}, .type = GF_OPTION_TYPE_ANY, .description = "check by 'ibv_devinfo'" }, { .key = {"transport.rdma.work-request-send-count", - "rdma-work-request-send-count"}, + "rdma-work-request-send-count"}, .type = GF_OPTION_TYPE_INT, }, { .key = {"transport.rdma.work-request-recv-count", - "rdma-work-request-recv-count"}, + "rdma-work-request-recv-count"}, .type = GF_OPTION_TYPE_INT, }, - { .key = {"remote-port", + { .key = {"remote-port", "transport.remote-port", - "transport.rdma.remote-port"}, - .type = GF_OPTION_TYPE_INT + "transport.rdma.remote-port"}, + .type = GF_OPTION_TYPE_INT + }, + { .key = {"transport.rdma.attr-timeout", + "rdma-attr-timeout"}, + .type = GF_OPTION_TYPE_INT + }, + { .key = {"transport.rdma.attr-retry-cnt", + "rdma-attr-retry-cnt"}, + .type = GF_OPTION_TYPE_INT + }, + { .key = {"transport.rdma.attr-rnr-retry", + "rdma-attr-rnr-retry"}, + .type = GF_OPTION_TYPE_INT }, - { .key = {"transport.rdma.listen-port", "listen-port"}, - .type = GF_OPTION_TYPE_INT + { .key = {"transport.rdma.listen-port", "listen-port"}, + .type = GF_OPTION_TYPE_INT }, - { .key = {"transport.rdma.connect-path", "connect-path"}, - .type = GF_OPTION_TYPE_ANY + { .key = {"transport.rdma.connect-path", "connect-path"}, + .type = GF_OPTION_TYPE_ANY }, - { .key = {"transport.rdma.bind-path", "bind-path"}, - .type = GF_OPTION_TYPE_ANY + { .key = {"transport.rdma.bind-path", "bind-path"}, + .type = GF_OPTION_TYPE_ANY }, - { .key = {"transport.rdma.listen-path", "listen-path"}, - .type = GF_OPTION_TYPE_ANY + { .key = {"transport.rdma.listen-path", "listen-path"}, + .type = GF_OPTION_TYPE_ANY }, { .key = {"transport.address-family", - "address-family"}, + "address-family"}, .value = {"inet", "inet6", "inet/inet6", "inet6/inet", "unix", "inet-sdp" }, - .type = GF_OPTION_TYPE_STR + .type = GF_OPTION_TYPE_STR }, { .key = {"transport.socket.lowlat"}, .type = GF_OPTION_TYPE_BOOL diff --git a/rpc/rpc-transport/rdma/src/rdma.h b/rpc/rpc-transport/rdma/src/rdma.h index dbd15d668..7f76244f0 100644 --- a/rpc/rpc-transport/rdma/src/rdma.h +++ b/rpc/rpc-transport/rdma/src/rdma.h @@ -1,20 +1,11 @@ /* - Copyright (c) 2006-2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #ifndef _XPORT_RDMA_H @@ -38,53 +29,59 @@ #include <list.h> #include <arpa/inet.h> #include <infiniband/verbs.h> +#include <rdma/rdma_cma.h> /* FIXME: give appropriate values to these macros */ #define GF_DEFAULT_RDMA_LISTEN_PORT (GF_DEFAULT_BASE_PORT + 1) -/* If you are changing RDMA_MAX_SEGMENTS, please make sure to update - * GLUSTERFS_RDMA_MAX_HEADER_SIZE defined in glusterfs.h . +/* If you are changing GF_RDMA_MAX_SEGMENTS, please make sure to update + * GLUSTERFS_GF_RDMA_MAX_HEADER_SIZE defined in glusterfs.h . */ -#define RDMA_MAX_SEGMENTS 8 +#define GF_RDMA_MAX_SEGMENTS 8 -#define RDMA_VERSION 1 -#define RDMA_POOL_SIZE 512 +#define GF_RDMA_VERSION 1 +#define GF_RDMA_POOL_SIZE 512 -typedef enum rdma_errcode { +/* Additional attributes */ +#define GF_RDMA_TIMEOUT 14 +#define GF_RDMA_RETRY_CNT 7 +#define GF_RDMA_RNR_RETRY 7 + +typedef enum gf_rdma_errcode { ERR_VERS = 1, ERR_CHUNK = 2 -}rdma_errcode_t; +}gf_rdma_errcode_t; -struct rdma_err_vers { - uint32_t rdma_vers_low; /* Version range supported by peer */ - uint32_t rdma_vers_high; +struct gf_rdma_err_vers { + uint32_t gf_rdma_vers_low; /* Version range supported by peer */ + uint32_t gf_rdma_vers_high; }__attribute__ ((packed)); -typedef struct rdma_err_vers rdma_err_vers_t; - -typedef enum rdma_proc { - RDMA_MSG = 0, /* An RPC call or reply msg */ - RDMA_NOMSG = 1, /* An RPC call or reply msg - separate body */ - RDMA_MSGP = 2, /* An RPC call or reply msg with padding */ - RDMA_DONE = 3, /* Client signals reply completion */ - RDMA_ERROR = 4 /* An RPC RDMA encoding error */ -}rdma_proc_t; - -typedef enum rdma_chunktype { - rdma_noch = 0, /* no chunk */ - rdma_readch, /* some argument through rdma read */ - rdma_areadch, /* entire request through rdma read */ - rdma_writech, /* some result through rdma write */ - rdma_replych /* entire reply through rdma write */ -}rdma_chunktype_t; - -/* If you are modifying __rdma_header, please make sure to change - * GLUSTERFS_RDMA_MAX_HEADER_SIZE defined in glusterfs.h to reflect your changes +typedef struct gf_rdma_err_vers gf_rdma_err_vers_t; + +typedef enum gf_rdma_proc { + GF_RDMA_MSG = 0, /* An RPC call or reply msg */ + GF_RDMA_NOMSG = 1, /* An RPC call or reply msg - separate body */ + GF_RDMA_MSGP = 2, /* An RPC call or reply msg with padding */ + GF_RDMA_DONE = 3, /* Client signals reply completion */ + GF_RDMA_ERROR = 4 /* An RPC RDMA encoding error */ +}gf_rdma_proc_t; + +typedef enum gf_rdma_chunktype { + gf_rdma_noch = 0, /* no chunk */ + gf_rdma_readch, /* some argument through rdma read */ + gf_rdma_areadch, /* entire request through rdma read */ + gf_rdma_writech, /* some result through rdma write */ + gf_rdma_replych /* entire reply through rdma write */ +}gf_rdma_chunktype_t; + +/* If you are modifying __gf_rdma_header, please make sure to change + * GLUSTERFS_GF_RDMA_MAX_HEADER_SIZE defined in glusterfs.h to reflect your changes */ -struct __rdma_header { +struct __gf_rdma_header { uint32_t rm_xid; /* Mirrors the RPC header xid */ uint32_t rm_vers; /* Version of this protocol */ uint32_t rm_credit; /* Buffers requested/granted */ - uint32_t rm_type; /* Type of message (enum rdma_proc) */ + uint32_t rm_type; /* Type of message (enum gf_rdma_proc) */ union { struct { /* no chunks */ uint32_t rm_empty[3]; /* 3 empty chunk lists */ @@ -98,49 +95,49 @@ struct __rdma_header { struct { uint32_t rm_type; - rdma_err_vers_t rm_version; + gf_rdma_err_vers_t rm_version; }__attribute__ ((packed)) rm_error; uint32_t rm_chunks[0]; /* read, write and reply chunks */ }__attribute__ ((packed)) rm_body; } __attribute__((packed)); -typedef struct __rdma_header rdma_header_t; +typedef struct __gf_rdma_header gf_rdma_header_t; -/* If you are modifying __rdma_segment or __rdma_read_chunk, please make sure - * to change GLUSTERFS_RDMA_MAX_HEADER_SIZE defined in glusterfs.h to reflect +/* If you are modifying __gf_rdma_segment or __gf_rdma_read_chunk, please make sure + * to change GLUSTERFS_GF_RDMA_MAX_HEADER_SIZE defined in glusterfs.h to reflect * your changes. */ -struct __rdma_segment { +struct __gf_rdma_segment { uint32_t rs_handle; /* Registered memory handle */ uint32_t rs_length; /* Length of the chunk in bytes */ uint64_t rs_offset; /* Chunk virtual address or offset */ } __attribute__((packed)); -typedef struct __rdma_segment rdma_segment_t; +typedef struct __gf_rdma_segment gf_rdma_segment_t; /* read chunk(s), encoded as a linked list. */ -struct __rdma_read_chunk { +struct __gf_rdma_read_chunk { uint32_t rc_discrim; /* 1 indicates presence */ uint32_t rc_position; /* Position in XDR stream */ - rdma_segment_t rc_target; + gf_rdma_segment_t rc_target; } __attribute__((packed)); -typedef struct __rdma_read_chunk rdma_read_chunk_t; +typedef struct __gf_rdma_read_chunk gf_rdma_read_chunk_t; /* write chunk, and reply chunk. */ -struct __rdma_write_chunk { - rdma_segment_t wc_target; +struct __gf_rdma_write_chunk { + gf_rdma_segment_t wc_target; } __attribute__((packed)); -typedef struct __rdma_write_chunk rdma_write_chunk_t; +typedef struct __gf_rdma_write_chunk gf_rdma_write_chunk_t; /* write chunk(s), encoded as a counted array. */ -struct __rdma_write_array { +struct __gf_rdma_write_array { uint32_t wc_discrim; /* 1 indicates presence */ uint32_t wc_nchunks; /* Array count */ - struct __rdma_write_chunk wc_array[0]; + struct __gf_rdma_write_chunk wc_array[0]; } __attribute__((packed)); -typedef struct __rdma_write_array rdma_write_array_t; +typedef struct __gf_rdma_write_array gf_rdma_write_array_t; /* options per transport end point */ -struct __rdma_options { +struct __gf_rdma_options { int32_t port; char *device_name; enum ibv_mtu mtu; @@ -148,26 +145,29 @@ struct __rdma_options { int32_t recv_count; uint64_t recv_size; uint64_t send_size; + uint8_t attr_timeout; + uint8_t attr_retry_cnt; + uint8_t attr_rnr_retry; }; -typedef struct __rdma_options rdma_options_t; +typedef struct __gf_rdma_options gf_rdma_options_t; -struct __rdma_reply_info { +struct __gf_rdma_reply_info { uint32_t rm_xid; /* xid in network endian */ - rdma_chunktype_t type; /* - * can be either rdma_replych - * or rdma_writech. + gf_rdma_chunktype_t type; /* + * can be either gf_rdma_replych + * or gf_rdma_writech. */ - rdma_write_array_t *wc_array; + gf_rdma_write_array_t *wc_array; struct mem_pool *pool; }; -typedef struct __rdma_reply_info rdma_reply_info_t; +typedef struct __gf_rdma_reply_info gf_rdma_reply_info_t; -struct __rdma_ioq { +struct __gf_rdma_ioq { union { struct list_head list; struct { - struct __rdma_ioq *next; - struct __rdma_ioq *prev; + struct __gf_rdma_ioq *next; + struct __gf_rdma_ioq *prev; }; }; @@ -182,8 +182,8 @@ struct __rdma_ioq { struct iobref *iobref; union { - struct __rdma_ioq_request { - /* used to build reply_chunk for RDMA_NOMSG type msgs */ + struct __gf_rdma_ioq_request { + /* used to build reply_chunk for GF_RDMA_NOMSG type msgs */ struct iovec rsphdr_vec[MAX_IOVEC]; int rsphdr_count; @@ -200,205 +200,183 @@ struct __rdma_ioq { struct iobref *rsp_iobref; }request; - rdma_reply_info_t *reply_info; + gf_rdma_reply_info_t *reply_info; }msg; struct mem_pool *pool; }; -typedef struct __rdma_ioq rdma_ioq_t; +typedef struct __gf_rdma_ioq gf_rdma_ioq_t; -typedef enum __rdma_send_post_type { - RDMA_SEND_POST_NO_CHUNKLIST, /* post which is sent using rdma-send +typedef enum __gf_rdma_send_post_type { + GF_RDMA_SEND_POST_NO_CHUNKLIST, /* post which is sent using rdma-send * and the msg carries no * chunklists. */ - RDMA_SEND_POST_READ_CHUNKLIST, /* post which is sent using rdma-send + GF_RDMA_SEND_POST_READ_CHUNKLIST, /* post which is sent using rdma-send * and the msg carries only read * chunklist. */ - RDMA_SEND_POST_WRITE_CHUNKLIST, /* post which is sent using + GF_RDMA_SEND_POST_WRITE_CHUNKLIST, /* post which is sent using * rdma-send and the msg carries * only write chunklist. */ - RDMA_SEND_POST_READ_WRITE_CHUNKLIST, /* post which is sent using + GF_RDMA_SEND_POST_READ_WRITE_CHUNKLIST, /* post which is sent using * rdma-send and the msg * carries both read and * write chunklists. */ - RDMA_SEND_POST_RDMA_READ, /* RDMA read */ - RDMA_SEND_POST_RDMA_WRITE, /* RDMA write */ -}rdma_send_post_type_t; - + GF_RDMA_SEND_POST_GF_RDMA_READ, /* RDMA read */ + GF_RDMA_SEND_POST_GF_RDMA_WRITE, /* RDMA write */ +}gf_rdma_send_post_type_t; + /* represents one communication peer, two per transport_t */ -struct __rdma_peer { - rpc_transport_t *trans; - struct ibv_qp *qp; +struct __gf_rdma_peer { + rpc_transport_t *trans; + struct rdma_cm_id *cm_id; + struct ibv_qp *qp; + pthread_t rdma_event_thread; + char quota_set; int32_t recv_count; int32_t send_count; int32_t recv_size; int32_t send_size; - int32_t quota; + int32_t quota; union { - struct list_head ioq; + struct list_head ioq; struct { - rdma_ioq_t *ioq_next; - rdma_ioq_t *ioq_prev; + gf_rdma_ioq_t *ioq_next; + gf_rdma_ioq_t *ioq_prev; }; }; /* QP attributes, needed to connect with remote QP */ - int32_t local_lid; - int32_t local_psn; - int32_t local_qpn; - int32_t remote_lid; - int32_t remote_psn; - int32_t remote_qpn; + int32_t local_lid; + int32_t local_psn; + int32_t local_qpn; + int32_t remote_lid; + int32_t remote_psn; + int32_t remote_qpn; }; -typedef struct __rdma_peer rdma_peer_t; +typedef struct __gf_rdma_peer gf_rdma_peer_t; -struct __rdma_post_context { - struct ibv_mr *mr[RDMA_MAX_SEGMENTS]; +struct __gf_rdma_post_context { + struct ibv_mr *mr[GF_RDMA_MAX_SEGMENTS]; int mr_count; struct iovec vector[MAX_IOVEC]; int count; struct iobref *iobref; struct iobuf *hdr_iobuf; char is_request; - int rdma_reads; - rdma_reply_info_t *reply_info; + int gf_rdma_reads; + gf_rdma_reply_info_t *reply_info; }; -typedef struct __rdma_post_context rdma_post_context_t; +typedef struct __gf_rdma_post_context gf_rdma_post_context_t; typedef enum { - RDMA_SEND_POST, - RDMA_RECV_POST -} rdma_post_type_t; + GF_RDMA_SEND_POST, + GF_RDMA_RECV_POST +} gf_rdma_post_type_t; -struct __rdma_post { - struct __rdma_post *next, *prev; +struct __gf_rdma_post { + struct __gf_rdma_post *next, *prev; struct ibv_mr *mr; char *buf; int32_t buf_size; char aux; int32_t reused; - struct __rdma_device *device; - rdma_post_type_t type; - rdma_post_context_t ctx; + struct __gf_rdma_device *device; + gf_rdma_post_type_t type; + gf_rdma_post_context_t ctx; int refcount; pthread_mutex_t lock; }; -typedef struct __rdma_post rdma_post_t; +typedef struct __gf_rdma_post gf_rdma_post_t; -struct __rdma_queue { - rdma_post_t active_posts, passive_posts; +struct __gf_rdma_queue { + gf_rdma_post_t active_posts, passive_posts; int32_t active_count, passive_count; pthread_mutex_t lock; }; -typedef struct __rdma_queue rdma_queue_t; +typedef struct __gf_rdma_queue gf_rdma_queue_t; -struct __rdma_qpreg { +struct __gf_rdma_qpreg { pthread_mutex_t lock; int32_t count; struct _qpent { struct _qpent *next, *prev; int32_t qp_num; - rdma_peer_t *peer; + gf_rdma_peer_t *peer; } ents[42]; }; -typedef struct __rdma_qpreg rdma_qpreg_t; +typedef struct __gf_rdma_qpreg gf_rdma_qpreg_t; /* context per device, stored in global glusterfs_ctx_t->ib */ -struct __rdma_device { - struct __rdma_device *next; +struct __gf_rdma_device { + struct __gf_rdma_device *next; const char *device_name; struct ibv_context *context; int32_t port; struct ibv_pd *pd; struct ibv_srq *srq; - rdma_qpreg_t qpreg; + gf_rdma_qpreg_t qpreg; struct ibv_comp_channel *send_chan, *recv_chan; struct ibv_cq *send_cq, *recv_cq; - rdma_queue_t sendq, recvq; - pthread_t send_thread, recv_thread; + gf_rdma_queue_t sendq, recvq; + pthread_t send_thread, recv_thread, async_event_thread; struct mem_pool *request_ctx_pool; struct mem_pool *ioq_pool; struct mem_pool *reply_info_pool; }; -typedef struct __rdma_device rdma_device_t; +typedef struct __gf_rdma_device gf_rdma_device_t; -typedef enum { - RDMA_HANDSHAKE_START = 0, - RDMA_HANDSHAKE_SENDING_DATA, - RDMA_HANDSHAKE_RECEIVING_DATA, - RDMA_HANDSHAKE_SENT_DATA, - RDMA_HANDSHAKE_RECEIVED_DATA, - RDMA_HANDSHAKE_SENDING_ACK, - RDMA_HANDSHAKE_RECEIVING_ACK, - RDMA_HANDSHAKE_RECEIVED_ACK, - RDMA_HANDSHAKE_COMPLETE, -} rdma_handshake_state_t; - -struct rdma_nbio { - int state; - char *buf; - int count; - struct iovec vector; - struct iovec *pending_vector; - int pending_count; +struct __gf_rdma_ctx { + gf_rdma_device_t *device; + struct rdma_event_channel *rdma_cm_event_channel; + pthread_t rdma_cm_thread; }; +typedef struct __gf_rdma_ctx gf_rdma_ctx_t; -struct __rdma_request_context { - struct ibv_mr *mr[RDMA_MAX_SEGMENTS]; +struct __gf_rdma_request_context { + struct ibv_mr *mr[GF_RDMA_MAX_SEGMENTS]; int mr_count; struct mem_pool *pool; - rdma_peer_t *peer; + gf_rdma_peer_t *peer; struct iobref *iobref; struct iobref *rsp_iobref; }; -typedef struct __rdma_request_context rdma_request_context_t; - -struct __rdma_private { - int32_t sock; - int32_t idx; - unsigned char connected; - unsigned char tcp_connected; - unsigned char ib_connected; - in_addr_t addr; +typedef struct __gf_rdma_request_context gf_rdma_request_context_t; + +typedef enum { + GF_RDMA_SERVER_LISTENER, + GF_RDMA_SERVER, + GF_RDMA_CLIENT, +} gf_rdma_transport_entity_t; + +struct __gf_rdma_private { + int32_t idx; + unsigned char connected; + in_addr_t addr; unsigned short port; /* IB Verbs Driver specific variables, pointers */ - rdma_peer_t peer; - struct __rdma_device *device; - rdma_options_t options; + gf_rdma_peer_t peer; + struct __gf_rdma_device *device; + gf_rdma_options_t options; /* Used by trans->op->receive */ - char *data_ptr; - int32_t data_offset; - int32_t data_len; + char *data_ptr; + int32_t data_offset; + int32_t data_len; /* Mutex */ - pthread_mutex_t read_mutex; - pthread_mutex_t write_mutex; - pthread_barrier_t handshake_barrier; - char handshake_ret; - char is_server; - rpc_transport_t *listener; - - pthread_mutex_t recv_mutex; - pthread_cond_t recv_cond; - - /* used during rdma_handshake */ - struct { - struct rdma_nbio incoming; - struct rdma_nbio outgoing; - int state; - rdma_header_t header; - char *buf; - size_t size; - } handshake; + pthread_mutex_t write_mutex; + rpc_transport_t *listener; + pthread_mutex_t recv_mutex; + pthread_cond_t recv_cond; + gf_rdma_transport_entity_t entity; }; -typedef struct __rdma_private rdma_private_t; +typedef struct __gf_rdma_private gf_rdma_private_t; -#endif /* _XPORT_RDMA_H */ +#endif /* _XPORT_GF_RDMA_H */ diff --git a/rpc/rpc-transport/socket/src/Makefile.am b/rpc/rpc-transport/socket/src/Makefile.am index 2c918c7e3..71e6ed6ff 100644 --- a/rpc/rpc-transport/socket/src/Makefile.am +++ b/rpc/rpc-transport/socket/src/Makefile.am @@ -3,13 +3,15 @@ noinst_HEADERS = socket.h name.h rpctransport_LTLIBRARIES = socket.la rpctransportdir = $(libdir)/glusterfs/$(PACKAGE_VERSION)/rpc-transport -socket_la_LDFLAGS = -module -avoidversion +socket_la_LDFLAGS = -module -avoid-version socket_la_SOURCES = socket.c name.c -socket_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la +socket_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la -lssl -AM_CFLAGS = -fPIC -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -Wall -D$(GF_HOST_OS)\ +AM_CPPFLAGS = $(GF_CPPFLAGS) \ -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src/ \ - -I$(top_srcdir)/rpc/xdr/src/ -shared -nostartfiles $(GF_CFLAGS) + -I$(top_srcdir)/rpc/xdr/src/ + +AM_CFLAGS = -Wall $(GF_CFLAGS) CLEANFILES = *~ diff --git a/rpc/rpc-transport/socket/src/name.c b/rpc/rpc-transport/socket/src/name.c index 8be29163e..1647d5b6b 100644 --- a/rpc/rpc-transport/socket/src/name.c +++ b/rpc/rpc-transport/socket/src/name.c @@ -1,20 +1,11 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #include <sys/types.h> @@ -24,18 +15,13 @@ #include <netdb.h> #include <string.h> -#ifdef CLIENT_PORT_CEILING -#undef CLIENT_PORT_CEILING -#endif - -#define CLIENT_PORT_CEILING 1024 - #ifndef AF_INET_SDP #define AF_INET_SDP 27 #endif #include "rpc-transport.h" #include "socket.h" +#include "common-utils.h" int32_t gf_resolve_ip6 (const char *hostname, @@ -48,9 +34,17 @@ static int32_t af_inet_bind_to_port_lt_ceiling (int fd, struct sockaddr *sockaddr, socklen_t sockaddr_len, int ceiling) { - int32_t ret = -1; - /* struct sockaddr_in sin = {0, }; */ - uint16_t port = ceiling - 1; + int32_t ret = -1; + uint16_t port = ceiling - 1; + // by default assume none of the ports are blocked and all are available + gf_boolean_t ports[1024] = {_gf_false,}; + int i = 0; + + ret = gf_process_reserved_ports (ports); + if (ret != 0) { + for (i = 0; i < 1024; i++) + ports[i] = _gf_false; + } while (port) { @@ -65,7 +59,11 @@ af_inet_bind_to_port_lt_ceiling (int fd, struct sockaddr *sockaddr, ((struct sockaddr_in *)sockaddr)->sin_port = htons (port); break; } - + // ignore the reserved ports + if (ports[port] == _gf_true) { + port--; + continue; + } ret = bind (fd, sockaddr, sockaddr_len); if (ret == 0) @@ -95,7 +93,7 @@ af_unix_client_bind (rpc_transport_t *this, char *path = data_to_str (path_data); if (!path || strlen (path) > UNIX_PATH_MAX) { gf_log (this->name, GF_LOG_TRACE, - "bind-path not specfied for unix socket, " + "bind-path not specified for unix socket, " "letting connect to assign default value"); goto err; } @@ -111,7 +109,7 @@ af_unix_client_bind (rpc_transport_t *this, } } else { gf_log (this->name, GF_LOG_TRACE, - "bind-path not specfied for unix socket, " + "bind-path not specified for unix socket, " "letting connect to assign default value"); } @@ -126,6 +124,8 @@ client_fill_address_family (rpc_transport_t *this, sa_family_t *sa_family) int32_t ret = -1; if (sa_family == NULL) { + gf_log_callingfn ("", GF_LOG_WARNING, + "sa_family argument is NULL"); goto out; } @@ -140,24 +140,24 @@ client_fill_address_family (rpc_transport_t *this, sa_family_t *sa_family) if (!(remote_host_data || connect_path_data) || (remote_host_data && connect_path_data)) { gf_log (this->name, GF_LOG_ERROR, - "transport.address-family not specified and " - "not able to determine the " - "same from other options (remote-host:%s and " - "transport.unix.connect-path:%s)", + "transport.address-family not specified. " + "Could not guess default value from (remote-host:%s or " + "transport.unix.connect-path:%s) options", data_to_str (remote_host_data), data_to_str (connect_path_data)); + *sa_family = AF_UNSPEC; goto out; } if (remote_host_data) { gf_log (this->name, GF_LOG_DEBUG, "address-family not specified, guessing it " - "to be inet/inet6"); - *sa_family = AF_UNSPEC; + "to be inet from (remote-host: %s)", data_to_str (remote_host_data)); + *sa_family = AF_INET; } else { gf_log (this->name, GF_LOG_DEBUG, "address-family not specified, guessing it " - "to be unix"); + "to be unix from (transport.unix.connect-path: %s)", data_to_str (connect_path_data)); *sa_family = AF_UNIX; } @@ -171,13 +171,11 @@ client_fill_address_family (rpc_transport_t *this, sa_family_t *sa_family) *sa_family = AF_INET6; } else if (!strcasecmp (address_family, "inet-sdp")) { *sa_family = AF_INET_SDP; - } else if (!strcasecmp (address_family, "inet/inet6") - || !strcasecmp (address_family, "inet6/inet")) { - *sa_family = AF_UNSPEC; } else { gf_log (this->name, GF_LOG_ERROR, "unknown address-family (%s) specified", address_family); + *sa_family = AF_UNSPEC; goto out; } } @@ -353,7 +351,7 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, struct sockaddr *addr, socklen_t *addr_len) { - struct addrinfo hints, *res = 0; + struct addrinfo hints, *res = 0, *rp = NULL; data_t *listen_port_data = NULL, *listen_host_data = NULL; uint16_t listen_port = -1; char service[NI_MAXSERV], *listen_host = NULL; @@ -378,20 +376,20 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, { listen_host = data_to_str (listen_host_data); } else { - if (addr->sa_family == AF_INET6) { - struct sockaddr_in6 *in = (struct sockaddr_in6 *) addr; - in->sin6_addr = in6addr_any; - in->sin6_port = htons(listen_port); - *addr_len = sizeof(struct sockaddr_in6); + if (addr->sa_family == AF_INET6) { + struct sockaddr_in6 *in = (struct sockaddr_in6 *) addr; + in->sin6_addr = in6addr_any; + in->sin6_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in6); goto out; - } else if (addr->sa_family == AF_INET) { - struct sockaddr_in *in = (struct sockaddr_in *) addr; - in->sin_addr.s_addr = htonl(INADDR_ANY); - in->sin_port = htons(listen_port); - *addr_len = sizeof(struct sockaddr_in); - goto out; - } - } + } else if (addr->sa_family == AF_INET) { + struct sockaddr_in *in = (struct sockaddr_in *) addr; + in->sin_addr.s_addr = htonl(INADDR_ANY); + in->sin_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in); + goto out; + } + } memset (service, 0, sizeof (service)); sprintf (service, "%d", listen_port); @@ -399,7 +397,7 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, memset (&hints, 0, sizeof (hints)); hints.ai_family = addr->sa_family; hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE; + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; ret = getaddrinfo(listen_host, service, &hints, &res); if (ret != 0) { @@ -409,9 +407,20 @@ af_inet_server_get_local_sockaddr (rpc_transport_t *this, ret = -1; goto out; } + /* IPV6 server can handle both ipv4 and ipv6 clients */ + for (rp = res; rp != NULL; rp = rp->ai_next) { + if (rp->ai_addr == NULL) + continue; + if (rp->ai_family == AF_INET6) { + memcpy (addr, rp->ai_addr, rp->ai_addrlen); + *addr_len = rp->ai_addrlen; + } + } - memcpy (addr, res->ai_addr, res->ai_addrlen); - *addr_len = res->ai_addrlen; + if (!(*addr_len)) { + memcpy (addr, res->ai_addr, res->ai_addrlen); + *addr_len = res->ai_addrlen; + } freeaddrinfo (res); @@ -435,12 +444,14 @@ client_bind (rpc_transport_t *this, *sockaddr_len = sizeof (struct sockaddr_in); case AF_INET6: - ret = af_inet_bind_to_port_lt_ceiling (sock, sockaddr, - *sockaddr_len, CLIENT_PORT_CEILING); + if (!this->bind_insecure) { + ret = af_inet_bind_to_port_lt_ceiling (sock, sockaddr, + *sockaddr_len, GF_CLIENT_PORT_CEILING); + } if (ret == -1) { - gf_log (this->name, GF_LOG_WARNING, + gf_log (this->name, GF_LOG_DEBUG, "cannot bind inet socket (%d) to port less than %d (%s)", - sock, CLIENT_PORT_CEILING, strerror (errno)); + sock, GF_CLIENT_PORT_CEILING, strerror (errno)); ret = 0; } break; @@ -469,12 +480,9 @@ socket_client_get_remote_sockaddr (rpc_transport_t *this, { int32_t ret = 0; - if ((sockaddr == NULL) || (sockaddr_len == NULL) - || (sa_family == NULL)) { - ret = -1; - goto err; - } - + GF_VALIDATE_OR_GOTO ("socket", sockaddr, err); + GF_VALIDATE_OR_GOTO ("socket", sockaddr_len, err); + GF_VALIDATE_OR_GOTO ("socket", sa_family, err); ret = client_fill_address_family (this, &sockaddr->sa_family); if (ret) { @@ -522,9 +530,7 @@ server_fill_address_family (rpc_transport_t *this, sa_family_t *sa_family) data_t *address_family_data = NULL; int32_t ret = -1; - if (sa_family == NULL) { - goto out; - } + GF_VALIDATE_OR_GOTO ("socket", sa_family, out); address_family_data = dict_get (this->options, "transport.address-family"); @@ -540,18 +546,16 @@ server_fill_address_family (rpc_transport_t *this, sa_family_t *sa_family) *sa_family = AF_INET_SDP; } else if (!strcasecmp (address_family, "unix")) { *sa_family = AF_UNIX; - } else if (!strcasecmp (address_family, "inet/inet6") - || !strcasecmp (address_family, "inet6/inet")) { - *sa_family = AF_UNSPEC; } else { gf_log (this->name, GF_LOG_ERROR, "unknown address family (%s) specified", address_family); + *sa_family = AF_UNSPEC; goto out; } } else { gf_log (this->name, GF_LOG_DEBUG, - "option address-family not specified, defaulting to inet/inet6"); - *sa_family = AF_UNSPEC; + "option address-family not specified, defaulting to inet"); + *sa_family = AF_INET; } ret = 0; @@ -566,9 +570,9 @@ socket_server_get_local_sockaddr (rpc_transport_t *this, struct sockaddr *addr, { int32_t ret = -1; - if ((addr == NULL) || (addr_len == NULL) || (sa_family == NULL)) { - goto err; - } + GF_VALIDATE_OR_GOTO ("socket", sa_family, err); + GF_VALIDATE_OR_GOTO ("socket", addr, err); + GF_VALIDATE_OR_GOTO ("socket", addr_len, err); ret = server_fill_address_family (this, &addr->sa_family); if (ret == -1) { @@ -605,7 +609,7 @@ int32_t fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *addr, int32_t addr_len, char *identifier) { - struct sockaddr_storage tmpaddr; + union gf_sock_union sock_union; char service[NI_MAXSERV] = {0,}; char host[NI_MAXHOST] = {0,}; @@ -617,26 +621,26 @@ fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *add int16_t eight_to_ten = 0; int16_t ten_to_twelve = 0; - memset (&tmpaddr, 0, sizeof (tmpaddr)); - tmpaddr = *addr; + memset (&sock_union, 0, sizeof (sock_union)); + sock_union.storage = *addr; tmpaddr_len = addr_len; - if (((struct sockaddr *) &tmpaddr)->sa_family == AF_INET6) { - one_to_four = ((struct sockaddr_in6 *) &tmpaddr)->sin6_addr.s6_addr32[0]; - four_to_eight = ((struct sockaddr_in6 *) &tmpaddr)->sin6_addr.s6_addr32[1]; + if (sock_union.sa.sa_family == AF_INET6) { + one_to_four = sock_union.sin6.sin6_addr.s6_addr32[0]; + four_to_eight = sock_union.sin6.sin6_addr.s6_addr32[1]; #ifdef GF_SOLARIS_HOST_OS - eight_to_ten = S6_ADDR16(((struct sockaddr_in6 *) &tmpaddr)->sin6_addr)[4]; + eight_to_ten = S6_ADDR16(sock_union.sin6.sin6_addr)[4]; #else - eight_to_ten = ((struct sockaddr_in6 *) &tmpaddr)->sin6_addr.s6_addr16[4]; + eight_to_ten = sock_union.sin6.sin6_addr.s6_addr16[4]; #endif #ifdef GF_SOLARIS_HOST_OS - ten_to_twelve = S6_ADDR16(((struct sockaddr_in6 *) &tmpaddr)->sin6_addr)[5]; + ten_to_twelve = S6_ADDR16(sock_union.sin6.sin6_addr)[5]; #else - ten_to_twelve = ((struct sockaddr_in6 *) &tmpaddr)->sin6_addr.s6_addr16[5]; + ten_to_twelve = sock_union.sin6.sin6_addr.s6_addr16[5]; #endif - twelve_to_sixteen = ((struct sockaddr_in6 *) &tmpaddr)->sin6_addr.s6_addr32[3]; + twelve_to_sixteen = sock_union.sin6.sin6_addr.s6_addr32[3]; /* ipv4 mapped ipv6 address has bits 0-80: 0 @@ -648,8 +652,8 @@ fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *add four_to_eight == 0 && eight_to_ten == 0 && ten_to_twelve == -1) { - struct sockaddr_in *in_ptr = (struct sockaddr_in *)&tmpaddr; - memset (&tmpaddr, 0, sizeof (tmpaddr)); + struct sockaddr_in *in_ptr = &sock_union.sin; + memset (&sock_union, 0, sizeof (sock_union)); in_ptr->sin_family = AF_INET; in_ptr->sin_port = ((struct sockaddr_in6 *)addr)->sin6_port; @@ -658,7 +662,7 @@ fill_inet6_inet_identifiers (rpc_transport_t *this, struct sockaddr_storage *add } } - ret = getnameinfo ((struct sockaddr *) &tmpaddr, + ret = getnameinfo (&sock_union.sa, tmpaddr_len, host, sizeof (host), service, sizeof (service), diff --git a/rpc/rpc-transport/socket/src/name.h b/rpc/rpc-transport/socket/src/name.h index 66eccf8d5..0a13d8a96 100644 --- a/rpc/rpc-transport/socket/src/name.h +++ b/rpc/rpc-transport/socket/src/name.h @@ -1,20 +1,11 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #ifndef _SOCKET_NAME_H diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c index 4ae49815a..93da3f296 100644 --- a/rpc/rpc-transport/socket/src/socket.c +++ b/rpc/rpc-transport/socket/src/socket.c @@ -1,20 +1,11 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ @@ -37,104 +28,408 @@ /* ugly #includes below */ #include "protocol-common.h" #include "glusterfs3-xdr.h" -#include "glusterfs3.h" +#include "xdr-nfs3.h" +#include "rpcsvc.h" #include <fcntl.h> #include <errno.h> #include <netinet/tcp.h> #include <rpc/xdr.h> - +#include <sys/ioctl.h> #define GF_LOG_ERRNO(errno) ((errno == ENOTCONN) ? GF_LOG_DEBUG : GF_LOG_ERROR) #define SA(ptr) ((struct sockaddr *)ptr) +#define SSL_ENABLED_OPT "transport.socket.ssl-enabled" +#define SSL_OWN_CERT_OPT "transport.socket.ssl-own-cert" +#define SSL_PRIVATE_KEY_OPT "transport.socket.ssl-private-key" +#define SSL_CA_LIST_OPT "transport.socket.ssl-ca-list" +#define OWN_THREAD_OPT "transport.socket.own-thread" + +/* TBD: do automake substitutions etc. (ick) to set these. */ +#if !defined(DEFAULT_CERT_PATH) +#define DEFAULT_CERT_PATH "/etc/ssl/glusterfs.pem" +#endif +#if !defined(DEFAULT_KEY_PATH) +#define DEFAULT_KEY_PATH "/etc/ssl/glusterfs.key" +#endif +#if !defined(DEFAULT_CA_PATH) +#define DEFAULT_CA_PATH "/etc/ssl/glusterfs.ca" +#endif -#define __socket_proto_reset_pending(priv) do { \ - memset (&priv->incoming.frag.vector, 0, \ - sizeof (priv->incoming.frag.vector)); \ - priv->incoming.frag.pending_vector = \ - &priv->incoming.frag.vector; \ - priv->incoming.frag.pending_vector->iov_base = \ - priv->incoming.frag.fragcurrent; \ - priv->incoming.pending_vector = \ - priv->incoming.frag.pending_vector; \ - } while (0); - - -#define __socket_proto_update_pending(priv) \ - do { \ - uint32_t remaining_fragsize = 0; \ - if (priv->incoming.frag.pending_vector->iov_len == 0) { \ - remaining_fragsize = RPC_FRAGSIZE (priv->incoming.fraghdr) \ - - priv->incoming.frag.bytes_read; \ - \ - priv->incoming.frag.pending_vector->iov_len = \ - remaining_fragsize > priv->incoming.frag.remaining_size \ - ? priv->incoming.frag.remaining_size : remaining_fragsize; \ - \ - priv->incoming.frag.remaining_size -= \ - priv->incoming.frag.pending_vector->iov_len; \ - } \ - } while (0); - -#define __socket_proto_update_priv_after_read(priv, ret, bytes_read) \ - { \ - priv->incoming.frag.fragcurrent += bytes_read; \ - priv->incoming.frag.bytes_read += bytes_read; \ - \ - if ((ret > 0) || (priv->incoming.frag.remaining_size != 0)) { \ - if (priv->incoming.frag.remaining_size != 0) { \ - __socket_proto_reset_pending (priv); \ - } \ - \ - gf_log (this->name, GF_LOG_TRACE, "partial read on non-blocking socket"); \ - \ - break; \ - } \ - } - -#define __socket_proto_init_pending(priv, size) \ - do { \ - uint32_t remaining_fragsize = 0; \ - remaining_fragsize = RPC_FRAGSIZE (priv->incoming.fraghdr) \ - - priv->incoming.frag.bytes_read; \ - \ - __socket_proto_reset_pending (priv); \ - \ - priv->incoming.frag.pending_vector->iov_len = \ - remaining_fragsize > size ? size : remaining_fragsize; \ - \ - priv->incoming.frag.remaining_size = \ - size - priv->incoming.frag.pending_vector->iov_len; \ - \ -} while (0); +#define POLL_MASK_INPUT (POLLIN | POLLPRI) +#define POLL_MASK_OUTPUT (POLLOUT) +#define POLL_MASK_ERROR (POLLERR | POLLHUP | POLLNVAL) + +typedef int SSL_unary_func (SSL *); +typedef int SSL_trinary_func (SSL *, void *, int); + +#define __socket_proto_reset_pending(priv) do { \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + memset (&frag->vector, 0, sizeof (frag->vector)); \ + frag->pending_vector = &frag->vector; \ + frag->pending_vector->iov_base = frag->fragcurrent; \ + priv->incoming.pending_vector = frag->pending_vector; \ + } while (0) + + +#define __socket_proto_update_pending(priv) \ + do { \ + uint32_t remaining; \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + if (frag->pending_vector->iov_len == 0) { \ + remaining = (RPC_FRAGSIZE (priv->incoming.fraghdr) \ + - frag->bytes_read); \ + \ + frag->pending_vector->iov_len = \ + (remaining > frag->remaining_size) \ + ? frag->remaining_size : remaining; \ + \ + frag->remaining_size -= \ + frag->pending_vector->iov_len; \ + } \ + } while (0) + +#define __socket_proto_update_priv_after_read(priv, ret, bytes_read) \ + { \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + frag->fragcurrent += bytes_read; \ + frag->bytes_read += bytes_read; \ + \ + if ((ret > 0) || (frag->remaining_size != 0)) { \ + if (frag->remaining_size != 0 && ret == 0) { \ + __socket_proto_reset_pending (priv); \ + } \ + \ + gf_log (this->name, GF_LOG_TRACE, \ + "partial read on non-blocking socket"); \ + \ + break; \ + } \ + } + +#define __socket_proto_init_pending(priv,size) \ + do { \ + uint32_t remaining = 0; \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + remaining = (RPC_FRAGSIZE (priv->incoming.fraghdr) \ + - frag->bytes_read); \ + \ + __socket_proto_reset_pending (priv); \ + \ + frag->pending_vector->iov_len = \ + (remaining > size) ? size : remaining; \ + \ + frag->remaining_size = (size - frag->pending_vector->iov_len); \ + \ + } while(0) /* This will be used in a switch case and breaks from the switch case if all * the pending data is not read. */ -#define __socket_proto_read(priv, ret) \ - { \ - size_t bytes_read = 0; \ - \ - __socket_proto_update_pending (priv); \ - \ - ret = __socket_readv (this, \ - priv->incoming.pending_vector, 1, \ - &priv->incoming.pending_vector, \ - &priv->incoming.pending_count, \ - &bytes_read); \ - if (ret == -1) { \ - gf_log (this->name, GF_LOG_TRACE, \ - "reading from socket failed. Error (%s), " \ - "peer (%s)", strerror (errno), \ - this->peerinfo.identifier); \ - break; \ - } \ +#define __socket_proto_read(priv, ret) \ + { \ + size_t bytes_read = 0; \ + struct gf_sock_incoming *in; \ + in = &priv->incoming; \ + \ + __socket_proto_update_pending (priv); \ + \ + ret = __socket_readv (this, \ + in->pending_vector, 1, \ + &in->pending_vector, \ + &in->pending_count, \ + &bytes_read); \ + if (ret == -1) \ + break; \ __socket_proto_update_priv_after_read (priv, ret, bytes_read); \ - } + } + +static int socket_init (rpc_transport_t *this); + +static void +ssl_dump_error_stack (const char *caller) +{ + unsigned long errnum = 0; + char errbuf[120] = {0,}; + + /* OpenSSL docs explicitly give 120 as the error-string length. */ + + while ((errnum = ERR_get_error())) { + ERR_error_string(errnum,errbuf); + gf_log(caller,GF_LOG_ERROR," %s",errbuf); + } +} + +static int +ssl_do (rpc_transport_t *this, void *buf, size_t len, SSL_trinary_func *func) +{ + int r = (-1); + struct pollfd pfd = {-1,}; + socket_private_t *priv = NULL; + + GF_VALIDATE_OR_GOTO(this->name,this->private,out); + priv = this->private; + + for (;;) { + if (buf) { + if (priv->connected == -1) { + /* + * Fields in the SSL structure (especially + * the BIO pointers) are not valid at this + * point, so we'll segfault if we pass them + * to SSL_read/SSL_write. + */ + gf_log(this->name,GF_LOG_INFO, + "lost connection in %s", __func__); + break; + } + r = func(priv->ssl_ssl,buf,len); + } + else { + /* + * We actually need these functions to get to + * priv->connected == 1. + */ + r = ((SSL_unary_func *)func)(priv->ssl_ssl); + } + switch (SSL_get_error(priv->ssl_ssl,r)) { + case SSL_ERROR_NONE: + return r; + case SSL_ERROR_WANT_READ: + pfd.fd = priv->sock; + pfd.events = POLLIN; + if (poll(&pfd,1,-1) < 0) { + gf_log(this->name,GF_LOG_ERROR,"poll error %d", + errno); + } + break; + case SSL_ERROR_WANT_WRITE: + pfd.fd = priv->sock; + pfd.events = POLLOUT; + if (poll(&pfd,1,-1) < 0) { + gf_log(this->name,GF_LOG_ERROR,"poll error %d", + errno); + } + break; + case SSL_ERROR_SYSCALL: + /* This is what we get when remote disconnects. */ + gf_log(this->name,GF_LOG_DEBUG, + "syscall error (probably remote disconnect)"); + errno = ENODATA; + goto out; + default: + errno = EIO; + goto out; /* "break" would just loop again */ + } + } +out: + return -1; +} + +#define ssl_connect_one(t) ssl_do((t),NULL,0,(SSL_trinary_func *)SSL_connect) +#define ssl_accept_one(t) ssl_do((t),NULL,0,(SSL_trinary_func *)SSL_accept) +#define ssl_read_one(t,b,l) ssl_do((t),(b),(l),(SSL_trinary_func *)SSL_read) +#define ssl_write_one(t,b,l) ssl_do((t),(b),(l),(SSL_trinary_func *)SSL_write) + +static int +ssl_setup_connection (rpc_transport_t *this, int server) +{ + X509 *peer = NULL; + char peer_CN[256] = ""; + int ret = -1; + socket_private_t *priv = NULL; + + GF_VALIDATE_OR_GOTO(this->name,this->private,done); + priv = this->private; + + priv->ssl_ssl = SSL_new(priv->ssl_ctx); + if (!priv->ssl_ssl) { + gf_log(this->name,GF_LOG_ERROR,"SSL_new failed"); + ssl_dump_error_stack(this->name); + goto done; + } + priv->ssl_sbio = BIO_new_socket(priv->sock,BIO_NOCLOSE); + if (!priv->ssl_sbio) { + gf_log(this->name,GF_LOG_ERROR,"BIO_new_socket failed"); + ssl_dump_error_stack(this->name); + goto free_ssl; + } + SSL_set_bio(priv->ssl_ssl,priv->ssl_sbio,priv->ssl_sbio); + + if (server) { + ret = ssl_accept_one(this); + } + else { + ret = ssl_connect_one(this); + } + + /* Make sure _the call_ succeeded. */ + if (ret < 0) { + goto ssl_error; + } + + /* Make sure _SSL verification_ succeeded, yielding an identity. */ + if (SSL_get_verify_result(priv->ssl_ssl) != X509_V_OK) { + goto ssl_error; + } + peer = SSL_get_peer_certificate(priv->ssl_ssl); + if (!peer) { + goto ssl_error; + } + + /* Finally, everything seems OK. */ + X509_NAME_get_text_by_NID(X509_get_subject_name(peer), + NID_commonName, peer_CN, sizeof(peer_CN)-1); + peer_CN[sizeof(peer_CN)-1] = '\0'; + gf_log(this->name,GF_LOG_INFO,"peer CN = %s", peer_CN); + return 0; + + /* Error paths. */ +ssl_error: + gf_log(this->name,GF_LOG_ERROR,"SSL connect error"); + ssl_dump_error_stack(this->name); +free_ssl: + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; +done: + return ret; +} + + +static void +ssl_teardown_connection (socket_private_t *priv) +{ + SSL_shutdown(priv->ssl_ssl); + SSL_clear(priv->ssl_ssl); + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; +} + + +static ssize_t +__socket_ssl_readv (rpc_transport_t *this, struct iovec *opvector, int opcount) +{ + socket_private_t *priv = NULL; + int sock = -1; + int ret = -1; + + priv = this->private; + sock = priv->sock; + + if (priv->use_ssl) { + ret = ssl_read_one (this, opvector->iov_base, opvector->iov_len); + } else { + ret = readv (sock, opvector, opcount); + } + + return ret; +} + + +static ssize_t +__socket_ssl_read (rpc_transport_t *this, void *buf, size_t count) +{ + struct iovec iov = {0, }; + int ret = -1; + + iov.iov_base = buf; + iov.iov_len = count; + + ret = __socket_ssl_readv (this, &iov, 1); + + return ret; +} + + +static int +__socket_cached_read (rpc_transport_t *this, struct iovec *opvector, int opcount) +{ + socket_private_t *priv = NULL; + int sock = -1; + struct gf_sock_incoming *in = NULL; + int req_len = -1; + int ret = -1; + + priv = this->private; + sock = priv->sock; + in = &priv->incoming; + req_len = iov_length (opvector, opcount); + + if (in->record_state == SP_STATE_READING_FRAGHDR) { + in->ra_read = 0; + in->ra_served = 0; + in->ra_max = 0; + in->ra_buf = NULL; + goto uncached; + } + + if (!in->ra_max) { + /* first call after passing SP_STATE_READING_FRAGHDR */ + in->ra_max = min (RPC_FRAGSIZE (in->fraghdr), GF_SOCKET_RA_MAX); + /* Note that the in->iobuf is the primary iobuf into which + headers are read into. By using this itself as our + read-ahead cache, we can avoid memory copies in iov_load + */ + in->ra_buf = iobuf_ptr (in->iobuf); + } + + /* fill read-ahead */ + if (in->ra_read < in->ra_max) { + ret = __socket_ssl_read (this, &in->ra_buf[in->ra_read], + (in->ra_max - in->ra_read)); + if (ret > 0) + in->ra_read += ret; + + /* we proceed to test if there is still cached data to + be served even if readahead could not progress */ + } + + /* serve cached */ + if (in->ra_served < in->ra_read) { + ret = iov_load (opvector, opcount, &in->ra_buf[in->ra_served], + min (req_len, (in->ra_read - in->ra_served))); + + in->ra_served += ret; + /* Do not read uncached and cached in the same call */ + goto out; + } + if (in->ra_read < in->ra_max) + /* If there was no cached data to be served, (and we are + guaranteed to have already performed an attempt to progress + readahead above), and we have not yet read out the full + readahead capacity, then bail out for now without doing + the uncached read below (as that will overtake future cached + read) + */ + goto out; +uncached: + ret = __socket_ssl_readv (this, opvector, opcount); +out: + return ret; +} + +static gf_boolean_t +__does_socket_rwv_error_need_logging (socket_private_t *priv, int write) +{ + int read = !write; + + if (priv->connected == -1) /* Didn't even connect, of course it fails */ + return _gf_false; -int socket_init (rpc_transport_t *this); + if (read && (priv->read_fail_log == _gf_false)) + return _gf_false; + + return _gf_true; +} /* * return value: @@ -143,7 +438,7 @@ int socket_init (rpc_transport_t *this); * > 0 = incomplete */ -int +static int __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, struct iovec **pending_vector, int *pending_count, size_t *bytes, int write) @@ -155,22 +450,35 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, int opcount = 0; int moved = 0; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; sock = priv->sock; - opvector = vector; - opcount = count; + opvector = vector; + opcount = count; if (bytes != NULL) { *bytes = 0; } - while (opcount) { + while (opcount > 0) { + if (opvector->iov_len == 0) { + gf_log(this->name,GF_LOG_DEBUG, + "would have passed zero length to read/write"); + ++opvector; + --opcount; + continue; + } if (write) { - ret = writev (sock, opvector, opcount); + if (priv->use_ssl) { + ret = ssl_write_one(this, + opvector->iov_base, opvector->iov_len); + } + else { + ret = writev (sock, opvector, opcount); + } if (ret == 0 || (ret == -1 && errno == EAGAIN)) { /* done for now */ @@ -178,7 +486,13 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, } this->total_bytes_write += ret; } else { - ret = readv (sock, opvector, opcount); + ret = __socket_cached_read (this, opvector, opcount); + + if (ret == 0) { + gf_log(this->name,GF_LOG_DEBUG,"EOF on socket"); + errno = ENODATA; + ret = -1; + } if (ret == -1 && errno == EAGAIN) { /* done for now */ break; @@ -189,7 +503,7 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, if (ret == 0) { /* Mostly due to 'umount' in client */ - gf_log (this->name, GF_LOG_TRACE, + gf_log (this->name, GF_LOG_DEBUG, "EOF from peer %s", this->peerinfo.identifier); opcount = -1; errno = ENOTCONN; @@ -199,9 +513,18 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, if (errno == EINTR) continue; - gf_log (this->name, GF_LOG_TRACE, - "%s failed (%s)", write ? "writev" : "readv", - strerror (errno)); + if (__does_socket_rwv_error_need_logging (priv, + write)) { + gf_log (this->name, GF_LOG_WARNING, + "%s on %s failed (%s)", + write ? "writev":"readv", + this->peerinfo.identifier, + strerror (errno)); + } + + if (priv->use_ssl) { + ssl_dump_error_stack(this->name); + } opcount = -1; break; } @@ -213,6 +536,17 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, moved = 0; while (moved < ret) { + if (!opcount) { + gf_log(this->name,GF_LOG_DEBUG, + "ran out of iov, moved %d/%d", + moved, ret); + goto ran_out; + } + if (!opvector[0].iov_len) { + opvector++; + opcount--; + continue; + } if ((ret - moved) >= opvector[0].iov_len) { moved += opvector[0].iov_len; opvector++; @@ -222,13 +556,11 @@ __socket_rwv (rpc_transport_t *this, struct iovec *vector, int count, opvector[0].iov_base += (ret - moved); moved += (ret - moved); } - while (opcount && !opvector[0].iov_len) { - opvector++; - opcount--; - } } } +ran_out: + if (pending_vector) *pending_vector = opvector; @@ -240,7 +572,7 @@ out: } -int +static int __socket_readv (rpc_transport_t *this, struct iovec *vector, int count, struct iovec **pending_vector, int *pending_count, size_t *bytes) @@ -248,43 +580,75 @@ __socket_readv (rpc_transport_t *this, struct iovec *vector, int count, int ret = -1; ret = __socket_rwv (this, vector, count, - pending_vector, pending_count, bytes, 0); + pending_vector, pending_count, bytes, 0); return ret; } -int +static int __socket_writev (rpc_transport_t *this, struct iovec *vector, int count, struct iovec **pending_vector, int *pending_count) { int ret = -1; ret = __socket_rwv (this, vector, count, - pending_vector, pending_count, NULL, 1); + pending_vector, pending_count, NULL, 1); return ret; } -int -__socket_disconnect (rpc_transport_t *this) +static int +__socket_shutdown (rpc_transport_t *this) { - socket_private_t *priv = NULL; int ret = -1; + socket_private_t *priv = this->private; - if (!this || !this->private) - goto out; + priv->connected = -1; + ret = shutdown (priv->sock, SHUT_RDWR); + if (ret) { + /* its already disconnected.. no need to understand + why it failed to shutdown in normal cases */ + gf_log (this->name, GF_LOG_DEBUG, + "shutdown() returned %d. %s", + ret, strerror (errno)); + } + + return ret; +} + +static int +__socket_disconnect (rpc_transport_t *this) +{ + int ret = -1; + socket_private_t *priv = NULL; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; + gf_log (this->name, GF_LOG_TRACE, + "disconnecting %p, state=%u gen=%u sock=%d", this, + priv->ot_state, priv->ot_gen, priv->sock); + if (priv->sock != -1) { - ret = shutdown (priv->sock, SHUT_RDWR); - priv->connected = -1; - gf_log (this->name, GF_LOG_TRACE, - "shutdown() returned %d. set connection state to -1", - ret); + ret = __socket_shutdown(this); + if (priv->own_thread) { + /* + * Without this, reconnect (= disconnect + connect) + * won't work except by accident. + */ + close(priv->sock); + priv->sock = -1; + gf_log (this->name, GF_LOG_TRACE, + "OT_PLEASE_DIE on %p", this); + priv->ot_state = OT_PLEASE_DIE; + } + else if (priv->use_ssl) { + ssl_teardown_connection(priv); + } } out: @@ -292,20 +656,22 @@ out: } -int +static int __socket_server_bind (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = -1; - int opt = 1; + int opt = 1; + int reuse_check_sock = -1; + struct sockaddr_storage unix_addr = {0}; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); - priv = this->private; + priv = this->private; ret = setsockopt (priv->sock, SOL_SOCKET, SO_REUSEADDR, - &opt, sizeof (opt)); + &opt, sizeof (opt)); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, @@ -313,8 +679,23 @@ __socket_server_bind (rpc_transport_t *this) strerror (errno)); } + /* reuse-address doesn't work for unix type sockets */ + if (AF_UNIX == SA (&this->myinfo.sockaddr)->sa_family) { + memcpy (&unix_addr, SA (&this->myinfo.sockaddr), + this->myinfo.sockaddr_len); + reuse_check_sock = socket (AF_UNIX, SOCK_STREAM, 0); + if (reuse_check_sock >= 0) { + ret = connect (reuse_check_sock, SA (&unix_addr), + this->myinfo.sockaddr_len); + if ((ret == -1) && (ECONNREFUSED == errno)) { + unlink (((struct sockaddr_un*)&unix_addr)->sun_path); + } + close (reuse_check_sock); + } + } + ret = bind (priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, - this->myinfo.sockaddr_len); + this->myinfo.sockaddr_len); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, @@ -331,7 +712,7 @@ out: } -int +static int __socket_nonblock (int fd) { int flags = 0; @@ -345,17 +726,16 @@ __socket_nonblock (int fd) return ret; } - -int +static int __socket_nodelay (int fd) { int on = 1; int ret = -1; ret = setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, - &on, sizeof (on)); + &on, sizeof (on)); if (!ret) - gf_log ("", GF_LOG_TRACE, + gf_log (THIS->name, GF_LOG_TRACE, "NODELAY enabled for socket %d", fd); return ret; @@ -363,19 +743,22 @@ __socket_nodelay (int fd) static int -__socket_keepalive (int fd, int keepalive_intvl, int keepalive_idle) +__socket_keepalive (int fd, int family, int keepalive_intvl, int keepalive_idle) { int on = 1; int ret = -1; ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)); - if (ret == -1) + if (ret == -1) { + gf_log ("socket", GF_LOG_WARNING, + "failed to set keep alive option on socket %d", fd); goto err; + } if (keepalive_intvl == GF_USE_DEFAULT_KEEPALIVE) goto done; -#ifndef GF_LINUX_HOST_OS +#if !defined(GF_LINUX_HOST_OS) && !defined(__NetBSD__) #ifdef GF_SOLARIS_HOST_OS ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive_intvl, sizeof (keepalive_intvl)); @@ -383,22 +766,35 @@ __socket_keepalive (int fd, int keepalive_intvl, int keepalive_idle) ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive_intvl, sizeof (keepalive_intvl)); #endif - if (ret == -1) + if (ret == -1) { + gf_log ("socket", GF_LOG_WARNING, + "failed to set keep alive interval on socket %d", fd); goto err; + } #else + if (family != AF_INET) + goto done; + ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_idle, sizeof (keepalive_intvl)); - if (ret == -1) + if (ret == -1) { + gf_log ("socket", GF_LOG_WARNING, + "failed to set keep idle %d on socket %d, %s", + keepalive_idle, fd, strerror(errno)); goto err; - - ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl, + } + ret = setsockopt (fd, IPPROTO_TCP , TCP_KEEPINTVL, &keepalive_intvl, sizeof (keepalive_intvl)); - if (ret == -1) + if (ret == -1) { + gf_log ("socket", GF_LOG_WARNING, + "failed to set keep interval %d on socket %d, %s", + keepalive_intvl, fd, strerror(errno)); goto err; + } #endif done: - gf_log ("", GF_LOG_TRACE, "Keep-alive enabled for socket %d, interval " + gf_log (THIS->name, GF_LOG_TRACE, "Keep-alive enabled for socket %d, interval " "%d, idle: %d", fd, keepalive_intvl, keepalive_idle); err: @@ -406,7 +802,7 @@ err: } -int +static int __socket_connect_finish (int fd) { int ret = -1; @@ -424,13 +820,13 @@ __socket_connect_finish (int fd) } -void +static void __socket_reset (rpc_transport_t *this) { socket_private_t *priv = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -445,9 +841,7 @@ __socket_reset (rpc_transport_t *this) iobuf_unref (priv->incoming.iobuf); } - if (priv->incoming.request_info != NULL) { - GF_FREE (priv->incoming.request_info); - } + GF_FREE (priv->incoming.request_info); memset (&priv->incoming, 0, sizeof (priv->incoming)); @@ -463,13 +857,13 @@ out: } -void +static void socket_set_lastfrag (uint32_t *fragsize) { (*fragsize) |= 0x80000000U; } -void +static void socket_set_frag_header_size (uint32_t size, char *haddr) { size = htonl (size); @@ -477,22 +871,21 @@ socket_set_frag_header_size (uint32_t size, char *haddr) } -void +static void socket_set_last_frag_header_size (uint32_t size, char *haddr) { socket_set_lastfrag (&size); socket_set_frag_header_size (size, haddr); } -struct ioq * +static struct ioq * __socket_ioq_new (rpc_transport_t *this, rpc_transport_msg_t *msg) { struct ioq *entry = NULL; int count = 0; uint32_t size = 0; - if (!this) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); /* TODO: use mem-pool */ entry = GF_CALLOC (1, sizeof (*entry), gf_common_mt_ioq); @@ -552,11 +945,10 @@ out: } -void +static void __socket_ioq_entry_free (struct ioq *entry) { - if (!entry) - return; + GF_VALIDATE_OR_GOTO ("socket", entry, out); list_del_init (&entry->list); if (entry->iobref) @@ -564,17 +956,20 @@ __socket_ioq_entry_free (struct ioq *entry) /* TODO: use mem-pool */ GF_FREE (entry); + +out: + return; } -void +static void __socket_ioq_flush (rpc_transport_t *this) { socket_private_t *priv = NULL; struct ioq *entry = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -588,35 +983,49 @@ out: } -int -__socket_ioq_churn_entry (rpc_transport_t *this, struct ioq *entry) +static int +__socket_ioq_churn_entry (rpc_transport_t *this, struct ioq *entry, int direct) { - int ret = -1; + int ret = -1; + socket_private_t *priv = NULL; + char a_byte = 0; ret = __socket_writev (this, entry->pending_vector, - entry->pending_count, + entry->pending_count, &entry->pending_vector, - &entry->pending_count); + &entry->pending_count); if (ret == 0) { /* current entry was completely written */ GF_ASSERT (entry->pending_count == 0); __socket_ioq_entry_free (entry); + priv = this->private; + if (priv->own_thread) { + /* + * The pipe should only remain readable if there are + * more entries after this, so drain the byte + * representing this entry. + */ + if (!direct && read(priv->pipe[0],&a_byte,1) < 1) { + gf_log(this->name,GF_LOG_WARNING, + "read error on pipe"); + } + } } return ret; } -int +static int __socket_ioq_churn (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = 0; struct ioq *entry = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -624,16 +1033,16 @@ __socket_ioq_churn (rpc_transport_t *this) /* pick next entry */ entry = priv->ioq_next; - ret = __socket_ioq_churn_entry (this, entry); + ret = __socket_ioq_churn_entry (this, entry, 0); if (ret != 0) break; } - if (list_empty (&priv->ioq)) { + if (!priv->own_thread && list_empty (&priv->ioq)) { /* all pending writes done, not interested in POLLOUT */ priv->idx = event_select_on (this->ctx->event_pool, - priv->sock, priv->idx, -1, 0); + priv->sock, priv->idx, -1, 0); } out: @@ -641,14 +1050,14 @@ out: } -int +static int socket_event_poll_err (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = -1; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -666,14 +1075,14 @@ out: } -int +static int socket_event_poll_out (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = -1; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -696,56 +1105,58 @@ out: } -inline int +static inline int __socket_read_simple_msg (rpc_transport_t *this) { - socket_private_t *priv = NULL; - int ret = 0; - uint32_t remaining_size = 0; - size_t bytes_read = 0; + int ret = 0; + uint32_t remaining_size = 0; + size_t bytes_read = 0; + socket_private_t *priv = NULL; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - switch (priv->incoming.frag.simple_state) { + in = &priv->incoming; + frag = &in->frag; + + switch (frag->simple_state) { case SP_STATE_SIMPLE_MSG_INIT: - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; __socket_proto_init_pending (priv, remaining_size); - priv->incoming.frag.simple_state = - SP_STATE_READING_SIMPLE_MSG; + frag->simple_state = SP_STATE_READING_SIMPLE_MSG; /* fall through */ case SP_STATE_READING_SIMPLE_MSG: ret = 0; - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; if (remaining_size > 0) { ret = __socket_readv (this, - priv->incoming.pending_vector, 1, - &priv->incoming.pending_vector, - &priv->incoming.pending_count, + in->pending_vector, 1, + &in->pending_vector, + &in->pending_count, &bytes_read); } if (ret == -1) { - gf_log (this->name, GF_LOG_TRACE, + gf_log (this->name, GF_LOG_WARNING, "reading from socket failed. Error (%s), " "peer (%s)", strerror (errno), this->peerinfo.identifier); break; } - priv->incoming.frag.bytes_read += bytes_read; - priv->incoming.frag.fragcurrent += bytes_read; + frag->bytes_read += bytes_read; + frag->fragcurrent += bytes_read; if (ret > 0) { gf_log (this->name, GF_LOG_TRACE, @@ -754,8 +1165,7 @@ __socket_read_simple_msg (rpc_transport_t *this) } if (ret == 0) { - priv->incoming.frag.simple_state - = SP_STATE_SIMPLE_MSG_INIT; + frag->simple_state = SP_STATE_SIMPLE_MSG_INIT; } } @@ -764,7 +1174,7 @@ out: } -inline int +static inline int __socket_read_simple_request (rpc_transport_t *this) { return __socket_read_simple_msg (this); @@ -775,9 +1185,14 @@ __socket_read_simple_request (rpc_transport_t *this) #define rpc_verf_addr(fragcurrent) (fragcurrent - 4) +#define rpc_msgtype_addr(buf) (buf + 4) -inline int -__socket_read_vectored_request (rpc_transport_t *this) +#define rpc_prognum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 4) +#define rpc_progver_addr(buf) (buf + RPC_MSGTYPE_SIZE + 8) +#define rpc_procnum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 12) + +static inline int +__socket_read_vectored_request (rpc_transport_t *this, rpcsvc_vector_sizer vector_sizer) { socket_private_t *priv = NULL; int ret = 0; @@ -785,17 +1200,27 @@ __socket_read_vectored_request (rpc_transport_t *this) char *addr = NULL; struct iobuf *iobuf = NULL; uint32_t remaining_size = 0; - uint32_t gluster_write_proc_len = 0; - gfs3_write_req write_req = {{0,},}; + ssize_t readsize = 0; + size_t size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + sp_rpcfrag_request_state_t *request = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - switch (priv->incoming.frag.call_body.request.vector_state) { + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + request = &frag->call_body.request; + + switch (request->vector_state) { case SP_STATE_VECTORED_REQUEST_INIT: - addr = rpc_cred_addr (iobuf_ptr (priv->incoming.iobuf)); + request->vector_sizer_state = 0; + + addr = rpc_cred_addr (iobuf_ptr (in->iobuf)); /* also read verf flavour and verflen */ credlen = ntoh32 (*((uint32_t *)addr)) @@ -803,88 +1228,114 @@ __socket_read_vectored_request (rpc_transport_t *this) __socket_proto_init_pending (priv, credlen); - priv->incoming.frag.call_body.request.vector_state = - SP_STATE_READING_CREDBYTES; + request->vector_state = SP_STATE_READING_CREDBYTES; /* fall through */ case SP_STATE_READING_CREDBYTES: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.request.vector_state = - SP_STATE_READ_CREDBYTES; + request->vector_state = SP_STATE_READ_CREDBYTES; /* fall through */ case SP_STATE_READ_CREDBYTES: - addr = rpc_verf_addr (priv->incoming.frag.fragcurrent); - - /* FIXME: Also handle procedures other than glusterfs-write - * here - */ - /* also read proc-header */ - gluster_write_proc_len = xdr_sizeof ((xdrproc_t) xdr_gfs3_write_req, - &write_req); + addr = rpc_verf_addr (frag->fragcurrent); + verflen = ntoh32 (*((uint32_t *)addr)); - if (gluster_write_proc_len == 0) { - gf_log (this->name, GF_LOG_DEBUG, - "xdr_sizeof on gfs3_write_req failed"); - ret = -1; - goto out; + if (verflen == 0) { + request->vector_state = SP_STATE_READ_VERFBYTES; + goto sp_state_read_verfbytes; } - - verflen = ntoh32 (*((uint32_t *)addr)) - + gluster_write_proc_len; - __socket_proto_init_pending (priv, verflen); - priv->incoming.frag.call_body.request.vector_state - = SP_STATE_READING_VERFBYTES; + request->vector_state = SP_STATE_READING_VERFBYTES; /* fall through */ case SP_STATE_READING_VERFBYTES: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.request.vector_state = - SP_STATE_READ_VERFBYTES; + request->vector_state = SP_STATE_READ_VERFBYTES; /* fall through */ case SP_STATE_READ_VERFBYTES: - if (priv->incoming.payload_vector.iov_base == NULL) { - iobuf = iobuf_get (this->ctx->iobuf_pool); +sp_state_read_verfbytes: + /* set the base_addr 'persistently' across multiple calls + into the state machine */ + in->proghdr_base_addr = frag->fragcurrent; + + request->vector_sizer_state = + vector_sizer (request->vector_sizer_state, + &readsize, in->proghdr_base_addr, + frag->fragcurrent); + __socket_proto_init_pending (priv, readsize); + + request->vector_state = SP_STATE_READING_PROGHDR; + + /* fall through */ + + case SP_STATE_READING_PROGHDR: + __socket_proto_read (priv, ret); + + request->vector_state = SP_STATE_READ_PROGHDR; + + /* fall through */ + + case SP_STATE_READ_PROGHDR: +sp_state_read_proghdr: + request->vector_sizer_state = + vector_sizer (request->vector_sizer_state, + &readsize, in->proghdr_base_addr, + frag->fragcurrent); + if (readsize == 0) { + request->vector_state = SP_STATE_READ_PROGHDR_XDATA; + goto sp_state_read_proghdr_xdata; + } + + __socket_proto_init_pending (priv, readsize); + + request->vector_state = SP_STATE_READING_PROGHDR_XDATA; + + /* fall through */ + + case SP_STATE_READING_PROGHDR_XDATA: + __socket_proto_read (priv, ret); + + request->vector_state = SP_STATE_READ_PROGHDR; + /* check if the vector_sizer() has more to say */ + goto sp_state_read_proghdr; + + case SP_STATE_READ_PROGHDR_XDATA: +sp_state_read_proghdr_xdata: + if (in->payload_vector.iov_base == NULL) { + + size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; + iobuf = iobuf_get2 (this->ctx->iobuf_pool, size); if (!iobuf) { - gf_log (this->name, GF_LOG_ERROR, - "unable to allocate IO buffer " - "for peer %s", - this->peerinfo.identifier); ret = -1; break; } - if (priv->incoming.iobref == NULL) { - priv->incoming.iobref = iobref_new (); - if (priv->incoming.iobref == NULL) { - gf_log (this->name, GF_LOG_ERROR, - "out of memory"); + if (in->iobref == NULL) { + in->iobref = iobref_new (); + if (in->iobref == NULL) { ret = -1; iobuf_unref (iobuf); break; } } - iobref_add (priv->incoming.iobref, iobuf); + iobref_add (in->iobref, iobuf); iobuf_unref (iobuf); - priv->incoming.payload_vector.iov_base - = iobuf_ptr (iobuf); + in->payload_vector.iov_base = iobuf_ptr (iobuf); - priv->incoming.frag.fragcurrent = iobuf_ptr (iobuf); + frag->fragcurrent = iobuf_ptr (iobuf); } - priv->incoming.frag.call_body.request.vector_state = - SP_STATE_READING_PROG; + request->vector_state = SP_STATE_READING_PROG; /* fall through */ @@ -895,19 +1346,15 @@ __socket_read_vectored_request (rpc_transport_t *this) ret = __socket_read_simple_msg (this); - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; - if ((ret == -1) - || ((ret == 0) - && (remaining_size == 0) - && RPC_LASTFRAG (priv->incoming.fraghdr))) { - priv->incoming.frag.call_body.request.vector_state - = SP_STATE_VECTORED_REQUEST_INIT; - priv->incoming.payload_vector.iov_len - = (unsigned long)priv->incoming.frag.fragcurrent - - (unsigned long) - priv->incoming.payload_vector.iov_base; + if ((ret == -1) || + ((ret == 0) && (remaining_size == 0) + && RPC_LASTFRAG (in->fraghdr))) { + request->vector_state = SP_STATE_VECTORED_REQUEST_INIT; + in->payload_vector.iov_len + = ((unsigned long)frag->fragcurrent + - (unsigned long)in->payload_vector.iov_base); } break; } @@ -916,70 +1363,77 @@ out: return ret; } - -#define rpc_msgtype_addr(buf) (buf + 4) - -#define rpc_prognum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 4) - -#define rpc_procnum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 12) - - -inline int +static inline int __socket_read_request (rpc_transport_t *this) { socket_private_t *priv = NULL; - uint32_t prognum = 0, procnum = 0; + uint32_t prognum = 0, procnum = 0, progver = 0; uint32_t remaining_size = 0; int ret = -1; char *buf = NULL; + rpcsvc_vector_sizer vector_sizer = NULL; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + sp_rpcfrag_request_state_t *request = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - switch (priv->incoming.frag.call_body.request.header_state) { + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + request = &frag->call_body.request; + + switch (request->header_state) { case SP_STATE_REQUEST_HEADER_INIT: __socket_proto_init_pending (priv, RPC_CALL_BODY_SIZE); - priv->incoming.frag.call_body.request.header_state - = SP_STATE_READING_RPCHDR1; + request->header_state = SP_STATE_READING_RPCHDR1; /* fall through */ case SP_STATE_READING_RPCHDR1: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.request.header_state = - SP_STATE_READ_RPCHDR1; + request->header_state = SP_STATE_READ_RPCHDR1; /* fall through */ case SP_STATE_READ_RPCHDR1: - buf = rpc_prognum_addr (iobuf_ptr (priv->incoming.iobuf)); + buf = rpc_prognum_addr (iobuf_ptr (in->iobuf)); prognum = ntoh32 (*((uint32_t *)buf)); - buf = rpc_procnum_addr (iobuf_ptr (priv->incoming.iobuf)); + buf = rpc_progver_addr (iobuf_ptr (in->iobuf)); + progver = ntoh32 (*((uint32_t *)buf)); + + buf = rpc_procnum_addr (iobuf_ptr (in->iobuf)); procnum = ntoh32 (*((uint32_t *)buf)); - if ((prognum == GLUSTER3_1_FOP_PROGRAM) - && (procnum == GF_FOP_WRITE)) { - ret = __socket_read_vectored_request (this); + if (priv->is_server) { + /* this check is needed as rpcsvc and rpc-clnt + * actor structures are not same */ + vector_sizer = + rpcsvc_get_program_vector_sizer ((rpcsvc_t *)this->mydata, + prognum, progver, procnum); + } + + if (vector_sizer) { + ret = __socket_read_vectored_request (this, vector_sizer); } else { ret = __socket_read_simple_request (this); } - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; if ((ret == -1) || ((ret == 0) && (remaining_size == 0) - && (RPC_LASTFRAG (priv->incoming.fraghdr)))) { - priv->incoming.frag.call_body.request.header_state = - SP_STATE_REQUEST_HEADER_INIT; + && (RPC_LASTFRAG (in->fraghdr)))) { + request->header_state = SP_STATE_REQUEST_HEADER_INIT; } break; @@ -990,35 +1444,40 @@ out: } -inline int +static inline int __socket_read_accepted_successful_reply (rpc_transport_t *this) { - socket_private_t *priv = NULL; - int ret = 0; - struct iobuf *iobuf = NULL; - uint32_t gluster_read_rsp_hdr_len = 0; - gfs3_read_rsp read_rsp = {0, }; - - if (!this || !this->private) - goto out; + socket_private_t *priv = NULL; + int ret = 0; + struct iobuf *iobuf = NULL; + gfs3_read_rsp read_rsp = {0, }; + ssize_t size = 0; + ssize_t default_read_size = 0; + char *proghdr_buf = NULL; + XDR xdr; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - switch (priv->incoming.frag.call_body.reply.accepted_success_state) { + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + switch (frag->call_body.reply.accepted_success_state) { case SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT: - gluster_read_rsp_hdr_len = xdr_sizeof ((xdrproc_t) xdr_gfs3_read_rsp, - &read_rsp); + default_read_size = xdr_sizeof ((xdrproc_t) xdr_gfs3_read_rsp, + &read_rsp); - if (gluster_read_rsp_hdr_len == 0) { - gf_log (this->name, GF_LOG_DEBUG, - "xdr_sizeof on gfs3_read_rsp failed"); - ret = -1; - goto out; - } - __socket_proto_init_pending (priv, gluster_read_rsp_hdr_len); + proghdr_buf = frag->fragcurrent; + + __socket_proto_init_pending (priv, default_read_size); - priv->incoming.frag.call_body.reply.accepted_success_state + frag->call_body.reply.accepted_success_state = SP_STATE_READING_PROC_HEADER; /* fall through */ @@ -1026,34 +1485,70 @@ __socket_read_accepted_successful_reply (rpc_transport_t *this) case SP_STATE_READING_PROC_HEADER: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.reply.accepted_success_state - = SP_STATE_READ_PROC_HEADER; + /* there can be 'xdata' in read response, figure it out */ + xdrmem_create (&xdr, proghdr_buf, default_read_size, + XDR_DECODE); + + /* This will fail if there is xdata sent from server, if not, + well and good, we don't need to worry about */ + xdr_gfs3_read_rsp (&xdr, &read_rsp); + + free (read_rsp.xdata.xdata_val); + + /* need to round off to proper roof (%4), as XDR packing pads + the end of opaque object with '0' */ + size = roof (read_rsp.xdata.xdata_len, 4); + + if (!size) { + frag->call_body.reply.accepted_success_state + = SP_STATE_READ_PROC_OPAQUE; + goto read_proc_opaque; + } + + __socket_proto_init_pending (priv, size); + + frag->call_body.reply.accepted_success_state + = SP_STATE_READING_PROC_OPAQUE; + + case SP_STATE_READING_PROC_OPAQUE: + __socket_proto_read (priv, ret); - if (priv->incoming.payload_vector.iov_base == NULL) { - iobuf = iobuf_get (this->ctx->iobuf_pool); + frag->call_body.reply.accepted_success_state + = SP_STATE_READ_PROC_OPAQUE; + + case SP_STATE_READ_PROC_OPAQUE: + read_proc_opaque: + if (in->payload_vector.iov_base == NULL) { + + size = (RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read); + + iobuf = iobuf_get2 (this->ctx->iobuf_pool, size); if (iobuf == NULL) { ret = -1; goto out; } - if (priv->incoming.iobref == NULL) { - priv->incoming.iobref = iobref_new (); - if (priv->incoming.iobref == NULL) { + if (in->iobref == NULL) { + in->iobref = iobref_new (); + if (in->iobref == NULL) { ret = -1; iobuf_unref (iobuf); goto out; } } - iobref_add (priv->incoming.iobref, iobuf); + iobref_add (in->iobref, iobuf); iobuf_unref (iobuf); - priv->incoming.payload_vector.iov_base - = iobuf_ptr (iobuf); + in->payload_vector.iov_base = iobuf_ptr (iobuf); + + in->payload_vector.iov_len = size; } - priv->incoming.frag.fragcurrent - = priv->incoming.payload_vector.iov_base; + frag->fragcurrent = in->payload_vector.iov_base; + + frag->call_body.reply.accepted_success_state + = SP_STATE_READ_PROC_HEADER; /* fall through */ @@ -1061,9 +1556,8 @@ __socket_read_accepted_successful_reply (rpc_transport_t *this) /* now read the entire remaining msg into new iobuf */ ret = __socket_read_simple_msg (this); if ((ret == -1) - || ((ret == 0) - && RPC_LASTFRAG (priv->incoming.fraghdr))) { - priv->incoming.frag.call_body.reply.accepted_success_state + || ((ret == 0) && RPC_LASTFRAG (in->fraghdr))) { + frag->call_body.reply.accepted_success_state = SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT; } @@ -1077,7 +1571,7 @@ out: #define rpc_reply_verflen_addr(fragcurrent) ((char *)fragcurrent - 4) #define rpc_reply_accept_status_addr(fragcurrent) ((char *)fragcurrent - 4) -inline int +static inline int __socket_read_accepted_reply (rpc_transport_t *this) { socket_private_t *priv = NULL; @@ -1085,19 +1579,24 @@ __socket_read_accepted_reply (rpc_transport_t *this) char *buf = NULL; uint32_t verflen = 0, len = 0; uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; - switch (priv->incoming.frag.call_body.reply.accepted_state) { + switch (frag->call_body.reply.accepted_state) { case SP_STATE_ACCEPTED_REPLY_INIT: __socket_proto_init_pending (priv, RPC_AUTH_FLAVOUR_N_LENGTH_SIZE); - priv->incoming.frag.call_body.reply.accepted_state + frag->call_body.reply.accepted_state = SP_STATE_READING_REPLY_VERFLEN; /* fall through */ @@ -1105,13 +1604,13 @@ __socket_read_accepted_reply (rpc_transport_t *this) case SP_STATE_READING_REPLY_VERFLEN: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.reply.accepted_state + frag->call_body.reply.accepted_state = SP_STATE_READ_REPLY_VERFLEN; /* fall through */ case SP_STATE_READ_REPLY_VERFLEN: - buf = rpc_reply_verflen_addr (priv->incoming.frag.fragcurrent); + buf = rpc_reply_verflen_addr (frag->fragcurrent); verflen = ntoh32 (*((uint32_t *) buf)); @@ -1120,7 +1619,7 @@ __socket_read_accepted_reply (rpc_transport_t *this) __socket_proto_init_pending (priv, len); - priv->incoming.frag.call_body.reply.accepted_state + frag->call_body.reply.accepted_state = SP_STATE_READING_REPLY_VERFBYTES; /* fall through */ @@ -1128,19 +1627,19 @@ __socket_read_accepted_reply (rpc_transport_t *this) case SP_STATE_READING_REPLY_VERFBYTES: __socket_proto_read (priv, ret); - priv->incoming.frag.call_body.reply.accepted_state + frag->call_body.reply.accepted_state = SP_STATE_READ_REPLY_VERFBYTES; - buf = rpc_reply_accept_status_addr (priv->incoming.frag.fragcurrent); + buf = rpc_reply_accept_status_addr (frag->fragcurrent); - priv->incoming.frag.call_body.reply.accept_status + frag->call_body.reply.accept_status = ntoh32 (*(uint32_t *) buf); /* fall through */ case SP_STATE_READ_REPLY_VERFBYTES: - if (priv->incoming.frag.call_body.reply.accept_status + if (frag->call_body.reply.accept_status == SUCCESS) { ret = __socket_read_accepted_successful_reply (this); } else { @@ -1150,14 +1649,13 @@ __socket_read_accepted_reply (rpc_transport_t *this) ret = __socket_read_simple_msg (this); } - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) + - frag->bytes_read; if ((ret == -1) - || ((ret == 0) - && (remaining_size == 0) - && (RPC_LASTFRAG (priv->incoming.fraghdr)))) { - priv->incoming.frag.call_body.reply.accepted_state + || ((ret == 0) && (remaining_size == 0) + && (RPC_LASTFRAG (in->fraghdr)))) { + frag->call_body.reply.accepted_state = SP_STATE_ACCEPTED_REPLY_INIT; } @@ -1169,7 +1667,7 @@ out: } -inline int +static inline int __socket_read_denied_reply (rpc_transport_t *this) { return __socket_read_simple_msg (this); @@ -1179,25 +1677,29 @@ __socket_read_denied_reply (rpc_transport_t *this) #define rpc_reply_status_addr(fragcurrent) ((char *)fragcurrent - 4) -inline int +static inline int __socket_read_vectored_reply (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = 0; char *buf = NULL; uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; + in = &priv->incoming; + frag = &in->frag; - switch (priv->incoming.frag.call_body.reply.status_state) { + switch (frag->call_body.reply.status_state) { case SP_STATE_ACCEPTED_REPLY_INIT: __socket_proto_init_pending (priv, RPC_REPLY_STATUS_SIZE); - priv->incoming.frag.call_body.reply.status_state + frag->call_body.reply.status_state = SP_STATE_READING_REPLY_STATUS; /* fall through */ @@ -1205,37 +1707,33 @@ __socket_read_vectored_reply (rpc_transport_t *this) case SP_STATE_READING_REPLY_STATUS: __socket_proto_read (priv, ret); - buf = rpc_reply_status_addr (priv->incoming.frag.fragcurrent); + buf = rpc_reply_status_addr (frag->fragcurrent); - priv->incoming.frag.call_body.reply.accept_status + frag->call_body.reply.accept_status = ntoh32 (*((uint32_t *) buf)); - priv->incoming.frag.call_body.reply.status_state + frag->call_body.reply.status_state = SP_STATE_READ_REPLY_STATUS; /* fall through */ case SP_STATE_READ_REPLY_STATUS: - if (priv->incoming.frag.call_body.reply.accept_status - == MSG_ACCEPTED) { + if (frag->call_body.reply.accept_status == MSG_ACCEPTED) { ret = __socket_read_accepted_reply (this); } else { ret = __socket_read_denied_reply (this); } - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; if ((ret == -1) - || ((ret == 0) - && (remaining_size == 0) - && (RPC_LASTFRAG (priv->incoming.fraghdr)))) { - priv->incoming.frag.call_body.reply.status_state + || ((ret == 0) && (remaining_size == 0) + && (RPC_LASTFRAG (in->fraghdr)))) { + frag->call_body.reply.status_state = SP_STATE_ACCEPTED_REPLY_INIT; - priv->incoming.payload_vector.iov_len - = (unsigned long)priv->incoming.frag.fragcurrent - - (unsigned long) - priv->incoming.payload_vector.iov_base; + in->payload_vector.iov_len + = (unsigned long)frag->fragcurrent + - (unsigned long)in->payload_vector.iov_base; } break; } @@ -1245,7 +1743,7 @@ out: } -inline int +static inline int __socket_read_simple_reply (rpc_transport_t *this) { return __socket_read_simple_msg (this); @@ -1253,7 +1751,7 @@ __socket_read_simple_reply (rpc_transport_t *this) #define rpc_xid_addr(buf) (buf) -inline int +static inline int __socket_read_reply (rpc_transport_t *this) { socket_private_t *priv = NULL; @@ -1261,27 +1759,29 @@ __socket_read_reply (rpc_transport_t *this) int32_t ret = -1; rpc_request_info_t *request_info = NULL; char map_xid = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; + in = &priv->incoming; + frag = &in->frag; - buf = rpc_xid_addr (iobuf_ptr (priv->incoming.iobuf)); + buf = rpc_xid_addr (iobuf_ptr (in->iobuf)); - if (priv->incoming.request_info == NULL) { - priv->incoming.request_info = GF_CALLOC (1, - sizeof (*request_info), - gf_common_mt_rpc_trans_reqinfo_t); - if (priv->incoming.request_info == NULL) { - gf_log (this->name, GF_LOG_ERROR, "out of memory"); + if (in->request_info == NULL) { + in->request_info = GF_CALLOC (1, sizeof (*request_info), + gf_common_mt_rpc_trans_reqinfo_t); + if (in->request_info == NULL) { goto out; } map_xid = 1; } - request_info = priv->incoming.request_info; + request_info = in->request_info; if (map_xid) { request_info->xid = ntoh32 (*((uint32_t *) buf)); @@ -1293,22 +1793,22 @@ __socket_read_reply (rpc_transport_t *this) { ret = rpc_transport_notify (this, RPC_TRANSPORT_MAP_XID_REQUEST, - priv->incoming.request_info); + in->request_info); } pthread_mutex_lock (&priv->lock); if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "notify for event MAP_XID failed"); goto out; } } - if ((request_info->prognum == GLUSTER3_1_FOP_PROGRAM) + if ((request_info->prognum == GLUSTER_FOP_PROGRAM) && (request_info->procnum == GF_FOP_READ)) { if (map_xid && request_info->rsp.rsp_payload_count != 0) { - priv->incoming.iobref - = iobref_ref (request_info->rsp.rsp_iobref); - priv->incoming.payload_vector - = *request_info->rsp.rsp_payload; + in->iobref = iobref_ref (request_info->rsp.rsp_iobref); + in->payload_vector = *request_info->rsp.rsp_payload; } ret = __socket_read_vectored_reply (this); @@ -1321,62 +1821,65 @@ out: /* returns the number of bytes yet to be read in a fragment */ -inline int +static inline int __socket_read_frag (rpc_transport_t *this) { socket_private_t *priv = NULL; int32_t ret = 0; char *buf = NULL; uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; - switch (priv->incoming.frag.state) { + switch (frag->state) { case SP_STATE_NADA: __socket_proto_init_pending (priv, RPC_MSGTYPE_SIZE); - priv->incoming.frag.state = SP_STATE_READING_MSGTYPE; + frag->state = SP_STATE_READING_MSGTYPE; /* fall through */ case SP_STATE_READING_MSGTYPE: __socket_proto_read (priv, ret); - priv->incoming.frag.state = SP_STATE_READ_MSGTYPE; + frag->state = SP_STATE_READ_MSGTYPE; /* fall through */ case SP_STATE_READ_MSGTYPE: - buf = rpc_msgtype_addr (iobuf_ptr (priv->incoming.iobuf)); - priv->incoming.msg_type = ntoh32 (*((uint32_t *)buf)); + buf = rpc_msgtype_addr (iobuf_ptr (in->iobuf)); + in->msg_type = ntoh32 (*((uint32_t *)buf)); - if (priv->incoming.msg_type == CALL) { + if (in->msg_type == CALL) { ret = __socket_read_request (this); - } else if (priv->incoming.msg_type == REPLY) { + } else if (in->msg_type == REPLY) { ret = __socket_read_reply (this); - } else if (priv->incoming.msg_type == GF_UNIVERSAL_ANSWER) { + } else if (in->msg_type == GF_UNIVERSAL_ANSWER) { gf_log ("rpc", GF_LOG_ERROR, "older version of protocol/process trying to " - "connect from %s. use newer verison on that node", + "connect from %s. use newer version on that node", this->peerinfo.identifier); } else { gf_log ("rpc", GF_LOG_ERROR, "wrong MSG-TYPE (%d) received from %s", - priv->incoming.msg_type, + in->msg_type, this->peerinfo.identifier); ret = -1; } - remaining_size = RPC_FRAGSIZE (priv->incoming.fraghdr) - - priv->incoming.frag.bytes_read; + remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read; if ((ret == -1) - || ((ret == 0) - && (remaining_size == 0) - && (RPC_LASTFRAG (priv->incoming.fraghdr)))) { - priv->incoming.frag.state = SP_STATE_NADA; + || ((ret == 0) && (remaining_size == 0) + && (RPC_LASTFRAG (in->fraghdr)))) { + frag->state = SP_STATE_NADA; } break; @@ -1387,31 +1890,36 @@ out: } -inline +static inline void __socket_reset_priv (socket_private_t *priv) { - if (priv->incoming.iobref) { - iobref_unref (priv->incoming.iobref); - priv->incoming.iobref = NULL; + struct gf_sock_incoming *in = NULL; + + /* used to reduce the indirection */ + in = &priv->incoming; + + if (in->iobref) { + iobref_unref (in->iobref); + in->iobref = NULL; } - if (priv->incoming.iobuf) { - iobuf_unref (priv->incoming.iobuf); + if (in->iobuf) { + iobuf_unref (in->iobuf); } - if (priv->incoming.request_info != NULL) { - GF_FREE (priv->incoming.request_info); - priv->incoming.request_info = NULL; + if (in->request_info != NULL) { + GF_FREE (in->request_info); + in->request_info = NULL; } - memset (&priv->incoming.payload_vector, 0, - sizeof (priv->incoming.payload_vector)); + memset (&in->payload_vector, 0, + sizeof (in->payload_vector)); - priv->incoming.iobuf = NULL; + in->iobuf = NULL; } -int +static int __socket_proto_state_machine (rpc_transport_t *this, rpc_transport_pollin_t **pollin) { @@ -1420,55 +1928,40 @@ __socket_proto_state_machine (rpc_transport_t *this, struct iobuf *iobuf = NULL; struct iobref *iobref = NULL; struct iovec vector[2]; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - while (priv->incoming.record_state != SP_STATE_COMPLETE) { - switch (priv->incoming.record_state) { + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; - case SP_STATE_NADA: - iobuf = iobuf_get (this->ctx->iobuf_pool); - if (!iobuf) { - gf_log (this->name, GF_LOG_ERROR, - "unable to allocate IO buffer " - "for peer %s", - this->peerinfo.identifier); - ret = -ENOMEM; - goto out; - } + while (in->record_state != SP_STATE_COMPLETE) { + switch (in->record_state) { - priv->incoming.iobuf = iobuf; - priv->incoming.iobuf_size = 0; - priv->incoming.total_bytes_read = 0; - priv->incoming.payload_vector.iov_len = 0; + case SP_STATE_NADA: + in->total_bytes_read = 0; + in->payload_vector.iov_len = 0; - priv->incoming.pending_vector = priv->incoming.vector; - priv->incoming.pending_vector->iov_base = - &priv->incoming.fraghdr; + in->pending_vector = in->vector; + in->pending_vector->iov_base = &in->fraghdr; - priv->incoming.frag.fragcurrent = iobuf_ptr (iobuf); - priv->incoming.pending_vector->iov_len = - sizeof (priv->incoming.fraghdr); + in->pending_vector->iov_len = sizeof (in->fraghdr); - priv->incoming.record_state = SP_STATE_READING_FRAGHDR; + in->record_state = SP_STATE_READING_FRAGHDR; /* fall through */ case SP_STATE_READING_FRAGHDR: - ret = __socket_readv (this, - priv->incoming.pending_vector, 1, - &priv->incoming.pending_vector, - &priv->incoming.pending_count, + ret = __socket_readv (this, in->pending_vector, 1, + &in->pending_vector, + &in->pending_count, NULL); - if (ret == -1) { - gf_log (this->name, GF_LOG_TRACE, - "reading from socket failed. Error (%s), " - "peer (%s)", strerror (errno), - this->peerinfo.identifier); + if (ret == -1) goto out; - } if (ret > 0) { gf_log (this->name, GF_LOG_TRACE, "partial " @@ -1477,33 +1970,40 @@ __socket_proto_state_machine (rpc_transport_t *this, } if (ret == 0) { - priv->incoming.record_state = - SP_STATE_READ_FRAGHDR; + in->record_state = SP_STATE_READ_FRAGHDR; } /* fall through */ case SP_STATE_READ_FRAGHDR: - priv->incoming.fraghdr = ntoh32 (priv->incoming.fraghdr); - priv->incoming.record_state = SP_STATE_READING_FRAG; - priv->incoming.total_bytes_read - += RPC_FRAGSIZE(priv->incoming.fraghdr); + in->fraghdr = ntoh32 (in->fraghdr); + in->total_bytes_read += RPC_FRAGSIZE(in->fraghdr); + iobuf = iobuf_get2 (this->ctx->iobuf_pool, + (in->total_bytes_read + + sizeof (in->fraghdr))); + if (!iobuf) { + ret = -ENOMEM; + goto out; + } + + in->iobuf = iobuf; + in->iobuf_size = 0; + frag->fragcurrent = iobuf_ptr (iobuf); + in->record_state = SP_STATE_READING_FRAG; /* fall through */ case SP_STATE_READING_FRAG: ret = __socket_read_frag (this); - if ((ret == -1) - || (priv->incoming.frag.bytes_read != - RPC_FRAGSIZE (priv->incoming.fraghdr))) { + if ((ret == -1) || + (frag->bytes_read != RPC_FRAGSIZE (in->fraghdr))) { goto out; } - priv->incoming.frag.bytes_read = 0; + frag->bytes_read = 0; - if (!RPC_LASTFRAG (priv->incoming.fraghdr)) { - priv->incoming.record_state = - SP_STATE_READING_FRAGHDR; + if (!RPC_LASTFRAG (in->fraghdr)) { + in->record_state = SP_STATE_READING_FRAGHDR; break; } @@ -1512,71 +2012,65 @@ __socket_proto_state_machine (rpc_transport_t *this, */ if (pollin != NULL) { int count = 0; - priv->incoming.iobuf_size - = priv->incoming.total_bytes_read - - priv->incoming.payload_vector.iov_len; + in->iobuf_size = (in->total_bytes_read - + in->payload_vector.iov_len); memset (vector, 0, sizeof (vector)); - if (priv->incoming.iobref == NULL) { - priv->incoming.iobref = iobref_new (); - if (priv->incoming.iobref == NULL) { - gf_log (this->name, - GF_LOG_ERROR, - "out of memory"); + if (in->iobref == NULL) { + in->iobref = iobref_new (); + if (in->iobref == NULL) { ret = -1; goto out; } } - vector[count].iov_base - = iobuf_ptr (priv->incoming.iobuf); - vector[count].iov_len - = priv->incoming.iobuf_size; + vector[count].iov_base = iobuf_ptr (in->iobuf); + vector[count].iov_len = in->iobuf_size; - iobref = priv->incoming.iobref; + iobref = in->iobref; count++; - if (priv->incoming.payload_vector.iov_base - != NULL) { - vector[count] - = priv->incoming.payload_vector; + if (in->payload_vector.iov_base != NULL) { + vector[count] = in->payload_vector; count++; } *pollin = rpc_transport_pollin_alloc (this, vector, count, - priv->incoming.iobuf, + in->iobuf, iobref, - priv->incoming.request_info); - iobuf_unref (priv->incoming.iobuf); - priv->incoming.iobuf = NULL; + in->request_info); + iobuf_unref (in->iobuf); + in->iobuf = NULL; if (*pollin == NULL) { + gf_log (this->name, GF_LOG_WARNING, + "transport pollin allocation failed"); ret = -1; goto out; } - if (priv->incoming.msg_type == REPLY) + if (in->msg_type == REPLY) (*pollin)->is_reply = 1; - priv->incoming.request_info = NULL; + in->request_info = NULL; } - priv->incoming.record_state = SP_STATE_COMPLETE; + in->record_state = SP_STATE_COMPLETE; break; case SP_STATE_COMPLETE: /* control should not reach here */ - gf_log (this->name, GF_LOG_DEBUG, "control reached to " + gf_log (this->name, GF_LOG_WARNING, "control reached to " "SP_STATE_COMPLETE, which should not have " "happened"); break; } } - if (priv->incoming.record_state == SP_STATE_COMPLETE) { - priv->incoming.record_state = SP_STATE_NADA; + if (in->record_state == SP_STATE_COMPLETE) { + in->record_state = SP_STATE_NADA; __socket_reset_priv (priv); } @@ -1588,41 +2082,45 @@ out: } -int +static int socket_proto_state_machine (rpc_transport_t *this, rpc_transport_pollin_t **pollin) { socket_private_t *priv = NULL; - int ret = 0; + int ret = 0; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); - priv = this->private; + priv = this->private; - pthread_mutex_lock (&priv->lock); - { - ret = __socket_proto_state_machine (this, pollin); - } + pthread_mutex_lock (&priv->lock); + { + ret = __socket_proto_state_machine (this, pollin); + } pthread_mutex_unlock (&priv->lock); out: - return ret; + return ret; } -int +static int socket_event_poll_in (rpc_transport_t *this) { int ret = -1; rpc_transport_pollin_t *pollin = NULL; + socket_private_t *priv = this->private; ret = socket_proto_state_machine (this, &pollin); if (pollin != NULL) { + priv->ot_state = OT_CALLBACK; ret = rpc_transport_notify (this, RPC_TRANSPORT_MSG_RECEIVED, pollin); - + if (priv->ot_state == OT_CALLBACK) { + priv->ot_state = OT_RUNNING; + } rpc_transport_pollin_destroy (pollin); } @@ -1630,7 +2128,7 @@ socket_event_poll_in (rpc_transport_t *this) } -int +static int socket_connect_finish (rpc_transport_t *this) { int ret = -1; @@ -1638,58 +2136,57 @@ socket_connect_finish (rpc_transport_t *this) rpc_transport_event_t event = 0; char notify_rpc = 0; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; pthread_mutex_lock (&priv->lock); { - if (priv->connected) - goto unlock; + if (priv->connected != 0) + goto unlock; + + get_transport_identifiers (this); - ret = __socket_connect_finish (priv->sock); + ret = __socket_connect_finish (priv->sock); - if (ret == -1 && errno == EINPROGRESS) - ret = 1; + if (ret == -1 && errno == EINPROGRESS) + ret = 1; - if (ret == -1 && errno != EINPROGRESS) { - if (!priv->connect_finish_log) { - gf_log (this->name, GF_LOG_ERROR, - "connection to %s failed (%s)", + if (ret == -1 && errno != EINPROGRESS) { + if (!priv->connect_finish_log) { + gf_log (this->name, GF_LOG_ERROR, + "connection to %s failed (%s)", this->peerinfo.identifier, - strerror (errno)); - priv->connect_finish_log = 1; - } - __socket_disconnect (this); - notify_rpc = 1; - event = RPC_TRANSPORT_DISCONNECT; - goto unlock; - } + strerror (errno)); + priv->connect_finish_log = 1; + } + __socket_disconnect (this); + goto unlock; + } - if (ret == 0) { - notify_rpc = 1; - - this->myinfo.sockaddr_len = - sizeof (this->myinfo.sockaddr); - - ret = getsockname (priv->sock, - SA (&this->myinfo.sockaddr), - &this->myinfo.sockaddr_len); - if (ret == -1) { - gf_log (this->name, GF_LOG_DEBUG, - "getsockname on (%d) failed (%s)", - priv->sock, strerror (errno)); - __socket_disconnect (this); - event = GF_EVENT_POLLERR; - goto unlock; - } + if (ret == 0) { + notify_rpc = 1; - priv->connected = 1; - priv->connect_finish_log = 0; - event = RPC_TRANSPORT_CONNECT; - get_transport_identifiers (this); - } + this->myinfo.sockaddr_len = + sizeof (this->myinfo.sockaddr); + + ret = getsockname (priv->sock, + SA (&this->myinfo.sockaddr), + &this->myinfo.sockaddr_len); + if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "getsockname on (%d) failed (%s)", + priv->sock, strerror (errno)); + __socket_disconnect (this); + event = GF_EVENT_POLLERR; + goto unlock; + } + + priv->connected = 1; + priv->connect_finish_log = 0; + event = RPC_TRANSPORT_CONNECT; + } } unlock: pthread_mutex_unlock (&priv->lock); @@ -1703,31 +2200,29 @@ out: /* reads rpc_requests during pollin */ -int +static int socket_event_handler (int fd, int idx, void *data, int poll_in, int poll_out, int poll_err) { - rpc_transport_t *this = NULL; + rpc_transport_t *this = NULL; socket_private_t *priv = NULL; - int ret = 0; + int ret = -1; this = data; - if (!this || !this->private || !this->xl) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); + GF_VALIDATE_OR_GOTO ("socket", this->xl, out); THIS = this->xl; priv = this->private; - pthread_mutex_lock (&priv->lock); { priv->idx = idx; } pthread_mutex_unlock (&priv->lock); - if (!priv->connected) { - ret = socket_connect_finish (this); - } + ret = (priv->connected == 1) ? 0 : socket_connect_finish(this); if (!ret && poll_out) { ret = socket_event_poll_out (this); @@ -1738,17 +2233,203 @@ socket_event_handler (int fd, int idx, void *data, } if ((ret < 0) || poll_err) { - gf_log ("transport", GF_LOG_TRACE, "disconnecting now"); + /* Logging has happened already in earlier cases */ + gf_log ("transport", ((ret >= 0) ? GF_LOG_INFO : GF_LOG_DEBUG), + "disconnecting now"); socket_event_poll_err (this); rpc_transport_unref (this); - } + } out: - return 0; + return ret; } -int +static void * +socket_poller (void *ctx) +{ + rpc_transport_t *this = ctx; + socket_private_t *priv = this->private; + struct pollfd pfd[2] = {{0,},}; + gf_boolean_t to_write = _gf_false; + int ret = 0; + uint32_t gen = 0; + + priv->ot_state = OT_RUNNING; + + if (priv->use_ssl) { + if (ssl_setup_connection(this,priv->connected) < 0) { + gf_log (this->name,GF_LOG_ERROR, "%s setup failed", + priv->connected ? "server" : "client"); + goto err; + } + } + + if (!priv->bio) { + ret = __socket_nonblock (priv->sock); + if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "NBIO on %d failed (%s)", + priv->sock, strerror (errno)); + goto err; + } + } + + if (priv->connected == 0) { + THIS = this->xl; + ret = socket_connect_finish (this); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "asynchronous socket_connect_finish failed"); + } + } + + ret = rpc_transport_notify (this->listener, + RPC_TRANSPORT_ACCEPT, this); + if (ret != 0) { + gf_log (this->name, GF_LOG_WARNING, + "asynchronous rpc_transport_notify failed"); + } + + gen = priv->ot_gen; + for (;;) { + pthread_mutex_lock(&priv->lock); + to_write = !list_empty(&priv->ioq); + pthread_mutex_unlock(&priv->lock); + pfd[0].fd = priv->pipe[0]; + pfd[0].events = POLL_MASK_ERROR; + pfd[0].revents = 0; + pfd[1].fd = priv->sock; + pfd[1].events = POLL_MASK_INPUT | POLL_MASK_ERROR; + pfd[1].revents = 0; + if (to_write) { + pfd[1].events |= POLL_MASK_OUTPUT; + } + else { + pfd[0].events |= POLL_MASK_INPUT; + } + if (poll(pfd,2,-1) < 0) { + gf_log(this->name,GF_LOG_ERROR,"poll failed"); + break; + } + if (pfd[0].revents & POLL_MASK_ERROR) { + gf_log(this->name,GF_LOG_ERROR, + "poll error on pipe"); + break; + } + /* Only glusterd actually seems to need this. */ + THIS = this->xl; + if (pfd[1].revents & POLL_MASK_INPUT) { + ret = socket_event_poll_in(this); + if (ret >= 0) { + /* Suppress errors while making progress. */ + pfd[1].revents &= ~POLL_MASK_ERROR; + } + else if (errno == ENOTCONN) { + ret = 0; + } + if (priv->ot_state == OT_PLEASE_DIE) { + gf_log (this->name, GF_LOG_TRACE, + "OT_IDLE on %p (input request)", + this); + priv->ot_state = OT_IDLE; + break; + } + } + else if (pfd[1].revents & POLL_MASK_OUTPUT) { + ret = socket_event_poll_out(this); + if (ret >= 0) { + /* Suppress errors while making progress. */ + pfd[1].revents &= ~POLL_MASK_ERROR; + } + else if (errno == ENOTCONN) { + ret = 0; + } + if (priv->ot_state == OT_PLEASE_DIE) { + gf_log (this->name, GF_LOG_TRACE, + "OT_IDLE on %p (output request)", + this); + priv->ot_state = OT_IDLE; + break; + } + } + else { + /* + * This usually means that we left poll() because + * somebody pushed a byte onto our pipe. That wakeup + * is why the pipe is there, but once awake we can do + * all the checking we need on the next iteration. + */ + ret = 0; + } + if (pfd[1].revents & POLL_MASK_ERROR) { + gf_log(this->name,GF_LOG_ERROR, + "poll error on socket"); + break; + } + if (ret < 0) { + gf_log(this->name,GF_LOG_ERROR, + "error in polling loop"); + break; + } + if (priv->ot_gen != gen) { + gf_log (this->name, GF_LOG_TRACE, + "generation mismatch, my %u != %u", + gen, priv->ot_gen); + return NULL; + } + } + +err: + /* All (and only) I/O errors should come here. */ + pthread_mutex_lock(&priv->lock); + if (priv->ssl_ssl) { + /* + * We're always responsible for this part, but only actually + * have to do it if we got far enough for ssl_ssl to be valid + * (i.e. errors in ssl_setup_connection don't count). + */ + ssl_teardown_connection(priv); + } + __socket_shutdown(this); + close(priv->sock); + priv->sock = -1; + priv->ot_state = OT_IDLE; + pthread_mutex_unlock(&priv->lock); + rpc_transport_notify (this->listener, RPC_TRANSPORT_DISCONNECT, + this); + rpc_transport_unref (this); + return NULL; +} + + +static void +socket_spawn (rpc_transport_t *this) +{ + socket_private_t *priv = this->private; + + switch (priv->ot_state) { + case OT_IDLE: + case OT_PLEASE_DIE: + break; + default: + gf_log (this->name, GF_LOG_WARNING, + "refusing to start redundant poller"); + return; + } + + priv->ot_gen += 7; + priv->ot_state = OT_SPAWNING; + gf_log (this->name, GF_LOG_TRACE, + "spawning %p with gen %u", this, priv->ot_gen); + + if (gf_thread_create(&priv->thread,NULL,socket_poller,this) != 0) { + gf_log (this->name, GF_LOG_ERROR, + "could not create poll thread"); + } +} + +static int socket_server_event_handler (int fd, int idx, void *data, int poll_in, int poll_out, int poll_err) { @@ -1760,15 +2441,16 @@ socket_server_event_handler (int fd, int idx, void *data, struct sockaddr_storage new_sockaddr = {0, }; socklen_t addrlen = sizeof (new_sockaddr); socket_private_t *new_priv = NULL; - glusterfs_ctx_t *ctx = NULL; + glusterfs_ctx_t *ctx = NULL; this = data; - if (!this || !this->private || !this->xl) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); + GF_VALIDATE_OR_GOTO ("socket", this->xl, out); THIS = this->xl; priv = this->private; - ctx = this->ctx; + ctx = this->ctx; pthread_mutex_lock (&priv->lock); { @@ -1776,40 +2458,33 @@ socket_server_event_handler (int fd, int idx, void *data, if (poll_in) { new_sock = accept (priv->sock, SA (&new_sockaddr), - &addrlen); + &addrlen); - if (new_sock == -1) + if (new_sock == -1) { + gf_log (this->name, GF_LOG_WARNING, + "accept on %d failed (%s)", + priv->sock, strerror (errno)); goto unlock; - - if (!priv->bio) { - ret = __socket_nonblock (new_sock); - - if (ret == -1) { - gf_log (this->name, GF_LOG_DEBUG, - "NBIO on %d failed (%s)", - new_sock, strerror (errno)); - - close (new_sock); - goto unlock; - } } - if (priv->nodelay) { + if (priv->nodelay && (new_sockaddr.ss_family != AF_UNIX)) { ret = __socket_nodelay (new_sock); if (ret == -1) { - gf_log (this->name, GF_LOG_ERROR, + gf_log (this->name, GF_LOG_WARNING, "setsockopt() failed for " "NODELAY (%s)", strerror (errno)); } } - if (priv->keepalive) { + if (priv->keepalive && + new_sockaddr.ss_family != AF_UNIX) { ret = __socket_keepalive (new_sock, + new_sockaddr.ss_family, priv->keepaliveintvl, priv->keepaliveidle); if (ret == -1) - gf_log (this->name, GF_LOG_ERROR, + gf_log (this->name, GF_LOG_WARNING, "Failed to set keep-alive: %s", strerror (errno)); } @@ -1819,20 +2494,29 @@ socket_server_event_handler (int fd, int idx, void *data, if (!new_trans) goto unlock; + ret = pthread_mutex_init(&new_trans->lock, NULL); + if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "pthread_mutex_init() failed: %s", + strerror (errno)); + close (new_sock); + goto unlock; + } + new_trans->name = gf_strdup (this->name); memcpy (&new_trans->peerinfo.sockaddr, &new_sockaddr, - addrlen); + addrlen); new_trans->peerinfo.sockaddr_len = addrlen; new_trans->myinfo.sockaddr_len = - sizeof (new_trans->myinfo.sockaddr); + sizeof (new_trans->myinfo.sockaddr); ret = getsockname (new_sock, SA (&new_trans->myinfo.sockaddr), &new_trans->myinfo.sockaddr_len); if (ret == -1) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log (this->name, GF_LOG_WARNING, "getsockname on %d failed (%s)", new_sock, strerror (errno)); close (new_sock); @@ -1840,7 +2524,11 @@ socket_server_event_handler (int fd, int idx, void *data, } get_transport_identifiers (new_trans); - socket_init (new_trans); + ret = socket_init(new_trans); + if (ret != 0) { + close(new_sock); + goto unlock; + } new_trans->ops = this->ops; new_trans->init = this->init; new_trans->fini = this->fini; @@ -1851,27 +2539,74 @@ socket_server_event_handler (int fd, int idx, void *data, new_trans->listener = this; new_priv = new_trans->private; + new_priv->use_ssl = priv->use_ssl; + new_priv->sock = new_sock; + new_priv->own_thread = priv->own_thread; + + new_priv->ssl_ctx = priv->ssl_ctx; + if (priv->use_ssl && !priv->own_thread) { + if (ssl_setup_connection(new_trans,1) < 0) { + gf_log(this->name,GF_LOG_ERROR, + "server setup failed"); + close(new_sock); + goto unlock; + } + } + + if (!priv->bio && !priv->own_thread) { + ret = __socket_nonblock (new_sock); + + if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "NBIO on %d failed (%s)", + new_sock, strerror (errno)); + + close (new_sock); + goto unlock; + } + } + pthread_mutex_lock (&new_priv->lock); { - new_priv->sock = new_sock; + /* + * In the own_thread case, this is used to + * indicate that we're initializing a server + * connection. + */ new_priv->connected = 1; + new_priv->is_server = _gf_true; rpc_transport_ref (new_trans); - new_priv->idx = - event_register (ctx->event_pool, - new_sock, - socket_event_handler, - new_trans, 1, 0); + if (new_priv->own_thread) { + if (pipe(new_priv->pipe) < 0) { + gf_log(this->name,GF_LOG_ERROR, + "could not create pipe"); + } + socket_spawn(new_trans); + } + else { + new_priv->idx = + event_register (ctx->event_pool, + new_sock, + socket_event_handler, + new_trans, + 1, 0); + if (new_priv->idx == -1) + ret = -1; + } - if (new_priv->idx == -1) - ret = -1; } pthread_mutex_unlock (&new_priv->lock); - if (ret == -1) + if (ret == -1) { + gf_log (this->name, GF_LOG_WARNING, + "failed to register the socket with event"); goto unlock; + } - ret = rpc_transport_notify (this, RPC_TRANSPORT_ACCEPT, - new_trans); + if (!priv->own_thread) { + ret = rpc_transport_notify (this, + RPC_TRANSPORT_ACCEPT, new_trans); + } } } unlock: @@ -1882,14 +2617,14 @@ out: } -int +static int socket_disconnect (rpc_transport_t *this) { socket_private_t *priv = NULL; int ret = -1; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; @@ -1904,25 +2639,27 @@ out: } -int +static int socket_connect (rpc_transport_t *this, int port) { int ret = -1; - int sock = -1; + int sock = -1; socket_private_t *priv = NULL; - struct sockaddr_storage sockaddr = {0, }; socklen_t sockaddr_len = 0; - glusterfs_ctx_t *ctx = NULL; + glusterfs_ctx_t *ctx = NULL; sa_family_t sa_family = {0, }; + char *local_addr = NULL; + union gf_sock_union sock_union; + struct sockaddr_in *addr = NULL; - if (!this || !this->private) - goto err; + GF_VALIDATE_OR_GOTO ("socket", this, err); + GF_VALIDATE_OR_GOTO ("socket", this->private, err); priv = this->private; - ctx = this->ctx; + ctx = this->ctx; if (!priv) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log_callingfn (this->name, GF_LOG_WARNING, "connect() called on uninitialized transport"); goto err; } @@ -1934,22 +2671,41 @@ socket_connect (rpc_transport_t *this, int port) pthread_mutex_unlock (&priv->lock); if (sock != -1) { - gf_log (this->name, GF_LOG_TRACE, - "connect () called on transport already connected"); - ret = 0; + gf_log_callingfn (this->name, GF_LOG_TRACE, + "connect () called on transport already connected"); + errno = EINPROGRESS; + ret = -1; goto err; } - ret = socket_client_get_remote_sockaddr (this, SA (&sockaddr), + gf_log (this->name, GF_LOG_TRACE, + "connecting %p, state=%u gen=%u sock=%d", this, + priv->ot_state, priv->ot_gen, priv->sock); + + ret = socket_client_get_remote_sockaddr (this, &sock_union.sa, &sockaddr_len, &sa_family); if (ret == -1) { /* logged inside client_get_remote_sockaddr */ goto err; } - if (port > 0) - ((struct sockaddr_in *) (&sockaddr))->sin_port = htons (port); - + if (port > 0) { + sock_union.sin.sin_port = htons (port); + } + if (ntohs(sock_union.sin.sin_port) == GF_DEFAULT_SOCKET_LISTEN_PORT) { + if (priv->use_ssl) { + gf_log(this->name,GF_LOG_DEBUG, + "disabling SSL for portmapper connection"); + priv->use_ssl = _gf_false; + } + } + else { + if (priv->ssl_enabled && !priv->use_ssl) { + gf_log(this->name,GF_LOG_DEBUG, + "re-enabling SSL for I/O connection"); + priv->use_ssl = _gf_true; + } + } pthread_mutex_lock (&priv->lock); { if (priv->sock != -1) { @@ -1958,63 +2714,56 @@ socket_connect (rpc_transport_t *this, int port) goto unlock; } - memcpy (&this->peerinfo.sockaddr, &sockaddr, sockaddr_len); + memcpy (&this->peerinfo.sockaddr, &sock_union.storage, + sockaddr_len); this->peerinfo.sockaddr_len = sockaddr_len; priv->sock = socket (sa_family, SOCK_STREAM, 0); if (priv->sock == -1) { gf_log (this->name, GF_LOG_ERROR, "socket creation failed (%s)", - strerror (errno)); + strerror (errno)); goto unlock; } /* Cant help if setting socket options fails. We can continue * working nonetheless. */ - if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF, - &priv->windowsize, - sizeof (priv->windowsize)) < 0) { - gf_log (this->name, GF_LOG_ERROR, - "setting receive window size failed: %d: %d: " - "%s", priv->sock, priv->windowsize, - strerror (errno)); - } - - if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF, - &priv->windowsize, - sizeof (priv->windowsize)) < 0) { - gf_log (this->name, GF_LOG_ERROR, - "setting send window size failed: %d: %d: " - "%s", priv->sock, priv->windowsize, - strerror (errno)); - } - + if (priv->windowsize != 0) { + if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF, + &priv->windowsize, + sizeof (priv->windowsize)) < 0) { + gf_log (this->name, GF_LOG_ERROR, + "setting receive window " + "size failed: %d: %d: %s", + priv->sock, priv->windowsize, + strerror (errno)); + } - if (priv->nodelay) { - ret = __socket_nodelay (priv->sock); - if (ret == -1) { + if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF, + &priv->windowsize, + sizeof (priv->windowsize)) < 0) { gf_log (this->name, GF_LOG_ERROR, - "setsockopt() failed for NODELAY (%s)", + "setting send window size " + "failed: %d: %d: %s", + priv->sock, priv->windowsize, strerror (errno)); } } - if (!priv->bio) { - ret = __socket_nonblock (priv->sock); + if (priv->nodelay && (sa_family != AF_UNIX)) { + ret = __socket_nodelay (priv->sock); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, - "NBIO on %d failed (%s)", + "NODELAY on %d failed (%s)", priv->sock, strerror (errno)); - close (priv->sock); - priv->sock = -1; - goto unlock; } } - if (priv->keepalive) { + if (priv->keepalive && sa_family != AF_UNIX) { ret = __socket_keepalive (priv->sock, + sa_family, priv->keepaliveintvl, priv->keepaliveidle); if (ret == -1) @@ -2024,10 +2773,19 @@ socket_connect (rpc_transport_t *this, int port) } SA (&this->myinfo.sockaddr)->sa_family = - SA (&this->peerinfo.sockaddr)->sa_family; + SA (&this->peerinfo.sockaddr)->sa_family; + + /* If a source addr is explicitly specified, use it */ + ret = dict_get_str (this->options, + "transport.socket.source-addr", + &local_addr); + if (!ret && SA (&this->myinfo.sockaddr)->sa_family == AF_INET) { + addr = (struct sockaddr_in *)(&this->myinfo.sockaddr); + ret = inet_pton (AF_INET, local_addr, &(addr->sin_addr.s_addr)); + } ret = client_bind (this, SA (&this->myinfo.sockaddr), - &this->myinfo.sockaddr_len, priv->sock); + &this->myinfo.sockaddr_len, priv->sock); if (ret == -1) { gf_log (this->name, GF_LOG_WARNING, "client bind failed: %s", strerror (errno)); @@ -2036,26 +2794,86 @@ socket_connect (rpc_transport_t *this, int port) goto unlock; } + if (!priv->use_ssl && !priv->bio && !priv->own_thread) { + ret = __socket_nonblock (priv->sock); + if (ret == -1) { + gf_log (this->name, GF_LOG_ERROR, + "NBIO on %d failed (%s)", + priv->sock, strerror (errno)); + close (priv->sock); + priv->sock = -1; + goto unlock; + } + } + ret = connect (priv->sock, SA (&this->peerinfo.sockaddr), - this->peerinfo.sockaddr_len); + this->peerinfo.sockaddr_len); - if (ret == -1 && errno != EINPROGRESS) { - gf_log (this->name, GF_LOG_ERROR, - "connection attempt failed (%s)", - strerror (errno)); + if (ret == -1 && ((errno != EINPROGRESS) && (errno != ENOENT))) { + /* For unix path based sockets, the socket path is + * cryptic (md5sum of path) and may not be useful for + * the user in debugging so log it in DEBUG + */ + gf_log (this->name, ((sa_family == AF_UNIX) ? + GF_LOG_DEBUG : GF_LOG_ERROR), + "connection attempt on %s failed, (%s)", + this->peerinfo.identifier, strerror (errno)); close (priv->sock); priv->sock = -1; goto unlock; } - priv->connected = 0; + if (priv->use_ssl && !priv->own_thread) { + ret = ssl_setup_connection(this,0); + if (ret < 0) { + gf_log(this->name,GF_LOG_ERROR, + "client setup failed"); + close(priv->sock); + priv->sock = -1; + goto unlock; + } + } + + if (!priv->bio && !priv->own_thread) { + ret = __socket_nonblock (priv->sock); + + if (ret == -1) { + gf_log (this->name, GF_LOG_ERROR, + "NBIO on %d failed (%s)", + priv->sock, strerror (errno)); + close (priv->sock); + priv->sock = -1; + goto unlock; + } + } + /* + * In the own_thread case, this is used to indicate that we're + * initializing a client connection. + */ + priv->connected = 0; + priv->is_server = _gf_false; rpc_transport_ref (this); - priv->idx = event_register (ctx->event_pool, priv->sock, - socket_event_handler, this, 1, 1); - if (priv->idx == -1) - ret = -1; + if (priv->own_thread) { + if (pipe(priv->pipe) < 0) { + gf_log(this->name,GF_LOG_ERROR, + "could not create pipe"); + } + + this->listener = this; + socket_spawn(this); + } + else { + priv->idx = event_register (ctx->event_pool, priv->sock, + socket_event_handler, + this, 1, 1); + if (priv->idx == -1) { + gf_log ("", GF_LOG_WARNING, + "failed to register the event"); + ret = -1; + } + } } unlock: pthread_mutex_unlock (&priv->lock); @@ -2065,24 +2883,24 @@ err: } -int +static int socket_listen (rpc_transport_t *this) { socket_private_t * priv = NULL; int ret = -1; - int sock = -1; + int sock = -1; struct sockaddr_storage sockaddr; - socklen_t sockaddr_len; + socklen_t sockaddr_len = 0; peer_info_t *myinfo = NULL; - glusterfs_ctx_t *ctx = NULL; + glusterfs_ctx_t *ctx = NULL; sa_family_t sa_family = {0, }; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); - priv = this->private; - myinfo = &this->myinfo; - ctx = this->ctx; + priv = this->private; + myinfo = &this->myinfo; + ctx = this->ctx; pthread_mutex_lock (&priv->lock); { @@ -2091,8 +2909,8 @@ socket_listen (rpc_transport_t *this) pthread_mutex_unlock (&priv->lock); if (sock != -1) { - gf_log (this->name, GF_LOG_DEBUG, - "alreading listening"); + gf_log_callingfn (this->name, GF_LOG_DEBUG, + "already listening"); return ret; } @@ -2118,32 +2936,36 @@ socket_listen (rpc_transport_t *this) if (priv->sock == -1) { gf_log (this->name, GF_LOG_ERROR, "socket creation failed (%s)", - strerror (errno)); + strerror (errno)); goto unlock; } /* Cant help if setting socket options fails. We can continue * working nonetheless. */ - if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF, - &priv->windowsize, - sizeof (priv->windowsize)) < 0) { - gf_log (this->name, GF_LOG_ERROR, - "setting receive window size failed: %d: %d: " - "%s", priv->sock, priv->windowsize, - strerror (errno)); - } + if (priv->windowsize != 0) { + if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF, + &priv->windowsize, + sizeof (priv->windowsize)) < 0) { + gf_log (this->name, GF_LOG_ERROR, + "setting receive window size " + "failed: %d: %d: %s", priv->sock, + priv->windowsize, + strerror (errno)); + } - if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF, - &priv->windowsize, - sizeof (priv->windowsize)) < 0) { - gf_log (this->name, GF_LOG_ERROR, - "setting send window size failed: %d: %d: " - "%s", priv->sock, priv->windowsize, - strerror (errno)); + if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF, + &priv->windowsize, + sizeof (priv->windowsize)) < 0) { + gf_log (this->name, GF_LOG_ERROR, + "setting send window size failed:" + " %d: %d: %s", priv->sock, + priv->windowsize, + strerror (errno)); + } } - if (priv->nodelay) { + if (priv->nodelay && (sa_family != AF_UNIX)) { ret = __socket_nodelay (priv->sock); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, @@ -2174,12 +2996,15 @@ socket_listen (rpc_transport_t *this) goto unlock; } - ret = listen (priv->sock, 10); + if (priv->backlog) + ret = listen (priv->sock, priv->backlog); + else + ret = listen (priv->sock, 10); if (ret == -1) { gf_log (this->name, GF_LOG_ERROR, "could not set socket %d to listen mode (%s)", - priv->sock, strerror (errno)); + priv->sock, strerror (errno)); close (priv->sock); priv->sock = -1; goto unlock; @@ -2189,12 +3014,12 @@ socket_listen (rpc_transport_t *this) priv->idx = event_register (ctx->event_pool, priv->sock, socket_server_event_handler, - this, 1, 0); + this, 1, 0); if (priv->idx == -1) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log (this->name, GF_LOG_WARNING, "could not register socket %d with events", - priv->sock); + priv->sock); ret = -1; close (priv->sock); priv->sock = -1; @@ -2209,7 +3034,7 @@ out: } -int32_t +static int32_t socket_submit_request (rpc_transport_t *this, rpc_transport_req_t *req) { socket_private_t *priv = NULL; @@ -2217,19 +3042,20 @@ socket_submit_request (rpc_transport_t *this, rpc_transport_req_t *req) char need_poll_out = 0; char need_append = 1; struct ioq *entry = NULL; - glusterfs_ctx_t *ctx = NULL; + glusterfs_ctx_t *ctx = NULL; + char a_byte = 'j'; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - ctx = this->ctx; + ctx = this->ctx; pthread_mutex_lock (&priv->lock); { if (priv->connected != 1) { if (!priv->submit_log && !priv->connect_finish_log) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log (this->name, GF_LOG_INFO, "not connected (priv->connected = %d)", priv->connected); priv->submit_log = 1; @@ -2243,24 +3069,34 @@ socket_submit_request (rpc_transport_t *this, rpc_transport_req_t *req) goto unlock; if (list_empty (&priv->ioq)) { - ret = __socket_ioq_churn_entry (this, entry); + ret = __socket_ioq_churn_entry (this, entry, 1); - if (ret == 0) + if (ret == 0) { need_append = 0; - - if (ret > 0) + } + if (ret > 0) { need_poll_out = 1; + } } if (need_append) { list_add_tail (&entry->list, &priv->ioq); + if (priv->own_thread) { + /* + * Make sure the polling thread wakes up, by + * writing a byte to represent this entry. + */ + if (write(priv->pipe[1],&a_byte,1) < 1) { + gf_log(this->name,GF_LOG_WARNING, + "write error on pipe"); + } + } ret = 0; } - - if (need_poll_out) { + if (!priv->own_thread && need_poll_out) { /* first entry to wait. continue writing on POLLOUT */ priv->idx = event_select_on (ctx->event_pool, - priv->sock, + priv->sock, priv->idx, -1, 1); } } @@ -2272,7 +3108,7 @@ out: } -int32_t +static int32_t socket_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply) { socket_private_t *priv = NULL; @@ -2280,52 +3116,64 @@ socket_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply) char need_poll_out = 0; char need_append = 1; struct ioq *entry = NULL; - glusterfs_ctx_t *ctx = NULL; + glusterfs_ctx_t *ctx = NULL; + char a_byte = 'd'; - if (!this || !this->private) - goto out; + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); priv = this->private; - ctx = this->ctx; + ctx = this->ctx; pthread_mutex_lock (&priv->lock); { if (priv->connected != 1) { if (!priv->submit_log && !priv->connect_finish_log) { - gf_log (this->name, GF_LOG_DEBUG, + gf_log (this->name, GF_LOG_INFO, "not connected (priv->connected = %d)", priv->connected); priv->submit_log = 1; } goto unlock; } + priv->submit_log = 0; entry = __socket_ioq_new (this, &reply->msg); if (!entry) goto unlock; + if (list_empty (&priv->ioq)) { - ret = __socket_ioq_churn_entry (this, entry); + ret = __socket_ioq_churn_entry (this, entry, 1); - if (ret == 0) + if (ret == 0) { need_append = 0; - - if (ret > 0) + } + if (ret > 0) { need_poll_out = 1; + } } if (need_append) { list_add_tail (&entry->list, &priv->ioq); + if (priv->own_thread) { + /* + * Make sure the polling thread wakes up, by + * writing a byte to represent this entry. + */ + if (write(priv->pipe[1],&a_byte,1) < 1) { + gf_log(this->name,GF_LOG_WARNING, + "write error on pipe"); + } + } ret = 0; } - - if (need_poll_out) { + if (!priv->own_thread && need_poll_out) { /* first entry to wait. continue writing on POLLOUT */ priv->idx = event_select_on (ctx->event_pool, - priv->sock, + priv->sock, priv->idx, -1, 1); } } - unlock: pthread_mutex_unlock (&priv->lock); @@ -2334,14 +3182,13 @@ out: } -int32_t +static int32_t socket_getpeername (rpc_transport_t *this, char *hostname, int hostlen) { int32_t ret = -1; - if ((this == NULL) || (hostname == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", hostname, out); if (hostlen < (strlen (this->peerinfo.identifier) + 1)) { goto out; @@ -2354,35 +3201,34 @@ out: } -int32_t +static int32_t socket_getpeeraddr (rpc_transport_t *this, char *peeraddr, int addrlen, struct sockaddr_storage *sa, socklen_t salen) { int32_t ret = -1; - if ((this == NULL) || (sa == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", sa, out); *sa = this->peerinfo.sockaddr; if (peeraddr != NULL) { ret = socket_getpeername (this, peeraddr, addrlen); } + ret = 0; out: return ret; } -int32_t +static int32_t socket_getmyname (rpc_transport_t *this, char *hostname, int hostlen) { int32_t ret = -1; - if ((this == NULL) || (hostname == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", hostname, out); if (hostlen < (strlen (this->myinfo.identifier) + 1)) { goto out; @@ -2395,15 +3241,14 @@ out: } -int32_t +static int32_t socket_getmyaddr (rpc_transport_t *this, char *myaddr, int addrlen, struct sockaddr_storage *sa, socklen_t salen) { int32_t ret = 0; - if ((this == NULL) || (sa == NULL)) { - goto out; - } + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", sa, out); *sa = this->myinfo.sockaddr; @@ -2416,6 +3261,25 @@ out: } +static int +socket_throttle (rpc_transport_t *this, gf_boolean_t onoff) +{ + socket_private_t *priv = NULL; + + priv = this->private; + + /* The way we implement throttling is by taking off + POLLIN event from the polled flags. This way we + never get called with the POLLIN event and therefore + will never read() any more data until throttling + is turned off. + */ + priv->idx = event_select_on (this->ctx->event_pool, priv->sock, + priv->idx, (int) !onoff, -1); + return 0; +} + + struct rpc_transport_ops tops = { .listen = socket_listen, .connect = socket_connect, @@ -2426,71 +3290,64 @@ struct rpc_transport_ops tops = { .get_peeraddr = socket_getpeeraddr, .get_myname = socket_getmyname, .get_myaddr = socket_getmyaddr, + .throttle = socket_throttle, }; int -validate_options (rpc_transport_t *this, dict_t *options, char **op_errstr) -{ - char *optstr = NULL; - int ret = -1; - gf_boolean_t tmp_bool = _gf_false; - - if (dict_get_str (options, "transport.socket.keepalive", - &optstr) == 0) { - if (gf_string2boolean (optstr, &tmp_bool) == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'transport.socket.keepalive' takes only " - "boolean options, not taking any action"); - *op_errstr = "Value should be only boolean!!"; - ret =-1; - goto out; - } - } - - ret =0; -out: - return ret; - -} - -int reconfigure (rpc_transport_t *this, dict_t *options) { - socket_private_t *priv = NULL; - gf_boolean_t tmp_bool = _gf_false; - char *optstr = NULL; - int ret = -1; + socket_private_t *priv = NULL; + gf_boolean_t tmp_bool = _gf_false; + char *optstr = NULL; + int ret = 0; + uint64_t windowsize = 0; + + GF_VALIDATE_OR_GOTO ("socket", this, out); + GF_VALIDATE_OR_GOTO ("socket", this->private, out); if (!this || !this->private) { ret =-1; goto out; } - - + priv = this->private; if (dict_get_str (this->options, "transport.socket.keepalive", - &optstr) == 0) { - if (gf_string2boolean (optstr, &tmp_bool) == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'transport.socket.keepalive' takes only " - "boolean options, not taking any action"); - priv->keepalive = 1; - goto out; - } - gf_log (this->name, GF_LOG_DEBUG, "Reconfigured transport.socket.keepalive"); - - priv->keepalive = tmp_bool; + &optstr) == 0) { + if (gf_string2boolean (optstr, &tmp_bool) == -1) { + gf_log (this->name, GF_LOG_ERROR, + "'transport.socket.keepalive' takes only " + "boolean options, not taking any action"); + priv->keepalive = 1; + ret = -1; + goto out; + } + gf_log (this->name, GF_LOG_DEBUG, "Reconfigured transport.socket.keepalive"); + + priv->keepalive = tmp_bool; } else priv->keepalive = 1; + + optstr = NULL; + if (dict_get_str (this->options, "tcp-window-size", + &optstr) == 0) { + if (gf_string2bytesize (optstr, &windowsize) != 0) { + gf_log (this->name, GF_LOG_ERROR, + "invalid number format: %s", optstr); + goto out; + } + } + + priv->windowsize = (int)windowsize; + ret = 0; out: return ret; } -int +static int socket_init (rpc_transport_t *this) { socket_private_t *priv = NULL; @@ -2498,20 +3355,20 @@ socket_init (rpc_transport_t *this) uint64_t windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE; char *optstr = NULL; uint32_t keepalive = 0; + uint32_t backlog = 0; + int session_id = 0; if (this->private) { - gf_log (this->name, GF_LOG_DEBUG, - "double init attempted"); + gf_log_callingfn (this->name, GF_LOG_ERROR, + "double init attempted"); return -1; } priv = GF_CALLOC (1, sizeof (*priv), gf_common_mt_socket_private_t); if (!priv) { - gf_log (this->name, GF_LOG_ERROR, - "calloc (1, %"GF_PRI_SIZET") returned NULL", - sizeof (*priv)); return -1; } + memset(priv,0,sizeof(*priv)); pthread_mutex_init (&priv->lock, NULL); @@ -2521,7 +3378,6 @@ socket_init (rpc_transport_t *this) priv->nodelay = 1; priv->bio = 0; priv->windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE; - INIT_LIST_HEAD (&priv->ioq); /* All the below section needs 'this->options' to be present */ @@ -2535,7 +3391,7 @@ socket_init (rpc_transport_t *this) if (gf_string2boolean (optstr, &tmp_bool) == -1) { gf_log (this->name, GF_LOG_ERROR, "'non-blocking-io' takes only boolean options," - " not taking any action"); + " not taking any action"); tmp_bool = 1; } @@ -2566,9 +3422,8 @@ socket_init (rpc_transport_t *this) } } - optstr = NULL; - if (dict_get_str (this->options, "transport.window-size", + if (dict_get_str (this->options, "tcp-window-size", &optstr) == 0) { if (gf_string2bytesize (optstr, &windowsize) != 0) { gf_log (this->name, GF_LOG_ERROR, @@ -2577,8 +3432,9 @@ socket_init (rpc_transport_t *this) } } - optstr = NULL; + priv->windowsize = (int)windowsize; + optstr = NULL; /* Enable Keep-alive by default. */ priv->keepalive = 1; priv->keepaliveintvl = 2; @@ -2588,7 +3444,7 @@ socket_init (rpc_transport_t *this) if (gf_string2boolean (optstr, &tmp_bool) == -1) { gf_log (this->name, GF_LOG_ERROR, "'transport.socket.keepalive' takes only " - "boolean options, not taking any action"); + "boolean options, not taking any action"); tmp_bool = 1; } @@ -2608,11 +3464,156 @@ socket_init (rpc_transport_t *this) priv->keepaliveidle = keepalive; } + if (dict_get_uint32 (this->options, + "transport.socket.listen-backlog", + &backlog) == 0) { + priv->backlog = backlog; + } + + optstr = NULL; + + /* Check if socket read failures are to be logged */ + priv->read_fail_log = 1; + if (dict_get (this->options, "transport.socket.read-fail-log")) { + optstr = data_to_str (dict_get (this->options, "transport.socket.read-fail-log")); + if (gf_string2boolean (optstr, &tmp_bool) == -1) { + gf_log (this->name, GF_LOG_WARNING, + "'transport.socket.read-fail-log' takes only " + "boolean options; logging socket read fails"); + } + else if (tmp_bool == _gf_false) { + priv->read_fail_log = 0; + } + } + priv->windowsize = (int)windowsize; + + priv->ssl_enabled = _gf_false; + if (dict_get_str(this->options,SSL_ENABLED_OPT,&optstr) == 0) { + if (gf_string2boolean (optstr, &priv->ssl_enabled) != 0) { + gf_log (this->name, GF_LOG_ERROR, + "invalid value given for ssl-enabled boolean"); + } + } + + priv->ssl_own_cert = DEFAULT_CERT_PATH; + if (dict_get_str(this->options,SSL_OWN_CERT_OPT,&optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name,GF_LOG_WARNING, + "%s specified without %s (ignored)", + SSL_OWN_CERT_OPT, SSL_ENABLED_OPT); + } + priv->ssl_own_cert = optstr; + } + priv->ssl_own_cert = gf_strdup(priv->ssl_own_cert); + + priv->ssl_private_key = DEFAULT_KEY_PATH; + if (dict_get_str(this->options,SSL_PRIVATE_KEY_OPT,&optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name,GF_LOG_WARNING, + "%s specified without %s (ignored)", + SSL_PRIVATE_KEY_OPT, SSL_ENABLED_OPT); + } + priv->ssl_private_key = optstr; + } + priv->ssl_private_key = gf_strdup(priv->ssl_private_key); + + priv->ssl_ca_list = DEFAULT_CA_PATH; + if (dict_get_str(this->options,SSL_CA_LIST_OPT,&optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name,GF_LOG_WARNING, + "%s specified without %s (ignored)", + SSL_CA_LIST_OPT, SSL_ENABLED_OPT); + } + priv->ssl_ca_list = optstr; + } + priv->ssl_ca_list = gf_strdup(priv->ssl_ca_list); + + gf_log(this->name,GF_LOG_INFO,"SSL support is %s", + priv->ssl_enabled ? "ENABLED" : "NOT enabled"); + /* + * This might get overridden temporarily in socket_connect (q.v.) + * if we're using the glusterd portmapper. + */ + priv->use_ssl = priv->ssl_enabled; + + priv->own_thread = priv->use_ssl; + if (dict_get_str(this->options,OWN_THREAD_OPT,&optstr) == 0) { + if (gf_string2boolean (optstr, &priv->own_thread) != 0) { + gf_log (this->name, GF_LOG_ERROR, + "invalid value given for own-thread boolean"); + } + } + gf_log(this->name,GF_LOG_INFO,"using %s polling thread", + priv->own_thread ? "private" : "system"); + + if (priv->use_ssl) { + SSL_library_init(); + SSL_load_error_strings(); + priv->ssl_meth = (SSL_METHOD *)TLSv1_method(); + priv->ssl_ctx = SSL_CTX_new(priv->ssl_meth); + + if (SSL_CTX_set_cipher_list(priv->ssl_ctx, + "HIGH:-SSLv2") == 0) { + gf_log(this->name,GF_LOG_ERROR, + "failed to find any valid ciphers"); + goto err; + } + + if (!SSL_CTX_use_certificate_chain_file(priv->ssl_ctx, + priv->ssl_own_cert)) { + gf_log(this->name,GF_LOG_ERROR, + "could not load our cert"); + goto err; + } + + if (!SSL_CTX_use_PrivateKey_file(priv->ssl_ctx, + priv->ssl_private_key, + SSL_FILETYPE_PEM)) { + gf_log(this->name,GF_LOG_ERROR, + "could not load private key"); + goto err; + } + + if (!SSL_CTX_load_verify_locations(priv->ssl_ctx, + priv->ssl_ca_list,0)) { + gf_log(this->name,GF_LOG_ERROR, + "could not load CA list"); + goto err; + } + +#if (OPENSSL_VERSION_NUMBER < 0x00905100L) + SSL_CTX_set_verify_depth(ctx,1); +#endif + + priv->ssl_session_id = ++session_id; + SSL_CTX_set_session_id_context(priv->ssl_ctx, + (void *)&priv->ssl_session_id, + sizeof(priv->ssl_session_id)); + + SSL_CTX_set_verify(priv->ssl_ctx,SSL_VERIFY_PEER,0); + } + + if (priv->own_thread) { + priv->ot_state = OT_IDLE; + } + out: this->private = priv; - return 0; + +err: + if (priv->ssl_own_cert) { + GF_FREE(priv->ssl_own_cert); + } + if (priv->ssl_private_key) { + GF_FREE(priv->ssl_private_key); + } + if (priv->ssl_ca_list) { + GF_FREE(priv->ssl_ca_list); + } + GF_FREE(priv); + return -1; } @@ -2638,6 +3639,15 @@ fini (rpc_transport_t *this) "transport %p destroyed", this); pthread_mutex_destroy (&priv->lock); + if (priv->ssl_private_key) { + GF_FREE(priv->ssl_private_key); + } + if (priv->ssl_own_cert) { + GF_FREE(priv->ssl_own_cert); + } + if (priv->ssl_ca_list) { + GF_FREE(priv->ssl_ca_list); + } GF_FREE (priv); } @@ -2682,18 +3692,17 @@ struct volume_options options[] = { }, { .key = { "transport.address-family", "address-family" }, - .value = {"inet", "inet6", "inet/inet6", "inet6/inet", - "unix", "inet-sdp" }, + .value = {"inet", "inet6", "unix", "inet-sdp" }, .type = GF_OPTION_TYPE_STR }, { .key = {"non-blocking-io"}, .type = GF_OPTION_TYPE_BOOL }, - { .key = {"transport.window-size"}, + { .key = {"tcp-window-size"}, .type = GF_OPTION_TYPE_SIZET, .min = GF_MIN_SOCKET_WINDOW_SIZE, - .max = GF_MAX_SOCKET_WINDOW_SIZE, + .max = GF_MAX_SOCKET_WINDOW_SIZE }, { .key = {"transport.socket.nodelay"}, .type = GF_OPTION_TYPE_BOOL @@ -2710,5 +3719,26 @@ struct volume_options options[] = { { .key = {"transport.socket.keepalive-time"}, .type = GF_OPTION_TYPE_INT }, + { .key = {"transport.socket.listen-backlog"}, + .type = GF_OPTION_TYPE_INT + }, + { .key = {"transport.socket.read-fail-log"}, + .type = GF_OPTION_TYPE_BOOL + }, + { .key = {SSL_ENABLED_OPT}, + .type = GF_OPTION_TYPE_BOOL + }, + { .key = {SSL_OWN_CERT_OPT}, + .type = GF_OPTION_TYPE_STR + }, + { .key = {SSL_PRIVATE_KEY_OPT}, + .type = GF_OPTION_TYPE_STR + }, + { .key = {SSL_CA_LIST_OPT}, + .type = GF_OPTION_TYPE_STR + }, + { .key = {OWN_THREAD_OPT}, + .type = GF_OPTION_TYPE_BOOL + }, { .key = {NULL} } }; diff --git a/rpc/rpc-transport/socket/src/socket.h b/rpc/rpc-transport/socket/src/socket.h index c43c5576c..e0b412fcc 100644 --- a/rpc/rpc-transport/socket/src/socket.h +++ b/rpc/rpc-transport/socket/src/socket.h @@ -1,25 +1,18 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ #ifndef _SOCKET_H #define _SOCKET_H +#include <openssl/ssl.h> +#include <openssl/err.h> #ifndef _CONFIG_H #define _CONFIG_H @@ -41,19 +34,17 @@ #define RPC_MAX_FRAGMENT_SIZE 0x7fffffff -/* This is the size set through setsockopt for - * both the TCP receive window size and the - * send buffer size. - * Till the time iobuf size becomes configurable, this size is set to include - * two iobufs + the GlusterFS protocol headers. +/* The default window size will be 0, indicating not to set + * it to any size. Default size of Linux is found to be + * performance friendly. * Linux allows us to over-ride the max values for the system. * Should we over-ride them? Because if we set a value larger than the default * setsockopt will fail. Having larger values might be beneficial for * IB links. */ -#define GF_DEFAULT_SOCKET_WINDOW_SIZE (512 * GF_UNIT_KB) +#define GF_DEFAULT_SOCKET_WINDOW_SIZE (0) #define GF_MAX_SOCKET_WINDOW_SIZE (1 * GF_UNIT_MB) -#define GF_MIN_SOCKET_WINDOW_SIZE (128 * GF_UNIT_KB) +#define GF_MIN_SOCKET_WINDOW_SIZE (0) #define GF_USE_DEFAULT_KEEPALIVE (-1) typedef enum { @@ -81,6 +72,14 @@ typedef enum { SP_STATE_READ_CREDBYTES, /* read credential data. */ SP_STATE_READING_VERFBYTES, SP_STATE_READ_VERFBYTES, /* read verifier data */ + SP_STATE_READING_PROGHDR, + SP_STATE_READ_PROGHDR, + SP_STATE_READING_PROGHDR_XDATA, + SP_STATE_READ_PROGHDR_XDATA, /* It's a bad "name" in the generic + RPC state machine, but greatly + aids code review (and xdata is + the only "consumer" of this state) + */ SP_STATE_READING_PROG, } sp_rpcfrag_vectored_request_state_t; @@ -88,8 +87,8 @@ typedef enum { SP_STATE_REQUEST_HEADER_INIT, SP_STATE_READING_RPCHDR1, SP_STATE_READ_RPCHDR1, /* read msg from beginning till and - * including credlen - */ + * including credlen + */ } sp_rpcfrag_request_header_state_t; struct ioq { @@ -112,6 +111,7 @@ struct ioq { typedef struct { sp_rpcfrag_request_header_state_t header_state; sp_rpcfrag_vectored_request_state_t vector_state; + int vector_sizer_state; } sp_rpcfrag_request_state_t; typedef enum { @@ -123,6 +123,8 @@ typedef enum { typedef enum { SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT, SP_STATE_READING_PROC_HEADER, + SP_STATE_READING_PROC_OPAQUE, + SP_STATE_READ_PROC_OPAQUE, SP_STATE_READ_PROC_HEADER, } sp_rpcfrag_vectored_reply_accepted_success_state_t; @@ -141,10 +143,60 @@ typedef struct { sp_rpcfrag_vectored_reply_accepted_success_state_t accepted_success_state; } sp_rpcfrag_vectored_reply_state_t; +struct gf_sock_incoming_frag { + char *fragcurrent; + uint32_t bytes_read; + uint32_t remaining_size; + struct iovec vector; + struct iovec *pending_vector; + union { + sp_rpcfrag_request_state_t request; + sp_rpcfrag_vectored_reply_state_t reply; + } call_body; + + sp_rpcfrag_simple_msg_state_t simple_state; + sp_rpcfrag_state_t state; +}; + +#define GF_SOCKET_RA_MAX 1024 + +struct gf_sock_incoming { + sp_rpcrecord_state_t record_state; + struct gf_sock_incoming_frag frag; + char *proghdr_base_addr; + struct iobuf *iobuf; + size_t iobuf_size; + struct iovec vector[2]; + int count; + struct iovec payload_vector; + struct iobref *iobref; + rpc_request_info_t *request_info; + struct iovec *pending_vector; + int pending_count; + uint32_t fraghdr; + char complete_record; + msg_type_t msg_type; + size_t total_bytes_read; + + size_t ra_read; + size_t ra_max; + size_t ra_served; + char *ra_buf; +}; + +typedef enum { + OT_IDLE, /* Uninitialized or termination complete. */ + OT_SPAWNING, /* Past pthread_create but not in thread yet. */ + OT_RUNNING, /* Poller thread running normally. */ + OT_CALLBACK, /* Poller thread in the middle of a callback. */ + OT_PLEASE_DIE, /* Poller termination requested. */ +} ot_state_t; + typedef struct { int32_t sock; int32_t idx; - unsigned char connected; // -1 = not connected. 0 = in progress. 1 = connected + /* -1 = not connected. 0 = in progress. 1 = connected */ + char connected; char bio; char connect_finish_log; char submit_log; @@ -155,36 +207,7 @@ typedef struct { struct ioq *ioq_prev; }; }; - struct { - sp_rpcrecord_state_t record_state; - struct { - char *fragcurrent; - uint32_t bytes_read; - uint32_t remaining_size; - struct iovec vector; - struct iovec *pending_vector; - union { - sp_rpcfrag_request_state_t request; - sp_rpcfrag_vectored_reply_state_t reply; - } call_body; - - sp_rpcfrag_simple_msg_state_t simple_state; - sp_rpcfrag_state_t state; - } frag; - struct iobuf *iobuf; - size_t iobuf_size; - struct iovec vector[2]; - int count; - struct iovec payload_vector; - struct iobref *iobref; - rpc_request_info_t *request_info; - struct iovec *pending_vector; - int pending_count; - uint32_t fraghdr; - char complete_record; - msg_type_t msg_type; - size_t total_bytes_read; - } incoming; + struct gf_sock_incoming incoming; pthread_mutex_t lock; int windowsize; char lowlat; @@ -192,6 +215,24 @@ typedef struct { int keepalive; int keepaliveidle; int keepaliveintvl; + uint32_t backlog; + gf_boolean_t read_fail_log; + gf_boolean_t ssl_enabled; + gf_boolean_t use_ssl; + SSL_METHOD *ssl_meth; + SSL_CTX *ssl_ctx; + int ssl_session_id; + BIO *ssl_sbio; + SSL *ssl_ssl; + char *ssl_own_cert; + char *ssl_private_key; + char *ssl_ca_list; + pthread_t thread; + int pipe[2]; + gf_boolean_t own_thread; + ot_state_t ot_state; + uint32_t ot_gen; + gf_boolean_t is_server; } socket_private_t; |
