From patchwork Thu Dec 29 08:02:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 37322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2287497wrt; Thu, 29 Dec 2022 00:08:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXviKE7kxMJ6IuqOW9tBCPfzJqA67rSDBEcuuN9W6NjUCCr0S73EyUjj1JhAhlCyPgQ4X28X X-Received: by 2002:a17:902:bd42:b0:192:4d3e:a573 with SMTP id b2-20020a170902bd4200b001924d3ea573mr26071943plx.24.1672301282944; Thu, 29 Dec 2022 00:08:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672301282; cv=none; d=google.com; s=arc-20160816; b=GSWjU3bwgI3CxG7uFDKKhZIJqi/54i1GM855w1Gi1MCXlw7joMmbdLTOa+Rbi8Ks6D xfrIh+hIQ2sCK1cMyVVui/rjdmkFXiSVJyGO8GLMEljiBTXS5s6w5EbAdrKcLPxQez6H tRLhtHN4mTn3ut293GtDV43w4shuYlVahayevWd0mk67+/OMT5eNICFeA6+afFH0SvzJ MpQr0qHFrkXXHKZmcnAXjpqmusZi23ETz7tAqQrzqHSqazIKdKpRat5rUd0kUFv6uhHy r75ISuJa6ua5XuuSPIlGZRossSDz8SRbUOKDv82iH/22UcN2QE8s6W/+U0Apw4suIw1g Xh+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=YYn77ci8+oadBW76lYzJfKZ6fFNIjqBEwRX0Ww9TJ7s=; b=F6Q3QJwLBf0ZUhPXbf9k+6TDPaCmldwaONYIS+GipjaucUTQuLqrXRxAL68BiKgAcM nj9JqLbTcZDZaU0SnL36kJBnJz2doG+7v+H8JYKqoV/GJtfqux0jZG+nA+Ws4qAqHnX+ G+cU5qaW49KJlLpTDsaz7rayh8jwt1avD5ZlBOWdTi79rbN6zdhn/Nz4wauqZ7Tbpf/E ThHUsnILPetlDWO7f8vBXx+MiMbZupgc2y37zO19Tpo7WFNe//GqBpyMvJtywa8wWzTs dAk29r6VFggbXF/saCrUIQfSVCkG8OyfkYvN3l2QcbdRy5Uu7+uoAPzWpxo0kN2YDS0K +OPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Dn6En9W1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z2-20020a170902708200b00188fead2329si17294978plk.135.2022.12.29.00.07.50; Thu, 29 Dec 2022 00:08:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Dn6En9W1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbiL2ICz (ORCPT + 99 others); Thu, 29 Dec 2022 03:02:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229520AbiL2ICx (ORCPT ); Thu, 29 Dec 2022 03:02:53 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA17FD2DA for ; Thu, 29 Dec 2022 00:02:23 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id b145so8095394pfb.2 for ; Thu, 29 Dec 2022 00:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=YYn77ci8+oadBW76lYzJfKZ6fFNIjqBEwRX0Ww9TJ7s=; b=Dn6En9W1e8cvEiQoDDVWTZzEhI8jZ1ErnKGxbi6HBk4DW/AKMU1+gcswtxwjrru8kf 8B/8zwi25Cr+1OxPpx/7zMQjEiw5ewfmX8xAhAo9u4gxjj/NLtDGgk+YprKWbhlCmeWW AAxgE6cdm36llA/q9WPCKI60l9jGJEWmmbk3TpkHmvrWSyMB6OkkE8MvTG+6RNIu+oU0 R/gm9aBaQOxiJmdN70/jpopv7gVx6htBToGrpLSOyTfQIac1gBCYsYazgZ9Zw+uTsAMP MIF4VqjN4rr/1GTHPuC1lvhf5OdDFDjVM7chHc5oVlqaM8tc8pbn3lMGIIK3DC72THig T4rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YYn77ci8+oadBW76lYzJfKZ6fFNIjqBEwRX0Ww9TJ7s=; b=6fPyw9PUR0Xf5TV81oNBVW1uXDjPEs9uwdtA7+KPxl+kzy+t9R4tXrzfVEvDaDHID9 OmxMuiYqCikTNk/x8WfU+ZAW3vH6Icp0ghxRQIc7SfC9fHD71RaNH4ibBWeoPsjvD6iM CQsi+Z/fDDTfsgEFPsKmjZk8abcmvIoB1TYDHJZz+cQF6K614tDuN1HjpxhreGoMg9hj fScw36CEA0eldeoHrIMA4vl1RL+k3BSHfzm6qr2PMnF4JwrNPENyftxUK1tZjWv+k6p/ ape/EPH/5PN3S/Bn+HkBB8CHjXDLFv/RAdzRptAF9bQui4lpCPM5cYbzc7jXhqjzRyUw ieZA== X-Gm-Message-State: AFqh2kp6spA+9tOedSmM4pdkL9q0M6+XpgJktkmiBJ7FXrwIR6fb09Ab 0awbQx+DqGO1Bp5ASAVm/funlw== X-Received: by 2002:a62:648b:0:b0:57a:a199:93e7 with SMTP id y133-20020a62648b000000b0057aa19993e7mr28677722pfb.28.1672300943082; Thu, 29 Dec 2022 00:02:23 -0800 (PST) Received: from PF2E59YH-BKX.inc.bytedance.com ([139.177.225.249]) by smtp.gmail.com with ESMTPSA id k17-20020aa79d11000000b005764c8f8f15sm11485781pfp.73.2022.12.29.00.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Dec 2022 00:02:22 -0800 (PST) From: Yunhui Cui To: edumazet@google.com, rostedt@goodmis.org, mhiramat@kernel.org, davem@davemloft.net, yoshfuji@linux-ipv6.org, dsahern@kernel.org, kuba@kernel.org, pabeni@redhat.com, duanxiongchun@bytedance.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, cuiyunhui@bytedance.com Subject: [PATCH] tcp/udp: add tracepoint for send recv length Date: Thu, 29 Dec 2022 16:02:07 +0800 Message-Id: <20221229080207.1029-1-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.37.3.windows.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753534990350520720?= X-GMAIL-MSGID: =?utf-8?q?1753534990350520720?= From: Xiongchun Duan Add a tracepoint for capturing TCP segments with a send or receive length. This makes it easy to obtain the packet sending and receiving information of each process in the user mode, such as the netatop tool. Signed-off-by: Xiongchun Duan --- include/trace/events/tcp.h | 41 ++++++++++++++++++++++++++++++++++++++ include/trace/events/udp.h | 34 +++++++++++++++++++++++++++++++ net/ipv4/tcp.c | 7 +++++++ net/ipv4/udp.c | 11 ++++++++-- 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/include/trace/events/tcp.h b/include/trace/events/tcp.h index 901b440238d5..d9973c8508d1 100644 --- a/include/trace/events/tcp.h +++ b/include/trace/events/tcp.h @@ -187,6 +187,47 @@ DEFINE_EVENT(tcp_event_sk, tcp_rcv_space_adjust, TP_ARGS(sk) ); +/* + * tcp send/recv stream length + * + * Note: this class requires positive integer + */ +DECLARE_EVENT_CLASS(tcp_stream_length, + + TP_PROTO(struct sock *sk, int length, int error, int flags), + + TP_ARGS(sk, length, error, flags), + + TP_STRUCT__entry( + __field(void *, sk) + __field(int, length) + __field(int, error) + __field(int, flags) + ), + + TP_fast_assign( + __entry->sk = sk; + __entry->length = length; + __entry->error = error; + __entry->flags = flags; + ), + + TP_printk("sk address = %p, length = %d, error = %d flags = %u ", + __entry->sk, __entry->length, __entry->error, __entry->flags) +); + +DEFINE_EVENT(tcp_stream_length, tcp_send_length, + TP_PROTO(struct sock *sk, int length, int error, int flags), + + TP_ARGS(sk, length, error, flags) +); + +DEFINE_EVENT(tcp_stream_length, tcp_recv_length, + TP_PROTO(struct sock *sk, int length, int error, int flags), + + TP_ARGS(sk, length, error, flags) +); + TRACE_EVENT(tcp_retransmit_synack, TP_PROTO(const struct sock *sk, const struct request_sock *req), diff --git a/include/trace/events/udp.h b/include/trace/events/udp.h index 336fe272889f..22181c91c8e2 100644 --- a/include/trace/events/udp.h +++ b/include/trace/events/udp.h @@ -27,6 +27,40 @@ TRACE_EVENT(udp_fail_queue_rcv_skb, TP_printk("rc=%d port=%hu", __entry->rc, __entry->lport) ); +DECLARE_EVENT_CLASS(udp_stream_length, + + TP_PROTO(struct sock *sk, int length, int error, int flags), + + TP_ARGS(sk, length, error, flags), + + TP_STRUCT__entry( + __field(void *, sk) + __field(int, length) + __field(int, error) + __field(int, flags) + ), + + TP_fast_assign( + __entry->sk = sk; + __entry->length = length; + __entry->error = error; + __entry->flags = flags; + ), + + TP_printk("sk address = %p, length = %d, error=%d, flags = %u ", + __entry->sk, __entry->length, __entry->error, __entry->flags) +); + +DEFINE_EVENT(udp_stream_length, udp_send_length, + TP_PROTO(struct sock *sk, int length, int error, int flags), + TP_ARGS(sk, length, error, flags) +); + +DEFINE_EVENT(udp_stream_length, udp_recv_length, + TP_PROTO(struct sock *sk, int length, int error, int flags), + TP_ARGS(sk, length, error, flags) +); + #endif /* _TRACE_UDP_H */ /* This part must be outside protection */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index c567d5e8053e..5deb69e2d3e7 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -267,6 +267,7 @@ #include #include #include +#include #include #include @@ -1150,6 +1151,7 @@ int tcp_sendpage(struct sock *sk, struct page *page, int offset, lock_sock(sk); ret = tcp_sendpage_locked(sk, page, offset, size, flags); release_sock(sk); + trace_tcp_send_length(sk, ret > 0 ? ret : 0, ret > 0 ? 0 : ret, 0); return ret; } @@ -1482,6 +1484,7 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) lock_sock(sk); ret = tcp_sendmsg_locked(sk, msg, size); release_sock(sk); + trace_tcp_send_length(sk, ret > 0 ? ret : 0, ret > 0 ? 0 : ret, 0); return ret; } @@ -2647,6 +2650,10 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len, /* Clean up data we have read: This will do ACK frames. */ tcp_cleanup_rbuf(sk, copied); + trace_tcp_recv_length(sk, (copied > 0 && !(flags & MSG_PEEK)) ? + copied : 0, + (copied > 0 && + !(flags & MSG_PEEK)) ? 0 : copied, flags); return copied; out: diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 9592fe3e444a..1b336af4df6d 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1300,6 +1300,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) release_sock(sk); out: + trace_udp_send_length(sk, err == 0 ? len : 0, err, 0); ip_rt_put(rt); out_free: if (free) @@ -1364,8 +1365,10 @@ int udp_sendpage(struct sock *sk, struct page *page, int offset, page, offset, size, flags); if (ret == -EOPNOTSUPP) { release_sock(sk); - return sock_no_sendpage(sk->sk_socket, page, offset, - size, flags); + ret = sock_no_sendpage(sk->sk_socket, page, offset, + size, flags); + trace_udp_send_length(sk, ret > 0 ? ret : 0, ret > 0 ? 0 : ret, 0); + return ret; } if (ret < 0) { udp_flush_pending_frames(sk); @@ -1377,6 +1380,7 @@ int udp_sendpage(struct sock *sk, struct page *page, int offset, ret = udp_push_pending_frames(sk); if (!ret) ret = size; + trace_udp_send_length(sk, ret > 0 ? ret : 0, ret > 0 ? 0 : ret, 0); out: release_sock(sk); return ret; @@ -1935,6 +1939,9 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, if (flags & MSG_TRUNC) err = ulen; + trace_udp_recv_length(sk, (err > 0 && !peeking) ? err : 0, + (err > 0 && !peeking) ? 0 : err, flags); + skb_consume_udp(sk, skb, peeking ? -err : err); return err;