18c2ecf20Sopenharmony_ci/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com 28c2ecf20Sopenharmony_ci * 38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public 58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 88c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 98c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h> 108c2ecf20Sopenharmony_ci#include <linux/version.h> 118c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h> 128c2ecf20Sopenharmony_ci#include <bpf/bpf_tracing.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define _(P) \ 158c2ecf20Sopenharmony_ci ({ \ 168c2ecf20Sopenharmony_ci typeof(P) val = 0; \ 178c2ecf20Sopenharmony_ci bpf_probe_read_kernel(&val, sizeof(val), &(P)); \ 188c2ecf20Sopenharmony_ci val; \ 198c2ecf20Sopenharmony_ci }) 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* kprobe is NOT a stable ABI 228c2ecf20Sopenharmony_ci * kernel functions can be removed, renamed or completely change semantics. 238c2ecf20Sopenharmony_ci * Number of arguments and their positions can change, etc. 248c2ecf20Sopenharmony_ci * In such case this bpf+kprobe example will no longer be meaningful 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ciSEC("kprobe/__netif_receive_skb_core") 278c2ecf20Sopenharmony_ciint bpf_prog1(struct pt_regs *ctx) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci /* attaches to kprobe __netif_receive_skb_core, 308c2ecf20Sopenharmony_ci * looks for packets on loobpack device and prints them 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci char devname[IFNAMSIZ]; 338c2ecf20Sopenharmony_ci struct net_device *dev; 348c2ecf20Sopenharmony_ci struct sk_buff *skb; 358c2ecf20Sopenharmony_ci int len; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* non-portable! works for the given kernel only */ 388c2ecf20Sopenharmony_ci bpf_probe_read_kernel(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx)); 398c2ecf20Sopenharmony_ci dev = _(skb->dev); 408c2ecf20Sopenharmony_ci len = _(skb->len); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci bpf_probe_read_kernel(devname, sizeof(devname), dev->name); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (devname[0] == 'l' && devname[1] == 'o') { 458c2ecf20Sopenharmony_ci char fmt[] = "skb %p len %d\n"; 468c2ecf20Sopenharmony_ci /* using bpf_trace_printk() for DEBUG ONLY */ 478c2ecf20Sopenharmony_ci bpf_trace_printk(fmt, sizeof(fmt), skb, len); 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return 0; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL"; 548c2ecf20Sopenharmony_ciu32 _version SEC("version") = LINUX_VERSION_CODE; 55