2016-08-02 22:35:29 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2016-08-02 22:35:29 +00:00
|
|
|
|
2018-02-08 00:28:19 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-02 22:35:29 +00:00
|
|
|
#include "spdk/log.h"
|
2018-02-07 19:58:14 +00:00
|
|
|
#include "spdk/sock.h"
|
2018-02-16 18:14:32 +00:00
|
|
|
#include "spdk_internal/sock.h"
|
2018-02-08 00:28:19 +00:00
|
|
|
#include "spdk/queue.h"
|
2016-08-02 22:35:29 +00:00
|
|
|
|
|
|
|
#define MAX_TMPBUF 1024
|
|
|
|
#define PORTNUMLEN 32
|
|
|
|
|
2018-02-08 00:28:19 +00:00
|
|
|
#define MAX_EVENTS_PER_POLL 32
|
|
|
|
|
2018-02-16 18:14:32 +00:00
|
|
|
STAILQ_HEAD(, spdk_net_impl) g_net_impls = STAILQ_HEAD_INITIALIZER(g_net_impls);
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
struct spdk_sock {
|
2018-02-08 00:28:19 +00:00
|
|
|
spdk_sock_cb cb_fn;
|
|
|
|
void *cb_arg;
|
|
|
|
TAILQ_ENTRY(spdk_sock) link;
|
|
|
|
};
|
|
|
|
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock {
|
|
|
|
struct spdk_sock base;
|
2018-02-08 00:28:19 +00:00
|
|
|
int fd;
|
2018-02-16 20:44:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct spdk_sock_group {
|
2018-02-08 00:28:19 +00:00
|
|
|
TAILQ_HEAD(, spdk_sock) socks;
|
2018-02-07 21:46:22 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group {
|
|
|
|
struct spdk_sock_group base;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2017-10-25 02:27:58 +00:00
|
|
|
static int get_addr_str(struct sockaddr *sa, char *host, size_t hlen)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2017-10-25 02:27:58 +00:00
|
|
|
const char *result = NULL;
|
2017-06-26 18:28:14 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (sa == NULL || host == NULL) {
|
2016-08-02 22:35:29 +00:00
|
|
|
return -1;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
|
2017-10-25 02:27:58 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
result = inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
|
|
|
|
host, hlen);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
result = inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
|
|
|
|
host, hlen);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (result != NULL) {
|
2017-10-25 02:27:58 +00:00
|
|
|
return 0;
|
2017-12-07 23:23:48 +00:00
|
|
|
} else {
|
2017-10-25 02:27:58 +00:00
|
|
|
return -1;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 20:44:54 +00:00
|
|
|
#define __posix_sock(sock) (struct spdk_posix_sock *)sock
|
|
|
|
#define __posix_group(group) (struct spdk_posix_sock_group *)group
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, char *caddr, int clen)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2016-08-02 22:35:29 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
|
|
|
int rc;
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
2016-08-02 22:35:29 +00:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
salen = sizeof sa;
|
2018-02-07 21:46:22 +00:00
|
|
|
rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sa.ss_family) {
|
|
|
|
case AF_UNIX:
|
|
|
|
/* Acceptable connection types that don't have IPs */
|
|
|
|
return 0;
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
|
|
|
/* Code below will get IP addresses */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unsupported socket family */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:27:58 +00:00
|
|
|
rc = get_addr_str((struct sockaddr *)&sa, saddr, slen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
salen = sizeof sa;
|
2018-02-07 21:46:22 +00:00
|
|
|
rc = getpeername(sock->fd, (struct sockaddr *) &sa, &salen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getpeername() failed (errno=%d)\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:27:58 +00:00
|
|
|
rc = get_addr_str((struct sockaddr *)&sa, caddr, clen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
enum spdk_posix_sock_create_type {
|
2016-08-02 22:35:29 +00:00
|
|
|
SPDK_SOCK_CREATE_LISTEN,
|
|
|
|
SPDK_SOCK_CREATE_CONNECT,
|
|
|
|
};
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
static struct spdk_sock *
|
2018-02-16 19:04:00 +00:00
|
|
|
spdk_posix_sock_create(const char *ip, int port, enum spdk_posix_sock_create_type type)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock;
|
2016-08-02 22:35:29 +00:00
|
|
|
char buf[MAX_TMPBUF];
|
|
|
|
char portnum[PORTNUMLEN];
|
|
|
|
char *p;
|
|
|
|
struct addrinfo hints, *res, *res0;
|
2018-02-07 20:20:51 +00:00
|
|
|
int fd, flag;
|
2016-08-02 22:35:29 +00:00
|
|
|
int val = 1;
|
|
|
|
int rc;
|
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (ip == NULL) {
|
2018-02-07 21:46:22 +00:00
|
|
|
return NULL;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
if (ip[0] == '[') {
|
|
|
|
snprintf(buf, sizeof(buf), "%s", ip + 1);
|
|
|
|
p = strchr(buf, ']');
|
2017-12-07 23:23:48 +00:00
|
|
|
if (p != NULL) {
|
2016-08-02 22:35:29 +00:00
|
|
|
*p = '\0';
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
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 (errno=%d)\n", errno);
|
2018-02-07 21:46:22 +00:00
|
|
|
return NULL;
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try listen */
|
2018-02-07 20:20:51 +00:00
|
|
|
fd = -1;
|
2016-08-02 22:35:29 +00:00
|
|
|
for (res = res0; res != NULL; res = res->ai_next) {
|
|
|
|
retry:
|
2018-02-07 20:20:51 +00:00
|
|
|
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (fd < 0) {
|
2016-08-02 22:35:29 +00:00
|
|
|
/* error */
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-07 20:20:51 +00:00
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
2016-08-02 22:35:29 +00:00
|
|
|
/* error */
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-07 20:20:51 +00:00
|
|
|
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
2016-08-02 22:35:29 +00:00
|
|
|
/* error */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == SPDK_SOCK_CREATE_LISTEN) {
|
2018-02-07 20:20:51 +00:00
|
|
|
rc = bind(fd, res->ai_addr, res->ai_addrlen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("bind() failed, errno = %d\n", errno);
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
/* interrupted? */
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
2016-08-02 22:35:29 +00:00
|
|
|
goto retry;
|
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
SPDK_ERRLOG("IP address %s not available. "
|
|
|
|
"Verify IP address in config file "
|
2016-08-08 22:57:49 +00:00
|
|
|
"and make sure setup script is "
|
2016-08-02 22:35:29 +00:00
|
|
|
"run before starting spdk app.\n", ip);
|
2017-06-02 06:32:45 +00:00
|
|
|
/* FALLTHROUGH */
|
2016-08-02 22:35:29 +00:00
|
|
|
default:
|
|
|
|
/* try next family */
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2016-08-02 22:35:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* bind OK */
|
2018-02-07 20:20:51 +00:00
|
|
|
rc = listen(fd, 512);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2016-08-02 22:35:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (type == SPDK_SOCK_CREATE_CONNECT) {
|
2018-02-07 20:20:51 +00:00
|
|
|
rc = connect(fd, res->ai_addr, res->ai_addrlen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
|
|
|
|
/* try next family */
|
2018-02-07 20:20:51 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2016-08-02 22:35:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 20:20:51 +00:00
|
|
|
flag = fcntl(fd, F_GETFL);
|
|
|
|
if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
|
|
|
|
SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2016-08-02 22:35:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
|
2018-02-07 20:20:51 +00:00
|
|
|
if (fd < 0) {
|
2018-02-07 21:46:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = calloc(1, sizeof(*sock));
|
|
|
|
if (sock == NULL) {
|
|
|
|
SPDK_ERRLOG("sock allocation failed\n");
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
2018-02-07 21:46:22 +00:00
|
|
|
|
|
|
|
sock->fd = fd;
|
2018-02-16 20:44:54 +00:00
|
|
|
return &sock->base;
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static struct spdk_sock *
|
|
|
|
spdk_posix_sock_listen(const char *ip, int port)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 19:04:00 +00:00
|
|
|
return spdk_posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN);
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static struct spdk_sock *
|
|
|
|
spdk_posix_sock_connect(const char *ip, int port)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 19:04:00 +00:00
|
|
|
return spdk_posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT);
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static struct spdk_sock *
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_accept(struct spdk_sock *_sock)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2016-08-02 22:35:29 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
2018-02-07 21:46:22 +00:00
|
|
|
int rc;
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *new_sock;
|
2016-08-02 22:35:29 +00:00
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
salen = sizeof(sa);
|
2018-02-07 21:46:22 +00:00
|
|
|
|
|
|
|
assert(sock != NULL);
|
|
|
|
|
|
|
|
rc = accept(sock->fd, (struct sockaddr *)&sa, &salen);
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_sock = calloc(1, sizeof(*sock));
|
|
|
|
if (new_sock == NULL) {
|
|
|
|
SPDK_ERRLOG("sock allocation failed\n");
|
|
|
|
close(rc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_sock->fd = rc;
|
2018-02-16 20:44:54 +00:00
|
|
|
return &new_sock->base;
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_close(struct spdk_sock *_sock)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
|
|
|
|
2018-02-16 20:53:47 +00:00
|
|
|
return close(sock->fd);
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static ssize_t
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_recv(struct spdk_sock *_sock, void *buf, size_t len)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
return recv(sock->fd, buf, len, MSG_DONTWAIT);
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static ssize_t
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_writev(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
return writev(sock->fd, iov, iovcnt);
|
2016-08-02 22:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_set_recvlowat(struct spdk_sock *_sock, int nbytes)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2016-08-02 22:35:29 +00:00
|
|
|
int val;
|
|
|
|
int rc;
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
2016-08-02 22:35:29 +00:00
|
|
|
val = nbytes;
|
2018-02-07 21:46:22 +00:00
|
|
|
rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof val);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (rc != 0) {
|
2016-08-02 22:35:29 +00:00
|
|
|
return -1;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 22:35:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
|
|
|
return setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
|
2016-08-02 22:35:29 +00:00
|
|
|
&sz, sizeof(sz));
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
|
|
|
return setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
|
2016-08-02 22:35:29 +00:00
|
|
|
&sz, sizeof(sz));
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static bool
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_is_ipv6(struct spdk_sock *_sock)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2016-08-02 22:35:29 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
|
|
|
int rc;
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
2016-08-02 22:35:29 +00:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
salen = sizeof sa;
|
2018-02-07 21:46:22 +00:00
|
|
|
rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sa.ss_family == AF_INET6);
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static bool
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_is_ipv4(struct spdk_sock *_sock)
|
2016-08-02 22:35:29 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2016-08-02 22:35:29 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
|
|
|
int rc;
|
|
|
|
|
2018-02-07 21:46:22 +00:00
|
|
|
assert(sock != NULL);
|
|
|
|
|
2016-08-02 22:35:29 +00:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
salen = sizeof sa;
|
2018-02-07 21:46:22 +00:00
|
|
|
rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
|
2016-08-02 22:35:29 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sa.ss_family == AF_INET);
|
|
|
|
}
|
2018-02-08 00:28:19 +00:00
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static struct spdk_sock_group *
|
|
|
|
spdk_posix_sock_group_create(void)
|
2018-02-08 00:28:19 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group *sock_group;
|
2018-02-08 00:28:19 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
fd = epoll_create1(0);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
fd = kqueue();
|
|
|
|
#endif
|
|
|
|
if (fd == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_group = calloc(1, sizeof(*sock_group));
|
|
|
|
if (sock_group == NULL) {
|
|
|
|
SPDK_ERRLOG("sock_group allocation failed\n");
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_group->fd = fd;
|
|
|
|
|
2018-02-16 20:44:54 +00:00
|
|
|
return &sock_group->base;
|
2018-02-08 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_group_add_sock(struct spdk_sock_group *_group, struct spdk_sock *_sock)
|
2018-02-08 00:28:19 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group *group = __posix_group(_group);
|
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2018-02-08 00:28:19 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct epoll_event event;
|
|
|
|
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
event.data.ptr = sock;
|
|
|
|
|
|
|
|
rc = epoll_ctl(group->fd, EPOLL_CTL_ADD, sock->fd, &event);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
struct kevent event;
|
|
|
|
struct timespec ts = {0};
|
|
|
|
|
|
|
|
EV_SET(&event, sock->fd, EVFILT_READ, EV_ADD, 0, 0, sock);
|
|
|
|
|
|
|
|
rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_group_remove_sock(struct spdk_sock_group *_group, struct spdk_sock *_sock)
|
2018-02-08 00:28:19 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group *group = __posix_group(_group);
|
|
|
|
struct spdk_posix_sock *sock = __posix_sock(_sock);
|
2018-02-08 00:28:19 +00:00
|
|
|
int rc;
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct epoll_event event;
|
|
|
|
|
|
|
|
/* Event parameter is ignored but some old kernel version still require it. */
|
|
|
|
rc = epoll_ctl(group->fd, EPOLL_CTL_DEL, sock->fd, &event);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
struct kevent event;
|
|
|
|
struct timespec ts = {0};
|
|
|
|
|
|
|
|
EV_SET(&event, sock->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
|
|
|
|
|
|
|
rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
|
|
|
|
if (rc == 0 && event.flags & EV_ERROR) {
|
|
|
|
rc = -1;
|
|
|
|
errno = event.data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_group_poll_count(struct spdk_sock_group *_group, int max_events,
|
2018-02-16 21:18:23 +00:00
|
|
|
struct spdk_sock **socks)
|
2018-02-08 00:28:19 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group *group = __posix_group(_group);
|
2018-02-08 00:28:19 +00:00
|
|
|
int num_events, i;
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct epoll_event events[MAX_EVENTS_PER_POLL];
|
|
|
|
|
|
|
|
num_events = epoll_wait(group->fd, events, max_events, 0);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
struct kevent events[MAX_EVENTS_PER_POLL];
|
|
|
|
struct timespec ts = {0};
|
|
|
|
|
|
|
|
num_events = kevent(group->fd, NULL, 0, events, max_events, &ts);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (num_events == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_events; i++) {
|
|
|
|
#if defined(__linux__)
|
2018-02-16 21:18:23 +00:00
|
|
|
socks[i] = events[i].data.ptr;
|
2018-02-08 00:28:19 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
2018-02-16 21:18:23 +00:00
|
|
|
socks[i] = events[i].udata;
|
2018-02-08 00:28:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:18:23 +00:00
|
|
|
return num_events;
|
2018-02-08 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
static int
|
2018-02-16 20:44:54 +00:00
|
|
|
spdk_posix_sock_group_close(struct spdk_sock_group *_group)
|
2018-02-08 00:28:19 +00:00
|
|
|
{
|
2018-02-16 20:44:54 +00:00
|
|
|
struct spdk_posix_sock_group *group = __posix_group(_group);
|
|
|
|
|
2018-02-16 20:53:47 +00:00
|
|
|
return close(group->fd);
|
2018-02-08 00:28:19 +00:00
|
|
|
}
|
2018-02-16 19:04:00 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_getaddr(struct spdk_sock *sock, char *saddr, int slen, char *caddr, int clen)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_getaddr(sock, saddr, slen, caddr, clen);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_sock *
|
|
|
|
spdk_sock_connect(const char *ip, int port)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_connect(ip, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_sock *
|
|
|
|
spdk_sock_listen(const char *ip, int port)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_listen(ip, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_sock *
|
|
|
|
spdk_sock_accept(struct spdk_sock *sock)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_accept(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_close(struct spdk_sock **sock)
|
|
|
|
{
|
2018-02-16 20:53:47 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (*sock == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*sock)->cb_fn != NULL) {
|
|
|
|
/* This sock is still part of a sock_group. */
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_posix_sock_close(*sock);
|
|
|
|
if (rc == 0) {
|
|
|
|
free(*sock);
|
|
|
|
*sock = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
spdk_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
|
|
|
|
{
|
2018-02-19 04:12:55 +00:00
|
|
|
if (sock == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
return spdk_posix_sock_recv(sock, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
|
|
|
|
{
|
2018-02-19 04:12:55 +00:00
|
|
|
if (sock == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:04:00 +00:00
|
|
|
return spdk_posix_sock_writev(sock, iov, iovcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_set_recvlowat(struct spdk_sock *sock, int nbytes)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_set_recvlowat(sock, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_set_recvbuf(struct spdk_sock *sock, int sz)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_set_recvbuf(sock, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_set_sendbuf(struct spdk_sock *sock, int sz)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_set_sendbuf(sock, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
spdk_sock_is_ipv6(struct spdk_sock *sock)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_is_ipv6(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
spdk_sock_is_ipv4(struct spdk_sock *sock)
|
|
|
|
{
|
|
|
|
return spdk_posix_sock_is_ipv4(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spdk_sock_group *
|
|
|
|
spdk_sock_group_create(void)
|
|
|
|
{
|
2018-02-16 21:10:29 +00:00
|
|
|
struct spdk_sock_group *group;
|
|
|
|
|
|
|
|
group = spdk_posix_sock_group_create();
|
|
|
|
if (group != NULL) {
|
|
|
|
TAILQ_INIT(&group->socks);
|
|
|
|
}
|
|
|
|
|
|
|
|
return group;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_group_add_sock(struct spdk_sock_group *group, struct spdk_sock *sock,
|
|
|
|
spdk_sock_cb cb_fn, void *cb_arg)
|
|
|
|
{
|
2018-02-16 21:10:29 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (cb_fn == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->cb_fn != NULL) {
|
|
|
|
/*
|
|
|
|
* This sock is already part of a sock_group. Currently we don't
|
|
|
|
* support this.
|
|
|
|
*/
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_posix_sock_group_add_sock(group, sock);
|
|
|
|
if (rc == 0) {
|
|
|
|
TAILQ_INSERT_TAIL(&group->socks, sock, link);
|
|
|
|
sock->cb_fn = cb_fn;
|
|
|
|
sock->cb_arg = cb_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_group_remove_sock(struct spdk_sock_group *group, struct spdk_sock *sock)
|
|
|
|
{
|
2018-02-16 21:10:29 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_posix_sock_group_remove_sock(group, sock);
|
|
|
|
if (rc == 0) {
|
|
|
|
TAILQ_REMOVE(&group->socks, sock, link);
|
|
|
|
sock->cb_fn = NULL;
|
|
|
|
sock->cb_arg = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_group_poll(struct spdk_sock_group *group)
|
|
|
|
{
|
|
|
|
return spdk_sock_group_poll_count(group, MAX_EVENTS_PER_POLL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_group_poll_count(struct spdk_sock_group *group, int max_events)
|
|
|
|
{
|
2018-02-16 21:18:23 +00:00
|
|
|
struct spdk_sock *socks[MAX_EVENTS_PER_POLL];
|
|
|
|
int num_events, i;
|
|
|
|
|
|
|
|
if (max_events < 1) {
|
|
|
|
errno = -EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only poll for up to 32 events at a time - if more events are pending,
|
|
|
|
* the next call to this function will reap them.
|
|
|
|
*/
|
|
|
|
if (max_events > MAX_EVENTS_PER_POLL) {
|
|
|
|
max_events = MAX_EVENTS_PER_POLL;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_events = spdk_posix_sock_group_poll_count(group, max_events, socks);
|
|
|
|
if (num_events == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_events; i++) {
|
|
|
|
struct spdk_sock *sock = socks[i];
|
|
|
|
|
|
|
|
assert(sock->cb_fn != NULL);
|
|
|
|
sock->cb_fn(sock->cb_arg, group, sock);
|
|
|
|
}
|
|
|
|
return 0;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_sock_group_close(struct spdk_sock_group **group)
|
|
|
|
{
|
2018-02-16 20:53:47 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (*group == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TAILQ_EMPTY(&(*group)->socks)) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_posix_sock_group_close(*group);
|
|
|
|
if (rc == 0) {
|
|
|
|
free(*group);
|
|
|
|
*group = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-02-16 19:04:00 +00:00
|
|
|
}
|
2018-02-16 18:14:32 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
spdk_net_impl_register(struct spdk_net_impl *impl)
|
|
|
|
{
|
|
|
|
STAILQ_INSERT_TAIL(&g_net_impls, impl, link);
|
|
|
|
}
|