From patchwork Tue Mar 26 13:39:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 13604196 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BFBBBC6FD1F for ; Tue, 26 Mar 2024 13:40:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rp72T-0007qN-2B; Tue, 26 Mar 2024 09:40:30 -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 1rp72M-0007fG-Pg for qemu-devel@nongnu.org; Tue, 26 Mar 2024 09:40:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rp72K-0005Bf-Nm for qemu-devel@nongnu.org; Tue, 26 Mar 2024 09:40:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711460418; 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=7su9FVCHZgY5W4CseWaTDVux0tjQnk92YCDB/2U9JF8=; b=Hysc5W/1w9oaVfJdto3MzDFnHSlL4cly7f1fxLTC19x1rGxCoD65wWHRMis0mOLc/rFnce BqcBjevfBg5Pe37dT2OEEmT7897XVs71V8PfgC+OCXeomNKTN//hRGvE02xj06DHucuGHr Y7X7tGXouk01V6QoyKrcd44JmKo7cqU= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-306-f6KyqsNfNKK9lYpI3W0gmw-1; Tue, 26 Mar 2024 09:40:17 -0400 X-MC-Unique: f6KyqsNfNKK9lYpI3W0gmw-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-33ed2555b61so3025951f8f.3 for ; Tue, 26 Mar 2024 06:40:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711460415; x=1712065215; 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=7su9FVCHZgY5W4CseWaTDVux0tjQnk92YCDB/2U9JF8=; b=FAaQ2FpKGUzmBxNrH783LqlZv/72EaIXFTm1AoV3Re4p34OVQ3L3kaZvHVtV/4eVWc KNuqB4ugYf5NvwpRXw9x4LqypUK1MH5OQz8rIJr8LxW2hwZOXkc1HLRD3ailoOqeELRw AWAgrflTczIWkxgqnXULUYEcLOZWYYbiQwgCnnVxImKFNc6q5hfDfA7PUK/eahKi3qO+ Uy5C70sPsJkAe0BBsL4NYwNYZxYZtmtdoe57Ks4AmjsXxi6VBbkdH8BIa5Y0LvFRxCoL tbSocF8Dpp56EuneOLQgQyr89bwj0PlDaj6fDPzOR4Fc03BQSIkEqrXp8QX72i3NjUhT qItA== X-Gm-Message-State: AOJu0Yzg/5xFes76A007YKbFUGJCLMnXgMwyj5rIkrMlbOnzsFviMFMU KXIDVb1CfhCZqsn2vaHogIBiFkamg7UG0NBGYhqJwjpsr/K0Bh1MiaLjh5fN1NpUBe/lg8/+Dd8 HVmGFOI3EYn2lcUGjzXtOKagfET8DYx8Ckituw4if/ryKWIbyK5x1zQD3TY3ZfQ4qLwx7ik9f+G sAvpDtX+BR1vZGYcygc601RCAcyZbhcovib+yE X-Received: by 2002:a05:6000:151:b0:33e:c03e:62 with SMTP id r17-20020a056000015100b0033ec03e0062mr1222115wrx.40.1711460415515; Tue, 26 Mar 2024 06:40:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+vrtdSqfVW5LepjfmQo0yObYwg/nk6kHX4OepH3ROAphzNTgzs2wlRG59CBc8zYpMOEDBPg== X-Received: by 2002:a05:6000:151:b0:33e:c03e:62 with SMTP id r17-20020a056000015100b0033ec03e0062mr1222081wrx.40.1711460415232; Tue, 26 Mar 2024 06:40:15 -0700 (PDT) Received: from step1.redhat.com (host-87-12-25-33.business.telecomitalia.it. [87.12.25.33]) by smtp.gmail.com with ESMTPSA id bu14-20020a056000078e00b00341d2728e02sm3688526wrb.37.2024.03.26.06.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 06:40:14 -0700 (PDT) From: Stefano Garzarella To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , gmaglione@redhat.com, Eric Blake , Eduardo Habkost , Hanna Reitz , stefanha@redhat.com, Coiby Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Igor Mammedov , Jason Wang , Raphael Norwitz , slp@redhat.com, =?utf-8?q?Marc-And?= =?utf-8?q?r=C3=A9_Lureau?= , David Hildenbrand , Laurent Vivier , Markus Armbruster , Thomas Huth , Brad Smith , qemu-block@nongnu.org, Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Kevin Wolf , Stefano Garzarella Subject: [PATCH for-9.1 v2 07/11] libvhost-user: enable it on any POSIX system Date: Tue, 26 Mar 2024 14:39:32 +0100 Message-ID: <20240326133936.125332-8-sgarzare@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240326133936.125332-1-sgarzare@redhat.com> References: <20240326133936.125332-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain Received-SPF: pass client-ip=170.10.133.124; envelope-from=sgarzare@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.088, 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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 avaibale 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) Signed-off-by: Stefano Garzarella --- 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 c19d51501a..3197a2f62e 100644 --- a/meson.build +++ b/meson.build @@ -3194,7 +3194,7 @@ endif config_host_data.set('CONFIG_FDT', fdt.found()) vhost_user = not_found -if host_os == 'linux' and have_vhost_user +if have_vhost_user libvhost_user = subproject('libvhost-user') vhost_user = libvhost_user.get_variable('vhost_user_dep') endif diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvhost-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" /* 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/libvhost-user/libvhost-user.c index 1c361ffd51..03edb4bf64 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 /* 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" #if defined(__linux__) +#include #include -#include #include #include #include @@ -52,6 +50,62 @@ #endif +#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)) == sizeof(value)) ? 0 : -1; +} + +int eventfd_read(int fd, eventfd_t *value) +{ + return (read(fd, value, sizeof(*value)) == 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" #include "libvhost-user.h"