uring: Not enable zero copy if fd is opened on a loopback device.

In order to not affect the loopback test.

Also create a sock_common.c file which can be used by posix/uring
implementation. We do not put such code in sock.c. Because sock.c
is the general layer. Other users may include their own user space
sock impelmentations. So put those common code in sock_common.c instead.

Signed-off-by: Ziye Yang <ziye.yang@intel.com>
Change-Id: I983ec2313119539e6eed2d9f11ba1488c0ed6560
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/8769
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
This commit is contained in:
Ziye Yang 2021-07-14 22:56:03 +08:00 committed by Tomasz Zawadzki
parent 34ab803d30
commit fc9a264595
3 changed files with 112 additions and 108 deletions

View File

@ -52,6 +52,7 @@
#include "spdk/sock.h"
#include "spdk/util.h"
#include "spdk_internal/sock.h"
#include "../sock_kernel.h"
#define MAX_TMPBUF 1024
#define PORTNUMLEN 32
@ -108,35 +109,6 @@ posix_sock_map_cleanup(void)
spdk_sock_map_cleanup(&g_map);
}
static int
get_addr_str(struct sockaddr *sa, char *host, size_t hlen)
{
const char *result = NULL;
if (sa == NULL || host == NULL) {
return -1;
}
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;
}
if (result != NULL) {
return 0;
} else {
return -1;
}
}
#define __posix_sock(sock) (struct spdk_posix_sock *)sock
#define __posix_group_impl(group) (struct spdk_posix_sock_group_impl *)group
@ -382,55 +354,6 @@ posix_sock_alloc(int fd, bool enable_zero_copy)
return sock;
}
static bool
sock_is_loopback(int fd)
{
struct ifaddrs *addrs, *tmp;
struct sockaddr_storage sa = {};
socklen_t salen;
struct ifreq ifr = {};
char ip_addr[256], ip_addr_tmp[256];
int rc;
bool is_loopback = false;
salen = sizeof(sa);
rc = getsockname(fd, (struct sockaddr *)&sa, &salen);
if (rc != 0) {
return is_loopback;
}
memset(ip_addr, 0, sizeof(ip_addr));
rc = get_addr_str((struct sockaddr *)&sa, ip_addr, sizeof(ip_addr));
if (rc != 0) {
return is_loopback;
}
getifaddrs(&addrs);
for (tmp = addrs; tmp != NULL; tmp = tmp->ifa_next) {
if (tmp->ifa_addr && (tmp->ifa_flags & IFF_UP) &&
(tmp->ifa_addr->sa_family == sa.ss_family)) {
memset(ip_addr_tmp, 0, sizeof(ip_addr_tmp));
rc = get_addr_str(tmp->ifa_addr, ip_addr_tmp, sizeof(ip_addr_tmp));
if (rc != 0) {
continue;
}
if (strncmp(ip_addr, ip_addr_tmp, sizeof(ip_addr)) == 0) {
memcpy(ifr.ifr_name, tmp->ifa_name, sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFFLAGS, &ifr);
if (ifr.ifr_flags & IFF_LOOPBACK) {
is_loopback = true;
}
goto end;
}
}
}
end:
freeifaddrs(addrs);
return is_loopback;
}
static struct spdk_sock *
posix_sock_create(const char *ip, int port,
enum posix_sock_create_type type,

109
module/sock/sock_kernel.h Normal file
View File

@ -0,0 +1,109 @@
/*-
* 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.
*/
static int
get_addr_str(struct sockaddr *sa, char *host, size_t hlen)
{
const char *result = NULL;
if (sa == NULL || host == NULL) {
return -1;
}
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;
}
if (result != NULL) {
return 0;
} else {
return -1;
}
}
static bool
sock_is_loopback(int fd)
{
struct ifaddrs *addrs, *tmp;
struct sockaddr_storage sa = {};
socklen_t salen;
struct ifreq ifr = {};
char ip_addr[256], ip_addr_tmp[256];
int rc;
bool is_loopback = false;
salen = sizeof(sa);
rc = getsockname(fd, (struct sockaddr *)&sa, &salen);
if (rc != 0) {
return is_loopback;
}
memset(ip_addr, 0, sizeof(ip_addr));
rc = get_addr_str((struct sockaddr *)&sa, ip_addr, sizeof(ip_addr));
if (rc != 0) {
return is_loopback;
}
getifaddrs(&addrs);
for (tmp = addrs; tmp != NULL; tmp = tmp->ifa_next) {
if (tmp->ifa_addr && (tmp->ifa_flags & IFF_UP) &&
(tmp->ifa_addr->sa_family == sa.ss_family)) {
memset(ip_addr_tmp, 0, sizeof(ip_addr_tmp));
rc = get_addr_str(tmp->ifa_addr, ip_addr_tmp, sizeof(ip_addr_tmp));
if (rc != 0) {
continue;
}
if (strncmp(ip_addr, ip_addr_tmp, sizeof(ip_addr)) == 0) {
memcpy(ifr.ifr_name, tmp->ifa_name, sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFFLAGS, &ifr);
if (ifr.ifr_flags & IFF_LOOPBACK) {
is_loopback = true;
}
goto end;
}
}
}
end:
freeifaddrs(addrs);
return is_loopback;
}

View File

@ -48,6 +48,7 @@
#include "spdk_internal/sock.h"
#include "spdk_internal/assert.h"
#include "../sock_kernel.h"
#define MAX_TMPBUF 1024
#define PORTNUMLEN 32
@ -136,35 +137,6 @@ uring_sock_map_cleanup(void)
#define SPDK_URING_SOCK_REQUEST_IOV(req) ((struct iovec *)((uint8_t *)req + sizeof(struct spdk_sock_request)))
static int
get_addr_str(struct sockaddr *sa, char *host, size_t hlen)
{
const char *result = NULL;
if (sa == NULL || host == NULL) {
return -1;
}
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;
}
if (result != NULL) {
return 0;
} else {
return -1;
}
}
#define __uring_sock(sock) (struct spdk_uring_sock *)sock
#define __uring_group_impl(group) (struct spdk_uring_sock_group_impl *)group
@ -565,7 +537,7 @@ retry:
return NULL;
}
enable_zcopy_user_opts = opts->zcopy;
enable_zcopy_user_opts = opts->zcopy && !sock_is_loopback(fd);
sock = uring_sock_alloc(fd, enable_zcopy_user_opts && enable_zcopy_impl_opts);
if (sock == NULL) {
SPDK_ERRLOG("sock allocation failed\n");