Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  */
0007 #include <linux/skbuff.h>
0008 #include <linux/netdevice.h>
0009 #include <uapi/linux/bpf.h>
0010 #include <linux/version.h>
0011 #include <bpf/bpf_helpers.h>
0012 #include <bpf/bpf_tracing.h>
0013 
0014 #define _(P)                                                                   \
0015     ({                                                                     \
0016         typeof(P) val = 0;                                             \
0017         bpf_probe_read_kernel(&val, sizeof(val), &(P));                \
0018         val;                                                           \
0019     })
0020 
0021 /* kprobe is NOT a stable ABI
0022  * kernel functions can be removed, renamed or completely change semantics.
0023  * Number of arguments and their positions can change, etc.
0024  * In such case this bpf+kprobe example will no longer be meaningful
0025  */
0026 SEC("kprobe/__netif_receive_skb_core")
0027 int bpf_prog1(struct pt_regs *ctx)
0028 {
0029     /* attaches to kprobe __netif_receive_skb_core,
0030      * looks for packets on loobpack device and prints them
0031      */
0032     char devname[IFNAMSIZ];
0033     struct net_device *dev;
0034     struct sk_buff *skb;
0035     int len;
0036 
0037     /* non-portable! works for the given kernel only */
0038     bpf_probe_read_kernel(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
0039     dev = _(skb->dev);
0040     len = _(skb->len);
0041 
0042     bpf_probe_read_kernel(devname, sizeof(devname), dev->name);
0043 
0044     if (devname[0] == 'l' && devname[1] == 'o') {
0045         char fmt[] = "skb %p len %d\n";
0046         /* using bpf_trace_printk() for DEBUG ONLY */
0047         bpf_trace_printk(fmt, sizeof(fmt), skb, len);
0048     }
0049 
0050     return 0;
0051 }
0052 
0053 char _license[] SEC("license") = "GPL";
0054 u32 _version SEC("version") = LINUX_VERSION_CODE;