diff mbox series

[4/7] net/virtio: adapt Virtio-user status size

Message ID 20200911150805.79901-5-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers show
Series virtio-user: introduce vhost-vdpa backend | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Maxime Coquelin Sept. 11, 2020, 3:08 p.m. UTC
Set proper payload size for set/get status message. The payload
size varies according to backend types.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Signed-off-by: Patrick Fu <patrick.fu@intel.com>
---
 .../net/virtio/virtio_user/virtio_user_dev.c  | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

Comments

Xia, Chenbo Sept. 24, 2020, 5:25 a.m. UTC | #1
Hi Maxime,

> -----Original Message-----
> From: dev <dev-bounces@dpdk.org> On Behalf Of Maxime Coquelin
> Sent: Friday, September 11, 2020 11:08 PM
> To: dev@dpdk.org; Fu, Patrick <patrick.fu@intel.com>; amorenoz@redhat.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [dpdk-dev] [PATCH 4/7] net/virtio: adapt Virtio-user status size
> 
> Set proper payload size for set/get status message. The payload
> size varies according to backend types.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> Signed-off-by: Patrick Fu <patrick.fu@intel.com>
> ---
>  .../net/virtio/virtio_user/virtio_user_dev.c  | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c
> b/drivers/net/virtio/virtio_user/virtio_user_dev.c
> index 93274b2a94..753611ef42 100644
> --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
> +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
> @@ -838,14 +838,18 @@ virtio_user_send_status_update(struct
> virtio_user_dev *dev, uint8_t status)
>  	enum virtio_user_backend_type backend_type =
>  				virtio_user_backend_type(dev->path);
> 
> -	/* Vhost-user only for now */
> -	if (backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
> +	if (!(dev->protocol_features & (1ULL <<
> VHOST_USER_PROTOCOL_F_STATUS)))
>  		return 0;
> 
> -	if (!(dev->protocol_features & (1ULL <<
> VHOST_USER_PROTOCOL_F_STATUS)))
> +	if (backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
> +		ret = dev->ops->send_request(dev,
> +				VHOST_USER_SET_STATUS, &arg);
> +	else if (backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
> +		ret = dev->ops->send_request(dev,
> +				VHOST_USER_SET_STATUS, &status);
> +	else
>  		return 0;
> 
> -	ret = dev->ops->send_request(dev, VHOST_USER_SET_STATUS, &arg);
>  	if (ret) {
>  		PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
>  			     strerror(errno));
> @@ -858,7 +862,7 @@ virtio_user_send_status_update(struct virtio_user_dev
> *dev, uint8_t status)
>  int
>  virtio_user_update_status(struct virtio_user_dev *dev)
>  {
> -	uint64_t ret;
> +	uint8_t ret;

If you use uint8_t here, for vhost-vdpa, it's ok. But for vhost-user, with Adrian's
patch, it takes the status as uint64_t, and with below logic, it may overflow, right?

		switch (req) {
		case VHOST_USER_GET_FEATURES:
		case VHOST_USER_GET_STATUS:
		case VHOST_USER_GET_PROTOCOL_FEATURES:
			if (msg.size != sizeof(m.payload.u64)) {
				PMD_DRV_LOG(ERR, "Received bad msg size");
				return -1;
			}
			*((__u64 *)arg) = msg.payload.u64;
			break;

Thanks!
Chenbo

>  	int err;
>  	enum virtio_user_backend_type backend_type =
>  				virtio_user_backend_type(dev->path);
> @@ -876,11 +880,6 @@ virtio_user_update_status(struct virtio_user_dev *dev)
>  			     strerror(errno));
>  		return -1;
>  	}
> -	if (ret > UINT8_MAX) {
> -		PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
> -				"response 0x%" PRIx64 "\n", ret);
> -		return -1;
> -	}
> 
>  	dev->status = ret;
>  	PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
> --
> 2.26.2
Maxime Coquelin Sept. 24, 2020, 8:05 a.m. UTC | #2
On 9/24/20 7:25 AM, Xia, Chenbo wrote:
> Hi Maxime,
> 
>> -----Original Message-----
>> From: dev <dev-bounces@dpdk.org> On Behalf Of Maxime Coquelin
>> Sent: Friday, September 11, 2020 11:08 PM
>> To: dev@dpdk.org; Fu, Patrick <patrick.fu@intel.com>; amorenoz@redhat.com
>> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Subject: [dpdk-dev] [PATCH 4/7] net/virtio: adapt Virtio-user status size
>>
>> Set proper payload size for set/get status message. The payload
>> size varies according to backend types.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Signed-off-by: Patrick Fu <patrick.fu@intel.com>
>> ---
>>  .../net/virtio/virtio_user/virtio_user_dev.c  | 19 +++++++++----------
>>  1 file changed, 9 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c
>> b/drivers/net/virtio/virtio_user/virtio_user_dev.c
>> index 93274b2a94..753611ef42 100644
>> --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
>> +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
>> @@ -838,14 +838,18 @@ virtio_user_send_status_update(struct
>> virtio_user_dev *dev, uint8_t status)
>>  	enum virtio_user_backend_type backend_type =
>>  				virtio_user_backend_type(dev->path);
>>
>> -	/* Vhost-user only for now */
>> -	if (backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
>> +	if (!(dev->protocol_features & (1ULL <<
>> VHOST_USER_PROTOCOL_F_STATUS)))
>>  		return 0;
>>
>> -	if (!(dev->protocol_features & (1ULL <<
>> VHOST_USER_PROTOCOL_F_STATUS)))
>> +	if (backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
>> +		ret = dev->ops->send_request(dev,
>> +				VHOST_USER_SET_STATUS, &arg);
>> +	else if (backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
>> +		ret = dev->ops->send_request(dev,
>> +				VHOST_USER_SET_STATUS, &status);
>> +	else
>>  		return 0;
>>
>> -	ret = dev->ops->send_request(dev, VHOST_USER_SET_STATUS, &arg);
>>  	if (ret) {
>>  		PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
>>  			     strerror(errno));
>> @@ -858,7 +862,7 @@ virtio_user_send_status_update(struct virtio_user_dev
>> *dev, uint8_t status)
>>  int
>>  virtio_user_update_status(struct virtio_user_dev *dev)
>>  {
>> -	uint64_t ret;
>> +	uint8_t ret;
> 
> If you use uint8_t here, for vhost-vdpa, it's ok. But for vhost-user, with Adrian's
> patch, it takes the status as uint64_t, and with below logic, it may overflow, right?
> 
> 		switch (req) {
> 		case VHOST_USER_GET_FEATURES:
> 		case VHOST_USER_GET_STATUS:
> 		case VHOST_USER_GET_PROTOCOL_FEATURES:
> 			if (msg.size != sizeof(m.payload.u64)) {
> 				PMD_DRV_LOG(ERR, "Received bad msg size");
> 				return -1;
> 			}
> 			*((__u64 *)arg) = msg.payload.u64;
> 			break;

Yes, it could.
I can add a dedicated case for GET_STATUS, so that it can check it does
not overflow 8bits.

Thanks,
Maxime

> Thanks!
> Chenbo
> 
>>  	int err;
>>  	enum virtio_user_backend_type backend_type =
>>  				virtio_user_backend_type(dev->path);
>> @@ -876,11 +880,6 @@ virtio_user_update_status(struct virtio_user_dev *dev)
>>  			     strerror(errno));
>>  		return -1;
>>  	}
>> -	if (ret > UINT8_MAX) {
>> -		PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
>> -				"response 0x%" PRIx64 "\n", ret);
>> -		return -1;
>> -	}
>>
>>  	dev->status = ret;
>>  	PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
>> --
>> 2.26.2
>
diff mbox series

Patch

diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c
index 93274b2a94..753611ef42 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -838,14 +838,18 @@  virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status)
 	enum virtio_user_backend_type backend_type =
 				virtio_user_backend_type(dev->path);
 
-	/* Vhost-user only for now */
-	if (backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
+	if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
 		return 0;
 
-	if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
+	if (backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
+		ret = dev->ops->send_request(dev,
+				VHOST_USER_SET_STATUS, &arg);
+	else if (backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
+		ret = dev->ops->send_request(dev,
+				VHOST_USER_SET_STATUS, &status);
+	else
 		return 0;
 
-	ret = dev->ops->send_request(dev, VHOST_USER_SET_STATUS, &arg);
 	if (ret) {
 		PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
 			     strerror(errno));
@@ -858,7 +862,7 @@  virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status)
 int
 virtio_user_update_status(struct virtio_user_dev *dev)
 {
-	uint64_t ret;
+	uint8_t ret;
 	int err;
 	enum virtio_user_backend_type backend_type =
 				virtio_user_backend_type(dev->path);
@@ -876,11 +880,6 @@  virtio_user_update_status(struct virtio_user_dev *dev)
 			     strerror(errno));
 		return -1;
 	}
-	if (ret > UINT8_MAX) {
-		PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
-				"response 0x%" PRIx64 "\n", ret);
-		return -1;
-	}
 
 	dev->status = ret;
 	PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"