【问题标题】:eBPF bcctools get contents from tracepipeeBPF bcctools 从 tracepipe 获取内容
【发布时间】:2021-11-27 13:18:25
【问题描述】:

这是一个在tracepipe中跟踪通信的小程序。我的问题是如何使用 bcctools 访问 tracepipe 的内容。 我尝试使用fopen 手动读取文件,但不起作用,有什么我不知道的功能吗?

Modified  #define KBUILD_MODNAME "filter"
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/udp.h>

#include <linux/if_packet.h>
#include <linux/if_vlan.h>


int udpfilter(struct xdp_md *ctx) {
  bpf_trace_printk("got a packet\n");
  void *data = (void *)(long)ctx->data;
  void *data_end = (void *)(long)ctx->data_end;
  struct ethhdr *eth = data;
  if ((void*)eth + sizeof(*eth) <= data_end) {
    struct iphdr *ip = data + sizeof(*eth);
    if ((void*)ip + sizeof(*ip) <= data_end) {
      if (ip->protocol == IPPROTO_UDP) {
        struct udphdr *udp = (void*)ip + sizeof(*ip);
        if ((void*)udp + sizeof(*udp) <= data_end) {
          if (udp->dest == ntohs(7999)) {
            bpf_trace_printk("udp port 7999\n");
            udp->dest = ntohs(7998);
          }
        }
      }
    }
  }
  return XDP_PASS;
}

【问题讨论】:

    标签: c ebpf bpf bcc-bpf


    【解决方案1】:

    要显示跟踪管道的输出,您可以简单地使用:

    bpftool prog tracelog
    

    但请注意,跟踪管道仅用于调试。如果您检查您的系统日志,每当您使用bpf_trace_printk 时,您都会注意到一个很大的警告。相反,您应该use the perf ring buffer

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 2011-01-22
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多