net: tcp_drop adds `reason` and SNMP parameters for tracing v4
Subsystems (4)
PATCH 1/1 net: tcp_drop adds `reason` and SNMP parameters for tracing v4
I used the suggestion from Brendan Gregg. In addition to the reason parameter there is also the field parameter pointing to SNMP to distinguish the tcp_drop cause. I know what I submitted is not accurate, so I am submitting the current patch to get comments and criticism from everyone so that I can submit better code and solutions.And of course to make me more familiar and understand the linux kernel network code. Thank you everyone!
--- 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
@@ -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
Quoted 8 lines · Zhongya Yan
I used the suggestion from
Brendan Gregg. In addition to thereasonparameter there is also thefieldparameter pointing toSNMPto distinguish thetcp_dropcause. I know what I submitted is not accurate, so I am submitting the current patch to get comments and criticism from everyone so that I can submit better code and solutions.And of course to make me more familiar and understand thelinuxkernel network code.
Any reason why only limit this to TCP? I am pretty sure we are interested in packet drops across the entire stack.
You can take a look at net/core/drop_monitor.c, it actually has a big advantage over your solution, which is sending entire dropped packets to user-space for inspection.
If you can have a solution for all packet drops, not just TCP, it would make your patch much more useful. Therefore, I'd suggest you to consider extending the drop monitor.
Thanks.
A message that is not in the archive (sent off-list or not yet received): CALcyL7h=vS_Nmg4GfRe-SD9ZSZopCM5p6U1mojn1gncw1Nds6g@mail.gmail.com
Quoted 2 lines · Zhongya Yan
Thanks for the tip!
See reason:https://github.com/iovisor/bcc/issues/3533
This link only explains why TCP needs it, my question is why not do it for other protocols too, therefore neither this link nor you answers it.
BTW, net/core/drop_monitor.c is based on kfree_skb() tracepoint too, in case you are only interested in tracepoints not other things.
Thanks.
Steven Rostedt
maintainerQuoted 19 lines · On Fri, 3 Sep 2021 23:40:44 -0700 Zhongya Yan <[email protected]> wrote:
net/ipv4/tcp_input.c +2
-2@@ -4708,7 +4710,7 @@ static void tcp_ofo_queue(struct sock *sk)rb_erase (&skb->rbnode, &tp->out_of_order_queue);if (unlikely (!after (TCP_SKB_CB (skb)->end_seq, tp->rcv_nxt))) { -tcp_drop (sk, skb); +tcp_drop (sk, skb,LINUX_MIB_TCPOFOQUEUE ,"Tcp queue error" );continue ; }@@ -4764,7 +4766,7 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)if (unlikely (tcp_try_rmem_schedule (sk, skb, skb->truesize))) {NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPOFODROP ); sk->sk_data_ready (sk); -tcp_drop (sk, skb); +tcp_drop (sk, skb,LINUX_MIB_TCPOFODROP ,"Tcp rmem failed" );return ; }
net/ipv4/tcp_input.c static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb) · line 4832
Quoted 5 lines
net/ipv4/tcp_input.c +1
-1@@ -4827,7 +4829,7 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)/* All the bits are present. Drop. */ NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPOFOMERGE ); -tcp_drop (sk, skb); +tcp_drop (sk, skb,LINUX_MIB_TCPOFOMERGE ,"Tcp bits are present" );
Just curious. Is "Tcp" the normal way to write "TCP" in the kernel? I see it in snmp_seq_show_tcp_udp() in net/ipv4/proc.c, but no where else (besides doing CamelCase words). Should these be written using "TCP" instead of "Tcp". It just looks awkward to me.
But hey, I'm not one of the networking folks, so what do I know?
-- Steve
Quoted 5 lines · Steven Rostedt
Just curious. Is "Tcp" the normal way to write "TCP" in the kernel? I see it in
snmp_seq_show_tcp_udp()innet/ipv4/proc.c, but no where else (besides doing CamelCase words). Should these be written using "TCP" instead of "Tcp". It just looks awkward to me.
Yes, we prefer tcp or TCP, although SNMP counters have Tcp in them, for some reason
( grep Tcp /proc/net/netstat /proc/net/snmp ), that can not be changed.