1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f74599f7SThomas Graf #include <linux/unistd.h>
3f74599f7SThomas Graf #include <linux/bpf.h>
4f74599f7SThomas Graf 
5f74599f7SThomas Graf #include <stdlib.h>
6f74599f7SThomas Graf #include <stdio.h>
7f74599f7SThomas Graf #include <unistd.h>
8f74599f7SThomas Graf #include <string.h>
9f74599f7SThomas Graf #include <errno.h>
10f74599f7SThomas Graf #include <arpa/inet.h>
11f74599f7SThomas Graf 
122bf3e2efSJakub Kicinski #include <bpf/bpf.h>
13f74599f7SThomas Graf #include "bpf_util.h"
14f74599f7SThomas Graf 
15f74599f7SThomas Graf #define MAX_INDEX 64
16f74599f7SThomas Graf #define MAX_STARS 38
17f74599f7SThomas Graf 
stars(char * str,long val,long max,int width)18f74599f7SThomas Graf static void stars(char *str, long val, long max, int width)
19f74599f7SThomas Graf {
20f74599f7SThomas Graf 	int i;
21f74599f7SThomas Graf 
22f74599f7SThomas Graf 	for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
23f74599f7SThomas Graf 		str[i] = '*';
24f74599f7SThomas Graf 	if (val > max)
25f74599f7SThomas Graf 		str[i - 1] = '+';
26f74599f7SThomas Graf 	str[i] = '\0';
27f74599f7SThomas Graf }
28f74599f7SThomas Graf 
main(int argc,char ** argv)29f74599f7SThomas Graf int main(int argc, char **argv)
30f74599f7SThomas Graf {
31f74599f7SThomas Graf 	unsigned int nr_cpus = bpf_num_possible_cpus();
32f74599f7SThomas Graf 	const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map";
33f74599f7SThomas Graf 	uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {};
34f74599f7SThomas Graf 	uint64_t key = 0, next_key, max_key = 0;
35f74599f7SThomas Graf 	char starstr[MAX_STARS];
36f74599f7SThomas Graf 	int i, map_fd;
37f74599f7SThomas Graf 
38f74599f7SThomas Graf 	map_fd = bpf_obj_get(map_filename);
39f74599f7SThomas Graf 	if (map_fd < 0) {
40f74599f7SThomas Graf 		fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
41f74599f7SThomas Graf 			map_filename, strerror(errno), errno);
42f74599f7SThomas Graf 		return -1;
43f74599f7SThomas Graf 	}
44f74599f7SThomas Graf 
45d40fc181SJoe Stringer 	while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
46f74599f7SThomas Graf 		if (next_key >= MAX_INDEX) {
47f74599f7SThomas Graf 			fprintf(stderr, "Key %lu out of bounds\n", next_key);
48f74599f7SThomas Graf 			continue;
49f74599f7SThomas Graf 		}
50f74599f7SThomas Graf 
51d40fc181SJoe Stringer 		bpf_map_lookup_elem(map_fd, &next_key, values);
52f74599f7SThomas Graf 
53f74599f7SThomas Graf 		sum = 0;
54f74599f7SThomas Graf 		for (i = 0; i < nr_cpus; i++)
55f74599f7SThomas Graf 			sum += values[i];
56f74599f7SThomas Graf 
57f74599f7SThomas Graf 		data[next_key] = sum;
58f74599f7SThomas Graf 		if (sum && next_key > max_key)
59f74599f7SThomas Graf 			max_key = next_key;
60f74599f7SThomas Graf 
61f74599f7SThomas Graf 		if (sum > max_value)
62f74599f7SThomas Graf 			max_value = sum;
63f74599f7SThomas Graf 
64f74599f7SThomas Graf 		key = next_key;
65f74599f7SThomas Graf 	}
66f74599f7SThomas Graf 
67f74599f7SThomas Graf 	for (i = 1; i <= max_key + 1; i++) {
68f74599f7SThomas Graf 		stars(starstr, data[i - 1], max_value, MAX_STARS);
69f74599f7SThomas Graf 		printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
70f74599f7SThomas Graf 		       (1l << i) >> 1, (1l << i) - 1, data[i - 1],
71f74599f7SThomas Graf 		       MAX_STARS, starstr);
72f74599f7SThomas Graf 	}
73f74599f7SThomas Graf 
74f74599f7SThomas Graf 	close(map_fd);
75f74599f7SThomas Graf 
76f74599f7SThomas Graf 	return 0;
77f74599f7SThomas Graf }
78