From: Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
To: dev@dpdk.org
Cc: Daniel Verkamp <daniel.verkamp@intel.com>,
Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
Subject: [dpdk-dev] [PATCH v4 3/3] vhost: access VhostUsrMsg via packed struct
Date: Fri, 26 May 2017 13:59:15 +0200 [thread overview]
Message-ID: <1495799955-190227-4-git-send-email-dariuszx.stojaczyk@intel.com> (raw)
In-Reply-To: <1495799955-190227-1-git-send-email-dariuszx.stojaczyk@intel.com>
From: Daniel Verkamp <daniel.verkamp@intel.com>
Accessing fields of a packed struct through unaligned pointers is
undefined behavior. Instead of passing pointers to particular fields,
a pointer to the root struct should be used. This patch does exactly
that.
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
Signed-off-by: Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
---
v4: updated commit message
v3: fix checkpatch warnings
v2: remove gerrit id
lib/librte_vhost/vhost_user.c | 60 +++++++++++++++++++++++--------------------
1 file changed, 32 insertions(+), 28 deletions(-)
diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 2cc0b66..ab2f40a 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -197,11 +197,11 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
*/
static int
vhost_user_set_vring_num(struct virtio_net *dev,
- struct vhost_vring_state *state)
+ VhostUserMsg *msg)
{
- struct vhost_virtqueue *vq = dev->virtqueue[state->index];
+ struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
- vq->size = state->num;
+ vq->size = msg->payload.state.num;
if (dev->dequeue_zero_copy) {
vq->nr_zmbuf = 0;
@@ -334,7 +334,7 @@ qva_to_vva(struct virtio_net *dev, uint64_t qva)
* This function then converts these to our address space.
*/
static int
-vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
+vhost_user_set_vring_addr(struct virtio_net *dev, VhostUserMsg *msg)
{
struct vhost_virtqueue *vq;
@@ -342,11 +342,11 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
return -1;
/* addr->index refers to the queue index. The txq 1, rxq is 0. */
- vq = dev->virtqueue[addr->index];
+ vq = dev->virtqueue[msg->payload.addr.index];
/* The addresses are converted from QEMU virtual to Vhost virtual. */
vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
- addr->desc_user_addr);
+ msg->payload.addr.desc_user_addr);
if (vq->desc == 0) {
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) failed to find desc ring address.\n",
@@ -354,11 +354,11 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
return -1;
}
- dev = numa_realloc(dev, addr->index);
- vq = dev->virtqueue[addr->index];
+ dev = numa_realloc(dev, msg->payload.addr.index);
+ vq = dev->virtqueue[msg->payload.addr.index];
vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
- addr->avail_user_addr);
+ msg->payload.addr.avail_user_addr);
if (vq->avail == 0) {
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) failed to find avail ring address.\n",
@@ -367,7 +367,7 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
}
vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
- addr->used_user_addr);
+ msg->payload.addr.used_user_addr);
if (vq->used == 0) {
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) failed to find used ring address.\n",
@@ -384,7 +384,7 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
vq->last_avail_idx = vq->used->idx;
}
- vq->log_guest_addr = addr->log_guest_addr;
+ vq->log_guest_addr = msg->payload.addr.log_guest_addr;
LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
dev->vid, vq->desc);
@@ -403,10 +403,12 @@ vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
*/
static int
vhost_user_set_vring_base(struct virtio_net *dev,
- struct vhost_vring_state *state)
+ VhostUserMsg *msg)
{
- dev->virtqueue[state->index]->last_used_idx = state->num;
- dev->virtqueue[state->index]->last_avail_idx = state->num;
+ dev->virtqueue[msg->payload.state.index]->last_used_idx =
+ msg->payload.state.num;
+ dev->virtqueue[msg->payload.state.index]->last_avail_idx =
+ msg->payload.state.num;
return 0;
}
@@ -697,9 +699,9 @@ free_zmbufs(struct vhost_virtqueue *vq)
*/
static int
vhost_user_get_vring_base(struct virtio_net *dev,
- struct vhost_vring_state *state)
+ VhostUserMsg *msg)
{
- struct vhost_virtqueue *vq = dev->virtqueue[state->index];
+ struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
/* We have to stop the queue (virtio) if it is running. */
if (dev->flags & VIRTIO_DEV_RUNNING) {
@@ -710,10 +712,11 @@ vhost_user_get_vring_base(struct virtio_net *dev,
dev->flags &= ~VIRTIO_DEV_READY;
/* Here we are safe to get the last used index */
- state->num = vq->last_used_idx;
+ msg->payload.state.num = vq->last_used_idx;
RTE_LOG(INFO, VHOST_CONFIG,
- "vring base idx:%d file:%d\n", state->index, state->num);
+ "vring base idx:%d file:%d\n", msg->payload.state.index,
+ msg->payload.state.num);
/*
* Based on current qemu vhost-user implementation, this message is
* sent and only sent in vhost_vring_stop.
@@ -738,18 +741,19 @@ vhost_user_get_vring_base(struct virtio_net *dev,
*/
static int
vhost_user_set_vring_enable(struct virtio_net *dev,
- struct vhost_vring_state *state)
+ VhostUserMsg *msg)
{
- int enable = (int)state->num;
+ int enable = (int)msg->payload.state.num;
RTE_LOG(INFO, VHOST_CONFIG,
"set queue enable: %d to qp idx: %d\n",
- enable, state->index);
+ enable, msg->payload.state.index);
if (dev->notify_ops->vring_state_changed)
- dev->notify_ops->vring_state_changed(dev->vid, state->index, enable);
+ dev->notify_ops->vring_state_changed(dev->vid,
+ msg->payload.state.index, enable);
- dev->virtqueue[state->index]->enabled = enable;
+ dev->virtqueue[msg->payload.state.index]->enabled = enable;
return 0;
}
@@ -1038,17 +1042,17 @@ vhost_user_msg_handler(int vid, int fd)
break;
case VHOST_USER_SET_VRING_NUM:
- vhost_user_set_vring_num(dev, &msg.payload.state);
+ vhost_user_set_vring_num(dev, &msg);
break;
case VHOST_USER_SET_VRING_ADDR:
- vhost_user_set_vring_addr(dev, &msg.payload.addr);
+ vhost_user_set_vring_addr(dev, &msg);
break;
case VHOST_USER_SET_VRING_BASE:
- vhost_user_set_vring_base(dev, &msg.payload.state);
+ vhost_user_set_vring_base(dev, &msg);
break;
case VHOST_USER_GET_VRING_BASE:
- vhost_user_get_vring_base(dev, &msg.payload.state);
+ vhost_user_get_vring_base(dev, &msg);
msg.size = sizeof(msg.payload.state);
send_vhost_message(fd, &msg);
break;
@@ -1073,7 +1077,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
case VHOST_USER_SET_VRING_ENABLE:
- vhost_user_set_vring_enable(dev, &msg.payload.state);
+ vhost_user_set_vring_enable(dev, &msg);
break;
case VHOST_USER_SEND_RARP:
vhost_user_send_rarp(dev, &msg);
--
2.7.4
prev parent reply other threads:[~2017-05-26 8:29 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-11 10:23 [dpdk-dev] [PATCH 1/3] vhost: fix malloc in rte_vhost_get_mem_table Dariusz Stojaczyk
2017-05-11 10:23 ` [dpdk-dev] [PATCH 2/3] vhost: free virtio_net::guest_pages in vhost_backend_cleanup() Dariusz Stojaczyk
2017-05-11 10:23 ` [dpdk-dev] [PATCH 3/3] vhost: access VhostUsrMsg via packed struct Dariusz Stojaczyk
2017-05-11 10:56 ` [dpdk-dev] [PATCH v2 1/3] vhost: fix malloc in rte_vhost_get_mem_table Dariusz Stojaczyk
2017-05-11 10:56 ` [dpdk-dev] [PATCH v2 2/3] vhost: free virtio_net::guest_pages in vhost_backend_cleanup() Dariusz Stojaczyk
2017-05-11 10:56 ` [dpdk-dev] [PATCH v2 3/3] vhost: access VhostUsrMsg via packed struct Dariusz Stojaczyk
2017-05-11 11:42 ` [dpdk-dev] [PATCH v2 1/3] vhost: fix malloc in rte_vhost_get_mem_table Jens Freimann
2017-05-11 12:32 ` Stojaczyk, DariuszX
2017-05-11 14:33 ` [dpdk-dev] [PATCH v3 0/3] vhost undefined behavior fixes Dariusz Stojaczyk
2017-05-11 14:33 ` [dpdk-dev] [PATCH v3 1/3] vhost: fix malloc in rte_vhost_get_mem_table() Dariusz Stojaczyk
2017-05-11 13:56 ` Jens Freimann
2017-05-11 14:33 ` [dpdk-dev] [PATCH v3 2/3] vhost: free guest_pages in vhost_backend_cleanup() Dariusz Stojaczyk
2017-05-11 14:31 ` Jens Freimann
2017-05-11 14:33 ` [dpdk-dev] [PATCH v3 3/3] vhost: access VhostUsrMsg via packed struct Dariusz Stojaczyk
2017-05-22 7:01 ` Yuanhan Liu
2017-05-24 13:12 ` Stojaczyk, DariuszX
2017-05-26 5:58 ` Yuanhan Liu
2017-05-26 11:59 ` [dpdk-dev] [PATCH v4 0/3] vhost: undefined behavior fixes Dariusz Stojaczyk
2017-05-26 8:40 ` Yuanhan Liu
2017-05-26 11:59 ` [dpdk-dev] [PATCH v4 1/3] vhost: fix malloc in rte_vhost_get_mem_table() Dariusz Stojaczyk
2017-05-26 11:59 ` [dpdk-dev] [PATCH v4 2/3] vhost: free guest_pages in vhost_backend_cleanup() Dariusz Stojaczyk
2017-05-26 11:59 ` Dariusz Stojaczyk [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1495799955-190227-4-git-send-email-dariuszx.stojaczyk@intel.com \
--to=dariuszx.stojaczyk@intel.com \
--cc=daniel.verkamp@intel.com \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).