xref: /openbmc/linux/samples/bpf/sockex3_user.c (revision bc1a8597)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2530b2c86SAlexei Starovoitov #include <stdio.h>
3530b2c86SAlexei Starovoitov #include <assert.h>
42bf3e2efSJakub Kicinski #include <bpf/bpf.h>
5bc1a8597SDaniel T. Lee #include <bpf/libbpf.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 
1132c00979SPrashant Bhole struct flow_key_record {
12530b2c86SAlexei Starovoitov 	__be32 src;
13530b2c86SAlexei Starovoitov 	__be32 dst;
14530b2c86SAlexei Starovoitov 	union {
15530b2c86SAlexei Starovoitov 		__be32 ports;
16530b2c86SAlexei Starovoitov 		__be16 port16[2];
17530b2c86SAlexei Starovoitov 	};
18530b2c86SAlexei Starovoitov 	__u32 ip_proto;
19530b2c86SAlexei Starovoitov };
20530b2c86SAlexei Starovoitov 
21530b2c86SAlexei Starovoitov struct pair {
22530b2c86SAlexei Starovoitov 	__u64 packets;
23530b2c86SAlexei Starovoitov 	__u64 bytes;
24530b2c86SAlexei Starovoitov };
25530b2c86SAlexei Starovoitov 
26530b2c86SAlexei Starovoitov int main(int argc, char **argv)
27530b2c86SAlexei Starovoitov {
28bc1a8597SDaniel T. Lee 	int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd;
29eb88d585SWilliam Tu 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
30bc1a8597SDaniel T. Lee 	struct bpf_program *prog;
31bc1a8597SDaniel T. Lee 	struct bpf_object *obj;
32530b2c86SAlexei Starovoitov 	char filename[256];
33bc1a8597SDaniel T. Lee 	const char *title;
34530b2c86SAlexei Starovoitov 	FILE *f;
35530b2c86SAlexei Starovoitov 
36530b2c86SAlexei Starovoitov 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
37eb88d585SWilliam Tu 	setrlimit(RLIMIT_MEMLOCK, &r);
38530b2c86SAlexei Starovoitov 
39bc1a8597SDaniel T. Lee 	obj = bpf_object__open_file(filename, NULL);
40bc1a8597SDaniel T. Lee 	if (libbpf_get_error(obj)) {
41bc1a8597SDaniel T. Lee 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
42bc1a8597SDaniel T. Lee 		return 0;
43530b2c86SAlexei Starovoitov 	}
44530b2c86SAlexei Starovoitov 
45bc1a8597SDaniel T. Lee 	/* load BPF program */
46bc1a8597SDaniel T. Lee 	if (bpf_object__load(obj)) {
47bc1a8597SDaniel T. Lee 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
48bc1a8597SDaniel T. Lee 		goto cleanup;
49bc1a8597SDaniel T. Lee 	}
50bc1a8597SDaniel T. Lee 
51bc1a8597SDaniel T. Lee 	jmp_table_fd = bpf_object__find_map_fd_by_name(obj, "jmp_table");
52bc1a8597SDaniel T. Lee 	hash_map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
53bc1a8597SDaniel T. Lee 	if (jmp_table_fd < 0 || hash_map_fd < 0) {
54bc1a8597SDaniel T. Lee 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
55bc1a8597SDaniel T. Lee 		goto cleanup;
56bc1a8597SDaniel T. Lee 	}
57bc1a8597SDaniel T. Lee 
58bc1a8597SDaniel T. Lee 	bpf_object__for_each_program(prog, obj) {
59bc1a8597SDaniel T. Lee 		fd = bpf_program__fd(prog);
60bc1a8597SDaniel T. Lee 
61bc1a8597SDaniel T. Lee 		title = bpf_program__title(prog, false);
62bc1a8597SDaniel T. Lee 		if (sscanf(title, "socket/%d", &key) != 1) {
63bc1a8597SDaniel T. Lee 			fprintf(stderr, "ERROR: finding prog failed\n");
64bc1a8597SDaniel T. Lee 			goto cleanup;
65bc1a8597SDaniel T. Lee 		}
66bc1a8597SDaniel T. Lee 
67bc1a8597SDaniel T. Lee 		if (key == 0)
68bc1a8597SDaniel T. Lee 			main_prog_fd = fd;
69bc1a8597SDaniel T. Lee 		else
70bc1a8597SDaniel T. Lee 			bpf_map_update_elem(jmp_table_fd, &key, &fd, BPF_ANY);
71bc1a8597SDaniel T. Lee 	}
72a8744f25SMartin KaFai Lau 
73530b2c86SAlexei Starovoitov 	sock = open_raw_sock("lo");
74530b2c86SAlexei Starovoitov 
75bc1a8597SDaniel T. Lee 	/* attach BPF program to socket */
76bc1a8597SDaniel T. Lee 	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &main_prog_fd,
77530b2c86SAlexei Starovoitov 			  sizeof(__u32)) == 0);
78530b2c86SAlexei Starovoitov 
79530b2c86SAlexei Starovoitov 	if (argc > 1)
805c3cf87dSJakub Kicinski 		f = popen("ping -4 -c5 localhost", "r");
81530b2c86SAlexei Starovoitov 	else
82530b2c86SAlexei Starovoitov 		f = popen("netperf -l 4 localhost", "r");
83530b2c86SAlexei Starovoitov 	(void) f;
84530b2c86SAlexei Starovoitov 
85530b2c86SAlexei Starovoitov 	for (i = 0; i < 5; i++) {
8632c00979SPrashant Bhole 		struct flow_key_record key = {}, next_key;
87530b2c86SAlexei Starovoitov 		struct pair value;
88530b2c86SAlexei Starovoitov 
89530b2c86SAlexei Starovoitov 		sleep(1);
90530b2c86SAlexei Starovoitov 		printf("IP     src.port -> dst.port               bytes      packets\n");
91bc1a8597SDaniel T. Lee 		while (bpf_map_get_next_key(hash_map_fd, &key, &next_key) == 0) {
92bc1a8597SDaniel T. Lee 			bpf_map_lookup_elem(hash_map_fd, &next_key, &value);
93530b2c86SAlexei Starovoitov 			printf("%s.%05d -> %s.%05d %12lld %12lld\n",
94530b2c86SAlexei Starovoitov 			       inet_ntoa((struct in_addr){htonl(next_key.src)}),
95530b2c86SAlexei Starovoitov 			       next_key.port16[0],
96530b2c86SAlexei Starovoitov 			       inet_ntoa((struct in_addr){htonl(next_key.dst)}),
97530b2c86SAlexei Starovoitov 			       next_key.port16[1],
98530b2c86SAlexei Starovoitov 			       value.bytes, value.packets);
99530b2c86SAlexei Starovoitov 			key = next_key;
100530b2c86SAlexei Starovoitov 		}
101530b2c86SAlexei Starovoitov 	}
102bc1a8597SDaniel T. Lee 
103bc1a8597SDaniel T. Lee cleanup:
104bc1a8597SDaniel T. Lee 	bpf_object__close(obj);
105530b2c86SAlexei Starovoitov 	return 0;
106530b2c86SAlexei Starovoitov }
107