Commit e0b9348b authored by Ben Walker's avatar Ben Walker Committed by Konrad Sztyber
Browse files

hello_sock: Use zero copy receive for the server side



Change-Id: I188fc50202e111ef7b1f8811220b2a01915a4270
Signed-off-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16610


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
parent 2432eadf
Loading
Loading
Loading
Loading
+57 −14
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ static char *g_psk_identity;
struct hello_context_t {
	bool is_server;
	char *host;
	char *sock_impl_name;
	const char *sock_impl_name;
	int port;
	int zcopy;
	int ktls;
@@ -54,6 +54,7 @@ struct hello_context_t {
	struct spdk_sock *sock;

	struct spdk_sock_group *group;
	void *buf;
	struct spdk_poller *poller_in;
	struct spdk_poller *poller_out;
	struct spdk_poller *time_out;
@@ -144,6 +145,8 @@ hello_sock_close_timeout_poll(void *arg)
	struct hello_context_t *ctx = arg;
	SPDK_NOTICELOG("Connection closed\n");

	free(ctx->buf);

	spdk_poller_unregister(&ctx->time_out);
	spdk_poller_unregister(&ctx->poller_in);
	spdk_sock_close(&ctx->sock);
@@ -295,31 +298,35 @@ hello_sock_connect(struct hello_context_t *ctx)
static void
hello_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
{
	ssize_t n;
	char buf[BUFFER_SIZE];
	struct iovec iov;
	int rc;
	struct hello_context_t *ctx = arg;
	struct iovec iov = {};
	ssize_t n;
	void *user_ctx;

	n = spdk_sock_recv(sock, buf, sizeof(buf));
	if (n < 0) {
	rc = spdk_sock_recv_next(sock, &iov.iov_base, &user_ctx);
	if (rc < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
				    errno, spdk_strerror(errno));
			return;
		}

		SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
		if (errno != ENOTCONN && errno != ECONNRESET) {
			SPDK_ERRLOG("spdk_sock_recv_zcopy() failed, errno %d: %s\n",
				    errno, spdk_strerror(errno));
		}
	}

	if (n > 0) {
		ctx->bytes_in += n;
		iov.iov_base = buf;
		iov.iov_len = n;
	iov.iov_len = rc;

	if (iov.iov_len > 0) {
		ctx->bytes_in += iov.iov_len;
		n = spdk_sock_writev(sock, &iov, 1);
		if (n > 0) {
			assert(n == rc);
			ctx->bytes_out += n;
		}

		spdk_sock_group_provide_buf(ctx->group, iov.iov_base, BUFFER_SIZE, NULL);
		return;
	}

@@ -442,6 +449,18 @@ hello_sock_listen(struct hello_context_t *ctx)
	 */
	ctx->group = spdk_sock_group_create(NULL);

	/*
	 * Provide a buffer to the group to be used with receive.
	 */
	ctx->buf = calloc(1, BUFFER_SIZE);
	if (ctx->buf == NULL) {
		SPDK_ERRLOG("Cannot allocate memory for sock group\n");
		spdk_sock_close(&ctx->sock);
		return -1;
	}

	spdk_sock_group_provide_buf(ctx->group, ctx->buf, BUFFER_SIZE, NULL);

	g_is_running = true;

	/*
@@ -510,6 +529,30 @@ main(int argc, char **argv)
	hello_context.psk_identity = g_psk_identity;
	hello_context.verbose = g_verbose;

	if (hello_context.sock_impl_name == NULL) {
		hello_context.sock_impl_name = spdk_sock_get_default_impl();

		if (hello_context.sock_impl_name == NULL) {
			SPDK_ERRLOG("No sock implementations available!\n");
			exit(-1);
		}
	}

	if (hello_context.is_server) {
		struct spdk_sock_impl_opts impl_opts = {};
		size_t len = sizeof(impl_opts);

		rc = spdk_sock_impl_get_opts(hello_context.sock_impl_name, &impl_opts, &len);
		if (rc < 0) {
			exit(rc);
		}

		/* Our applications will post buffers to be used for receiving. That feature
		 * is mutually exclusive with the recv pipe, so we need to disable it. */
		impl_opts.enable_recv_pipe = false;
		spdk_sock_impl_set_opts(hello_context.sock_impl_name, &impl_opts, len);
	}

	rc = spdk_app_start(&opts, hello_start, &hello_context);
	if (rc) {
		SPDK_ERRLOG("ERROR starting application\n");