eBPF:读取getaddrinfo的结果



类似于 BPF 编译器集合 (bcc) 中的 gethostlatency.py 工具,我想跟踪函数调用getaddrinfo。此外,我想收集返回的值(IP 地址、地址系列)

但是,我似乎无法通过返回正确结果的解决方案来通过 BPF 验证器。

getaddrinfo 函数:

int getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res);

结果以struct addrinfo **res为单位返回。

这个直接基于 gethostlatency.py 的代码示例至少不返回任何错误,但返回错误的结果:

#!/usr/bin/python
#
# Based on (gethostlatency.py) https://github.com/iovisor/bcc/blob/master/tools/gethostlatency.py
# Licensed under the Apache License, Version 2.0 (the "License")
from __future__ import print_function
from bcc import BPF
from time import strftime
import argparse
examples = """examples:
./gethostlatency           # trace all TCP accept()s
./gethostlatency -p 181    # only trace PID 181
"""
parser = argparse.ArgumentParser(
description="Show latency for getaddrinfo/gethostbyname[2] calls",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
default=-1)
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
# load BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
// Copied from: include/netdb.h
struct addrinfo
{
int ai_flags;         /* Input flags.  */
int ai_family;        /* Protocol family for socket.  */
int ai_socktype;      /* Socket type.  */
int ai_protocol;      /* Protocol for socket.  */
u32 ai_addrlen;       /* Length of socket address.  */ // CHANGED from socklen_t
struct sockaddr *ai_addr; /* Socket address for socket.  */
char *ai_canonname;       /* Canonical name for service location.  */
struct addrinfo *ai_next; /* Pointer to next in list.  */
};
struct val_t {
u32 pid;
char comm[TASK_COMM_LEN];
char host[80];
u64 ts;
};
struct data_t {
u32 pid;
u64 delta;
char comm[TASK_COMM_LEN];
u32 af;
char host[80];
};
BPF_HASH(start, u32, struct val_t);
BPF_HASH(currres, u32, struct addrinfo *);
BPF_PERF_OUTPUT(events);
int do_entry(struct pt_regs *ctx, const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res) {
if (!PT_REGS_PARM1(ctx))
return 0;
struct val_t val = {};
u32 pid = bpf_get_current_pid_tgid();
if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
bpf_probe_read(&val.host, sizeof(val.host),
(void *)PT_REGS_PARM1(ctx));
val.pid = bpf_get_current_pid_tgid();
val.ts = bpf_ktime_get_ns();
start.update(&pid, &val);
currres.update(&pid, &res);
}
return 0;
}
int do_return(struct pt_regs *ctx) {
struct val_t *valp;
struct data_t data = {};
u64 delta;
u32 pid = bpf_get_current_pid_tgid();
u64 tsp = bpf_ktime_get_ns();
valp = start.lookup(&pid);
if (valp == 0)
return 0;       // missed start
bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
bpf_probe_read(&data.host, sizeof(data.host), (void *)valp->host);
struct addrinfo **result;
result = currres.lookup(&pid);
if (result == 0) {
return 0;   // missed entry
}

struct addrinfo* resx = *result;
bpf_probe_read(&data.af, sizeof(data.af), &resx->ai_family);
//data.af = resx->ai_family;
data.pid = valp->pid;
data.delta = tsp - valp->ts;
events.perf_submit(ctx, &data, sizeof(data));
start.delete(&pid);
return 0;
}
"""
if args.ebpf:
print(bpf_text)
exit()
b = BPF(text=bpf_text)
b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid)
b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return",
pid=args.pid)
# header
print("%-9s %-6s %-16s %10s %-10s %s" % ("TIME", "PID", "COMM", "LATms", "AF", "HOST"))
def print_event(cpu, data, size):
event = b["events"].event(data)
print("%-9s %-6d %-16s %10.2f %-10d %s" % (strftime("%H:%M:%S"), event.pid,
event.comm.decode('utf-8', 'replace'), (float(event.delta) / 1000000),
event.af,
event.host.decode('utf-8', 'replace')))
# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()

可能出错的事情:

  • 我从netdb.h复制了addrinfo类型定义,因为它似乎在密件抄送中无法访问。
  • 我收集结果的方式:currres.update(&pid, &res);
  • 我从结果中读取的方式:bpf_probe_read(&data.af, sizeof(data.af), &resx->ai_family);

在此示例中,程序只是尝试读取地址族 (AF)。因此,预期值将是 2 (AF_INET) 或 10 (AF_INET6),而是显示 32xxx 等数字。

要触发输出,必须发出 DNS 请求。

上面的程序版本还显示了一个警告:

/virtual/main.c:52:30: warning: incompatible pointer types passing 'struct addrinfo ***' to parameter of type
'struct addrinfo **'; remove & [-Wincompatible-pointer-types]
currres.update(&pid, &res);
^~~~

解决警告会导致 BPF 验证程序中出现更多神秘错误。

在内核版本 4.18 的 x64 上测试。

您希望currres存储从getaddrinfo获得的内核指针,因此其声明应为:

BPF_HASH(currres, u32, struct addrinfo *);

然后,您需要使用两个probe_reads从该内核指针访问值:

struct addrinfo **resx = *result;
struct addrinfo *resxx;
bpf_probe_read(&resxx, sizeof(resxx), resx);
bpf_probe_read(&data.af, sizeof(data.af), &resxx->ai_family);

请注意,对于最新的 bcc 版本 (>= v0.6.0),您也可以直接编写以下内容,bcc 会将其转换为对bpf_probe_read的相应调用。

struct addrinfo **resx = *result;
struct addrinfo *resxx = *resx;
data.af = resxx->ai_family;

这是完整的程序:

#!/usr/bin/python
#
# Based on (gethostlatency.py) https://github.com/iovisor/bcc/blob/master/tools/gethostlatency.py
# Licensed under the Apache License, Version 2.0 (the "License")
from __future__ import print_function
from bcc import BPF
from time import strftime
import argparse
examples = """examples:
./gethostlatency           # trace all TCP accept()s
./gethostlatency -p 181    # only trace PID 181
"""
parser = argparse.ArgumentParser(
description="Show latency for getaddrinfo/gethostbyname[2] calls",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
default=-1)
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
# load BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
// Copied from: include/netdb.h
struct addrinfo
{
int ai_flags;         /* Input flags.  */
int ai_family;        /* Protocol family for socket.  */
int ai_socktype;      /* Socket type.  */
int ai_protocol;      /* Protocol for socket.  */
u32 ai_addrlen;       /* Length of socket address.  */ // CHANGED from socklen_t
struct sockaddr *ai_addr; /* Socket address for socket.  */
char *ai_canonname;       /* Canonical name for service location.  */
struct addrinfo *ai_next; /* Pointer to next in list.  */
};
struct val_t {
u32 pid;
char comm[TASK_COMM_LEN];
char host[80];
u64 ts;
};
struct data_t {
u32 pid;
u64 delta;
char comm[TASK_COMM_LEN];
u32 af;
char host[80];
};
BPF_HASH(start, u32, struct val_t);
BPF_HASH(currres, u32, struct addrinfo **);
BPF_PERF_OUTPUT(events);
int do_entry(struct pt_regs *ctx, const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res) {
if (!PT_REGS_PARM1(ctx))
return 0;
struct val_t val = {};
u32 pid = bpf_get_current_pid_tgid();
if (bpf_get_current_comm(&val.comm, sizeof(val.comm)) == 0) {
bpf_probe_read(&val.host, sizeof(val.host),
(void *)PT_REGS_PARM1(ctx));
val.pid = bpf_get_current_pid_tgid();
val.ts = bpf_ktime_get_ns();
start.update(&pid, &val);
currres.update(&pid, &res);
}
return 0;
}
int do_return(struct pt_regs *ctx) {
struct val_t *valp;
struct data_t data = {};
u64 delta;
u32 pid = bpf_get_current_pid_tgid();
u64 tsp = bpf_ktime_get_ns();
valp = start.lookup(&pid);
if (valp == 0)
return 0;       // missed start
bpf_probe_read(&data.comm, sizeof(data.comm), valp->comm);
bpf_probe_read(&data.host, sizeof(data.host), (void *)valp->host);
struct addrinfo ***result;
result = currres.lookup(&pid);
if (!result || !(*result)) {
return 0;   // missed entry
}
struct addrinfo **resx = *result;
struct addrinfo *resxx = *resx;
data.af = resxx->ai_family;
//data.af = resx->ai_family;
data.pid = valp->pid;
data.delta = tsp - valp->ts;
events.perf_submit(ctx, &data, sizeof(data));
start.delete(&pid);
return 0;
}
"""
if args.ebpf:
print(bpf_text)
exit()
b = BPF(text=bpf_text)
b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid)
b.attach_uretprobe(name="c", sym="getaddrinfo", fn_name="do_return",
pid=args.pid)
# header
print("%-9s %-6s %-16s %10s %-10s %s" % ("TIME", "PID", "COMM", "LATms", "AF", "HOST"))
def print_event(cpu, data, size):
event = b["events"].event(data)
print("%-9s %-6d %-16s %10.2f %-10d %s" % (strftime("%H:%M:%S"), event.pid,
event.comm.decode('utf-8', 'replace'), (float(event.delta) / 1000000),
event.af,
event.host.decode('utf-8', 'replace')))
# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()

相关内容

  • 没有找到相关文章

最新更新