From patchwork Tue Dec 17 20:38:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 1211700 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=hemlock.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47cqjv6Sv2z9sR8 for ; Wed, 18 Dec 2019 07:38:59 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 77F6587EC5; Tue, 17 Dec 2019 20:38:57 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CsswIr5V4zhq; Tue, 17 Dec 2019 20:38:56 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 9EA3687D94; Tue, 17 Dec 2019 20:38:56 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7E4F1C1AE8; Tue, 17 Dec 2019 20:38:56 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 35875C077D for ; Tue, 17 Dec 2019 20:38:55 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 23FDD869EC for ; Tue, 17 Dec 2019 20:38:55 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qNMSSwGfJUXk for ; Tue, 17 Dec 2019 20:38:54 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) by whitealder.osuosl.org (Postfix) with ESMTPS id BAB63869B6 for ; Tue, 17 Dec 2019 20:38:53 +0000 (UTC) X-Originating-IP: 66.170.99.95 Received: from localhost.localdomain (unknown [66.170.99.95]) (Authenticated sender: blp@ovn.org) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id D0F63FF802; Tue, 17 Dec 2019 20:38:49 +0000 (UTC) From: Ben Pfaff To: dev@openvswitch.org Date: Tue, 17 Dec 2019 12:38:37 -0800 Message-Id: <20191217203836.2208039-1-blp@ovn.org> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Cc: Ben Pfaff Subject: [ovs-dev] [PATCH] socket-util: Introduce emulation and wrapper for recvmmsg(). X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Not every system will have recvmmsg(), so introduce compatibility code that will allow it to be used blindly from the rest of the tree. This assumes that recvmmsg() and sendmmsg() are either both present or both absent in system libraries and headers. CC: Yi Yang Signed-off-by: Ben Pfaff --- I haven't actually tested this! include/sparse/sys/socket.h | 7 ++++- lib/socket-util.c | 56 +++++++++++++++++++++++++++++++++++++ lib/socket-util.h | 24 +++++++++------- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/include/sparse/sys/socket.h b/include/sparse/sys/socket.h index 4178f57e2bda..6ff245ae939b 100644 --- a/include/sparse/sys/socket.h +++ b/include/sparse/sys/socket.h @@ -27,6 +27,7 @@ typedef unsigned short int sa_family_t; typedef __socklen_t socklen_t; +struct timespec; struct sockaddr { sa_family_t sa_family; @@ -126,7 +127,8 @@ enum { MSG_PEEK, MSG_TRUNC, MSG_WAITALL, - MSG_DONTWAIT + MSG_DONTWAIT, + MSG_WAITFORONE }; enum { @@ -171,4 +173,7 @@ int sockatmark(int); int socket(int, int, int); int socketpair(int, int, int, int[2]); +int sendmmsg(int, struct mmsghdr *, unsigned int, int); +int recvmmsg(int, struct mmsghdr *, unsigned int, int, struct timespec *); + #endif /* for sparse */ diff --git a/lib/socket-util.c b/lib/socket-util.c index 6b7378de934b..f6f6f3b0a33f 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -1283,3 +1283,59 @@ wrap_sendmmsg(int fd, struct mmsghdr *msgs, unsigned int n, unsigned int flags) } #endif #endif + +#ifndef _WIN32 /* Avoid using recvmsg on Windows entirely. */ +static int +emulate_recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, + int flags, struct timespec *timeout OVS_UNUSED) +{ + ovs_assert(!timeout); /* XXX not emulated */ + + bool waitforone = flags & MSG_WAITFORONE; + flags &= ~MSG_WAITFORONE; + + for (unsigned int i = 0; i < n; i++) { + ssize_t retval = recvmsg(fd, &msgs[i].msg_hdr, flags); + if (retval < 0) { + return i ? i : retval; + } + msgs[i].msg_len = retval; + + if (waitforone) { + flags |= MSG_DONTWAIT; + } + } + return n; +} + +#ifndef HAVE_SENDMMSG +int +recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, + int flags, struct timespec *timeout) +{ + return emulate_recvmmsg(fd, msgs, n, flags, timeout); +} +#else +/* recvmmsg was redefined in lib/socket-util.c, should undef recvmmsg here + * to avoid recursion */ +#undef recvmmsg +int +wrap_recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, + int flags, struct timespec *timeout) +{ + ovs_assert(!timeout); /* XXX not emulated */ + + static bool recvmmsg_broken = false; + if (!recvmmsg_broken) { + int save_errno = errno; + int retval = recvmmsg(fd, msgs, n, flags, timeout); + if (retval >= 0 || errno != ENOSYS) { + return retval; + } + recvmmsg_broken = true; + errno = save_errno; + } + return emulate_recvmmsg(fd, msgs, n, flags, timeout); +} +#endif +#endif diff --git a/lib/socket-util.h b/lib/socket-util.h index a65433d90738..71bd68926aaa 100644 --- a/lib/socket-util.h +++ b/lib/socket-util.h @@ -104,19 +104,20 @@ int make_unix_socket(int style, bool nonblock, const char *bind_path, const char *connect_path); int get_unix_name_len(const struct sockaddr_un *sun, socklen_t sun_len); -/* Universal sendmmsg support. +/* Universal sendmmsg and recvmmsg support. * - * Some platforms, such as new enough Linux and FreeBSD, support sendmmsg, but - * other platforms (or older ones) do not. We add the following infrastructure - * to allow all code to use sendmmsg, regardless of platform support: + * Some platforms, such as new enough Linux and FreeBSD, support sendmmsg and + * recvmmsg, but other platforms (or older ones) do not. We add the following + * infrastructure to allow all code to use sendmmsg and recvmmsg, regardless of + * platform support: * - * - For platforms that lack sendmmsg entirely, we emulate it. + * - For platforms that lack these functions entirely, we emulate them. * - * - Some platforms have sendmmsg() in the C library but not in the kernel. - * For example, this is true if a Linux system has a newer glibc with an - * old kernel. To compensate, even if sendmmsg() appears to be available, - * we still wrap it with a handler that uses our emulation if sendmmsg() - * returns ENOSYS. + * - Some platforms have sendmmsg() and recvmmsg() in the C library but not in + * the kernel. For example, this is true if a Linux system has a newer glibc + * with an old kernel. To compensate, even if these functions appear to be + * available, we still wrap them with handlers that uses our emulation if the + * underlying function returns ENOSYS. */ #ifndef HAVE_STRUCT_MMSGHDR_MSG_LEN struct mmsghdr { @@ -126,9 +127,12 @@ struct mmsghdr { #endif #ifndef HAVE_SENDMMSG int sendmmsg(int, struct mmsghdr *, unsigned int, unsigned int); +int recvmmsg(int, struct mmsghdr *, unsigned int, int, struct timespec *); #else #define sendmmsg wrap_sendmmsg int wrap_sendmmsg(int, struct mmsghdr *, unsigned int, unsigned int); +#define recvmmsg wrap_recvmmsg +int wrap_recvmmsg(int, struct mmsghdr *, unsigned int, int, struct timespec *); #endif /* Helpers for calling ioctl() on an AF_INET socket. */