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
10ace6d6ecSYonghong Song #define IPV6_SEQ_DGRAM_HEADER \
11ace6d6ecSYonghong Song " sl " \
12ace6d6ecSYonghong Song "local_address " \
13ace6d6ecSYonghong Song "remote_address " \
14ace6d6ecSYonghong Song "st tx_queue rx_queue tr tm->when retrnsmt" \
15ace6d6ecSYonghong Song " uid timeout inode ref pointer drops\n"
16ace6d6ecSYonghong Song
sock_i_ino(const struct sock * sk)17ace6d6ecSYonghong Song static long sock_i_ino(const struct sock *sk)
18ace6d6ecSYonghong Song {
19ace6d6ecSYonghong Song const struct socket *sk_socket = sk->sk_socket;
20ace6d6ecSYonghong Song const struct inode *inode;
21ace6d6ecSYonghong Song unsigned long ino;
22ace6d6ecSYonghong Song
23ace6d6ecSYonghong Song if (!sk_socket)
24ace6d6ecSYonghong Song return 0;
25ace6d6ecSYonghong Song
26ace6d6ecSYonghong Song inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
27*e4d9c232SIlya Leoshkevich bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
28ace6d6ecSYonghong Song return ino;
29ace6d6ecSYonghong Song }
30ace6d6ecSYonghong Song
31ace6d6ecSYonghong Song SEC("iter/udp")
dump_udp6(struct bpf_iter__udp * ctx)32ace6d6ecSYonghong Song int dump_udp6(struct bpf_iter__udp *ctx)
33ace6d6ecSYonghong Song {
34ace6d6ecSYonghong Song struct seq_file *seq = ctx->meta->seq;
35ace6d6ecSYonghong Song struct udp_sock *udp_sk = ctx->udp_sk;
36ace6d6ecSYonghong Song const struct in6_addr *dest, *src;
37ace6d6ecSYonghong Song struct udp6_sock *udp6_sk;
38ace6d6ecSYonghong Song struct inet_sock *inet;
39ace6d6ecSYonghong Song __u16 srcp, destp;
40ace6d6ecSYonghong Song __u32 seq_num;
41ace6d6ecSYonghong Song int rqueue;
42ace6d6ecSYonghong Song
43ace6d6ecSYonghong Song if (udp_sk == (void *)0)
44ace6d6ecSYonghong Song return 0;
45ace6d6ecSYonghong Song
46ace6d6ecSYonghong Song seq_num = ctx->meta->seq_num;
47ace6d6ecSYonghong Song if (seq_num == 0)
48ace6d6ecSYonghong Song BPF_SEQ_PRINTF(seq, IPV6_SEQ_DGRAM_HEADER);
49ace6d6ecSYonghong Song
50ace6d6ecSYonghong Song udp6_sk = bpf_skc_to_udp6_sock(udp_sk);
51ace6d6ecSYonghong Song if (udp6_sk == (void *)0)
52ace6d6ecSYonghong Song return 0;
53ace6d6ecSYonghong Song
54ace6d6ecSYonghong Song inet = &udp_sk->inet;
55ace6d6ecSYonghong Song srcp = bpf_ntohs(inet->inet_sport);
56ace6d6ecSYonghong Song destp = bpf_ntohs(inet->inet_dport);
57ace6d6ecSYonghong Song rqueue = inet->sk.sk_rmem_alloc.counter - udp_sk->forward_deficit;
58ace6d6ecSYonghong Song dest = &inet->sk.sk_v6_daddr;
59ace6d6ecSYonghong Song src = &inet->sk.sk_v6_rcv_saddr;
60ace6d6ecSYonghong Song
61ace6d6ecSYonghong Song BPF_SEQ_PRINTF(seq, "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X ",
62ace6d6ecSYonghong Song ctx->bucket,
63ace6d6ecSYonghong Song src->s6_addr32[0], src->s6_addr32[1],
64ace6d6ecSYonghong Song src->s6_addr32[2], src->s6_addr32[3], srcp,
65ace6d6ecSYonghong Song dest->s6_addr32[0], dest->s6_addr32[1],
66ace6d6ecSYonghong Song dest->s6_addr32[2], dest->s6_addr32[3], destp);
67ace6d6ecSYonghong Song
68ace6d6ecSYonghong Song BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
69ace6d6ecSYonghong Song inet->sk.sk_state,
70ace6d6ecSYonghong Song inet->sk.sk_wmem_alloc.refs.counter - 1,
71ace6d6ecSYonghong Song rqueue,
72ace6d6ecSYonghong Song 0, 0L, 0, ctx->uid, 0,
73ace6d6ecSYonghong Song sock_i_ino(&inet->sk),
74ace6d6ecSYonghong Song inet->sk.sk_refcnt.refs.counter, udp_sk,
75ace6d6ecSYonghong Song inet->sk.sk_drops.counter);
76ace6d6ecSYonghong Song
77ace6d6ecSYonghong Song return 0;
78ace6d6ecSYonghong Song }
79