Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wangqiongkaka opened this issue Jul 16, 2024 · 1 comment
Open

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

wangqiongkaka opened this issue Jul 16, 2024 · 1 comment

Comments

@wangqiongkaka
Copy link

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
Copy link
Collaborator

BSWANG commented Jul 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants