xref: /linux/tools/testing/selftests/bpf/progs/xsk_xdp_progs.c (revision a55f7f5f29b32c2c53cc291899cf9b0c25a07f7c)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Intel */
3 
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include <linux/if_ether.h>
7 #include <linux/ip.h>
8 #include <linux/errno.h>
9 #include "xsk_xdp_common.h"
10 
11 struct {
12 	__uint(type, BPF_MAP_TYPE_XSKMAP);
13 	__uint(max_entries, 2);
14 	__uint(key_size, sizeof(int));
15 	__uint(value_size, sizeof(int));
16 } xsk SEC(".maps");
17 
18 static unsigned int idx;
19 int adjust_value = 0;
20 int count = 0;
21 
xsk_def_prog(struct xdp_md * xdp)22 SEC("xdp.frags") int xsk_def_prog(struct xdp_md *xdp)
23 {
24 	return bpf_redirect_map(&xsk, 0, XDP_DROP);
25 }
26 
xsk_xdp_drop(struct xdp_md * xdp)27 SEC("xdp.frags") int xsk_xdp_drop(struct xdp_md *xdp)
28 {
29 	static unsigned int drop_idx;
30 
31 	/* Drop every other packet */
32 	if (drop_idx++ % 2)
33 		return XDP_DROP;
34 
35 	return bpf_redirect_map(&xsk, 0, XDP_DROP);
36 }
37 
xsk_xdp_populate_metadata(struct xdp_md * xdp)38 SEC("xdp.frags") int xsk_xdp_populate_metadata(struct xdp_md *xdp)
39 {
40 	void *data, *data_meta;
41 	struct xdp_info *meta;
42 	int err;
43 
44 	/* Reserve enough for all custom metadata. */
45 	err = bpf_xdp_adjust_meta(xdp, -(int)sizeof(struct xdp_info));
46 	if (err)
47 		return XDP_DROP;
48 
49 	data = (void *)(long)xdp->data;
50 	data_meta = (void *)(long)xdp->data_meta;
51 
52 	if (data_meta + sizeof(struct xdp_info) > data)
53 		return XDP_DROP;
54 
55 	meta = data_meta;
56 	meta->count = count++;
57 
58 	return bpf_redirect_map(&xsk, 0, XDP_DROP);
59 }
60 
xsk_xdp_shared_umem(struct xdp_md * xdp)61 SEC("xdp") int xsk_xdp_shared_umem(struct xdp_md *xdp)
62 {
63 	void *data = (void *)(long)xdp->data;
64 	void *data_end = (void *)(long)xdp->data_end;
65 	struct ethhdr *eth = data;
66 
67 	if (eth + 1 > data_end)
68 		return XDP_DROP;
69 
70 	/* Redirecting packets based on the destination MAC address */
71 	idx = ((unsigned int)(eth->h_dest[5])) / 2;
72 	if (idx > MAX_SOCKETS)
73 		return XDP_DROP;
74 
75 	return bpf_redirect_map(&xsk, idx, XDP_DROP);
76 }
77 
xsk_xdp_adjust_tail(struct xdp_md * xdp)78 SEC("xdp.frags") int xsk_xdp_adjust_tail(struct xdp_md *xdp)
79 {
80 	__u32 buff_len, curr_buff_len;
81 	int ret;
82 
83 	buff_len = bpf_xdp_get_buff_len(xdp);
84 	if (buff_len == 0)
85 		return XDP_DROP;
86 
87 	ret = bpf_xdp_adjust_tail(xdp, adjust_value);
88 	if (ret < 0) {
89 		/* Handle unsupported cases */
90 		if (ret == -EOPNOTSUPP) {
91 			/* Set adjust_value to -EOPNOTSUPP to indicate to userspace that this case
92 			 * is unsupported
93 			 */
94 			adjust_value = -EOPNOTSUPP;
95 			return bpf_redirect_map(&xsk, 0, XDP_DROP);
96 		}
97 
98 		return XDP_DROP;
99 	}
100 
101 	curr_buff_len = bpf_xdp_get_buff_len(xdp);
102 	if (curr_buff_len != buff_len + adjust_value)
103 		return XDP_DROP;
104 
105 	if (curr_buff_len > buff_len) {
106 		__u32 *pkt_data = (void *)(long)xdp->data;
107 		__u32 len, words_to_end, seq_num;
108 
109 		len = curr_buff_len - PKT_HDR_ALIGN;
110 		words_to_end = len / sizeof(*pkt_data) - 1;
111 		seq_num = words_to_end;
112 
113 		/* Convert sequence number to network byte order. Store this in the last 4 bytes of
114 		 * the packet. Use 'adjust_value' to determine the position at the end of the
115 		 * packet for storing the sequence number.
116 		 */
117 		seq_num = __constant_htonl(words_to_end);
118 		bpf_xdp_store_bytes(xdp, curr_buff_len - sizeof(seq_num), &seq_num,
119 				    sizeof(seq_num));
120 	}
121 
122 	return bpf_redirect_map(&xsk, 0, XDP_DROP);
123 }
124 
125 char _license[] SEC("license") = "GPL";
126