From patchwork Mon May 16 10:39:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 12850684 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 86C86C433F5 for ; Mon, 16 May 2022 12:07:14 +0000 (UTC) Received: from localhost ([::1]:49910 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqZVJ-0007za-Ks for qemu-devel@archiver.kernel.org; Mon, 16 May 2022 08:07:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqY8t-000272-KN for qemu-devel@nongnu.org; Mon, 16 May 2022 06:40:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45367) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqY8r-0005gf-Ft for qemu-devel@nongnu.org; Mon, 16 May 2022 06:39:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652697597; 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: in-reply-to:in-reply-to:references:references; bh=vo/2VeT20CuW+c/BpnizV6V1YuzcJs592waA9ARcD/0=; b=ag5zKxThDHmL/61DJiDiHs7EZJ+iwYTMc2OyNgP8V1anIuil7y978hdHB+7YtNtsNtVy3Q eXmnwamC9sjMDIlPJUAfSHFXVdqrnk4anYRKW2PBQEdfU+Ud4aBIxv62Sc2EkrWoPsUlc4 I6/PRsKyJyHfaKPxJhYs9OQFcNFgA8s= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-390-GhkjBYaYPeypouDU86PiVA-1; Mon, 16 May 2022 06:39:55 -0400 X-MC-Unique: GhkjBYaYPeypouDU86PiVA-1 Received: by mail-wr1-f70.google.com with SMTP id bs25-20020a056000071900b0020d03bbc3bbso451586wrb.19 for ; Mon, 16 May 2022 03:39:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vo/2VeT20CuW+c/BpnizV6V1YuzcJs592waA9ARcD/0=; b=rcpQ4CR48V9JNIV4ObWkgxLDfwINkKuylmI7wlFIaJUL2LhwNV8G/R7DI27o1mlJzb OXt2+Ctfrkv1/GLkeLeudI99UtduVQIOEwInT2fXn3wT10qZUC4WO03LeEVeKdwYv6zL 27aaM2ltpXbZAAwDoAdI4mJFgqZfcU1BPGJPEeCdlwGbKPNWSLPf4ftze8sAmswLkikG 0FOSjN+hYZ4Tt+6A07Ho86m1N6r7TCWu34ZO6HuZN2fN14OLj7Uz7AWKuahSDsTVopLj 865DXxta9rDmHuDgJWUcXdAny02T6/nBeuD4qt87avqNxzSHB5q50jxFPrh+nW2av6F0 xd+w== X-Gm-Message-State: AOAM532XrDkBh8hQSGjZtLW48dBPWl2lqrGdHhp0pexKWXRhvWWrg9Vt bpX9/HNs1BbvJY8xa+vgUlMfD2u1wYE80jIIq5oxB1R/Ldjmlnt/AoSiwasw5bT5a5OOqaw/Z/n QnUaFnN/Sksx+Ss1Ql9EBDbeu73wt8BsAKJu+UOWIlerKCSQs6j3OfvvsbF+x X-Received: by 2002:a05:600c:4346:b0:394:1a53:c8ba with SMTP id r6-20020a05600c434600b003941a53c8bamr26494130wme.173.1652697593279; Mon, 16 May 2022 03:39:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/sW6wUFTkyc4F+DSJl9Es48uSsQxuwgMyiAbGVV4CNpa5Zwt4Xc0Ro6bnTy4cJKD9/wpv9A== X-Received: by 2002:a05:600c:4346:b0:394:1a53:c8ba with SMTP id r6-20020a05600c434600b003941a53c8bamr26494105wme.173.1652697593009; Mon, 16 May 2022 03:39:53 -0700 (PDT) Received: from redhat.com ([2.55.141.66]) by smtp.gmail.com with ESMTPSA id t6-20020a7bc3c6000000b003942a244f47sm13006818wmj.32.2022.05.16.03.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 03:39:52 -0700 (PDT) Date: Mon, 16 May 2022 06:39:49 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Laurent Vivier , Jonah Palmer , Markus Armbruster , Michael Roth , Eric Blake , Thomas Huth , Paolo Bonzini Subject: [PULL 72/91] qmp: add QMP command x-query-virtio Message-ID: <20220516095448.507876-73-mst@redhat.com> References: <20220516095448.507876-1-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220516095448.507876-1-mst@redhat.com> X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass client-ip=170.10.133.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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" From: Laurent Vivier This new command lists all the instances of VirtIODevices with their canonical QOM path and name. [Jonah: @virtio_list duplicates information that already exists in the QOM composition tree. However, extracting necessary information from this tree seems to be a bit convoluted. Instead, we still create our own list of realized virtio devices but use @qmp_qom_get with the device's canonical QOM path to confirm that the device exists and is realized. If the device exists but is actually not realized, then we remove it from our list (for synchronicity to the QOM composition tree). Also, the QMP command @x-query-virtio is redundant as @qom-list and @qom-get are sufficient to search '/machine/' for realized virtio devices. However, @x-query-virtio is much more convenient in listing realized virtio devices.] Signed-off-by: Jonah Palmer Message-Id: <1648819405-25696-4-git-send-email-jonah.palmer@oracle.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- qapi/qapi-schema.json | 1 + qapi/virtio.json | 68 ++++++++++++++++++++++++++++++++++++++ include/hw/virtio/virtio.h | 1 + hw/virtio/virtio-stub.c | 14 ++++++++ hw/virtio/virtio.c | 44 ++++++++++++++++++++++++ tests/qtest/qmp-cmd-test.c | 1 + hw/virtio/meson.build | 2 ++ qapi/meson.build | 1 + 8 files changed, 132 insertions(+) create mode 100644 qapi/virtio.json create mode 100644 hw/virtio/virtio-stub.c diff --git a/qapi/qapi-schema.json b/qapi/qapi-schema.json index 4912b9744e..1512adaf92 100644 --- a/qapi/qapi-schema.json +++ b/qapi/qapi-schema.json @@ -93,3 +93,4 @@ { 'include': 'audio.json' } { 'include': 'acpi.json' } { 'include': 'pci.json' } +{ 'include': 'virtio.json' } diff --git a/qapi/virtio.json b/qapi/virtio.json new file mode 100644 index 0000000000..aee0e40daa --- /dev/null +++ b/qapi/virtio.json @@ -0,0 +1,68 @@ +# -*- Mode: Python -*- +# vim: filetype=python +# + +## +# = Virtio devices +## + +## +# @VirtioInfo: +# +# Basic information about a given VirtIODevice +# +# @path: The VirtIODevice's canonical QOM path +# +# @name: Name of the VirtIODevice +# +# Since: 7.0 +# +## +{ 'struct': 'VirtioInfo', + 'data': { 'path': 'str', + 'name': 'str' } } + +## +# @x-query-virtio: +# +# Returns a list of all realized VirtIODevices +# +# Features: +# @unstable: This command is meant for debugging. +# +# Returns: List of gathered VirtIODevices +# +# Since: 7.0 +# +# Example: +# +# -> { "execute": "x-query-virtio" } +# <- { "return": [ +# { +# "path": "/machine/peripheral-anon/device[4]/virtio-backend", +# "name": "virtio-input" +# }, +# { +# "path": "/machine/peripheral/crypto0/virtio-backend", +# "name": "virtio-crypto" +# }, +# { +# "path": "/machine/peripheral-anon/device[2]/virtio-backend", +# "name": "virtio-scsi" +# }, +# { +# "path": "/machine/peripheral-anon/device[1]/virtio-backend", +# "name": "virtio-net" +# }, +# { +# "path": "/machine/peripheral-anon/device[0]/virtio-backend", +# "name": "virtio-serial" +# } +# ] +# } +# +## + +{ 'command': 'x-query-virtio', + 'returns': [ 'VirtioInfo' ], + 'features': [ 'unstable' ] } diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index db1c0ddf6b..375eb5671b 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -110,6 +110,7 @@ struct VirtIODevice bool use_guest_notifier_mask; AddressSpace *dma_as; QLIST_HEAD(, VirtQueue) *vector_queues; + QTAILQ_ENTRY(VirtIODevice) next; }; struct VirtioDeviceClass { diff --git a/hw/virtio/virtio-stub.c b/hw/virtio/virtio-stub.c new file mode 100644 index 0000000000..05a81edc92 --- /dev/null +++ b/hw/virtio/virtio-stub.c @@ -0,0 +1,14 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-virtio.h" + +static void *qmp_virtio_unsupported(Error **errp) +{ + error_setg(errp, "Virtio is disabled"); + return NULL; +} + +VirtioInfoList *qmp_x_query_virtio(Error **errp) +{ + return qmp_virtio_unsupported(errp); +} diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 5d607aeaa0..bdfa82e9c0 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -13,12 +13,18 @@ #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qapi-commands-virtio.h" +#include "qapi/qapi-commands-qom.h" +#include "qapi/qapi-visit-virtio.h" +#include "qapi/qmp/qjson.h" #include "cpu.h" #include "trace.h" #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/main-loop.h" #include "qemu/module.h" +#include "qom/object_interfaces.h" #include "hw/virtio/virtio.h" #include "migration/qemu-file-types.h" #include "qemu/atomic.h" @@ -29,6 +35,9 @@ #include "sysemu/runstate.h" #include "standard-headers/linux/virtio_ids.h" +/* QAPI list of realized VirtIODevices */ +static QTAILQ_HEAD(, VirtIODevice) virtio_list; + /* * The alignment to use between consumer and producer parts of vring. * x86 pagesize again. This is the default, used by transports like PCI @@ -3698,6 +3707,7 @@ static void virtio_device_realize(DeviceState *dev, Error **errp) vdev->listener.commit = virtio_memory_listener_commit; vdev->listener.name = "virtio"; memory_listener_register(&vdev->listener, vdev->dma_as); + QTAILQ_INSERT_TAIL(&virtio_list, vdev, next); } static void virtio_device_unrealize(DeviceState *dev) @@ -3712,6 +3722,7 @@ static void virtio_device_unrealize(DeviceState *dev) vdc->unrealize(dev); } + QTAILQ_REMOVE(&virtio_list, vdev, next); g_free(vdev->bus_name); vdev->bus_name = NULL; } @@ -3885,6 +3896,8 @@ static void virtio_device_class_init(ObjectClass *klass, void *data) vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl; vdc->legacy_features |= VIRTIO_LEGACY_FEATURES; + + QTAILQ_INIT(&virtio_list); } bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev) @@ -3895,6 +3908,37 @@ bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev) return virtio_bus_ioeventfd_enabled(vbus); } +VirtioInfoList *qmp_x_query_virtio(Error **errp) +{ + VirtioInfoList *list = NULL; + VirtioInfoList *node; + VirtIODevice *vdev; + + QTAILQ_FOREACH(vdev, &virtio_list, next) { + DeviceState *dev = DEVICE(vdev); + Error *err = NULL; + QObject *obj = qmp_qom_get(dev->canonical_path, "realized", &err); + + if (err == NULL) { + GString *is_realized = qobject_to_json_pretty(obj, true); + /* virtio device is NOT realized, remove it from list */ + if (!strncmp(is_realized->str, "false", 4)) { + QTAILQ_REMOVE(&virtio_list, vdev, next); + } else { + node = g_new0(VirtioInfoList, 1); + node->value = g_new(VirtioInfo, 1); + node->value->path = g_strdup(dev->canonical_path); + node->value->name = g_strdup(vdev->name); + QAPI_LIST_PREPEND(list, node->value); + } + g_string_free(is_realized, true); + } + qobject_unref(obj); + } + + return list; +} + static const TypeInfo virtio_device_info = { .name = TYPE_VIRTIO_DEVICE, .parent = TYPE_DEVICE, diff --git a/tests/qtest/qmp-cmd-test.c b/tests/qtest/qmp-cmd-test.c index 056b40e67f..155cd5a75b 100644 --- a/tests/qtest/qmp-cmd-test.c +++ b/tests/qtest/qmp-cmd-test.c @@ -103,6 +103,7 @@ static bool query_is_ignored(const char *cmd) "query-gic-capabilities", /* arm */ /* Success depends on target-specific build configuration: */ "query-pci", /* CONFIG_PCI */ + "x-query-virtio", /* CONFIG_VIRTIO */ /* Success depends on launching SEV guest */ "query-sev-launch-measure", /* Success depends on Host or Hypervisor SEV support */ diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 7e8877fd64..e16f1b22d4 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -60,4 +60,6 @@ virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss) specific_ss.add_all(when: 'CONFIG_VIRTIO', if_true: virtio_ss) softmmu_ss.add_all(when: 'CONFIG_VIRTIO', if_true: softmmu_virtio_ss) softmmu_ss.add(when: 'CONFIG_VIRTIO', if_false: files('vhost-stub.c')) +softmmu_ss.add(when: 'CONFIG_VIRTIO', if_false: files('virtio-stub.c')) softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) +softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('virtio-stub.c')) diff --git a/qapi/meson.build b/qapi/meson.build index 656ef0e039..10c54d68e5 100644 --- a/qapi/meson.build +++ b/qapi/meson.build @@ -48,6 +48,7 @@ qapi_all_modules = [ 'sockets', 'trace', 'transaction', + 'virtio', 'yank', ] if have_system