Commit a0889ece authored by Ben Walker's avatar Ben Walker Committed by Tomasz Zawadzki
Browse files

sock: Add a function to check if a socket is connected



This is useful for detecting sockets that have been disconnected
by the other end without reading data.

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


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 85d9f0a9
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -195,6 +195,15 @@ bool spdk_sock_is_ipv6(struct spdk_sock *sock);
 */
bool spdk_sock_is_ipv4(struct spdk_sock *sock);

/**
 * Check whether the socket is currently connected.
 *
 * \param sock Socket to check
 *
 * \return true if the socket is connected or false otherwise.
 */
bool spdk_sock_is_connected(struct spdk_sock *sock);

/**
 * Callback function for spdk_sock_group_add_sock().
 *
+1 −0
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ struct spdk_net_impl {

	bool (*is_ipv6)(struct spdk_sock *sock);
	bool (*is_ipv4)(struct spdk_sock *sock);
	bool (*is_connected)(struct spdk_sock *sock);

	int (*get_placement_id)(struct spdk_sock *sock, int *placement_id);
	struct spdk_sock_group_impl *(*group_impl_create)(void);
+6 −0
Original line number Diff line number Diff line
@@ -308,6 +308,12 @@ spdk_sock_is_ipv4(struct spdk_sock *sock)
	return sock->net_impl->is_ipv4(sock);
}

bool
spdk_sock_is_connected(struct spdk_sock *sock)
{
	return sock->net_impl->is_connected(sock);
}

struct spdk_sock_group *
spdk_sock_group_create(void *ctx)
{
+24 −0
Original line number Diff line number Diff line
@@ -529,6 +529,29 @@ spdk_posix_sock_is_ipv4(struct spdk_sock *_sock)
	return (sa.ss_family == AF_INET);
}

static bool
spdk_posix_sock_is_connected(struct spdk_sock *_sock)
{
	struct spdk_posix_sock *sock = __posix_sock(_sock);
	uint8_t byte;
	int rc;

	rc = recv(sock->fd, &byte, 1, MSG_PEEK);
	if (rc == 0) {
		return false;
	}

	if (rc < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			return true;
		}

		return false;
	}

	return true;
}

static int
spdk_posix_sock_get_placement_id(struct spdk_sock *_sock, int *placement_id)
{
@@ -686,6 +709,7 @@ static struct spdk_net_impl g_posix_net_impl = {
	.set_priority	= spdk_posix_sock_set_priority,
	.is_ipv6	= spdk_posix_sock_is_ipv6,
	.is_ipv4	= spdk_posix_sock_is_ipv4,
	.is_connected	= spdk_posix_sock_is_connected,
	.get_placement_id	= spdk_posix_sock_get_placement_id,
	.group_impl_create	= spdk_posix_sock_group_impl_create,
	.group_impl_add_sock	= spdk_posix_sock_group_impl_add_sock,
+9 −0
Original line number Diff line number Diff line
@@ -1006,6 +1006,14 @@ spdk_vpp_sock_is_ipv4(struct spdk_sock *_sock)
	return __vpp_session(_sock)->app_session.transport.is_ip4;
}

static bool
spdk_vpp_sock_is_connected(struct spdk_sock *_sock)
{
	assert(g_svm.vpp_initialized);

	return (__vpp_session(_sock)->app_session.session_state == VPP_SESSION_STATE_READY);
}

static int
spdk_vpp_sock_get_placement_id(struct spdk_sock *_sock, int *placement_id)
{
@@ -1440,6 +1448,7 @@ static struct spdk_net_impl g_vpp_net_impl = {
	.set_priority	= spdk_vpp_sock_set_priority,
	.is_ipv6	= spdk_vpp_sock_is_ipv6,
	.is_ipv4	= spdk_vpp_sock_is_ipv4,
	.is_connected	= spdk_vpp_sock_is_connected,
	.get_placement_id	= spdk_vpp_sock_get_placement_id,
	.group_impl_create	= spdk_vpp_sock_group_impl_create,
	.group_impl_add_sock	= spdk_vpp_sock_group_impl_add_sock,
Loading