Re: [PATCH] libtraceevent plugins: Add plugin_net to handle bswap during print

Steven Rostedt <[email protected]> Thu, 17 Apr 2025 08:54:10 -0400
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
On Thu, 17 Apr 2025 07:49:00 -0400
Steven Rostedt <[email protected]> wrote:

> BTW, libtraceevent also provides its own swap functions so if the GCC
> builtin is not always available, then it should use that instead.
> 
>   See the "tep_data2host*()" fuctions.
> 
> But that may need to be updated as "tep_swapbytes*()" and have:
> 
> 	if (!tep || tep->host_bigendian != tep->file_bigendian)
> 		return data;

Something like this:

diff --git a/src/event-parse-api.c b/src/event-parse-api.c
index 8a0e976ade45..1897117142c9 100644
--- a/src/event-parse-api.c
+++ b/src/event-parse-api.c
@@ -106,52 +106,80 @@ bool tep_test_flag(struct tep_handle *tep, enum tep_flag flag)
 	return false;
 }
 
-__hidden unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data)
+static inline  unsigned short swap2(unsigned short data)
 {
-	unsigned short swap;
+	return ((data & 0xffULL) << 8) |
+		((data & (0xffULL << 8)) >> 8);
+}
 
+static inline  unsigned int swap4(unsigned int data)
+{
+	return ((data & 0xffULL) << 24) |
+		((data & (0xffULL << 8)) << 8) |
+		((data & (0xffULL << 16)) >> 8) |
+		((data & (0xffULL << 24)) >> 24);
+}
+
+static inline  unsigned long long swap8(unsigned long long data)
+{
+	return ((data & 0xffULL) << 56) |
+		((data & (0xffULL << 8)) << 40) |
+		((data & (0xffULL << 16)) << 24) |
+		((data & (0xffULL << 24)) << 8) |
+		((data & (0xffULL << 32)) >> 8) |
+		((data & (0xffULL << 40)) >> 24) |
+		((data & (0xffULL << 48)) >> 40) |
+		((data & (0xffULL << 56)) >> 56);
+}
+
+__hidden unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data)
+{
 	if (!tep || tep->host_bigendian == tep->file_bigendian)
 		return data;
 
-	swap = ((data & 0xffULL) << 8) |
-		((data & (0xffULL << 8)) >> 8);
-
-	return swap;
+	return swap2(data);
 }
 
 __hidden unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data)
 {
-	unsigned int swap;
-
 	if (!tep || tep->host_bigendian == tep->file_bigendian)
 		return data;
 
-	swap = ((data & 0xffULL) << 24) |
-		((data & (0xffULL << 8)) << 8) |
-		((data & (0xffULL << 16)) >> 8) |
-		((data & (0xffULL << 24)) >> 24);
-
-	return swap;
+	return swap4(data);
 }
 
 __hidden  unsigned long long
 tep_data2host8(struct tep_handle *tep, unsigned long long data)
 {
-	unsigned long long swap;
-
 	if (!tep || tep->host_bigendian == tep->file_bigendian)
 		return data;
 
-	swap = ((data & 0xffULL) << 56) |
-		((data & (0xffULL << 8)) << 40) |
-		((data & (0xffULL << 16)) << 24) |
-		((data & (0xffULL << 24)) << 8) |
-		((data & (0xffULL << 32)) >> 8) |
-		((data & (0xffULL << 40)) >> 24) |
-		((data & (0xffULL << 48)) >> 40) |
-		((data & (0xffULL << 56)) >> 56);
+	return swap8(data);
+}
+
+__hidden unsigned short tep_swapbytes2(struct tep_handle *tep, unsigned short data)
+{
+	if (tep && tep->host_bigendian != tep->file_bigendian)
+		return data;
+
+	return swap2(data);
+}
+
+__hidden unsigned int tep_swapbytes4(struct tep_handle *tep, unsigned int data)
+{
+	if (tep && tep->host_bigendian != tep->file_bigendian)
+		return data;
+
+	return swap4(data);
+}
+
+__hidden unsigned long long
+tep_swapbytes8(struct tep_handle *tep, unsigned long long data)
+{
+	if (tep && tep->host_bigendian != tep->file_bigendian)
+		return data;
 
-	return swap;
+	return swap8(data);
 }
 
 /**
diff --git a/src/event-parse-local.h b/src/event-parse-local.h
index d9e9faf649d1..cb2154547069 100644
--- a/src/event-parse-local.h
+++ b/src/event-parse-local.h
@@ -116,6 +116,10 @@ unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data);
 unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data);
 unsigned long long tep_data2host8(struct tep_handle *tep, unsigned long long data);
 
+unsigned short tep_swapbytes2(struct tep_handle *tep, unsigned short data);
+unsigned int tep_swapbytes4(struct tep_handle *tep, unsigned int data);
+unsigned long long tep_swapbytes8(struct tep_handle *tep, unsigned long long data);
+
 /* access to the internal parser */
 int tep_peek_char(struct tep_handle *tep);
 void tep_init_input_buf(struct tep_handle *tep, const char *buf, unsigned long long size);


-- Steve