1ace6d6ecSYonghong Song // SPDX-License-Identifier: GPL-2.0
2ace6d6ecSYonghong Song /* Copyright (c) 2020 Facebook */
3ace6d6ecSYonghong Song #include "bpf_iter.h"
4ace6d6ecSYonghong Song #include "bpf_tracing_net.h"
5ace6d6ecSYonghong Song #include <bpf/bpf_helpers.h>
6ace6d6ecSYonghong Song #include <bpf/bpf_endian.h>
7ace6d6ecSYonghong Song 
8ace6d6ecSYonghong Song char _license[] SEC("license") = "GPL";
9ace6d6ecSYonghong Song 
sock_i_ino(const struct sock * sk)10ace6d6ecSYonghong Song static long sock_i_ino(const struct sock *sk)
11ace6d6ecSYonghong Song {
12ace6d6ecSYonghong Song 	const struct socket *sk_socket = sk->sk_socket;
13ace6d6ecSYonghong Song 	const struct inode *inode;
14ace6d6ecSYonghong Song 	unsigned long ino;
15ace6d6ecSYonghong Song 
16ace6d6ecSYonghong Song 	if (!sk_socket)
17ace6d6ecSYonghong Song 		return 0;
18ace6d6ecSYonghong Song 
19ace6d6ecSYonghong Song 	inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
20e4d9c232SIlya Leoshkevich 	bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
21ace6d6ecSYonghong Song 	return ino;
22ace6d6ecSYonghong Song }
23ace6d6ecSYonghong Song 
24ace6d6ecSYonghong Song SEC("iter/udp")
dump_udp4(struct bpf_iter__udp * ctx)25ace6d6ecSYonghong Song int dump_udp4(struct bpf_iter__udp *ctx)
26ace6d6ecSYonghong Song {
27ace6d6ecSYonghong Song 	struct seq_file *seq = ctx->meta->seq;
28ace6d6ecSYonghong Song 	struct udp_sock *udp_sk = ctx->udp_sk;
29ace6d6ecSYonghong Song 	struct inet_sock *inet;
30ace6d6ecSYonghong Song 	__u16 srcp, destp;
31ace6d6ecSYonghong Song 	__be32 dest, src;
32ace6d6ecSYonghong Song 	__u32 seq_num;
33ace6d6ecSYonghong Song 	int rqueue;
34ace6d6ecSYonghong Song 
35ace6d6ecSYonghong Song 	if (udp_sk == (void *)0)
36ace6d6ecSYonghong Song 		return 0;
37ace6d6ecSYonghong Song 
38ace6d6ecSYonghong Song 	seq_num = ctx->meta->seq_num;
39ace6d6ecSYonghong Song 	if (seq_num == 0)
40ace6d6ecSYonghong Song 		BPF_SEQ_PRINTF(seq,
41ace6d6ecSYonghong Song 			       "  sl  local_address rem_address   st tx_queue "
42ace6d6ecSYonghong Song 			       "rx_queue tr tm->when retrnsmt   uid  timeout "
43ace6d6ecSYonghong Song 			       "inode ref pointer drops\n");
44ace6d6ecSYonghong Song 
45ace6d6ecSYonghong Song 	/* filter out udp6 sockets */
46ace6d6ecSYonghong Song 	inet = &udp_sk->inet;
47ace6d6ecSYonghong Song 	if (inet->sk.sk_family == AF_INET6)
48ace6d6ecSYonghong Song 		return 0;
49ace6d6ecSYonghong Song 
50ace6d6ecSYonghong Song 	inet = &udp_sk->inet;
51ace6d6ecSYonghong Song 	dest = inet->inet_daddr;
52ace6d6ecSYonghong Song 	src = inet->inet_rcv_saddr;
53ace6d6ecSYonghong Song 	srcp = bpf_ntohs(inet->inet_sport);
54ace6d6ecSYonghong Song 	destp = bpf_ntohs(inet->inet_dport);
55ace6d6ecSYonghong Song 	rqueue = inet->sk.sk_rmem_alloc.counter - udp_sk->forward_deficit;
56ace6d6ecSYonghong Song 
57ace6d6ecSYonghong Song 	BPF_SEQ_PRINTF(seq, "%5d: %08X:%04X %08X:%04X ",
58ace6d6ecSYonghong Song 		       ctx->bucket, src, srcp, dest, destp);
59ace6d6ecSYonghong Song 
60ace6d6ecSYonghong Song 	BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
61ace6d6ecSYonghong Song 		       inet->sk.sk_state,
62ace6d6ecSYonghong Song 		       inet->sk.sk_wmem_alloc.refs.counter - 1,
63ace6d6ecSYonghong Song 		       rqueue,
64ace6d6ecSYonghong Song 		       0, 0L, 0, ctx->uid, 0,
65ace6d6ecSYonghong Song 		       sock_i_ino(&inet->sk),
66ace6d6ecSYonghong Song 		       inet->sk.sk_refcnt.refs.counter, udp_sk,
67ace6d6ecSYonghong Song 		       inet->sk.sk_drops.counter);
68ace6d6ecSYonghong Song 
69ace6d6ecSYonghong Song 	return 0;
70ace6d6ecSYonghong Song }
71