Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net/ipv4: add tracepoint for icmp_send

Introduce a tracepoint for icmp_send, which can help users to get more
detail information conveniently when icmp abnormal events happen.

1. Giving an usecase example:
=============================
When an application experiences packet loss due to an unreachable UDP
destination port, the kernel will send an exception message through the
icmp_send function. By adding a trace point for icmp_send, developers or
system administrators can obtain detailed information about the UDP
packet loss, including the type, code, source address, destination address,
source port, and destination port. This facilitates the trouble-shooting
of UDP packet loss issues especially for those network-service
applications.

2. Operation Instructions:
==========================
Switch to the tracing directory.
cd /sys/kernel/tracing
Filter for destination port unreachable.
echo "type==3 && code==3" > events/icmp/icmp_send/filter
Enable trace event.
echo 1 > events/icmp/icmp_send/enable

3. Result View:
================
udp_client_erro-11370 [002] ...s.12 124.728002:
icmp_send: icmp_send: type=3, code=3.
From 127.0.0.1:41895 to 127.0.0.1:6666 ulen=23
skbaddr=00000000589b167a

Signed-off-by: Peilin He <he.peilin@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Reviewed-by: Yunkai Zhang <zhang.yunkai@zte.com.cn>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Cc: Liu Chun <liu.chun2@zte.com.cn>
Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Peilin He and committed by
David S. Miller
db3efdcf 9f481cea

+71
+67
include/trace/events/icmp.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #undef TRACE_SYSTEM 3 + #define TRACE_SYSTEM icmp 4 + 5 + #if !defined(_TRACE_ICMP_H) || defined(TRACE_HEADER_MULTI_READ) 6 + #define _TRACE_ICMP_H 7 + 8 + #include <linux/icmp.h> 9 + #include <linux/tracepoint.h> 10 + 11 + TRACE_EVENT(icmp_send, 12 + 13 + TP_PROTO(const struct sk_buff *skb, int type, int code), 14 + 15 + TP_ARGS(skb, type, code), 16 + 17 + TP_STRUCT__entry( 18 + __field(const void *, skbaddr) 19 + __field(int, type) 20 + __field(int, code) 21 + __array(__u8, saddr, 4) 22 + __array(__u8, daddr, 4) 23 + __field(__u16, sport) 24 + __field(__u16, dport) 25 + __field(unsigned short, ulen) 26 + ), 27 + 28 + TP_fast_assign( 29 + struct iphdr *iph = ip_hdr(skb); 30 + struct udphdr *uh = udp_hdr(skb); 31 + int proto_4 = iph->protocol; 32 + __be32 *p32; 33 + 34 + __entry->skbaddr = skb; 35 + __entry->type = type; 36 + __entry->code = code; 37 + 38 + if (proto_4 != IPPROTO_UDP || (u8 *)uh < skb->head || 39 + (u8 *)uh + sizeof(struct udphdr) 40 + > skb_tail_pointer(skb)) { 41 + __entry->sport = 0; 42 + __entry->dport = 0; 43 + __entry->ulen = 0; 44 + } else { 45 + __entry->sport = ntohs(uh->source); 46 + __entry->dport = ntohs(uh->dest); 47 + __entry->ulen = ntohs(uh->len); 48 + } 49 + 50 + p32 = (__be32 *) __entry->saddr; 51 + *p32 = iph->saddr; 52 + 53 + p32 = (__be32 *) __entry->daddr; 54 + *p32 = iph->daddr; 55 + ), 56 + 57 + TP_printk("icmp_send: type=%d, code=%d. From %pI4:%u to %pI4:%u ulen=%d skbaddr=%p", 58 + __entry->type, __entry->code, 59 + __entry->saddr, __entry->sport, __entry->daddr, 60 + __entry->dport, __entry->ulen, __entry->skbaddr) 61 + ); 62 + 63 + #endif /* _TRACE_ICMP_H */ 64 + 65 + /* This part must be outside protection */ 66 + #include <trace/define_trace.h> 67 +
+4
net/ipv4/icmp.c
··· 93 93 #include <net/ip_fib.h> 94 94 #include <net/l3mdev.h> 95 95 #include <net/addrconf.h> 96 + #define CREATE_TRACE_POINTS 97 + #include <trace/events/icmp.h> 96 98 97 99 /* 98 100 * Build xmit assembly blocks ··· 771 769 */ 772 770 if (!fl4.saddr) 773 771 fl4.saddr = htonl(INADDR_DUMMY); 772 + 773 + trace_icmp_send(skb_in, type, code); 774 774 775 775 icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt); 776 776 ende: