Skip to contentSkip to conversation

Fix the "-nic help" option

PATCH In review Thomas Huth 2 participants 6 messages Nov 10, 2022 – Dec 16, 2022 Networking

lore
Share & tools
lore
#1 lore raw

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

In reply to 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 | 14 ++++++++++++++
 hw/pci/pci.c      | 29 +----------------------------
 net/net.c         | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 28 deletions(-)
include/net/net.h +14 -0
index 3db75ff841..31d9b980c7 100644
@@ -201,6 +201,20 @@ void net_socket_rs_init(SocketReadState *rs,
                         bool vnet_hdr);
 NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
 
+/**
+ * qemu_get_nic_models:
+ * @device_type: Defines which devices should be taken into consideration
+ *               (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI)
+ *
+ * Get an array of pointers to names of NIC devices that are available in
+ * the QEMU binary. The array is terminated with a NULL pointer entry.
+ * The caller is responsible for freeing the memory when it is not required
+ * anymore, e.g. with g_ptr_array_free(..., true).
+ *
+ * Returns: Pointer to the array that contains the pointers to the names.
+ */
+GPtrArray *qemu_get_nic_models(const char *device_type);
+
 /* NIC info */
 
 #define MAX_NICS 8
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 +34 -0
index 840ad9dca5..49cced691f 100644
@@ -899,6 +899,40 @@ static int nic_get_free_idx(void)
     return -1;
 }
 
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+    GPtrArray *nic_models = g_ptr_array_new();
+    GSList *list = object_class_get_list_sorted(device_type, false);
+
+    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
#2 lore raw

PATCH v2 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 49cced691f..cdd3ebc515 100644
@@ -1569,8 +1569,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
#3 lore raw

PATCH v2 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 cdd3ebc515..5d96c8858f 100644
@@ -941,7 +941,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
#0 lore raw

v2 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.

v2:
- Add function comment in the first patch
- Add Reviewed-by in the third patch

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 | 14 +++++++++++++
 hw/pci/pci.c      | 29 +--------------------------
 net/net.c         | 50 ++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 62 insertions(+), 31 deletions(-)
Signature · 1 line
2.31.1
#4 lore raw
Quoted 15 lines · Thomas Huth

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.

v2:
- Add function comment in the first patch
- Add Reviewed-by in the third patch

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"

Quoted 5 lines
  include/net/net.h | 14 +++++++++++++
  hw/pci/pci.c      | 29 +--------------------------
  net/net.c         | 50 ++++++++++++++++++++++++++++++++++++++++++++---
  3 files changed, 62 insertions(+), 31 deletions(-)

Friendly ping!

Thomas

#5 lore raw
Quoted 17 lines · Thomas Huth
Quoted 15 lines · Thomas Huth

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.

v2:
- Add function comment in the first patch
- Add Reviewed-by in the third patch

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"

Quoted 7 lines
Quoted 5 lines
  include/net/net.h | 14 +++++++++++++
  hw/pci/pci.c      | 29 +--------------------------
  net/net.c         | 50 ++++++++++++++++++++++++++++++++++++++++++++---
  3 files changed, 62 insertions(+), 31 deletions(-)

Friendly ping!

I've queued this series.

Thanks

Quoted 3 lines

Thomas