vDPA-net inflight descriptors migration with SVQ
cover vDPA-net inflight descriptors migration with SVQ
The state of the descriptors (avail or used) may not be recoverable just looking at the guest memory. Out of order used descriptor may override previous avail ones in the descriptor table or avail vring.
Currently we're not migrating this status in net devices because virtio-net, vhost-kernel etc use the descriptors in order, so the information always recoverable from guest's memory. However, vDPA devices may use them out of order, and other kind of devices like block need this support.
Shadow virtqueue is able to track these and resend them at the destination. Add them to the virtio-net migration description so they are not lose in the process.
This is a very early RFC just to validate the first draft so expect leftovers. To fetch and request the descriptors from a device without SVQ need to be implemented on top. Some other notable pending items are:
* Do not send the descriptors actually recoverable from the guest memory.
* Properly version the migrate data.
* Properly abstract the descriptors access from virtio-net to SVQ.
* Do not use VirtQueueElementOld but migrate directly VirtQueueElement.
* Replace lots of assertions with runtime conditionals.
* Other TODOs in the patch message or code changes.
Thanks.
Eugenio Pérez (13):
vhost: add available descriptor list in SVQ
vhost: iterate only available descriptors at SVQ stop
vhost: merge avail list and next avail descriptors detach
vhost: add vhost_svq_save_inflight
virtio: Specify uint32_t as VirtQueueElementOld members type
virtio: refactor qemu_get_virtqueue_element
virtio: refactor qemu_put_virtqueue_element
virtio: expose VirtQueueElementOld
virtio: add vmstate_virtqueue_element_old
virtio-net: Migrate vhost inflight descriptors
virtio-net: save inflight descriptors at vhost shutdown
vhost: expose vhost_svq_add_element
vdpa: Recover inflight descriptors
hw/virtio/vhost-shadow-virtqueue.h | 9 ++ include/hw/virtio/virtio-net.h | 2 + include/hw/virtio/virtio.h | 32 ++++++ include/migration/vmstate.h | 22 ++++ hw/net/vhost_net.c | 56 ++++++++++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 52 +++++++-- hw/virtio/vhost-vdpa.c | 11 -- hw/virtio/virtio.c | 162 ++++++++++++++++++----------- 9 files changed, 392 insertions(+), 83 deletions(-)
Signature · 1 line
2.31.1
RFC PATCH 1/13 vhost: add available descriptor list in SVQ
This helps to track the inflight buffers, make easier to transverse all of them, and return them in order.
--- hw/virtio/vhost-shadow-virtqueue.h | 6 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 5 +++++ 2 files changed, 11 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +6 -0
@@ -23,6 +23,9 @@ typedef struct SVQDescState {* guest's */ unsigned int ndescs; + +/* List to save or free inflight descriptors */ +QTAILQ_ENTRY (SVQDescState) entry; } SVQDescState;typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;
@@ -81,6 +84,9 @@ typedef struct VhostShadowVirtqueue {/* SVQ vring descriptors state */ SVQDescState *desc_state; +/* Linked list to follow avail descriptors */ + QTAILQ_HEAD(, SVQDescState) desc_state_avail; +/* Next VirtQueue element that guest made available */ VirtQueueElement *next_guest_avail_elem;
hw/virtio/vhost-shadow-virtqueue.c +5 -0
@@ -265,6 +265,8 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, svq->desc_state[qemu_head].elem = elem; svq->desc_state[qemu_head].ndescs = ndescs; +QTAILQ_INSERT_TAIL (&svq->desc_state_avail, &svq->desc_state[qemu_head], + entry);vhost_svq_kick (svq);return 0 ; }
@@ -451,6 +453,8 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq, svq->free_head = used_elem.id; *len = used_elem.len; +QTAILQ_REMOVE (&svq->desc_state_avail, &svq->desc_state[used_elem.id], + entry);return g_steal_pointer (&svq->desc_state[used_elem.id].elem); }
@@ -665,6 +669,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, svq->vring.used =qemu_memalign (qemu_real_host_page_size (), device_size);memset (svq->vring.used,0 , device_size); svq->desc_state =g_new0 (SVQDescState, svq->vring.num); +QTAILQ_INIT (&svq->desc_state_avail); svq->desc_next =g_new0 (uint16_t, svq->vring.num);for (unsigned i =0 ; i < svq->vring.num -1 ; i++) { svq->desc_next[i] =cpu_to_le16 (i +1 );
Signature · 1 line
2.31.1
RFC PATCH 2/13 vhost: iterate only available descriptors at SVQ stop
In reply to Eugenio PérezWhile we're at it, simplify the free path making just transverse the list instead of all of them.
--- hw/virtio/vhost-shadow-virtqueue.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +6 -5
@@ -692,12 +692,13 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/* Send all pending used descriptors to guest */ vhost_svq_flush (svq, false); -for (unsigned i =0 ; i < svq->vring.num; ++i) { +while (!QTAILQ_EMPTY (&svq->desc_state_avail)) { +SVQDescState *s =QTAILQ_FIRST (&svq->desc_state_avail); g_autofree VirtQueueElement *elem = NULL; - elem =g_steal_pointer (&svq->desc_state[i].elem); -if (elem) { -virtqueue_detach_element (svq->vq, elem,0 ); - } + + elem =g_steal_pointer (&s->elem); +virtqueue_detach_element (svq->vq, elem,0 ); +QTAILQ_REMOVE (&svq->desc_state_avail, s, entry); } next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem);
Signature · 1 line
2.31.1
RFC PATCH 3/13 vhost: merge avail list and next avail descriptors detach
In reply to Eugenio PérezWe need to perform the same actions for the two.
--- hw/virtio/vhost-shadow-virtqueue.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +9 -9
@@ -692,18 +692,18 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/* Send all pending used descriptors to guest */ vhost_svq_flush (svq, false); -while (!QTAILQ_EMPTY (&svq->desc_state_avail)) { +while (!QTAILQ_EMPTY (&svq->desc_state_avail) + || svq->next_guest_avail_elem) {SVQDescState *s =QTAILQ_FIRST (&svq->desc_state_avail); g_autofree VirtQueueElement *elem = NULL; - elem =g_steal_pointer (&s->elem); -virtqueue_detach_element (svq->vq, elem,0 ); -QTAILQ_REMOVE (&svq->desc_state_avail, s, entry); - } - - next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); -if (next_avail_elem) { -virtqueue_detach_element (svq->vq, next_avail_elem,0 ); + elem =g_steal_pointer (s ? &s->elem : &svq->next_guest_avail_elem); +if (elem) { +virtqueue_detach_element (svq->vq, elem,0 ); + } +if (s) { +QTAILQ_REMOVE (&svq->desc_state_avail, s, entry); + } } svq->vq =NULL ;g_free (svq->desc_next);
Signature · 1 line
2.31.1
RFC PATCH 4/13 vhost: add vhost_svq_save_inflight
In reply to Eugenio PérezThis allows SVQ to return all the inflight descriptors in order.
TODO: Rewind descriptors that can be fetched from avail ring again.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +2 -0
@@ -133,6 +133,8 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev,VirtQueue *vq); +VirtQueueElement **vhost_svq_save_inflight (VhostShadowVirtqueue *svq, +uint32_t *num);void vhost_svq_stop (VhostShadowVirtqueue *svq); VhostShadowVirtqueue *vhost_svq_new(VhostIOVATree *iova_tree,
hw/virtio/vhost-shadow-virtqueue.c +20 -0
@@ -676,6 +676,26 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, } } +VirtQueueElement **vhost_svq_save_inflight (VhostShadowVirtqueue *svq, +uint32_t *num) +{ +GPtrArray *aret =g_ptr_array_new (); +SVQDescState *s, *tmp; + + QTAILQ_FOREACH_SAFE(s, &svq->desc_state_avail, entry, tmp) { +VirtQueueElement *elem = s->elem; + +virtqueue_detach_element (svq->vq, elem,0 ); +g_ptr_array_add (aret, elem); +QTAILQ_REMOVE (&svq->desc_state_avail, s, entry); + } + + *num = aret->len; + +/* TODO: return g_ptr_array_steal(aret) when min glib >= 2.64 */ +return (void *)g_ptr_array_free (aret, false); +} +/** * Stop the shadow virtqueue operation. * @svq: Shadow Virtqueue
Signature · 1 line
2.31.1
RFC PATCH 5/13 virtio: Specify uint32_t as VirtQueueElementOld members type
In reply to Eugenio PérezThis struct will be the one used as a base to serialize and deserialize the virtqueue elements within this RFC. The VMState fields are way more simpler using this struct.
TODO: Use VirtQueueElement to serialize and deserialize inflight elements.
--- hw/virtio/virtio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +3 -3
@@ -2288,9 +2288,9 @@ unsigned int virtqueue_drop_all(VirtQueue *vq)* used before the change. */ typedef struct VirtQueueElementOld { - unsigned int index; - unsigned int out_num; - unsigned int in_num; + uint32_t index; + uint32_t out_num; + uint32_t in_num; hwaddr in_addr[VIRTQUEUE_MAX_SIZE]; hwaddr out_addr[VIRTQUEUE_MAX_SIZE]; struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
Signature · 1 line
2.31.1
RFC PATCH 6/13 virtio: refactor qemu_get_virtqueue_element
In reply to Eugenio PérezThe core of the function is useful to transform from VirtQueueElementOld to VirtQueueElement. Extract from qemu_get_virtqueue_element, and leave there the handling of QEMUFile.
No functional change intended.
--- hw/virtio/virtio.c | 68 ++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
hw/virtio/virtio.c +42 -26
@@ -2297,50 +2297,66 @@ typedef struct VirtQueueElementOld {struct iovec out_sg[VIRTQUEUE_MAX_SIZE ]; } VirtQueueElementOld; -void *qemu_get_virtqueue_element (VirtIODevice *vdev,QEMUFile *f,size_t sz) +/* Convert VirtQueueElementOld to VirtQueueElement */ +static void *qemu_get_virtqueue_element_from_old (VirtIODevice *vdev, +const VirtQueueElementOld *data, +size_t sz) { -VirtQueueElement *elem; -VirtQueueElementOld data; -int i; - -qemu_get_buffer (f, (uint8_t *)&data,sizeof (VirtQueueElementOld)); - -/* TODO: teach all callers that this can fail, and return failure instead -* of asserting here. -* This is just one thing (there are probably more) that must be -* fixed before we can allow NDEBUG compilation. -*/ -assert (ARRAY_SIZE (data.in_addr) >= data.in_num); -assert (ARRAY_SIZE (data.out_addr) >= data.out_num); - - elem =virtqueue_alloc_element (sz, data.out_num, data.in_num); - elem->index = data.index; +VirtQueueElement *elem =virtqueue_alloc_element (sz, data->out_num, + data->in_num); + elem->index = data->index; -for (i =0 ; i < elem->in_num; i++) { - elem->in_addr[i] = data.in_addr[i]; +for (uint16_t i =0 ; i < elem->in_num; i++) { + elem->in_addr[i] = data->in_addr[i]; } -for (i =0 ; i < elem->out_num; i++) { - elem->out_addr[i] = data.out_addr[i]; +for (uint16_t i =0 ; i < elem->out_num; i++) { + elem->out_addr[i] = data->out_addr[i]; } -for (i =0 ; i < elem->in_num; i++) { +for (uint16_t i =0 ; i < elem->in_num; i++) {/* Base is overwritten by virtqueue_map. */ elem->in_sg[i].iov_base =0 ; - elem->in_sg[i].iov_len = data.in_sg[i].iov_len; + elem->in_sg[i].iov_len = data->in_sg[i].iov_len; } -for (i =0 ; i < elem->out_num; i++) { +for (uint16_t i =0 ; i < elem->out_num; i++) {/* Base is overwritten by virtqueue_map. */ elem->out_sg[i].iov_base =0 ; - elem->out_sg[i].iov_len = data.out_sg[i].iov_len; + elem->out_sg[i].iov_len = data->out_sg[i].iov_len; } +virtqueue_map (vdev, elem); +return elem; +} + +static bool vq_element_in_range (void *opaque,int version_id) +{ +VirtQueueElementOld *data = opaque; + +return ARRAY_SIZE (data->in_addr) >= data->in_num && +ARRAY_SIZE (data->out_addr) >= data->out_num; +} + +void *qemu_get_virtqueue_element (VirtIODevice *vdev,QEMUFile *f,size_t sz) +{ +VirtQueueElement *elem; +VirtQueueElementOld data; + +qemu_get_buffer (f, (uint8_t *)&data,sizeof (VirtQueueElementOld)); + +/* TODO: teach all callers that this can fail, and return failure instead +* of asserting here. +* This is just one thing (there are probably more) that must be +* fixed before we can allow NDEBUG compilation. +*/ +assert (vq_element_in_range (&data,0 )); + + elem =qemu_get_virtqueue_element_from_old (vdev, &data, sz);if (virtio_host_has_feature (vdev,VIRTIO_F_RING_PACKED )) {qemu_get_be32s (f, &elem->ndescs); } -virtqueue_map (vdev, elem);return elem; }
Signature · 1 line
2.31.1
RFC PATCH 7/13 virtio: refactor qemu_put_virtqueue_element
In reply to Eugenio PérezThe core of the function is useful to transform from VirtQueueElement to VirtQueueElementOld. Extract these from qemu_put_virtqueue_element, and leave there the handling of QEMUFile.
No functional change intended.
--- hw/virtio/virtio.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)
hw/virtio/virtio.c +26 -19
@@ -2360,36 +2360,43 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)return elem; } -void qemu_put_virtqueue_element (VirtIODevice *vdev,QEMUFile *f, -VirtQueueElement *elem) +/* Convert VirtQueueElement to VirtQueueElementOld */ +static void qemu_put_virtqueue_element_old (const VirtQueueElement *elem, +VirtQueueElementOld *data) { -VirtQueueElementOld data; -int i; +memset (data,0 ,sizeof (*data)); + data->index = elem->index; + data->in_num = elem->in_num; + data->out_num = elem->out_num; -memset (&data,0 ,sizeof (data)); - data.index = elem->index; - data.in_num = elem->in_num; - data.out_num = elem->out_num; - -for (i =0 ; i < elem->in_num; i++) { - data.in_addr[i] = elem->in_addr[i]; +for (int i =0 ; i < elem->in_num; i++) { + data->in_addr[i] = elem->in_addr[i]; } -for (i =0 ; i < elem->out_num; i++) { - data.out_addr[i] = elem->out_addr[i]; +for (int i =0 ; i < elem->out_num; i++) { + data->out_addr[i] = elem->out_addr[i]; } -for (i =0 ; i < elem->in_num; i++) { -/* Base is overwritten by virtqueue_map when loading. Do not -* save it, as it would leak the QEMU address space layout. */ - data.in_sg[i].iov_len = elem->in_sg[i].iov_len; +for (int i =0 ; i < elem->in_num; i++) { +/* +* Base is overwritten by virtqueue_map when loading. Do not +* save it, as it would leak the QEMU address space layout. +*/ + data->in_sg[i].iov_len = elem->in_sg[i].iov_len; } -for (i =0 ; i < elem->out_num; i++) { +for (int i =0 ; i < elem->out_num; i++) {/* Do not save iov_base as above. */ - data.out_sg[i].iov_len = elem->out_sg[i].iov_len; + data->out_sg[i].iov_len = elem->out_sg[i].iov_len; } +} + +void qemu_put_virtqueue_element (VirtIODevice *vdev,QEMUFile *f, +VirtQueueElement *elem) +{ +VirtQueueElementOld data; +qemu_put_virtqueue_element_old (elem, &data);if (virtio_host_has_feature (vdev,VIRTIO_F_RING_PACKED )) {qemu_put_be32s (f, &elem->ndescs); }
Signature · 1 line
2.31.1
RFC PATCH 8/13 virtio: expose VirtQueueElementOld
In reply to Eugenio PérezWe will convert between VirtQueueElement and VirtQueueElementOld to migrate the inflight descriptors, so we need virtio-net to see these.
This will not be exported in the final version, but working with VirtQueueElementOld is way more easier than with VirtQueueElement and VMState macros.
--- include/hw/virtio/virtio.h | 31 +++++++++++++++++++++++++++++++ hw/virtio/virtio.c | 27 +++++---------------------- 2 files changed, 36 insertions(+), 22 deletions(-)
include/hw/virtio/virtio.h +31 -0
@@ -75,6 +75,37 @@ typedef struct VirtQueueElementstruct iovec *out_sg; } VirtQueueElement; +/* +* Reading and writing a structure directly to QEMUFile is *awful*, but +* it is what QEMU has always done by mistake. We can change it sooner +* or later by bumping the version number of the affected vm states. +* In the meanwhile, since the in-memory layout of VirtQueueElement +* has changed, we need to marshal to and from the layout that was +* used before the change. +*/ +typedef struct VirtQueueElementOld { +uint32_t index; +uint32_t out_num; +uint32_t in_num; +hwaddr in_addr[VIRTQUEUE_MAX_SIZE ]; +hwaddr out_addr[VIRTQUEUE_MAX_SIZE ]; +/* Unions help to serialize the descriptor using VMStateDescription */ +union { +struct iovec in_sg[VIRTQUEUE_MAX_SIZE ]; +uint64_t in_sg_64[VIRTQUEUE_MAX_SIZE *2 ]; + }; +union { +struct iovec out_sg[VIRTQUEUE_MAX_SIZE ]; +uint64_t out_sg_64[VIRTQUEUE_MAX_SIZE *2 ]; + }; +}VirtQueueElementOld ; + +void *qemu_get_virtqueue_element_from_old (VirtIODevice *vdev, +const VirtQueueElementOld *data, +size_t sz); +void qemu_put_virtqueue_element_old (const VirtQueueElement *elem, +VirtQueueElementOld *data); +#define VIRTIO_QUEUE_MAX 1024#define VIRTIO_NO_VECTOR 0xffff
hw/virtio/virtio.c +5 -22
@@ -2280,27 +2280,10 @@ unsigned int virtqueue_drop_all(VirtQueue *vq) } } -/* Reading and writing a structure directly to QEMUFile is *awful*, but -* it is what QEMU has always done by mistake. We can change it sooner -* or later by bumping the version number of the affected vm states. -* In the meanwhile, since the in-memory layout of VirtQueueElement -* has changed, we need to marshal to and from the layout that was -* used before the change. -*/ -typedef struct VirtQueueElementOld { -uint32_t index; -uint32_t out_num; -uint32_t in_num; -hwaddr in_addr[VIRTQUEUE_MAX_SIZE ]; -hwaddr out_addr[VIRTQUEUE_MAX_SIZE ]; -struct iovec in_sg[VIRTQUEUE_MAX_SIZE ]; -struct iovec out_sg[VIRTQUEUE_MAX_SIZE ]; -}VirtQueueElementOld ; -/* Convert VirtQueueElementOld to VirtQueueElement */ -static void *qemu_get_virtqueue_element_from_old (VirtIODevice *vdev, -const VirtQueueElementOld *data, -size_t sz) +void *qemu_get_virtqueue_element_from_old (VirtIODevice *vdev, +const VirtQueueElementOld *data, +size_t sz) {VirtQueueElement *elem =virtqueue_alloc_element (sz, data->out_num, data->in_num);
@@ -2361,8 +2344,8 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) }/* Convert VirtQueueElement to VirtQueueElementOld */ -static void qemu_put_virtqueue_element_old (const VirtQueueElement *elem, -VirtQueueElementOld *data) +void qemu_put_virtqueue_element_old (const VirtQueueElement *elem, +VirtQueueElementOld *data) {memset (data,0 ,sizeof (*data)); data->index = elem->index;
Signature · 1 line
2.31.1
RFC PATCH 9/13 virtio: add vmstate_virtqueue_element_old
In reply to Eugenio PérezVMStateDescription of an inflight descriptor represented in VirtQueueElementOld.
TODO: Convert to VirtQueueElement.
--- include/hw/virtio/virtio.h | 1 + include/migration/vmstate.h | 11 +++++++++++ hw/virtio/virtio.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
include/hw/virtio/virtio.h +1 -0
@@ -105,6 +105,7 @@ void *qemu_get_virtqueue_element_from_old(VirtIODevice *vdev, size_t sz);void qemu_put_virtqueue_element_old (const VirtQueueElement *elem,VirtQueueElementOld *data); +extern const VMStateDescription vmstate_virtqueue_element_old;#define VIRTIO_QUEUE_MAX 1024
include/migration/vmstate.h +11 -0
@@ -448,6 +448,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_VARRAY_UINT32_UNSAFE (_field, _state, _field_num, _version, \ + _info, _type) {\ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint32_t),\ + .info = &(_info), \ + .size = sizeof(_type), \ + .flags = VMS_VARRAY_UINT32, \ + .offset = vmstate_offset_varray(_state, _field, _type), \ +} +#define VMSTATE_VSTRUCT_TEST (_field, _state, _test, _version, _vmsd, _type, _struct_version) { \ .name = (stringify(_field)), \ .version_id = (_version), \
hw/virtio/virtio.c +32 -0
@@ -3165,6 +3165,38 @@ static bool virtio_disabled_needed(void *opaque)return vdev->disabled; } +const VMStateDescription vmstate_virtqueue_element_old = { + .name ="virtqueue_element" , + .fields = (VMStateField []) { +VMSTATE_UINT32 (index, VirtQueueElementOld), +VMSTATE_UINT32 (in_num, VirtQueueElementOld), +VMSTATE_UINT32 (out_num, VirtQueueElementOld), +/* +* TODO: Needed for packed +* VMSTATE_UINT16(ndescs, VirtQueueElement), +*/ + +VMSTATE_VALIDATE ("fit" , vq_element_in_range), +VMSTATE_VARRAY_UINT32_UNSAFE (in_addr, VirtQueueElementOld, in_num,0 , + vmstate_info_uint64, hwaddr), +VMSTATE_VARRAY_UINT32_UNSAFE (out_addr, VirtQueueElementOld, out_num,0 , + vmstate_info_uint64, hwaddr), + +/* +* Assume iovec[n] == uint64_t[n*2] +* TODO: Probably need to send each field individually because of +* endianess. +*/ +VMSTATE_VARRAY_MULTIPLY (in_sg_64, VirtQueueElementOld, in_num, +sizeof (struct iovec ) /sizeof (uint64_t ), + vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_MULTIPLY (out_sg_64, VirtQueueElementOld, out_num, +sizeof (struct iovec ) /sizeof (uint64_t ), + vmstate_info_uint64, uint64_t), +VMSTATE_END_OF_LIST () + }, +}; +static const VMStateDescription vmstate_virtqueue = { .name ="virtqueue_state" , .version_id =1 ,
Signature · 1 line
2.31.1
RFC PATCH 10/13 virtio-net: Migrate vhost inflight descriptors
In reply to Eugenio PérezThere is currently no data to be migrated, since nothing populates or read the fields on virtio-net.
The migration of in-flight descriptors is modelled after the migration of requests in virtio-blk. With some differences:
* virtio-blk migrates queue number on each request. Here we only add a vq if it has descriptors to migrate, and then we make all descriptors in an array.
* Use of QTAILQ since it works similar to signal the end of the inflight
descriptors: 1 for more data, 0 if end. But do it for each vq instead of for each descriptor.
* Usage of VMState macros.
The fields of descriptors would be way more complicated if we use the VirtQueueElements directly, since there would be a few levels of indirections. Using VirtQueueElementOld for the moment, and migrate to VirtQueueElement for the final patch.
TODO: Proper migration versioning
TODO: Do not embed vhost-vdpa structs
TODO: Migrate the VirtQueueElement, not VirtQueueElementOld.
--- include/hw/virtio/virtio-net.h | 2 + include/migration/vmstate.h | 11 +++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)
include/hw/virtio/virtio-net.h +2 -0
@@ -151,9 +151,11 @@ typedef struct VirtIONetQueue {QEMUTimer *tx_timer;QEMUBH *tx_bh;uint32_t tx_waiting; +uint32_t tx_inflight_num, rx_inflight_num;struct {VirtQueueElement *elem; } async_tx; +VirtQueueElement **tx_inflight, **rx_inflight;struct VirtIONet *n; } VirtIONetQueue;
include/migration/vmstate.h +11 -0
@@ -626,6 +626,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (_field, _state, _field_num, \ + _version, _vmsd, _type) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \ + .size = sizeof(_type), \ + .flags = VMS_STRUCT | VMS_VARRAY_UINT16 | VMS_ALLOC | VMS_POINTER, \ + .offset = vmstate_offset_pointer(_state, _field, _type), \ +} +#define VMSTATE_STRUCT_VARRAY_ALLOC (_field, _state, _field_num, _version, _vmsd, _type) {\ .name = (stringify(_field)), \ .version_id = (_version), \
hw/net/virtio-net.c +129 -0
153 lines not on this page · raw message
Signature · 1 line
2.31.1
RE: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 214 lines
From: Eugenio Pérez <[email protected]>
Sent: Monday, December 5, 2022 12:05 PMThere is currently no data to be migrated, since nothing populates or read the fields on virtio-net.
The migration of in-flight descriptors is modelled after the migration of requests in virtio-blk. With some differences:
* virtio-blk migrates queue number on each request. Here we only add a vq if it has descriptors to migrate, and then we make all descriptors in an array.
* Use of QTAILQ since it works similar to signal the end of the inflight
descriptors: 1 for more data, 0 if end. But do it for each vq instead of for each descriptor.
* Usage of VMState macros.The fields of descriptors would be way more complicated if we use the VirtQueueElements directly, since there would be a few levels of indirections. Using VirtQueueElementOld for the moment, and migrate to VirtQueueElement for the final patch.
TODO: Proper migration versioning
TODO: Do not embed vhost-vdpa structs
TODO: Migrate the VirtQueueElement, not VirtQueueElementOld.Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/virtio-net.h | 2 + include/migration/vmstate.h | 11 +++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)include/hw/virtio/virtio-net.h +2
-0@@ -151,9 +151,11 @@ typedef struct VirtIONetQueue {QEMUTimer *tx_timer;QEMUBH *tx_bh;uint32_t tx_waiting; +uint32_t tx_inflight_num, rx_inflight_num;struct {VirtQueueElement *elem; } async_tx; +VirtQueueElement **tx_inflight, **rx_inflight;struct VirtIONet *n; } VirtIONetQueue;include/migration/vmstate.h +11
-0@@ -626,6 +626,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (_field, _state, _field_num, \ + _version, _vmsd, _type) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \ + .size = sizeof(_type), \ + .flags = VMS_STRUCT | VMS_VARRAY_UINT16 | VMS_ALLOC | VMS_POINTER, \ + .offset =vmstate_offset_pointer (_state, _field, _type), \ +} + #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num,_version, _vmsd, _type) {\ .name = (stringify(_field)), \ .version_id = (_version), \hw/net/virtio-net.c index renamed from hw/net/virtio-net.c +0
-0
aba12759d5..ffd7bf1fc7100644hw/net/virtio-net.c +8
-0@@ -3077,6 +3077,13 @@ static bool mac_table_doesnt_fit(void *opaque, int version_id)return !mac_table_fits (opaque, version_id); } +typedef struct VirtIONetInflightQueue { +uint16_t idx; +uint16_t num; +QTAILQ_ENTRY (VirtIONetInflightQueue) entry; +VirtQueueElementOld *elems; +}VirtIONetInflightQueue ; +/* This temporary type is shared by all the WITH_TMP methods * although only some fields are used by each. */ @@ -3086,6 +3093,7 @@ struct VirtIONetMigTmp {uint16_t curr_queue_pairs_1;uint8_t has_ufo;uint32_t has_vnet_hdr; + QTAILQ_HEAD(, VirtIONetInflightQueue) queues_inflight; };/* The 2nd and subsequent tx_waiting flags are loaded later than @@ - 3231,6 +3239,124 @@ static const VMStateDescription
vmstate_virtio_net_rss = { },};
hw/net/virtio-net.c +7
-0+static const VMStateDescription vmstate_virtio_net_inflight_queue = { + .name ="virtio-net-device/inflight/queue" , + .fields = (VMStateField []) { +VMSTATE_UINT16 (idx, VirtIONetInflightQueue), +VMSTATE_UINT16 (num, VirtIONetInflightQueue), + + VMSTATE_STRUCT_VARRAY_ALLOC_UINT16(elems,VirtIONetInflightQueue, num,
hw/net/virtio-net.c +17
-0+ 0, vmstate_virtqueue_element_old, + VirtQueueElementOld), +VMSTATE_END_OF_LIST () + }, +}; + +static int virtio_net_inflight_init (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; + +QTAILQ_INIT (&tmp->queues_inflight); +return 0 ; +} + +static int virtio_net_inflight_pre_save (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; + uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1;
hw/net/virtio-net.c +44
-0+VirtIONetInflightQueue *qi =g_new0 (VirtIONetInflightQueue, + curr_queue_pairs *2 ); + +virtio_net_inflight_init (opaque); +for (uint16_t i =0 ; i < curr_queue_pairs *2 ; ++i) { +VirtIONetQueue *q = &net->vqs[vq2q (i)]; +size_t n = i %2 ? q->tx_inflight_num : q->rx_inflight_num; +VirtQueueElement **inflight = i %2 ? q->tx_inflight : + q->rx_inflight; + +if (n ==0 ) { +continue ; + } + + qi[i].idx = i; + qi[i].num = n; + qi[i].elems =g_new0 (VirtQueueElementOld, n); +for (uint16_t j =0 ; j < n; ++j) { +qemu_put_virtqueue_element_old (inflight[j], &qi[i].elems[j]); + } +QTAILQ_INSERT_TAIL (&tmp->queues_inflight, &qi[i], entry); + } + +return 0 ; +} + +static int virtio_net_inflight_post_save (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +static int virtio_net_inflight_post_load (void *opaque,int version_id) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; + uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1;
hw/net/virtio-net.c +38
-0+VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +VirtIONetQueue *q = &net->vqs[vq2q (qi->idx)]; +uint32_t *n = qi->idx %2 ? &q->tx_inflight_num : &q->rx_inflight_num; +VirtQueueElement ***inflight = qi->idx %2 ? + &q->tx_inflight : &q->rx_inflight; +if (unlikely (qi->num ==0 )) { +/* TODO: error message */ +return -1 ; + } + +if (unlikely (qi->idx > curr_queue_pairs *2 )) { +/* TODO: error message */ +return -1 ; + } + + *n = qi->num; + *inflight = g_new(VirtQueueElement *, *n); +for (uint16_t j =0 ; j < *n; ++j) { + (*inflight)[j] =qemu_get_virtqueue_element_from_old ( + &net->parent_obj, &qi->elems[j], +sizeof (VirtQueueElement)); + } + +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +/* TODO: Allocate a temporal per queue / queue element, not all of +them! */ static const VMStateDescription vmstate_virtio_net_inflight = { + .name ="virtio-net-device/inflight" , + .pre_save = virtio_net_inflight_pre_save, + .post_save = virtio_net_inflight_post_save,
hw/net/virtio-net.c static const VMStateDescription vmstate_virtio_net_rss = { · line 3359
Quoted 10 lines
hw/net/virtio-net.c +10
-0+ .pre_load = virtio_net_inflight_init, + .post_load = virtio_net_inflight_post_load, + .fields = (VMStateField []) { + VMSTATE_QTAILQ_V(queues_inflight, struct VirtIONetMigTmp, 0, + vmstate_virtio_net_inflight_queue, + VirtIONetInflightQueue, entry), +VMSTATE_END_OF_LIST () + }, +}; +
How is the CVQ related mac, vlan, rss replay different than these inflight descriptors, due to which inflights to be done by these callbacks and CVQ differently?
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 218 lines · Parav Pandit
Quoted 218 lines
From: Eugenio Pérez <[email protected]>
Sent: Monday, December 5, 2022 12:05 PMThere is currently no data to be migrated, since nothing populates or read the fields on virtio-net.
The migration of in-flight descriptors is modelled after the migration of requests in virtio-blk. With some differences:
* virtio-blk migrates queue number on each request. Here we only add a vq if it has descriptors to migrate, and then we make all descriptors in an array.
* Use of QTAILQ since it works similar to signal the end of the inflight
descriptors: 1 for more data, 0 if end. But do it for each vq instead of for each descriptor.
* Usage of VMState macros.The fields of descriptors would be way more complicated if we use the VirtQueueElements directly, since there would be a few levels of indirections. Using VirtQueueElementOld for the moment, and migrate to VirtQueueElement for the final patch.
TODO: Proper migration versioning
TODO: Do not embed vhost-vdpa structs
TODO: Migrate the VirtQueueElement, not VirtQueueElementOld.Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/virtio-net.h | 2 + include/migration/vmstate.h | 11 +++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)include/hw/virtio/virtio-net.h +2
-0@@ -151,9 +151,11 @@ typedef struct VirtIONetQueue {QEMUTimer *tx_timer;QEMUBH *tx_bh;uint32_t tx_waiting; +uint32_t tx_inflight_num, rx_inflight_num;struct {VirtQueueElement *elem; } async_tx; +VirtQueueElement **tx_inflight, **rx_inflight;struct VirtIONet *n; } VirtIONetQueue;include/migration/vmstate.h +11
-0@@ -626,6 +626,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (_field, _state, _field_num, \ + _version, _vmsd, _type) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \ + .size = sizeof(_type), \ + .flags = VMS_STRUCT | VMS_VARRAY_UINT16 | VMS_ALLOC | VMS_POINTER, \ + .offset =vmstate_offset_pointer (_state, _field, _type), \ +} + #define VMSTATE_STRUCT_VARRAY_ALLOC(_field, _state, _field_num,_version, _vmsd, _type) {\ .name = (stringify(_field)), \ .version_id = (_version), \hw/net/virtio-net.c index renamed from hw/net/virtio-net.c +0
-0
aba12759d5..ffd7bf1fc7100644hw/net/virtio-net.c +8
-0@@ -3077,6 +3077,13 @@ static bool mac_table_doesnt_fit(void *opaque, int version_id)return !mac_table_fits (opaque, version_id); } +typedef struct VirtIONetInflightQueue { +uint16_t idx; +uint16_t num; +QTAILQ_ENTRY (VirtIONetInflightQueue) entry; +VirtQueueElementOld *elems; +}VirtIONetInflightQueue ; +/* This temporary type is shared by all the WITH_TMP methods * although only some fields are used by each. */ @@ -3086,6 +3093,7 @@ struct VirtIONetMigTmp {uint16_t curr_queue_pairs_1;uint8_t has_ufo;uint32_t has_vnet_hdr; + QTAILQ_HEAD(, VirtIONetInflightQueue) queues_inflight; };/* The 2nd and subsequent tx_waiting flags are loaded later than @@ - 3231,6 +3239,124 @@ static const VMStateDescription
vmstate_virtio_net_rss = { },};
hw/net/virtio-net.c +7
-0+static const VMStateDescription vmstate_virtio_net_inflight_queue = { + .name ="virtio-net-device/inflight/queue" , + .fields = (VMStateField []) { +VMSTATE_UINT16 (idx, VirtIONetInflightQueue), +VMSTATE_UINT16 (num, VirtIONetInflightQueue), + + VMSTATE_STRUCT_VARRAY_ALLOC_UINT16(elems,VirtIONetInflightQueue, num,
hw/net/virtio-net.c +17
-0+ 0, vmstate_virtqueue_element_old, + VirtQueueElementOld), +VMSTATE_END_OF_LIST () + }, +}; + +static int virtio_net_inflight_init (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; + +QTAILQ_INIT (&tmp->queues_inflight); +return 0 ; +} + +static int virtio_net_inflight_pre_save (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; + uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1;
hw/net/virtio-net.c +44
-0+VirtIONetInflightQueue *qi =g_new0 (VirtIONetInflightQueue, + curr_queue_pairs *2 ); + +virtio_net_inflight_init (opaque); +for (uint16_t i =0 ; i < curr_queue_pairs *2 ; ++i) { +VirtIONetQueue *q = &net->vqs[vq2q (i)]; +size_t n = i %2 ? q->tx_inflight_num : q->rx_inflight_num; +VirtQueueElement **inflight = i %2 ? q->tx_inflight : + q->rx_inflight; + +if (n ==0 ) { +continue ; + } + + qi[i].idx = i; + qi[i].num = n; + qi[i].elems =g_new0 (VirtQueueElementOld, n); +for (uint16_t j =0 ; j < n; ++j) { +qemu_put_virtqueue_element_old (inflight[j], &qi[i].elems[j]); + } +QTAILQ_INSERT_TAIL (&tmp->queues_inflight, &qi[i], entry); + } + +return 0 ; +} + +static int virtio_net_inflight_post_save (void *opaque) { +struct VirtIONetMigTmp *tmp = opaque; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +static int virtio_net_inflight_post_load (void *opaque,int version_id) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; + uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1;
hw/net/virtio-net.c +41
-0+VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +VirtIONetQueue *q = &net->vqs[vq2q (qi->idx)]; +uint32_t *n = qi->idx %2 ? &q->tx_inflight_num : &q->rx_inflight_num; +VirtQueueElement ***inflight = qi->idx %2 ? + &q->tx_inflight : &q->rx_inflight; +if (unlikely (qi->num ==0 )) { +/* TODO: error message */ +return -1 ; + } + +if (unlikely (qi->idx > curr_queue_pairs *2 )) { +/* TODO: error message */ +return -1 ; + } + + *n = qi->num; + *inflight = g_new(VirtQueueElement *, *n); +for (uint16_t j =0 ; j < *n; ++j) { + (*inflight)[j] =qemu_get_virtqueue_element_from_old ( + &net->parent_obj, &qi->elems[j], +sizeof (VirtQueueElement)); + } + +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +/* TODO: Allocate a temporal per queue / queue element, not all of +them! */ static const VMStateDescription vmstate_virtio_net_inflight = { + .name ="virtio-net-device/inflight" , + .pre_save = virtio_net_inflight_pre_save, + .post_save = virtio_net_inflight_post_save, + .pre_load = virtio_net_inflight_init, + .post_load = virtio_net_inflight_post_load, + .fields = (VMStateField []) {
Quoted 8 lines
hw/net/virtio-net.c static const VMStateDescription vmstate_virtio_net_rss = { · line 3359
Quoted 7 lines
hw/net/virtio-net.c +7
-0+ VMSTATE_QTAILQ_V(queues_inflight, struct VirtIONetMigTmp, 0, + vmstate_virtio_net_inflight_queue, + VirtIONetInflightQueue, entry), +VMSTATE_END_OF_LIST () + }, +}; +How is the CVQ related mac, vlan, rss replay different than these inflight descriptors, due to which inflights to be done by these callbacks and CVQ differently?
CVQ is processed by qemu directly, so it is guaranteed they will not be out of order. Guest memory is enough to recover in the destination.
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
In reply to Eugenio PérezQuoted 100 lines · Eugenio Pérez
There is currently no data to be migrated, since nothing populates or read the fields on virtio-net.
The migration of in-flight descriptors is modelled after the migration of requests in virtio-blk. With some differences:
* virtio-blk migrates queue number on each request. Here we only add a vq if it has descriptors to migrate, and then we make all descriptors in an array.
* Use of QTAILQ since it works similar to signal the end of the inflight
descriptors: 1 for more data, 0 if end. But do it for each vq instead of for each descriptor.
* Usage of VMState macros.The fields of descriptors would be way more complicated if we use the VirtQueueElements directly, since there would be a few levels of indirections. Using VirtQueueElementOld for the moment, and migrate to VirtQueueElement for the final patch.
TODO: Proper migration versioning
TODO: Do not embed vhost-vdpa structs
TODO: Migrate the VirtQueueElement, not VirtQueueElementOld.Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/virtio-net.h | 2 + include/migration/vmstate.h | 11 +++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)include/hw/virtio/virtio-net.h +2
-0@@ -151,9 +151,11 @@ typedef struct VirtIONetQueue {QEMUTimer *tx_timer;QEMUBH *tx_bh;uint32_t tx_waiting; +uint32_t tx_inflight_num, rx_inflight_num;struct {VirtQueueElement *elem; } async_tx; +VirtQueueElement **tx_inflight, **rx_inflight;struct VirtIONet *n; } VirtIONetQueue;include/migration/vmstate.h +11
-0@@ -626,6 +626,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (_field, _state, _field_num, \ + _version, _vmsd, _type) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \ + .size = sizeof(_type), \ + .flags = VMS_STRUCT | VMS_VARRAY_UINT16 | VMS_ALLOC | VMS_POINTER, \ + .offset = vmstate_offset_pointer(_state, _field, _type), \ +} +#define VMSTATE_STRUCT_VARRAY_ALLOC (_field, _state, _field_num, _version, _vmsd, _type) {\ .name = (stringify(_field)), \ .version_id = (_version), \hw/net/virtio-net.c +10
-0@@ -3077,6 +3077,13 @@ static bool mac_table_doesnt_fit(void *opaque, int version_id)return !mac_table_fits (opaque, version_id); } +typedef struct VirtIONetInflightQueue { +uint16_t idx; +uint16_t num; +QTAILQ_ENTRY (VirtIONetInflightQueue) entry; +VirtQueueElementOld *elems; +}VirtIONetInflightQueue ; +/* This temporary type is shared by all the WITH_TMP methods * although only some fields are used by each. */ @@ -3086,6 +3093,7 @@ struct VirtIONetMigTmp {uint16_t curr_queue_pairs_1;uint8_t has_ufo;uint32_t has_vnet_hdr; + QTAILQ_HEAD(, VirtIONetInflightQueue) queues_inflight; };/* The 2nd and subsequent tx_waiting flags are loaded later than @@ -3231,6 +3239,124 @@ static const VMStateDescription vmstate_virtio_net_rss = { }, }; +static const VMStateDescription vmstate_virtio_net_inflight_queue = { + .name ="virtio-net-device/inflight/queue" ,
hw/net/virtio-net.c static const VMStateDescription vmstate_virtio_net_rss = { · line 3253
Quoted 10 lines
hw/net/virtio-net.c +10
-0+ .fields = (VMStateField[]) { +VMSTATE_UINT16 (idx, VirtIONetInflightQueue), +VMSTATE_UINT16 (num, VirtIONetInflightQueue), + +VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (elems, VirtIONetInflightQueue, num, +0 , vmstate_virtqueue_element_old, + VirtQueueElementOld), +VMSTATE_END_OF_LIST () + }, +};
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
I guess virtio-blk is the better place.
Thanks
Quoted 122 lines
hw/net/virtio-net.c +109
-1+ +static int virtio_net_inflight_init (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; + +QTAILQ_INIT (&tmp->queues_inflight); +return 0 ; +} + +static int virtio_net_inflight_pre_save (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; +uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1 ; +VirtIONetInflightQueue *qi =g_new0 (VirtIONetInflightQueue, + curr_queue_pairs *2 ); + +virtio_net_inflight_init (opaque); +for (uint16_t i =0 ; i < curr_queue_pairs *2 ; ++i) { +VirtIONetQueue *q = &net->vqs[vq2q (i)]; +size_t n = i %2 ? q->tx_inflight_num : q->rx_inflight_num; +VirtQueueElement **inflight = i %2 ? q->tx_inflight : q->rx_inflight; + +if (n ==0 ) { +continue ; + } + + qi[i].idx = i; + qi[i].num = n; + qi[i].elems =g_new0 (VirtQueueElementOld, n); +for (uint16_t j =0 ; j < n; ++j) { +qemu_put_virtqueue_element_old (inflight[j], &qi[i].elems[j]); + } +QTAILQ_INSERT_TAIL (&tmp->queues_inflight, &qi[i], entry); + } + +return 0 ; +} + +static int virtio_net_inflight_post_save (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +static int virtio_net_inflight_post_load (void *opaque,int version_id) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; +uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1 ; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +VirtIONetQueue *q = &net->vqs[vq2q (qi->idx)]; +uint32_t *n = qi->idx %2 ? &q->tx_inflight_num : &q->rx_inflight_num; +VirtQueueElement ***inflight = qi->idx %2 ? + &q->tx_inflight : &q->rx_inflight; +if (unlikely (qi->num ==0 )) { +/* TODO: error message */ +return -1 ; + } + +if (unlikely (qi->idx > curr_queue_pairs *2 )) { +/* TODO: error message */ +return -1 ; + } + + *n = qi->num; + *inflight = g_new(VirtQueueElement *, *n); +for (uint16_t j =0 ; j < *n; ++j) { + (*inflight)[j] =qemu_get_virtqueue_element_from_old ( + &net->parent_obj, &qi->elems[j], +sizeof (VirtQueueElement)); + } + +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +/* TODO: Allocate a temporal per queue / queue element, not all of them! */ +static const VMStateDescription vmstate_virtio_net_inflight = { + .name ="virtio-net-device/inflight" , + .pre_save = virtio_net_inflight_pre_save, + .post_save = virtio_net_inflight_post_save, + .pre_load = virtio_net_inflight_init, + .post_load = virtio_net_inflight_post_load, + .fields = (VMStateField []) { + VMSTATE_QTAILQ_V(queues_inflight, struct VirtIONetMigTmp, 0, + vmstate_virtio_net_inflight_queue, + VirtIONetInflightQueue, entry), +VMSTATE_END_OF_LIST () + }, +}; +static const VMStateDescription vmstate_virtio_net_device = { .name ="virtio-net-device" , .version_id =VIRTIO_NET_VM_VERSION ,@@ -3279,6 +3405,9 @@ static const VMStateDescription vmstate_virtio_net_device = { vmstate_virtio_net_tx_waiting),VMSTATE_UINT64_TEST (curr_guest_offloads ,VirtIONet ,has_ctrl_guest_offloads ), +/* TODO: Move to subsection */ +VMSTATE_WITH_TMP (VirtIONet ,struct VirtIONetMigTmp , +vmstate_virtio_net_inflight ),VMSTATE_END_OF_LIST () }, .subsections = (const VMStateDescription * []) { --2.31.1
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 116 lines · Jason Wang
Quoted 100 lines · Eugenio Pérez
There is currently no data to be migrated, since nothing populates or read the fields on virtio-net.
The migration of in-flight descriptors is modelled after the migration of requests in virtio-blk. With some differences:
* virtio-blk migrates queue number on each request. Here we only add a vq if it has descriptors to migrate, and then we make all descriptors in an array.
* Use of QTAILQ since it works similar to signal the end of the inflight
descriptors: 1 for more data, 0 if end. But do it for each vq instead of for each descriptor.
* Usage of VMState macros.The fields of descriptors would be way more complicated if we use the VirtQueueElements directly, since there would be a few levels of indirections. Using VirtQueueElementOld for the moment, and migrate to VirtQueueElement for the final patch.
TODO: Proper migration versioning
TODO: Do not embed vhost-vdpa structs
TODO: Migrate the VirtQueueElement, not VirtQueueElementOld.Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/virtio-net.h | 2 + include/migration/vmstate.h | 11 +++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)include/hw/virtio/virtio-net.h +2
-0@@ -151,9 +151,11 @@ typedef struct VirtIONetQueue {QEMUTimer *tx_timer;QEMUBH *tx_bh;uint32_t tx_waiting; +uint32_t tx_inflight_num, rx_inflight_num;struct {VirtQueueElement *elem; } async_tx; +VirtQueueElement **tx_inflight, **rx_inflight;struct VirtIONet *n; } VirtIONetQueue;include/migration/vmstate.h +11
-0@@ -626,6 +626,17 @@ extern const VMStateInfo vmstate_info_qlist; .offset = vmstate_offset_varray(_state, _field, _type), \ } +#define VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (_field, _state, _field_num, \ + _version, _vmsd, _type) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \ + .size = sizeof(_type), \ + .flags = VMS_STRUCT | VMS_VARRAY_UINT16 | VMS_ALLOC | VMS_POINTER, \ + .offset = vmstate_offset_pointer(_state, _field, _type), \ +} +#define VMSTATE_STRUCT_VARRAY_ALLOC (_field, _state, _field_num, _version, _vmsd, _type) {\ .name = (stringify(_field)), \ .version_id = (_version), \hw/net/virtio-net.c +10
-0@@ -3077,6 +3077,13 @@ static bool mac_table_doesnt_fit(void *opaque, int version_id)return !mac_table_fits (opaque, version_id); } +typedef struct VirtIONetInflightQueue { +uint16_t idx; +uint16_t num; +QTAILQ_ENTRY (VirtIONetInflightQueue) entry; +VirtQueueElementOld *elems; +}VirtIONetInflightQueue ; +/* This temporary type is shared by all the WITH_TMP methods * although only some fields are used by each. */ @@ -3086,6 +3093,7 @@ struct VirtIONetMigTmp {uint16_t curr_queue_pairs_1;uint8_t has_ufo;uint32_t has_vnet_hdr; + QTAILQ_HEAD(, VirtIONetInflightQueue) queues_inflight; };/* The 2nd and subsequent tx_waiting flags are loaded later than @@ -3231,6 +3239,124 @@ static const VMStateDescription vmstate_virtio_net_rss = { }, }; +static const VMStateDescription vmstate_virtio_net_inflight_queue = { + .name ="virtio-net-device/inflight/queue" ,hw/net/virtio-net.c static const VMStateDescription vmstate_virtio_net_rss = { · line 3253
Quoted 10 lines
hw/net/virtio-net.c +10
-0+ .fields = (VMStateField[]) { +VMSTATE_UINT16 (idx, VirtIONetInflightQueue), +VMSTATE_UINT16 (num, VirtIONetInflightQueue), + +VMSTATE_STRUCT_VARRAY_ALLOC_UINT16 (elems, VirtIONetInflightQueue, num, +0 , vmstate_virtqueue_element_old, + VirtQueueElementOld), +VMSTATE_END_OF_LIST () + }, +};A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Quoted 2 lines
I guess virtio-blk is the better place.
I'm fine to start with -blk, but if -net devices are processing buffers out of order we have chances of losing descriptors too.
We can wait for more feedback to prioritize correctly this though.
Thanks!
Quoted 125 lines
Thanks
Quoted 122 lines
hw/net/virtio-net.c +109
-1+ +static int virtio_net_inflight_init (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; + +QTAILQ_INIT (&tmp->queues_inflight); +return 0 ; +} + +static int virtio_net_inflight_pre_save (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; +uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1 ; +VirtIONetInflightQueue *qi =g_new0 (VirtIONetInflightQueue, + curr_queue_pairs *2 ); + +virtio_net_inflight_init (opaque); +for (uint16_t i =0 ; i < curr_queue_pairs *2 ; ++i) { +VirtIONetQueue *q = &net->vqs[vq2q (i)]; +size_t n = i %2 ? q->tx_inflight_num : q->rx_inflight_num; +VirtQueueElement **inflight = i %2 ? q->tx_inflight : q->rx_inflight; + +if (n ==0 ) { +continue ; + } + + qi[i].idx = i; + qi[i].num = n; + qi[i].elems =g_new0 (VirtQueueElementOld, n); +for (uint16_t j =0 ; j < n; ++j) { +qemu_put_virtqueue_element_old (inflight[j], &qi[i].elems[j]); + } +QTAILQ_INSERT_TAIL (&tmp->queues_inflight, &qi[i], entry); + } + +return 0 ; +} + +static int virtio_net_inflight_post_save (void *opaque) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +static int virtio_net_inflight_post_load (void *opaque,int version_id) +{ +struct VirtIONetMigTmp *tmp = opaque; +VirtIONet *net = tmp->parent; +uint16_t curr_queue_pairs = net->multiqueue ? net->curr_queue_pairs :1 ; +VirtIONetInflightQueue *qi; + +while ((qi =QTAILQ_FIRST (&tmp->queues_inflight))) { +VirtIONetQueue *q = &net->vqs[vq2q (qi->idx)]; +uint32_t *n = qi->idx %2 ? &q->tx_inflight_num : &q->rx_inflight_num; +VirtQueueElement ***inflight = qi->idx %2 ? + &q->tx_inflight : &q->rx_inflight; +if (unlikely (qi->num ==0 )) { +/* TODO: error message */ +return -1 ; + } + +if (unlikely (qi->idx > curr_queue_pairs *2 )) { +/* TODO: error message */ +return -1 ; + } + + *n = qi->num; + *inflight = g_new(VirtQueueElement *, *n); +for (uint16_t j =0 ; j < *n; ++j) { + (*inflight)[j] =qemu_get_virtqueue_element_from_old ( + &net->parent_obj, &qi->elems[j], +sizeof (VirtQueueElement)); + } + +QTAILQ_REMOVE (&tmp->queues_inflight, qi, entry); +g_free (qi->elems); +g_free (qi); + } + +return 0 ; +} + +/* TODO: Allocate a temporal per queue / queue element, not all of them! */ +static const VMStateDescription vmstate_virtio_net_inflight = { + .name ="virtio-net-device/inflight" , + .pre_save = virtio_net_inflight_pre_save, + .post_save = virtio_net_inflight_post_save, + .pre_load = virtio_net_inflight_init, + .post_load = virtio_net_inflight_post_load, + .fields = (VMStateField []) { + VMSTATE_QTAILQ_V(queues_inflight, struct VirtIONetMigTmp, 0, + vmstate_virtio_net_inflight_queue, + VirtIONetInflightQueue, entry), +VMSTATE_END_OF_LIST () + }, +}; +static const VMStateDescription vmstate_virtio_net_device = { .name ="virtio-net-device" , .version_id =VIRTIO_NET_VM_VERSION ,@@ -3279,6 +3405,9 @@ static const VMStateDescription vmstate_virtio_net_device = { vmstate_virtio_net_tx_waiting),VMSTATE_UINT64_TEST (curr_guest_offloads ,VirtIONet ,has_ctrl_guest_offloads ), +/* TODO: Move to subsection */ +VMSTATE_WITH_TMP (VirtIONet ,struct VirtIONetMigTmp , +vmstate_virtio_net_inflight ),VMSTATE_END_OF_LIST () }, .subsections = (const VMStateDescription * []) { --2.31.1
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 6 lines · Eugenio Perez Martin
Quoted 5 lines
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
I guess virtio-blk is the better place.
Quoted 6 lines
I'm fine to start with -blk, but if -net devices are processing buffers out of order we have chances of losing descriptors too.
We can wait for more feedback to prioritize correctly this though.
Thanks!
Traditionally vhost serialized everything when dropping the VQ. Would be interesting to hear from hardware vendors on whether it's hard or easy to do in hardware.
But I suspect all devices will want the capability eventually because why not, if we have the code let's just do it everywhere.
Signature · 1 line
MST
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 11 lines · Michael S. Tsirkin
Quoted 9 lines · Eugenio Perez Martin
Quoted 5 lines
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
I guess virtio-blk is the better place.
I'm fine to start with -blk, but if -net devices are processing buffers out of order we have chances of losing descriptors too.
Quoted 7 lines
Quoted 3 lines
We can wait for more feedback to prioritize correctly this though.
Thanks!
Traditionally vhost serialized everything when dropping the VQ. Would be interesting to hear from hardware vendors on whether it's hard or easy to do in hardware.
The feedback is some vendors will do the serialization while others are not.
Quoted 2 lines
But I suspect all devices will want the capability eventually because why not, if we have the code let's just do it everywhere.
Yes, but it's more about priority/compatibility other than whether it is needed. We want to let the migration work as earlier as possible for vendor to test and try.
Thanks
Quoted 4 lines
--
MST
RE: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
In reply to Jason WangHi Jason,
Quoted 6 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMA dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer IN_ORDER flag.
And when a VQ is suspended it cannot complete these descriptors as some dummy zero length completions. The guest VM is flooded with [1].
So it is needed for the devices that doesn’t offer IN_ORDER feature.
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 7 lines · Parav Pandit
Hi Jason,
Quoted 4 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PM
Quoted 8 lines
Quoted 2 lines
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.And when a VQ is suspended it cannot complete these descriptors as some dummy zero length completions. The guest VM is flooded with [1].
Yes, but any reason for the device to do out-of-order for RX?
Quoted 4 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.
It is only enabled in a debug kernel which should be harmless?
Thanks
Quoted 1 line
RE: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 15 lines
From: Jason Wang <[email protected]>
Sent: Tuesday, January 10, 2023 11:35 PMQuoted 10 lines · Parav Pandit
Hi Jason,
Quoted 6 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMA dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Quoted 9 lines
Quoted 4 lines
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.And when a VQ is suspended it cannot complete these descriptors as some
dummy zero length completions.
Quoted 1 line
The guest VM is flooded with [1].
Yes, but any reason for the device to do out-of-order for RX?
For some devices it is more optimal to process them out of order. And its not limited to RX.
Quoted 8 lines
Quoted 6 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tre
e/drivers/net/virtio_net.c?h=v6.2-rc3#n1252It is only enabled in a debug kernel which should be harmless?
it is KERN_DEBUG log level. Its is not debug kernel, just the debug log level. And regardless, generating zero length packets for debug kernel is even more confusing.
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 19 lines · Parav Pandit
Quoted 19 lines
From: Jason Wang <[email protected]>
Sent: Tuesday, January 10, 2023 11:35 PMQuoted 13 lines · Parav Pandit
Hi Jason,
Quoted 6 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMA dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
And when a VQ is suspended it cannot complete these descriptors as some
dummy zero length completions.
Quoted 1 line
The guest VM is flooded with [1].
Yes, but any reason for the device to do out-of-order for RX?
For some devices it is more optimal to process them out of order. And its not limited to RX.
TX should be fine, since the device can anyhow pretend to send all packets, so we won't have any in-flight descriptors.
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tre
e/drivers/net/virtio_net.c?h=v6.2-rc3#n1252It is only enabled in a debug kernel which should be harmless?
it is
KERN_DEBUGlog level. Its is not debug kernel, just the debug log level.
Ok, but the production environment should not use that level anyhow.
Quoted 1 line
And regardless, generating zero length packets for debug kernel is even more confusing.
Note that it is allowed in the virtio-spec[1] (we probably can fix that in the driver) and we have pr_debug() all over this drivers and other places. It doesn't cause any side effects except for the debugging purpose.
So I think having inflight tracking is useful, but I'm not sure it's worth bothering with virtio-net (or worth to bothering now):
- zero length is allowed
- it only helps for debugging
- may cause issues for migration compatibility
- requires new infrastructure to be invented
Thanks
[1] spec said
"
Note: len is particularly useful for drivers using untrusted buffers:
if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.
"
RE: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 55 lines
From: Jason Wang <[email protected]>
Sent: Wednesday, January 11, 2023 12:51 AMQuoted 7 lines · Parav Pandit
Quoted 7 lines
From: Jason Wang <[email protected]>
Sent: Tuesday, January 10, 2023 11:35 PMQuoted 1 line · Parav Pandit
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Hi Jason,
Quoted 6 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMA dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration
compatibility.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.And when a VQ is suspended it cannot complete these descriptors as some
Quoted 7 lines
Quoted 5 lines
dummy zero length completions.
Quoted 1 line
The guest VM is flooded with [1].
Yes, but any reason for the device to do out-of-order for RX?
For some devices it is more optimal to process them out of order. And its not limited to RX.
TX should be fine, since the device can anyhow pretend to send all packets, so we won't have any in-flight descriptors.
Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
/tre
e/drivers/net/virtio_net.c?h=v6.2-rc3#n1252It is only enabled in a debug kernel which should be harmless?
it is
KERN_DEBUGlog level. Its is not debug kernel, just the debug log level.Ok, but the production environment should not use that level anyhow.
Quoted 1 line
And regardless, generating zero length packets for debug kernel is even more
confusing.
Quoted 8 lines
Note that it is allowed in the virtio-spec[1] (we probably can fix that in the driver) and we have
pr_debug()all over this drivers and other places. It doesn't cause any side effects except for the debugging purpose.So I think having inflight tracking is useful, but I'm not sure it's worth bothering with virtio-net (or worth to bothering now):
- zero length is allowed
This isn’t explicitly called out. It may be worth to add to the spec.
Quoted 2 lines
- it only helps for debugging
- may cause issues for migration compatibility
We have this missing for long time regardless of this feature. So let's not mix here.
The mlx5 vdpa device can do eventual in-order completion before/at time of suspend, so I think we can wait for now to until more advance hw arrives.
Quoted 6 lines
- requires new infrastructure to be invented
Thanks
[1] spec said
This doesn’t say that its zero-length completion. Len is a mandatory field to tell how many bytes device wrote.
Quoted 6 lines
"
Note: len is particularly useful for drivers using untrusted buffers:
if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.
"
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
In reply to Jason WangQuoted 24 lines · Jason Wang
Quoted 23 lines · Parav Pandit
Quoted 23 lines
From: Jason Wang <[email protected]>
Sent: Tuesday, January 10, 2023 11:35 PMQuoted 7 lines · Parav Pandit
Hi Jason,
Quoted 4 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMQuoted 7 lines
Quoted 2 lines
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.And when a VQ is suspended it cannot complete these descriptors as some
dummy zero length completions.
Quoted 1 line
The guest VM is flooded with [1].
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Yes, but any reason for the device to do out-of-order for RX?
For some devices it is more optimal to process them out of order. And its not limited to RX.
TX should be fine, since the device can anyhow pretend to send all packets, so we won't have any in-flight descriptors.
And drop them all? You end up with multisecond delays for things like DHCP. Yes theoretically packets can be dropped at any time, but practically people expect this to happen on busy systems, not randomly out of the blue.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tre
e/drivers/net/virtio_net.c?h=v6.2-rc3#n1252It is only enabled in a debug kernel which should be harmless?
it is
KERN_DEBUGlog level. Its is not debug kernel, just the debug log level.Ok, but the production environment should not use that level anyhow.
It's just one example. And it's enough in my eyes to prove we really can't start sending zero length RX buffers to drivers and expect all to be well. If we want to we need to negotiate a new feature bit.
Quoted 17 lines
Quoted 1 line
And regardless, generating zero length packets for debug kernel is even more confusing.
Note that it is allowed in the virtio-spec[1] (we probably can fix that in the driver) and we have
pr_debug()all over this drivers and other places. It doesn't cause any side effects except for the debugging purpose.So I think having inflight tracking is useful, but I'm not sure it's worth bothering with virtio-net (or worth to bothering now):
- zero length is allowed
- it only helps for debugging
- may cause issues for migration compatibility
- requires new infrastructure to be inventedThanks
Quoted 8 lines
[1] spec said
"
Note: len is particularly useful for drivers using untrusted buffers:
if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.
"
I don't think this talks about zero length at all. Let me try to explain what this talk about in my opinion.
There are cases where device does not know exactly how much data it wrote into buffer. Should it over-estimate such that driver can be sure that buffer after the reported length is unchanged? Or should it instead under-estimate such that driver can be sure that the reported length has been initialized by device?
What this text in the spec says is that it must always under-estimate and not over-estimate. And it attempts to explain why this is useful: imagine driver that trusts the device and wants to make sure buffer is initialized. With the definition in the spec, it only needs to initialize data after the reported length. Initialize how? It's up to the driver but for example it can zero this buffer.
In short, all the text says is "do not over-report length, only set it to part of buffer you wrote".
Besides that, the text itself is from the original spec and it did not age well:
1)- no one actually relies on this
2)- rather than untrusted "buffers" what we commonly have is untrusted
devices so length can't be trusted either
3)- writes on PCI are posted and if your security model
depends on buffer being initialized and you want to recover from errors you really can't expect device to give you this info. Luckily no one cares see 1) above.
Signature · 1 line
MST
Re: [RFC PATCH for 8.0 10/13] virtio-net: Migrate vhost inflight descriptors
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Parav Pandit
Quoted 25 lines
From: Jason Wang <[email protected]>
Sent: Tuesday, January 10, 2023 11:35 PMQuoted 7 lines · Parav Pandit
Hi Jason,
Quoted 4 lines
From: Jason Wang <[email protected]>
Sent: Monday, December 5, 2022 10:25 PMQuoted 7 lines
Quoted 2 lines
A dumb question, any reason we need bother with virtio-net? It looks to me it's not a must and would complicate migration compatibility.
Virtio net vdpa device is processing the descriptors out of order. This vdpa device doesn’t offer
IN_ORDERflag.And when a VQ is suspended it cannot complete these descriptors as some
dummy zero length completions.
Quoted 1 line
The guest VM is flooded with [1].
Yes, but any reason for the device to do out-of-order for RX?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
For some devices it is more optimal to process them out of order. And its not limited to RX.
TX should be fine, since the device can anyhow pretend to send all packets, so we won't have any in-flight descriptors.
And drop them all?
Depends on how many inflight descriptors. This is the worst case and actually this is how software vhost backends did since it can't validate whether or not the packet is sent to the wire. And it's not worse than RX in which a lot of packets will be dropped for sure during live migration.
Quoted 2 lines
You end up with multisecond delays for things like DHCP.
Well, if DHCP is done during the live migration this is somehow unavoidable, a lot of things needs to be recovered not only from the migration downtime. E.g it may suffer from delay of gARP packet and others.
Quoted 3 lines
Yes theoretically packets can be dropped at any time, but practically people expect this to happen on busy systems, not randomly out of the blue.
The problem is that we never validate whether or not the packet is sent for a software device. Various layers could be placed under the vhost, so there's no guarantee that the packet won't be lost.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
So it is needed for the devices that doesn’t offer
IN_ORDERfeature.[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tre
e/drivers/net/virtio_net.c?h=v6.2-rc3#n1252
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
It is only enabled in a debug kernel which should be harmless?
it is
KERN_DEBUGlog level. Its is not debug kernel, just the debug log level.Ok, but the production environment should not use that level anyhow.
It's just one example. And it's enough in my eyes to prove we really can't start sending zero length RX buffers to drivers and expect all to be well. If we want to we need to negotiate a new feature bit.
Ok.
Quoted 28 lines
Quoted 19 lines
Quoted 3 lines
And regardless, generating zero length packets for debug kernel is even more confusing.
Note that it is allowed in the virtio-spec[1] (we probably can fix that in the driver) and we have
pr_debug()all over this drivers and other places. It doesn't cause any side effects except for the debugging purpose.So I think having inflight tracking is useful, but I'm not sure it's worth bothering with virtio-net (or worth to bothering now):
- zero length is allowed
- it only helps for debugging
- may cause issues for migration compatibility
- requires new infrastructure to be inventedThanks
Quoted 8 lines
[1] spec said
"
Note: len is particularly useful for drivers using untrusted buffers:
if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.
"
Quoted 6 lines
I don't think this talks about zero length at all. Let me try to explain what this talk about in my opinion.
There are cases where device does not know exactly how much data it wrote into buffer.
Actually, I think the inflight could be one case. Or do you have any other case when the device doesn't know how much data it wrote?
Quoted 3 lines
Should it over-estimate
such that driver can be sure that buffer after the reported length is unchanged?
I can't think of a case when such over-estimation can help for any logic. (Filling magic value into the buffer and deduce the actual length that is written by the device?)
Quoted 10 lines
Or should it instead under-estimate
such that driver can be sure that the reported length has been initialized by device?What this text in the spec says is that it must always under-estimate and not over-estimate. And it attempts to explain why this is useful: imagine driver that trusts the device and wants to make sure buffer is initialized. With the definition in the spec, it only needs to initialize data after the reported length.
Just to make sure I understand, such initialization can happen only after the buffer is completed by the device. But after that the buffer doesn't belong to the device anymore so drivers are free to do any initialization they want. Or anything makes this special?
Thanks
Quoted 24 lines
Initialize how? It's up to the
driver but for example it can zero this buffer.In short, all the text says is "do not over-report length, only set it to part of buffer you wrote".
Besides that, the text itself is from the original spec and it did not age well:
1)- no one actually relies on this
2)- rather than untrusted "buffers" what we commonly have is untrusted
devices so length can't be trusted either3)- writes on PCI are posted and if your security model
depends on buffer being initialized and you want to recover from errors you really can't expect device to give you this info. Luckily no one cares see 1) above.--
MST
RFC PATCH 11/13 virtio-net: save inflight descriptors at vhost shutdown
In reply to Eugenio PérezSo they can be migrated in virtio-net
--- hw/net/vhost_net.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
hw/net/vhost_net.c +22 -0
@@ -21,6 +21,7 @@#include "standard-headers/linux/vhost_types.h" #include "hw/virtio/virtio-net.h" +#include "hw/virtio/vhost-vdpa.h" /* TODO remove me */ #include "net/vhost_net.h" #include "qapi/error.h" #include "qemu/error-report.h"
@@ -319,6 +320,7 @@ static void vhost_net_stop_one(struct vhost_net *net, VirtIODevice *dev) {struct vhost_vring_file file = { .fd =-1 }; +VirtIONet *n =VIRTIO_NET (dev);if (net->nc->info->type ==NET_CLIENT_DRIVER_TAP ) {for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) {
@@ -329,6 +331,26 @@ static void vhost_net_stop_one(struct vhost_net *net,if (net->nc->info->poll) { net->nc->info->poll (net->nc, true); } + +for (size_t i =0 ; i < net->dev.nvqs; ++i) { +struct vhost_vdpa *v = net->dev.opaque; + +if (net->dev.nvqs !=2 ) { +continue ; + } + +if (!v->shadow_vqs_enabled) { +continue ; + } + + n->vqs[i].rx_inflight =vhost_svq_save_inflight ( +g_ptr_array_index (v->shadow_vqs,0 ), + &n->vqs[i].rx_inflight_num); + n->vqs[i].tx_inflight =vhost_svq_save_inflight ( +g_ptr_array_index (v->shadow_vqs,1 ), + &n->vqs[i].tx_inflight_num); + } +vhost_dev_stop (&net->dev, dev, false);if (net->nc->info->stop) { net->nc->info->stop (net->nc);
Signature · 1 line
2.31.1
RFC PATCH 12/13 vhost: expose vhost_svq_add_element
In reply to Eugenio PérezNeeded to inject new inflight elements
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -122,6 +122,7 @@ void vhost_svq_push_elem(VhostShadowVirtqueue *svq,int vhost_svq_add (VhostShadowVirtqueue *svq,const struct iovec *out_sg,size_t out_num,const struct iovec *in_sg,size_t in_num,VirtQueueElement *elem); +int vhost_svq_add_element (VhostShadowVirtqueue *svq,VirtQueueElement *elem);size_t vhost_svq_poll (VhostShadowVirtqueue *svq);void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);
hw/virtio/vhost-shadow-virtqueue.c +9 -3
@@ -271,9 +271,15 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,return 0 ; } -/* Convenience wrapper to add a guest's element to SVQ */ -static int vhost_svq_add_element (VhostShadowVirtqueue *svq, -VirtQueueElement *elem) +/* +* Add a guest's element to SVQ. SVQ return the element When the device mark +* the descriptor as used, so make sure the guest will understand it. +* +* This function can be used to add the elements after a migration event or in +* the case the device is reset. To add elements that must not be seen by the +* guest use vhost_svq_add. +*/ +int vhost_svq_add_element (VhostShadowVirtqueue *svq,VirtQueueElement *elem) {return vhost_svq_add (svq, elem->out_sg, elem->out_num, elem->in_sg, elem->in_num, elem);
Signature · 1 line
2.31.1
RFC PATCH 13/13 vdpa: Recover inflight descriptors
In reply to Eugenio PérezFinally recover the inflight descriptors at vhost_net_start.
TODO: Abstract it properly instead of using SVQ directly.
--- hw/net/vhost_net.c | 34 ++++++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 11 ----------- 2 files changed, 34 insertions(+), 11 deletions(-)
hw/net/vhost_net.c +34 -0
@@ -428,6 +428,40 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, } } +for (i =0 ; i < data_queue_pairs; i++) { +struct vhost_vdpa *v; + + peer =qemu_get_peer (ncs, i); +if (peer->info->type !=NET_CLIENT_DRIVER_VHOST_VDPA ) { +continue ; + } + net =get_vhost_net (peer); + v = net->dev.opaque; + +if (!v->shadow_vqs_enabled) { +return 0 ; + } + +for (size_t i =0 ; i < v->dev->nvqs; ++i) { +VirtIONetQueue *q = &n->vqs[(i + v->dev->vq_index) /2 ]; +size_t num = i %2 ? q->tx_inflight_num : q->rx_inflight_num; + g_autofree VirtQueueElement **inflight = NULL; + +assert (v->dev->vq_index %2 ==0 ); + inflight =g_steal_pointer (i %2 ? &q->tx_inflight + : &q->rx_inflight); +for (size_t j =0 ; j < num; ++j) { +int r; + + r =vhost_svq_add_element (g_ptr_array_index (v->shadow_vqs, i), + inflight[j]); + +/* TODO: Proper error handling */ +assert (r ==0 ); + } + } + } +return 0 ; err_start:
hw/virtio/vhost-vdpa.c +0 -11
@@ -1167,18 +1167,7 @@ static int vhost_vdpa_set_vring_base(struct vhost_dev *dev, struct vhost_vring_state *ring) {struct vhost_vdpa *v = dev->opaque; -VirtQueue *vq =virtio_get_queue (dev->vdev, ring->index); -/* -* vhost-vdpa devices does not support in-flight requests. Set all of them -* as available. -* -* TODO: This is ok for networking, but other kinds of devices might -* have problems with these retransmissions. -*/ -while (virtqueue_rewind (vq,1 )) { -continue ; - }if (v->shadow_vqs_enabled) {/* * Device vring base was set at device start. SVQ base is handled by
Signature · 1 line
2.31.1
Quoted 7 lines · Eugenio Pérez
The state of the descriptors (avail or used) may not be recoverable just looking at the guest memory. Out of order used descriptor may override previous avail ones in the descriptor table or avail vring.
Currently we're not migrating this status in net devices because virtio-net, vhost-kernel etc use the descriptors in order,
Note that this might not be the truth (when zerocopy is enabled).
Quoted 5 lines
so the information always
recoverable from guest's memory. However, vDPA devices may use them out of order, and other kind of devices like block need this support.Shadow virtqueue is able to track these and resend them at the destination.
As discussed, there's a bootstrap issue here:
When SVQ needs to be enabled on demand, do we still need another way to get inflight ones without the help of SVQ?
Thanks
Quoted 45 lines
Add them to the virtio-net migration description so they are not lose in the process.
This is a very early RFC just to validate the first draft so expect leftovers. To fetch and request the descriptors from a device without SVQ need to be implemented on top. Some other notable pending items are:
* Do not send the descriptors actually recoverable from the guest memory.
* Properly version the migrate data.
* Properly abstract the descriptors access from virtio-net to SVQ.
* Do not use VirtQueueElementOld but migrate directly VirtQueueElement.
* Replace lots of assertions with runtime conditionals.
* Other TODOs in the patch message or code changes.Thanks.
Eugenio Pérez (13):
vhost: add available descriptor list in SVQ
vhost: iterate only available descriptors at SVQ stop
vhost: merge avail list and next avail descriptors detach
vhost: addvhost_svq_save_inflight
virtio: Specifyuint32_tas VirtQueueElementOld members type
virtio: refactorqemu_get_virtqueue_element
virtio: refactorqemu_put_virtqueue_element
virtio: expose VirtQueueElementOld
virtio: addvmstate_virtqueue_element_old
virtio-net: Migrate vhost inflight descriptors
virtio-net: save inflight descriptors at vhost shutdown
vhost: exposevhost_svq_add_element
vdpa: Recover inflight descriptorshw/virtio/vhost-shadow-virtqueue.h | 9 ++ include/hw/virtio/virtio-net.h | 2 + include/hw/virtio/virtio.h | 32 ++++++ include/migration/vmstate.h | 22 ++++ hw/net/vhost_net.c | 56 ++++++++++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 52 +++++++-- hw/virtio/vhost-vdpa.c | 11 -- hw/virtio/virtio.c | 162 ++++++++++++++++++----------- 9 files changed, 392 insertions(+), 83 deletions(-)--
2.31.1
Quoted 12 lines · Jason Wang
Quoted 7 lines · Eugenio Pérez
The state of the descriptors (avail or used) may not be recoverable just looking at the guest memory. Out of order used descriptor may override previous avail ones in the descriptor table or avail vring.
Currently we're not migrating this status in net devices because virtio-net, vhost-kernel etc use the descriptors in order,
Note that this might not be the truth (when zerocopy is enabled).
Good point. So will virtio-net wait for those to complete then? How does qemu handle if there are still inflight descriptors?
Quoted 11 lines
Quoted 5 lines
so the information always
recoverable from guest's memory. However, vDPA devices may use them out of order, and other kind of devices like block need this support.Shadow virtqueue is able to track these and resend them at the destination.
As discussed, there's a bootstrap issue here:
When SVQ needs to be enabled on demand, do we still need another way to get inflight ones without the help of SVQ?
To send and retrieve the descriptor without SVQ needs to be developed on top of this. I should have made that more clear here in the cover letter.
Thanks!
Quoted 48 lines
Thanks
Quoted 45 lines
Add them to the virtio-net migration description so they are not lose in the process.
This is a very early RFC just to validate the first draft so expect leftovers. To fetch and request the descriptors from a device without SVQ need to be implemented on top. Some other notable pending items are:
* Do not send the descriptors actually recoverable from the guest memory.
* Properly version the migrate data.
* Properly abstract the descriptors access from virtio-net to SVQ.
* Do not use VirtQueueElementOld but migrate directly VirtQueueElement.
* Replace lots of assertions with runtime conditionals.
* Other TODOs in the patch message or code changes.Thanks.
Eugenio Pérez (13):
vhost: add available descriptor list in SVQ
vhost: iterate only available descriptors at SVQ stop
vhost: merge avail list and next avail descriptors detach
vhost: addvhost_svq_save_inflight
virtio: Specifyuint32_tas VirtQueueElementOld members type
virtio: refactorqemu_get_virtqueue_element
virtio: refactorqemu_put_virtqueue_element
virtio: expose VirtQueueElementOld
virtio: addvmstate_virtqueue_element_old
virtio-net: Migrate vhost inflight descriptors
virtio-net: save inflight descriptors at vhost shutdown
vhost: exposevhost_svq_add_element
vdpa: Recover inflight descriptorshw/virtio/vhost-shadow-virtqueue.h | 9 ++ include/hw/virtio/virtio-net.h | 2 + include/hw/virtio/virtio.h | 32 ++++++ include/migration/vmstate.h | 22 ++++ hw/net/vhost_net.c | 56 ++++++++++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 52 +++++++-- hw/virtio/vhost-vdpa.c | 11 -- hw/virtio/virtio.c | 162 ++++++++++++++++++----------- 9 files changed, 392 insertions(+), 83 deletions(-)--
2.31.1
Quoted 9 lines · Eugenio Perez Martin
Quoted 7 lines · Jason Wang
Quoted 5 lines · Eugenio Pérez
The state of the descriptors (avail or used) may not be recoverable just looking at the guest memory. Out of order used descriptor may override previous avail ones in the descriptor table or avail vring.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Currently we're not migrating this status in net devices because virtio-net, vhost-kernel etc use the descriptors in order,
Note that this might not be the truth (when zerocopy is enabled).
Good point. So will virtio-net wait for those to complete then?
Vhost-net will wait for all the inflight descriptors to be completed.
Quoted 2 lines
How
does qemu handle if there are still inflight descriptors?
It doesn't care right now. For networking devices, devices can choose to flush inflight before suspending. But this won't work for other type of device like block.
Thanks
Quoted 68 lines
Quoted 5 lines
Quoted 5 lines
so the information always
recoverable from guest's memory. However, vDPA devices may use them out of order, and other kind of devices like block need this support.Quoted 7 lines
Quoted 1 line
Shadow virtqueue is able to track these and resend them at the destination.
As discussed, there's a bootstrap issue here:
When SVQ needs to be enabled on demand, do we still need another way to get inflight ones without the help of SVQ?
To send and retrieve the descriptor without SVQ needs to be developed on top of this. I should have made that more clear here in the cover letter.
Thanks!
Quoted 48 lines
Thanks
Quoted 45 lines
Add them to the virtio-net migration description so they are not lose in the process.
This is a very early RFC just to validate the first draft so expect leftovers. To fetch and request the descriptors from a device without SVQ need to be implemented on top. Some other notable pending items are:
* Do not send the descriptors actually recoverable from the guest memory.
* Properly version the migrate data.
* Properly abstract the descriptors access from virtio-net to SVQ.
* Do not use VirtQueueElementOld but migrate directly VirtQueueElement.
* Replace lots of assertions with runtime conditionals.
* Other TODOs in the patch message or code changes.Thanks.
Eugenio Pérez (13):
vhost: add available descriptor list in SVQ
vhost: iterate only available descriptors at SVQ stop
vhost: merge avail list and next avail descriptors detach
vhost: addvhost_svq_save_inflight
virtio: Specifyuint32_tas VirtQueueElementOld members type
virtio: refactorqemu_get_virtqueue_element
virtio: refactorqemu_put_virtqueue_element
virtio: expose VirtQueueElementOld
virtio: addvmstate_virtqueue_element_old
virtio-net: Migrate vhost inflight descriptors
virtio-net: save inflight descriptors at vhost shutdown
vhost: exposevhost_svq_add_element
vdpa: Recover inflight descriptorshw/virtio/vhost-shadow-virtqueue.h | 9 ++ include/hw/virtio/virtio-net.h | 2 + include/hw/virtio/virtio.h | 32 ++++++ include/migration/vmstate.h | 22 ++++ hw/net/vhost_net.c | 56 ++++++++++ hw/net/virtio-net.c | 129 +++++++++++++++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 52 +++++++-- hw/virtio/vhost-vdpa.c | 11 -- hw/virtio/virtio.c | 162 ++++++++++++++++++----------- 9 files changed, 392 insertions(+), 83 deletions(-)--
2.31.1