Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Facebook */
0003 #include "bpf_iter.h"
0004 #include "bpf_tracing_net.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_endian.h>
0007 
0008 char _license[] SEC("license") = "GPL";
0009 
0010 static int hlist_unhashed_lockless(const struct hlist_node *h)
0011 {
0012         return !(h->pprev);
0013 }
0014 
0015 static int timer_pending(const struct timer_list * timer)
0016 {
0017     return !hlist_unhashed_lockless(&timer->entry);
0018 }
0019 
0020 extern unsigned CONFIG_HZ __kconfig;
0021 
0022 #define USER_HZ     100
0023 #define NSEC_PER_SEC    1000000000ULL
0024 static clock_t jiffies_to_clock_t(unsigned long x)
0025 {
0026     /* The implementation here tailored to a particular
0027      * setting of USER_HZ.
0028      */
0029     u64 tick_nsec = (NSEC_PER_SEC + CONFIG_HZ/2) / CONFIG_HZ;
0030     u64 user_hz_nsec = NSEC_PER_SEC / USER_HZ;
0031 
0032     if ((tick_nsec % user_hz_nsec) == 0) {
0033         if (CONFIG_HZ < USER_HZ)
0034             return x * (USER_HZ / CONFIG_HZ);
0035         else
0036             return x / (CONFIG_HZ / USER_HZ);
0037     }
0038     return x * tick_nsec/user_hz_nsec;
0039 }
0040 
0041 static clock_t jiffies_delta_to_clock_t(long delta)
0042 {
0043     if (delta <= 0)
0044         return 0;
0045 
0046     return jiffies_to_clock_t(delta);
0047 }
0048 
0049 static long sock_i_ino(const struct sock *sk)
0050 {
0051     const struct socket *sk_socket = sk->sk_socket;
0052     const struct inode *inode;
0053     unsigned long ino;
0054 
0055     if (!sk_socket)
0056         return 0;
0057 
0058     inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
0059     bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
0060     return ino;
0061 }
0062 
0063 static bool
0064 inet_csk_in_pingpong_mode(const struct inet_connection_sock *icsk)
0065 {
0066     return icsk->icsk_ack.pingpong >= TCP_PINGPONG_THRESH;
0067 }
0068 
0069 static bool tcp_in_initial_slowstart(const struct tcp_sock *tcp)
0070 {
0071     return tcp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
0072 }
0073 
0074 static int dump_tcp_sock(struct seq_file *seq, struct tcp_sock *tp,
0075              uid_t uid, __u32 seq_num)
0076 {
0077     const struct inet_connection_sock *icsk;
0078     const struct fastopen_queue *fastopenq;
0079     const struct inet_sock *inet;
0080     unsigned long timer_expires;
0081     const struct sock *sp;
0082     __u16 destp, srcp;
0083     __be32 dest, src;
0084     int timer_active;
0085     int rx_queue;
0086     int state;
0087 
0088     icsk = &tp->inet_conn;
0089     inet = &icsk->icsk_inet;
0090     sp = &inet->sk;
0091     fastopenq = &icsk->icsk_accept_queue.fastopenq;
0092 
0093     dest = inet->inet_daddr;
0094     src = inet->inet_rcv_saddr;
0095     destp = bpf_ntohs(inet->inet_dport);
0096     srcp = bpf_ntohs(inet->inet_sport);
0097 
0098     if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
0099         icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
0100         icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
0101         timer_active = 1;
0102         timer_expires = icsk->icsk_timeout;
0103     } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
0104         timer_active = 4;
0105         timer_expires = icsk->icsk_timeout;
0106     } else if (timer_pending(&sp->sk_timer)) {
0107         timer_active = 2;
0108         timer_expires = sp->sk_timer.expires;
0109     } else {
0110         timer_active = 0;
0111         timer_expires = bpf_jiffies64();
0112     }
0113 
0114     state = sp->sk_state;
0115     if (state == TCP_LISTEN) {
0116         rx_queue = sp->sk_ack_backlog;
0117     } else {
0118         rx_queue = tp->rcv_nxt - tp->copied_seq;
0119         if (rx_queue < 0)
0120             rx_queue = 0;
0121     }
0122 
0123     BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
0124                seq_num, src, srcp, dest, destp);
0125     BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d ",
0126                state,
0127                tp->write_seq - tp->snd_una, rx_queue,
0128                timer_active,
0129                jiffies_delta_to_clock_t(timer_expires - bpf_jiffies64()),
0130                icsk->icsk_retransmits, uid,
0131                icsk->icsk_probes_out,
0132                sock_i_ino(sp),
0133                sp->sk_refcnt.refs.counter);
0134     BPF_SEQ_PRINTF(seq, "%pK %lu %lu %u %u %d\n",
0135                tp,
0136                jiffies_to_clock_t(icsk->icsk_rto),
0137                jiffies_to_clock_t(icsk->icsk_ack.ato),
0138                (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(icsk),
0139                tp->snd_cwnd,
0140                state == TCP_LISTEN ? fastopenq->max_qlen
0141                 : (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)
0142               );
0143 
0144     return 0;
0145 }
0146 
0147 static int dump_tw_sock(struct seq_file *seq, struct tcp_timewait_sock *ttw,
0148             uid_t uid, __u32 seq_num)
0149 {
0150     struct inet_timewait_sock *tw = &ttw->tw_sk;
0151     __u16 destp, srcp;
0152     __be32 dest, src;
0153     long delta;
0154 
0155     delta = tw->tw_timer.expires - bpf_jiffies64();
0156     dest = tw->tw_daddr;
0157     src  = tw->tw_rcv_saddr;
0158     destp = bpf_ntohs(tw->tw_dport);
0159     srcp  = bpf_ntohs(tw->tw_sport);
0160 
0161     BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
0162                seq_num, src, srcp, dest, destp);
0163 
0164     BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
0165                tw->tw_substate, 0, 0,
0166                3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
0167                tw->tw_refcnt.refs.counter, tw);
0168 
0169     return 0;
0170 }
0171 
0172 static int dump_req_sock(struct seq_file *seq, struct tcp_request_sock *treq,
0173              uid_t uid, __u32 seq_num)
0174 {
0175     struct inet_request_sock *irsk = &treq->req;
0176     struct request_sock *req = &irsk->req;
0177     long ttd;
0178 
0179     ttd = req->rsk_timer.expires - bpf_jiffies64();
0180 
0181     if (ttd < 0)
0182         ttd = 0;
0183 
0184     BPF_SEQ_PRINTF(seq, "%4d: %08X:%04X %08X:%04X ",
0185                seq_num, irsk->ir_loc_addr,
0186                irsk->ir_num, irsk->ir_rmt_addr,
0187                bpf_ntohs(irsk->ir_rmt_port));
0188     BPF_SEQ_PRINTF(seq, "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
0189                TCP_SYN_RECV, 0, 0, 1, jiffies_to_clock_t(ttd),
0190                req->num_timeout, uid, 0, 0, 0, req);
0191 
0192     return 0;
0193 }
0194 
0195 SEC("iter/tcp")
0196 int dump_tcp4(struct bpf_iter__tcp *ctx)
0197 {
0198     struct sock_common *sk_common = ctx->sk_common;
0199     struct seq_file *seq = ctx->meta->seq;
0200     struct tcp_timewait_sock *tw;
0201     struct tcp_request_sock *req;
0202     struct tcp_sock *tp;
0203     uid_t uid = ctx->uid;
0204     __u32 seq_num;
0205 
0206     if (sk_common == (void *)0)
0207         return 0;
0208 
0209     seq_num = ctx->meta->seq_num;
0210     if (seq_num == 0)
0211         BPF_SEQ_PRINTF(seq, "  sl  "
0212                     "local_address "
0213                     "rem_address   "
0214                     "st tx_queue rx_queue tr tm->when retrnsmt"
0215                     "   uid  timeout inode\n");
0216 
0217     if (sk_common->skc_family != AF_INET)
0218         return 0;
0219 
0220     tp = bpf_skc_to_tcp_sock(sk_common);
0221     if (tp)
0222         return dump_tcp_sock(seq, tp, uid, seq_num);
0223 
0224     tw = bpf_skc_to_tcp_timewait_sock(sk_common);
0225     if (tw)
0226         return dump_tw_sock(seq, tw, uid, seq_num);
0227 
0228     req = bpf_skc_to_tcp_request_sock(sk_common);
0229     if (req)
0230         return dump_req_sock(seq, req, uid, seq_num);
0231 
0232     return 0;
0233 }