xref: /openbmc/linux/samples/bpf/sockex3_user.c (revision a8744f25)
1530b2c86SAlexei Starovoitov #include <stdio.h>
2530b2c86SAlexei Starovoitov #include <assert.h>
3530b2c86SAlexei Starovoitov #include <linux/bpf.h>
4530b2c86SAlexei Starovoitov #include "libbpf.h"
5530b2c86SAlexei Starovoitov #include "bpf_load.h"
69899694aSJoe Stringer #include "sock_example.h"
7530b2c86SAlexei Starovoitov #include <unistd.h>
8530b2c86SAlexei Starovoitov #include <arpa/inet.h>
9eb88d585SWilliam Tu #include <sys/resource.h>
10530b2c86SAlexei Starovoitov 
11a8744f25SMartin KaFai Lau #define PARSE_IP 3
12a8744f25SMartin KaFai Lau #define PARSE_IP_PROG_FD (prog_fd[0])
13a8744f25SMartin KaFai Lau #define PROG_ARRAY_FD (map_fd[0])
14a8744f25SMartin KaFai Lau 
152b064fffSNaveen N. Rao struct bpf_flow_keys {
16530b2c86SAlexei Starovoitov 	__be32 src;
17530b2c86SAlexei Starovoitov 	__be32 dst;
18530b2c86SAlexei Starovoitov 	union {
19530b2c86SAlexei Starovoitov 		__be32 ports;
20530b2c86SAlexei Starovoitov 		__be16 port16[2];
21530b2c86SAlexei Starovoitov 	};
22530b2c86SAlexei Starovoitov 	__u32 ip_proto;
23530b2c86SAlexei Starovoitov };
24530b2c86SAlexei Starovoitov 
25530b2c86SAlexei Starovoitov struct pair {
26530b2c86SAlexei Starovoitov 	__u64 packets;
27530b2c86SAlexei Starovoitov 	__u64 bytes;
28530b2c86SAlexei Starovoitov };
29530b2c86SAlexei Starovoitov 
30530b2c86SAlexei Starovoitov int main(int argc, char **argv)
31530b2c86SAlexei Starovoitov {
32eb88d585SWilliam Tu 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
33530b2c86SAlexei Starovoitov 	char filename[256];
34530b2c86SAlexei Starovoitov 	FILE *f;
35a8744f25SMartin KaFai Lau 	int i, sock, err, id, key = PARSE_IP;
36a8744f25SMartin KaFai Lau 	struct bpf_prog_info info = {};
37a8744f25SMartin KaFai Lau 	uint32_t info_len = sizeof(info);
38530b2c86SAlexei Starovoitov 
39530b2c86SAlexei Starovoitov 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
40eb88d585SWilliam Tu 	setrlimit(RLIMIT_MEMLOCK, &r);
41530b2c86SAlexei Starovoitov 
42530b2c86SAlexei Starovoitov 	if (load_bpf_file(filename)) {
43530b2c86SAlexei Starovoitov 		printf("%s", bpf_log_buf);
44530b2c86SAlexei Starovoitov 		return 1;
45530b2c86SAlexei Starovoitov 	}
46530b2c86SAlexei Starovoitov 
47a8744f25SMartin KaFai Lau 	/* Test fd array lookup which returns the id of the bpf_prog */
48a8744f25SMartin KaFai Lau 	err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len);
49a8744f25SMartin KaFai Lau 	assert(!err);
50a8744f25SMartin KaFai Lau 	err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id);
51a8744f25SMartin KaFai Lau 	assert(!err);
52a8744f25SMartin KaFai Lau 	assert(id == info.id);
53a8744f25SMartin KaFai Lau 
54530b2c86SAlexei Starovoitov 	sock = open_raw_sock("lo");
55530b2c86SAlexei Starovoitov 
56530b2c86SAlexei Starovoitov 	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4],
57530b2c86SAlexei Starovoitov 			  sizeof(__u32)) == 0);
58530b2c86SAlexei Starovoitov 
59530b2c86SAlexei Starovoitov 	if (argc > 1)
60530b2c86SAlexei Starovoitov 		f = popen("ping -c5 localhost", "r");
61530b2c86SAlexei Starovoitov 	else
62530b2c86SAlexei Starovoitov 		f = popen("netperf -l 4 localhost", "r");
63530b2c86SAlexei Starovoitov 	(void) f;
64530b2c86SAlexei Starovoitov 
65530b2c86SAlexei Starovoitov 	for (i = 0; i < 5; i++) {
662b064fffSNaveen N. Rao 		struct bpf_flow_keys key = {}, next_key;
67530b2c86SAlexei Starovoitov 		struct pair value;
68530b2c86SAlexei Starovoitov 
69530b2c86SAlexei Starovoitov 		sleep(1);
70530b2c86SAlexei Starovoitov 		printf("IP     src.port -> dst.port               bytes      packets\n");
71d40fc181SJoe Stringer 		while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) {
72d40fc181SJoe Stringer 			bpf_map_lookup_elem(map_fd[2], &next_key, &value);
73530b2c86SAlexei Starovoitov 			printf("%s.%05d -> %s.%05d %12lld %12lld\n",
74530b2c86SAlexei Starovoitov 			       inet_ntoa((struct in_addr){htonl(next_key.src)}),
75530b2c86SAlexei Starovoitov 			       next_key.port16[0],
76530b2c86SAlexei Starovoitov 			       inet_ntoa((struct in_addr){htonl(next_key.dst)}),
77530b2c86SAlexei Starovoitov 			       next_key.port16[1],
78530b2c86SAlexei Starovoitov 			       value.bytes, value.packets);
79530b2c86SAlexei Starovoitov 			key = next_key;
80530b2c86SAlexei Starovoitov 		}
81530b2c86SAlexei Starovoitov 	}
82530b2c86SAlexei Starovoitov 	return 0;
83530b2c86SAlexei Starovoitov }
84