Received: by 10.223.164.202 with SMTP id h10csp520326wrb; Wed, 8 Nov 2017 22:02:34 -0800 (PST) X-Google-Smtp-Source: ABhQp+R7QaNhauYijhY3xP7IxRsNc53Q9a1WTpENyXVjOUEKPvxRwcj+g6Gi3XhIMn3HGpHRhY4Z X-Received: by 10.99.97.200 with SMTP id v191mr593258pgb.300.1510207354701; Wed, 08 Nov 2017 22:02:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510207354; cv=none; d=google.com; s=arc-20160816; b=uxDvjTDC+YfTuJ3gZq1Qk9zqvM1sc/kWWeD7lKmweckCLucYE1Yo/QnjYf4VUHKskT SU8vIqRCKj+4IsAwQQ5gHwWUjAb8m97yxF03SDQCtEVDhYIwmbfsjeq466PRcWJ1DN1U zSISRC2CIizHCH8QeBHndchDPkB6mBL9BVrbChmU6qFF1kN9KT4Tfu2/N0ojWFYV8bqv N+UtUxjqkwWY1SsKod3rCkDfYsILiMqTofseahRSfk5+DVk7Xg/akCinC632T7FN6Y6m F+TbL8aYQ7cLjxDKWYBuLbBXfExHYOmbhWrosE95LN5YScy3W3nYecYPIurtxkjswDaK ThLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=R7CC2iEDL4j9u+DvpYX5Nv7koQZPPor4KGhd9GxgnEU=; b=phyw2NA1U+uG7g6lXvUeZUmu17CJq5VDjBwMMgT7YHVbVdPXrzTdIkYuo/GoPcLvP3 iwPp1UVxj02pNN/g26uCGP7JgoHoyIdC8uAr02+Ar58IRXDUTrGmWxpA0e1olzSRkSFd 1Rot3OdcuD+NOdb/dlvzlW7DwnOKosMkp/IDG7ZzDJ3lrVhltUUvDqmKav6Bkih/N5Pa JCJgKz72diYbQdh0Uoxdcc030ItTPCiBsk0XTfzPOtmuFam17UJEztBqI1hQ520KhrBx auRqLodi8phJcMYOmlTiHBPOeyn0S3vkXXP3/Z0dPnX8yAI0T+0zBkn1t5HCQYmxqVdl wAiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=J+enToQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m3si5366842pld.27.2017.11.08.22.02.22; Wed, 08 Nov 2017 22:02:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=J+enToQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751348AbdKIGBq (ORCPT + 82 others); Thu, 9 Nov 2017 01:01:46 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:53784 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750837AbdKIGBo (ORCPT ); Thu, 9 Nov 2017 01:01:44 -0500 Received: by mail-pg0-f67.google.com with SMTP id s2so3889653pge.10; Wed, 08 Nov 2017 22:01:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=R7CC2iEDL4j9u+DvpYX5Nv7koQZPPor4KGhd9GxgnEU=; b=J+enToQetQcUckeXdfmLya0f2aqkBScJVmcuqtX+YV8eqoJTjAskQ33nwRY67HxmlD Cl+G/jaq3C/8Fj47RIp4Wss+VEG8L3xRteBf/pS/SmSN7LvFfwg/T48szkIeHPN/1MiS l18kxgUycFbYaoM/MeApKW1M6/3v5aC7u0wNOhcE48b9HISWMf6fhcXRpp6c7KotxXzb iXWl7UBSEVJpDOGv630xV6grllhhvfc2qONAtJ+cYQOj7SB1cKCBZPkjj57inoTcqaCw u1jPnxqjx+0CmM3zN5Bbfw2t0MucLpGH5Fe6E+PZeoSZ/z7cnICIFJ0fFVptXNM7umd1 lDnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=R7CC2iEDL4j9u+DvpYX5Nv7koQZPPor4KGhd9GxgnEU=; b=c+TjB5+n1mdpzv0Yy7QkWigGvGKFDzmzqRqwRgi7Kcfpon0R8op70kYnZ9yq8O5faH CIoHHHeebpL+5xktm4f/ck+ksCBQ8o2YVGxd9Z+rdrPIotJThLzKpyHXflIyssDT79qw QntPcc7w6XgV/HCUtZGnYTzc3HLKBIcHX0tYwVLRnr1KkaC4clQuc0eG7ckTMljhkUuy +oAfSW3bpgOJOEzlYWPxTRPzuCCY0GoroG/wtThfXumSuGNoEJ0SZ9U/uTvi8JsTGWgj /U9Eq7kFclmgqC/re6Yfd1ZdszY6Jv6fFR+UsUVGlK+QvksnC/cPaLa1dDTpTo8J21oH 6sGg== X-Gm-Message-State: AJaThX6sfOljLcsXUKCLG3Ss2YIZBK27aaoBn2uPWVddeeij+JdUXFGM wLxp6F7aXxtZQeudLRYZhWE= X-Received: by 10.99.175.7 with SMTP id w7mr2904069pge.11.1510207304041; Wed, 08 Nov 2017 22:01:44 -0800 (PST) Received: from localhost.localdomain.localdomain ([122.115.74.182]) by smtp.gmail.com with ESMTPSA id w9sm11156665pfk.16.2017.11.08.22.01.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Nov 2017 22:01:43 -0800 (PST) From: Yafang Shao To: davem@davemloft.net Cc: kuznet@ms2.inr.ac.ru, yoshfuji@linux-ipv6.org, rostedt@goodmis.org, mingo@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH] net/tcp: introduce TRACE_EVENT for TCP/IPv4 state transition Date: Thu, 9 Nov 2017 14:01:38 +0800 Message-Id: <1510207298-14828-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With this newly introduced TRACE_EVENT, it will be very easy to minotor TCP/IPv4 state transition. A new TRACE_SYSTEM named tcp is added, in which we can trace other TCP event as well. Two helpers are added, static inline void __tcp_set_state(struct sock *sk, int state) static inline void __sk_state_store(struct sock *sk, int newstate) When do TCP/IPv4 state transition, we should use these two helpers or use tcp_set_state() instead of assign a value to sk_state directly. Signed-off-by: Yafang Shao --- include/net/tcp.h | 16 ++++++++++++ include/trace/events/tcp.h | 58 +++++++++++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 9 ++++--- net/ipv4/inet_hashtables.c | 2 +- net/ipv4/tcp.c | 2 +- 5 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 include/trace/events/tcp.h diff --git a/include/net/tcp.h b/include/net/tcp.h index 89974c5..a8336d3 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -49,6 +49,7 @@ #include #include #include +#include extern struct inet_hashinfo tcp_hashinfo; @@ -1284,6 +1285,21 @@ static inline bool tcp_checksum_complete(struct sk_buff *skb) #endif void tcp_set_state(struct sock *sk, int state); +/* + * To trace TCP state transition. + */ +static inline void __tcp_set_state(struct sock *sk, int state) +{ + trace_tcp_set_state(sk, sk->sk_state, state); + sk->sk_state = state; +} + +static inline void __sk_state_store(struct sock *sk, int newstate) +{ + trace_tcp_set_state(sk, sk->sk_state, newstate); + sk_state_store(sk, newstate); +} + void tcp_done(struct sock *sk); int tcp_abort(struct sock *sk, int err); diff --git a/include/trace/events/tcp.h b/include/trace/events/tcp.h new file mode 100644 index 0000000..abf65af --- /dev/null +++ b/include/trace/events/tcp.h @@ -0,0 +1,58 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM tcp + +#if !defined(_TRACE_TCP_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_TCP_H + +#include +#include +#include +#include +#include +#include + +TRACE_EVENT(tcp_set_state, + TP_PROTO(struct sock *sk, int oldstate, int newstate), + TP_ARGS(sk, oldstate, newstate), + + TP_STRUCT__entry( + __field(__be32, dst) + __field(__be32, src) + __field(__u16, dport) + __field(__u16, sport) + __field(int, oldstate) + __field(int, newstate) + ), + + TP_fast_assign( + if (oldstate == TCP_TIME_WAIT) { + __entry->dst = inet_twsk(sk)->tw_daddr; + __entry->src = inet_twsk(sk)->tw_rcv_saddr; + __entry->dport = ntohs(inet_twsk(sk)->tw_dport); + __entry->sport = ntohs(inet_twsk(sk)->tw_sport); + } else if (oldstate == TCP_NEW_SYN_RECV) { + __entry->dst = inet_rsk(inet_reqsk(sk))->ir_rmt_addr; + __entry->src = inet_rsk(inet_reqsk(sk))->ir_loc_addr; + __entry->dport = + ntohs(inet_rsk(inet_reqsk(sk))->ir_rmt_port); + __entry->sport = inet_rsk(inet_reqsk(sk))->ir_num; + } else { + __entry->dst = inet_sk(sk)->inet_daddr; + __entry->src = inet_sk(sk)->inet_rcv_saddr; + __entry->dport = ntohs(inet_sk(sk)->inet_dport); + __entry->sport = ntohs(inet_sk(sk)->inet_sport); + } + + __entry->oldstate = oldstate; + __entry->newstate = newstate; + ), + + TP_printk("%08X:%04X %08X:%04X, %02x %02x", + __entry->src, __entry->sport, __entry->dst, __entry->dport, + __entry->oldstate, __entry->newstate) +); + +#endif + +/* This part must be outside protection */ +#include diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index c039c93..307a046 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -27,6 +27,9 @@ #include #include +#define CREATE_TRACE_POINTS +#include + #ifdef INET_CSK_DEBUG const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n"; EXPORT_SYMBOL(inet_csk_timer_bug_msg); @@ -786,7 +789,7 @@ struct sock *inet_csk_clone_lock(const struct sock *sk, if (newsk) { struct inet_connection_sock *newicsk = inet_csk(newsk); - newsk->sk_state = TCP_SYN_RECV; + __tcp_set_state(newsk, TCP_SYN_RECV); newicsk->icsk_bind_hash = NULL; inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port; @@ -880,7 +883,7 @@ int inet_csk_listen_start(struct sock *sk, int backlog) * It is OK, because this socket enters to hash table only * after validation is complete. */ - sk_state_store(sk, TCP_LISTEN); + __sk_state_store(sk, TCP_LISTEN); if (!sk->sk_prot->get_port(sk, inet->inet_num)) { inet->inet_sport = htons(inet->inet_num); @@ -891,7 +894,7 @@ int inet_csk_listen_start(struct sock *sk, int backlog) return 0; } - sk->sk_state = TCP_CLOSE; + __tcp_set_state(sk, TCP_CLOSE); return err; } EXPORT_SYMBOL_GPL(inet_csk_listen_start); diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 597bb4c..0f45d456 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -430,7 +430,7 @@ bool inet_ehash_nolisten(struct sock *sk, struct sock *osk) sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); } else { percpu_counter_inc(sk->sk_prot->orphan_count); - sk->sk_state = TCP_CLOSE; + __tcp_set_state(sk, TCP_CLOSE); sock_set_flag(sk, SOCK_DEAD); inet_csk_destroy_sock(sk); } diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 5091402..984dce6 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2040,7 +2040,7 @@ void tcp_set_state(struct sock *sk, int state) /* Change state AFTER socket is unhashed to avoid closed * socket sitting in hash tables. */ - sk_state_store(sk, state); + __sk_state_store(sk, state); #ifdef STATE_TRACE SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]); -- 1.8.3.1 From 1583957002075467532@xxx Mon Nov 13 13:18:31 +0000 2017 X-GM-THRID: 1583955901072359269 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread