tracex1_kern.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <linux/skbuff.h>
  8. #include <linux/netdevice.h>
  9. #include <uapi/linux/bpf.h>
  10. #include <linux/version.h>
  11. #include "bpf_helpers.h"
  12. #define _(P) ({typeof(P) val = 0; bpf_probe_read(&val, sizeof(val), &P); val;})
  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/__netif_receive_skb_core")
  19. int bpf_prog1(struct pt_regs *ctx)
  20. {
  21. /* attaches to kprobe netif_receive_skb,
  22. * looks for packets on loobpack device and prints them
  23. */
  24. char devname[IFNAMSIZ] = {};
  25. struct net_device *dev;
  26. struct sk_buff *skb;
  27. int len;
  28. /* non-portable! works for the given kernel only */
  29. skb = (struct sk_buff *) ctx->di;
  30. dev = _(skb->dev);
  31. len = _(skb->len);
  32. bpf_probe_read(devname, sizeof(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;