Skip to contentSkip to conversation

hw/pci: Fix crash when running QEMU with "-nic model=rocker"

PATCH Applied Thomas Huth 4 participants 5 messages May 27, 2020 – Jun 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/pci: Fix crash when running QEMU with "-nic model=rocker"

QEMU currently aborts when being started with "-nic model=rocker" or with "-net nic,model=rocker". This happens because the "rocker" device is not a normal NIC but a switch, which has different properties. Thus we should only consider real NIC devices for "-nic" and "-net". These devices can be identified by the "netdev" property, so check for this property before adding the device to the list.

Fixes 52310c3fa7dc854d ("net: allow using any PCI NICs in -net or -nic")
---
 hw/pci/pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/pci/pci.c +12 -1
index 70c66965f5..46214f8287 100644
@@ -1887,7 +1887,18 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
             dc->user_creatable) {
             const char *name = object_class_get_name(list->data);
-            g_ptr_array_add(pci_nic_models, (gpointer)name);
+            /*
+             * A network device might also be something else than a NIC, see
+             * e.g. the "rocker" device. Thus we have to look for the "netdev"
+             * property, too. Unfortunately, some devices like virtio-net only
+             * create this property during instance_init, so we have to create
+             * a temporary instance here to be able to check it.
+             */
+            Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+            if (object_property_find(obj, "netdev", NULL)) {
+                g_ptr_array_add(pci_nic_models, (gpointer)name);
+            }
+            object_unref(obj);
         }
         next = list->next;
         g_slist_free_1(list);
Signature · 1 line
2.18.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Thomas Huth

QEMU currently aborts when being started with "-nic model=rocker" or with "-net nic,model=rocker". This happens because the "rocker" device is not a normal NIC but a switch, which has different properties. Thus we should only consider real NIC devices for "-nic" and "-net". These devices can be identified by the "netdev" property, so check for this property before adding the device to the list.

Fixes 52310c3fa7dc854d ("net: allow using any PCI NICs in -net or -nic")
---
 hw/pci/pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/pci/pci.c +5 -1
index 70c66965f5..46214f8287 100644
@@ -1887,7 +1887,18 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
             dc->user_creatable) {
             const char *name = object_class_get_name(list->data);
-            g_ptr_array_add(pci_nic_models, (gpointer)name);
+            /*
+             * A network device might also be something else than a NIC, see
+             * e.g. the "rocker" device. Thus we have to look for the "netdev"
+             * property, too. Unfortunately, some devices like virtio-net only
+             * create this property during instance_init, so we have to create

hw/pci/pci.c PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, · line 1904

Quoted 10 lines
hw/pci/pci.c +7 -0
+             * a temporary instance here to be able to check it.
+             */
+            Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+            if (object_property_find(obj, "netdev", NULL)) {
+                g_ptr_array_add(pci_nic_models, (gpointer)name);
+            }
+            object_unref(obj);
         }
         next = list->next;
         g_slist_free_1(list);
#2 lore raw
In reply to Thomas Huth

Le 27/05/2020 à 17:31, Thomas Huth a écrit :

Quoted 29 lines

QEMU currently aborts when being started with "-nic model=rocker" or with "-net nic,model=rocker". This happens because the "rocker" device is not a normal NIC but a switch, which has different properties. Thus we should only consider real NIC devices for "-nic" and "-net". These devices can be identified by the "netdev" property, so check for this property before adding the device to the list.

Fixes 52310c3fa7dc854d ("net: allow using any PCI NICs in -net or -nic")
---
 hw/pci/pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/pci/pci.c +5 -1
index 70c66965f5..46214f8287 100644
@@ -1887,7 +1887,18 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
             dc->user_creatable) {
             const char *name = object_class_get_name(list->data);
-            g_ptr_array_add(pci_nic_models, (gpointer)name);
+            /*
+             * A network device might also be something else than a NIC, see
+             * e.g. the "rocker" device. Thus we have to look for the "netdev"
+             * property, too. Unfortunately, some devices like virtio-net only
+             * create this property during instance_init, so we have to create

hw/pci/pci.c PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, · line 1904

Quoted 10 lines
hw/pci/pci.c +7 -0
+             * a temporary instance here to be able to check it.
+             */
+            Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+            if (object_property_find(obj, "netdev", NULL)) {
+                g_ptr_array_add(pci_nic_models, (gpointer)name);
+            }
+            object_unref(obj);
         }
         next = list->next;
         g_slist_free_1(list);

Not really trivial, I will not pick up this patch via trivial-branch, unless PCI maintainers request it.

Thanks,
Laurent

#3 lore raw
Quoted 44 lines · Laurent Vivier

Le 27/05/2020 à 17:31, Thomas Huth a écrit :

Quoted 29 lines

QEMU currently aborts when being started with "-nic model=rocker" or with "-net nic,model=rocker". This happens because the "rocker" device is not a normal NIC but a switch, which has different properties. Thus we should only consider real NIC devices for "-nic" and "-net". These devices can be identified by the "netdev" property, so check for this property before adding the device to the list.

Fixes 52310c3fa7dc854d ("net: allow using any PCI NICs in -net or -nic")
---
 hw/pci/pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/pci/pci.c +5 -1
index 70c66965f5..46214f8287 100644
@@ -1887,7 +1887,18 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
             dc->user_creatable) {
             const char *name = object_class_get_name(list->data);
-            g_ptr_array_add(pci_nic_models, (gpointer)name);
+            /*
+             * A network device might also be something else than a NIC, see
+             * e.g. the "rocker" device. Thus we have to look for the "netdev"
+             * property, too. Unfortunately, some devices like virtio-net only
+             * create this property during instance_init, so we have to create

hw/pci/pci.c PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, · line 1904

Quoted 10 lines
hw/pci/pci.c +7 -0
+             * a temporary instance here to be able to check it.
+             */
+            Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+            if (object_property_find(obj, "netdev", NULL)) {
+                g_ptr_array_add(pci_nic_models, (gpointer)name);
+            }
+            object_unref(obj);
         }
         next = list->next;
         g_slist_free_1(list);

Not really trivial, I will not pick up this patch via trivial-branch, unless PCI maintainers request it.

Quoted 3 lines

Thanks,
Laurent

I'll pick it up then.

Paolo

#4 lore raw
Quoted 45 lines · Paolo Bonzini
Quoted 44 lines · Laurent Vivier

Le 27/05/2020 à 17:31, Thomas Huth a écrit :

Quoted 29 lines

QEMU currently aborts when being started with "-nic model=rocker" or with "-net nic,model=rocker". This happens because the "rocker" device is not a normal NIC but a switch, which has different properties. Thus we should only consider real NIC devices for "-nic" and "-net". These devices can be identified by the "netdev" property, so check for this property before adding the device to the list.

Fixes 52310c3fa7dc854d ("net: allow using any PCI NICs in -net or -nic")
---
 hw/pci/pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/pci/pci.c +5 -1
index 70c66965f5..46214f8287 100644
@@ -1887,7 +1887,18 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
             dc->user_creatable) {
             const char *name = object_class_get_name(list->data);
-            g_ptr_array_add(pci_nic_models, (gpointer)name);
+            /*
+             * A network device might also be something else than a NIC, see
+             * e.g. the "rocker" device. Thus we have to look for the "netdev"
+             * property, too. Unfortunately, some devices like virtio-net only
+             * create this property during instance_init, so we have to create

hw/pci/pci.c PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, · line 1904

Quoted 10 lines
hw/pci/pci.c +7 -0
+             * a temporary instance here to be able to check it.
+             */
+            Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+            if (object_property_find(obj, "netdev", NULL)) {
+                g_ptr_array_add(pci_nic_models, (gpointer)name);
+            }
+            object_unref(obj);
         }
         next = list->next;
         g_slist_free_1(list);

Not really trivial, I will not pick up this patch via trivial-branch, unless PCI maintainers request it.

Quoted 7 lines
Quoted 3 lines

Thanks,
Laurent

I'll pick it up then.

Paolo

I queued this already. Thanks!

Signature · 1 line
MST