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 95AEB41D63; Fri, 24 Feb 2023 12:25:38 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2DA6140697; Fri, 24 Feb 2023 12:25:38 +0100 (CET) Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by mails.dpdk.org (Postfix) with ESMTP id B0F5F40693 for ; Fri, 24 Feb 2023 12:25:36 +0100 (CET) Received: by mail-wr1-f41.google.com with SMTP id c12so13569973wrw.1 for ; Fri, 24 Feb 2023 03:25:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NpwUD7q1UT78xG4hzHuN3KGwnr20rw/6fGf1ERB3cA0=; b=dAkSO4+dzikvSgcRaOj7VH/Vc+WBlfiV01tdwlRAhp54EfobkjFOfgLPWseOCoC99k cJZyiGyAviChzesDHNx6zE8KrDTl1KDqJRjdm2EzK3LfqAOg11dxGEdnG+P4c9GneiQ4 uVXTc6BeSp02wkmE22ZYsRZWr9VAnuySLXdaZC6npKsiyDTzPSKrc2GuVzdDL3Rt86v/ BDtwRSZ60OlGJ2B8Ts7gui6eSnVsNsdwLuDm5/3MXqx4HamhQkquTuBX7YS7cPo4tNrM K8vfEJvtrq4G1FHdwJ3RBPaaRgVXpmQWsljS4Vsk2f/outhtDC50zyQTLINHUVaJI9dv iE5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NpwUD7q1UT78xG4hzHuN3KGwnr20rw/6fGf1ERB3cA0=; b=sJgJJXCtUC+l5h2WvzeoSwvrFpgSCd2lC1kYtRYu4IOyUp3kBcs533sDvp+TgWANyU sbCpWa23bV7+DBc1aiAm9givzo7pICgOHdC6rZcJpce2FNRRtj/j5An9HudxkdIKkyVI r8wnoXZvfvHpIfj6/SAvPg96oHU0x1rFqfYZ0ymrTEEkzV9B5x9FKYbroKvdy/5bMXED LGNnkcykBGMGQYz5a8KtU2TDveOdoG+BCrS33GUQicxEH4a4TyYPmzZLi53/ODCp9eG9 jvWh0llQxP0Kik2iAX/Dh+D1JkKwbhr1V9nLQAQH1e1frtqHGPPS0sA42DTCrUKhYxw7 0aLA== X-Gm-Message-State: AO0yUKWy6LQbQVuFP1E3sBEGvv8bhJEaWor9n+cLdEmPfqRDd/oxdDNv Hgxq0+JcqmSBLDOfQTyrePo= X-Google-Smtp-Source: AK7set/l/+zVl+mepeLhdiMC0sWn6YphnzpQmGNxTQnkvRWfh03/hOMNl9q+vok1I1WupwlkkT/Xaw== X-Received: by 2002:a5d:4303:0:b0:2c3:eeeb:b2f7 with SMTP id h3-20020a5d4303000000b002c3eeebb2f7mr13726882wrq.62.1677237936212; Fri, 24 Feb 2023 03:25:36 -0800 (PST) Received: from DESKTOP-KPIMD3E.localdomain ([2407:d000:f:9db9:a92c:5562:804c:14b1]) by smtp.gmail.com with ESMTPSA id d9-20020a056000114900b002c5584d0e3dsm12699398wrx.24.2023.02.24.03.25.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 03:25:35 -0800 (PST) From: Tanzeel-inline To: olivier.matz@6wind.com, thomas@monjalon.net, tanzeelahmed713@gmail.com Cc: dev@dpdk.org, Tanzeel Ahmed Subject: [PATCH v3] lib/net: add MPLS insert and strip functionality Date: Fri, 24 Feb 2023 16:25:21 +0500 Message-Id: <20230224112521.27-1-tanxeel1.ahmed@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230219224334.309-1-tanxeel1.ahmed@gmail.com> References: <20230219224334.309-1-tanxeel1.ahmed@gmail.com> MIME-Version: 1.0 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 From: Tanzeel Ahmed This patch is new version of [PATCH] lib/net: added push MPLS header API. I have also added the MPLS strip functionality to address the question asked in last patch. > You should explain why you add this function. None of the foundational NICs currently supports MPLS insertion and stripping, this functionality can help users who rely on MPLS in their network application. > I'm not sure it should be inline I did for performance in high-traffic application. Signed-off-by: Tanzeel Ahmed --- v3: * fixed patch checks failure v2: * marked experimental * coding style fixed * changed rte_memcpy to memcpy * mpls header marked as const in parameter * added MPLS stripping functionality --- .mailmap | 1 + lib/net/rte_mpls.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/.mailmap b/.mailmap index a9f4f28..2af4e0d 100644 --- a/.mailmap +++ b/.mailmap @@ -1312,6 +1312,7 @@ Takeshi Yoshimura Takuya Asada Tal Avraham Tal Shnaiderman +Tanzeel Ahmed Tao Y Yang Tao Zhu Taripin Samuel diff --git a/lib/net/rte_mpls.h b/lib/net/rte_mpls.h index 51523e7..14b55fe 100644 --- a/lib/net/rte_mpls.h +++ b/lib/net/rte_mpls.h @@ -13,6 +13,8 @@ #include #include +#include +#include #ifdef __cplusplus extern "C" { @@ -36,6 +38,101 @@ struct rte_mpls_hdr { uint8_t ttl; /**< Time to live. */ } __rte_packed; +#define RTE_MPLS_HLEN 4 /**< Length of MPLS header. */ + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Insert MPLS header into the packet. + * If it's first MPLS header to be inserted in the packet, + * - Updates the ether type. + * - Sets the MPLS bottom-of-stack bit to 1. + * + * @param m + * The pointer to the mbuf. + * @param mp + * The pointer to the MPLS header. + * @return + * 0 on success, -1 on error + */ +__rte_experimental +static inline int +rte_mpls_push_over_l2(struct rte_mbuf **m, const struct rte_mpls_hdr *mp) +{ + struct rte_ether_hdr *oh, *nh; + struct rte_mpls_hdr *mph; + + /* Can't insert header if mbuf is shared */ + if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1) + return -EINVAL; + + /* Can't insert header if ethernet frame doesn't exist */ + if (rte_pktmbuf_data_len(*m) < RTE_ETHER_HDR_LEN) + return -EINVAL; + + oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *); + nh = (struct rte_ether_hdr *)(void *) + rte_pktmbuf_prepend(*m, sizeof(struct rte_mpls_hdr)); + if (nh == NULL) + return -ENOSPC; + + memmove(nh, oh, RTE_ETHER_HDR_LEN); + + /* Copy the MPLS header after ethernet frame */ + mph = rte_pktmbuf_mtod_offset(*m, struct rte_mpls_hdr*, + sizeof(struct rte_ether_hdr)); + memcpy(mph, mp, RTE_MPLS_HLEN); + + mph->tag_msb = rte_cpu_to_be_16(mp->tag_msb); + + /* If first MPLS header, update ether type and bottom-of-stack bit */ + if (nh->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLS)) { + nh->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLS); + mph->bs = 1; + } else { + mph->bs = 0; + } + + return 0; +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Strips MPLS from the packet. Doesn't update the ether type + * + * @param m + * The pointer to the mbuf. + * @return + * 0 on success, -1 on error + */ +__rte_experimental +static inline int +rte_mpls_strip_over_l2(struct rte_mbuf *m) +{ + struct rte_ether_hdr *eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); + struct rte_mpls_hdr *mph; + bool mpls_exist = true; + + if (eh->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLS)) + return -1; + + /* Stripping all MPLS header */ + while (mpls_exist) { + mph = rte_pktmbuf_mtod_offset(m, struct rte_mpls_hdr*, + sizeof(struct rte_ether_hdr)); + if (mph->bs & 1) + mpls_exist = false; + memmove(rte_pktmbuf_adj(m, sizeof(struct rte_mpls_hdr)), + eh, sizeof(struct rte_ether_hdr)); + eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); + } + + return 0; +} + #ifdef __cplusplus } #endif -- 1.8.3.1