[PATCH 09/12] qdev: Add machine_get_container()

Peter Xu posted 12 patches 2 days, 17 hours ago
There is a newer version of this series
[PATCH 09/12] qdev: Add machine_get_container()
Posted by Peter Xu 2 days, 17 hours ago
Add a helper to fetch machine containers.  Add some sanity check around.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/hw/qdev-core.h | 10 ++++++++++
 hw/core/qdev.c         | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 5be9844412..38edfb1b54 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
 void qdev_assert_realized_properly(void);
 Object *qdev_get_machine(void);
 
+/**
+ * machine_get_container:
+ * @name: The name of container to lookup
+ *
+ * Get a container of the machine (QOM path "/machine/XXX").
+ *
+ * Returns: the machine container object.
+ */
+Object *machine_get_container(const char *name);
+
 /**
  * qdev_get_human_name() - Return a human-readable name for a device
  * @dev: The device. Must be a valid and non-NULL pointer.
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index c869c47a27..6cb4fe4691 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -829,6 +829,23 @@ Object *qdev_get_machine(void)
     return dev;
 }
 
+Object *machine_get_container(const char *name)
+{
+    Object *container, *machine;
+
+    /*
+     * NOTE: nobody should call this _before_ machine is created.  If it
+     * happens, it's a programming error.
+     */
+    machine = qdev_get_machine();
+    assert(machine);
+
+    container = object_resolve_path_component(machine, name);
+    assert(object_dynamic_cast(container, TYPE_CONTAINER));
+
+    return container;
+}
+
 char *qdev_get_human_name(DeviceState *dev)
 {
     g_assert(dev != NULL);
-- 
2.45.0
Re: [PATCH 09/12] qdev: Add machine_get_container()
Posted by Markus Armbruster 2 days, 2 hours ago
Peter Xu <peterx@redhat.com> writes:

> Add a helper to fetch machine containers.  Add some sanity check around.
>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  include/hw/qdev-core.h | 10 ++++++++++
>  hw/core/qdev.c         | 17 +++++++++++++++++
>  2 files changed, 27 insertions(+)
>
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index 5be9844412..38edfb1b54 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
>  void qdev_assert_realized_properly(void);
>  Object *qdev_get_machine(void);
>  
> +/**
> + * machine_get_container:
> + * @name: The name of container to lookup
> + *
> + * Get a container of the machine (QOM path "/machine/XXX").

Suggest "/machine/NAME" here.

> + *
> + * Returns: the machine container object.
> + */
> +Object *machine_get_container(const char *name);
> +
>  /**
>   * qdev_get_human_name() - Return a human-readable name for a device
>   * @dev: The device. Must be a valid and non-NULL pointer.

[...]
Re: [PATCH 09/12] qdev: Add machine_get_container()
Posted by Daniel P. Berrangé 2 days, 5 hours ago
On Wed, Nov 20, 2024 at 04:57:00PM -0500, Peter Xu wrote:
> Add a helper to fetch machine containers.  Add some sanity check around.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  include/hw/qdev-core.h | 10 ++++++++++
>  hw/core/qdev.c         | 17 +++++++++++++++++
>  2 files changed, 27 insertions(+)
> 
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index 5be9844412..38edfb1b54 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -996,6 +996,16 @@ const char *qdev_fw_name(DeviceState *dev);
>  void qdev_assert_realized_properly(void);
>  Object *qdev_get_machine(void);
>  
> +/**
> + * machine_get_container:
> + * @name: The name of container to lookup
> + *
> + * Get a container of the machine (QOM path "/machine/XXX").
> + *
> + * Returns: the machine container object.
> + */
> +Object *machine_get_container(const char *name);
> +
>  /**
>   * qdev_get_human_name() - Return a human-readable name for a device
>   * @dev: The device. Must be a valid and non-NULL pointer.
> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> index c869c47a27..6cb4fe4691 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -829,6 +829,23 @@ Object *qdev_get_machine(void)
>      return dev;
>  }
>  
> +Object *machine_get_container(const char *name)
> +{
> +    Object *container, *machine;
> +
> +    /*
> +     * NOTE: nobody should call this _before_ machine is created.  If it
> +     * happens, it's a programming error.
> +     */
> +    machine = qdev_get_machine();
> +    assert(machine);

IMHO this assert should be in  qdev_get_machine() itself any
premature call of it is a programming error.

> +
> +    container = object_resolve_path_component(machine, name);
> +    assert(object_dynamic_cast(container, TYPE_CONTAINER));
> +
> +    return container;
> +}
> +
>  char *qdev_get_human_name(DeviceState *dev)
>  {
>      g_assert(dev != NULL);
> -- 
> 2.45.0
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH 09/12] qdev: Add machine_get_container()
Posted by Philippe Mathieu-Daudé 2 days, 6 hours ago
On 20/11/24 22:57, Peter Xu wrote:
> Add a helper to fetch machine containers.  Add some sanity check around.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>   include/hw/qdev-core.h | 10 ++++++++++
>   hw/core/qdev.c         | 17 +++++++++++++++++
>   2 files changed, 27 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>