diff mbox

qdev-monitor: Avoid exiting when hot-plugging two devices with the same bootindex value

Message ID 1378924006-14057-1-git-send-email-marcel.a@redhat.com
State New
Headers show

Commit Message

Marcel Apfelbaum Sept. 11, 2013, 6:26 p.m. UTC
Qemu is expected to quit if the same boot index value is used by two devices.
However, hot-plugging a device with a bootindex value already used should
fail with a friendly message rather than quitting a running VM.

Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
---
 qdev-monitor.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

Comments

Marcel Apfelbaum Sept. 11, 2013, 6:41 p.m. UTC | #1
On Wed, 2013-09-11 at 21:26 +0300, Marcel Apfelbaum wrote:
> Qemu is expected to quit if the same boot index value is used by two devices.
> However, hot-plugging a device with a bootindex value already used should
> fail with a friendly message rather than quitting a running VM.
> 
> Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
> ---
>  qdev-monitor.c | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/qdev-monitor.c b/qdev-monitor.c
> index 410cdcb..654d086 100644
> --- a/qdev-monitor.c
> +++ b/qdev-monitor.c
> @@ -24,6 +24,7 @@
>  #include "qmp-commands.h"
>  #include "sysemu/arch_init.h"
>  #include "qemu/config-file.h"
> +#include "sysemu/sysemu.h"
>  
>  /*
>   * Aliases were a bad idea from the start.  Let's keep them
> @@ -442,6 +443,31 @@ static BusState *qbus_find(const char *path)
>      }
>  }
>  
> +#define OBJ_PROP_BOOTINDEX "bootindex"
A drawback of this patch is that is based on the assumption that
all devices supporting boot ordering will have a property with the
same name: bootindex.
A more robust approach would be to have some kind of interface
for such devices with a single "getter" method: bootindex()

Any thoughts?
Thanks,
Marcel 

> +
> +static bool bootindex_colision(Object *obj, QemuOpts *opts)
> +{
> +    int32_t bootindex;
> +
> +    if (!object_property_find(obj, OBJ_PROP_BOOTINDEX, NULL)) {
> +        return false;
> +    }
> +
> +    /* avoid parsing by setting the property and then getting it typed */
> +    object_property_parse(obj, qemu_opt_get(opts, OBJ_PROP_BOOTINDEX),
> +                          OBJ_PROP_BOOTINDEX, NULL);
> +    bootindex = (int32_t)object_property_get_int(obj, OBJ_PROP_BOOTINDEX,
> +                                                 NULL);
> +
> +    if (bootindex >= 0) {
> +        if (get_boot_device(bootindex)) {
> +            return true;
> +        }
> +    }
> +
> +    return false;
> +}
> +
>  DeviceState *qdev_device_add(QemuOpts *opts)
>  {
>      ObjectClass *obj;
> @@ -502,6 +528,13 @@ DeviceState *qdev_device_add(QemuOpts *opts)
>      /* create device, set properties */
>      qdev = DEVICE(object_new(driver));
>  
> +    if (qdev_hotplug && bootindex_colision(OBJECT(qdev), opts)) {
> +        qerror_report(QERR_INVALID_PARAMETER_VALUE,
> +                      "bootindex", "an unused boot index value");
> +        qdev_free(qdev);
> +        return NULL;
> +    }
> +
>      if (bus) {
>          qdev_set_parent_bus(qdev, bus);
>      }
Paolo Bonzini Sept. 12, 2013, 7:49 a.m. UTC | #2
Il 11/09/2013 20:26, Marcel Apfelbaum ha scritto:
> Qemu is expected to quit if the same boot index value is used by two devices.
> However, hot-plugging a device with a bootindex value already used should
> fail with a friendly message rather than quitting a running VM.

I think the problem is right where QEMU exits, i.e. in
add_boot_device_path.  This function should return an error instead, via
an Error ** argument.

Callers, typically a device's init or realize function, will either
print the error before returning an error code (e.g. -EBUSY for init) or
propagate the error up (for realize).

Returning/propagating failure will still cause QEMU to exit when the
duplicate bootindexes are found on the command line.

Paolo

> Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
> ---
>  qdev-monitor.c | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/qdev-monitor.c b/qdev-monitor.c
> index 410cdcb..654d086 100644
> --- a/qdev-monitor.c
> +++ b/qdev-monitor.c
> @@ -24,6 +24,7 @@
>  #include "qmp-commands.h"
>  #include "sysemu/arch_init.h"
>  #include "qemu/config-file.h"
> +#include "sysemu/sysemu.h"
>  
>  /*
>   * Aliases were a bad idea from the start.  Let's keep them
> @@ -442,6 +443,31 @@ static BusState *qbus_find(const char *path)
>      }
>  }
>  
> +#define OBJ_PROP_BOOTINDEX "bootindex"
> +
> +static bool bootindex_colision(Object *obj, QemuOpts *opts)
> +{
> +    int32_t bootindex;
> +
> +    if (!object_property_find(obj, OBJ_PROP_BOOTINDEX, NULL)) {
> +        return false;
> +    }
> +
> +    /* avoid parsing by setting the property and then getting it typed */
> +    object_property_parse(obj, qemu_opt_get(opts, OBJ_PROP_BOOTINDEX),
> +                          OBJ_PROP_BOOTINDEX, NULL);
> +    bootindex = (int32_t)object_property_get_int(obj, OBJ_PROP_BOOTINDEX,
> +                                                 NULL);
> +
> +    if (bootindex >= 0) {
> +        if (get_boot_device(bootindex)) {
> +            return true;
> +        }
> +    }
> +
> +    return false;
> +}
> +
>  DeviceState *qdev_device_add(QemuOpts *opts)
>  {
>      ObjectClass *obj;
> @@ -502,6 +528,13 @@ DeviceState *qdev_device_add(QemuOpts *opts)
>      /* create device, set properties */
>      qdev = DEVICE(object_new(driver));
>  
> +    if (qdev_hotplug && bootindex_colision(OBJECT(qdev), opts)) {
> +        qerror_report(QERR_INVALID_PARAMETER_VALUE,
> +                      "bootindex", "an unused boot index value");
> +        qdev_free(qdev);
> +        return NULL;
> +    }
> +
>      if (bus) {
>          qdev_set_parent_bus(qdev, bus);
>      }
>
Marcel Apfelbaum Sept. 12, 2013, 8:38 a.m. UTC | #3
On Thu, 2013-09-12 at 09:49 +0200, Paolo Bonzini wrote:
> Il 11/09/2013 20:26, Marcel Apfelbaum ha scritto:
> > Qemu is expected to quit if the same boot index value is used by two devices.
> > However, hot-plugging a device with a bootindex value already used should
> > fail with a friendly message rather than quitting a running VM.
> 
> I think the problem is right where QEMU exits, i.e. in
> add_boot_device_path.  This function should return an error instead, via
> an Error ** argument.
> 
> Callers, typically a device's init or realize function, will either
> print the error before returning an error code (e.g. -EBUSY for init) or
> propagate the error up (for realize).
Thanks, I'll try this.
Marcel

> Returning/propagating failure will still cause QEMU to exit when the
> duplicate bootindexes are found on the command line.
> 
> Paolo
> 
> > Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
> > ---
> >  qdev-monitor.c | 33 +++++++++++++++++++++++++++++++++
> >  1 file changed, 33 insertions(+)
> > 
> > diff --git a/qdev-monitor.c b/qdev-monitor.c
> > index 410cdcb..654d086 100644
> > --- a/qdev-monitor.c
> > +++ b/qdev-monitor.c
> > @@ -24,6 +24,7 @@
> >  #include "qmp-commands.h"
> >  #include "sysemu/arch_init.h"
> >  #include "qemu/config-file.h"
> > +#include "sysemu/sysemu.h"
> >  
> >  /*
> >   * Aliases were a bad idea from the start.  Let's keep them
> > @@ -442,6 +443,31 @@ static BusState *qbus_find(const char *path)
> >      }
> >  }
> >  
> > +#define OBJ_PROP_BOOTINDEX "bootindex"
> > +
> > +static bool bootindex_colision(Object *obj, QemuOpts *opts)
> > +{
> > +    int32_t bootindex;
> > +
> > +    if (!object_property_find(obj, OBJ_PROP_BOOTINDEX, NULL)) {
> > +        return false;
> > +    }
> > +
> > +    /* avoid parsing by setting the property and then getting it typed */
> > +    object_property_parse(obj, qemu_opt_get(opts, OBJ_PROP_BOOTINDEX),
> > +                          OBJ_PROP_BOOTINDEX, NULL);
> > +    bootindex = (int32_t)object_property_get_int(obj, OBJ_PROP_BOOTINDEX,
> > +                                                 NULL);
> > +
> > +    if (bootindex >= 0) {
> > +        if (get_boot_device(bootindex)) {
> > +            return true;
> > +        }
> > +    }
> > +
> > +    return false;
> > +}
> > +
> >  DeviceState *qdev_device_add(QemuOpts *opts)
> >  {
> >      ObjectClass *obj;
> > @@ -502,6 +528,13 @@ DeviceState *qdev_device_add(QemuOpts *opts)
> >      /* create device, set properties */
> >      qdev = DEVICE(object_new(driver));
> >  
> > +    if (qdev_hotplug && bootindex_colision(OBJECT(qdev), opts)) {
> > +        qerror_report(QERR_INVALID_PARAMETER_VALUE,
> > +                      "bootindex", "an unused boot index value");
> > +        qdev_free(qdev);
> > +        return NULL;
> > +    }
> > +
> >      if (bus) {
> >          qdev_set_parent_bus(qdev, bus);
> >      }
> > 
>
diff mbox

Patch

diff --git a/qdev-monitor.c b/qdev-monitor.c
index 410cdcb..654d086 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -24,6 +24,7 @@ 
 #include "qmp-commands.h"
 #include "sysemu/arch_init.h"
 #include "qemu/config-file.h"
+#include "sysemu/sysemu.h"
 
 /*
  * Aliases were a bad idea from the start.  Let's keep them
@@ -442,6 +443,31 @@  static BusState *qbus_find(const char *path)
     }
 }
 
+#define OBJ_PROP_BOOTINDEX "bootindex"
+
+static bool bootindex_colision(Object *obj, QemuOpts *opts)
+{
+    int32_t bootindex;
+
+    if (!object_property_find(obj, OBJ_PROP_BOOTINDEX, NULL)) {
+        return false;
+    }
+
+    /* avoid parsing by setting the property and then getting it typed */
+    object_property_parse(obj, qemu_opt_get(opts, OBJ_PROP_BOOTINDEX),
+                          OBJ_PROP_BOOTINDEX, NULL);
+    bootindex = (int32_t)object_property_get_int(obj, OBJ_PROP_BOOTINDEX,
+                                                 NULL);
+
+    if (bootindex >= 0) {
+        if (get_boot_device(bootindex)) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 DeviceState *qdev_device_add(QemuOpts *opts)
 {
     ObjectClass *obj;
@@ -502,6 +528,13 @@  DeviceState *qdev_device_add(QemuOpts *opts)
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
 
+    if (qdev_hotplug && bootindex_colision(OBJECT(qdev), opts)) {
+        qerror_report(QERR_INVALID_PARAMETER_VALUE,
+                      "bootindex", "an unused boot index value");
+        qdev_free(qdev);
+        return NULL;
+    }
+
     if (bus) {
         qdev_set_parent_bus(qdev, bus);
     }