Re: [dpdk-dev] [PATCH v6 4/5] vhost: unify message handling function signature

2018-10-05 Thread Nikolay Nikolaev
On Tue, Oct 2, 2018 at 11:59 AM Maxime Coquelin
 wrote:
>
>
>
> On 09/24/2018 10:17 PM, Nikolay Nikolaev wrote:
> > Each vhost-user message handling function will return an int result
> > which is described in the new enum vh_result: error, OK and reply.
> > All functions will now have two arguments, virtio_net double pointer
> > and VhostUserMsg pointer.
> >
> > Signed-off-by: Nikolay Nikolaev 
> > ---
> >   lib/librte_vhost/vhost_user.c |  211 
> > -
> >   1 file changed, 125 insertions(+), 86 deletions(-)
> >
> > diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
> > index 77905dda0..e1b705fa7 100644
> > --- a/lib/librte_vhost/vhost_user.c
> > +++ b/lib/librte_vhost/vhost_user.c
> > @@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
> >   [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
> >   };
> >
> > +/* The possible results of a message handling function */
> > +enum vh_result {
> > + /* Message handling failed */
> > + VH_RESULT_ERR   = -1,
> > + /* Message handling successful */
> > + VH_RESULT_OK=  0,
> > + /* Message handling successful and reply prepared */
> > + VH_RESULT_REPLY =  1,
> > +};
> > +
>
>
> > -vhost_user_get_vring_base(struct virtio_net *dev,
> > +vhost_user_get_vring_base(struct virtio_net **pdev,
> > struct VhostUserMsg *msg)
> >   {
> > + struct virtio_net *dev = *pdev;
> >   struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
> >
> >   /* We have to stop the queue (virtio) if it is running. */
> > @@ -1135,7 +1161,7 @@ vhost_user_get_vring_base(struct virtio_net *dev,
> >
> >   msg->size = sizeof(msg->payload.state);
> >
> > - return 0;
> > + return VH_RESULT_OK;
> >   }
>
> VH_RESULT_REPLY here.
>
> > -static void
> > -vhost_user_get_protocol_features(struct virtio_net *dev,
> > +static int
> > +vhost_user_get_protocol_features(struct virtio_net **pdev,
> >struct VhostUserMsg *msg)
> >   {
> > + struct virtio_net *dev = *pdev;
> >   uint64_t features, protocol_features;
> >
> >   rte_vhost_driver_get_features(dev->ifname, &features);
> > @@ -1189,40 +1217,46 @@ vhost_user_get_protocol_features(struct virtio_net 
> > *dev,
> >
> >   msg->payload.u64 = protocol_features;
> >   msg->size = sizeof(msg->payload.u64);
> > +
> > + return VH_RESULT_OK;
> >   }
>
> Ditto.
>
> I have the patches to fix these, it will be posted as preliminary part
> of my postcopy series.
>
> Please, next time, test your series before posting.

Oh, sorry for that. I obviously underestimated the complexity of the refactoring
and indeed did not test beyond compilation and visual code inspect,
which of course can not be excused.
I guess at least running the vhost sample against qemu would have
shown these issues.

I'm sorry again, I could have done better.

regards,
Nikolay Nikolaev

>
> Thanks,
> Maxime


[dpdk-dev] [PATCH v1 0/5] vhost_user.c code cleanup

2018-06-26 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduce a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to introduce any functional changes.

---

Nikolay Nikolaev (5):
  vhost: unify VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  391 ++---
 1 file changed, 208 insertions(+), 183 deletions(-)

--
Signature


[dpdk-dev] [PATCH v1 1/5] vhost: unify VhostUserMsg usage

2018-06-26 Thread Nikolay Nikolaev
Use the typedef version of struct VhostUserMsg. Also unify the related
parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   50 +
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 26cfebec0..9b5598871 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -706,10 +706,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -730,7 +730,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -766,7 +766,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -905,16 +905,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -926,17 +926,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct 
VhostUserMsg *pmsg)
 }
 
 static void
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1058,7 +1058,7 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
 
 static void
 vhost_user_get_protocol_features(struct virtio_net *dev,
-struct VhostUserMsg *msg)
+VhostUserMsg *msg)
 {
uint64_t features, protocol_features;
 
@@ -1089,7 +1089,7 @@ vhost_user_set_protocol_features(struct virtio_net *dev,
 }
 
 static int
-vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_set_log_base(struct virtio_net *dev, VhostUserMsg *msg)
 {
int fd = msg->fds[0];
uint64_t size, off;
@@ -1156,7 +1156,7 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
  */
 static int
-vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_send_rarp(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint8_t *mac = (uint8_t *)&msg->payload.u64;
struct rte_vdpa_device *vdpa_dev;
@@ -1185,7 +1185,7 @@ vhost_user_send_rarp(struct virtio_net *dev, struct 
VhostUserMsg *msg)
 }
 
 static int
-vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_net_set_mtu(struct virtio_net *dev, VhostUserMsg *msg)
 {
if (msg->payload.u64 < VIRTIO_MIN_MTU ||
msg->payload.u64 > VIRTIO_MAX_MTU) {
@@ -1201,7 +1201,7 @@ vhost_user_net_se

[dpdk-dev] [PATCH v1 3/5] vhost: handle unsupported message types in functions

2018-06-26 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 1bc3d1629..d999c80ed 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -933,6 +933,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net *dev __rte_unused,
+   VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static void
 vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
@@ -1159,6 +1167,13 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
return 0;
 }
 
+static void
+vhost_user_set_log_fd(struct virtio_net *dev __rte_unused, VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1602,8 +1617,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1629,9 +1643,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM:



[dpdk-dev] [PATCH v1 2/5] vhost: make message handling functions prepare the reply

2018-06-26 Thread Nikolay Nikolaev
As VhostUserMsg structure is resued to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   26 --
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 9b5598871..1bc3d1629 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1022,6 +1030,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1144,6 +1154,8 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1557,8 +1569,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1588,9 +1599,6 @@ vhost_user_msg_handler(int vid, int fd)
 
case VHOST_USER_SET_LOG_BASE:
vhost_user_set_log_base(dev, &msg);
-
-   /* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1610,7 +1618,6 @@ vhost_user_msg_handler(int vid, int fd)
 
case VHOST_USER_GET_VRING_BASE:
vhost_user_get_vring_base(dev, &msg);
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1628,8 +1635,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v1 4/5] vhost: unify message handling function signature

2018-06-26 Thread Nikolay Nikolaev
Each vhost-user message handlign function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  217 -
 1 file changed, 129 insertions(+), 88 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index d999c80ed..dd47d84c7 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handlig failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handlig successful */
+   VH_RESULT_OK=  0,
+   /* Message handlig successful and reply prepared */
+   VH_RESULT_REPLY =  1
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   VhostUserMsg * msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   VhostUserMsg * msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct virtio_net *dev,
+vhost_user_set_vring_num(struct virtio_net **pdev,

[dpdk-dev] [PATCH v1 5/5] vhost: message handling implemented as a callback array

2018-06-26 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  144 -
 1 file changed, 55 insertions(+), 89 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index dd47d84c7..e62164d63 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1379,6 +1379,36 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*message_handler)(struct virtio_net **pdev, VhostUserMsg * msg);
+static message_handler vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR]  = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES]  = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES]  = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM]  = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE]  = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP]  = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU]  = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD]  = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG]  = vhost_user_iotlb_msg,
+   [VHOST_USER_CRYPTO_CREATE_SESS] = NULL,
+   [VHOST_USER_CRYPTO_CLOSE_SESS] = NULL,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, VhostUserMsg *msg)
@@ -1623,97 +1653,33 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   if (ret)
-   return -1;
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_MEM_TABLE:
-   ret = vhost_user_set_mem_table(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_LOG_BASE:
-   ret = vhost_user_set_log_base(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_LOG_FD:
-   ret = vhost_user_set_log_fd(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_NUM:
-   ret = vhost_user_set_vring_num(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_ADDR:
-   ret = vhost_user_set_vring_addr(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_BASE:
-   ret = vhost_user_set_vring_base(&dev, &msg);
-   break;
 
-   case VHOST_USER_GET_VRING_BASE:
-   ret = vhost_user_get_vring_base(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
+   int request = msg.request.master;
+   if (vhost_message_handlers[request]) {
+   ret = vhost_message_handlers[request](&dev, &msg);
 
-   case VHOST_USER_SET_VRING_KICK:
-   ret = vhost_user_set_vring_kick(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_CALL:
-   ret = vhost_user_set_vring_call(&dev, &msg);
-   b

[dpdk-dev] [PATCH v2 1/5] vhost: unify VhostUserMsg usage

2018-07-19 Thread Nikolay Nikolaev
Use the typedef version of struct VhostUserMsg. Also unify the related
parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   50 +
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index dc53ff712..d51616695 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -778,10 +778,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -802,7 +802,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -838,7 +838,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -987,16 +987,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -1008,17 +1008,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
struct VhostUserMsg *pmsg)
 }
 
 static void
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1145,7 +1145,7 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
 
 static void
 vhost_user_get_protocol_features(struct virtio_net *dev,
-struct VhostUserMsg *msg)
+VhostUserMsg *msg)
 {
uint64_t features, protocol_features;
 
@@ -1176,7 +1176,7 @@ vhost_user_set_protocol_features(struct virtio_net *dev,
 }
 
 static int
-vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_set_log_base(struct virtio_net *dev, VhostUserMsg *msg)
 {
int fd = msg->fds[0];
uint64_t size, off;
@@ -1243,7 +1243,7 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
  */
 static int
-vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_send_rarp(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint8_t *mac = (uint8_t *)&msg->payload.u64;
struct rte_vdpa_device *vdpa_dev;
@@ -1272,7 +1272,7 @@ vhost_user_send_rarp(struct virtio_net *dev, struct 
VhostUserMsg *msg)
 }
 
 static int
-vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_net_set_mtu(struct virtio_net *dev, VhostUserMsg *msg)
 {
if (msg->payload.u64 < VIRTIO_MIN_MTU ||
msg->payload.u64 > VIRTIO_MAX_MTU) {
@@ -1288,7 +1288,7 @@ vhost_user_net_se

[dpdk-dev] [PATCH v2 0/5] vhost_user.c code cleanup

2018-07-19 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduce a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to introduce any functional changes.

v2 changes:
 - Fix the comments by Tiwei Bie
 - Keep the old behavior
   - Fall through when the callback returns VH_RESULT_ERR
   - Fall through if the request is out of range

---

Nikolay Nikolaev (5):
  vhost: unify VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  392 ++---
 1 file changed, 208 insertions(+), 184 deletions(-)

--
Signature


[dpdk-dev] [PATCH v2 3/5] vhost: handle unsupported message types in functions

2018-07-19 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index e97b2d563..b408460f0 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1015,6 +1015,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net *dev __rte_unused,
+   VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static void
 vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
@@ -1246,6 +1254,13 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
return 0;
 }
 
+static void
+vhost_user_set_log_fd(struct virtio_net *dev __rte_unused, VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1689,8 +1704,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1716,9 +1730,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM:



[dpdk-dev] [PATCH v2 4/5] vhost: unify message handling function signature

2018-07-19 Thread Nikolay Nikolaev
Each vhost-user message handling function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  219 -
 1 file changed, 130 insertions(+), 89 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index b408460f0..6b39d1e30 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handling failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handling successful */
+   VH_RESULT_OK=  0,
+   /* Message handling successful and reply prepared */
+   VH_RESULT_REPLY =  1,
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   VhostUserMsg * msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   VhostUserMsg * msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct virtio_net *dev,
+vhost_user_set_vring_num(struct virtio_net **pdev,

[dpdk-dev] [PATCH v2 2/5] vhost: make message handling functions prepare the reply

2018-07-19 Thread Nikolay Nikolaev
As VhostUserMsg structure is resued to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   26 --
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index d51616695..e97b2d563 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1109,6 +1117,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1231,6 +1241,8 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1644,8 +1656,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1675,9 +1686,6 @@ vhost_user_msg_handler(int vid, int fd)
 
case VHOST_USER_SET_LOG_BASE:
vhost_user_set_log_base(dev, &msg);
-
-   /* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1697,7 +1705,6 @@ vhost_user_msg_handler(int vid, int fd)
 
case VHOST_USER_GET_VRING_BASE:
vhost_user_get_vring_base(dev, &msg);
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1715,8 +1722,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v2 5/5] vhost: message handling implemented as a callback array

2018-07-19 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  143 +++--
 1 file changed, 54 insertions(+), 89 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 6b39d1e30..1b164df27 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1466,6 +1466,34 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, VhostUserMsg 
* msg);
+static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, VhostUserMsg *msg)
@@ -1618,6 +1646,7 @@ vhost_user_msg_handler(int vid, int fd)
int ret;
int unlock_required = 0;
uint32_t skip_master = 0;
+   int request;
 
dev = get_device(vid);
if (dev == NULL)
@@ -1710,97 +1739,33 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   if (ret)
-   return -1;
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_MEM_TABLE:
-   ret = vhost_user_set_mem_table(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_LOG_BASE:
-   ret = vhost_user_set_log_base(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_LOG_FD:
-   ret = vhost_user_set_log_fd(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_NUM:
-   ret = vhost_user_set_vring_num(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_ADDR:
-   ret = vhost_user_set_vring_addr(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_BASE:
-   ret = vhost_user_set_vring_base(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_VRING_BASE:
-   ret = vhost_user_get_vring_base(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_KICK:
-   ret = vhost_user_set_vring_kick(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_CALL:
-   ret = vhost_user_set_vring_call(&dev, &msg);
-   break;
-

Re: [dpdk-dev] [PATCH v2 0/5] vhost_user.c code cleanup

2018-09-12 Thread Nikolay Nikolaev
Hello Maxime,

I'll rebase and fix Ilya's comments. Thanks for the reviews.

regards,
Nikolay Nikolaev

On Tue, Sep 11, 2018 at 12:38 PM Maxime Coquelin 
wrote:

> Hi Nikolay,
>
> On 07/19/2018 09:13 PM, Nikolay Nikolaev wrote:
> > vhost: vhost_user.c code cleanup
> >
> > This patchesries introduce a set of code redesigns in vhost_user.c.
> >
> > The goal is to unify and simplify vhost-user message handling. The
> > patches do not intend to introduce any functional changes.
> >
> > v2 changes:
> >   - Fix the comments by Tiwei Bie
> >   - Keep the old behavior
> > - Fall through when the callback returns VH_RESULT_ERR
> > - Fall through if the request is out of range
> >
> > ---
> >
> > Nikolay Nikolaev (5):
> >vhost: unify VhostUserMsg usage
> >vhost: make message handling functions prepare the reply
> >vhost: handle unsupported message types in functions
> >vhost: unify message handling function signature
> >vhost: message handling implemented as a callback array
> >
> >
> >   lib/librte_vhost/vhost_user.c |  392
> ++---
> >   1 file changed, 208 insertions(+), 184 deletions(-)
> >
> > --
> > Signature
> >
>
> Could you please rebase and fix the series (see Iliya comments) on top
> of git://dpdk.org/next/dpdk-next-virtio master branch?
>
> Thanks,
> Maxime
>


[dpdk-dev] [PATCH v3 2/5] vhost: make message handling functions prepare the reply

2018-09-14 Thread Nikolay Nikolaev
As VhostUserMsg structure is reused to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   24 
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index b05b57670..3a00d53cf 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1117,6 +1125,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1244,6 +1254,8 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1657,8 +1669,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1689,7 +1700,6 @@ vhost_user_msg_handler(int vid, int fd)
if (ret)
goto skip_to_reply;
/* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1711,7 +1721,6 @@ vhost_user_msg_handler(int vid, int fd)
ret = vhost_user_get_vring_base(dev, &msg);
if (ret)
goto skip_to_reply;
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1729,8 +1738,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v3 0/5] vhost: vhost_user.c code cleanup

2018-09-14 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduces a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to make any functional changes.

v3 changes:
 - rebased on top of git://dpdk.org/next/dpdk-next-virtio dead0602
 - introduce VH_RESULT_FATAL (Maxime Coquelin)
 - vhost_user_set_features return VH_RESULT_FATAL on failure.
   This allows keeping the propagate error logic (Ilya Maximets)
 - fixed vhost_user_set_vring_kick and vhost_user_set_protocol_features
   return VH_RESULT_ERR upon failure
 - fixed missing break in case VH_RESULT_ERR (Ilya Maximets)
 - fixed a type on the description of 2/5 patch (Maxime Coquelin)

v2 changes:
 - Fix the comments by Tiwei Bie
 - Keep the old behavior
   - Fall through when the callback returns VH_RESULT_ERR
   - Fall through if the request is out of range

---

Nikolay Nikolaev (5):
  vhost: unify VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  402 ++---
 1 file changed, 216 insertions(+), 186 deletions(-)

--
Signature


[dpdk-dev] [PATCH v3 1/5] vhost: unify VhostUserMsg usage

2018-09-14 Thread Nikolay Nikolaev
Use the typedef version of struct VhostUserMsg. Also unify the related
parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   50 +
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 63d145b2d..b05b57670 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -780,10 +780,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -804,7 +804,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -845,7 +845,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -994,16 +994,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -1015,17 +1015,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
struct VhostUserMsg *pmsg)
 }
 
 static int
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1153,7 +1153,7 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
 
 static void
 vhost_user_get_protocol_features(struct virtio_net *dev,
-struct VhostUserMsg *msg)
+VhostUserMsg *msg)
 {
uint64_t features, protocol_features;
 
@@ -1189,7 +1189,7 @@ vhost_user_set_protocol_features(struct virtio_net *dev,
 }
 
 static int
-vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_set_log_base(struct virtio_net *dev, VhostUserMsg *msg)
 {
int fd = msg->fds[0];
uint64_t size, off;
@@ -1256,7 +1256,7 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
  */
 static int
-vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_send_rarp(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint8_t *mac = (uint8_t *)&msg->payload.u64;
struct rte_vdpa_device *vdpa_dev;
@@ -1285,7 +1285,7 @@ vhost_user_send_rarp(struct virtio_net *dev, struct 
VhostUserMsg *msg)
 }
 
 static int
-vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
+vhost_user_net_set_mtu(struct virtio_net *dev, VhostUserMsg *msg)
 {
if (msg->payload.u64 < VIRTIO_MIN_MTU ||
msg->payload.u64 > VIRTIO_MAX_MTU) {
@@ -1301,7 +1301,7 @@ vhost_user_net_se

[dpdk-dev] [PATCH v3 4/5] vhost: unify message handling function signature

2018-09-14 Thread Nikolay Nikolaev
Each vhost-user message handling function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  213 -
 1 file changed, 126 insertions(+), 87 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index c38b0fd70..81c0396de 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handling failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handling successful */
+   VH_RESULT_OK=  0,
+   /* Message handling successful and reply prepared */
+   VH_RESULT_REPLY =  1,
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   VhostUserMsg * msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   VhostUserMsg * msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct virtio_net *dev,
+vhost_user_set_vring_num(struct virtio_net **pdev,

[dpdk-dev] [PATCH v3 3/5] vhost: handle unsupported message types in functions

2018-09-14 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 3a00d53cf..c38b0fd70 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1022,6 +1022,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net *dev __rte_unused,
+   VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static int
 vhost_user_set_vring_kick(struct virtio_net **pdev, VhostUserMsg *msg)
 {
@@ -1259,6 +1267,13 @@ vhost_user_set_log_base(struct virtio_net *dev, 
VhostUserMsg *msg)
return 0;
 }
 
+static void
+vhost_user_set_log_fd(struct virtio_net *dev __rte_unused, VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1703,8 +1718,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1732,9 +1746,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM:



[dpdk-dev] [PATCH v3 5/5] vhost: message handling implemented as a callback array

2018-09-14 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Failing vhost_user_set_features is fatal and all processing should
stop immediately and propagate the error to the upper layers. Change
the code accordingly to reflect that.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  161 +
 1 file changed, 66 insertions(+), 95 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 81c0396de..9004e1ac8 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -73,6 +73,8 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
 
 /* The possible results of a message handling function */
 enum vh_result {
+   /* Message handling failed with an unrecoverable error */
+   VH_RESULT_FATAL = -2,
/* Message handling failed */
VH_RESULT_ERR   = -1,
/* Message handling successful */
@@ -206,7 +208,7 @@ vhost_user_set_features(struct virtio_net **pdev, 
VhostUserMsg *msg)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return VH_RESULT_ERR;
+   return VH_RESULT_FATAL;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
@@ -222,7 +224,7 @@ vhost_user_set_features(struct virtio_net **pdev, 
VhostUserMsg *msg)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return VH_RESULT_ERR;
+   return VH_RESULT_FATAL;
}
 
if (dev->notify_ops->features_changed)
@@ -1477,6 +1479,34 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, VhostUserMsg 
* msg);
+static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, VhostUserMsg *msg)
@@ -1629,6 +1659,7 @@ vhost_user_msg_handler(int vid, int fd)
int ret;
int unlock_required = 0;
uint32_t skip_master = 0;
+   int request;
 
dev = get_device(vid);
if (dev == NULL)
@@ -1721,100 +1752,40 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev,

Re: [dpdk-dev] [PATCH v3 5/5] vhost: message handling implemented as a callback array

2018-09-19 Thread Nikolay Nikolaev
On Wed, Sep 19, 2018 at 10:37 AM Maxime Coquelin
 wrote:
>
>
>
> On 09/15/2018 07:20 AM, Nikolay Nikolaev wrote:
> > Introduce vhost_message_handlers, which maps the message request
> > type to the message handler. Then replace the switch construct
> > with a map and call.
> >
> > Failing vhost_user_set_features is fatal and all processing should
> > stop immediately and propagate the error to the upper layers. Change
> > the code accordingly to reflect that.
> >
> > Signed-off-by: Nikolay Nikolaev 
> > ---
> >   lib/librte_vhost/vhost_user.c |  161 
> > +
> >   1 file changed, 66 insertions(+), 95 deletions(-)
> >
> > diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
> > index 81c0396de..9004e1ac8 100644
> > --- a/lib/librte_vhost/vhost_user.c
> > +++ b/lib/librte_vhost/vhost_user.c
> > @@ -73,6 +73,8 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
> >
> >   /* The possible results of a message handling function */
> >   enum vh_result {
> > + /* Message handling failed with an unrecoverable error */
> > + VH_RESULT_FATAL = -2,
> >   /* Message handling failed */
> >   VH_RESULT_ERR   = -1,
> >   /* Message handling successful */
> > @@ -206,7 +208,7 @@ vhost_user_set_features(struct virtio_net **pdev, 
> > VhostUserMsg *msg)
> >   RTE_LOG(ERR, VHOST_CONFIG,
> >   "(%d) received invalid negotiated features.\n",
> >   dev->vid);
> > - return VH_RESULT_ERR;
> > + return VH_RESULT_FATAL;
> >   }
> >
> >   if (dev->flags & VIRTIO_DEV_RUNNING) {
> > @@ -222,7 +224,7 @@ vhost_user_set_features(struct virtio_net **pdev, 
> > VhostUserMsg *msg)
> >   RTE_LOG(ERR, VHOST_CONFIG,
> >   "(%d) features changed while device is 
> > running.\n",
> >   dev->vid);
> > - return VH_RESULT_ERR;
> > + return VH_RESULT_FATAL;
> >   }
> >
> >   if (dev->notify_ops->features_changed)
> > @@ -1477,6 +1479,34 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, 
> > VhostUserMsg *msg)
> >   return VH_RESULT_OK;
> >   }
> >
> > +typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, 
> > VhostUserMsg * msg);
> > +static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
> > + [VHOST_USER_NONE] = NULL,
> > + [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
> > + [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
> > + [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
> > + [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
> > + [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
> > + [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
> > + [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
> > + [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
> > + [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
> > + [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
> > + [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
> > + [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
> > + [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
> > + [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
> > + [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
> > + [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
> > + [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
> > + [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
> > + [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
> > + [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
> > + [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
> > + [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
> > +};
> > +
> > +
> >   /* return bytes# of read on success or negative val on failure. */
> >   static int
> >   read_vhost_message(int sockfd, VhostUserMsg *msg)
> > @@ -1629,6 +1659,7 @@ vhost_user_msg_handler(int vid, int fd)
> >   int ret;
> >   int unlock_required = 0;
> >   uint32_t skip_master = 0;
> > + int request;
> >
> >   dev = get_device(vid);
> >   if (dev == NULL)
> > @@ -1721,100 +1752,40 @@ vhost_user_msg_handler(int vid, int fd)
&

[dpdk-dev] [PATCH v4 1/5] vhost: unify struct VhostUserMsg usage

2018-09-22 Thread Nikolay Nikolaev
Do not use the typedef version of struct VhostUserMsg. Also unify the
related parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   41 +
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 63d145b2d..505db3bfc 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -250,7 +250,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
  */
 static int
 vhost_user_set_vring_num(struct virtio_net *dev,
-VhostUserMsg *msg)
+struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
 
@@ -611,7 +611,7 @@ translate_ring_addresses(struct virtio_net *dev, int 
vq_index)
  * This function then converts these to our address space.
  */
 static int
-vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
+vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq;
struct vhost_vring_addr *addr = &msg->payload.addr;
@@ -648,7 +648,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, 
VhostUserMsg *msg)
  */
 static int
 vhost_user_set_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
dev->virtqueue[msg->payload.state.index]->last_used_idx  =
msg->payload.state.num;
@@ -780,10 +780,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -804,7 +804,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -845,7 +845,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -994,16 +994,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -1015,17 +1015,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
struct VhostUserMsg *pmsg)
 }
 
 static int
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1073,7 +1073,7 @@ free_zmbufs(struct vhost_virtqueue *vq)
  */
 static int
 vhost_user_get_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtque

[dpdk-dev] [PATCH v4 0/5] vhost: vhost_user.c code cleanup

2018-09-22 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduce a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to introduce any functional changes.

v4 changes:
 - use struct VhostUserMsg as the coding style guide suggests (Anatoly Burakov)
 - VH_RESULT_FATAL is removed as not needed anymore (Maxime Coquelin)

v3 changes:
 - rebased on top of git://dpdk.org/next/dpdk-next-virtio dead0602
 - introduce VH_RESULT_FATAL (Maxime Coquelin)
 - vhost_user_set_features return VH_RESULT_FATAL on failure.
   This allows keeping the propagate error logic (Ilya Maximets)
 - fixed vhost_user_set_vring_kick and vhost_user_set_protocol_features
   return VH_RESULT_ERR upon failure
 - fixed missing break in case VH_RESULT_ERR (Ilya Maximets)
 - fixed a type on the description of 2/5 patch (Maxime Coquelin)

v2 changes:
 - Fix the comments by Tiwei Bie
 - Keep the old behavior
   - Fall through when the callback returns VH_RESULT_ERR
   - Fall through if the request is out of range

---

Nikolay Nikolaev (5):
  vhost: unify struct VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  393 ++---
 1 file changed, 208 insertions(+), 185 deletions(-)

--
Signature


[dpdk-dev] [PATCH v4 3/5] vhost: handle unsupported message types in functions

2018-09-22 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index b20aa6adc..1627d594e 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1022,6 +1022,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net *dev __rte_unused,
+   VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static int
 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
@@ -1259,6 +1267,13 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
return 0;
 }
 
+static void
+vhost_user_set_log_fd(struct virtio_net *dev __rte_unused, VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1704,8 +1719,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1733,9 +1747,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM:



[dpdk-dev] [PATCH v4 2/5] vhost: make message handling functions prepare the reply

2018-09-22 Thread Nikolay Nikolaev
As VhostUserMsg structure is reused to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   24 
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 505db3bfc..b20aa6adc 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1117,6 +1125,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1244,6 +1254,8 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1658,8 +1670,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1690,7 +1701,6 @@ vhost_user_msg_handler(int vid, int fd)
if (ret)
goto skip_to_reply;
/* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1712,7 +1722,6 @@ vhost_user_msg_handler(int vid, int fd)
ret = vhost_user_get_vring_base(dev, &msg);
if (ret)
goto skip_to_reply;
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1730,8 +1739,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v4 4/5] vhost: unify message handling function signature

2018-09-22 Thread Nikolay Nikolaev
Each vhost-user message handling function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  213 -
 1 file changed, 126 insertions(+), 87 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 1627d594e..051740477 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handling failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handling successful */
+   VH_RESULT_OK=  0,
+   /* Message handling successful and reply prepared */
+   VH_RESULT_REPLY =  1,
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   VhostUserMsg * msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   VhostUserMsg * msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct virtio_net *dev,
+vhost_user_set_vring_num(struct virtio_net **pdev,
 

[dpdk-dev] [PATCH v4 5/5] vhost: message handling implemented as a callback array

2018-09-22 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Failing vhost_user_set_features is fatal and all processing should
stop immediately and propagate the error to the upper layers. Change
the code accordingly to reflect that.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  149 +++--
 1 file changed, 56 insertions(+), 93 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 051740477..0c2faa6af 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1477,6 +1477,34 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, VhostUserMsg 
* msg);
+static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
@@ -1630,6 +1658,7 @@ vhost_user_msg_handler(int vid, int fd)
int ret;
int unlock_required = 0;
uint32_t skip_master = 0;
+   int request;
 
dev = get_device(vid);
if (dev == NULL)
@@ -1722,100 +1751,34 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_MEM_TABLE:
-   ret = vhost_user_set_mem_table(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_LOG_BASE:
-   ret = vhost_user_set_log_base(&dev, &msg);
-   if (ret)
-   goto skip_to_reply;
-   /* it needs a reply */
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_LOG_FD:
-   ret = vhost_user_set_log_fd(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_NUM:
-   ret = vhost_user_set_vring_num(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_ADDR:
-   ret = vhost_user_set_vring_addr(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_BASE:
-   ret = vhost_user_set_vring_base(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_VRING_BASE:
-   ret = vhost_user_get_vring_base(&dev, &msg);
-   if (ret)
-   goto skip_to_reply;
-   send_vhost_reply(fd, &msg);
-  

[dpdk-dev] [PATCH v5 1/5] vhost: unify struct VhostUserMsg usage

2018-09-24 Thread Nikolay Nikolaev
Do not use the typedef version of struct VhostUserMsg. Also unify the
related parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   41 +
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 63d145b2d..505db3bfc 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -250,7 +250,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
  */
 static int
 vhost_user_set_vring_num(struct virtio_net *dev,
-VhostUserMsg *msg)
+struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
 
@@ -611,7 +611,7 @@ translate_ring_addresses(struct virtio_net *dev, int 
vq_index)
  * This function then converts these to our address space.
  */
 static int
-vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
+vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq;
struct vhost_vring_addr *addr = &msg->payload.addr;
@@ -648,7 +648,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, 
VhostUserMsg *msg)
  */
 static int
 vhost_user_set_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
dev->virtqueue[msg->payload.state.index]->last_used_idx  =
msg->payload.state.num;
@@ -780,10 +780,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -804,7 +804,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -845,7 +845,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -994,16 +994,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -1015,17 +1015,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
struct VhostUserMsg *pmsg)
 }
 
 static int
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1073,7 +1073,7 @@ free_zmbufs(struct vhost_virtqueue *vq)
  */
 static int
 vhost_user_get_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtque

[dpdk-dev] [PATCH v5 2/5] vhost: make message handling functions prepare the reply

2018-09-24 Thread Nikolay Nikolaev
As VhostUserMsg structure is reused to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   24 
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 505db3bfc..4ae7b9346 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1117,6 +1125,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1244,6 +1254,8 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1658,8 +1670,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1690,7 +1701,6 @@ vhost_user_msg_handler(int vid, int fd)
if (ret)
goto skip_to_reply;
/* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1712,7 +1722,6 @@ vhost_user_msg_handler(int vid, int fd)
ret = vhost_user_get_vring_base(dev, &msg);
if (ret)
goto skip_to_reply;
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1730,8 +1739,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v5 3/5] vhost: handle unsupported message types in functions

2018-09-24 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 4ae7b9346..77905dda0 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1022,6 +1022,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
+   struct VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static int
 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
@@ -1259,6 +1267,13 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
return 0;
 }
 
+static void vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
+   struct VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1704,8 +1719,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(&dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1733,9 +1747,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(&dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM:



[dpdk-dev] [PATCH v5 0/5] vhost_user.c code cleanup

2018-09-24 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduce a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to introduce any functional changes.

v5 changes:
 - fixed the usage of struct VhostUserMsg in all patches (Anatoly Burakov)

v4 changes:
 - use struct VhostUserMsg as the coding style guide suggests (Anatoly Burakov)
 - VH_RESULT_FATAL is removed as not needed anymore (Maxime Coquelin)

v3 changes:
 - rebased on top of git://dpdk.org/next/dpdk-next-virtio dead0602
 - introduce VH_RESULT_FATAL (Maxime Coquelin)
 - vhost_user_set_features return VH_RESULT_FATAL on failure.
   This allows keeping the propagate error logic (Ilya Maximets)
 - fixed vhost_user_set_vring_kick and vhost_user_set_protocol_features
   return VH_RESULT_ERR upon failure
 - fixed missing break in case VH_RESULT_ERR (Ilya Maximets)
 - fixed a type on the description of 2/5 patch (Maxime Coquelin)

v2 changes:
 - Fix the comments by Tiwei Bie
 - Keep the old behavior
   - Fall through when the callback returns VH_RESULT_ERR
   - Fall through if the request is out of range

---

Nikolay Nikolaev (5):
  vhost: unify struct VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  393 ++---
 1 file changed, 208 insertions(+), 185 deletions(-)

--
Signature


[dpdk-dev] [PATCH v5 4/5] vhost: unify message handling function signature

2018-09-24 Thread Nikolay Nikolaev
Each vhost-user message handling function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  211 -
 1 file changed, 125 insertions(+), 86 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 77905dda0..ac89f413d 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handling failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handling successful */
+   VH_RESULT_OK=  0,
+   /* Message handling successful and reply prepared */
+   VH_RESULT_REPLY =  1,
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   VhostUserMsg * msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   VhostUserMsg * msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, struct VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, struct 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, struct VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, struct 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct virtio_net *dev,
+vhost_user_set_vring

[dpdk-dev] [PATCH v5 5/5] vhost: message handling implemented as a callback array

2018-09-24 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Failing vhost_user_set_features is fatal and all processing should
stop immediately and propagate the error to the upper layers. Change
the code accordingly to reflect that.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  149 +++--
 1 file changed, 56 insertions(+), 93 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index ac89f413d..faad3ba49 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1477,6 +1477,34 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, VhostUserMsg 
* msg);
+static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
@@ -1630,6 +1658,7 @@ vhost_user_msg_handler(int vid, int fd)
int ret;
int unlock_required = 0;
uint32_t skip_master = 0;
+   int request;
 
dev = get_device(vid);
if (dev == NULL)
@@ -1722,100 +1751,34 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_MEM_TABLE:
-   ret = vhost_user_set_mem_table(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_LOG_BASE:
-   ret = vhost_user_set_log_base(&dev, &msg);
-   if (ret)
-   goto skip_to_reply;
-   /* it needs a reply */
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_LOG_FD:
-   ret = vhost_user_set_log_fd(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_NUM:
-   ret = vhost_user_set_vring_num(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_ADDR:
-   ret = vhost_user_set_vring_addr(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_BASE:
-   ret = vhost_user_set_vring_base(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_VRING_BASE:
-   ret = vhost_user_get_vring_base(&dev, &msg);
-   if (ret)
-   goto skip_to_reply;
-   send_vhost_reply(fd, &msg);
-  

[dpdk-dev] [PATCH v6 0/5] vhost: vhost_user.c code cleanup

2018-09-24 Thread Nikolay Nikolaev
vhost: vhost_user.c code cleanup

This patchesries introduce a set of code redesigns in vhost_user.c.

The goal is to unify and simplify vhost-user message handling. The
patches do not intend to introduce any functional changes.

v6 changes:
 - Even more fixes to the usage of struct VhostUserMsg in
   the patches (Anatoly Burakov)

v5 changes:
 - fixed the usage of struct VhostUserMsg in all patches (Anatoly Burakov)

v4 changes:
 - use struct VhostUserMsg as the coding style guide suggests (Anatoly Burakov)
 - VH_RESULT_FATAL is removed as not needed anymore (Maxime Coquelin)

v3 changes:
 - rebased on top of git://dpdk.org/next/dpdk-next-virtio dead0602
 - introduce VH_RESULT_FATAL (Maxime Coquelin)
 - vhost_user_set_features return VH_RESULT_FATAL on failure.
   This allows keeping the propagate error logic (Ilya Maximets)
 - fixed vhost_user_set_vring_kick and vhost_user_set_protocol_features
   return VH_RESULT_ERR upon failure
 - fixed missing break in case VH_RESULT_ERR (Ilya Maximets)
 - fixed a type on the description of 2/5 patch (Maxime Coquelin)

v2 changes:
 - Fix the comments by Tiwei Bie
 - Keep the old behavior
   - Fall through when the callback returns VH_RESULT_ERR
   - Fall through if the request is out of range

---

Nikolay Nikolaev (5):
  vhost: unify struct VhostUserMsg usage
  vhost: make message handling functions prepare the reply
  vhost: handle unsupported message types in functions
  vhost: unify message handling function signature
  vhost: message handling implemented as a callback array


 lib/librte_vhost/vhost_user.c |  394 ++---
 1 file changed, 209 insertions(+), 185 deletions(-)

--
Signature


[dpdk-dev] [PATCH v6 4/5] vhost: unify message handling function signature

2018-09-24 Thread Nikolay Nikolaev
Each vhost-user message handling function will return an int result
which is described in the new enum vh_result: error, OK and reply.
All functions will now have two arguments, virtio_net double pointer
and VhostUserMsg pointer.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  211 -
 1 file changed, 125 insertions(+), 86 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 77905dda0..e1b705fa7 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -71,6 +71,16 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
+/* The possible results of a message handling function */
+enum vh_result {
+   /* Message handling failed */
+   VH_RESULT_ERR   = -1,
+   /* Message handling successful */
+   VH_RESULT_OK=  0,
+   /* Message handling successful and reply prepared */
+   VH_RESULT_REPLY =  1,
+};
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -127,27 +137,31 @@ vhost_backend_cleanup(struct virtio_net *dev)
  * the device hasn't been initialised.
  */
 static int
-vhost_user_set_owner(void)
+vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
+   struct VhostUserMsg *msg __rte_unused)
 {
-   return 0;
+   return VH_RESULT_OK;
 }
 
 static int
-vhost_user_reset_owner(struct virtio_net *dev)
+vhost_user_reset_owner(struct virtio_net **pdev,
+   struct VhostUserMsg *msg __rte_unused)
 {
+   struct virtio_net *dev = *pdev;
vhost_destroy_device_notify(dev);
 
cleanup_device(dev, 0);
reset_device(dev);
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The features that we support are requested.
  */
-static uint64_t
-vhost_user_get_features(struct virtio_net *dev, struct VhostUserMsg *msg)
+static int
+vhost_user_get_features(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
@@ -155,15 +169,16 @@ vhost_user_get_features(struct virtio_net *dev, struct 
VhostUserMsg *msg)
msg->payload.u64 = features;
msg->size = sizeof(msg->payload.u64);
 
-   return features;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * The queue number that we support are requested.
  */
-static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev, struct VhostUserMsg *msg)
+static int
+vhost_user_get_queue_num(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
@@ -171,15 +186,17 @@ vhost_user_get_queue_num(struct virtio_net *dev, struct 
VhostUserMsg *msg)
msg->payload.u64 = (uint64_t)queue_num;
msg->size = sizeof(msg->payload.u64);
 
-   return queue_num;
+   return VH_RESULT_REPLY;
 }
 
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
 static int
-vhost_user_set_features(struct virtio_net *dev, uint64_t features)
+vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+   struct virtio_net *dev = *pdev;
+   uint64_t features = msg->payload.u64;
uint64_t vhost_features = 0;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
@@ -189,12 +206,12 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) received invalid negotiated features.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->flags & VIRTIO_DEV_RUNNING) {
if (dev->features == features)
-   return 0;
+   return VH_RESULT_OK;
 
/*
 * Error out if master tries to change features while device is
@@ -205,7 +222,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
RTE_LOG(ERR, VHOST_CONFIG,
"(%d) features changed while device is 
running.\n",
dev->vid);
-   return -1;
+   return VH_RESULT_ERR;
}
 
if (dev->notify_ops->features_changed)
@@ -250,16 +267,17 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
if (vdpa_dev && vdpa_dev->ops->set_features)
vdpa_dev->ops->set_features(dev->vid);
 
-   return 0;
+   return VH_RESULT_OK;
 }
 
 /*
  * The virtio device sends us the size of the descriptor ring.
  */
 static int
-vhost_user_set_vring_num(struct vir

[dpdk-dev] [PATCH v6 5/5] vhost: message handling implemented as a callback array

2018-09-24 Thread Nikolay Nikolaev
Introduce vhost_message_handlers, which maps the message request
type to the message handler. Then replace the switch construct
with a map and call.

Failing vhost_user_set_features is fatal and all processing should
stop immediately and propagate the error to the upper layers. Change
the code accordingly to reflect that.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |  150 -
 1 file changed, 57 insertions(+), 93 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index e1b705fa7..f6ce8e092 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1477,6 +1477,35 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct 
VhostUserMsg *msg)
return VH_RESULT_OK;
 }
 
+typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
+   struct VhostUserMsg *msg);
+static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
+   [VHOST_USER_NONE] = NULL,
+   [VHOST_USER_GET_FEATURES] = vhost_user_get_features,
+   [VHOST_USER_SET_FEATURES] = vhost_user_set_features,
+   [VHOST_USER_SET_OWNER] = vhost_user_set_owner,
+   [VHOST_USER_RESET_OWNER] = vhost_user_reset_owner,
+   [VHOST_USER_SET_MEM_TABLE] = vhost_user_set_mem_table,
+   [VHOST_USER_SET_LOG_BASE] = vhost_user_set_log_base,
+   [VHOST_USER_SET_LOG_FD] = vhost_user_set_log_fd,
+   [VHOST_USER_SET_VRING_NUM] = vhost_user_set_vring_num,
+   [VHOST_USER_SET_VRING_ADDR] = vhost_user_set_vring_addr,
+   [VHOST_USER_SET_VRING_BASE] = vhost_user_set_vring_base,
+   [VHOST_USER_GET_VRING_BASE] = vhost_user_get_vring_base,
+   [VHOST_USER_SET_VRING_KICK] = vhost_user_set_vring_kick,
+   [VHOST_USER_SET_VRING_CALL] = vhost_user_set_vring_call,
+   [VHOST_USER_SET_VRING_ERR] = vhost_user_set_vring_err,
+   [VHOST_USER_GET_PROTOCOL_FEATURES] = vhost_user_get_protocol_features,
+   [VHOST_USER_SET_PROTOCOL_FEATURES] = vhost_user_set_protocol_features,
+   [VHOST_USER_GET_QUEUE_NUM] = vhost_user_get_queue_num,
+   [VHOST_USER_SET_VRING_ENABLE] = vhost_user_set_vring_enable,
+   [VHOST_USER_SEND_RARP] = vhost_user_send_rarp,
+   [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
+   [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
+   [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
+};
+
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
@@ -1630,6 +1659,7 @@ vhost_user_msg_handler(int vid, int fd)
int ret;
int unlock_required = 0;
uint32_t skip_master = 0;
+   int request;
 
dev = get_device(vid);
if (dev == NULL)
@@ -1722,100 +1752,34 @@ vhost_user_msg_handler(int vid, int fd)
goto skip_to_post_handle;
}
 
-   switch (msg.request.master) {
-   case VHOST_USER_GET_FEATURES:
-   ret = vhost_user_get_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_FEATURES:
-   ret = vhost_user_set_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_PROTOCOL_FEATURES:
-   ret = vhost_user_get_protocol_features(&dev, &msg);
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_PROTOCOL_FEATURES:
-   ret = vhost_user_set_protocol_features(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_OWNER:
-   ret = vhost_user_set_owner(&dev, &msg);
-   break;
-   case VHOST_USER_RESET_OWNER:
-   ret = vhost_user_reset_owner(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_MEM_TABLE:
-   ret = vhost_user_set_mem_table(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_LOG_BASE:
-   ret = vhost_user_set_log_base(&dev, &msg);
-   if (ret)
-   goto skip_to_reply;
-   /* it needs a reply */
-   send_vhost_reply(fd, &msg);
-   break;
-   case VHOST_USER_SET_LOG_FD:
-   ret = vhost_user_set_log_fd(&dev, &msg);
-   break;
-
-   case VHOST_USER_SET_VRING_NUM:
-   ret = vhost_user_set_vring_num(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_ADDR:
-   ret = vhost_user_set_vring_addr(&dev, &msg);
-   break;
-   case VHOST_USER_SET_VRING_BASE:
-   ret = vhost_user_set_vring_base(&dev, &msg);
-   break;
-
-   case VHOST_USER_GET_VRING_BASE:
-   ret = vhost_user_get_vring_base(&dev, &msg);
-   if (ret)
-   goto skip_t

[dpdk-dev] [PATCH v6 1/5] vhost: unify struct VhostUserMsg usage

2018-09-24 Thread Nikolay Nikolaev
Do not use the typedef version of struct VhostUserMsg. Also unify the
related parameter name.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   41 +
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 63d145b2d..505db3bfc 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -250,7 +250,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t 
features)
  */
 static int
 vhost_user_set_vring_num(struct virtio_net *dev,
-VhostUserMsg *msg)
+struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
 
@@ -611,7 +611,7 @@ translate_ring_addresses(struct virtio_net *dev, int 
vq_index)
  * This function then converts these to our address space.
  */
 static int
-vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
+vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq;
struct vhost_vring_addr *addr = &msg->payload.addr;
@@ -648,7 +648,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, 
VhostUserMsg *msg)
  */
 static int
 vhost_user_set_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
dev->virtqueue[msg->payload.state.index]->last_used_idx  =
msg->payload.state.num;
@@ -780,10 +780,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct virtio_net *dev = *pdev;
-   struct VhostUserMemory memory = pmsg->payload.memory;
+   struct VhostUserMemory memory = msg->payload.memory;
struct rte_vhost_mem_region *reg;
void *mmap_addr;
uint64_t mmap_size;
@@ -804,7 +804,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
"(%d) memory regions not changed\n", dev->vid);
 
for (i = 0; i < memory.nregions; i++)
-   close(pmsg->fds[i]);
+   close(msg->fds[i]);
 
return 0;
}
@@ -845,7 +845,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct 
VhostUserMsg *pmsg)
dev->mem->nregions = memory.nregions;
 
for (i = 0; i < memory.nregions; i++) {
-   fd  = pmsg->fds[i];
+   fd  = msg->fds[i];
reg = &dev->mem->regions[i];
 
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
@@ -994,16 +994,16 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 static void
-vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring call idx:%d file:%d\n", file.index, file.fd);
 
@@ -1015,17 +1015,17 @@ vhost_user_set_vring_call(struct virtio_net *dev, 
struct VhostUserMsg *pmsg)
 }
 
 static int
-vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
+vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
struct virtio_net *dev = *pdev;
 
-   file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
-   if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
+   file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+   if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
-   file.fd = pmsg->fds[0];
+   file.fd = msg->fds[0];
RTE_LOG(INFO, VHOST_CONFIG,
"vring kick idx:%d file:%d\n", file.index, file.fd);
 
@@ -1073,7 +1073,7 @@ free_zmbufs(struct vhost_virtqueue *vq)
  */
 static int
 vhost_user_get_vring_base(struct virtio_net *dev,
- VhostUserMsg *msg)
+ struct VhostUserMsg *msg)
 {
struct vhost_virtqueue *vq = dev->virtque

[dpdk-dev] [PATCH v6 2/5] vhost: make message handling functions prepare the reply

2018-09-24 Thread Nikolay Nikolaev
As VhostUserMsg structure is reused to generate the reply, move the
relevant fields update into the respective message handling functions.

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   24 
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 505db3bfc..4ae7b9346 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -146,11 +146,15 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(struct virtio_net *dev)
+vhost_user_get_features(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
uint64_t features = 0;
 
rte_vhost_driver_get_features(dev->ifname, &features);
+
+   msg->payload.u64 = features;
+   msg->size = sizeof(msg->payload.u64);
+
return features;
 }
 
@@ -158,11 +162,15 @@ vhost_user_get_features(struct virtio_net *dev)
  * The queue number that we support are requested.
  */
 static uint32_t
-vhost_user_get_queue_num(struct virtio_net *dev)
+vhost_user_get_queue_num(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
uint32_t queue_num = 0;
 
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+
+   msg->payload.u64 = (uint64_t)queue_num;
+   msg->size = sizeof(msg->payload.u64);
+
return queue_num;
 }
 
@@ -1117,6 +1125,8 @@ vhost_user_get_vring_base(struct virtio_net *dev,
rte_free(vq->batch_copy_elems);
vq->batch_copy_elems = NULL;
 
+   msg->size = sizeof(msg->payload.state);
+
return 0;
 }
 
@@ -1244,6 +1254,8 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
dev->log_base = dev->log_addr + off;
dev->log_size = size;
 
+   msg->size = sizeof(msg->payload.u64);
+
return 0;
 }
 
@@ -1658,8 +1670,7 @@ vhost_user_msg_handler(int vid, int fd)
 
switch (msg.request.master) {
case VHOST_USER_GET_FEATURES:
-   msg.payload.u64 = vhost_user_get_features(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_features(dev, &msg);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_FEATURES:
@@ -1690,7 +1701,6 @@ vhost_user_msg_handler(int vid, int fd)
if (ret)
goto skip_to_reply;
/* it needs a reply */
-   msg.size = sizeof(msg.payload.u64);
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
@@ -1712,7 +1722,6 @@ vhost_user_msg_handler(int vid, int fd)
ret = vhost_user_get_vring_base(dev, &msg);
if (ret)
goto skip_to_reply;
-   msg.size = sizeof(msg.payload.state);
send_vhost_reply(fd, &msg);
break;
 
@@ -1730,8 +1739,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_GET_QUEUE_NUM:
-   msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
-   msg.size = sizeof(msg.payload.u64);
+   vhost_user_get_queue_num(dev, &msg);
send_vhost_reply(fd, &msg);
break;
 



[dpdk-dev] [PATCH v6 3/5] vhost: handle unsupported message types in functions

2018-09-24 Thread Nikolay Nikolaev
Add new functions to handle the unsupported vhost message types:
 - vhost_user_set_vring_err
 - vhost_user_set_log_fd

Signed-off-by: Nikolay Nikolaev 
---
 lib/librte_vhost/vhost_user.c |   22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 4ae7b9346..77905dda0 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -1022,6 +1022,14 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct 
VhostUserMsg *msg)
vq->callfd = file.fd;
 }
 
+static void vhost_user_set_vring_err(struct virtio_net **pdev __rte_unused,
+   struct VhostUserMsg *msg)
+{
+   if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+}
+
 static int
 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
@@ -1259,6 +1267,13 @@ vhost_user_set_log_base(struct virtio_net *dev, struct 
VhostUserMsg *msg)
return 0;
 }
 
+static void vhost_user_set_log_fd(struct virtio_net **pdev __rte_unused,
+   struct VhostUserMsg *msg)
+{
+   close(msg->fds[0]);
+   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+}
+
 /*
  * An rarp packet is constructed and broadcasted to notify switches about
  * the new location of the migrated VM, so that packets from outside will
@@ -1704,8 +1719,7 @@ vhost_user_msg_handler(int vid, int fd)
send_vhost_reply(fd, &msg);
break;
case VHOST_USER_SET_LOG_FD:
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+   vhost_user_set_log_fd(&dev, &msg);
break;
 
case VHOST_USER_SET_VRING_NUM:
@@ -1733,9 +1747,7 @@ vhost_user_msg_handler(int vid, int fd)
break;
 
case VHOST_USER_SET_VRING_ERR:
-   if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
-   close(msg.fds[0]);
-   RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+   vhost_user_set_vring_err(&dev, &msg);
break;
 
case VHOST_USER_GET_QUEUE_NUM: