fix race condition in cor_announce_send_stop
[cor.git] / samples / bpf / test_map_in_map_user.c
blobeb29bcb76f3fb573f5de41b7357f5fb9388acdd7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2017 Facebook
4 */
5 #include <sys/resource.h>
6 #include <sys/socket.h>
7 #include <arpa/inet.h>
8 #include <stdint.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <bpf/bpf.h>
14 #include "bpf_load.h"
16 #define PORT_A (map_fd[0])
17 #define PORT_H (map_fd[1])
18 #define REG_RESULT_H (map_fd[2])
19 #define INLINE_RESULT_H (map_fd[3])
20 #define A_OF_PORT_A (map_fd[4]) /* Test case #0 */
21 #define H_OF_PORT_A (map_fd[5]) /* Test case #1 */
22 #define H_OF_PORT_H (map_fd[6]) /* Test case #2 */
24 static const char * const test_names[] = {
25 "Array of Array",
26 "Hash of Array",
27 "Hash of Hash",
30 #define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
32 static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
34 struct bpf_map_info info = {};
35 uint32_t info_len = sizeof(info);
36 int ret, id;
38 ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
39 assert(!ret);
41 ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
42 assert(!ret);
43 assert(id == info.id);
46 static void populate_map(uint32_t port_key, int magic_result)
48 int ret;
50 ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
51 assert(!ret);
53 ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
54 BPF_NOEXIST);
55 assert(!ret);
57 ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
58 assert(!ret);
59 check_map_id(PORT_A, A_OF_PORT_A, port_key);
61 ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
62 assert(!ret);
63 check_map_id(PORT_A, H_OF_PORT_A, port_key);
65 ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
66 assert(!ret);
67 check_map_id(PORT_H, H_OF_PORT_H, port_key);
70 static void test_map_in_map(void)
72 struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
73 uint32_t result_key = 0, port_key;
74 int result, inline_result;
75 int magic_result = 0xfaceb00c;
76 int ret;
77 int i;
79 port_key = rand() & 0x00FF;
80 populate_map(port_key, magic_result);
82 in6.sin6_addr.s6_addr16[0] = 0xdead;
83 in6.sin6_addr.s6_addr16[1] = 0xbeef;
84 in6.sin6_port = port_key;
86 for (i = 0; i < NR_TESTS; i++) {
87 printf("%s: ", test_names[i]);
89 in6.sin6_addr.s6_addr16[7] = i;
90 ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
91 assert(ret == -1 && errno == EBADF);
93 ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
94 assert(!ret);
96 ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
97 &inline_result);
98 assert(!ret);
100 if (result != magic_result || inline_result != magic_result) {
101 printf("Error. result:%d inline_result:%d\n",
102 result, inline_result);
103 exit(1);
106 bpf_map_delete_elem(REG_RESULT_H, &result_key);
107 bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
109 printf("Pass\n");
113 int main(int argc, char **argv)
115 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
116 char filename[256];
118 assert(!setrlimit(RLIMIT_MEMLOCK, &r));
120 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
122 if (load_bpf_file(filename)) {
123 printf("%s", bpf_log_buf);
124 return 1;
127 test_map_in_map();
129 return 0;