netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next v5 1/2] pktgen: Automate flag enumeration for unknown flag handling
@ 2023-09-20 12:56 Liang Chen
  2023-09-20 12:56 ` [PATCH net-next v5 2/2] pktgen: Introducing 'SHARED' flag for testing with non-shared skb Liang Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Liang Chen @ 2023-09-20 12:56 UTC (permalink / raw)
  To: davem, edumazet, kuba, pabeni, bpoirier, corbet
  Cc: netdev, linux-doc, gregkh, keescook, Jason, djwong, jack,
	linyunsheng, ulf.hansson, liangchen.linux

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 <liangchen.linux@gmail.com>
Signed-off-by: Benjamin Poirier <bpoirier@nvidia.com>
---
 Changes from v3:
- check "n == IPSEC_SHIFT" instead of string comparison
- use snprintf and check that the result does not overrun pkg_dev->result[]
- avoid double '\n' at the end
- move "return" in the OK case to remove "else" and decrease indent
---
 net/core/pktgen.c | 38 ++++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/net/core/pktgen.c b/net/core/pktgen.c
index f56b8d697014..48306a101fd9 100644
--- a/net/core/pktgen.c
+++ b/net/core/pktgen.c
@@ -1318,9 +1318,10 @@ static ssize_t pktgen_if_write(struct file *file,
 		return count;
 	}
 	if (!strcmp(name, "flag")) {
+		bool disable = false;
 		__u32 flag;
 		char f[32];
-		bool disable = false;
+		char *end;
 
 		memset(f, 0, 32);
 		len = strn_len(&user_buffer[i], sizeof(f) - 1);
@@ -1332,28 +1333,33 @@ static ssize_t pktgen_if_write(struct file *file,
 		i += len;
 
 		flag = pktgen_read_flag(f, &disable);
-
 		if (flag) {
 			if (disable)
 				pkt_dev->flags &= ~flag;
 			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, "
-#endif
-				"NODE_ALLOC\n");
+
+			sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
 			return count;
 		}
-		sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
+
+		/* Unknown flag */
+		end = pkt_dev->result + sizeof(pkt_dev->result);
+		pg_result += sprintf(pg_result,
+			"Flag -:%s:- unknown\n"
+			"Available flags, (prepend ! to un-set flag):\n", f);
+
+		for (int n = 0; n < NR_PKT_FLAGS && pg_result < end; n++) {
+			if (!IS_ENABLED(CONFIG_XFRM) && n == IPSEC_SHIFT)
+				continue;
+			pg_result += snprintf(pg_result, end - pg_result,
+					      "%s, ", pkt_flag_names[n]);
+		}
+		if (!WARN_ON_ONCE(pg_result >= end)) {
+			/* Remove the comma and whitespace at the end */
+			*(pg_result - 2) = '\0';
+		}
+
 		return count;
 	}
 	if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-09-30 18:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-20 12:56 [PATCH net-next v5 1/2] pktgen: Automate flag enumeration for unknown flag handling Liang Chen
2023-09-20 12:56 ` [PATCH net-next v5 2/2] pktgen: Introducing 'SHARED' flag for testing with non-shared skb Liang Chen
2023-09-28 11:21 ` [PATCH net-next v5 1/2] pktgen: Automate flag enumeration for unknown flag handling Simon Horman
2023-09-28 13:18   ` Benjamin Poirier
2023-09-30 18:13     ` Simon Horman
2023-09-28 14:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).