183e4b88bSEelco Chaudron // SPDX-License-Identifier: GPL-2.0
283e4b88bSEelco Chaudron #include <linux/bpf.h>
3df8ff353SAndrii Nakryiko #include <bpf/bpf_tracing.h>
43e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
583e4b88bSEelco Chaudron 
6d831ee84SEelco Chaudron char _license[] SEC("license") = "GPL";
7d831ee84SEelco Chaudron 
883e4b88bSEelco Chaudron struct net_device {
983e4b88bSEelco Chaudron 	/* Structure does not need to contain all entries,
1083e4b88bSEelco Chaudron 	 * as "preserve_access_index" will use BTF to fix this...
1183e4b88bSEelco Chaudron 	 */
1283e4b88bSEelco Chaudron 	int ifindex;
1383e4b88bSEelco Chaudron } __attribute__((preserve_access_index));
1483e4b88bSEelco Chaudron 
1583e4b88bSEelco Chaudron struct xdp_rxq_info {
1683e4b88bSEelco Chaudron 	/* Structure does not need to contain all entries,
1783e4b88bSEelco Chaudron 	 * as "preserve_access_index" will use BTF to fix this...
1883e4b88bSEelco Chaudron 	 */
1983e4b88bSEelco Chaudron 	struct net_device *dev;
2083e4b88bSEelco Chaudron 	__u32 queue_index;
2183e4b88bSEelco Chaudron } __attribute__((preserve_access_index));
2283e4b88bSEelco Chaudron 
2383e4b88bSEelco Chaudron struct xdp_buff {
2483e4b88bSEelco Chaudron 	void *data;
2583e4b88bSEelco Chaudron 	void *data_end;
2683e4b88bSEelco Chaudron 	void *data_meta;
2783e4b88bSEelco Chaudron 	void *data_hard_start;
2883e4b88bSEelco Chaudron 	unsigned long handle;
2983e4b88bSEelco Chaudron 	struct xdp_rxq_info *rxq;
3083e4b88bSEelco Chaudron } __attribute__((preserve_access_index));
3183e4b88bSEelco Chaudron 
32d831ee84SEelco Chaudron struct meta {
33d831ee84SEelco Chaudron 	int ifindex;
34d831ee84SEelco Chaudron 	int pkt_len;
35d831ee84SEelco Chaudron };
36d831ee84SEelco Chaudron 
37d831ee84SEelco Chaudron struct {
38d831ee84SEelco Chaudron 	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
39bd368cb5SHengqi Chen 	__type(key, int);
40bd368cb5SHengqi Chen 	__type(value, int);
41d831ee84SEelco Chaudron } perf_buf_map SEC(".maps");
42d831ee84SEelco Chaudron 
4383e4b88bSEelco Chaudron __u64 test_result_fentry = 0;
44933ce62dSEelco Chaudron SEC("fentry/FUNC")
BPF_PROG(trace_on_entry,struct xdp_buff * xdp)4583e4b88bSEelco Chaudron int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
4683e4b88bSEelco Chaudron {
47d831ee84SEelco Chaudron 	struct meta meta;
48d831ee84SEelco Chaudron 
49d831ee84SEelco Chaudron 	meta.ifindex = xdp->rxq->dev->ifindex;
50*d9917302SEelco Chaudron 	meta.pkt_len = bpf_xdp_get_buff_len((struct xdp_md *)xdp);
51d831ee84SEelco Chaudron 	bpf_xdp_output(xdp, &perf_buf_map,
52d831ee84SEelco Chaudron 		       ((__u64) meta.pkt_len << 32) |
53d831ee84SEelco Chaudron 		       BPF_F_CURRENT_CPU,
54d831ee84SEelco Chaudron 		       &meta, sizeof(meta));
55d831ee84SEelco Chaudron 
5683e4b88bSEelco Chaudron 	test_result_fentry = xdp->rxq->dev->ifindex;
5783e4b88bSEelco Chaudron 	return 0;
5883e4b88bSEelco Chaudron }
5983e4b88bSEelco Chaudron 
6083e4b88bSEelco Chaudron __u64 test_result_fexit = 0;
61933ce62dSEelco Chaudron SEC("fexit/FUNC")
BPF_PROG(trace_on_exit,struct xdp_buff * xdp,int ret)6283e4b88bSEelco Chaudron int BPF_PROG(trace_on_exit, struct xdp_buff *xdp, int ret)
6383e4b88bSEelco Chaudron {
6483e4b88bSEelco Chaudron 	test_result_fexit = ret;
6583e4b88bSEelco Chaudron 	return 0;
6683e4b88bSEelco Chaudron }
67