tracex1.bpf.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include "vmlinux.h"
  8. #include "net_shared.h"
  9. #include <linux/version.h>
  10. #include <bpf/bpf_helpers.h>
  11. #include <bpf/bpf_core_read.h>
  12. #include <bpf/bpf_tracing.h>
  13. /* kprobe is NOT a stable ABI
  14. * kernel functions can be removed, renamed or completely change semantics.
  15. * Number of arguments and their positions can change, etc.
  16. * In such case this bpf+kprobe example will no longer be meaningful
  17. */
  18. SEC("kprobe.multi/__netif_receive_skb_core*")
  19. int bpf_prog1(struct pt_regs *ctx)
  20. {
  21. /* attaches to kprobe __netif_receive_skb_core,
  22. * looks for packets on loopback device and prints them
  23. * (wildcard is used for avoiding symbol mismatch due to optimization)
  24. */
  25. char devname[IFNAMSIZ];
  26. struct net_device *dev;
  27. struct sk_buff *skb;
  28. int len;
  29. bpf_core_read(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
  30. dev = BPF_CORE_READ(skb, dev);
  31. len = BPF_CORE_READ(skb, len);
  32. BPF_CORE_READ_STR_INTO(&devname, dev, name);
  33. if (devname[0] == 'l' && devname[1] == 'o') {
  34. char fmt[] = "skb %p len %d\n";
  35. /* using bpf_trace_printk() for DEBUG ONLY */
  36. bpf_trace_printk(fmt, sizeof(fmt), skb, len);
  37. }
  38. return 0;
  39. }
  40. char _license[] SEC("license") = "GPL";
  41. u32 _version SEC("version") = LINUX_VERSION_CODE;