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 E3C5EA0598; Fri, 10 Apr 2020 18:44:49 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 26F681D628; Fri, 10 Apr 2020 18:44:03 +0200 (CEST) Received: from mail-lf1-f66.google.com (mail-lf1-f66.google.com [209.85.167.66]) by dpdk.org (Postfix) with ESMTP id CFC651D613 for ; Fri, 10 Apr 2020 18:43:58 +0200 (CEST) Received: by mail-lf1-f66.google.com with SMTP id x23so1773234lfq.1 for ; Fri, 10 Apr 2020 09:43:58 -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=e5FBykAkTCpefwBaivctA0ZwNOIXcI6qpNeHbGcO22VZOUeVnSYIgYaC/GKJ/QUrJI rYH4XtzefH/cCJioImiouDuJe9orlc18nwyxScvkMFi3irRAXpGjYojS2KHNGGwgN5ba Q0UituTJ2RqgbB9sfkZNQUZNzHH7zyLCq6SBjsHLmhtxmIpfku1tVWDYCSBY9CO/q9ik rr5p2OgdSr70xaa6pHFkFUg+y2y1VQAOo+C/UIwOkVirAeP3OuSRFzRj5fdGWxXvH3l+ EYc8DeOJZhBspMi3wTTjSFLuGCUf4JfSsLL4bF5PDAto8TQwcYEylyo5sxjfuAhixJfB d58Q== 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=OJwix2Cmn7TSyh19Joe32Vo0p77NjU2vPOSoZFqC4TiENEmm7iEJXUsX0unRVQFZIX 9o0NBa0P0cJF2utxgPTUWRdPUKJyeRSrIpmnEW9Ih/bSfmO4GvjhPXcOK1rBGDJvVwE0 rwfBTjr7nflL8G4CrkC3mM+X6je0gdJHzEOgg2P8ms4bQG+5WqeFZpLcKaGW0MIxDV5/ Vwgx2NJDRvtE2cWPT0hv1MVrWSb6eeKrHo6sNf/YOIJP89isr0kLQWLAGcQVfPxme4GH v104zS/XutifVk1wt7HK1GYF4m5q5zfW3N/c8/NF2sxCY5M5rI4ePIAlOJrfeexK16OG IvOA== X-Gm-Message-State: AGi0Pua3UBbX9gtX1Kr3/xYc317sCgADhyMme+WkJR7gM0zPlCjY+RI+ sw3Bd0F9IDgA8NZj7Ley6i7eSLeM3XmEIg== X-Google-Smtp-Source: APiQypI8Ze1/9bwWoA9GfFnJo9dzRWzOAU7jFSiK9Tku8qanoMIIbKDoFfIZkyzT6YTjPlRXFAoIEg== X-Received: by 2002:a19:4014:: with SMTP id n20mr2997677lfa.6.1586537038002; Fri, 10 Apr 2020 09:43:58 -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 d21sm1343075ljc.49.2020.04.10.09.43.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2020 09:43:57 -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: Fri, 10 Apr 2020 19:43:37 +0300 Message-Id: <20200410164342.1194634-6-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200410164342.1194634-1-dmitry.kozliuk@gmail.com> References: <20200330041026.784624-1-dmitry.kozliuk@gmail.com> <20200410164342.1194634-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2 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