From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 28858A0613 for ; Sat, 28 Sep 2019 02:38:38 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 2F3201BE96; Sat, 28 Sep 2019 02:38:15 +0200 (CEST) Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by dpdk.org (Postfix) with ESMTP id 04A071B948 for ; Sat, 28 Sep 2019 02:38:07 +0200 (CEST) Received: by mail-pg1-f195.google.com with SMTP id i30so4326683pgl.0 for ; Fri, 27 Sep 2019 17:38:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YGTy358oZ3HU/DV9pSNDqY3tMnkYEw+Q0RgMPLx2tFk=; b=zU15/1U9+oJVsGskyI7DMTPU0xR0wE+cA+O1+0XgoKRXiPDhrmg2P1vG1mxpVG1RqC 8u72bIgHERu7xdL3cUuBRrAz+E8964wzIE0gqB1bnZdUmy2TVRLUBajLgJfSBljee8fj XVrjFFQl4KtQ7zT55uA3cBnKs4DpXemn8l5RWXG0N6idedlhL+Py6UPaiLpv0fOF75wO QxgS9aoyi6zIom6Swu8xeh34YoLZL6UqajU+JpFgXjJ2GV59ox7haCCWf3TatFt6CoAc podvEvtbgZqAQ7/Qtd9mDl196uBbZa9DmABxbmnPLYktDe3SXSbxphv1HYkPb2ZlxwDy JCiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YGTy358oZ3HU/DV9pSNDqY3tMnkYEw+Q0RgMPLx2tFk=; b=QPLACT1g4+xx+0FQL2TgmSkkhT0Hok8ChsKd6V6/c1HnagyIORKflvYFUoOtG+ViGO fRoXk9Tdo4UR59TMzcT6TBso+drckcyuftccbQUcDwakitdaQjwzVvUoD8rR6Dz23w/M juTj1UlnfKdLGVFlG8b+2c3U/1famVlDekwTvSRx7MpTj5w+DF8VtBx8sJTiComjMcVt zFQ2X5PcKQDfFY6Zm5TazghF+BU8pWUiVKZLZKn3eLonEcWDo1CGriczzMHGaOqTZv6Z 2vl9Pteom+85kY460tPfapgzcCCGqL8Rs4tlItWQKo2488jHyp29KseVrPrvxY9IQ6hy P5Cw== X-Gm-Message-State: APjAAAUJ5KfPQTecqcIgXLvnR5qwd/PZLeoBh8jwWtUkJiR1YyasNoyE EWLi3v+/bxttjfLJoi1FUBZvzoHjqwI= X-Google-Smtp-Source: APXvYqxika6pmJghabtkb23aJLMINerAOmrhHCqKXgrSDuzuYDlzcD3X1N6AlbstU1vzhJN7R1S3ZQ== X-Received: by 2002:a62:fb06:: with SMTP id x6mr7842606pfm.186.1569631085922; Fri, 27 Sep 2019 17:38:05 -0700 (PDT) Received: from hermes.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id t13sm3494942pfh.12.2019.09.27.17.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2019 17:38:05 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Fri, 27 Sep 2019 17:37:57 -0700 Message-Id: <20190928003758.18489-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190928003758.18489-1-stephen@networkplumber.org> References: <20190928003758.18489-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH 4/5] mbuf: add a pktmbuf copy routine X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 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" This is a commonly used operation that surprisingly the DPDK has not supported. The new rte_pktmbuf_copy does a deep copy of packet. This is a complete copy including meta-data. It handles the case where the source mbuf comes from a pool with larger data area than the destination pool. The routine also has options for skipping data, or truncating at a fixed length. Signed-off-by: Stephen Hemminger --- lib/librte_mbuf/rte_mbuf.c | 70 ++++++++++++++++++++++++++++ lib/librte_mbuf/rte_mbuf.h | 26 +++++++++++ lib/librte_mbuf/rte_mbuf_version.map | 1 + 3 files changed, 97 insertions(+) diff --git a/lib/librte_mbuf/rte_mbuf.c b/lib/librte_mbuf/rte_mbuf.c index 12d0258a120d..6888d6bd5dfc 100644 --- a/lib/librte_mbuf/rte_mbuf.c +++ b/lib/librte_mbuf/rte_mbuf.c @@ -324,6 +324,76 @@ rte_pktmbuf_linearize(struct rte_mbuf *mbuf) return 0; } +/* Create a deep copy of mbuf */ +struct rte_mbuf * +rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, + uint32_t off, uint32_t len) +{ + const struct rte_mbuf *seg = m; + struct rte_mbuf *mc, *m_last, **prev; + + if (unlikely(off >= m->pkt_len)) + return NULL; + + mc = rte_pktmbuf_alloc(mp); + if (unlikely(mc == NULL)) + return NULL; + + if (len > m->pkt_len - off) + len = m->pkt_len - off; + + /* clone meta data from original */ + mc->port = m->port; + mc->vlan_tci = m->vlan_tci; + mc->vlan_tci_outer = m->vlan_tci_outer; + mc->tx_offload = m->tx_offload; + mc->hash = m->hash; + mc->packet_type = m->packet_type; + mc->timestamp = m->timestamp; + + prev = &mc->next; + m_last = mc; + while (len > 0) { + uint32_t copy_len; + + while (off >= seg->data_len) { + off -= seg->data_len; + seg = seg->next; + } + + /* current buffer is full, chain a new one */ + if (rte_pktmbuf_tailroom(m_last) == 0) { + m_last = rte_pktmbuf_alloc(mp); + if (unlikely(m_last == NULL)) { + rte_pktmbuf_free(mc); + return NULL; + } + ++mc->nb_segs; + *prev = m_last; + prev = &m_last->next; + } + + copy_len = RTE_MIN(seg->data_len - off, len); + if (copy_len > rte_pktmbuf_tailroom(m_last)) + copy_len = rte_pktmbuf_tailroom(m_last); + + /* append from seg to m_last */ + rte_memcpy(rte_pktmbuf_mtod_offset(m_last, char *, + m_last->data_len), + rte_pktmbuf_mtod_offset(seg, char *, + off), + copy_len); + + m_last->data_len += copy_len; + mc->pkt_len += copy_len; + off += copy_len; + len -= copy_len; + } + + __rte_mbuf_sanity_check(mc, 1); + return mc; +} + /* dump a mbuf on console */ void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len) diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h index a6e78e4ea7a6..77266a07c75b 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -1927,6 +1927,32 @@ static inline void rte_pktmbuf_free(struct rte_mbuf *m) struct rte_mbuf * rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp); +/** + * Creates a full copy of a given packet mbuf. + * + * Copies all the data from a given packet mbuf to a newly allocated + * set of mbufs. + * + * @param m + * The packet mbuf to be cloned. + * @param mp + * The mempool from which the "clone" mbufs are allocated. + * @param offset + * The number of bytes to skip before copying. + * If the mbuf does not have that many bytes, it is an error + * and NULL is returned. + * @param length + * The upper limit on bytes to copy. Passing UINT32_MAX + * means all data (after offset). + * @return + * - The pointer to the new "clone" mbuf on success. + * - NULL if allocation fails. + */ +__rte_experimental +struct rte_mbuf * +rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, + uint32_t offset, uint32_t length); + /** * Adds given value to the refcnt of all packet mbuf segments. * diff --git a/lib/librte_mbuf/rte_mbuf_version.map b/lib/librte_mbuf/rte_mbuf_version.map index 9cc43e24bfa4..3742ed114e7c 100644 --- a/lib/librte_mbuf/rte_mbuf_version.map +++ b/lib/librte_mbuf/rte_mbuf_version.map @@ -57,4 +57,5 @@ EXPERIMENTAL { global: rte_mbuf_check; + rte_pktmbuf_copy; } DPDK_18.08; -- 2.20.1