tracex1_kern.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/bpf_helpers.h>
  12. #include <bpf/bpf_tracing.h>
  13. #define _(P) \
  14. ({ \
  15. typeof(P) val = 0; \
  16. bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
  17. val; \
  18. })
  19. /* kprobe is NOT a stable ABI
  20. * kernel functions can be removed, renamed or completely change semantics.
  21. * Number of arguments and their positions can change, etc.
  22. * In such case this bpf+kprobe example will no longer be meaningful
  23. */
  24. SEC("kprobe/__netif_receive_skb_core")
  25. int bpf_prog1(struct pt_regs *ctx)
  26. {
  27. /* attaches to kprobe __netif_receive_skb_core,
  28. * looks for packets on loobpack device and prints them
  29. */
  30. char devname[IFNAMSIZ];
  31. struct net_device *dev;
  32. struct sk_buff *skb;
  33. int len;
  34. /* non-portable! works for the given kernel only */
  35. bpf_probe_read_kernel(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
  36. dev = _(skb->dev);
  37. len = _(skb->len);
  38. bpf_probe_read_kernel(devname, sizeof(devname), dev->name);
  39. if (devname[0] == 'l' && devname[1] == 'o') {
  40. char fmt[] = "skb %p len %d\n";
  41. /* using bpf_trace_printk() for DEBUG ONLY */
  42. bpf_trace_printk(fmt, sizeof(fmt), skb, len);
  43. }
  44. return 0;
  45. }
  46. char _license[] SEC("license") = "GPL";
  47. u32 _version SEC("version") = LINUX_VERSION_CODE;