From 161a3002750e4acd9e9da110b1dc70c0730e37e8 Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Mon, 27 Nov 2017 09:57:41 -0500 Subject: [PATCH] VPP: change sockets from FIONBIO to O_NONBLOCK type There are two ways to set stockets to nonblocking type: - ioctl with FIONBIO - fcntl with O_NONBLOCK Those two should be equivalent for sockets used in SPDK. During testing it was shown that VPP interprets only the second type, so this patch changes all occurences of it. When here, more descriptive error logs were set in case of failure. Signed-off-by: Tomasz Zawadzki Change-Id: Ifa5b30e3a4fa04fe23f41fa2ae9dab4b01dd7d3c Reviewed-on: https://review.gerrithub.io/388816 Reviewed-by: Daniel Verkamp Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris --- lib/jsonrpc/jsonrpc_server_tcp.c | 21 +++++++++++---------- lib/nbd/nbd.c | 8 +++++++- lib/net/sock.c | 9 ++++----- lib/nvme/nvme_uevent.c | 11 ++++++----- lib/nvmf/rdma.c | 9 ++++++--- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/jsonrpc/jsonrpc_server_tcp.c b/lib/jsonrpc/jsonrpc_server_tcp.c index f4d10898c..9c1c75940 100644 --- a/lib/jsonrpc/jsonrpc_server_tcp.c +++ b/lib/jsonrpc/jsonrpc_server_tcp.c @@ -40,7 +40,7 @@ spdk_jsonrpc_server_listen(int domain, int protocol, spdk_jsonrpc_handle_request_fn handle_request) { struct spdk_jsonrpc_server *server; - int rc, val; + int rc, val, flag; char buf[64]; server = calloc(1, sizeof(struct spdk_jsonrpc_server)); @@ -63,10 +63,10 @@ spdk_jsonrpc_server_listen(int domain, int protocol, setsockopt(server->sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); } - val = 1; - rc = ioctl(server->sockfd, FIONBIO, &val); - if (rc != 0) { - SPDK_ERRLOG("ioctl(FIONBIO) failed\n"); + flag = fcntl(server->sockfd, F_GETFL); + if (fcntl(server->sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { + spdk_strerror_r(errno, buf, sizeof(buf)); + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", server->sockfd, buf); close(server->sockfd); free(server); return NULL; @@ -136,7 +136,8 @@ static int spdk_jsonrpc_server_accept(struct spdk_jsonrpc_server *server) { struct spdk_jsonrpc_server_conn *conn; - int rc, conn_idx, nonblock; + int rc, conn_idx, flag; + char buf[64]; rc = accept(server->sockfd, NULL, NULL); if (rc >= 0) { @@ -156,10 +157,10 @@ spdk_jsonrpc_server_accept(struct spdk_jsonrpc_server *server) return -1; } - nonblock = 1; - rc = ioctl(conn->sockfd, FIONBIO, &nonblock); - if (rc != 0) { - SPDK_ERRLOG("ioctl(FIONBIO) failed\n"); + flag = fcntl(conn->sockfd, F_GETFL); + if (fcntl(conn->sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { + spdk_strerror_r(errno, buf, sizeof(buf)); + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", conn->sockfd, buf); close(conn->sockfd); return -1; } diff --git a/lib/nbd/nbd.c b/lib/nbd/nbd.c index a68725daa..0d83b4b80 100644 --- a/lib/nbd/nbd.c +++ b/lib/nbd/nbd.c @@ -390,6 +390,7 @@ spdk_nbd_start(struct spdk_bdev *bdev, const char *nbd_path) int rc; int sp[2]; char buf[64]; + int flag; nbd = calloc(1, sizeof(*nbd)); if (nbd == NULL) { @@ -462,7 +463,12 @@ spdk_nbd_start(struct spdk_bdev *bdev, const char *nbd_path) goto err; } - fcntl(nbd->spdk_sp_fd, F_SETFL, O_NONBLOCK); + flag = fcntl(nbd->spdk_sp_fd, F_GETFL); + if (fcntl(nbd->spdk_sp_fd, F_SETFL, flag | O_NONBLOCK) < 0) { + spdk_strerror_r(errno, buf, sizeof(buf)); + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", nbd->spdk_sp_fd, buf); + goto err; + } to_be32(&nbd->io.resp.magic, NBD_REPLY_MAGIC); nbd->io.req_in_progress = true; diff --git a/lib/net/sock.c b/lib/net/sock.c index fb03a1261..76eacc55f 100644 --- a/lib/net/sock.c +++ b/lib/net/sock.c @@ -128,7 +128,7 @@ spdk_sock_create(const char *ip, int port, enum spdk_sock_create_type type) char portnum[PORTNUMLEN]; char *p; struct addrinfo hints, *res, *res0; - int sock, nonblock; + int sock, flag; int val = 1; int rc; @@ -218,10 +218,9 @@ retry: } } - nonblock = 1; - rc = ioctl(sock, FIONBIO, &nonblock); - if (rc != 0) { - SPDK_ERRLOG("ioctl(FIONBIO) failed\n"); + flag = fcntl(sock, F_GETFL); + if (fcntl(sock, F_SETFL, flag | O_NONBLOCK) < 0) { + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", sock, errno); close(sock); sock = -1; break; diff --git a/lib/nvme/nvme_uevent.c b/lib/nvme/nvme_uevent.c index cb32c3c60..52b50cf12 100644 --- a/lib/nvme/nvme_uevent.c +++ b/lib/nvme/nvme_uevent.c @@ -49,10 +49,10 @@ int spdk_uevent_connect(void) { struct sockaddr_nl addr; - int ret; int netlink_fd; int size = 64 * 1024; - int nonblock = 1; + char buf[64]; + int flag; memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; @@ -65,9 +65,10 @@ spdk_uevent_connect(void) setsockopt(netlink_fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); - ret = ioctl(netlink_fd, FIONBIO, &nonblock); - if (ret != 0) { - SPDK_ERRLOG("ioctl(FIONBIO) failed\n"); + flag = fcntl(netlink_fd, F_GETFL); + if (fcntl(netlink_fd, F_SETFL, flag | O_NONBLOCK) < 0) { + spdk_strerror_r(errno, buf, sizeof(buf)); + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", netlink_fd, buf); close(netlink_fd); return -1; } diff --git a/lib/nvmf/rdma.c b/lib/nvmf/rdma.c index 189806828..7de9a5f00 100644 --- a/lib/nvmf/rdma.c +++ b/lib/nvmf/rdma.c @@ -1145,6 +1145,7 @@ spdk_nvmf_rdma_create(struct spdk_nvmf_tgt *tgt) struct ibv_context **contexts; uint32_t i; char buf[64]; + int flag; rtransport = calloc(1, sizeof(*rtransport)); if (!rtransport) { @@ -1172,9 +1173,11 @@ spdk_nvmf_rdma_create(struct spdk_nvmf_tgt *tgt) return NULL; } - rc = fcntl(rtransport->event_channel->fd, F_SETFL, O_NONBLOCK); - if (rc < 0) { - SPDK_ERRLOG("fcntl to set fd to non-blocking failed\n"); + flag = fcntl(rtransport->event_channel->fd, F_GETFL); + if (fcntl(rtransport->event_channel->fd, F_SETFL, flag | O_NONBLOCK) < 0) { + spdk_strerror_r(errno, buf, sizeof(buf)); + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", + rtransport->event_channel->fd, buf); free(rtransport); return NULL; }