Skip to contentSkip to conversation

tcp: tcp_drop adds `SNMP` and `reason` parameter for tracing

PATCH In review Zhongya Yan 3 participants 5 messages Sep 14, 2021 – Sep 20, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: tcp_drop adds `SNMP` and `reason` parameter for tracing

When we wanted to trace the use of the tcp_drop(struct sock *sk, struct sk_buff *skb) function, we didn't know why tcp was deleting skb'. To solve this problem, I updated the function tcp_drop(struct sock *sk, struct sk_buff *skb, int field, const char *reason)`. This way you can understand the reason for the deletion based on the prompt message.
field: represents the SNMP-related value
reason: represents the reason why tcp deleted the current skb, and contains some hints.
Of course, if you want to know more about the reason for updating the current function, you can check: https://www.brendangregg.com/blog/2018-05-31/linux-tcpdrop.html

---
 include/trace/events/tcp.h |  62 ++++++++++++++++++
 net/ipv4/tcp_input.c       | 126 +++++++++++++++++++++++--------------
 2 files changed, 142 insertions(+), 46 deletions(-)
include/trace/events/tcp.h +62 -0
index 521059d8dc0a..68bbe8741ce8 100644
@@ -371,6 +371,68 @@ DEFINE_EVENT(tcp_event_skb, tcp_bad_csum,
 	TP_ARGS(skb)
 );
 
+TRACE_EVENT(tcp_drop,
+		TP_PROTO(struct sock *sk, struct sk_buff *skb, int field, const char *reason),
+
+		TP_ARGS(sk, skb, field, reason),
+
+		TP_STRUCT__entry(
+			__array(__u8, saddr, sizeof(struct sockaddr_in6))
+			__array(__u8, daddr, sizeof(struct sockaddr_in6))
+			__field(__u16, sport)
+			__field(__u16, dport)
+			__field(__u32, mark)
+			__field(__u16, data_len)
+			__field(__u32, snd_nxt)
+			__field(__u32, snd_una)
+			__field(__u32, snd_cwnd)
+			__field(__u32, ssthresh)
+			__field(__u32, snd_wnd)
+			__field(__u32, srtt)
+			__field(__u32, rcv_wnd)
+			__field(__u64, sock_cookie)
+			__field(int, field)
+			__string(reason, reason)
+			),
+
+		TP_fast_assign(
+				const struct tcphdr *th = (const struct tcphdr *)skb->data;
+				const struct inet_sock *inet = inet_sk(sk);
+				const struct tcp_sock *tp = tcp_sk(sk);
+
+				memset(__entry->saddr, 0, sizeof(struct sockaddr_in6));
+				memset(__entry->daddr, 0, sizeof(struct sockaddr_in6));
+
+				TP_STORE_ADDR_PORTS(__entry, inet, sk);
+
+				__entry->sport = ntohs(inet->inet_sport);
+				__entry->dport = ntohs(inet->inet_dport);
+				__entry->mark = skb->mark;
+
+				__entry->data_len = skb->len - __tcp_hdrlen(th);
+				__entry->snd_nxt = tp->snd_nxt;
+				__entry->snd_una = tp->snd_una;
+				__entry->snd_cwnd = tp->snd_cwnd;
+				__entry->snd_wnd = tp->snd_wnd;
+				__entry->rcv_wnd = tp->rcv_wnd;
+				__entry->ssthresh = tcp_current_ssthresh(sk);
+				__entry->srtt = tp->srtt_us >> 3;
+				__entry->sock_cookie = sock_gen_cookie(sk);
+				__entry->field = field;
+
+				__assign_str(reason, reason);
+		),
+
+		TP_printk("src=%pISpc dest=%pISpc mark=%#x data_len=%d snd_nxt=%#x snd_una=%#x \
+				snd_cwnd=%u ssthresh=%u snd_wnd=%u srtt=%u rcv_wnd=%u \
+				sock_cookie=%llx field=%d reason=%s",
+				__entry->saddr, __entry->daddr, __entry->mark,
+				__entry->data_len, __entry->snd_nxt, __entry->snd_una,
+				__entry->snd_cwnd, __entry->ssthresh, __entry->snd_wnd,
+				__entry->srtt, __entry->rcv_wnd, __entry->sock_cookie,
+				__entry->field, __get_str(reason))
+);
+
 #endif /* _TRACE_TCP_H */
 
 /* This part must be outside protection */
net/ipv4/tcp_input.c +80 -46

325 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Zhongya Yan

When we wanted to trace the use of the tcp_drop(struct sock *sk, struct sk_buff *skb) function, we didn't know why tcp was deleting skb'. To solve this problem, I updated the function tcp_drop(struct sock *sk, struct sk_buff *skb, int field, const char *reason)`. This way you can understand the reason for the deletion based on the prompt message.
field: represents the SNMP-related value
reason: represents the reason why tcp deleted the current skb, and contains some hints.
Of course, if you want to know more about the reason for updating the current function, you can check: https://www.brendangregg.com/blog/2018-05-31/linux-tcpdrop.html

I think you fail to explain why only TCP needs it? This should be useful for all kinds of drops, not just TCP, therefore you should consider extending net/core/drop_monitor.c instead of just tcp_drop().

Also, kernel does not have to explain it in strings, those SNMP counters are already available for user-space, so kernel could just use SNMP enums and let user-space interpret them. In many cases, you are just adding strings for those SNMP enums.

Thanks.

maintainer
#2 lore raw
Quoted 4 lines · On Mon, 20 Sep 2021 09:54:02 -0700 Cong Wang <[email protected]> wrote:

Also, kernel does not have to explain it in strings, those SNMP counters are already available for user-space, so kernel could just use SNMP enums and let user-space interpret them. In many cases, you are just adding strings for those SNMP enums.

The strings were requested by the networking maintainers.

https://lore.kernel.org/all/CANn89iJO8jzjFWvJ610TPmKDE8WKi8ojTr_HWXLz5g=4pdQHEA@mail.gmail.com/

-- Steve

#3 lore raw
Quoted 4 lines · Steven Rostedt

On Mon, 20 Sep 2021 09:54:02 -0700
Cong Wang <[email protected]> wrote:

Quoted 8 lines
Quoted 4 lines

Also, kernel does not have to explain it in strings, those SNMP counters are already available for user-space, so kernel could just use SNMP enums and let user-space interpret them. In many cases, you are just adding strings for those SNMP enums.

The strings were requested by the networking maintainers.

https://lore.kernel.org/all/CANn89iJO8jzjFWvJ610TPmKDE8WKi8ojTr_HWXLz5g=4pdQHEA@mail.gmail.com/

I think you misunderstand my point. Eric's point is hex address vs. string, which I never disagree. With SNMP enum, user-space can easily interpret it to string too, so at the end you still get strings but not from kernel. This would at least save a handful of strings from vmlinux, especially if we expand it beyond TCP.

Thanks.

maintainer
#4 lore raw
Quoted 9 lines · On Mon, 20 Sep 2021 10:20:33 -0700 Cong Wang <[email protected]> wrote:
Quoted 3 lines

The strings were requested by the networking maintainers.

https://lore.kernel.org/all/CANn89iJO8jzjFWvJ610TPmKDE8WKi8ojTr_HWXLz5g=4pdQHEA@mail.gmail.com/

I think you misunderstand my point. Eric's point is hex address vs. string, which I never disagree. With SNMP enum, user-space can easily interpret it to string too, so at the end you still get strings but not from kernel. This would at least save a handful of strings from vmlinux, especially if we expand it beyond TCP.

If an enum is saved in the ring buffer, it is also trivial to have it show a string in the buffer, as there's a way to map enums to strings on output as well. Which I discussed in that same thread.

-- Steve