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 63ADCA0577; Tue, 14 Apr 2020 21:45:33 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 482581D440; Tue, 14 Apr 2020 21:44:53 +0200 (CEST) Received: from mail-lf1-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) by dpdk.org (Postfix) with ESMTP id 0A18D1D418 for ; Tue, 14 Apr 2020 21:44:48 +0200 (CEST) Received: by mail-lf1-f68.google.com with SMTP id u10so730133lfo.8 for ; Tue, 14 Apr 2020 12:44:48 -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=C27Z+XHQPlR/3v09fEYJyoGWFH01nD1Ie7MCpke9tAc=; b=bOtkpWIr5eO/2fJ+A8Frurg787LKCtZErIyn7v43NZmnCvG+tY6pkvIxhx33S/tte1 0HfJWFiDyDqMPqUqoD9WhKMKXK7ekgAy+4oLVHpoS5gLIpKCOH5/dUuuFm9493vI+LVL Hz+db6qyu9KacgfV5g2LYaFHeg7lw0cNNcqbkSSKf1FhVmmKx0sclAWlH0oJ+DRDh442 KRgXtDxZLnIbC+CmGbqsEy0Eix847ifbp5NBDe60rBjmUgzOiFkAJZ1h6WRPs1cuNF9w uPR1j2K8r93YifPEWPGkRyAsOYtdjsJinm1X1DrfvCsnYpeTcHBASRKMxwOTo8XAh10E BfVw== 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=C27Z+XHQPlR/3v09fEYJyoGWFH01nD1Ie7MCpke9tAc=; b=IQsJKHOEBwDfs5UdPEitkarNz9NUJsfoqdcZhcO5npsM2dqMi4otdL6NteBapApV6K qfcmJZHOnagrDhJJ08wa+nz1JO38+QLwiz2YOWPVt9RL+hNkiR9JTjhAFdrFf/4+wBGR QZQ/xfDcRvlaN2vNQEks0dLS4QbcTd5XRZ+n1M/PJUtxTw9Q+arC/xS405IlmdLLs52K AwJuldT7OYSfruJv1mG6Wz6A5WESUfZAX+34rA/xEzlXBYNObIQIWGXL1W33Vlpryd51 cEminmfXHKYRyydxlpv1uGCmE6p9txh9JaPVYb19O0TUL+gL8bdPVyxbOefAfnqKwk50 p7Pw== X-Gm-Message-State: AGi0PuZ0jBLOIqHeqKEvMl/ShTVMIGvmS4EsQUK4+/9j4BmqfqsytxUB B7yR0Ylfoj0weeFOZp/N2Fpcnn/gvTa+Qg== X-Google-Smtp-Source: APiQypIetvjDhPvfnHCenV/eW9RaQ/h+rWGuZ/0NNgkwahP6eKZ2eMGc8SQ8p0Z2lOSjqW6ZMF1g3Q== X-Received: by 2002:a05:6512:3189:: with SMTP id i9mr829702lfe.178.1586893487015; Tue, 14 Apr 2020 12:44:47 -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 h14sm11272426lfm.60.2020.04.14.12.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 12:44:46 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: "Dmitry Malloy (MESHCHANINOV)" , Narcisa Ana Maria Vasile , Fady Bader , Tal Shnaiderman , Dmitry Kozlyuk , Harini Ramakrishnan , Omar Cardona , Pallavi Kadam , Ranjit Menon Date: Tue, 14 Apr 2020 22:44:20 +0300 Message-Id: <20200414194426.1640704-6-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200414194426.1640704-1-dmitry.kozliuk@gmail.com> References: <20200410164342.1194634-1-dmitry.kozliuk@gmail.com> <20200414194426.1640704-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v3 05/10] 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_private.h | 45 ++++++++++++++++ lib/librte_eal/meson.build | 4 ++ lib/librte_eal/unix/eal.c | 47 ++++++++++++++++ lib/librte_eal/unix/meson.build | 6 +++ lib/librte_eal/windows/eal.c | 83 +++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 lib/librte_eal/unix/eal.c create mode 100644 lib/librte_eal/unix/meson.build diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index ddcfbe2e4..65d61ff13 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -443,4 +443,49 @@ rte_option_usage(void); uint64_t eal_get_baseaddr(void); +/** 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/meson.build b/lib/librte_eal/meson.build index 9d219a0e6..1f89efb88 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.c b/lib/librte_eal/unix/eal.c new file mode 100644 index 000000000..a337b59b1 --- /dev/null +++ b/lib/librte_eal/unix/eal.c @@ -0,0 +1,47 @@ +#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..13564838e --- /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.c', +) diff --git a/lib/librte_eal/windows/eal.c b/lib/librte_eal/windows/eal.c index 63461f51a..9dba895e7 100644 --- a/lib/librte_eal/windows/eal.c +++ b/lib/librte_eal/windows/eal.c @@ -224,6 +224,89 @@ rte_eal_init_alert(const char *msg) RTE_LOG(ERR, EAL, "%s\n", msg); } +int +eal_file_truncate(int fd, ssize_t size) +{ + HANDLE handle; + DWORD ret; + LONG low = (LONG)((size_t)size); + LONG high = (LONG)((size_t)size >> 32); + + handle = (HANDLE)_get_osfhandle(fd); + if (handle == INVALID_HANDLE_VALUE) { + rte_errno = EBADF; + return -1; + } + + ret = SetFilePointer(handle, low, &high, FILE_BEGIN); + if (ret == INVALID_SET_FILE_POINTER) { + RTE_LOG_WIN32_ERR("SetFilePointer()"); + rte_errno = EINVAL; + return -1; + } + + return 0; +} + +static int +lock_file(HANDLE handle, enum eal_flock_op op, enum eal_flock_mode mode) +{ + DWORD sys_flags = 0; + OVERLAPPED overlapped; + + if (op == EAL_FLOCK_EXCLUSIVE) + sys_flags |= LOCKFILE_EXCLUSIVE_LOCK; + if (mode == EAL_FLOCK_RETURN) + sys_flags |= LOCKFILE_FAIL_IMMEDIATELY; + + memset(&overlapped, 0, sizeof(overlapped)); + if (!LockFileEx(handle, sys_flags, 0, 0, 0, &overlapped)) { + if ((sys_flags & LOCKFILE_FAIL_IMMEDIATELY) && + (GetLastError() == ERROR_IO_PENDING)) { + rte_errno = EWOULDBLOCK; + } else { + RTE_LOG_WIN32_ERR("LockFileEx()"); + rte_errno = EINVAL; + } + return -1; + } + + return 0; +} + +static int +unlock_file(HANDLE handle) +{ + if (!UnlockFileEx(handle, 0, 0, 0, NULL)) { + RTE_LOG_WIN32_ERR("UnlockFileEx()"); + rte_errno = EINVAL; + return -1; + } + return 0; +} + +int +eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode) +{ + HANDLE handle = (HANDLE)_get_osfhandle(fd); + + if (handle == INVALID_HANDLE_VALUE) { + rte_errno = EBADF; + return -1; + } + + switch (op) { + case EAL_FLOCK_EXCLUSIVE: + case EAL_FLOCK_SHARED: + return lock_file(handle, op, mode); + case EAL_FLOCK_UNLOCK: + return unlock_file(handle); + default: + rte_errno = EINVAL; + return -1; + } +} + /* Launch threads, called at application init(). */ int rte_eal_init(int argc, char **argv) -- 2.25.1