aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_xdp_adjust_tail_shrink.c
blob: ca68c038357c3b86ed79ba81fa0b2f9b997caad9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2018 Facebook
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 */
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <bpf/bpf_helpers.h>

SEC("xdp")
int _xdp_adjust_tail_shrink(struct xdp_md *xdp)
{
	__u8 *data_end = (void *)(long)xdp->data_end;
	__u8 *data = (void *)(long)xdp->data;
	int offset = 0;

	switch (bpf_xdp_get_buff_len(xdp)) {
	case 54:
		/* sizeof(pkt_v4) */
		offset = 256; /* shrink too much */
		break;
	case 9000:
		/* non-linear buff test cases */
		if (data + 1 > data_end)
			return XDP_DROP;

		switch (data[0]) {
		case 0:
			offset = 10;
			break;
		case 1:
			offset = 4100;
			break;
		case 2:
			offset = 8200;
			break;
		default:
			return XDP_DROP;
		}
		break;
	default:
		offset = 20;
		break;
	}
	if (bpf_xdp_adjust_tail(xdp, 0 - offset))
		return XDP_DROP;
	return XDP_TX;
}

char _license[] SEC("license") = "GPL";