Commit 673cf4bf authored by Jacek Kalwas's avatar Jacek Kalwas Committed by Tomasz Zawadzki
Browse files

sock: split listen and connect



With recent utils there is not much common code left and connect flow
will divarage more in following patches.

Change-Id: I52c1b3fa398c5fc729a9597057e3e727ab8e0826
Signed-off-by: default avatarJacek Kalwas <jacek.kalwas@nutanix.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/25903


Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
parent 561716c5
Loading
Loading
Loading
Loading
+116 −78
Original line number Diff line number Diff line
@@ -273,11 +273,6 @@ posix_sock_get_numa_id(struct spdk_sock *sock)
	}
}

enum posix_sock_create_type {
	SPDK_SOCK_CREATE_LISTEN,
	SPDK_SOCK_CREATE_CONNECT,
};

static int
posix_sock_alloc_pipe(struct spdk_posix_sock *sock, int sz)
{
@@ -845,19 +840,12 @@ SSL_writev(SSL *ssl, struct iovec *iov, int iovcnt)
}

static struct spdk_sock *
posix_sock_create(const char *ip, int port,
		  enum posix_sock_create_type type,
		  struct spdk_sock_opts *opts,
		  bool enable_ssl)
_posix_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts, bool enable_ssl)
{
	struct spdk_posix_sock *sock;
	struct spdk_sock_impl_opts impl_opts;
	struct addrinfo *res, *res0;
	int fd, rc;
	bool enable_zcopy_user_opts = true;
	bool enable_zcopy_impl_opts = true;
	SSL_CTX *ctx = 0;
	SSL *ssl = 0;
	struct spdk_posix_sock *sock;
	struct addrinfo *res0;
	int rc, fd = -1;

	assert(opts != NULL);
	if (enable_ssl) {
@@ -871,15 +859,13 @@ posix_sock_create(const char *ip, int port,
		return NULL;
	}

	/* try listen */
	fd = -1;
	for (res = res0; res != NULL; res = res->ai_next) {
	for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) {
retry:
		fd = spdk_sock_posix_fd_create(res, opts, &impl_opts);
		if (fd < 0) {
			continue;
		}
		if (type == SPDK_SOCK_CREATE_LISTEN) {

		rc = bind(fd, res->ai_addr, res->ai_addrlen);
		if (rc != 0) {
			SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
@@ -901,7 +887,7 @@ retry:
				continue;
			}
		}
			/* bind OK */

		rc = listen(fd, 512);
		if (rc != 0) {
			SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
@@ -909,8 +895,61 @@ retry:
			fd = -1;
			break;
		}
			enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_server;
		} else if (type == SPDK_SOCK_CREATE_CONNECT) {

		if (spdk_fd_set_nonblock(fd)) {
			close(fd);
			fd = -1;
			break;
		}

		break;
	}

	freeaddrinfo(res0);
	if (fd < 0) {
		return NULL;
	}

	sock = posix_sock_alloc(fd, &impl_opts);
	if (sock == NULL) {
		close(fd);
		return NULL;
	}

	/* Only enable zero copy for non-loopback and non-ssl sockets. */
	posix_sock_init(sock, opts->zcopy && !spdk_net_is_loopback(fd) && !enable_ssl &&
			impl_opts.enable_zerocopy_send_server);
	return &sock->base;
}

static struct spdk_sock *
_posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts, bool enable_ssl)
{
	struct spdk_sock_impl_opts impl_opts;
	struct spdk_posix_sock *sock;
	struct addrinfo *res0;
	int rc, fd = -1;
	SSL_CTX *ctx = 0;
	SSL *ssl = 0;

	assert(opts != NULL);
	if (enable_ssl) {
		_opts_get_impl_opts(opts, &impl_opts, &g_ssl_impl_opts);
	} else {
		_opts_get_impl_opts(opts, &impl_opts, &g_posix_impl_opts);
	}

	res0 = spdk_sock_posix_getaddrinfo(ip, port);
	if (!res0) {
		return NULL;
	}

	for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) {
		fd = spdk_sock_posix_fd_create(res, opts, &impl_opts);
		if (fd < 0) {
			continue;
		}

		rc = spdk_sock_posix_fd_connect(fd, res, opts);
		if (rc != 0) {
			close(fd);
@@ -922,7 +961,6 @@ retry:
			}
		}

			enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_client;
		if (enable_ssl) {
			ctx = posix_sock_create_ssl_context(TLS_client_method(), opts, &impl_opts);
			if (!ctx) {
@@ -931,16 +969,16 @@ retry:
				fd = -1;
				break;
			}

			ssl = ssl_sock_setup_connect(ctx, fd);
			if (!ssl) {
				SPDK_ERRLOG("ssl_sock_setup_connect() failed, errno = %d\n", errno);
				SSL_CTX_free(ctx);
				close(fd);
				fd = -1;
					SSL_CTX_free(ctx);
				break;
			}
		}
		}

		if (spdk_fd_set_nonblock(fd)) {
			SSL_free(ssl);
@@ -949,17 +987,17 @@ retry:
			fd = -1;
			break;
		}

		break;
	}
	freeaddrinfo(res0);

	freeaddrinfo(res0);
	if (fd < 0) {
		return NULL;
	}

	sock = posix_sock_alloc(fd, &impl_opts);
	if (sock == NULL) {
		SPDK_ERRLOG("sock allocation failed\n");
		SSL_free(ssl);
		SSL_CTX_free(ctx);
		close(fd);
@@ -976,21 +1014,21 @@ retry:
	}

	/* Only enable zero copy for non-loopback and non-ssl sockets. */
	enable_zcopy_user_opts = opts->zcopy && !spdk_net_is_loopback(fd) && !enable_ssl;
	posix_sock_init(sock, enable_zcopy_user_opts && enable_zcopy_impl_opts);
	posix_sock_init(sock, opts->zcopy && !spdk_net_is_loopback(fd) && !enable_ssl &&
			impl_opts.enable_zerocopy_send_client);
	return &sock->base;
}

static struct spdk_sock *
posix_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
{
	return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, false);
	return _posix_sock_listen(ip, port, opts, false);
}

static struct spdk_sock *
posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
{
	return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, false);
	return _posix_sock_connect(ip, port, opts, false);
}

static struct spdk_sock *
@@ -2138,13 +2176,13 @@ SPDK_NET_IMPL_REGISTER_DEFAULT(posix, &g_posix_net_impl);
static struct spdk_sock *
ssl_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
{
	return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, true);
	return _posix_sock_listen(ip, port, opts, true);
}

static struct spdk_sock *
ssl_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
{
	return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, true);
	return _posix_sock_connect(ip, port, opts, true);
}

static struct spdk_sock *