From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BFB17A00BE; Mon, 25 Apr 2022 14:54:42 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 992D241151; Mon, 25 Apr 2022 14:54:42 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id DB40C41109 for ; Mon, 25 Apr 2022 14:54:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650891280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zXgXdPAVWNFrupoYStjQfBWcaFMaoqZO5u4qdcFXdcY=; b=IHUcJ3LgnFD64h5GlTFyCumJSdb4nGjibmFIwMaVWs3E8VxHm+v2t/nn7eDGYFq6c1SPRR UmyTFh7svCyQJQJhCZG81hvLcQzumyGTcxabECy7GUMBC5PDVeD0WbMRA78FKGJf4aGkfc e9byQ2kYxDsmtqMm2Z3dIcGm4hvyVC8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-83-7k16LcEvPlaBaSF-BLU4Tg-1; Mon, 25 Apr 2022 08:54:39 -0400 X-MC-Unique: 7k16LcEvPlaBaSF-BLU4Tg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED50E80005D; Mon, 25 Apr 2022 12:54:38 +0000 (UTC) Received: from dmarchan.remote.csb (unknown [10.40.193.200]) by smtp.corp.redhat.com (Postfix) with ESMTP id F34FBC28107; Mon, 25 Apr 2022 12:54:37 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: maxime.coquelin@redhat.com, chenbo.xia@intel.com Subject: [PATCH 1/2] vhost: remove unneeded max enums Date: Mon, 25 Apr 2022 14:54:29 +0200 Message-Id: <20220425125431.26464-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david.marchand@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Move message handler description and callbacks into a single array and remove unneeded VHOST_USER_MAX and VHOST_SLAVE_MAX enums. Signed-off-by: David Marchand --- drivers/net/virtio/virtio_user/vhost_user.c | 1 - examples/vhost_blk/blk_spec.h | 1 - lib/vhost/vhost_user.c | 175 +++++++++----------- lib/vhost/vhost_user.h | 2 - 4 files changed, 76 insertions(+), 103 deletions(-) diff --git a/drivers/net/virtio/virtio_user/vhost_user.c b/drivers/net/virtio/virtio_user/vhost_user.c index 00d0dcaa74..7d1749114d 100644 --- a/drivers/net/virtio/virtio_user/vhost_user.c +++ b/drivers/net/virtio/virtio_user/vhost_user.c @@ -78,7 +78,6 @@ enum vhost_user_request { VHOST_USER_SET_VRING_ENABLE = 18, VHOST_USER_SET_STATUS = 39, VHOST_USER_GET_STATUS = 40, - VHOST_USER_MAX }; struct vhost_user_msg { diff --git a/examples/vhost_blk/blk_spec.h b/examples/vhost_blk/blk_spec.h index 594bd6a29b..3c54f70eaf 100644 --- a/examples/vhost_blk/blk_spec.h +++ b/examples/vhost_blk/blk_spec.h @@ -56,7 +56,6 @@ enum vhost_user_request { VHOST_USER_SET_PROTOCOL_FEATURES = 16, VHOST_USER_GET_QUEUE_NUM = 17, VHOST_USER_SET_VRING_ENABLE = 18, - VHOST_USER_MAX }; /** Get/set config msg payload */ diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 1d390677fa..17cfeafa16 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -56,40 +56,12 @@ #define INFLIGHT_ALIGNMENT 64 #define INFLIGHT_VERSION 0x1 -static const char *vhost_message_str[VHOST_USER_MAX] = { - [VHOST_USER_NONE] = "VHOST_USER_NONE", - [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_SLAVE_REQ_FD", - [VHOST_USER_IOTLB_MSG] = "VHOST_USER_IOTLB_MSG", - [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS", - [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS", - [VHOST_USER_POSTCOPY_ADVISE] = "VHOST_USER_POSTCOPY_ADVISE", - [VHOST_USER_POSTCOPY_LISTEN] = "VHOST_USER_POSTCOPY_LISTEN", - [VHOST_USER_POSTCOPY_END] = "VHOST_USER_POSTCOPY_END", - [VHOST_USER_GET_INFLIGHT_FD] = "VHOST_USER_GET_INFLIGHT_FD", - [VHOST_USER_SET_INFLIGHT_FD] = "VHOST_USER_SET_INFLIGHT_FD", - [VHOST_USER_SET_STATUS] = "VHOST_USER_SET_STATUS", - [VHOST_USER_GET_STATUS] = "VHOST_USER_GET_STATUS", -}; +typedef struct vhost_message_handler { + const char *description; + int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx, + int main_fd); +} vhost_message_handler_t; +static vhost_message_handler_t vhost_message_handlers[]; static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx); @@ -122,7 +94,7 @@ validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expect VHOST_LOG_CONFIG(ERR, "(%s) expect %d FDs for request %s, received %d\n", dev->ifname, expected_fds, - vhost_message_str[ctx->msg.request.master], + vhost_message_handlers[ctx->msg.request.master].description, ctx->fd_num); close_msg_fds(ctx); @@ -2754,42 +2726,44 @@ vhost_user_set_status(struct virtio_net **pdev, return RTE_VHOST_MSG_RESULT_OK; } -typedef int (*vhost_message_handler_t)(struct virtio_net **pdev, - struct vhu_msg_context *ctx, - int main_fd); - -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, - [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise, - [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen, - [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end, - [VHOST_USER_GET_INFLIGHT_FD] = vhost_user_get_inflight_fd, - [VHOST_USER_SET_INFLIGHT_FD] = vhost_user_set_inflight_fd, - [VHOST_USER_SET_STATUS] = vhost_user_set_status, - [VHOST_USER_GET_STATUS] = vhost_user_get_status, +#define VHOST_MESSAGE_HANDLERS \ +VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_SLAVE_REQ_FD, vhost_user_set_req_fd) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status) + +#define VHOST_MESSAGE_HANDLER(id, handler) \ + [id] = { #id, handler }, +static vhost_message_handler_t vhost_message_handlers[] = { + VHOST_MESSAGE_HANDLERS }; +#undef VHOST_MESSAGE_HANDLER /* return bytes# of read on success or negative val on failure. */ static int @@ -2946,11 +2920,12 @@ vhost_user_msg_handler(int vid, int fd) { struct virtio_net *dev; struct vhu_msg_context ctx; + vhost_message_handler_t *msg_handler; struct rte_vdpa_device *vdpa_dev; int ret; int unlock_required = 0; bool handled; - int request; + uint32_t request; uint32_t i; dev = get_device(vid); @@ -2978,14 +2953,18 @@ vhost_user_msg_handler(int vid, int fd) ret = 0; request = ctx.msg.request.master; - if (request > VHOST_USER_NONE && request < VHOST_USER_MAX && - vhost_message_str[request]) { + if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers)) + msg_handler = &vhost_message_handlers[request]; + else + msg_handler = NULL; + + if (msg_handler != NULL && msg_handler->description != NULL) { if (request != VHOST_USER_IOTLB_MSG) VHOST_LOG_CONFIG(INFO, "(%s) read message %s\n", - dev->ifname, vhost_message_str[request]); + dev->ifname, msg_handler->description); else VHOST_LOG_CONFIG(DEBUG, "(%s) read message %s\n", - dev->ifname, vhost_message_str[request]); + dev->ifname, msg_handler->description); } else { VHOST_LOG_CONFIG(DEBUG, "(%s) external request %d\n", dev->ifname, request); } @@ -3048,31 +3027,29 @@ vhost_user_msg_handler(int vid, int fd) } } - if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) { - if (!vhost_message_handlers[request]) - goto skip_to_post_handle; - ret = vhost_message_handlers[request](&dev, &ctx, fd); + if (msg_handler == NULL || msg_handler->callback == NULL) + goto skip_to_post_handle; - switch (ret) { - case RTE_VHOST_MSG_RESULT_ERR: - VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n", - dev->ifname, vhost_message_str[request]); - handled = true; - break; - case RTE_VHOST_MSG_RESULT_OK: - VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n", - dev->ifname, vhost_message_str[request]); - handled = true; - break; - case RTE_VHOST_MSG_RESULT_REPLY: - VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n", - dev->ifname, vhost_message_str[request]); - send_vhost_reply(dev, fd, &ctx); - handled = true; - break; - default: - break; - } + ret = msg_handler->callback(&dev, &ctx, fd); + switch (ret) { + case RTE_VHOST_MSG_RESULT_ERR: + VHOST_LOG_CONFIG(ERR, "(%s) processing %s failed.\n", + dev->ifname, msg_handler->description); + handled = true; + break; + case RTE_VHOST_MSG_RESULT_OK: + VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded.\n", + dev->ifname, msg_handler->description); + handled = true; + break; + case RTE_VHOST_MSG_RESULT_REPLY: + VHOST_LOG_CONFIG(DEBUG, "(%s) processing %s succeeded and needs reply.\n", + dev->ifname, msg_handler->description); + send_vhost_reply(dev, fd, &ctx); + handled = true; + break; + default: + break; } skip_to_post_handle: diff --git a/lib/vhost/vhost_user.h b/lib/vhost/vhost_user.h index c946cc2ef4..ba1c5c7969 100644 --- a/lib/vhost/vhost_user.h +++ b/lib/vhost/vhost_user.h @@ -59,7 +59,6 @@ typedef enum VhostUserRequest { VHOST_USER_SET_INFLIGHT_FD = 32, VHOST_USER_SET_STATUS = 39, VHOST_USER_GET_STATUS = 40, - VHOST_USER_MAX = 41 } VhostUserRequest; typedef enum VhostUserSlaveRequest { @@ -67,7 +66,6 @@ typedef enum VhostUserSlaveRequest { VHOST_USER_SLAVE_IOTLB_MSG = 1, VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2, VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3, - VHOST_USER_SLAVE_MAX } VhostUserSlaveRequest; typedef struct VhostUserMemoryRegion { -- 2.23.0