Blame view

samples/bpf/test_map_in_map_user.c 4.42 KB
25763b3c8   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
2
3
  /*
   * Copyright (c) 2017 Facebook
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
4
5
6
7
8
9
10
11
12
   */
  #include <sys/resource.h>
  #include <sys/socket.h>
  #include <arpa/inet.h>
  #include <stdint.h>
  #include <assert.h>
  #include <errno.h>
  #include <stdlib.h>
  #include <stdio.h>
2bf3e2ef4   Jakub Kicinski   samples: bpf: inc...
13
  #include <bpf/bpf.h>
88795b4ad   Daniel T. Lee   samples: bpf: Ref...
14
15
16
  #include <bpf/libbpf.h>
  
  static int map_fd[7];
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  
  #define PORT_A		(map_fd[0])
  #define PORT_H		(map_fd[1])
  #define REG_RESULT_H	(map_fd[2])
  #define INLINE_RESULT_H	(map_fd[3])
  #define A_OF_PORT_A	(map_fd[4]) /* Test case #0 */
  #define H_OF_PORT_A	(map_fd[5]) /* Test case #1 */
  #define H_OF_PORT_H	(map_fd[6]) /* Test case #2 */
  
  static const char * const test_names[] = {
  	"Array of Array",
  	"Hash of Array",
  	"Hash of Hash",
  };
  
  #define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
a8744f252   Martin KaFai Lau   bpf: Add test for...
33
34
35
36
37
38
39
40
41
42
43
44
45
  static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
  {
  	struct bpf_map_info info = {};
  	uint32_t info_len = sizeof(info);
  	int ret, id;
  
  	ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
  	assert(!ret);
  
  	ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
  	assert(!ret);
  	assert(id == info.id);
  }
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
46
47
48
49
50
51
52
53
54
55
56
57
58
  static void populate_map(uint32_t port_key, int magic_result)
  {
  	int ret;
  
  	ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
  	assert(!ret);
  
  	ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
  				  BPF_NOEXIST);
  	assert(!ret);
  
  	ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
  	assert(!ret);
a8744f252   Martin KaFai Lau   bpf: Add test for...
59
  	check_map_id(PORT_A, A_OF_PORT_A, port_key);
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
60
61
62
  
  	ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
  	assert(!ret);
a8744f252   Martin KaFai Lau   bpf: Add test for...
63
  	check_map_id(PORT_A, H_OF_PORT_A, port_key);
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
64
65
66
  
  	ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
  	assert(!ret);
a8744f252   Martin KaFai Lau   bpf: Add test for...
67
  	check_map_id(PORT_H, H_OF_PORT_H, port_key);
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  }
  
  static void test_map_in_map(void)
  {
  	struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
  	uint32_t result_key = 0, port_key;
  	int result, inline_result;
  	int magic_result = 0xfaceb00c;
  	int ret;
  	int i;
  
  	port_key = rand() & 0x00FF;
  	populate_map(port_key, magic_result);
  
  	in6.sin6_addr.s6_addr16[0] = 0xdead;
  	in6.sin6_addr.s6_addr16[1] = 0xbeef;
  	in6.sin6_port = port_key;
  
  	for (i = 0; i < NR_TESTS; i++) {
  		printf("%s: ", test_names[i]);
  
  		in6.sin6_addr.s6_addr16[7] = i;
  		ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
  		assert(ret == -1 && errno == EBADF);
  
  		ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
  		assert(!ret);
  
  		ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
  					  &inline_result);
  		assert(!ret);
  
  		if (result != magic_result || inline_result != magic_result) {
  			printf("Error. result:%d inline_result:%d
  ",
  			       result, inline_result);
  			exit(1);
  		}
  
  		bpf_map_delete_elem(REG_RESULT_H, &result_key);
  		bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
  
  		printf("Pass
  ");
  	}
  }
  
  int main(int argc, char **argv)
  {
  	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
88795b4ad   Daniel T. Lee   samples: bpf: Ref...
118
119
120
  	struct bpf_link *link = NULL;
  	struct bpf_program *prog;
  	struct bpf_object *obj;
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
121
  	char filename[256];
88795b4ad   Daniel T. Lee   samples: bpf: Ref...
122
123
124
125
  	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  		perror("setrlimit(RLIMIT_MEMLOCK)");
  		return 1;
  	}
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
126
127
  
  	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
88795b4ad   Daniel T. Lee   samples: bpf: Ref...
128
129
130
131
132
133
  	obj = bpf_object__open_file(filename, NULL);
  	if (libbpf_get_error(obj)) {
  		fprintf(stderr, "ERROR: opening BPF object file failed
  ");
  		return 0;
  	}
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
134

88795b4ad   Daniel T. Lee   samples: bpf: Ref...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  	prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
  	if (!prog) {
  		printf("finding a prog in obj file failed
  ");
  		goto cleanup;
  	}
  
  	/* load BPF program */
  	if (bpf_object__load(obj)) {
  		fprintf(stderr, "ERROR: loading BPF object file failed
  ");
  		goto cleanup;
  	}
  
  	map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a");
  	map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h");
  	map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h");
  	map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h");
  	map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a");
  	map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a");
  	map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h");
  	if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 ||
  	    map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) {
  		fprintf(stderr, "ERROR: finding a map in obj file failed
  ");
  		goto cleanup;
  	}
  
  	link = bpf_program__attach(prog);
  	if (libbpf_get_error(link)) {
  		fprintf(stderr, "ERROR: bpf_program__attach failed
  ");
  		link = NULL;
  		goto cleanup;
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
169
170
171
  	}
  
  	test_map_in_map();
88795b4ad   Daniel T. Lee   samples: bpf: Ref...
172
173
174
  cleanup:
  	bpf_link__destroy(link);
  	bpf_object__close(obj);
fb30d4b71   Martin KaFai Lau   bpf: Add tests fo...
175
176
  	return 0;
  }