alibaba / kubeskoop

Network monitoring & diagnosis suite for Kubernetes
https://kubeskoop.io
Apache License 2.0
579 stars 68 forks source link

SOCKETLAT_SENDSLOW 源端口大小端转换问题 #306

Open wangqiongkaka opened 3 months ago

wangqiongkaka commented 3 months ago

static always_inline void set_tuple_sock(struct sock sk, struct tuple tpl) { short unsigned int skc_family; skc_family = BPF_CORE_READ(sk, __sk_common.skc_family); if (skc_family == PF_INET6) { // TODO: add v6 sock support tpl->l3_proto = ETH_P_IPV6; } else { bpf_probe_read(&tpl->saddr, sizeof(tpl->saddr.v4addr), &sk->sk_common.skc_rcv_saddr); bpf_probe_read(&tpl->daddr, sizeof(tpl->daddr.v4addr), &sk->__sk_common.skc_daddr); tpl->l3_proto = ETH_P_IP; }

tpl->sport = BPF_CORE_READ(sk, sk_common.skc_num); tpl->dport = BPF_CORE_READ(sk, sk_common.skc_dport); tpl->l4_proto = get_sock_protocol(sk); ; } 源目的端口的字段类型是 struct { __be16 skc_dport; __u16 skc_num; }; 用户态程序对源目的端口都进行了 bits.ReverseBytes16 转换 tuple := fmt.Sprintf("protocol=%s saddr=%s sport=%d daddr=%s dport=%d ", bpfutil.GetProtoStr(event.Tuple.L4Proto), bpfutil.GetAddrStr(event.Tuple.L3Proto, ([16]byte)(unsafe.Pointer(&event.Tuple.Saddr))), bits.ReverseBytes16(event.Tuple.Sport), bpfutil.GetAddrStr(event.Tuple.L3Proto, ([16]byte)(unsafe.Pointer(&event.Tuple.Daddr))), bits.ReverseBytes16(event.Tuple.Dport))

从而导致event日志事件,获取的目的端口是正确的,源端口是错误的

BSWANG commented 3 months ago

看了下,这里确实有问题,skc_num一般是主机序存储的,方便的话可以提个PR来修复下吗?感谢 可以参考bcc中的读取和处理代码: https://github.com/iovisor/bcc/blob/92526ad2e62f5da7b3017e9a64d24c579a0bc9a4/libbpf-tools/tcptop.bpf.c#L58