xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/lwt_helpers.h (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
1*43a7c3efSYan Zhai /* SPDX-License-Identifier: GPL-2.0 */
2*43a7c3efSYan Zhai 
3*43a7c3efSYan Zhai #ifndef __LWT_HELPERS_H
4*43a7c3efSYan Zhai #define __LWT_HELPERS_H
5*43a7c3efSYan Zhai 
6*43a7c3efSYan Zhai #include <time.h>
7*43a7c3efSYan Zhai #include <net/if.h>
8*43a7c3efSYan Zhai #include <linux/if_tun.h>
9*43a7c3efSYan Zhai #include <linux/icmp.h>
10*43a7c3efSYan Zhai 
11*43a7c3efSYan Zhai #include "test_progs.h"
12*43a7c3efSYan Zhai 
13*43a7c3efSYan Zhai #define log_err(MSG, ...) \
14*43a7c3efSYan Zhai 	fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
15*43a7c3efSYan Zhai 		__FILE__, __LINE__, strerror(errno), ##__VA_ARGS__)
16*43a7c3efSYan Zhai 
17*43a7c3efSYan Zhai #define RUN_TEST(name)                                                        \
18*43a7c3efSYan Zhai 	({                                                                    \
19*43a7c3efSYan Zhai 		if (test__start_subtest(#name))                               \
20*43a7c3efSYan Zhai 			if (ASSERT_OK(netns_create(), "netns_create")) {      \
21*43a7c3efSYan Zhai 				struct nstoken *token = open_netns(NETNS);    \
22*43a7c3efSYan Zhai 				if (ASSERT_OK_PTR(token, "setns")) {          \
23*43a7c3efSYan Zhai 					test_ ## name();                      \
24*43a7c3efSYan Zhai 					close_netns(token);                   \
25*43a7c3efSYan Zhai 				}                                             \
26*43a7c3efSYan Zhai 				netns_delete();                               \
27*43a7c3efSYan Zhai 			}                                                     \
28*43a7c3efSYan Zhai 	})
29*43a7c3efSYan Zhai 
netns_create(void)30*43a7c3efSYan Zhai static inline int netns_create(void)
31*43a7c3efSYan Zhai {
32*43a7c3efSYan Zhai 	return system("ip netns add " NETNS);
33*43a7c3efSYan Zhai }
34*43a7c3efSYan Zhai 
netns_delete(void)35*43a7c3efSYan Zhai static inline int netns_delete(void)
36*43a7c3efSYan Zhai {
37*43a7c3efSYan Zhai 	return system("ip netns del " NETNS ">/dev/null 2>&1");
38*43a7c3efSYan Zhai }
39*43a7c3efSYan Zhai 
open_tuntap(const char * dev_name,bool need_mac)40*43a7c3efSYan Zhai static int open_tuntap(const char *dev_name, bool need_mac)
41*43a7c3efSYan Zhai {
42*43a7c3efSYan Zhai 	int err = 0;
43*43a7c3efSYan Zhai 	struct ifreq ifr;
44*43a7c3efSYan Zhai 	int fd = open("/dev/net/tun", O_RDWR);
45*43a7c3efSYan Zhai 
46*43a7c3efSYan Zhai 	if (!ASSERT_GT(fd, 0, "open(/dev/net/tun)"))
47*43a7c3efSYan Zhai 		return -1;
48*43a7c3efSYan Zhai 
49*43a7c3efSYan Zhai 	ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN);
50*43a7c3efSYan Zhai 	memcpy(ifr.ifr_name, dev_name, IFNAMSIZ);
51*43a7c3efSYan Zhai 
52*43a7c3efSYan Zhai 	err = ioctl(fd, TUNSETIFF, &ifr);
53*43a7c3efSYan Zhai 	if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {
54*43a7c3efSYan Zhai 		close(fd);
55*43a7c3efSYan Zhai 		return -1;
56*43a7c3efSYan Zhai 	}
57*43a7c3efSYan Zhai 
58*43a7c3efSYan Zhai 	err = fcntl(fd, F_SETFL, O_NONBLOCK);
59*43a7c3efSYan Zhai 	if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) {
60*43a7c3efSYan Zhai 		close(fd);
61*43a7c3efSYan Zhai 		return -1;
62*43a7c3efSYan Zhai 	}
63*43a7c3efSYan Zhai 
64*43a7c3efSYan Zhai 	return fd;
65*43a7c3efSYan Zhai }
66*43a7c3efSYan Zhai 
67*43a7c3efSYan Zhai #define ICMP_PAYLOAD_SIZE     100
68*43a7c3efSYan Zhai 
69*43a7c3efSYan Zhai /* Match an ICMP packet with payload len ICMP_PAYLOAD_SIZE */
__expect_icmp_ipv4(char * buf,ssize_t len)70*43a7c3efSYan Zhai static int __expect_icmp_ipv4(char *buf, ssize_t len)
71*43a7c3efSYan Zhai {
72*43a7c3efSYan Zhai 	struct iphdr *ip = (struct iphdr *)buf;
73*43a7c3efSYan Zhai 	struct icmphdr *icmp = (struct icmphdr *)(ip + 1);
74*43a7c3efSYan Zhai 	ssize_t min_header_len = sizeof(*ip) + sizeof(*icmp);
75*43a7c3efSYan Zhai 
76*43a7c3efSYan Zhai 	if (len < min_header_len)
77*43a7c3efSYan Zhai 		return -1;
78*43a7c3efSYan Zhai 
79*43a7c3efSYan Zhai 	if (ip->protocol != IPPROTO_ICMP)
80*43a7c3efSYan Zhai 		return -1;
81*43a7c3efSYan Zhai 
82*43a7c3efSYan Zhai 	if (icmp->type != ICMP_ECHO)
83*43a7c3efSYan Zhai 		return -1;
84*43a7c3efSYan Zhai 
85*43a7c3efSYan Zhai 	return len == ICMP_PAYLOAD_SIZE + min_header_len;
86*43a7c3efSYan Zhai }
87*43a7c3efSYan Zhai 
88*43a7c3efSYan Zhai typedef int (*filter_t) (char *, ssize_t);
89*43a7c3efSYan Zhai 
90*43a7c3efSYan Zhai /* wait_for_packet - wait for a packet that matches the filter
91*43a7c3efSYan Zhai  *
92*43a7c3efSYan Zhai  * @fd: tun fd/packet socket to read packet
93*43a7c3efSYan Zhai  * @filter: filter function, returning 1 if matches
94*43a7c3efSYan Zhai  * @timeout: timeout to wait for the packet
95*43a7c3efSYan Zhai  *
96*43a7c3efSYan Zhai  * Returns 1 if a matching packet is read, 0 if timeout expired, -1 on error.
97*43a7c3efSYan Zhai  */
wait_for_packet(int fd,filter_t filter,struct timeval * timeout)98*43a7c3efSYan Zhai static int wait_for_packet(int fd, filter_t filter, struct timeval *timeout)
99*43a7c3efSYan Zhai {
100*43a7c3efSYan Zhai 	char buf[4096];
101*43a7c3efSYan Zhai 	int max_retry = 5; /* in case we read some spurious packets */
102*43a7c3efSYan Zhai 	fd_set fds;
103*43a7c3efSYan Zhai 
104*43a7c3efSYan Zhai 	FD_ZERO(&fds);
105*43a7c3efSYan Zhai 	while (max_retry--) {
106*43a7c3efSYan Zhai 		/* Linux modifies timeout arg... So make a copy */
107*43a7c3efSYan Zhai 		struct timeval copied_timeout = *timeout;
108*43a7c3efSYan Zhai 		ssize_t ret = -1;
109*43a7c3efSYan Zhai 
110*43a7c3efSYan Zhai 		FD_SET(fd, &fds);
111*43a7c3efSYan Zhai 
112*43a7c3efSYan Zhai 		ret = select(1 + fd, &fds, NULL, NULL, &copied_timeout);
113*43a7c3efSYan Zhai 		if (ret <= 0) {
114*43a7c3efSYan Zhai 			if (errno == EINTR)
115*43a7c3efSYan Zhai 				continue;
116*43a7c3efSYan Zhai 			else if (errno == EAGAIN || ret == 0)
117*43a7c3efSYan Zhai 				return 0;
118*43a7c3efSYan Zhai 
119*43a7c3efSYan Zhai 			log_err("select failed");
120*43a7c3efSYan Zhai 			return -1;
121*43a7c3efSYan Zhai 		}
122*43a7c3efSYan Zhai 
123*43a7c3efSYan Zhai 		ret = read(fd, buf, sizeof(buf));
124*43a7c3efSYan Zhai 
125*43a7c3efSYan Zhai 		if (ret <= 0) {
126*43a7c3efSYan Zhai 			log_err("read(dev): %ld", ret);
127*43a7c3efSYan Zhai 			return -1;
128*43a7c3efSYan Zhai 		}
129*43a7c3efSYan Zhai 
130*43a7c3efSYan Zhai 		if (filter && filter(buf, ret) > 0)
131*43a7c3efSYan Zhai 			return 1;
132*43a7c3efSYan Zhai 	}
133*43a7c3efSYan Zhai 
134*43a7c3efSYan Zhai 	return 0;
135*43a7c3efSYan Zhai }
136*43a7c3efSYan Zhai 
137*43a7c3efSYan Zhai #endif /* __LWT_HELPERS_H */
138