From patchwork Wed May 23 16:47:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lam, Tiago" X-Patchwork-Id: 919198 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40rdnF4bsQz9s15 for ; Thu, 24 May 2018 02:50:57 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 738E8DA8; Wed, 23 May 2018 16:48:00 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 71E6EDB1 for ; Wed, 23 May 2018 16:47:58 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id AED046C6 for ; Wed, 23 May 2018 16:47:57 +0000 (UTC) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 May 2018 09:47:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,433,1520924400"; d="scan'208";a="201846018" Received: from silpixa00399125.ir.intel.com ([10.237.223.34]) by orsmga004.jf.intel.com with ESMTP; 23 May 2018 09:47:56 -0700 From: Tiago Lam To: ovs-dev@openvswitch.org Date: Wed, 23 May 2018 17:47:21 +0100 Message-Id: <1527094048-105084-7-git-send-email-tiago.lam@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1527094048-105084-1-git-send-email-tiago.lam@intel.com> References: <1527094048-105084-1-git-send-email-tiago.lam@intel.com> X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [RFC v7 06/13] dp-packet: Handle multi-seg mbufs in put*() funcs. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org The dp_packet_put*() function - dp_packet_put_uninit(), dp_packet_put() and dp_packet_put_zeros() - are, in their current implementation, operating on the data buffer of a dp_packet as if it were contiguous, which in the case of multi-segment mbufs means they operate on the first mbuf in the chain. However, in the case of dp_packet_put_uninit(), for example, it is the data length of the last mbuf in the mbuf chain that should be adjusted. These functions have thus been modified to support multi-segment mbufs. Additionally, most of the core logic in dp_pcket_put_uninit() was moved to a new helper function, dp_packet_put_uninit()_, to abstract the implementation details from the API, since in the case of multi-seg mbufs a new struct is returned that holds the mbuf and offset that constitute the tail. For the single mbuf case a pointer to the byte that constitute the tail still returned. Co-authored-by: Mark Kavanagh Signed-off-by: Mark Kavanagh Signed-off-by: Tiago Lam --- lib/dp-packet.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++-------- lib/dp-packet.h | 5 +++ 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/lib/dp-packet.c b/lib/dp-packet.c index 782e7c2..1b39946 100644 --- a/lib/dp-packet.c +++ b/lib/dp-packet.c @@ -312,27 +312,66 @@ dp_packet_shift(struct dp_packet *b, int delta) } } -/* Appends 'size' bytes of data to the tail end of 'b', reallocating and - * copying its data if necessary. Returns a pointer to the first byte of the - * new data, which is left uninitialized. */ -void * -dp_packet_put_uninit(struct dp_packet *b, size_t size) +static void * +dp_packet_put_uninit_(struct dp_packet *b, size_t size) { void *p; dp_packet_prealloc_tailroom(b, size); +#ifdef DPDK_NETDEV + p = dp_packet_mbuf_tail(b); + /* In the case of multi-segment mbufs, the data length of the last mbuf + * should be adjusted by 'size' bytes. The packet length of the entire + * mbuf chain (stored in the first mbuf of said chain) is adjusted in + * the normal execution path below. + */ + + struct rte_mbuf *tmbuf = (struct rte_mbuf *) p; + struct mbuf_tail *mbuf_tail = xmalloc(sizeof(*mbuf_tail)); + size_t pkt_len = size; + + mbuf_tail->mbuf = tmbuf; + mbuf_tail->ofs = tmbuf->data_len; + + /* Adjust size of intermediate mbufs from current tail to end */ + while (tmbuf && pkt_len > 0) { + tmbuf->data_len = MIN(pkt_len, tmbuf->buf_len - tmbuf->data_off); + pkt_len -= tmbuf->data_len; + + tmbuf = tmbuf->next; + } + + p = (void *) mbuf_tail; +#else p = dp_packet_tail(b); +#endif dp_packet_set_size(b, dp_packet_size(b) + size); + return p; } -/* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is - * reallocated and copied if necessary. Returns a pointer to the first byte of - * the data's location in the dp_packet. */ +static void * +dp_packet_tail_to_addr(struct dp_packet *b OVS_UNUSED, void *p) { +#ifdef DPDK_NETDEV + struct mbuf_tail *mbuf_tail = (struct mbuf_tail *) p; + p = (void *) rte_pktmbuf_mtod_offset(mbuf_tail->mbuf, void *, + mbuf_tail->ofs); +#endif + + return p; +} + +/* Appends 'size' bytes of data to the tail end of 'b', reallocating and + * copying its data if necessary. Returns a pointer to the first byte of the + * new data, which is left uninitialized. */ void * -dp_packet_put_zeros(struct dp_packet *b, size_t size) +dp_packet_put_uninit(struct dp_packet *b, size_t size) { - void *dst = dp_packet_put_uninit(b, size); - memset(dst, 0, size); + void *tail = dp_packet_put_uninit_(b, size); + + void *dst = dp_packet_tail_to_addr(b, tail); + + free(tail); + return dst; } @@ -342,8 +381,40 @@ dp_packet_put_zeros(struct dp_packet *b, size_t size) void * dp_packet_put(struct dp_packet *b, const void *p, size_t size) { - void *dst = dp_packet_put_uninit(b, size); - memcpy(dst, p, size); + void *tail = dp_packet_put_uninit_(b, size); +#ifdef DPDK_NETDEV + struct mbuf_tail *mbuf_tail = (struct mbuf_tail *) tail; + + dp_packet_mbuf_write(mbuf_tail->mbuf, mbuf_tail->ofs, size, p); +#else + memcpy(tail, p, size); +#endif + + void *dst = dp_packet_tail_to_addr(b, tail); + + free(tail); + + return dst; +} + +/* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is + * reallocated and copied if necessary. Returns a pointer to the first byte of + * the data's location in the dp_packet. */ +void * +dp_packet_put_zeros(struct dp_packet *b, size_t size) +{ + void *dst; +#ifdef DPDK_NETDEV + char zeros[size]; + memset(zeros, 0, size); + + dst = dp_packet_put(b, zeros, size); + + return dst; +#endif + dst = dp_packet_put_uninit(b, size); + memset(dst, 0, size); + return dst; } diff --git a/lib/dp-packet.h b/lib/dp-packet.h index 4d4b420..2fa6205 100644 --- a/lib/dp-packet.h +++ b/lib/dp-packet.h @@ -83,6 +83,11 @@ struct dp_packet { #ifdef DPDK_NETDEV #define MBUF_BUF_END(BUF_ADDR, BUF_LEN) \ (char *) (((char *) BUF_ADDR) + BUF_LEN) + +struct mbuf_tail { + struct rte_mbuf *mbuf; + uint16_t ofs; +}; #endif static inline void *dp_packet_data(const struct dp_packet *);