From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by dpdk.org (Postfix) with ESMTP id 5BD7A2C28 for ; Thu, 28 Mar 2019 21:53:28 +0100 (CET) Received: by mail-pf1-f196.google.com with SMTP id c8so11893585pfd.10 for ; Thu, 28 Mar 2019 13:53:28 -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; bh=00kGNP2z+JPIaop+YydMvT+Zz86F0f1/pnIMTaxpIBA=; b=JGT7f5j9ngEHIOUdBoBeqbcLkpMTT0fXELitoJi5VEZnlO95vUc3bvFHuccrwWEqDS lhXB3bQkBBI0GzNPs6VAeUlsqIU3nPimNqGFpugNGMtjmdGdbAQIecfJS3ZGHvAuV+7E g3Gl9r0WiQmbIXMzJaakFuYXiaNLRJajzNecDL5FFa3JU67I/M0QiWZr9fsSx9DH6l4J 2CaEGJTcauHLdXA54YAz22OjyjbXEJ9fKS5DXhqkJlscwnIXSvWlPHxFFAE4Io12l7uS DoPKp+ucaQ2YFa3Z+QD3+DeH2TCESQa2WZ/FFeoebNKtXy2fhNxtp+bv3AzkN6rAc8sN DodA== 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; bh=00kGNP2z+JPIaop+YydMvT+Zz86F0f1/pnIMTaxpIBA=; b=Odz1QW3B2rMa3QY9LAwgKtuTcchnJfs5a7VYwp24MzXY5dmgkp/IgVlDiRqV9xTCFe XwNYXDKvYUJzZe5P6lfeGhpyBQIJCFZOFo5k1JLCL5Dq6sDNAl2xna851ZWqo/wbfGhr rt+pCrmXop6tJqltk2+kvRqXzQtJrm8rxojLr3Lgl39k+zw8emBhZd53Pgk92BwT7D6P gWmNcJRjPE7P6taI4Wr+eFU4lyMwbyhXq2AOhxYqE13CDyqoduuEjA13hOiNm2TskEjg E47Dl+bkRqbKCKIlHzznq2VC5TUrjwKUWeVIg+aPP8XeV4LFvf0y8BEXNf+nupMXBG0x pN+w== X-Gm-Message-State: APjAAAUiI3AQz6ziD2m8SzroOtj09Tm/7XmVYXnc5lIzn2rVLPsngMcX pwHgmKhmN9R+OFAm4pHIFRpNepheXKzi3g== X-Google-Smtp-Source: APXvYqwVC5oPkhMDjO4JWHvGRNwFvNl/08nx3yVd6yLbxH893XJS4P48odUw5cVgoBnB1cV+9RlU6w== X-Received: by 2002:a62:1e06:: with SMTP id e6mr43099362pfe.168.1553806407233; Thu, 28 Mar 2019 13:53:27 -0700 (PDT) Received: from shemminger-XPS-13-9360.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id 20sm94829pfn.131.2019.03.28.13.53.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Mar 2019 13:53:26 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Stephen Hemminger Date: Thu, 28 Mar 2019 13:53:22 -0700 Message-Id: <20190328205322.852-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Subject: [dpdk-dev] [RFC v2] net: fix rte_vlan_insert with shared mbuf 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: , X-List-Received-Date: Thu, 28 Mar 2019 20:53:28 -0000 If mbuf is shared then rte_vlan_insert() would clobber the original Ethernet header. The changed version handles this by getting an mbuf that will hold the new Ethernet and VLAN header followed by another mbuf (cloned) for the data. Fixes: c974021a5949 ("ether: add soft vlan encap/decap") Signed-off-by: Stephen Hemminger --- v2 - compile tested only, do copy/clone. lib/librte_net/rte_ether.h | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h index c2c5e249ffe9..5fc306e5d08c 100644 --- a/lib/librte_net/rte_ether.h +++ b/lib/librte_net/rte_ether.h @@ -374,10 +374,10 @@ static inline int rte_vlan_strip(struct rte_mbuf *m) * Software version of VLAN unstripping * * @param m - * The packet mbuf. + * Pointer to the packet mbuf. * @return * - 0: On success - * -EPERM: mbuf is is shared overwriting would be unsafe + * -ENOMEM: could not allocate mbuf for header * -ENOSPC: not enough headroom in mbuf */ static inline int rte_vlan_insert(struct rte_mbuf **m) @@ -385,15 +385,34 @@ static inline int rte_vlan_insert(struct rte_mbuf **m) struct ether_hdr *oh, *nh; struct vlan_hdr *vh; - /* Can't insert header if mbuf is shared */ - if (rte_mbuf_refcnt_read(*m) > 1) { - struct rte_mbuf *copy; + /* Can't safely directly insert header if mbuf is shared or indirect */ + if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1) { + struct rte_mempool *mp = (*m)->pool; + struct rte_mbuf *md, *mh; + + mh = rte_pktmbuf_alloc(mp); + if (unlikely(mh == NULL)) + return -ENOMEM; + + mh->tx_offload = (*m)->tx_offload; + mh->vlan_tci = (*m)->vlan_tci; + mh->vlan_tci_outer = (*m)->vlan_tci_outer; + mh->port = (*m)->port; + mh->ol_flags = (*m)->ol_flags; + mh->packet_type = (*m)->packet_type; - copy = rte_pktmbuf_clone(*m, (*m)->pool); - if (unlikely(copy == NULL)) + md = rte_pktmbuf_clone(*m, mp); + if (unlikely(md == NULL)) { + rte_pktmbuf_free(mh); return -ENOMEM; - rte_pktmbuf_free(*m); - *m = copy; + } + + mh->next = md; + mh->nb_segs = md->nb_segs + 1; + memcpy(rte_pktmbuf_append(mh, ETHER_HDR_LEN), + rte_pktmbuf_mtod(md, void *), ETHER_HDR_LEN); + rte_pktmbuf_adj(md, ETHER_HDR_LEN); + *m = mh; } oh = rte_pktmbuf_mtod(*m, struct ether_hdr *); -- 2.17.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id 0D2D9A0679 for ; Thu, 28 Mar 2019 21:53:30 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BDB1C326D; Thu, 28 Mar 2019 21:53:29 +0100 (CET) Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by dpdk.org (Postfix) with ESMTP id 5BD7A2C28 for ; Thu, 28 Mar 2019 21:53:28 +0100 (CET) Received: by mail-pf1-f196.google.com with SMTP id c8so11893585pfd.10 for ; Thu, 28 Mar 2019 13:53:28 -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; bh=00kGNP2z+JPIaop+YydMvT+Zz86F0f1/pnIMTaxpIBA=; b=JGT7f5j9ngEHIOUdBoBeqbcLkpMTT0fXELitoJi5VEZnlO95vUc3bvFHuccrwWEqDS lhXB3bQkBBI0GzNPs6VAeUlsqIU3nPimNqGFpugNGMtjmdGdbAQIecfJS3ZGHvAuV+7E g3Gl9r0WiQmbIXMzJaakFuYXiaNLRJajzNecDL5FFa3JU67I/M0QiWZr9fsSx9DH6l4J 2CaEGJTcauHLdXA54YAz22OjyjbXEJ9fKS5DXhqkJlscwnIXSvWlPHxFFAE4Io12l7uS DoPKp+ucaQ2YFa3Z+QD3+DeH2TCESQa2WZ/FFeoebNKtXy2fhNxtp+bv3AzkN6rAc8sN DodA== 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; bh=00kGNP2z+JPIaop+YydMvT+Zz86F0f1/pnIMTaxpIBA=; b=Odz1QW3B2rMa3QY9LAwgKtuTcchnJfs5a7VYwp24MzXY5dmgkp/IgVlDiRqV9xTCFe XwNYXDKvYUJzZe5P6lfeGhpyBQIJCFZOFo5k1JLCL5Dq6sDNAl2xna851ZWqo/wbfGhr rt+pCrmXop6tJqltk2+kvRqXzQtJrm8rxojLr3Lgl39k+zw8emBhZd53Pgk92BwT7D6P gWmNcJRjPE7P6taI4Wr+eFU4lyMwbyhXq2AOhxYqE13CDyqoduuEjA13hOiNm2TskEjg E47Dl+bkRqbKCKIlHzznq2VC5TUrjwKUWeVIg+aPP8XeV4LFvf0y8BEXNf+nupMXBG0x pN+w== X-Gm-Message-State: APjAAAUiI3AQz6ziD2m8SzroOtj09Tm/7XmVYXnc5lIzn2rVLPsngMcX pwHgmKhmN9R+OFAm4pHIFRpNepheXKzi3g== X-Google-Smtp-Source: APXvYqwVC5oPkhMDjO4JWHvGRNwFvNl/08nx3yVd6yLbxH893XJS4P48odUw5cVgoBnB1cV+9RlU6w== X-Received: by 2002:a62:1e06:: with SMTP id e6mr43099362pfe.168.1553806407233; Thu, 28 Mar 2019 13:53:27 -0700 (PDT) Received: from shemminger-XPS-13-9360.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id 20sm94829pfn.131.2019.03.28.13.53.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Mar 2019 13:53:26 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Stephen Hemminger Date: Thu, 28 Mar 2019 13:53:22 -0700 Message-Id: <20190328205322.852-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Subject: [dpdk-dev] [RFC v2] net: fix rte_vlan_insert with shared mbuf 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" Content-Type: text/plain; charset="UTF-8" Message-ID: <20190328205322.2QiaAOBY2_QFICQkynXdF4CjRXUx3b8BRgCE08r5HEI@z> If mbuf is shared then rte_vlan_insert() would clobber the original Ethernet header. The changed version handles this by getting an mbuf that will hold the new Ethernet and VLAN header followed by another mbuf (cloned) for the data. Fixes: c974021a5949 ("ether: add soft vlan encap/decap") Signed-off-by: Stephen Hemminger --- v2 - compile tested only, do copy/clone. lib/librte_net/rte_ether.h | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h index c2c5e249ffe9..5fc306e5d08c 100644 --- a/lib/librte_net/rte_ether.h +++ b/lib/librte_net/rte_ether.h @@ -374,10 +374,10 @@ static inline int rte_vlan_strip(struct rte_mbuf *m) * Software version of VLAN unstripping * * @param m - * The packet mbuf. + * Pointer to the packet mbuf. * @return * - 0: On success - * -EPERM: mbuf is is shared overwriting would be unsafe + * -ENOMEM: could not allocate mbuf for header * -ENOSPC: not enough headroom in mbuf */ static inline int rte_vlan_insert(struct rte_mbuf **m) @@ -385,15 +385,34 @@ static inline int rte_vlan_insert(struct rte_mbuf **m) struct ether_hdr *oh, *nh; struct vlan_hdr *vh; - /* Can't insert header if mbuf is shared */ - if (rte_mbuf_refcnt_read(*m) > 1) { - struct rte_mbuf *copy; + /* Can't safely directly insert header if mbuf is shared or indirect */ + if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1) { + struct rte_mempool *mp = (*m)->pool; + struct rte_mbuf *md, *mh; + + mh = rte_pktmbuf_alloc(mp); + if (unlikely(mh == NULL)) + return -ENOMEM; + + mh->tx_offload = (*m)->tx_offload; + mh->vlan_tci = (*m)->vlan_tci; + mh->vlan_tci_outer = (*m)->vlan_tci_outer; + mh->port = (*m)->port; + mh->ol_flags = (*m)->ol_flags; + mh->packet_type = (*m)->packet_type; - copy = rte_pktmbuf_clone(*m, (*m)->pool); - if (unlikely(copy == NULL)) + md = rte_pktmbuf_clone(*m, mp); + if (unlikely(md == NULL)) { + rte_pktmbuf_free(mh); return -ENOMEM; - rte_pktmbuf_free(*m); - *m = copy; + } + + mh->next = md; + mh->nb_segs = md->nb_segs + 1; + memcpy(rte_pktmbuf_append(mh, ETHER_HDR_LEN), + rte_pktmbuf_mtod(md, void *), ETHER_HDR_LEN); + rte_pktmbuf_adj(md, ETHER_HDR_LEN); + *m = mh; } oh = rte_pktmbuf_mtod(*m, struct ether_hdr *); -- 2.17.1