1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_endian.h>
7 #include <bpf/bpf_tracing.h>
9 char _license[] SEC("license") = "GPL";
11 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
14 } perf_buf_map SEC(".maps");
16 #define _(P) (__builtin_preserve_access_index(P))
18 /* define few struct-s that bpf program needs to access */
19 struct callback_head {
20 struct callback_head *next;
21 void (*func)(struct callback_head *head);
24 struct callback_head rcuhead;
27 struct net_device /* same as kernel's struct net_device */ {
29 struct dev_ifalias *ifalias;
35 typedef struct refcount_struct {
40 /* field names and sizes should match to those in the kernel */
41 unsigned int len, data_len;
42 __u16 mac_len, hdr_len, queue_mapping;
43 struct net_device *dev;
44 /* order of the fields doesn't matter */
47 char __pkt_type_offset[0];
57 /* TRACE_EVENT(kfree_skb,
58 * TP_PROTO(struct sk_buff *skb, void *location),
60 SEC("tp_btf/kfree_skb")
61 int BPF_PROG(trace_kfree_skb, struct sk_buff *skb, void *location)
63 struct net_device *dev;
64 struct callback_head *ptr;
68 unsigned short pkt_data;
69 struct meta meta = {};
74 __builtin_preserve_access_index(({
75 users = skb->users.refs.counter;
78 ptr = dev->ifalias->rcuhead.next;
80 cb8 = (__u8 *)&skb->cb;
81 cb32 = (__u32 *)&skb->cb;
84 meta.ifindex = _(dev->ifindex);
86 meta.cb32_0 = cb32[2];
88 bpf_probe_read_kernel(&pkt_type, sizeof(pkt_type), _(&skb->__pkt_type_offset));
92 bpf_probe_read_kernel(&pkt_data, sizeof(pkt_data), data + 12);
94 bpf_printk("rcuhead.next %llx func %llx\n", ptr, func);
95 bpf_printk("skb->len %d users %d pkt_type %x\n",
96 _(skb->len), users, pkt_type);
97 bpf_printk("skb->queue_mapping %d\n", _(skb->queue_mapping));
98 bpf_printk("dev->ifindex %d data %llx pkt_data %x\n",
99 meta.ifindex, data, pkt_data);
100 bpf_printk("cb8_0:%x cb32_0:%x\n", meta.cb8_0, meta.cb32_0);
102 if (users != 1 || pkt_data != bpf_htons(0x86dd) || meta.ifindex != 1)
103 /* raw tp ignores return value */
106 /* send first 72 byte of the packet to user space */
107 bpf_skb_output(skb, &perf_buf_map, (72ull << 32) | BPF_F_CURRENT_CPU,
108 &meta, sizeof(meta));
117 SEC("fentry/eth_type_trans")
118 int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb, struct net_device *dev,
119 unsigned short protocol)
123 __builtin_preserve_access_index(({
125 ifindex = dev->ifindex;
128 /* fentry sees full packet including L2 header */
129 if (len != 74 || ifindex != 1)
131 result.fentry_test_ok = true;
135 SEC("fexit/eth_type_trans")
136 int BPF_PROG(fexit_eth_type_trans, struct sk_buff *skb, struct net_device *dev,
137 unsigned short protocol)
141 __builtin_preserve_access_index(({
143 ifindex = dev->ifindex;
146 /* fexit sees packet without L2 header that eth_type_trans should have
149 if (len != 60 || protocol != bpf_htons(0x86dd) || ifindex != 1)
151 result.fexit_test_ok = true;