[英]ebpf: how to use BPF_FUNC_trace_printk in eBPF assembly program
[英]eBPF tools - skb_network_header crashes in a BPF Kernel trace function
我正在尋找追蹤ip_forward_finish
。 目的是跟蹤通過基於 linux 的網關路由器的所有 TCP 連接的延遲。 因此想到了跟蹤ip_forward_finish
kernel function。 並在路由器上捕獲SYN
、 SYN-ACK
和ACK
消息的時間戳。
問題是在跟蹤 function 中訪問iphdr
使驗證程序抱怨以下錯誤:
bpf: Failed to load program: Permission denied
0: (79) r6 = *(u64 *)(r1 +96)
1: (b7) r1 = 0
2: (6b) *(u16 *)(r10 -24) = r1
3: (bf) r3 = r6
4: (07) r3 += 192
5: (bf) r1 = r10
6: (07) r1 += -24
7: (b7) r2 = 2
8: (85) call bpf_probe_read#4
9: (69) r1 = *(u16 *)(r10 -24)
10: (55) if r1 != 0x8 goto pc+7
R0=inv(id=0) R1=inv8 R6=inv(id=0) R10=fp0
11: (69) r1 = *(u16 *)(r6 +196)
R6 invalid mem access 'inv'
HINT: The invalid mem access 'inv' error can happen if you try to dereference
memory without first using bpf_probe_read() to copy it to the BPF stack.
Sometimes the bpf_probe_read is automatic by the bcc rewriter, other times
you'll need to be explicit.
我最初的代碼片段如下,當訪問ip_Hdr->protocol
時發生崩潰。 而且我還檢查了ip_Hdr
不是 null。
int trace_forward_finish(struct pt_regs *ctx,struct net *net,
struct sock *sk, struct sk_buff *skb)
{
if (skb->protocol != htons(ETH_P_IP))
return 0;
struct iphdr* ip_Hdr = (struct iphdr *) skb_network_header(skb);
if (ip_Hdr->protocol != IPPROTO_TCP)
return 0;
/// More code
}
根據消息中的提示,我確實嘗試更改為bpf_probe_read
但結果仍然相同
int trace_forward_finish(struct pt_regs *ctx,struct net *net,
struct sock *sk, struct sk_buff *skb)
{
if (skb->protocol != htons(ETH_P_IP))
return 0;
struct iphdr ip_Hdr;
bpf_probe_read(&ip_Hdr, sizeof(ip_Hdr), (void*)ip_hdr(skb));
if (ip_Hdr.protocol != IPPROTO_TCP)
return 0;
return 0;
}
任何幫助,將不勝感激。
bcc 將嘗試將您對 kernel 指針的取消引用轉換為對bpf_probe_read
的調用。 您可以通過將debug=4
傳遞給BPF()
調用來看到這種情況。
在您的情況下,我懷疑您需要在代碼中包含 function skb_network_header
以便 bcc 能夠重寫它。
如果這還不夠,那么您可能需要手動調用bpf_probe_read
以從skb_network_header
的指針檢索結構struct iphdr
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.