Guest announce feature emulation using Shadow VirtQueue
cover Guest announce feature emulation using Shadow VirtQueue
A gratuitous ARP is recommended after a live migration to reduce the amount of time needed by the network links to be aware of the new location. A hypervisor may not have the knowledge of the guest network configuration, and this is especially true on passthrough devices, so its simpler to ask the guest to do it.
However, the device control part of this feature can be totally emulated by qemu and shadow virtqueue, not needing any special feature from the actual vdpa device.
Sending as RFC for the moment as VIRTIO_NET_F_STATUS is also needed for the guest to access the status of virtio net config to check if an announcement is needed.
Eugenio Pérez (5):
vdpa: Save emulated features list in vhost_vdpa
vdpa: Remove shadow CVQ command check
vdpa: handle VIRTIO_NET_CTRL_ANNOUNCE in
vhost_vdpa_net_handle_ctrl_avail
vhost_net: return VIRTIO_NET_S_ANNOUNCE is device model has it set
vdpa: Offer VIRTIO_NET_F_GUEST_ANNOUNCE feature if SVQ is enabled
include/hw/virtio/vhost-vdpa.h | 2 + hw/net/vhost_net.c | 30 ++++++++++++++- hw/virtio/vhost-vdpa.c | 8 ++-- net/vhost-vdpa.c | 70 +++++++++------------------------- 4 files changed, 54 insertions(+), 56 deletions(-)
Signature · 1 line
2.31.1
RFC PATCH 1/5 vdpa: Save emulated features list in vhost_vdpa
At this moment only _F_LOG is added there.
However future patches add features that depend on the kind of device. In particular, only net devices can add VIRTIO_F_GUEST_ANNOUNCE. So let's allow vhost_vdpa creator to set custom emulated device features.
--- include/hw/virtio/vhost-vdpa.h | 2 ++ hw/virtio/vhost-vdpa.c | 8 ++++---- net/vhost-vdpa.c | 4 ++++ 3 files changed, 10 insertions(+), 4 deletions(-)
include/hw/virtio/vhost-vdpa.h +2 -0
@@ -31,6 +31,8 @@ typedef struct vhost_vdpa {bool iotlb_batch_begin_sent;MemoryListener listener;struct vhost_vdpa_iova_range iova_range; +/* VirtIO device features that can be emulated by qemu */ +uint64_t added_features;uint64_t acked_features;bool shadow_vqs_enabled;/* IOVA mapping used by the Shadow Virtqueue */
hw/virtio/vhost-vdpa.c +4 -4
@@ -660,8 +660,8 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev, v->acked_features = features; -/* We must not ack _F_LOG if SVQ is enabled */ - features &= ~BIT_ULL (VHOST_F_LOG_ALL ); +/* Do not ack features emulated by qemu */ + features &= ~v->added_features; }trace_vhost_vdpa_set_features (dev, features);
@@ -1244,8 +1244,8 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,int ret =vhost_vdpa_get_dev_features (dev, features);if (ret ==0 && v->shadow_vqs_enabled) { -/* Add SVQ logging capabilities */ - *features |=BIT_ULL (VHOST_F_LOG_ALL ); +/* Add emulated capabilities */ + *features |= v->added_features; }return ret;
net/vhost-vdpa.c +4 -0
@@ -600,6 +600,10 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.index = queue_pair_index; s->vhost_vdpa.shadow_vqs_enabled = svq; s->vhost_vdpa.iova_tree = iova_tree; +if (svq) { +/* Add SVQ logging capabilities */ + s->vhost_vdpa.added_features |=BIT_ULL (VHOST_F_LOG_ALL ); + }if (!is_datapath) { s->cvq_cmd_out_buffer =qemu_memalign (qemu_real_host_page_size (),vhost_vdpa_net_cvq_cmd_page_len ());
Signature · 1 line
2.31.1
RFC PATCH 2/5 vdpa: Remove shadow CVQ command check
In reply to Eugenio PérezThe guest will see undefined behavior if it issue not negotiate commands, bit it is expected somehow.
Simplify code deleting this check.
--- net/vhost-vdpa.c | 48 ------------------------------------------------ 1 file changed, 48 deletions(-)
net/vhost-vdpa.c +0 -48
@@ -462,48 +462,6 @@ static NetClientInfo net_vhost_vdpa_cvq_info = { .check_peer_type = vhost_vdpa_check_peer_type, }; -/** -* Do not forward commands not supported by SVQ. Otherwise, the device could -* accept it and qemu would not know how to update the device model. -*/ -static bool vhost_vdpa_net_cvq_validate_cmd (const void *out_buf,size_t len) -{ -struct virtio_net_ctrl_hdr ctrl; - -if (unlikely (len <sizeof (ctrl))) { -qemu_log_mask (LOG_GUEST_ERROR , -"%s: invalid legnth of out buffer %zu\n" , __func__, len); -return false; - } - -memcpy (&ctrl, out_buf,sizeof (ctrl)); -switch (ctrl.class) { -case VIRTIO_NET_CTRL_MAC : -switch (ctrl.cmd) { -case VIRTIO_NET_CTRL_MAC_ADDR_SET : -return true; -default : -qemu_log_mask (LOG_GUEST_ERROR ,"%s: invalid mac cmd %u\n" , - __func__, ctrl.cmd); - }; -break ; -case VIRTIO_NET_CTRL_MQ : -switch (ctrl.cmd) { -case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET : -return true; -default : -qemu_log_mask (LOG_GUEST_ERROR ,"%s: invalid mq cmd %u\n" , - __func__, ctrl.cmd); - }; -break ; -default : -qemu_log_mask (LOG_GUEST_ERROR ,"%s: invalid control class %u\n" , - __func__, ctrl.class); - }; - -return false; -} -/** * Validate and copy control virtqueue commands. *
@@ -527,16 +485,10 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq, .iov_len = sizeof(status), };ssize_t dev_written = -EINVAL ; -bool ok; out.iov_len =iov_to_buf (elem->out_sg, elem->out_num,0 , s->cvq_cmd_out_buffer,vhost_vdpa_net_cvq_cmd_len ()); - ok =vhost_vdpa_net_cvq_validate_cmd (s->cvq_cmd_out_buffer, out.iov_len); -if (unlikely (!ok)) { - goto out; - } - dev_written =vhost_vdpa_net_cvq_add (s, out.iov_len,sizeof (status));if (unlikely (dev_written <0 )) { goto out;
Signature · 1 line
2.31.1
RFC PATCH 3/5 vdpa: handle VIRTIO_NET_CTRL_ANNOUNCE in vhost_vdpa_net_handle_ctrl_avail
In reply to Eugenio PérezSince this capability is emulated by qemu shadowed CVQ cannot forward it to the device. Process all that command within qemu.
--- net/vhost-vdpa.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
net/vhost-vdpa.c +12 -3
@@ -489,9 +489,18 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq, out.iov_len =iov_to_buf (elem->out_sg, elem->out_num,0 , s->cvq_cmd_out_buffer,vhost_vdpa_net_cvq_cmd_len ()); - dev_written =vhost_vdpa_net_cvq_add (s, out.iov_len,sizeof (status)); -if (unlikely (dev_written <0 )) { - goto out; +if (*(uint8_t *)s->cvq_cmd_out_buffer ==VIRTIO_NET_CTRL_ANNOUNCE ) { +/* +* Guest announce capability is emulated by qemu, so dont forward to +* the device. +*/ + dev_written =sizeof (status); + *s->status =VIRTIO_NET_OK ; + }else { + dev_written =vhost_vdpa_net_cvq_add (s, out.iov_len,sizeof (status)); +if (unlikely (dev_written <0 )) { + goto out; + } }if (unlikely (dev_written <sizeof (status))) {
Signature · 1 line
2.31.1
RFC PATCH 4/5 vhost_net: return VIRTIO_NET_S_ANNOUNCE is device model has it set
In reply to Eugenio PérezTemporal, as this affects other vhost backends and we must check status feature first.
--- hw/net/vhost_net.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
hw/net/vhost_net.c +29 -1
@@ -117,7 +117,35 @@ uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)int vhost_net_get_config (struct vhost_net *net,uint8_t *config,uint32_t config_len) { -return vhost_dev_get_config (&net->dev, config, config_len,NULL ); +VirtIODevice *vdev; +VirtIONet *n; +int r =vhost_dev_get_config (&net->dev, config, config_len,NULL ); + +if (unlikely (r !=0 )) { +return r; + } + + if (config_len < endof(struct virtio_net_config, status)) { +return 0 ; + } + +/* +* TODO: To check for VIRTIO_NET_F_STATUS too. +* TODO: Perform this only if vhost_vdpa. +*/ + vdev = net->dev.vdev; +if (!vdev) { +/* Device is starting */ +return 0 ; + } +if (!(net->dev.acked_features &BIT_ULL (VIRTIO_NET_F_GUEST_ANNOUNCE ))) { +return 0 ; + } + + n =VIRTIO_NET (vdev); + ((struct virtio_net_config *)config)->status |= + (n->status &VIRTIO_NET_S_ANNOUNCE ); +return 0 ; }int vhost_net_set_config (struct vhost_net *net,const uint8_t *data,uint32_t offset,uint32_t size,uint32_t flags)
Signature · 1 line
2.31.1
RFC PATCH 5/5 vdpa: Offer VIRTIO_NET_F_GUEST_ANNOUNCE feature if SVQ is enabled
In reply to Eugenio Pérez--- net/vhost-vdpa.c | 3 +++ 1 file changed, 3 insertions(+)
net/vhost-vdpa.c +3 -0
@@ -564,6 +564,9 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,if (svq) {/* Add SVQ logging capabilities */ s->vhost_vdpa.added_features |=BIT_ULL (VHOST_F_LOG_ALL ); + +/* We can emulate guest announce shadowing CVQ */ + s->vhost_vdpa.added_features |=BIT_ULL (VIRTIO_NET_F_GUEST_ANNOUNCE ); }if (!is_datapath) { s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
Signature · 1 line
2.31.1