From 91bdec96c7a4e7766adeb60066ce7bf7b8e3553e Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Fri, 30 Mar 2018 19:24:17 +0200 Subject: [PATCH] virtio/vhost_user: set NEEDS_RESET status bit if necessary This protects against brute-force initialization that virtio is not really prepared for. Most importantly, this behavior makes us Virtio spec compliant (3.1 Device Init.). Change-Id: I6d26f3d8b5cce488a71068777531b78538489662 Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/405917 Tested-by: SPDK Automated Test System Reviewed-by: Pawel Wodkowski Reviewed-by: Daniel Verkamp Reviewed-by: Jim Harris --- lib/virtio/virtio_user.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/virtio/virtio_user.c b/lib/virtio/virtio_user.c index a4459bb2a..226885470 100644 --- a/lib/virtio/virtio_user.c +++ b/lib/virtio/virtio_user.c @@ -236,14 +236,23 @@ static void virtio_user_set_status(struct virtio_dev *vdev, uint8_t status) { struct virtio_user_dev *dev = vdev->ctx; + int rc = 0; - if (status & VIRTIO_CONFIG_S_DRIVER_OK) { - virtio_user_start_device(vdev); + if ((dev->status & VIRTIO_CONFIG_S_NEEDS_RESET) && + status != VIRTIO_CONFIG_S_RESET) { + rc = -1; + } else if (status & VIRTIO_CONFIG_S_DRIVER_OK) { + rc = virtio_user_start_device(vdev); } else if (status == VIRTIO_CONFIG_S_RESET && (dev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { - virtio_user_stop_device(vdev); + rc = virtio_user_stop_device(vdev); + } + + if (rc != 0) { + dev->status |= VIRTIO_CONFIG_S_NEEDS_RESET; + } else { + dev->status = status; } - dev->status = status; } static uint8_t