【问题标题】:Why dont bpf redirect work correctly?(XDP)为什么 bpf 重定向不能正常工作?(XDP)
【发布时间】:2020-11-30 19:29:56
【问题描述】:

我写了一个这样的简单程序:

SEC("tc_redirect")
int _egress_redirect(struct __sk_buff *skb){
    return bpf_redirect(5,0); // redirect all egress packets to interface 5
}
# tc filter add dev (4) egress prio 1 handle 1 bpf da obj x.o sec tc_redirect

发送方将数据包重定向到接口5,接收方在对等接口接收数据包,但像iperf这样的程序没有收到,为什么?

【问题讨论】:

  • 给未来读者的简单说明:这里提供的tc命令用于加载程序作为TC过滤器,not作为标题和标签建议的XDP程序.

标签: linux bpf ebpf xdp-bpf


【解决方案1】:

如果您可以观察到数据包到达接收器的接口(例如,使用 tcpdump)但未在您的用户空间进程中,则 该数据包可能被内核丢弃

您应该检查目标 IP、目标 MAC 地址和目标端口是否正确。您还可以使用以下 bcc 脚本查看哪个内核函数丢弃了数据包。在例如搜索该功能然后,bootlin 可以为您提供一些关于正在发生的事情的指示。

#!/usr/bin/python
#
# packetdrop  Prints the kernel functions responsible for packet drops. Similar
#             to dropwatch.
#
# REQUIRES: Linux 4.7+ (BPF_PROG_TYPE_TRACEPOINT support).
#
# Copyright 2018 Orange.
# Licensed under the Apache License, Version 2.0 (the "License")
from __future__ import print_function
from bcc import BPF
from time import sleep

# load BPF program
b = BPF(text="""
struct key_t {
    u64 location;
};
BPF_HASH(drops, struct key_t);
TRACEPOINT_PROBE(skb, kfree_skb) {
    u64 zero = 0, *count;
    struct key_t key = {};
    // args is from /sys/kernel/debug/tracing/events/skb/kfree_skb/format
    key.location = (u64)args->location;
    count = drops.lookup_or_init(&key, &zero);
    (*count)++;
    return 0;
};
""")

# header
print("Tracing... Ctrl-C to end.")

# format output
try:
    sleep(99999999)
except KeyboardInterrupt:
    pass

print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT"))
drops = b.get_table("drops")
print(drops.items()[0][1].value)
for k, v in sorted(drops.items(),
                   key=lambda elem: elem[1].value):
    print("%-16x %-26s %8d"
          % (k.location, b.ksym(k.location), v.value))

【讨论】:

  • 作为记录,我认为dropwatch 可能也有帮助,虽然我从未使用过它。
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 2019-01-04
  • 2020-09-03
  • 2016-10-10
  • 2016-10-24
  • 2017-02-27
相关资源
最近更新 更多