1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
4 #include "bpf_tracing_net.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_endian.h>
8 char _license[] SEC("license") = "GPL";
10 static int hlist_unhashed_lockless(const struct hlist_node *h)
15 static int timer_pending(const struct timer_list * timer)
17 return !hlist_unhashed_lockless(&timer->entry);
20 extern unsigned CONFIG_HZ __kconfig;
23 #define NSEC_PER_SEC 1000000000ULL
24 static clock_t jiffies_to_clock_t(unsigned long x)
26 /* The implementation here tailored to a particular
29 u64 tick_nsec = (NSEC_PER_SEC + CONFIG_HZ/2) / CONFIG_HZ;
30 u64 user_hz_nsec = NSEC_PER_SEC / USER_HZ;
32 if ((tick_nsec % user_hz_nsec) == 0) {
33 if (CONFIG_HZ < USER_HZ)
34 return x * (USER_HZ / CONFIG_HZ);
36 return x / (CONFIG_HZ / USER_HZ);
38 return x * tick_nsec/user_hz_nsec;
41 static clock_t jiffies_delta_to_clock_t(long delta)
46 return jiffies_to_clock_t(delta);
49 static long sock_i_ino(const struct sock *sk)
51 const struct socket *sk_socket = sk->sk_socket;
52 const struct inode *inode;
58 inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
59 bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
64 inet_csk_in_pingpong_mode(const struct inet_connection_sock *icsk)
66 return icsk->icsk_ack.pingpong >= TCP_PINGPONG_THRESH;
69 static bool tcp_in_initial_slowstart(const struct tcp_sock *tcp)
71 return tcp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
74 static int dump_tcp_sock(struct seq_file *seq, struct tcp_sock *tp,
75 uid_t uid, __u32 seq_num)
77 const struct inet_connection_sock *icsk;
78 const struct fastopen_queue *fastopenq;
79 const struct inet_sock *inet;
80 unsigned long timer_expires;
81 const struct sock *sp;
88 icsk = &tp->inet_conn;
89 inet = &icsk->icsk_inet;
91 fastopenq = &icsk->icsk_accept_queue.fastopenq;
93 dest = inet->inet_daddr;
94 src = inet->inet_rcv_saddr;
95 destp = bpf_ntohs(inet->inet_dport);
96 srcp = bpf_ntohs(inet->inet_sport);
98 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
99 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
100 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
102 timer_expires = icsk->icsk_timeout;
103 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
105 timer_expires = icsk->icsk_timeout;
106 } else if (timer_pending(&sp->sk_timer)) {
108 timer_expires = sp->sk_timer.expires;
111 timer_expires = bpf_jiffies64();
114 state = sp->sk_state;
115 if (state == TCP_LISTEN) {
116 rx_queue = sp->sk_ack_backlog;
118 rx_queue = tp->rcv_nxt - tp->copied_seq;
123 BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
124 seq_num, src, srcp, dest, destp);
125 BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d ",
127 tp->write_seq - tp->snd_una, rx_queue,
129 jiffies_delta_to_clock_t(timer_expires - bpf_jiffies64()),
130 icsk->icsk_retransmits, uid,
131 icsk->icsk_probes_out,
133 sp->sk_refcnt.refs.counter);
134 BPF_SEQ_PRINTF(seq, "%pK %lu %lu %u %u %d\n",
136 jiffies_to_clock_t(icsk->icsk_rto),
137 jiffies_to_clock_t(icsk->icsk_ack.ato),
138 (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(icsk),
140 state == TCP_LISTEN ? fastopenq->max_qlen
141 : (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)
147 static int dump_tw_sock(struct seq_file *seq, struct tcp_timewait_sock *ttw,
148 uid_t uid, __u32 seq_num)
150 struct inet_timewait_sock *tw = &ttw->tw_sk;
155 delta = tw->tw_timer.expires - bpf_jiffies64();
157 src = tw->tw_rcv_saddr;
158 destp = bpf_ntohs(tw->tw_dport);
159 srcp = bpf_ntohs(tw->tw_sport);
161 BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
162 seq_num, src, srcp, dest, destp);
164 BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
165 tw->tw_substate, 0, 0,
166 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
167 tw->tw_refcnt.refs.counter, tw);
172 static int dump_req_sock(struct seq_file *seq, struct tcp_request_sock *treq,
173 uid_t uid, __u32 seq_num)
175 struct inet_request_sock *irsk = &treq->req;
176 struct request_sock *req = &irsk->req;
179 ttd = req->rsk_timer.expires - bpf_jiffies64();
184 BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
185 seq_num, irsk->ir_loc_addr,
186 irsk->ir_num, irsk->ir_rmt_addr,
187 bpf_ntohs(irsk->ir_rmt_port));
188 BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
189 TCP_SYN_RECV, 0, 0, 1, jiffies_to_clock_t(ttd),
190 req->num_timeout, uid, 0, 0, 0, req);
196 int dump_tcp4(struct bpf_iter__tcp *ctx)
198 struct sock_common *sk_common = ctx->sk_common;
199 struct seq_file *seq = ctx->meta->seq;
200 struct tcp_timewait_sock *tw;
201 struct tcp_request_sock *req;
203 uid_t uid = ctx->uid;
206 if (sk_common == (void *)0)
209 seq_num = ctx->meta->seq_num;
211 BPF_SEQ_PRINTF(seq, " sl "
214 "st tx_queue rx_queue tr tm->when retrnsmt"
215 " uid timeout inode\n");
217 if (sk_common->skc_family != AF_INET)
220 tp = bpf_skc_to_tcp_sock(sk_common);
222 return dump_tcp_sock(seq, tp, uid, seq_num);
224 tw = bpf_skc_to_tcp_timewait_sock(sk_common);
226 return dump_tw_sock(seq, tw, uid, seq_num);
228 req = bpf_skc_to_tcp_request_sock(sk_common);
230 return dump_req_sock(seq, req, uid, seq_num);