From patchwork Fri Apr 16 10:25:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Balazs Nemeth X-Patchwork-Id: 91634 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 376EFA0C40; Fri, 16 Apr 2021 12:26:17 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CFD85141CD3; Fri, 16 Apr 2021 12:26:16 +0200 (CEST) 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 B2CC2141CD0 for ; Fri, 16 Apr 2021 12:26:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618568774; 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=LoZ9kBC5CSrddPIyCKR78/Su9+kvQEDYp/Hct1qi0Yo=; b=e4FF33HqxlF8QCnsDZr3PP0QdS+THOUCbfauDRceXHXmKllGP1UCTvh0DbMln9Gj9pfTHQ ZrNzFMf+3JzubELwg8tWOFqTJJMYFl38/kSQN7A/ioWeJIOXZ8Nd8ffpjGNDiFGIS1a4UG M5R6o72JyFba0JlGUubvTV5ZtbyTRAY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-480-Avn6y_OiMXSsm-qmrDlU0w-1; Fri, 16 Apr 2021 06:26:12 -0400 X-MC-Unique: Avn6y_OiMXSsm-qmrDlU0w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D8FD119251A1 for ; Fri, 16 Apr 2021 10:26:11 +0000 (UTC) Received: from bnemeth.users.ipa.redhat.com (ovpn-114-172.ams2.redhat.com [10.36.114.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2B1CB6E0A3; Fri, 16 Apr 2021 10:25:54 +0000 (UTC) From: Balazs Nemeth To: bnemeth@redhat.com, dev@dpdk.org Cc: maxime.coquelin@redhat.com, david.marchand@redhat.com Date: Fri, 16 Apr 2021 12:25:19 +0200 Message-Id: <09c440a88d9c546ebbe7d5d4cd2e0d2e53e4e870.1618568597.git.bnemeth@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=bnemeth@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v5] vhost: allocate and free packets in bulk in Tx packed 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 Sender: "dev" Move allocation out further and perform all allocation in bulk. The same goes for freeing packets. In the process, also introduce virtio_dev_pktmbuf_prep and make virtio_dev_pktmbuf_alloc use that. Signed-off-by: Balazs Nemeth Reviewed-by: David Marchand Reviewed-by: Maxime Coquelin --- lib/librte_vhost/virtio_net.c | 80 +++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c index ff39878609..77a00c9499 100644 --- a/lib/librte_vhost/virtio_net.c +++ b/lib/librte_vhost/virtio_net.c @@ -2134,6 +2134,24 @@ virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size) return 0; } +static __rte_always_inline int +virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt, + uint32_t data_len) +{ + if (rte_pktmbuf_tailroom(pkt) >= data_len) + return 0; + + /* attach an external buffer if supported */ + if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len)) + return 0; + + /* check if chained buffers are allowed */ + if (!dev->linearbuf) + return 0; + + return -1; +} + /* * Allocate a host supported pktmbuf. */ @@ -2149,23 +2167,15 @@ virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp, return NULL; } - if (rte_pktmbuf_tailroom(pkt) >= data_len) - return pkt; + if (virtio_dev_pktmbuf_prep(dev, pkt, data_len)) { + /* Data doesn't fit into the buffer and the host supports + * only linear buffers + */ + rte_pktmbuf_free(pkt); + return NULL; + } - /* attach an external buffer if supported */ - if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len)) - return pkt; - - /* check if chained buffers are allowed */ - if (!dev->linearbuf) - return pkt; - - /* Data doesn't fit into the buffer and the host supports - * only linear buffers - */ - rte_pktmbuf_free(pkt); - - return NULL; + return pkt; } static __rte_noinline uint16_t @@ -2261,7 +2271,6 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, static __rte_always_inline int vhost_reserve_avail_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, - struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t avail_idx, uintptr_t *desc_addrs, @@ -2306,9 +2315,8 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev, } vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) { - pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]); - if (!pkts[i]) - goto free_buf; + if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i])) + goto err; } vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) @@ -2316,7 +2324,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev, vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) { if (unlikely(buf_lens[i] < (lens[i] - buf_offset))) - goto free_buf; + goto err; } vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) { @@ -2327,17 +2335,13 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev, return 0; -free_buf: - for (i = 0; i < PACKED_BATCH_SIZE; i++) - rte_pktmbuf_free(pkts[i]); - +err: return -1; } static __rte_always_inline int virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, - struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts) { uint16_t avail_idx = vq->last_avail_idx; @@ -2347,8 +2351,8 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, uint16_t ids[PACKED_BATCH_SIZE]; uint16_t i; - if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts, - avail_idx, desc_addrs, ids)) + if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx, + desc_addrs, ids)) return -1; vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) @@ -2381,7 +2385,7 @@ static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool, - struct rte_mbuf **pkts, + struct rte_mbuf *pkts, uint16_t *buf_id, uint16_t *desc_count) { @@ -2398,8 +2402,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev, VHOST_ACCESS_RO) < 0)) return -1; - *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len); - if (unlikely(*pkts == NULL)) { + if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) { if (!allocerr_warned) { VHOST_LOG_DATA(ERR, "Failed mbuf alloc of size %d from %s on %s.\n", @@ -2409,7 +2412,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev, return -1; } - err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts, + err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts, mbuf_pool); if (unlikely(err)) { if (!allocerr_warned) { @@ -2418,7 +2421,6 @@ vhost_dequeue_single_packed(struct virtio_net *dev, dev->ifname); allocerr_warned = true; } - rte_pktmbuf_free(*pkts); return -1; } @@ -2429,7 +2431,7 @@ static __rte_always_inline int virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool, - struct rte_mbuf **pkts) + struct rte_mbuf *pkts) { uint16_t buf_id, desc_count = 0; @@ -2462,11 +2464,14 @@ virtio_dev_tx_packed(struct virtio_net *dev, uint32_t pkt_idx = 0; uint32_t remained = count; + if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) + return 0; + do { rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); if (remained >= PACKED_BATCH_SIZE) { - if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool, + if (!virtio_dev_tx_batch_packed(dev, vq, &pkts[pkt_idx])) { pkt_idx += PACKED_BATCH_SIZE; remained -= PACKED_BATCH_SIZE; @@ -2475,13 +2480,16 @@ virtio_dev_tx_packed(struct virtio_net *dev, } if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool, - &pkts[pkt_idx])) + pkts[pkt_idx])) break; pkt_idx++; remained--; } while (remained); + if (pkt_idx != count) + rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx); + if (vq->shadow_used_idx) { do_data_copy_dequeue(vq);