eal: introduce internal wrappers for file operations
Introduce OS-independent wrappers in order to support common EAL code on Unix and Windows: * eal_file_open: open or create a file. * eal_file_lock: lock or unlock an open file. * eal_file_truncate: enforce a given size for an open file. Implementation for Linux and FreeBSD is placed in "unix" subdirectory, which is intended for common code between the two. These thin wrappers require no special maintenance. Common code supporting multi-process doesn't use the new wrappers, because it is inherently Unix-specific and would impose excessive requirements on the wrappers. Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
This commit is contained in:
parent
67a661ed85
commit
176bb37ca6
@ -168,6 +168,7 @@ T: git://dpdk.org/dpdk
|
|||||||
|
|
||||||
EAL API and common code
|
EAL API and common code
|
||||||
F: lib/librte_eal/common/
|
F: lib/librte_eal/common/
|
||||||
|
F: lib/librte_eal/unix/
|
||||||
F: lib/librte_eal/include/
|
F: lib/librte_eal/include/
|
||||||
F: lib/librte_eal/rte_eal_version.map
|
F: lib/librte_eal/rte_eal_version.map
|
||||||
F: doc/guides/prog_guide/env_abstraction_layer.rst
|
F: doc/guides/prog_guide/env_abstraction_layer.rst
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/file.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <rte_common.h>
|
#include <rte_common.h>
|
||||||
#include <rte_log.h>
|
#include <rte_log.h>
|
||||||
@ -85,10 +85,8 @@ resize_and_map(int fd, void *addr, size_t len)
|
|||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
void *map_addr;
|
void *map_addr;
|
||||||
|
|
||||||
if (ftruncate(fd, len)) {
|
if (eal_file_truncate(fd, len)) {
|
||||||
RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
|
RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
|
||||||
/* pass errno up the chain */
|
|
||||||
rte_errno = errno;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -772,15 +770,15 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
|
|||||||
* and see if we succeed. If we don't, someone else is using it
|
* and see if we succeed. If we don't, someone else is using it
|
||||||
* already.
|
* already.
|
||||||
*/
|
*/
|
||||||
fd = open(path, O_CREAT | O_RDWR, 0600);
|
fd = eal_file_open(path, EAL_OPEN_CREATE | EAL_OPEN_READWRITE);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n",
|
RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n",
|
||||||
__func__, path, strerror(errno));
|
__func__, path, rte_strerror(rte_errno));
|
||||||
rte_errno = errno;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
} else if (flock(fd, LOCK_EX | LOCK_NB)) {
|
} else if (eal_file_lock(
|
||||||
|
fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
|
||||||
RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n",
|
RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n",
|
||||||
__func__, path, strerror(errno));
|
__func__, path, rte_strerror(rte_errno));
|
||||||
rte_errno = EBUSY;
|
rte_errno = EBUSY;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@ -789,10 +787,8 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
|
|||||||
* still attach to it, but no other process could reinitialize
|
* still attach to it, but no other process could reinitialize
|
||||||
* it.
|
* it.
|
||||||
*/
|
*/
|
||||||
if (flock(fd, LOCK_SH | LOCK_NB)) {
|
if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
|
||||||
rte_errno = errno;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
|
||||||
|
|
||||||
if (resize_and_map(fd, data, mmap_len))
|
if (resize_and_map(fd, data, mmap_len))
|
||||||
goto fail;
|
goto fail;
|
||||||
@ -888,17 +884,14 @@ rte_fbarray_attach(struct rte_fbarray *arr)
|
|||||||
|
|
||||||
eal_get_fbarray_path(path, sizeof(path), arr->name);
|
eal_get_fbarray_path(path, sizeof(path), arr->name);
|
||||||
|
|
||||||
fd = open(path, O_RDWR);
|
fd = eal_file_open(path, EAL_OPEN_READWRITE);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
rte_errno = errno;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* lock the file, to let others know we're using it */
|
/* lock the file, to let others know we're using it */
|
||||||
if (flock(fd, LOCK_SH | LOCK_NB)) {
|
if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
|
||||||
rte_errno = errno;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
|
||||||
|
|
||||||
if (resize_and_map(fd, data, mmap_len))
|
if (resize_and_map(fd, data, mmap_len))
|
||||||
goto fail;
|
goto fail;
|
||||||
@ -1025,7 +1018,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr)
|
|||||||
* has been detached by all other processes
|
* has been detached by all other processes
|
||||||
*/
|
*/
|
||||||
fd = tmp->fd;
|
fd = tmp->fd;
|
||||||
if (flock(fd, LOCK_EX | LOCK_NB)) {
|
if (eal_file_lock(fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
|
||||||
RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
|
RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
|
||||||
rte_errno = EBUSY;
|
rte_errno = EBUSY;
|
||||||
ret = -1;
|
ret = -1;
|
||||||
@ -1042,7 +1035,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr)
|
|||||||
* we're still holding an exclusive lock, so drop it to
|
* we're still holding an exclusive lock, so drop it to
|
||||||
* shared.
|
* shared.
|
||||||
*/
|
*/
|
||||||
flock(fd, LOCK_SH | LOCK_NB);
|
eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN);
|
||||||
|
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -420,4 +420,77 @@ eal_malloc_no_trace(const char *type, size_t size, unsigned int align);
|
|||||||
|
|
||||||
void eal_free_no_trace(void *addr);
|
void eal_free_no_trace(void *addr);
|
||||||
|
|
||||||
|
/** Options for eal_file_open(). */
|
||||||
|
enum eal_open_flags {
|
||||||
|
/** Open file for reading. */
|
||||||
|
EAL_OPEN_READONLY = 0x00,
|
||||||
|
/** Open file for reading and writing. */
|
||||||
|
EAL_OPEN_READWRITE = 0x02,
|
||||||
|
/**
|
||||||
|
* Create the file if it doesn't exist.
|
||||||
|
* New files are only accessible to the owner (0600 equivalent).
|
||||||
|
*/
|
||||||
|
EAL_OPEN_CREATE = 0x04
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open or create a file.
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* Path to the file.
|
||||||
|
* @param flags
|
||||||
|
* A combination of eal_open_flags controlling operation and FD behavior.
|
||||||
|
* @return
|
||||||
|
* Open file descriptor on success, (-1) on failure and rte_errno is set.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
eal_file_open(const char *path, int flags);
|
||||||
|
|
||||||
|
/** File locking operation. */
|
||||||
|
enum eal_flock_op {
|
||||||
|
EAL_FLOCK_SHARED, /**< Acquire a shared lock. */
|
||||||
|
EAL_FLOCK_EXCLUSIVE, /**< Acquire an exclusive lock. */
|
||||||
|
EAL_FLOCK_UNLOCK /**< Release a previously taken lock. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Behavior on file locking conflict. */
|
||||||
|
enum eal_flock_mode {
|
||||||
|
EAL_FLOCK_WAIT, /**< Wait until the file gets unlocked to lock it. */
|
||||||
|
EAL_FLOCK_RETURN /**< Return immediately if the file is locked. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock or unlock the file.
|
||||||
|
*
|
||||||
|
* On failure @code rte_errno @endcode is set to the error code
|
||||||
|
* specified by POSIX flock(3) description.
|
||||||
|
*
|
||||||
|
* @param fd
|
||||||
|
* Opened file descriptor.
|
||||||
|
* @param op
|
||||||
|
* Operation to perform.
|
||||||
|
* @param mode
|
||||||
|
* Behavior on conflict.
|
||||||
|
* @return
|
||||||
|
* 0 on success, (-1) on failure.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Truncate or extend the file to the specified size.
|
||||||
|
*
|
||||||
|
* On failure @code rte_errno @endcode is set to the error code
|
||||||
|
* specified by POSIX ftruncate(3) description.
|
||||||
|
*
|
||||||
|
* @param fd
|
||||||
|
* Opened file descriptor.
|
||||||
|
* @param size
|
||||||
|
* Desired file size.
|
||||||
|
* @return
|
||||||
|
* 0 on success, (-1) on failure.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
eal_file_truncate(int fd, ssize_t size);
|
||||||
|
|
||||||
#endif /* _EAL_PRIVATE_H_ */
|
#endif /* _EAL_PRIVATE_H_ */
|
||||||
|
@ -7,6 +7,7 @@ LIB = librte_eal.a
|
|||||||
|
|
||||||
ARCH_DIR ?= $(RTE_ARCH)
|
ARCH_DIR ?= $(RTE_ARCH)
|
||||||
VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR)
|
VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR)
|
||||||
|
VPATH += $(RTE_SDK)/lib/librte_eal/unix
|
||||||
VPATH += $(RTE_SDK)/lib/librte_eal/common
|
VPATH += $(RTE_SDK)/lib/librte_eal/common
|
||||||
|
|
||||||
CFLAGS += -I$(SRCDIR)/include
|
CFLAGS += -I$(SRCDIR)/include
|
||||||
@ -74,6 +75,9 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_service.c
|
|||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_random.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_random.c
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_reciprocal.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_reciprocal.c
|
||||||
|
|
||||||
|
# from unix dir
|
||||||
|
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += eal_file.c
|
||||||
|
|
||||||
# from arch dir
|
# from arch dir
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_cpuflags.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_cpuflags.c
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_hypervisor.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_hypervisor.c
|
||||||
|
@ -7,6 +7,7 @@ LIB = librte_eal.a
|
|||||||
|
|
||||||
ARCH_DIR ?= $(RTE_ARCH)
|
ARCH_DIR ?= $(RTE_ARCH)
|
||||||
VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR)
|
VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR)
|
||||||
|
VPATH += $(RTE_SDK)/lib/librte_eal/unix
|
||||||
VPATH += $(RTE_SDK)/lib/librte_eal/common
|
VPATH += $(RTE_SDK)/lib/librte_eal/common
|
||||||
|
|
||||||
CFLAGS += -I$(SRCDIR)/include
|
CFLAGS += -I$(SRCDIR)/include
|
||||||
@ -81,6 +82,9 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_service.c
|
|||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_random.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_random.c
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_reciprocal.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_reciprocal.c
|
||||||
|
|
||||||
|
# from unix dir
|
||||||
|
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += eal_file.c
|
||||||
|
|
||||||
# from arch dir
|
# from arch dir
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_cpuflags.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_cpuflags.c
|
||||||
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_hypervisor.c
|
SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_hypervisor.c
|
||||||
|
@ -6,6 +6,10 @@ subdir('include')
|
|||||||
|
|
||||||
subdir('common')
|
subdir('common')
|
||||||
|
|
||||||
|
if not is_windows
|
||||||
|
subdir('unix')
|
||||||
|
endif
|
||||||
|
|
||||||
dpdk_conf.set('RTE_EXEC_ENV_' + exec_env.to_upper(), 1)
|
dpdk_conf.set('RTE_EXEC_ENV_' + exec_env.to_upper(), 1)
|
||||||
subdir(exec_env)
|
subdir(exec_env)
|
||||||
|
|
||||||
|
80
lib/librte_eal/unix/eal_file.c
Normal file
80
lib/librte_eal/unix/eal_file.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/* SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
* Copyright(c) 2020 Dmitry Kozlyuk
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <rte_errno.h>
|
||||||
|
|
||||||
|
#include "eal_private.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
eal_file_open(const char *path, int flags)
|
||||||
|
{
|
||||||
|
static const int MODE_MASK = EAL_OPEN_READONLY | EAL_OPEN_READWRITE;
|
||||||
|
|
||||||
|
int ret, sys_flags;
|
||||||
|
|
||||||
|
switch (flags & MODE_MASK) {
|
||||||
|
case EAL_OPEN_READONLY:
|
||||||
|
sys_flags = O_RDONLY;
|
||||||
|
break;
|
||||||
|
case EAL_OPEN_READWRITE:
|
||||||
|
sys_flags = O_RDWR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
rte_errno = ENOTSUP;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & EAL_OPEN_CREATE)
|
||||||
|
sys_flags |= O_CREAT;
|
||||||
|
|
||||||
|
ret = open(path, sys_flags, 0600);
|
||||||
|
if (ret < 0)
|
||||||
|
rte_errno = errno;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
eal_file_truncate(int fd, ssize_t size)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = ftruncate(fd, size);
|
||||||
|
if (ret)
|
||||||
|
rte_errno = errno;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode)
|
||||||
|
{
|
||||||
|
int sys_flags = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (mode == EAL_FLOCK_RETURN)
|
||||||
|
sys_flags |= LOCK_NB;
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case EAL_FLOCK_EXCLUSIVE:
|
||||||
|
sys_flags |= LOCK_EX;
|
||||||
|
break;
|
||||||
|
case EAL_FLOCK_SHARED:
|
||||||
|
sys_flags |= LOCK_SH;
|
||||||
|
break;
|
||||||
|
case EAL_FLOCK_UNLOCK:
|
||||||
|
sys_flags |= LOCK_UN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = flock(fd, sys_flags);
|
||||||
|
if (ret)
|
||||||
|
rte_errno = errno;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
6
lib/librte_eal/unix/meson.build
Normal file
6
lib/librte_eal/unix/meson.build
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
# Copyright(c) 2020 Dmitry Kozlyuk
|
||||||
|
|
||||||
|
sources += files(
|
||||||
|
'eal_file.c',
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user