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

sock: add util to getaddrinfo



Just moved the code from posix.c, code in uring.c is logically the same.

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


Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz@tzawadzki.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
parent e9938f77
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ extern "C" {
#define MIN_SO_RCVBUF_SIZE (4 * 1024)
#define MIN_SO_SNDBUF_SIZE (4 * 1024)
#define IOV_BATCH_SIZE 64
#define PORTNUMLEN 32

struct spdk_sock {
	struct spdk_net_impl		*net_impl;
@@ -385,6 +384,15 @@ spdk_sock_get_placement_id(int fd, enum spdk_placement_mode mode, int *placement
	}
}

/**
 * Converts ip and port into address.
 *
 * Use freeaddrinfo() when returned object is no longer needed.
 *
 * \return addrinfo object or NULL in case of any failures.
 */
struct addrinfo *spdk_sock_posix_getaddrinfo(const char *ip, int port);

/**
 * Creates sock file descriptor.
 *
+41 −0
Original line number Diff line number Diff line
@@ -19,6 +19,9 @@
#define SPDK_SOCK_DEFAULT_ZCOPY true
#define SPDK_SOCK_DEFAULT_ACK_TIMEOUT 0

#define PORTNUMLEN 32
#define MAX_TMPBUF 1024

#define SPDK_SOCK_OPTS_FIELD_OK(opts, field) (offsetof(struct spdk_sock_opts, field) + sizeof(opts->field) <= (opts->opts_size))

static STAILQ_HEAD(, spdk_net_impl) g_net_impls = STAILQ_HEAD_INITIALIZER(g_net_impls);
@@ -336,6 +339,44 @@ sock_init_opts(struct spdk_sock_opts *opts, struct spdk_sock_opts *opts_user)
	}
}

struct addrinfo *
spdk_sock_posix_getaddrinfo(const char *ip, int port)
{
	struct addrinfo *res, hints = {};
	char portnum[PORTNUMLEN];
	char buf[MAX_TMPBUF];
	char *p;
	int rc;

	if (ip == NULL) {
		return NULL;
	}

	if (ip[0] == '[') {
		snprintf(buf, sizeof(buf), "%s", ip + 1);
		p = strchr(buf, ']');
		if (p != NULL) {
			*p = '\0';
		}

		ip = (const char *) &buf[0];
	}

	snprintf(portnum, sizeof portnum, "%d", port);
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV;
	hints.ai_flags |= AI_PASSIVE;
	hints.ai_flags |= AI_NUMERICHOST;
	rc = getaddrinfo(ip, portnum, &hints, &res);
	if (rc != 0) {
		SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
		return NULL;
	}

	return res;
}

int
spdk_sock_posix_fd_create(struct addrinfo *res, struct spdk_sock_opts *opts,
			  struct spdk_sock_impl_opts *impl_opts)
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@
	spdk_net_impl_register;
	spdk_sock_posix_fd_create;
	spdk_sock_posix_fd_connect;
	spdk_sock_posix_getaddrinfo;
	spdk_sock_group_get_buf;
	spdk_sock_map_insert;
	spdk_sock_map_release;
+4 −30
Original line number Diff line number Diff line
@@ -32,8 +32,6 @@
#include "openssl/err.h"
#include "openssl/ssl.h"

#define MAX_TMPBUF 1024

#if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
#define SPDK_ZEROCOPY
#endif
@@ -857,12 +855,8 @@ posix_sock_create(const char *ip, int port,
{
	struct spdk_posix_sock *sock;
	struct spdk_sock_impl_opts impl_opts;
	char buf[MAX_TMPBUF];
	char portnum[PORTNUMLEN];
	char *p;
	struct addrinfo hints, *res, *res0;
	int fd;
	int rc;
	struct addrinfo *res, *res0;
	int fd, rc;
	bool enable_zcopy_user_opts = true;
	bool enable_zcopy_impl_opts = true;
	SSL_CTX *ctx = 0;
@@ -875,28 +869,8 @@ posix_sock_create(const char *ip, int port,
		_opts_get_impl_opts(opts, &impl_opts, &g_posix_impl_opts);
	}

	if (ip == NULL) {
		return NULL;
	}
	if (ip[0] == '[') {
		snprintf(buf, sizeof(buf), "%s", ip + 1);
		p = strchr(buf, ']');
		if (p != NULL) {
			*p = '\0';
		}
		ip = (const char *) &buf[0];
	}

	snprintf(portnum, sizeof portnum, "%d", port);
	memset(&hints, 0, sizeof hints);
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV;
	hints.ai_flags |= AI_PASSIVE;
	hints.ai_flags |= AI_NUMERICHOST;
	rc = getaddrinfo(ip, portnum, &hints, &res0);
	if (rc != 0) {
		SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
	res0 = spdk_sock_posix_getaddrinfo(ip, port);
	if (!res0) {
		return NULL;
	}

+4 −29
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@
#include "spdk_internal/assert.h"
#include "spdk/net.h"

#define MAX_TMPBUF 1024
#define SPDK_SOCK_GROUP_QUEUE_DEPTH 4096
#define SPDK_SOCK_CMG_INFO_SIZE (sizeof(struct cmsghdr) + sizeof(struct sock_extended_err))

@@ -474,40 +473,16 @@ uring_sock_create(const char *ip, int port,
{
	struct spdk_uring_sock *sock;
	struct spdk_sock_impl_opts impl_opts;
	char buf[MAX_TMPBUF];
	char portnum[PORTNUMLEN];
	char *p;
	struct addrinfo hints, *res, *res0;
	int fd;
	int rc;
	struct addrinfo *res, *res0;
	int fd, rc;
	bool enable_zcopy_impl_opts = false;
	bool enable_zcopy_user_opts = true;

	assert(opts != NULL);
	uring_opts_get_impl_opts(opts, &impl_opts);

	if (ip == NULL) {
		return NULL;
	}
	if (ip[0] == '[') {
		snprintf(buf, sizeof(buf), "%s", ip + 1);
		p = strchr(buf, ']');
		if (p != NULL) {
			*p = '\0';
		}
		ip = (const char *) &buf[0];
	}

	snprintf(portnum, sizeof portnum, "%d", port);
	memset(&hints, 0, sizeof hints);
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV;
	hints.ai_flags |= AI_PASSIVE;
	hints.ai_flags |= AI_NUMERICHOST;
	rc = getaddrinfo(ip, portnum, &hints, &res0);
	if (rc != 0) {
		SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
	res0 = spdk_sock_posix_getaddrinfo(ip, port);
	if (!res0) {
		return NULL;
	}

Loading