Skip to contentSkip to conversation

Fix the "-nic help" option

PATCH Superseded Thomas Huth 3 participants 22 messages Nov 4, 2022 – Nov 10, 2022 Networking

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix the "-nic help" option

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions, but since QEMU 6.0 it just complains that "help" is not a valid value here. This patch series fixes this problem and also extends the help output here to list the available NIC models, too.

Thomas Huth (3):
net: Move the code to collect available NIC models to a separate
function
net: Restore printing of the help text with "-nic help"
net: Replace "Supported NIC models" with "Available NIC models"

 include/net/net.h |  1 +
 hw/pci/pci.c      | 29 +-------------------------
 net/net.c         | 52 ++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 51 insertions(+), 31 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/3 net: Move the code to collect available NIC models to a separate function

The code that collects the available NIC models is not really specific to PCI anymore and will be required in the next patch, too, so let's move this into a new separate function in net.c instead.

---
 include/net/net.h |  1 +
 hw/pci/pci.c      | 29 +----------------------------
 net/net.c         | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 28 deletions(-)
include/net/net.h +1 -0
index 3db75ff841..c96cefb89a 100644
@@ -189,6 +189,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
 int qemu_set_vnet_le(NetClientState *nc, bool is_le);
 int qemu_set_vnet_be(NetClientState *nc, bool is_be);
 void qemu_macaddr_default_if_unset(MACAddr *macaddr);
+GPtrArray *qemu_get_nic_models(const char *device_type);
 int qemu_show_nic_models(const char *arg, const char *const *models);
 void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
hw/pci/pci.c +1 -28
index 2f450f6a72..2b7b343e82 100644
@@ -1964,7 +1964,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
                                const char *default_devaddr)
 {
     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
-    GSList *list;
     GPtrArray *pci_nic_models;
     PCIBus *bus;
     PCIDevice *pci_dev;
@@ -1979,33 +1978,7 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         nd->model = g_strdup("virtio-net-pci");
     }
 
-    list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
-    pci_nic_models = g_ptr_array_new();
-    while (list) {
-        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
-                                             TYPE_DEVICE);
-        GSList *next;
-        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
-            dc->user_creatable) {
-            const char *name = object_class_get_name(list->data);
-            /*
-             * 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")) {
-                g_ptr_array_add(pci_nic_models, (gpointer)name);
-            }
-            object_unref(obj);
-        }
-        next = list->next;
-        g_slist_free_1(list);
-        list = next;
-    }
-    g_ptr_array_add(pci_nic_models, NULL);
+    pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE);
 
     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
         exit(0);
net/net.c +36 -0
index 840ad9dca5..c0516a8067 100644
@@ -899,6 +899,42 @@ static int nic_get_free_idx(void)
     return -1;
 }
 
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models;
+    GSList *list;
+
+    list = object_class_get_list_sorted(device_type, false);
+    nic_models = g_ptr_array_new();
+    while (list) {
+        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+                                             TYPE_DEVICE);
+        GSList *next;
+        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+            dc->user_creatable) {
+            const char *name = object_class_get_name(list->data);
+            /*
+             * 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")) {
+                g_ptr_array_add(nic_models, (gpointer)name);
+            }
+            object_unref(obj);
+        }
+        next = list->next;
+        g_slist_free_1(list);
+        list = next;
+    }
+    g_ptr_array_add(nic_models, NULL);
+
+    return nic_models;
+}
+
 int qemu_show_nic_models(const char *arg, const char *const *models)
 {
     int i;
Signature · 1 line
2.31.1
#6 lore raw

PATCH 2/3 net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
 net/net.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +12 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
     const char *type;
 
     type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
+            exit(0);
+        }
     }
 
     idx = nic_get_free_idx();
Signature · 1 line
2.31.1
#20 lore raw

PATCH 3/3 net: Replace "Supported NIC models" with "Available NIC models"

In reply to Thomas Huth

Just because a NIC model is compiled into the QEMU binary does not necessary mean that it can be used with each and every machine. So let's rather talk about "available" models instead of "supported" models, just to avoid confusion.

---
 net/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/net.c +1 -1
index b4b8f2a9cc..173195dbf9 100644
@@ -943,7 +943,7 @@ int qemu_show_nic_models(const char *arg, const char *const *models)
         return 0;
     }
 
-    printf("Supported NIC models:\n");
+    printf("Available NIC models:\n");
     for (i = 0 ; models[i]; i++) {
         printf("%s\n", models[i]);
     }
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/3] net: Move the code to collect available NIC models to a separate function

In reply to Thomas Huth
Quoted 16 lines · Thomas Huth

The code that collects the available NIC models is not really specific to PCI anymore and will be required in the next patch, too, so let's move this into a new separate function in net.c instead.

---
 include/net/net.h |  1 +
 hw/pci/pci.c      | 29 +----------------------------
 net/net.c         | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 28 deletions(-)
include/net/net.h +0 -0
index 3db75ff841..c96cefb89a 100644

include/net/net.h void qemu_set_vnet_hdr_len(NetClientState *nc, int len); · line 192

Quoted 4 lines
include/net/net.h +1 -0
@@ -189,6 +189,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
 int qemu_set_vnet_le(NetClientState *nc, bool is_le);
 int qemu_set_vnet_be(NetClientState *nc, bool is_be);
 void qemu_macaddr_default_if_unset(MACAddr *macaddr);
+GPtrArray *qemu_get_nic_models(const char *device_type);

I know there is no precedent in this file, but it would be useful to document this function, what it does exactly and what it returns, the return value, allocation assumptions etc.

Quoted 87 lines
include/net/net.h +0 -0
 int qemu_show_nic_models(const char *arg, const char *const *models);
 void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
hw/pci/pci.c +1 -28
index 2f450f6a72..2b7b343e82 100644
@@ -1964,7 +1964,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
                                const char *default_devaddr)
 {
     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
-    GSList *list;
     GPtrArray *pci_nic_models;
     PCIBus *bus;
     PCIDevice *pci_dev;
@@ -1979,33 +1978,7 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         nd->model = g_strdup("virtio-net-pci");
     }
 
-    list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
-    pci_nic_models = g_ptr_array_new();
-    while (list) {
-        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
-                                             TYPE_DEVICE);
-        GSList *next;
-        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
-            dc->user_creatable) {
-            const char *name = object_class_get_name(list->data);
-            /*
-             * 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")) {
-                g_ptr_array_add(pci_nic_models, (gpointer)name);
-            }
-            object_unref(obj);
-        }
-        next = list->next;
-        g_slist_free_1(list);
-        list = next;
-    }
-    g_ptr_array_add(pci_nic_models, NULL);
+    pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE);
 
     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
         exit(0);
net/net.c +29 -0
index 840ad9dca5..c0516a8067 100644
@@ -899,6 +899,42 @@ static int nic_get_free_idx(void)
     return -1;
 }
 
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models;
+    GSList *list;
+
+    list = object_class_get_list_sorted(device_type, false);
+    nic_models = g_ptr_array_new();
+    while (list) {
+        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+                                             TYPE_DEVICE);
+        GSList *next;
+        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+            dc->user_creatable) {
+            const char *name = object_class_get_name(list->data);
+            /*
+             * 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")) {
+                g_ptr_array_add(nic_models, (gpointer)name);
+            }
+            object_unref(obj);
+        }
+        next = list->next;
+        g_slist_free_1(list);

net/net.c static int nic_get_free_idx(void) · line 940

Quoted 10 lines
net/net.c +7 -0
+        list = next;
+    }
+    g_ptr_array_add(nic_models, NULL);
+
+    return nic_models;
+}
+
 int qemu_show_nic_models(const char *arg, const char *const *models)
 {
     int i;

Claudio

#7 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth

should -net and -netdev be adapted too?

For audio, we now have support for help options in both -audiodev and -audio..

Thanks,

Claudio

Quoted 37 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
 net/net.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +12 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
     const char *type;
 
     type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
+            exit(0);
+        }
     }
 
     idx = nic_get_free_idx();
Reviewed-by on v1 3/3
#21 lore raw

Re: [PATCH 3/3] net: Replace "Supported NIC models" with "Available NIC models"

In reply to Thomas Huth
Quoted 15 lines · Thomas Huth

Just because a NIC model is compiled into the QEMU binary does not necessary mean that it can be used with each and every machine. So let's rather talk about "available" models instead of "supported" models, just to avoid confusion.

---
 net/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/net.c +0 -0
index b4b8f2a9cc..173195dbf9 100644

net/net.c int qemu_show_nic_models(const char *arg, const char *const *models) · line 949

Quoted 8 lines
net/net.c +1 -1
@@ -943,7 +943,7 @@ int qemu_show_nic_models(const char *arg, const char *const *models)
         return 0;
     }
 
-    printf("Supported NIC models:\n");
+    printf("Available NIC models:\n");
     for (i = 0 ; models[i]; i++) {
         printf("%s\n", models[i]);
     }
#3 lore raw

Re: [PATCH 1/3] net: Move the code to collect available NIC models to a separate function

In reply to Thomas Huth
Quoted 103 lines · Thomas Huth

The code that collects the available NIC models is not really specific to PCI anymore and will be required in the next patch, too, so let's move this into a new separate function in net.c instead.

---
 include/net/net.h |  1 +
 hw/pci/pci.c      | 29 +----------------------------
 net/net.c         | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 28 deletions(-)
include/net/net.h +1 -0
index 3db75ff841..c96cefb89a 100644
@@ -189,6 +189,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
 int qemu_set_vnet_le(NetClientState *nc, bool is_le);
 int qemu_set_vnet_be(NetClientState *nc, bool is_be);
 void qemu_macaddr_default_if_unset(MACAddr *macaddr);
+GPtrArray *qemu_get_nic_models(const char *device_type);
 int qemu_show_nic_models(const char *arg, const char *const *models);
 void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
hw/pci/pci.c +1 -28
index 2f450f6a72..2b7b343e82 100644
@@ -1964,7 +1964,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
                                const char *default_devaddr)
 {
     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
-    GSList *list;
     GPtrArray *pci_nic_models;
     PCIBus *bus;
     PCIDevice *pci_dev;
@@ -1979,33 +1978,7 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         nd->model = g_strdup("virtio-net-pci");
     }
 
-    list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
-    pci_nic_models = g_ptr_array_new();
-    while (list) {
-        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
-                                             TYPE_DEVICE);
-        GSList *next;
-        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
-            dc->user_creatable) {
-            const char *name = object_class_get_name(list->data);
-            /*
-             * 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")) {
-                g_ptr_array_add(pci_nic_models, (gpointer)name);
-            }
-            object_unref(obj);
-        }
-        next = list->next;
-        g_slist_free_1(list);
-        list = next;
-    }
-    g_ptr_array_add(pci_nic_models, NULL);
+    pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE);
 
     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
         exit(0);
net/net.c +25 -0
index 840ad9dca5..c0516a8067 100644
@@ -899,6 +899,42 @@ static int nic_get_free_idx(void)
     return -1;
 }
 
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models;
+    GSList *list;
+
+    list = object_class_get_list_sorted(device_type, false);
+    nic_models = g_ptr_array_new();
+    while (list) {
+        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+                                             TYPE_DEVICE);
+        GSList *next;
+        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+            dc->user_creatable) {
+            const char *name = object_class_get_name(list->data);
+            /*
+             * 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")) {
+                g_ptr_array_add(nic_models, (gpointer)name);
+            }

net/net.c static int nic_get_free_idx(void) · line 936

Quoted 10 lines
net/net.c +10 -0
+            object_unref(obj);
+        }
+        next = list->next;
+        g_slist_free_1(list);
+        list = next;
+    }
+    g_ptr_array_add(nic_models, NULL);
+
+    return nic_models;
+}

Is it worth freeing as you go and playing the next/list dance when you could just:

  GPtrArray *qemu_get_nic_models(const char *device_type)
  {
      GPtrArray *nic_models = g_ptr_array_new();
      g_autoptr(GSList) list = object_class_get_list_sorted(device_type, false);
      do {
          DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
                                               TYPE_DEVICE);
          if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
              dc->user_creatable) {
              const char *name = object_class_get_name(list->data);
              /*
               * 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")) {
                  g_ptr_array_add(nic_models, (gpointer)name);
              }
              object_unref(obj);
          }
      } while ((list = g_slist_next(list)));
      g_ptr_array_add(nic_models, NULL);

      return nic_models;

}

I must admit I'm not super clear on the lifetimes object_class_get_list_sorted but I assume the contents are static and we only need the equivalent of g_slist_free.

Quoted 4 lines
net/net.c +1 -0
+
 int qemu_show_nic_models(const char *arg, const char *const *models)
 {
     int i;
Signature · 1 line
Alex Bennée
#8 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Claudio Fontana
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Thomas

#9 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

Quoted 8 lines · Thomas Huth
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Thomas

I wonder if it could be done once for all, in net_init_clients, instead of repeating the is_help_option in net_init_netdev and net_param_nic (and that would take care of net_init_client too, so we'd get "net" for free)..

Ciao,

C

#10 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

Quoted 7 lines · Claudio Fontana
Quoted 6 lines · Thomas Huth
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Quoted 6 lines
Quoted 2 lines

Thomas

I wonder if it could be done once for all, in net_init_clients, instead of repeating the is_help_option in net_init_netdev and net_param_nic (and that would take care of net_init_client too, so we'd get "net" for free)..

I don't think that it makes too much sense to have one option for all - since all three CLI options are slightly different anyway. E.g. "-net nic" only exists for "-net", "hubport" cannot be used with "-net", "-nic" can also be used to configure the NIC model, etc.

Thomas

#11 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

Quoted 15 lines · Thomas Huth
Quoted 7 lines · Claudio Fontana
Quoted 6 lines · Thomas Huth
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Quoted 6 lines
Quoted 2 lines

Thomas

I wonder if it could be done once for all, in net_init_clients, instead of repeating the is_help_option in net_init_netdev and net_param_nic (and that would take care of net_init_client too, so we'd get "net" for free)..

Quoted 7 lines

I don't think that it makes too much sense to have one option for all - since all three CLI options are slightly different anyway. E.g. "-net nic" only exists for "-net", "hubport" cannot be used with "-net", "-nic" can also be used to configure the NIC model, etc.

Thomas

Hi Thomas, I would not suggest to merge everything together,

I was considering whether it would make sense to just check the "type" id for is_help_option once, since all the options "net", "netdev", "nic" have a "type" implied_opt_name,

and so it should be possible to make a list of structs that signify what to do for "net", "netdev", "nic", and loop on that and check for that help string once,

and then split off the codepath into the "net", "netdev", "nic" - specific code as it is now,
either manually or by storing the function that is now in the foreach as a function pointer in the struct, ie moving the is_help_option check one level up.

However, it might not be worth it since it seems that for "nic" the nic models need to also be printed, so it might make things needlessly verbose.

Not sure, have not tried to write the code for it.

Ciao,

Claudio

#14 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
 net/net.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
     const char *type;
 
     type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

flow more logically?

Ciao

C

Quoted 5 lines
net/net.c +2 -0
+            exit(0);
+        }
     }
 
     idx = nic_get_free_idx();
#4 lore raw

Re: [PATCH 1/3] net: Move the code to collect available NIC models to a separate function

In reply to Alex Bennée
Quoted 148 lines · Alex Bennée
Quoted 103 lines · Thomas Huth

The code that collects the available NIC models is not really specific to PCI anymore and will be required in the next patch, too, so let's move this into a new separate function in net.c instead.

---
  include/net/net.h |  1 +
  hw/pci/pci.c      | 29 +----------------------------
  net/net.c         | 36 ++++++++++++++++++++++++++++++++++++
  3 files changed, 38 insertions(+), 28 deletions(-)
include/net/net.h +1 -0
index 3db75ff841..c96cefb89a 100644
@@ -189,6 +189,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
  int qemu_set_vnet_le(NetClientState *nc, bool is_le);
  int qemu_set_vnet_be(NetClientState *nc, bool is_be);
  void qemu_macaddr_default_if_unset(MACAddr *macaddr);
+GPtrArray *qemu_get_nic_models(const char *device_type);
  int qemu_show_nic_models(const char *arg, const char *const *models);
  void qemu_check_nic_model(NICInfo *nd, const char *model);
  int qemu_find_nic_model(NICInfo *nd, const char * const *models,
hw/pci/pci.c +1 -28
index 2f450f6a72..2b7b343e82 100644
@@ -1964,7 +1964,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
                                 const char *default_devaddr)
  {
      const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
-    GSList *list;
      GPtrArray *pci_nic_models;
      PCIBus *bus;
      PCIDevice *pci_dev;
@@ -1979,33 +1978,7 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
          nd->model = g_strdup("virtio-net-pci");
      }
 
-    list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
-    pci_nic_models = g_ptr_array_new();
-    while (list) {
-        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
-                                             TYPE_DEVICE);
-        GSList *next;
-        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
-            dc->user_creatable) {
-            const char *name = object_class_get_name(list->data);
-            /*
-             * 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")) {
-                g_ptr_array_add(pci_nic_models, (gpointer)name);
-            }
-            object_unref(obj);
-        }
-        next = list->next;
-        g_slist_free_1(list);
-        list = next;
-    }
-    g_ptr_array_add(pci_nic_models, NULL);
+    pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE);
 
      if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
          exit(0);
net/net.c +25 -0
index 840ad9dca5..c0516a8067 100644
@@ -899,6 +899,42 @@ static int nic_get_free_idx(void)
      return -1;
  }
 
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models;
+    GSList *list;
+
+    list = object_class_get_list_sorted(device_type, false);
+    nic_models = g_ptr_array_new();
+    while (list) {
+        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+                                             TYPE_DEVICE);
+        GSList *next;
+        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+            dc->user_creatable) {
+            const char *name = object_class_get_name(list->data);
+            /*
+             * 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")) {
+                g_ptr_array_add(nic_models, (gpointer)name);
+            }

net/net.c static int nic_get_free_idx(void) · line 936

Quoted 10 lines
net/net.c +10 -0
+            object_unref(obj);
+        }
+        next = list->next;
+        g_slist_free_1(list);
+        list = next;
+    }
+    g_ptr_array_add(nic_models, NULL);
+
+    return nic_models;
+}

Is it worth freeing as you go and playing the next/list dance when you could just:

   GPtrArray *qemu_get_nic_models(const char *device_type)
   {
       GPtrArray *nic_models = g_ptr_array_new();
       g_autoptr(GSList) list = object_class_get_list_sorted(device_type, false);
       do {
           DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
                                                TYPE_DEVICE);
           if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
               dc->user_creatable) {
               const char *name = object_class_get_name(list->data);
               /*
                * 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")) {
                   g_ptr_array_add(nic_models, (gpointer)name);
               }
               object_unref(obj);
           }
       } while ((list = g_slist_next(list)));
       g_ptr_array_add(nic_models, NULL);

       return nic_models;
Quoted 5 lines

}

I must admit I'm not super clear on the lifetimes object_class_get_list_sorted but I assume the contents are static and we only need the equivalent of g_slist_free.

Looks like it could work, too. I'll add a patch on top to change it.

Thomas

#12 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Claudio Fontana
Quoted 32 lines · Claudio Fontana
Quoted 15 lines · Thomas Huth
Quoted 7 lines · Claudio Fontana
Quoted 6 lines · Thomas Huth
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Quoted 6 lines
Quoted 2 lines

Thomas

I wonder if it could be done once for all, in net_init_clients, instead of repeating the is_help_option in net_init_netdev and net_param_nic (and that would take care of net_init_client too, so we'd get "net" for free)..

Quoted 7 lines

I don't think that it makes too much sense to have one option for all - since all three CLI options are slightly different anyway. E.g. "-net nic" only exists for "-net", "hubport" cannot be used with "-net", "-nic" can also be used to configure the NIC model, etc.

Thomas

Hi Thomas, I would not suggest to merge everything together,

I was considering whether it would make sense to just check the "type" id for is_help_option once, since all the options "net", "netdev", "nic" have a "type" implied_opt_name,

and so it should be possible to make a list of structs that signify what to do for "net", "netdev", "nic", and loop on that and check for that help string once,

Quoted 6 lines

and then split off the codepath into the "net", "netdev", "nic" - specific code as it is now,
either manually or by storing the function that is now in the foreach as a function pointer in the struct, ie moving the is_help_option check one level up.

However, it might not be worth it since it seems that for "nic" the nic models need to also be printed, so it might make things needlessly verbose.

Not sure, have not tried to write the code for it.

Sorry, I currently fail to see how that should really work out nicely, so I'll continue with my current patch. But feel free to write some patches, maybe it's clearer to me if I see the code.

Thomas

#15 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Claudio Fontana
Quoted 36 lines · Claudio Fontana
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
  net/net.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
      const char *type;
 
      type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

Quoted 8 lines

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

flow more logically?

I think that's mostly a matter of taste ... and as long as the declaration of the variable has to stay at the top of the block (according to QEMU's coding style), I think I'd also prefer to keep the initialization there.

Thomas

#13 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth
Quoted 38 lines · Thomas Huth
Quoted 37 lines · Claudio Fontana
Quoted 15 lines · Thomas Huth
Quoted 7 lines · Claudio Fontana
Quoted 6 lines · Thomas Huth
Quoted 1 line · Claudio Fontana

should -net and -netdev be adapted too?

"-netdev help" already works just fine ... and "-net" should IMHO rather be removed than improved ;-)

Quoted 6 lines
Quoted 2 lines

Thomas

I wonder if it could be done once for all, in net_init_clients, instead of repeating the is_help_option in net_init_netdev and net_param_nic (and that would take care of net_init_client too, so we'd get "net" for free)..

Quoted 7 lines

I don't think that it makes too much sense to have one option for all - since all three CLI options are slightly different anyway. E.g. "-net nic" only exists for "-net", "hubport" cannot be used with "-net", "-nic" can also be used to configure the NIC model, etc.

Thomas

Hi Thomas, I would not suggest to merge everything together,

I was considering whether it would make sense to just check the "type" id for is_help_option once, since all the options "net", "netdev", "nic" have a "type" implied_opt_name,

and so it should be possible to make a list of structs that signify what to do for "net", "netdev", "nic", and loop on that and check for that help string once,

and then split off the codepath into the "net", "netdev", "nic" - specific code as it is now,
either manually or by storing the function that is now in the foreach as a function pointer in the struct, ie moving the is_help_option check one level up.

However, it might not be worth it since it seems that for "nic" the nic models need to also be printed, so it might make things needlessly verbose.

Quoted 8 lines
Quoted 1 line

Not sure, have not tried to write the code for it.

Sorry, I currently fail to see how that should really work out nicely, so I'll continue with my current patch. But feel free to write some patches, maybe it's clearer to me if I see the code.

Thomas

No worries, maybe it's just too complex and not worth it.

Ciao,

C

#16 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Claudio Fontana
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
  net/net.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
      const char *type;
 
      type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
Quoted 3 lines
Quoted 1 line

flow more logically?

I think that's mostly a matter of taste ...

To some extent, but for the reader it would make more sense not to intermix unrelated code?

I'd say:

- show_netdevs
_ get nic models
- show nic models

instead of:

- get nic models
- show netdevs
- show nic models

Quoted 4 lines

and as long as the declaration
of the variable has to stay at the top of the block (according to QEMU's coding style), I think I'd also prefer to keep the initialization there.

This conflict can easily be solved by putting the nic_models on its own line, as I have shown in my hunk above.

Ciao,

Claudio

#17 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

Quoted 64 lines · Claudio Fontana
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Claudio Fontana
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
  net/net.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
      const char *type;
 
      type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
Quoted 3 lines
Quoted 1 line

flow more logically?

I think that's mostly a matter of taste ...

To some extent, but for the reader it would make more sense not to intermix unrelated code?

I'd say:

- show_netdevs
_ get nic models
- show nic models

instead of:

- get nic models
- show netdevs
- show nic models

Quoted 8 lines
Quoted 4 lines

and as long as the declaration
of the variable has to stay at the top of the block (according to QEMU's coding style), I think I'd also prefer to keep the initialization there.

This conflict can easily be solved by putting the nic_models on its own line, as I have shown in my hunk above.

And another option (not used enough in QEMU code imo), is to open a block specifically for the nic models.

Ie you can say:

show_netdevs();
printf("\n");

 {
     GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
     qemu_show_nic_models(type, (const char **)nic_models->pdata);
     g_ptr_array_free(nic_models, true);

}

this even has the additional benefit of making the allocation/free of the nic_models clearer as it corresponds to the block.

C

#18 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Claudio Fontana
Quoted 45 lines · Claudio Fontana
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Claudio Fontana
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
   net/net.c | 14 ++++++++++++--
   1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
       const char *type;
 
       type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

flow more logically?

I think that's mostly a matter of taste ...

To some extent, but for the reader it would make more sense not to intermix unrelated code?

I'm pretty sure that as soon as I change it, another reviewer shows up and asks me to put everything into one line again since they prefer more compact code ;-)

Quoted 11 lines

I'd say:

- show_netdevs
_ get nic models
- show nic models

instead of:

- get nic models
- show netdevs
- show nic models

I get your point, and I would immediately agree with you if we were allowed to do:

        show_netdevs();
        printf("\n");
        GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
        qemu_show_nic_models(type, (const char **)nic_models->pdata);
        g_ptr_array_free(nic_models, true);

Although this is possible nowadays (since we're using not C89 anymore), it's against the QEMU coding style.

So it's a trade-off now - use two lines of code and have some more chronological code flow, or use one line of more compact code.

I'm in favor of the more compact code. So please let's stop bike-shedding now.

Thanks,
Thomas

#5 lore raw

Re: [PATCH 1/3] net: Move the code to collect available NIC models to a separate function

In reply to Thomas Huth
Quoted 155 lines · Thomas Huth
Quoted 154 lines · Alex Bennée
Quoted 117 lines · Thomas Huth

The code that collects the available NIC models is not really specific to PCI anymore and will be required in the next patch, too, so let's move this into a new separate function in net.c instead.

---
  include/net/net.h |  1 +
  hw/pci/pci.c      | 29 +---------------------------- net/net.c         | 36 ++++++++++++++++++++++++++++++++++++

  3 files changed, 38 insertions(+), 28 deletions(-)
include/net/net.h +1 -0
index 3db75ff841..c96cefb89a 100644
@@ -189,6 +189,7 @@ void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
   int qemu_set_vnet_le(NetClientState *nc, bool is_le);
   int qemu_set_vnet_be(NetClientState *nc, bool is_be);
   void qemu_macaddr_default_if_unset(MACAddr *macaddr);
+GPtrArray *qemu_get_nic_models(const char *device_type);
   int qemu_show_nic_models(const char *arg, const char *const *models);
   void qemu_check_nic_model(NICInfo *nd, const char *model);
   int qemu_find_nic_model(NICInfo *nd, const char * const *models,
hw/pci/pci.c +0 -1
index 2f450f6a72..2b7b343e82 100644
@@ -1964,7 +1964,6 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus 
 *rootbus,
                                  const char *default_devaddr)
   {
       const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
-    GSList *list;
       GPtrArray *pci_nic_models;
       PCIBus *bus;

      PCIDevice *pci_dev;

hw/pci/pci.c +0 -11
@@ -1979,33 +1978,7 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus 
 *rootbus,
           nd->model = g_strdup("virtio-net-pci");
       }
-    list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
-    pci_nic_models = g_ptr_array_new();
-    while (list) {
-        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
-                                             TYPE_DEVICE);
-        GSList *next;
-        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
-            dc->user_creatable) {
-            const char *name = object_class_get_name(list->data);
-            /*
-             * 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")) {
-                g_ptr_array_add(pci_nic_models, (gpointer)name);
-            }
-            object_unref(obj);
-        }
-        next = list->next;
-        g_slist_free_1(list);
-        list = next;
-    }
-    g_ptr_array_add(pci_nic_models, NULL);
+    pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE);
      if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) { exit(0);

net/net.c +16 -0
index 840ad9dca5..c0516a8067 100644
@@ -899,6 +899,42 @@ static int nic_get_free_idx(void)
       return -1;
   }
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models;
+    GSList *list;
+
+    list = object_class_get_list_sorted(device_type, false);
+    nic_models = g_ptr_array_new();
+    while (list) {
+        DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+                                             TYPE_DEVICE);
+        GSList *next;
+        if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+            dc->user_creatable) {
+            const char *name = object_class_get_name(list->data);
+            /*
+             * 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")) {
+                g_ptr_array_add(nic_models, (gpointer)name); +            }
+            object_unref(obj);
+        }
+        next = list->next;
+        g_slist_free_1(list);
+        list = next;
+    }
+    g_ptr_array_add(nic_models, NULL);

net/net.c static int nic_get_free_idx(void) · line 936

Quoted 3 lines
net/net.c +3 -0
+
+    return nic_models;
+}

Is it worth freeing as you go and playing the next/list dance when you could just:

   GPtrArray *qemu_get_nic_models(const char *device_type)
   {
       GPtrArray *nic_models = g_ptr_array_new();
       g_autoptr(GSList) list = object_class_get_list_sorted(device_type, false);

       do {
           DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data, TYPE_DEVICE);
           if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) && dc->user_creatable) {
               const char *name = object_class_get_name(list->data); /*
                * 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")) { g_ptr_array_add(nic_models, (gpointer)name);
               }
               object_unref(obj);
           }
       } while ((list = g_slist_next(list))); g_ptr_array_add(nic_models, NULL);

Quoted 8 lines
Quoted 6 lines

       return nic_models;
   }

I must admit I'm not super clear on the lifetimes object_class_get_list_sorted but I assume the contents are static and we only need the equivalent of g_slist_free.

Looks like it could work, too. I'll add a patch on top to change it.

Ok, now I've tried, and it does not work - valgrind compains about leaking memory here. The problem is: You have to keep the pointer to the list head around, by doing list = g_slist_next(list) you leave memory behind that cannot be freed anymore. Thus I'll drop this change for now, since keeping the pointer to the list head just for freeing it later is also ugly.

Thomas

#19 lore raw

Re: [PATCH 2/3] net: Restore printing of the help text with "-nic help"

In reply to Thomas Huth
Quoted 71 lines · Thomas Huth
Quoted 45 lines · Claudio Fontana
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Claudio Fontana
Quoted 22 lines · Thomas Huth

Running QEMU with "-nic help" used to work in QEMU 5.2 and earlier versions (it showed the available netdev backends), but this feature got broken during some refactoring in version 6.0. Let's restore the old behavior, and while we're at it, let's also print the available NIC models here now since this option can be used to configure both, netdev backend and model in one go.

Fixes ad6f932fe8 ("net: do not exit on "netdev_add help" monitor command")
---
   net/net.c | 14 ++++++++++++--
   1 file changed, 12 insertions(+), 2 deletions(-)
net/net.c +0 -2
index c0516a8067..b4b8f2a9cc 100644
@@ -1571,8 +1571,18 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
       const char *type;
 
       type = qemu_opt_get(opts, "type");
-    if (type && g_str_equal(type, "none")) {
-        return 0;    /* Nothing to do, default_net is cleared in vl.c */

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1583

Quoted 10 lines
net/net.c +10 -0
+    if (type) {
+        if (g_str_equal(type, "none")) {
+            return 0;    /* Nothing to do, default_net is cleared in vl.c */
+        }
+        if (is_help_option(type)) {
+            GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            show_netdevs();
+            printf("\n");
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);

nit: would not the order:

Quoted 6 lines
net/net.c +6 -0
+            GPtrArray *nic_models;
+            show_netdevs();
+            printf("\n");
+            nic_models = qemu_get_nic_models(TYPE_DEVICE);
+            qemu_show_nic_models(type, (const char **)nic_models->pdata);
+            g_ptr_array_free(nic_models, true);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

flow more logically?

I think that's mostly a matter of taste ...

To some extent, but for the reader it would make more sense not to intermix unrelated code?

I'm pretty sure that as soon as I change it, another reviewer shows up and asks me to put everything into one line again since they prefer more compact code ;-)

Quoted 11 lines

I'd say:

- show_netdevs
_ get nic models
- show nic models

instead of:

- get nic models
- show netdevs
- show nic models

I get your point, and I would immediately agree with you if we were allowed to do:

Quoted 8 lines
         show_netdevs();
         printf("\n");
         GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
         qemu_show_nic_models(type, (const char **)nic_models->pdata);
         g_ptr_array_free(nic_models, true);

Although this is possible nowadays (since we're using not C89 anymore), it's against the QEMU coding style.

It seems you haven't seen the next message I sent. You can write exactly the code above, with valid C89 syntax,

which is:

show_netdevs();
printf("\n");
{
GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE); qemu_show_nic_models(type, (const char **)nic_models->pdata); g_ptr_array_free(nic_models, true);
}

Quoted 10 lines

So it's a trade-off now - use two lines of code and have some more chronological code flow, or use one line of more compact code.

I'm in favor of the more compact code. So please let's stop bike-shedding now.

Thanks,
Thomas

Ah, you don't like it, I see. Well.

Thanks,

Claudio