[v1,5/5] vhost: message handling implemented as a callback array
Checks
Commit Message
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 <nicknickolaev@gmail.com>
---
lib/librte_vhost/vhost_user.c | 144 ++++++++++++++++-------------------------
1 file changed, 55 insertions(+), 89 deletions(-)
Comments
On Tue, Jun 26, 2018 at 07:19:38PM +0300, 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.
>
> Signed-off-by: Nikolay Nikolaev <nicknickolaev@gmail.com>
> ---
> 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);
s/message_handler/vhost_message_handler_t/
> +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,
Please keep using one space between "]" and "=".
> + [VHOST_USER_CRYPTO_CREATE_SESS] = NULL,
> + [VHOST_USER_CRYPTO_CLOSE_SESS] = NULL,
There is no need to zero above two explicitly.
> +};
> +
> +
> /* 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;
>
Please also remove above blank line.
> - 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;
Please define variable at the beginning of the function.
> + if (vhost_message_handlers[request]) {
The `request` needs to be checked with VHOST_USER_MAX first.
> + ret = vhost_message_handlers[request](&dev, &msg);
>
[...]
> -
> - default:
> - ret = -1;
> - break;
> + switch (ret) {
> + case VH_RESULT_ERR:
> + RTE_LOG(ERR, VHOST_CONFIG,
> + "Processing %s failed.\n",
> + vhost_message_str[request]);
> + return -1;
Queue pairs need to be unlocked when necessary.
> + case VH_RESULT_OK:
> + RTE_LOG(DEBUG, VHOST_CONFIG,
> + "Processing %s succeeded.\n",
> + vhost_message_str[request]);
> + break;
> + case VH_RESULT_REPLY:
> + RTE_LOG(INFO, VHOST_CONFIG,
> + "Processing %s succeeded and needs reply.\n",
> + vhost_message_str[request]);
> + send_vhost_reply(fd, &msg);
> + break;
> + }
> + } else {
> + RTE_LOG(ERR, VHOST_CONFIG,
> + "Requested invalid message type %d.\n", request);
> + return -1;
The old behaviour is changed, VHOST_USER_CRYPTO_CREATE_SESS
and VHOST_USER_CRYPTO_CLOSE_SESS will be broken.
> }
>
> skip_to_post_handle:
>
@@ -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);
- break;
-
- case VHOST_USER_SET_VRING_ERR:
- ret = vhost_user_set_vring_err(&dev, &msg);
- break;
-
- case VHOST_USER_GET_QUEUE_NUM:
- ret = vhost_user_get_queue_num(&dev, &msg);
- send_vhost_reply(fd, &msg);
- break;
-
- case VHOST_USER_SET_VRING_ENABLE:
- ret = vhost_user_set_vring_enable(&dev, &msg);
- break;
- case VHOST_USER_SEND_RARP:
- ret = vhost_user_send_rarp(&dev, &msg);
- break;
-
- case VHOST_USER_NET_SET_MTU:
- ret = vhost_user_net_set_mtu(&dev, &msg);
- break;
-
- case VHOST_USER_SET_SLAVE_REQ_FD:
- ret = vhost_user_set_req_fd(&dev, &msg);
- break;
-
- case VHOST_USER_IOTLB_MSG:
- ret = vhost_user_iotlb_msg(&dev, &msg);
- break;
-
- default:
- ret = -1;
- break;
+ switch (ret) {
+ case VH_RESULT_ERR:
+ RTE_LOG(ERR, VHOST_CONFIG,
+ "Processing %s failed.\n",
+ vhost_message_str[request]);
+ return -1;
+ case VH_RESULT_OK:
+ RTE_LOG(DEBUG, VHOST_CONFIG,
+ "Processing %s succeeded.\n",
+ vhost_message_str[request]);
+ break;
+ case VH_RESULT_REPLY:
+ RTE_LOG(INFO, VHOST_CONFIG,
+ "Processing %s succeeded and needs reply.\n",
+ vhost_message_str[request]);
+ send_vhost_reply(fd, &msg);
+ break;
+ }
+ } else {
+ RTE_LOG(ERR, VHOST_CONFIG,
+ "Requested invalid message type %d.\n", request);
+ return -1;
}
skip_to_post_handle: