Skip to contentSkip to conversation

virtio-net: Convert feature properties to OnOffAuto

PATCH Superseded Akihiko Odaki 2 participants 10 messages Jul 14, 2024 – Jul 16, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover virtio-net: Convert feature properties to OnOffAuto

Some features are not always available, and virtio-net used to disable them when not available even if the corresponding properties were explicitly set to "on".

Convert feature properties to OnOffAuto so that the user can explicitly tell QEMU to automatically select the value by setting them "auto". QEMU will give an error if they are set "on".

---
Changes in v3:
- Added patch "qdev-properties: Accept bool for OnOffAuto".
(Michael S. Tsirkin and Daniel P. Berrangé)
- Changed to report errors with ebpf-rss-fds (Daniel P. Berrangé)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Added patch "virtio-net: Remove fallback from ebpf-rss-fds".
- Added a compatibility property.
- Corrected property type name.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (5):
qdev-properties: Accept bool for OnOffAuto
qdev-properties: Add DEFINE_PROP_ON_OFF_AUTO_BIT64()
virtio-net: Convert feature properties to OnOffAuto
virtio-net: Report RSS warning at device realization
virtio-net: Remove fallback from ebpf-rss-fds

 include/hw/qdev-properties.h   |  18 +++
 include/hw/virtio/virtio-net.h |   3 +-
 hw/core/machine.c              |   1 +
 hw/core/qdev-properties.c      |  83 +++++++++++-
 hw/net/virtio-net.c            | 290 ++++++++++++++++++++++++-----------------
 5 files changed, 274 insertions(+), 121 deletions(-)

---
base-commit: f2cb4026fccfe073f84a4b440e41d3ed0c3134f6
change-id: 20240428-auto-be0dc010dda5

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v3 1/5 qdev-properties: Accept bool for OnOffAuto

Accept bool literals for OnOffAuto properties for consistency with bool properties.

---
 hw/core/qdev-properties.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +16 -1
index 86a583574dd0..f0a270bb4f61 100644
@@ -491,6 +491,21 @@ const PropertyInfo qdev_prop_string = {
     .set   = set_string,
 };
 
+static void set_on_off_auto(Object *obj, Visitor *v, const char *name,
+                            void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    int *ptr = object_field_prop_ptr(obj, prop);
+    bool value;
+
+    if (visit_type_bool(v, name, &value, NULL)) {
+        *ptr = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
+        return;
+    }
+
+    qdev_propinfo_set_enum(obj, v, name, opaque, errp);
+}
+
 /* --- on/off/auto --- */
 
 const PropertyInfo qdev_prop_on_off_auto = {
@@ -498,7 +513,7 @@ const PropertyInfo qdev_prop_on_off_auto = {
     .description = "on/off/auto",
     .enum_table = &OnOffAuto_lookup,
     .get = qdev_propinfo_get_enum,
-    .set = qdev_propinfo_set_enum,
+    .set = set_on_off_auto,
     .set_default_value = qdev_propinfo_set_default_value_enum,
 };
 
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH v3 1/5] qdev-properties: Accept bool for OnOffAuto

Quoted 20 lines · Akihiko Odaki

Accept bool literals for OnOffAuto properties for consistency with bool properties.

---
 hw/core/qdev-properties.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +4 -0
index 86a583574dd0..f0a270bb4f61 100644
@@ -491,6 +491,21 @@ const PropertyInfo qdev_prop_string = {
     .set   = set_string,
 };
 
+static void set_on_off_auto(Object *obj, Visitor *v, const char *name,
+                            void *opaque, Error **errp)
+{
+    Property *prop = opaque;

hw/core/qdev-properties.c const PropertyInfo qdev_prop_string = { · line 507

Quoted 10 lines
hw/core/qdev-properties.c +10 -0
+    int *ptr = object_field_prop_ptr(obj, prop);
+    bool value;
+
+    if (visit_type_bool(v, name, &value, NULL)) {
+        *ptr = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
+        return;
+    }
+
+    qdev_propinfo_set_enum(obj, v, name, opaque, errp);
+}

IMHO this is highly undesirable. It is adding redundant new syntax across countless places in QEMU that use OnOffAuto.

Quoted 17 lines
hw/core/qdev-properties.c +2 -1
+
 /* --- on/off/auto --- */
 
 const PropertyInfo qdev_prop_on_off_auto = {
@@ -498,7 +513,7 @@ const PropertyInfo qdev_prop_on_off_auto = {
     .description = "on/off/auto",
     .enum_table = &OnOffAuto_lookup,
     .get = qdev_propinfo_get_enum,
-    .set = qdev_propinfo_set_enum,
+    .set = set_on_off_auto,
     .set_default_value = qdev_propinfo_set_default_value_enum,
 };
 
Signature · 2 lines
-- 
2.45.2

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#3 lore raw

Re: [PATCH v3 1/5] qdev-properties: Accept bool for OnOffAuto

Quoted 26 lines · Daniel P. Berrangé
Quoted 25 lines · Akihiko Odaki

Accept bool literals for OnOffAuto properties for consistency with bool properties.

---
  hw/core/qdev-properties.c | 17 ++++++++++++++++-
  1 file changed, 16 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +9 -0
index 86a583574dd0..f0a270bb4f61 100644
@@ -491,6 +491,21 @@ const PropertyInfo qdev_prop_string = {
      .set   = set_string,
  };
 
+static void set_on_off_auto(Object *obj, Visitor *v, const char *name,
+                            void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    int *ptr = object_field_prop_ptr(obj, prop);
+    bool value;
+
+    if (visit_type_bool(v, name, &value, NULL)) {
+        *ptr = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
Quoted 8 lines

hw/core/qdev-properties.c const PropertyInfo qdev_prop_string = { · line 507

Quoted 5 lines
hw/core/qdev-properties.c +5 -0
+        return;
+    }
+
+    qdev_propinfo_set_enum(obj, v, name, opaque, errp);
+}

IMHO this is highly undesirable. It is adding redundant new syntax across countless places in QEMU that use OnOffAuto.

We already embrace the redundant syntax for bool properties and do not say it is deprecated. This is the most consistent way of handling on/off I came up with.

Regards,
Akihiko Odaki

#4 lore raw

PATCH v3 2/5 qdev-properties: Add DEFINE_PROP_ON_OFF_AUTO_BIT64()

In reply to Akihiko Odaki

DEFINE_PROP_ON_OFF_AUTO_BIT64() corresponds to DEFINE_PROP_ON_OFF_AUTO() as DEFINE_PROP_BIT64() corresponds to DEFINE_PROP_BOOL(). The difference is that DEFINE_PROP_ON_OFF_AUTO_BIT64() exposes OnOffAuto instead of bool.

---
 include/hw/qdev-properties.h | 18 ++++++++++++
 hw/core/qdev-properties.c    | 66 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 83 insertions(+), 1 deletion(-)
include/hw/qdev-properties.h +18 -0
index 09aa04ca1e27..afec53a48470 100644
@@ -43,11 +43,22 @@ struct PropertyInfo {
     ObjectPropertyRelease *release;
 };
 
+/**
+ * struct OnOffAutoBit64 - OnOffAuto storage with 64 elements.
+ * @on_bits: Bitmap of elements with "on".
+ * @auto_bits: Bitmap of elements with "auto".
+ */
+typedef struct OnOffAutoBit64 {
+    uint64_t on_bits;
+    uint64_t auto_bits;
+} OnOffAutoBit64;
+
 
 /*** qdev-properties.c ***/
 
 extern const PropertyInfo qdev_prop_bit;
 extern const PropertyInfo qdev_prop_bit64;
+extern const PropertyInfo qdev_prop_on_off_auto_bit64;
 extern const PropertyInfo qdev_prop_bool;
 extern const PropertyInfo qdev_prop_enum;
 extern const PropertyInfo qdev_prop_uint8;
@@ -100,6 +111,13 @@ extern const PropertyInfo qdev_prop_link;
                 .set_default = true,                              \
                 .defval.u  = (bool)_defval)
 
+#define DEFINE_PROP_ON_OFF_AUTO_BIT64(_name, _state, _field, _bit, _defval) \
+    DEFINE_PROP(_name, _state, _field, qdev_prop_on_off_auto_bit64,         \
+                OnOffAutoBit64,                                             \
+                .bitnr    = (_bit),                                         \
+                .set_default = true,                                        \
+                .defval.i = (OnOffAuto)_defval)
+
 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval)     \
     DEFINE_PROP(_name, _state, _field, qdev_prop_bool, bool, \
                 .set_default = true,                         \
hw/core/qdev-properties.c +65 -1

78 lines not on this page · raw message

Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH v3 2/5] qdev-properties: Add DEFINE_PROP_ON_OFF_AUTO_BIT64()

Quoted 4 lines · Akihiko Odaki

DEFINE_PROP_ON_OFF_AUTO_BIT64() corresponds to DEFINE_PROP_ON_OFF_AUTO() as DEFINE_PROP_BIT64() corresponds to DEFINE_PROP_BOOL(). The difference is that DEFINE_PROP_ON_OFF_AUTO_BIT64() exposes OnOffAuto instead of bool.

IMHO this shouldn't be implemented in terms of On/Off auto, as it is misleadingly accepting much more than PROP_ON_OFF accepts. Rather it should be just DEFINE_PROP_AUTO_BIT64, implemented in terms of 'bool', with an extra 'auto' value.

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#6 lore raw

Re: [PATCH v3 2/5] qdev-properties: Add DEFINE_PROP_ON_OFF_AUTO_BIT64()

Quoted 10 lines · Daniel P. Berrangé
Quoted 4 lines · Akihiko Odaki

DEFINE_PROP_ON_OFF_AUTO_BIT64() corresponds to DEFINE_PROP_ON_OFF_AUTO() as DEFINE_PROP_BIT64() corresponds to DEFINE_PROP_BOOL(). The difference is that DEFINE_PROP_ON_OFF_AUTO_BIT64() exposes OnOffAuto instead of bool.

IMHO this shouldn't be implemented in terms of On/Off auto, as it is misleadingly accepting much more than PROP_ON_OFF accepts. Rather it should be just DEFINE_PROP_AUTO_BIT64, implemented in terms of 'bool', with an extra 'auto' value.

I defined it in a way consistent with DEFINE_PROP_ON_OFF_AUTO(). A union of 'bool' and 'auto' would have a behavior somewhat inconsistent with DEFINE_PROP_ON_OFF_AUTO().

Regards,
Akihiko Odaki

#7 lore raw

PATCH v3 3/5 virtio-net: Convert feature properties to OnOffAuto

In reply to Akihiko Odaki

Some features are not always available, and virtio-net used to disable them when not available even if the corresponding properties were explicitly set to "on".

Convert feature properties to OnOffAuto so that the user can explicitly tell QEMU to automatically select the value by setting them "auto". QEMU will give an error if they are set "on".

Convert "on" to "auto" when using an old machine for compatbility.

---
 include/hw/virtio/virtio-net.h |   3 +-
 hw/core/machine.c              |   1 +
 hw/net/virtio-net.c            | 254 +++++++++++++++++++++++++----------------
 3 files changed, 161 insertions(+), 97 deletions(-)
include/hw/virtio/virtio-net.h +2 -1
index 060c23c04d2d..cfa87f739248 100644
@@ -178,7 +178,7 @@ struct VirtIONet {
     uint32_t has_vnet_hdr;
     size_t host_hdr_len;
     size_t guest_hdr_len;
-    uint64_t host_features;
+    OnOffAutoBit64 host_features;
     uint32_t rsc_timeout;
     uint8_t rsc4_enabled;
     uint8_t rsc6_enabled;
@@ -218,6 +218,7 @@ struct VirtIONet {
     /* primary failover device is hidden*/
     bool failover_primary_hidden;
     bool failover;
+    bool force_features_auto;
     DeviceListener primary_listener;
     QDict *primary_opts;
     bool primary_opts_from_json;
hw/core/machine.c +1 -0
index f4cba6496c84..783d3e5695d8 100644
@@ -39,6 +39,7 @@ GlobalProperty hw_compat_9_0[] = {
     {"scsi-disk-base", "migrate-emulated-scsi-request", "false" },
     {"vfio-pci", "skip-vsc-check", "false" },
     { "virtio-pci", "x-pcie-pm-no-soft-reset", "off" },
+    { TYPE_VIRTIO_NET, "x-force-features-auto", "on" },
 };
 const size_t hw_compat_9_0_len = G_N_ELEMENTS(hw_compat_9_0);
 
hw/net/virtio-net.c +158 -96

332 lines not on this page · raw message

Signature · 1 line
2.45.2
#8 lore raw

PATCH v3 4/5 virtio-net: Report RSS warning at device realization

In reply to Akihiko Odaki

Warning about RSS fallback at device realization allows the user to notice the configuration problem early.

---
 hw/net/virtio-net.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
hw/net/virtio-net.c +6 -8
index bd285921d507..e779ba2df428 100644
@@ -822,6 +822,10 @@ static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
 
     if (!get_vhost_net(nc->peer)) {
         if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
+            if (on_off_auto_features.on_bits & VIRTIO_NET_F_RSS) {
+                warn_report("Can't load eBPF RSS - fallback to software RSS");
+            }
+
             virtio_clear_feature(&on_off_auto_features.auto_bits,
                                  VIRTIO_NET_F_RSS);
         }
@@ -1332,16 +1336,10 @@ static void virtio_net_detach_epbf_rss(VirtIONet *n)
 static void virtio_net_commit_rss_config(VirtIONet *n)
 {
     if (n->rss_data.enabled) {
-        n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
+        n->rss_data.enabled_software_rss = n->rss_data.populate_hash ||
+                                           !virtio_net_attach_epbf_rss(n);
         if (n->rss_data.populate_hash) {
             virtio_net_detach_epbf_rss(n);
-        } else if (!virtio_net_attach_epbf_rss(n)) {
-            if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
-                warn_report("Can't load eBPF RSS for vhost");
-            } else {
-                warn_report("Can't load eBPF RSS - fallback to software RSS");
-                n->rss_data.enabled_software_rss = true;
-            }
         }
 
         trace_virtio_net_rss_enable(n->rss_data.hash_types,
Signature · 1 line
2.45.2
#9 lore raw

PATCH v3 5/5 virtio-net: Remove fallback from ebpf-rss-fds

In reply to Akihiko Odaki

If ebpf-rss-fds is specified but we fail to use, we should not fall back to loading eBPF programs by ourselves as such makes the situation complicated.

---
 hw/net/virtio-net.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
hw/net/virtio-net.c +9 -15
index e779ba2df428..2f0a4f2aabf7 100644
@@ -1361,21 +1361,21 @@ static void virtio_net_disable_rss(VirtIONet *n)
     virtio_net_commit_rss_config(n);
 }
 
-static bool virtio_net_load_ebpf_fds(VirtIONet *n)
+static bool virtio_net_load_ebpf_fds(VirtIONet *n, Error **errp)
 {
     int fds[EBPF_RSS_MAX_FDS] = { [0 ... EBPF_RSS_MAX_FDS - 1] = -1};
     int ret = true;
     int i = 0;
 
     if (n->nr_ebpf_rss_fds != EBPF_RSS_MAX_FDS) {
-        warn_report("Expected %d file descriptors but got %d",
-                    EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds);
+        error_setg(errp, "Expected %d file descriptors but got %d",
+                   EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds);
        return false;
    }
 
     for (i = 0; i < n->nr_ebpf_rss_fds; i++) {
         fds[i] = monitor_fd_param(monitor_cur(), n->ebpf_rss_fds[i],
-                                  &error_warn);
+                                  errp);
         if (fds[i] < 0) {
             ret = false;
             goto exit;
@@ -1394,17 +1394,11 @@ exit:
     return ret;
 }
 
-static bool virtio_net_load_ebpf(VirtIONet *n)
+static bool virtio_net_load_ebpf(VirtIONet *n, Error **errp)
 {
-    bool ret = false;
-
-    if (virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
-        if (!(n->ebpf_rss_fds && virtio_net_load_ebpf_fds(n))) {
-            ret = ebpf_rss_load(&n->ebpf_rss);
-        }
-    }
-
-    return ret;
+    return virtio_net_attach_ebpf_to_backend(n->nic, -1) &&
+           (n->ebpf_rss_fds ? virtio_net_load_ebpf_fds(n, errp) :
+                              ebpf_rss_load(&n->ebpf_rss));
 }
 
 static void virtio_net_unload_ebpf(VirtIONet *n)
@@ -3805,7 +3799,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     if (virtio_has_feature(n->host_features.on_bits |
                            n->host_features.auto_bits,
                            VIRTIO_NET_F_RSS)) {
-        virtio_net_load_ebpf(n);
+        virtio_net_load_ebpf(n, errp);
     }
 }
 
Signature · 1 line
2.45.2