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 8B02741B9E; Wed, 1 Feb 2023 12:16:02 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 7D5C742D2F; Wed, 1 Feb 2023 12:15:59 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id 8A1754021F for ; Wed, 1 Feb 2023 12:15:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675250158; 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: in-reply-to:in-reply-to:references:references; bh=7rNO8ph1JTx0wd0ozpKa/1xAtMzUnjXUCwWYGR5eows=; b=VzEOBCCLe6v8iKSkToDOGjiSgQV6mm80tTqmfbkeq/xyoz9e+o1fCEnNoBE0VfngBLYPqt l3xfHeSwbbZ3gAkThT69c9R0FnwPdpVWaigIkFF2EAr7naIZQOqIe2zYQlTS5yrxTcLH/N jSd6oAaUuL+s0y2BMe07jKrqPOY8zos= 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-424-cHfkQShcMxel06olZaT3Uw-1; Wed, 01 Feb 2023 06:15:55 -0500 X-MC-Unique: cHfkQShcMxel06olZaT3Uw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7F6FE185A794; Wed, 1 Feb 2023 11:15:54 +0000 (UTC) Received: from dmarchan.redhat.com (ovpn-192-67.brq.redhat.com [10.40.192.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE79040C2064; Wed, 1 Feb 2023 11:15:52 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: maxime.coquelin@redhat.com, stephen@networkplumber.org, chenbo.xia@intel.com, jiayu.hu@intel.com, yuanx.wang@intel.com, xuan.ding@intel.com, mb@smartsharesystems.com Subject: [PATCH v5 2/9] vhost: simplify need reply handling Date: Wed, 1 Feb 2023 12:14:04 +0100 Message-Id: <20230201111411.1509520-3-david.marchand@redhat.com> In-Reply-To: <20230201111411.1509520-1-david.marchand@redhat.com> References: <20220328121758.26632-1-david.marchand@redhat.com> <20230201111411.1509520-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Dedicate send_vhost_slave_message() helper to the case when no reply is needed. Add a send_vhost_slave_message_process_reply() helper for the opposite. This new helper merges both send_vhost_slave_message() and the code previously in process_slave_message_reply(). The slave_req_lock lock is then only handled in this helper which will make lock checks trivial. Signed-off-by: David Marchand Acked-by: Morten Brørup Reviewed-by: Maxime Coquelin --- lib/vhost/vhost_user.c | 119 ++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 68 deletions(-) diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 9902ae9944..3f6c5df900 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -2868,18 +2868,46 @@ send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx } static int -send_vhost_slave_message(struct virtio_net *dev, - struct vhu_msg_context *ctx) +send_vhost_slave_message(struct virtio_net *dev, struct vhu_msg_context *ctx) +{ + return send_vhost_message(dev, dev->slave_req_fd, ctx); +} + +static int +send_vhost_slave_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx) { + struct vhu_msg_context msg_reply; int ret; - if (ctx->msg.flags & VHOST_USER_NEED_REPLY) - rte_spinlock_lock(&dev->slave_req_lock); + rte_spinlock_lock(&dev->slave_req_lock); + ret = send_vhost_slave_message(dev, ctx); + if (ret < 0) { + VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to send config change (%d)\n", ret); + goto out; + } - ret = send_vhost_message(dev, dev->slave_req_fd, ctx); - if (ret < 0 && (ctx->msg.flags & VHOST_USER_NEED_REPLY)) - rte_spinlock_unlock(&dev->slave_req_lock); + ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply); + if (ret <= 0) { + if (ret < 0) + VHOST_LOG_CONFIG(dev->ifname, ERR, + "vhost read slave message reply failed\n"); + else + VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n"); + ret = -1; + goto out; + } + + if (msg_reply.msg.request.slave != ctx->msg.request.slave) { + VHOST_LOG_CONFIG(dev->ifname, ERR, + "received unexpected msg type (%u), expected %u\n", + msg_reply.msg.request.slave, ctx->msg.request.slave); + ret = -1; + goto out; + } + ret = msg_reply.msg.payload.u64 ? -1 : 0; +out: + rte_spinlock_unlock(&dev->slave_req_lock); return ret; } @@ -3203,42 +3231,6 @@ vhost_user_msg_handler(int vid, int fd) return ret; } -static int process_slave_message_reply(struct virtio_net *dev, - const struct vhu_msg_context *ctx) -{ - struct vhu_msg_context msg_reply; - int ret; - - if ((ctx->msg.flags & VHOST_USER_NEED_REPLY) == 0) - return 0; - - ret = read_vhost_message(dev, dev->slave_req_fd, &msg_reply); - if (ret <= 0) { - if (ret < 0) - VHOST_LOG_CONFIG(dev->ifname, ERR, - "vhost read slave message reply failed\n"); - else - VHOST_LOG_CONFIG(dev->ifname, INFO, "vhost peer closed\n"); - ret = -1; - goto out; - } - - ret = 0; - if (msg_reply.msg.request.slave != ctx->msg.request.slave) { - VHOST_LOG_CONFIG(dev->ifname, ERR, - "received unexpected msg type (%u), expected %u\n", - msg_reply.msg.request.slave, ctx->msg.request.slave); - ret = -1; - goto out; - } - - ret = msg_reply.msg.payload.u64 ? -1 : 0; - -out: - rte_spinlock_unlock(&dev->slave_req_lock); - return ret; -} - int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm) { @@ -3267,10 +3259,9 @@ vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm) return 0; } -static int -vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply) +int +rte_vhost_slave_config_change(int vid, bool need_reply) { - int ret; struct vhu_msg_context ctx = { .msg = { .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG, @@ -3278,29 +3269,23 @@ vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply) .size = 0, } }; - - if (need_reply) - ctx.msg.flags |= VHOST_USER_NEED_REPLY; - - ret = send_vhost_slave_message(dev, &ctx); - if (ret < 0) { - VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to send config change (%d)\n", ret); - return ret; - } - - return process_slave_message_reply(dev, &ctx); -} - -int -rte_vhost_slave_config_change(int vid, bool need_reply) -{ struct virtio_net *dev; + int ret; dev = get_device(vid); if (!dev) return -ENODEV; - return vhost_user_slave_config_change(dev, need_reply); + if (!need_reply) { + ret = send_vhost_slave_message(dev, &ctx); + } else { + ctx.msg.flags |= VHOST_USER_NEED_REPLY; + ret = send_vhost_slave_message_process_reply(dev, &ctx); + } + + if (ret < 0) + VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to send config change (%d)\n", ret); + return ret; } static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev, @@ -3329,13 +3314,11 @@ static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev, ctx.fd_num = 1; } - ret = send_vhost_slave_message(dev, &ctx); - if (ret < 0) { + ret = send_vhost_slave_message_process_reply(dev, &ctx); + if (ret < 0) VHOST_LOG_CONFIG(dev->ifname, ERR, "failed to set host notifier (%d)\n", ret); - return ret; - } - return process_slave_message_reply(dev, &ctx); + return ret; } int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable) -- 2.39.1