xref: /linux/tools/testing/selftests/bpf/prog_tests/tc_change_tail.c (revision 643e2e259c2b25a2af0ae4c23c6e16586d9fd19c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <error.h>
3 #include <test_progs.h>
4 #include <linux/pkt_cls.h>
5 
6 #include "test_tc_change_tail.skel.h"
7 #include "socket_helpers.h"
8 
9 #define LO_IFINDEX 1
10 
11 void test_tc_change_tail(void)
12 {
13 	LIBBPF_OPTS(bpf_tcx_opts, tcx_opts);
14 	struct test_tc_change_tail *skel = NULL;
15 	struct bpf_link *link;
16 	int c1, p1;
17 	char buf[2];
18 	int ret;
19 
20 	skel = test_tc_change_tail__open_and_load();
21 	if (!ASSERT_OK_PTR(skel, "test_tc_change_tail__open_and_load"))
22 		return;
23 
24 	link = bpf_program__attach_tcx(skel->progs.change_tail, LO_IFINDEX,
25 				     &tcx_opts);
26 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tcx"))
27 		goto destroy;
28 
29 	skel->links.change_tail = link;
30 	ret = create_pair(AF_INET, SOCK_DGRAM, &c1, &p1);
31 	if (!ASSERT_OK(ret, "create_pair"))
32 		goto destroy;
33 
34 	ret = xsend(p1, "Tr", 2, 0);
35 	ASSERT_EQ(ret, 2, "xsend(p1)");
36 	ret = recv(c1, buf, 2, 0);
37 	ASSERT_EQ(ret, 2, "recv(c1)");
38 	ASSERT_EQ(skel->data->change_tail_ret, 0, "change_tail_ret");
39 
40 	ret = xsend(p1, "G", 1, 0);
41 	ASSERT_EQ(ret, 1, "xsend(p1)");
42 	ret = recv(c1, buf, 2, 0);
43 	ASSERT_EQ(ret, 1, "recv(c1)");
44 	ASSERT_EQ(skel->data->change_tail_ret, 0, "change_tail_ret");
45 
46 	ret = xsend(p1, "E", 1, 0);
47 	ASSERT_EQ(ret, 1, "xsend(p1)");
48 	ret = recv(c1, buf, 1, 0);
49 	ASSERT_EQ(ret, 1, "recv(c1)");
50 	ASSERT_EQ(skel->data->change_tail_ret, -EINVAL, "change_tail_ret");
51 
52 	ret = xsend(p1, "Z", 1, 0);
53 	ASSERT_EQ(ret, 1, "xsend(p1)");
54 	ret = recv(c1, buf, 1, 0);
55 	ASSERT_EQ(ret, 1, "recv(c1)");
56 	ASSERT_EQ(skel->data->change_tail_ret, -EINVAL, "change_tail_ret");
57 
58 	close(c1);
59 	close(p1);
60 destroy:
61 	test_tc_change_tail__destroy(skel);
62 }
63