From nobody Wed Sep 18 23:02:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1716892657; cv=none; d=zohomail.com; s=zohoarc; b=aHkO0LGRfuOH6TXFsNRpr+BXH5suWgboX/DVVa7pRPvPJV/2Bychd+HZF7Vx8P3aCHv2aIQB5PTS+QHdOUUAwtC0Ytxx2zaKIve2vnSYzYtoD7WF1fxiUI+M7XsmHxwEp1u5mSv12ctJo/mavhg5ktkBoRwyeBT9LS2HAnuS0Es= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716892657; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ip8mp5/iMw0SwNnO89V12jrYLC8O/3xN2u7Jx3Io+wc=; b=bo2W6ZKHzrkE5VTjDYr3UgsdbAM8wjpoLEQ7tpscC2l8DG6LSZD7I+arwC8aZE0FwYSzGRUrRDHft0/42PyK18T1MIuaWzh8DN/36R0ODh+mFxgDB2+0zvboDru6vQhet3NqYwSX7cTjSQfxPp8SDvklmgjcyfFq7Dd6JuGKGyg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716892657519516.9994523954472; Tue, 28 May 2024 03:37:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBuC1-0004UL-9j; Tue, 28 May 2024 06:36:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBuC0-0004U7-92 for qemu-devel@nongnu.org; Tue, 28 May 2024 06:36:32 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBuBy-0008I6-Kh for qemu-devel@nongnu.org; Tue, 28 May 2024 06:36:31 -0400 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-365-K861vp1LMdafezdcFFtEdg-1; Tue, 28 May 2024 06:36:28 -0400 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42024ca94d0so3284445e9.1 for ; Tue, 28 May 2024 03:36:28 -0700 (PDT) Received: from step1.redhat.com (host-79-53-30-109.retail.telecomitalia.it. [79.53.30.109]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-421089b0410sm136686465e9.29.2024.05.28.03.36.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 03:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716892590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ip8mp5/iMw0SwNnO89V12jrYLC8O/3xN2u7Jx3Io+wc=; b=gpKiO3hCUiWFRYTQL/qtQLXmgE1q2Qb8XkGMO8LdYNKe/ngJb/VLNVg3zdds7VJn06XfeS GaqUpyXd+v5oh9iSLbvk6QUilwwtYT0MpMx0NCpbLzitGpeOiRGjVp1X7o72B0RCQU/MUJ 8Dtmmq8f4CCuk11I4yjtKDJpIcBtOzE= X-MC-Unique: K861vp1LMdafezdcFFtEdg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716892585; x=1717497385; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ip8mp5/iMw0SwNnO89V12jrYLC8O/3xN2u7Jx3Io+wc=; b=vIZs9Vp3A33EaUC+N5QE0IRAunXYdHykoeyeELeVNGMpl27KD4TEtg5aff6NIZU17Z Ym/GozO5EFmDaE33eZtyjUN+VRvBXw69C8JELPwjojrCjheFBfebqNg2i57mmYyM8OW1 PSU184H3eulR4d2rQZ+1bW+xF6pod6MDDYn9YH36nHmgx9qQKDZudkfRJqx0XKX+S1TD 8Dzkc9eKWSayVfuAnyqn5cQsQawKmfAu1MGMv7hxcgSTZEi44T1R0VOSZuHOAGdTndkc PMDVc8HkP1Zy1CruAe/xQ2Y8EfsNbeN+PCBvT+VGtr6MUdvmEqcER5HMD340vv3FvqnW o7QA== X-Gm-Message-State: AOJu0Ywvmw3dCdNCJMB53c+4uqs0I0qp8WPK/EVngAfoTUPIaG9mwYhI XJtFQIwtlgQXsbkU32fwNHhIEaS6Wijo7f1Mz5+7aJOQnfCL/I/IF1F11uAm1BBeN/gyiIx9CI3 QHpK0w/yWx5WfK2gQid/HP0fleNJX7SGhHXIyMf9JeFL1CN6/naryXlkXvZZ6a37c+eBLk/+R0B veF2L+/enbwdtsCKIuiNZ5ZD/OjR2ZpUDLe5ZG X-Received: by 2002:a05:600c:4f52:b0:41f:fca0:8c09 with SMTP id 5b1f17b1804b1-42108a15120mr84210615e9.40.1716892585635; Tue, 28 May 2024 03:36:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLihbkYiuY/sHdEh1Uq35i8VXI6O3hhOIRMCr4FsbNaMBIqWKLGOZ+IoWT6DLR8suQwTncrw== X-Received: by 2002:a05:600c:4f52:b0:41f:fca0:8c09 with SMTP id 5b1f17b1804b1-42108a15120mr84210275e9.40.1716892585267; Tue, 28 May 2024 03:36:25 -0700 (PDT) From: Stefano Garzarella To: qemu-devel@nongnu.org Cc: slp@redhat.com, David Hildenbrand , Paolo Bonzini , Eduardo Habkost , Igor Mammedov , Brad Smith , gmaglione@redhat.com, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Laurent Vivier , Kevin Wolf , Jason Wang , "Michael S. Tsirkin" , Eric Blake , Markus Armbruster , Coiby Xu , qemu-block@nongnu.org, Hanna Reitz , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Raphael Norwitz , Thomas Huth , stefanha@redhat.com, =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Gerd Hoffmann , Stefano Garzarella Subject: [PATCH v6 08/12] libvhost-user: enable it on any POSIX system Date: Tue, 28 May 2024 12:35:39 +0200 Message-ID: <20240528103543.145412-9-sgarzare@redhat.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240528103543.145412-1-sgarzare@redhat.com> References: <20240528103543.145412-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=sgarzare@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.034, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1716892658233100001 The vhost-user protocol is not really Linux-specific so let's enable libvhost-user for any POSIX system. Compiling it on macOS and FreeBSD some problems came up: - avoid to include linux/vhost.h which is available only on Linux (vhost_types.h contains many of the things we need) - macOS doesn't provide sys/endian.h, so let's define them (note: libvhost-user doesn't include QEMU's headers, so we can't use use "qemu/bswap.h") - define eventfd_[write|read] as write/read wrapper when system doesn't provide those (e.g. macOS) - copy SEAL defines from include/qemu/memfd.h to make the code works on FreeBSD where MFD_ALLOW_SEALING is defined - define MAP_NORESERVE if it's not defined (e.g. on FreeBSD) Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 Acked-by: Stefan Hajnoczi Signed-off-by: Stefano Garzarella --- v5: - fixed typos in the commit description [Phil] --- meson.build | 2 +- subprojects/libvhost-user/libvhost-user.h | 2 +- subprojects/libvhost-user/libvhost-user.c | 60 +++++++++++++++++++++-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/meson.build b/meson.build index a72500be77..48e476b237 100644 --- a/meson.build +++ b/meson.build @@ -3162,7 +3162,7 @@ if have_system and vfio_user_server_allowed endif =20 vhost_user =3D not_found -if host_os =3D=3D 'linux' and have_vhost_user +if have_vhost_user libvhost_user =3D subproject('libvhost-user') vhost_user =3D libvhost_user.get_variable('vhost_user_dep') endif diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvho= st-user/libvhost-user.h index deb40e77b3..e13e1d3931 100644 --- a/subprojects/libvhost-user/libvhost-user.h +++ b/subprojects/libvhost-user/libvhost-user.h @@ -18,9 +18,9 @@ #include #include #include -#include #include #include "standard-headers/linux/virtio_ring.h" +#include "standard-headers/linux/vhost_types.h" =20 /* Based on qemu/hw/virtio/vhost-user.c */ #define VHOST_USER_F_PROTOCOL_FEATURES 30 diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 2c20cdc16e..57e58d4adb 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -28,9 +28,7 @@ #include #include #include -#include #include -#include =20 /* Necessary to provide VIRTIO_F_VERSION_1 on system * with older linux headers. Must appear before @@ -39,8 +37,8 @@ #include "standard-headers/linux/virtio_config.h" =20 #if defined(__linux__) +#include #include -#include #include #include #include @@ -52,6 +50,62 @@ =20 #endif =20 +#if defined(__APPLE__) && (__MACH__) +#include +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#endif + +#ifdef CONFIG_EVENTFD +#include +#else +#define eventfd_t uint64_t + +int eventfd_write(int fd, eventfd_t value) +{ + return (write(fd, &value, sizeof(value)) =3D=3D sizeof(value)) ? 0 : -= 1; +} + +int eventfd_read(int fd, eventfd_t *value) +{ + return (read(fd, value, sizeof(*value)) =3D=3D sizeof(*value)) ? 0 : -= 1; +} +#endif + +#ifdef MFD_ALLOW_SEALING +#include + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif + +#ifndef F_ADD_SEALS +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) + +#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ +#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ +#define F_SEAL_GROW 0x0004 /* prevent file from growing */ +#define F_SEAL_WRITE 0x0008 /* prevent writes */ +#endif +#endif + +#ifndef MAP_NORESERVE +#define MAP_NORESERVE 0 +#endif + #include "include/atomic.h" =20 #include "libvhost-user.h" --=20 2.45.1