From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 02694A00BE; Wed, 29 Apr 2020 01:50:44 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3CC701D655; Wed, 29 Apr 2020 01:50:39 +0200 (CEST) Received: from mail-lj1-f195.google.com (mail-lj1-f195.google.com [209.85.208.195]) by dpdk.org (Postfix) with ESMTP id 808001D646 for ; Wed, 29 Apr 2020 01:50:26 +0200 (CEST) Received: by mail-lj1-f195.google.com with SMTP id a21so664937ljj.11 for ; Tue, 28 Apr 2020 16:50:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wfVgdBUV8oOJw8mhvbJXL0VthagqLB1SYhAc4eFx8dU=; b=NZ4RIeO1k453TYUVlfbKbEECT6ezmPBBSEi2nRiN6E5y3xeOFQjy/YUaOwQlHVe5FK ZHCt6/vMSvhBdkAxKePk7ypAANt6Qb74jtcFTlBNhWurPswX4deOVPC3umH+2X8br2VJ zOqc/DQqBhgmOuurjJVhWJjQPXFTispmyWHh/df3QusymANQw0Bdh34elA5CIuQIZ4Rv jLS5bSDuIV7QvRbR5vS0G936TgVQXRp+TLk5HbNWIR+oehapY4DROtJF45/0/lgV6xnS Ut+hlEOp57n/Gpev6B27SkeGT079P+5mslZcm20maylqWgrJwF3u+qfCrA+tfCNCpHXT auHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wfVgdBUV8oOJw8mhvbJXL0VthagqLB1SYhAc4eFx8dU=; b=MSi+Vf9WGI9fWUpGJe2Mx7ZtJVZINRnClxc7lVooWlhMyNOwmEV4E+el0OLjD0jJYi 273y6N8UaDwCZt9RtFeLAkR7MKY7lX20cg4rSYmvqo0MdIY0V/No5IMNsrNnD4mfIANV NBa/PCW0yC+dmoHsBVUIGJjV9bODKL42HKpEMmBfvJ3Yo4IMc0d2bx6hfAILoFnPJSNd BcYZV1+6GY1wuEdvzxJ8AYnxYQ1tSlFyYjjBufpepKzUzfKXx9vRnDSPVqJfnRXQ318D MFtKlu0WZiw3FmXiNWmInhMw8eqEzsQIsl5JsMpumzLgGXjiKg78DT/O+si00caZbBzB Xb5g== X-Gm-Message-State: AGi0PubUYrxmb3dyx8/Va8qcEsKXzsMGobag7oxSPwVbm3Ba4qIzMdTQ qciODxE1UUyG6IO8+wrY8hGaZOWR/xg= X-Google-Smtp-Source: APiQypIMxl6n2snooDxkjkr0cAXjtP8CmSzWrdGuuivq/uhoDztTp58dcJlspFWc8HRVhJwhisTWmg== X-Received: by 2002:a05:651c:c8:: with SMTP id 8mr18106789ljr.182.1588117825363; Tue, 28 Apr 2020 16:50:25 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.googlemail.com with ESMTPSA id g10sm183339lfc.95.2020.04.28.16.50.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2020 16:50:24 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: "Dmitry Malloy (MESHCHANINOV)" , Narcisa Ana Maria Vasile , Fady Bader , Tal Shnaiderman , Dmitry Kozlyuk , Anatoly Burakov , Bruce Richardson Date: Wed, 29 Apr 2020 02:50:09 +0300 Message-Id: <20200428235015.2820677-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200428235015.2820677-1-dmitry.kozliuk@gmail.com> References: <20200410164342.1194634-1-dmitry.kozliuk@gmail.com> <20200428235015.2820677-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v4 2/8] eal: introduce internal wrappers for file operations X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" EAL common code uses file locking and truncation. Introduce OS-independent wrappers in order to support both Linux/FreeBSD and Windows: * eal_file_lock: lock or unlock an open file. * eal_file_truncate: enforce a given size for an open file. Wrappers follow POSIX semantics, but interface is not POSIX, so that it can be made more clean, e.g. by not mixing locking operation and behaviour on conflict. Implementation for Linux and FreeBSD is placed in "unix" subdirectory, which is intended for common code between the two. Files should be named after the ones from which the code is factored in OS subdirectory. Signed-off-by: Dmitry Kozlyuk --- lib/librte_eal/common/eal_common_fbarray.c | 19 ++++---- lib/librte_eal/common/eal_private.h | 45 +++++++++++++++++++ lib/librte_eal/freebsd/Makefile | 4 ++ lib/librte_eal/linux/Makefile | 4 ++ lib/librte_eal/meson.build | 4 ++ lib/librte_eal/unix/eal_unix.c | 51 ++++++++++++++++++++++ lib/librte_eal/unix/meson.build | 6 +++ 7 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 lib/librte_eal/unix/eal_unix.c create mode 100644 lib/librte_eal/unix/meson.build diff --git a/lib/librte_eal/common/eal_common_fbarray.c b/lib/librte_eal/common/eal_common_fbarray.c index 4f8f1af73..1e55757ca 100644 --- a/lib/librte_eal/common/eal_common_fbarray.c +++ b/lib/librte_eal/common/eal_common_fbarray.c @@ -8,8 +8,8 @@ #include #include #include -#include #include +#include #include #include @@ -85,7 +85,7 @@ resize_and_map(int fd, void *addr, size_t len) char path[PATH_MAX]; void *map_addr; - if (ftruncate(fd, len)) { + if (eal_file_truncate(fd, len)) { RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path); /* pass errno up the chain */ rte_errno = errno; @@ -778,7 +778,8 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len, __func__, path, strerror(errno)); rte_errno = errno; 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", __func__, path, strerror(errno)); rte_errno = EBUSY; @@ -789,10 +790,8 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len, * still attach to it, but no other process could reinitialize * it. */ - if (flock(fd, LOCK_SH | LOCK_NB)) { - rte_errno = errno; + if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN)) goto fail; - } if (resize_and_map(fd, data, mmap_len)) goto fail; @@ -895,10 +894,8 @@ rte_fbarray_attach(struct rte_fbarray *arr) } /* lock the file, to let others know we're using it */ - if (flock(fd, LOCK_SH | LOCK_NB)) { - rte_errno = errno; + if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN)) goto fail; - } if (resize_and_map(fd, data, mmap_len)) goto fail; @@ -1025,7 +1022,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr) * has been detached by all other processes */ 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_errno = EBUSY; ret = -1; @@ -1042,7 +1039,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr) * we're still holding an exclusive lock, so drop it to * shared. */ - flock(fd, LOCK_SH | LOCK_NB); + eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN); ret = -1; goto out; diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index ecf827914..3aafd892f 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -448,4 +448,49 @@ eal_malloc_no_trace(const char *type, size_t size, unsigned int align); void eal_free_no_trace(void *addr); +/** 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_ */ diff --git a/lib/librte_eal/freebsd/Makefile b/lib/librte_eal/freebsd/Makefile index a8400f20a..a26c455c7 100644 --- a/lib/librte_eal/freebsd/Makefile +++ b/lib/librte_eal/freebsd/Makefile @@ -7,6 +7,7 @@ LIB = librte_eal.a ARCH_DIR ?= $(RTE_ARCH) VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR) +VPATH += $(RTE_SDK)/lib/librte_eal/unix VPATH += $(RTE_SDK)/lib/librte_eal/common 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_reciprocal.c +# from unix dir +SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += eal_unix.c + # from arch dir SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_cpuflags.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_hypervisor.c diff --git a/lib/librte_eal/linux/Makefile b/lib/librte_eal/linux/Makefile index a77eb1757..fa41f00bf 100644 --- a/lib/librte_eal/linux/Makefile +++ b/lib/librte_eal/linux/Makefile @@ -7,6 +7,7 @@ LIB = librte_eal.a ARCH_DIR ?= $(RTE_ARCH) VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR) +VPATH += $(RTE_SDK)/lib/librte_eal/unix VPATH += $(RTE_SDK)/lib/librte_eal/common 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_reciprocal.c +# from unix dir +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += eal_unix.c + # from arch dir SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_cpuflags.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_hypervisor.c diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build index 0267c3b9d..98c97dd07 100644 --- a/lib/librte_eal/meson.build +++ b/lib/librte_eal/meson.build @@ -6,6 +6,10 @@ subdir('include') subdir('common') +if not is_windows + subdir('unix') +endif + dpdk_conf.set('RTE_EXEC_ENV_' + exec_env.to_upper(), 1) subdir(exec_env) diff --git a/lib/librte_eal/unix/eal_unix.c b/lib/librte_eal/unix/eal_unix.c new file mode 100644 index 000000000..b9c57ef18 --- /dev/null +++ b/lib/librte_eal/unix/eal_unix.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2020 Dmitry Kozlyuk + */ + +#include +#include +#include + +#include + +#include "eal_private.h" + +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; +} diff --git a/lib/librte_eal/unix/meson.build b/lib/librte_eal/unix/meson.build new file mode 100644 index 000000000..cfa1b4ef9 --- /dev/null +++ b/lib/librte_eal/unix/meson.build @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2020 Dmitry Kozlyuk + +sources += files( + 'eal_unix.c', +) -- 2.25.1