From patchwork Fri Sep 15 12:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liang Chen X-Patchwork-Id: 13387010 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26FA310948 for ; Fri, 15 Sep 2023 12:24:06 +0000 (UTC) Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D343268F for ; Fri, 15 Sep 2023 05:24:04 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-573d52030fbso1701619a12.0 for ; Fri, 15 Sep 2023 05:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694780643; x=1695385443; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nNLIWA4i5VL9jGjfNW9VF4j/lei4wxTdyKGKThaxys0=; b=fCh1/+XU9LO4Xx14uEnuDfhsuQN/ocSsVzHAkRY0v08EMNgNjFadwOkxGj+RG9junX lq7dWa/iyTKQc3sV7TFqA6shmU0I4Kc5QQmdsLh05SZbNLTAqS15suHzxK+wxfNUv5UI mrElIZkchBTe3/XxLk3aE1em9gzWm9B4G9fT14OvcIEyd64A06vuZ+FJp15J4VjrqbRi LPU2Jppr0F9sgvmquedYFOjSoCgSNB3FRqDF8BOsnXoaFTrkfUC1FQQigBnMgPw5AW7n 9HxkyjDhKfnUxkQEQlM2Ha6XSXlMhF34jUXAjz2QKR8tkE/yYy7On/nvXQ3nH2M/4bOQ Tewg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694780643; x=1695385443; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nNLIWA4i5VL9jGjfNW9VF4j/lei4wxTdyKGKThaxys0=; b=CeH8O9mNmsYTCfnrJ34Q/GJlPEa8gafFlxb/6WF3A1Zi/7VUWjFkHU53aZ9Q3/s579 d1xwiA5El420nQ9Ymi4U5ri2VDaHxrAsI/D3jW+cRpOVZ62/KdpnFk9F1CIvd6ZNvVkJ o0qo9EeWcjJJX6ndAq4bi+iCCnSA6hsUYwfp71wHxtad0fxFLSwoXdpfBeTHChXuk4KL hGIxyQ++o4Qa7RgfkOAAN+EYpSWaLXZ0LG7UmB0TEFHwnjvmnZtNZrR6LLq0M5lcU9fe ZRpxh6hZtBtz2zlYTV6k9jhqeHy+FRY/9FiCmUq/av4J5/99o+fRNow7DvX+WtLohZwL hYcg== X-Gm-Message-State: AOJu0Yx0B1c0q8psQAVkRzZwBg9pVHU+lYKz1Gh0brPVXxTprQ9sFdE+ Vb1zViHfUxnvL53LaR+OM+M= X-Google-Smtp-Source: AGHT+IEFcsHo5Sbczc2DW8TTln1k9tGayeZNOacQvgbqGBAekY0hmybmjRE1B8Q5ySMWM2iHId9UvQ== X-Received: by 2002:a17:90a:1189:b0:274:2523:fc7f with SMTP id e9-20020a17090a118900b002742523fc7fmr1238141pja.47.1694780643546; Fri, 15 Sep 2023 05:24:03 -0700 (PDT) Received: from 192.168.0.123 ([199.119.202.101]) by smtp.googlemail.com with ESMTPSA id 23-20020a17090a199700b00267eead2f16sm3167231pji.36.2023.09.15.05.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 05:24:02 -0700 (PDT) From: Liang Chen To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, benjamin.poirier@gmail.com Cc: netdev@vger.kernel.org, liangchen.linux@gmail.com Subject: [PATCH net-next v3 1/2] pktgen: Automate flag enumeration for unknown flag handling Date: Fri, 15 Sep 2023 20:23:16 +0800 Message-Id: <20230915122317.100390-1-liangchen.linux@gmail.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org When specifying an unknown flag, it will print all available flags. Currently, these flags are provided as fixed strings, which requires manual updates when flags change. Replacing it with automated flag enumeration. Signed-off-by: Liang Chen --- net/core/pktgen.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index f56b8d697014..ffd659dbd6c3 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -1318,6 +1318,7 @@ static ssize_t pktgen_if_write(struct file *file, return count; } if (!strcmp(name, "flag")) { + unsigned int n; __u32 flag; char f[32]; bool disable = false; @@ -1339,18 +1340,19 @@ static ssize_t pktgen_if_write(struct file *file, else pkt_dev->flags |= flag; } else { - sprintf(pg_result, - "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", - f, - "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, " - "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, " - "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, " - "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, " - "NO_TIMESTAMP, " -#ifdef CONFIG_XFRM - "IPSEC, " + pg_result += sprintf(pg_result, + "Flag -:%s:- unknown\n%s", f, + "Available flags, (prepend ! to un-set flag):\n"); + for (n = 0; n < NR_PKT_FLAGS; n++) { +#ifndef CONFIG_XFRM + if (!strcmp("IPSEC", pkt_flag_names[n])) + continue; #endif - "NODE_ALLOC\n"); + pg_result += sprintf(pg_result, "%s, ", pkt_flag_names[n]); + } + /* Remove the comma and whitespace at the end */ + *(pg_result - 2) = '\n'; + return count; } sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); From patchwork Fri Sep 15 12:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liang Chen X-Patchwork-Id: 13387011 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96DDF10788 for ; Fri, 15 Sep 2023 12:24:29 +0000 (UTC) Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D716268E for ; Fri, 15 Sep 2023 05:24:18 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-5657add1073so1526708a12.0 for ; Fri, 15 Sep 2023 05:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694780658; x=1695385458; darn=vger.kernel.org; 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=Lz+KwltvljtV/MAPhQIKZt+LMsG+MjU6bvq7aqjMrAg=; b=b4YmHcN4Am22cYKSuL/PiXsUH0SAsAYyykE79ikxUJzj2S7DJ8Y29D6eCCDAkPoJZ7 3jPgcwNXn9+jPw2mk/G/IQDl2oDcl50B/2JHrnR+DFZ8FOb7gQrhDBk6v0ShHO5jEyfn KX/esqqf4bmbHo5aIN/X7ShQBy6fCiQ2taBho36byWN8pgJUw5+77VLYVTw8e050TB0Q hsVziGF5ED5Pz3KGAMOFHJjUxqcAKN9xlsK3BjSdDUC6kb9VZaIxMOfJbwB6M79+yyfO dPVRZPigUEzNU8PyAKeLXOpheghlW7dMPukP87GlOkIrCQxEyWPTtn+TYLSkcgKFlUCl tKAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694780658; x=1695385458; 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=Lz+KwltvljtV/MAPhQIKZt+LMsG+MjU6bvq7aqjMrAg=; b=fRCnkXRiNt9DmVRohQqDR5MY7Krp3UOLUwizf0TersIGWxkMD4Fi3ox59pz26B1OBp +t2sVWae1oD9b54gjAMqESsKZrfiYYngPCfGF57jzD5hUmOAirK7ZFXN0SiuH2AvfWma qiOGB89zJkBr+JADqA2llKeM0+howvrpgpZUEygaZmRJkFafTFlwkkPNbD6yEThhCInc HgLcbMZhM9lemkwTKkyBwTsgmpLO9XMhLdNtp+fb0hE+SJqD0x6A1Hj9yQVgnlsFlTCo Jnnp+10ue5Jb5kSMASptANHMlWXNotfL7BF9VZdZ9dEV6Ssx61T/Z9/+8Bwc0fByetjB yuIg== X-Gm-Message-State: AOJu0Yxv5grG9PPr644T/o2zTfe23XztbeJR15XXI92w7nrRmIlNa8z8 IuQwrrcwok7WvM+CKrpsrMLm3cbOavUg3Q== X-Google-Smtp-Source: AGHT+IG9tfu/vaUFy5XipV53xEIlZP2C0sUshnq2L6iUOHSz0VkYqmEMAOC8j3t4JpiiphUymHoevA== X-Received: by 2002:a17:90b:a43:b0:273:e090:6096 with SMTP id gw3-20020a17090b0a4300b00273e0906096mr2087317pjb.11.1694780657995; Fri, 15 Sep 2023 05:24:17 -0700 (PDT) Received: from 192.168.0.123 ([199.119.202.101]) by smtp.googlemail.com with ESMTPSA id 23-20020a17090a199700b00267eead2f16sm3167231pji.36.2023.09.15.05.24.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 05:24:16 -0700 (PDT) From: Liang Chen To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, benjamin.poirier@gmail.com Cc: netdev@vger.kernel.org, liangchen.linux@gmail.com Subject: [PATCH net-next v3 2/2] pktgen: Introducing 'SHARED' flag for testing with non-shared skb Date: Fri, 15 Sep 2023 20:23:17 +0800 Message-Id: <20230915122317.100390-2-liangchen.linux@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230915122317.100390-1-liangchen.linux@gmail.com> References: <20230915122317.100390-1-liangchen.linux@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org Currently, skbs generated by pktgen always have their reference count incremented before transmission, causing their reference count to be always greater than 1, leading to two issues: 1. Only the code paths for shared skbs can be tested. 2. In certain situations, skbs can only be released by pktgen. To enhance testing comprehensiveness, we are introducing the "SHARED" flag to indicate whether an SKB is shared. This flag is enabled by default, aligning with the current behavior. However, disabling this flag allows skbs with a reference count of 1 to be transmitted. So we can test non-shared skbs and code paths where skbs are released within the stack. Signed-off-by: Liang Chen --- Changes from v2: - Lifted the check on 'count' when 'not shared' is configured. - Fixed a use-after-free problem when sending failed --- Documentation/networking/pktgen.rst | 12 ++++++++ net/core/pktgen.c | 47 ++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/Documentation/networking/pktgen.rst b/Documentation/networking/pktgen.rst index 1225f0f63ff0..c945218946e1 100644 --- a/Documentation/networking/pktgen.rst +++ b/Documentation/networking/pktgen.rst @@ -178,6 +178,7 @@ Examples:: IPSEC # IPsec encapsulation (needs CONFIG_XFRM) NODE_ALLOC # node specific memory allocation NO_TIMESTAMP # disable timestamping + SHARED # enable shared SKB pgset 'flag ![name]' Clear a flag to determine behaviour. Note that you might need to use single quote in interactive mode, so that your shell wouldn't expand @@ -288,6 +289,16 @@ To avoid breaking existing testbed scripts for using AH type and tunnel mode, you can use "pgset spi SPI_VALUE" to specify which transformation mode to employ. +Disable shared SKB +================== +By default, SKBs sent by pktgen are shared (user count > 1). +To test with non-shared SKBs, remove the "SHARED" flag by simply setting:: + + pg_set "flag !SHARED" + +However, if the "clone_skb" or "burst" parameters are configured, the skb +still needs to be held by pktgen for further access. Hence the skb must be +shared. Current commands and configuration options ========================================== @@ -357,6 +368,7 @@ Current commands and configuration options IPSEC NODE_ALLOC NO_TIMESTAMP + SHARED spi (ipsec) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index ffd659dbd6c3..5cc69feec7d7 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -200,6 +200,7 @@ pf(VID_RND) /* Random VLAN ID */ \ pf(SVID_RND) /* Random SVLAN ID */ \ pf(NODE) /* Node memory alloc*/ \ + pf(SHARED) /* Shared SKB */ \ #define pf(flag) flag##_SHIFT, enum pkt_flags { @@ -1198,7 +1199,8 @@ static ssize_t pktgen_if_write(struct file *file, ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) || !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING))) return -ENOTSUPP; - if (value > 0 && pkt_dev->n_imix_entries > 0) + if (value > 0 && (pkt_dev->n_imix_entries > 0 || + !(pkt_dev->flags & F_SHARED))) return -EINVAL; i += len; @@ -1257,6 +1259,10 @@ static ssize_t pktgen_if_write(struct file *file, ((pkt_dev->xmit_mode == M_START_XMIT) && (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING))))) return -ENOTSUPP; + + if ((value > 1) && !(pkt_dev->flags & F_SHARED)) + return -EINVAL; + pkt_dev->burst = value < 1 ? 1 : value; sprintf(pg_result, "OK: burst=%u", pkt_dev->burst); return count; @@ -1335,10 +1341,18 @@ static ssize_t pktgen_if_write(struct file *file, flag = pktgen_read_flag(f, &disable); if (flag) { - if (disable) + if (disable) { + /* If "clone_skb", or "burst" parameters are + * configured, it means that the skb still needs to be + * referenced by the pktgen, so the skb must be shared. + */ + if (flag == F_SHARED && (pkt_dev->clone_skb || + pkt_dev->burst > 1)) + return -EINVAL; pkt_dev->flags &= ~flag; - else + } else { pkt_dev->flags |= flag; + } } else { pg_result += sprintf(pg_result, "Flag -:%s:- unknown\n%s", f, @@ -3485,7 +3499,8 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) { skb = pkt_dev->skb; skb->protocol = eth_type_trans(skb, skb->dev); - refcount_add(burst, &skb->users); + if (pkt_dev->flags & F_SHARED) + refcount_add(burst, &skb->users); local_bh_disable(); do { ret = netif_receive_skb(skb); @@ -3493,6 +3508,10 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) pkt_dev->errors++; pkt_dev->sofar++; pkt_dev->seq_num++; + if (unlikely(!(pkt_dev->flags & F_SHARED))) { + pkt_dev->skb = NULL; + break; + } if (refcount_read(&skb->users) != burst) { /* skb was queued by rps/rfs or taps, * so cannot reuse this skb @@ -3511,9 +3530,14 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) goto out; /* Skips xmit_mode M_START_XMIT */ } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) { local_bh_disable(); - refcount_inc(&pkt_dev->skb->users); + if (pkt_dev->flags & F_SHARED) + refcount_inc(&pkt_dev->skb->users); ret = dev_queue_xmit(pkt_dev->skb); + + if (!(pkt_dev->flags & F_SHARED) && dev_xmit_complete(ret)) + pkt_dev->skb = NULL; + switch (ret) { case NET_XMIT_SUCCESS: pkt_dev->sofar++; @@ -3551,11 +3575,15 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) pkt_dev->last_ok = 0; goto unlock; } - refcount_add(burst, &pkt_dev->skb->users); + if (pkt_dev->flags & F_SHARED) + refcount_add(burst, &pkt_dev->skb->users); xmit_more: ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0); + if (!(pkt_dev->flags & F_SHARED) && dev_xmit_complete(ret)) + pkt_dev->skb = NULL; + switch (ret) { case NETDEV_TX_OK: pkt_dev->last_ok = 1; @@ -3577,7 +3605,8 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) fallthrough; case NETDEV_TX_BUSY: /* Retry it next time */ - refcount_dec(&(pkt_dev->skb->users)); + if (!(pkt_dev->flags & F_SHARED)) + refcount_dec(&(pkt_dev->skb->users)); pkt_dev->last_ok = 0; } if (unlikely(burst)) @@ -3590,7 +3619,8 @@ static void pktgen_xmit(struct pktgen_dev *pkt_dev) /* If pkt_dev->count is zero, then run forever */ if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { - pktgen_wait_for_skb(pkt_dev); + if (pkt_dev->skb) + pktgen_wait_for_skb(pkt_dev); /* Done with this */ pktgen_stop_device(pkt_dev); @@ -3773,6 +3803,7 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) pkt_dev->svlan_id = 0xffff; pkt_dev->burst = 1; pkt_dev->node = NUMA_NO_NODE; + pkt_dev->flags = F_SHARED; /* SKB shared by default */ err = pktgen_setup_dev(t->net, pkt_dev, ifname); if (err)