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 0FC7EA0562; Mon, 30 Mar 2020 06:11:26 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6D7951C0AA; Mon, 30 Mar 2020 06:10:48 +0200 (CEST) Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by dpdk.org (Postfix) with ESMTP id 715F71C02D for ; Mon, 30 Mar 2020 06:10:43 +0200 (CEST) Received: by mail-lf1-f67.google.com with SMTP id t16so12123014lfl.2 for ; Sun, 29 Mar 2020 21:10:43 -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=kRuOpY95+Geg1/zp607Zcl+mSxcFIH4YiwhygeqAqo8=; b=chB8OYea8/gvLHlJQ1X3shxw7o0EWGjhcMFns0CHGPPCDQtq8PUIvODyP57HGPYpN1 jSio8ajYQe3ILiCr0NsmcJa+IJ7L/aAYBjigxhaJ2Xb97HFvJTSZR56Ix2EpPejtTz9c LoA95NdyWouUrRexPqVNy6gHl+YKi7YQ7+ADvEXYBYjNxJCVIpJZ9tbZ+vC9wQ1juLIO 6s2nBymBzs9a3RXIw1zt9+2C1sgQ0+spflmcXb4meYD8Wy0fk90lWOLYLqtSfrRyhHXE STSsBejl+dZSAjdOu8iadq+8prh27AAmlSpW6xIJIfHsfdqGjBvHV85G7rnj8oJrrKWe Eezw== 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=kRuOpY95+Geg1/zp607Zcl+mSxcFIH4YiwhygeqAqo8=; b=C7ahM5Kmoqe0+/6vOKh/g6PwMKWwbq1C0weHxTvm5Fj6Qc/MBcENoa6O61UU30dPm7 AYcTjHIs7gVB8V4KxOXNqQfbOX/PXrVMbx7Ix3CEdOAZ00CENitmMvGr5u3PKPdhrJAD AUuSN2VOFfp7Pk7/3Gcy28XdYGSRXQaxoB0qznIHmkaqX8Di2BDXivB3IZRp4G5cgDYB 8hKMSUcdczl6BFcM8T2Vg6e/IVMbPBIF1aEeYyWnInqoz0zG635MO8fizkcfGQIklaei Ju8qwumwMhnD9TFt9xNNGtyQzUMK4fiumFGj1yEKJLQLybKl3br7qaCDHAef5DbMzV3H /HXg== X-Gm-Message-State: AGi0PuYImpu+v08RWGrwk3ikoyk77Z2XdVr5YPO14Wgdi6Kw3/Vv7BjK FvwcrnTRRHtAK1qMGjj2TPs+tSHYOqahZg== X-Google-Smtp-Source: APiQypJi3zakf1BYhs2MjtLpJXVQtJMe+GMpg5niF4p+gYkLYoc8s+x8KTBruotxFx832g4auvZJnQ== X-Received: by 2002:a19:c515:: with SMTP id w21mr6717294lfe.72.1585541442654; Sun, 29 Mar 2020 21:10:42 -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 c22sm6210036lja.86.2020.03.29.21.10.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2020 21:10:42 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: "Dmitry Malloy (MESHCHANINOV)" , Dmitry Kozlyuk , Bruce Richardson , Harini Ramakrishnan , Omar Cardona , Pallavi Kadam , Ranjit Menon Date: Mon, 30 Mar 2020 07:10:22 +0300 Message-Id: <20200330041026.784624-6-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330041026.784624-1-dmitry.kozliuk@gmail.com> References: <20200330041026.784624-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [RFC PATCH 5/9] 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 wrapeprs in order to support both POSIX 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. Signed-off-by: Dmitry Kozlyuk WIP2 --- lib/librte_eal/common/eal_private.h | 45 ++++++++++++++++ lib/librte_eal/freebsd/eal/eal.c | 40 ++++++++++++++ lib/librte_eal/linux/eal/eal.c | 40 ++++++++++++++ lib/librte_eal/windows/eal/eal.c | 83 +++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+) diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index ddcfbe2e4..0130571e8 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 rte_flock_op { + RTE_FLOCK_SHARED, /**< Acquire a shared lock. */ + RTE_FLOCK_EXCLUSIVE, /**< Acquire an exclusive lock. */ + RTE_FLOCK_UNLOCK /**< Release a previously taken lock. */ +}; + +/** Behavior on file locking conflict. */ +enum rte_flock_mode { + RTE_FLOCK_WAIT, /**< Wait until the file gets unlocked to lock it. */ + RTE_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 rte_flock_op op, enum rte_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/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c index 6ae37e7e6..4bbcc7ab7 100644 --- a/lib/librte_eal/freebsd/eal/eal.c +++ b/lib/librte_eal/freebsd/eal/eal.c @@ -697,6 +697,46 @@ static void rte_eal_init_alert(const char *msg) RTE_LOG(ERR, EAL, "%s\n", msg); } +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 rte_flock_op op, enum rte_flock_mode mode) +{ + int sys_flags = 0; + int ret; + + if (mode == RTE_FLOCK_RETURN) + sys_flags |= LOCK_NB; + + switch (op) { + case RTE_FLOCK_EXCLUSIVE: + sys_flags |= LOCK_EX; + break; + case RTE_FLOCK_SHARED: + sys_flags |= LOCK_SH; + break; + case RTE_FLOCK_UNLOCK: + sys_flags |= LOCK_UN; + break; + } + + ret = flock(fd, sys_flags); + if (ret) + rte_errno = errno; + + return ret; +} + /* Launch threads, called at application init(). */ int rte_eal_init(int argc, char **argv) diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c index 9530ee55f..d75c162c1 100644 --- a/lib/librte_eal/linux/eal/eal.c +++ b/lib/librte_eal/linux/eal/eal.c @@ -956,6 +956,46 @@ is_iommu_enabled(void) return n > 2; } +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 rte_flock_op op, enum rte_flock_mode mode) +{ + int sys_flags = 0; + int ret; + + if (mode == RTE_FLOCK_RETURN) + sys_flags |= LOCK_NB; + + switch (op) { + case RTE_FLOCK_EXCLUSIVE: + sys_flags |= LOCK_EX; + break; + case RTE_FLOCK_SHARED: + sys_flags |= LOCK_SH; + break; + case RTE_FLOCK_UNLOCK: + sys_flags |= LOCK_UN; + break; + } + + ret = flock(fd, sys_flags); + if (ret) + rte_errno = errno; + + return ret; +} + /* Launch threads, called at application init(). */ int rte_eal_init(int argc, char **argv) diff --git a/lib/librte_eal/windows/eal/eal.c b/lib/librte_eal/windows/eal/eal.c index a84b6147a..4932185ec 100644 --- a/lib/librte_eal/windows/eal/eal.c +++ b/lib/librte_eal/windows/eal/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 rte_flock_op op, enum rte_flock_mode mode) +{ + DWORD sys_flags = 0; + OVERLAPPED overlapped; + + if (op == RTE_FLOCK_EXCLUSIVE) + sys_flags |= LOCKFILE_EXCLUSIVE_LOCK; + if (mode == RTE_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 rte_flock_op op, enum rte_flock_mode mode) +{ + HANDLE handle = (HANDLE)_get_osfhandle(fd); + + if (handle == INVALID_HANDLE_VALUE) { + rte_errno = EBADF; + return -1; + } + + switch (op) { + case RTE_FLOCK_EXCLUSIVE: + case RTE_FLOCK_SHARED: + return lock_file(handle, op, mode); + case RTE_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