[PATCH net-next 1/2] net: snmp: add tracepoint support for snmp

From: menglong8 . dong
Date: Thu Nov 11 2021 - 08:50:36 EST


From: Menglong Dong <imagedong@xxxxxxxxxxx>

snmp is the network package statistics module in kernel, and it is
useful in network issue diagnosis, such as packet drop.

However, it is hard to get the detail information about the packet.
For example, we can know that there is something wrong with the
checksum of udp packet though 'InCsumErrors' of UDP protocol in
/proc/net/snmp, but we can't figure out the ip and port of the packet
that this error is happening on.

Add tracepoint for snmp. Therefor, users can use some tools (such as
eBPF) to get the information of the exceptional packet.

Signed-off-by: Menglong Dong <imagedong@xxxxxxxxxxx>
---
include/trace/events/snmp.h | 45 +++++++++++++++++++++++++++++++++++++
net/core/net-traces.c | 1 +
2 files changed, 46 insertions(+)
create mode 100644 include/trace/events/snmp.h

diff --git a/include/trace/events/snmp.h b/include/trace/events/snmp.h
new file mode 100644
index 000000000000..9dbd630306dd
--- /dev/null
+++ b/include/trace/events/snmp.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM snmp
+
+#if !defined(_TRACE_SNMP_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_SNMP_H
+
+#include <linux/tracepoint.h>
+#include <linux/skbuff.h>
+#include <linux/snmp.h>
+
+DECLARE_EVENT_CLASS(snmp_template,
+
+ TP_PROTO(struct sk_buff *skb, int field, int val),
+
+ TP_ARGS(skb, field, val),
+
+ TP_STRUCT__entry(
+ __field(void *, skbaddr)
+ __field(int, field)
+ __field(int, val)
+ ),
+
+ TP_fast_assign(
+ __entry->skbaddr = skb;
+ __entry->field = field;
+ __entry->val = val;
+ ),
+
+ TP_printk("skbaddr=%p, field=%d, val=%d", __entry->skbaddr,
+ __entry->field, __entry->val)
+);
+
+#define DEFINE_SNMP_EVENT(proto) \
+DEFINE_EVENT(snmp_template, snmp_##proto, \
+ TP_PROTO(struct sk_buff *skb, int field, int val), \
+ TP_ARGS(skb, field, val) \
+)
+
+#define TRACE_SNMP(skb, proto, field, val) \
+ trace_snmp_##proto(skb, field, val)
+
+#endif
+
+#include <trace/define_trace.h>
diff --git a/net/core/net-traces.c b/net/core/net-traces.c
index c40cd8dd75c7..15ff40b83ca7 100644
--- a/net/core/net-traces.c
+++ b/net/core/net-traces.c
@@ -35,6 +35,7 @@
#include <trace/events/tcp.h>
#include <trace/events/fib.h>
#include <trace/events/qdisc.h>
+#include <trace/events/snmp.h>
#if IS_ENABLED(CONFIG_BRIDGE)
#include <trace/events/bridge.h>
EXPORT_TRACEPOINT_SYMBOL_GPL(br_fdb_add);
--
2.27.0