Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2017 Facebook
  4 */
 
  5#include <sys/socket.h>
  6#include <arpa/inet.h>
  7#include <stdint.h>
  8#include <assert.h>
  9#include <errno.h>
 10#include <stdlib.h>
 11#include <stdio.h>
 12#include <bpf/bpf.h>
 13#include <bpf/libbpf.h>
 14
 15#include "bpf_util.h"
 16
 17static int map_fd[7];
 18
 19#define PORT_A		(map_fd[0])
 20#define PORT_H		(map_fd[1])
 21#define REG_RESULT_H	(map_fd[2])
 22#define INLINE_RESULT_H	(map_fd[3])
 23#define A_OF_PORT_A	(map_fd[4]) /* Test case #0 */
 24#define H_OF_PORT_A	(map_fd[5]) /* Test case #1 */
 25#define H_OF_PORT_H	(map_fd[6]) /* Test case #2 */
 26
 27static const char * const test_names[] = {
 28	"Array of Array",
 29	"Hash of Array",
 30	"Hash of Hash",
 31};
 32
 33#define NR_TESTS ARRAY_SIZE(test_names)
 34
 35static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
 36{
 37	struct bpf_map_info info = {};
 38	uint32_t info_len = sizeof(info);
 39	int ret, id;
 40
 41	ret = bpf_map_get_info_by_fd(inner_map_fd, &info, &info_len);
 42	assert(!ret);
 43
 44	ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
 45	assert(!ret);
 46	assert(id == info.id);
 47}
 48
 49static void populate_map(uint32_t port_key, int magic_result)
 50{
 51	int ret;
 52
 53	ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
 54	assert(!ret);
 55
 56	ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
 57				  BPF_NOEXIST);
 58	assert(!ret);
 59
 60	ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
 61	assert(!ret);
 62	check_map_id(PORT_A, A_OF_PORT_A, port_key);
 63
 64	ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
 65	assert(!ret);
 66	check_map_id(PORT_A, H_OF_PORT_A, port_key);
 67
 68	ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
 69	assert(!ret);
 70	check_map_id(PORT_H, H_OF_PORT_H, port_key);
 71}
 72
 73static void test_map_in_map(void)
 74{
 75	struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
 76	uint32_t result_key = 0, port_key;
 77	int result, inline_result;
 78	int magic_result = 0xfaceb00c;
 79	int ret;
 80	int i;
 81
 82	port_key = rand() & 0x00FF;
 83	populate_map(port_key, magic_result);
 84
 85	in6.sin6_addr.s6_addr16[0] = 0xdead;
 86	in6.sin6_addr.s6_addr16[1] = 0xbeef;
 87	in6.sin6_port = port_key;
 88
 89	for (i = 0; i < NR_TESTS; i++) {
 90		printf("%s: ", test_names[i]);
 91
 92		in6.sin6_addr.s6_addr16[7] = i;
 93		ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
 94		assert(ret == -1 && errno == EBADF);
 95
 96		ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
 97		assert(!ret);
 98
 99		ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
100					  &inline_result);
101		assert(!ret);
102
103		if (result != magic_result || inline_result != magic_result) {
104			printf("Error. result:%d inline_result:%d\n",
105			       result, inline_result);
106			exit(1);
107		}
108
109		bpf_map_delete_elem(REG_RESULT_H, &result_key);
110		bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
111
112		printf("Pass\n");
113	}
114}
115
116int main(int argc, char **argv)
117{
118	struct bpf_link *link = NULL;
119	struct bpf_program *prog;
120	struct bpf_object *obj;
121	char filename[256];
122
123	snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
124	obj = bpf_object__open_file(filename, NULL);
125	if (libbpf_get_error(obj)) {
126		fprintf(stderr, "ERROR: opening BPF object file failed\n");
127		return 0;
128	}
129
130	prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
131	if (!prog) {
132		printf("finding a prog in obj file failed\n");
133		goto cleanup;
134	}
135
136	/* load BPF program */
137	if (bpf_object__load(obj)) {
138		fprintf(stderr, "ERROR: loading BPF object file failed\n");
139		goto cleanup;
140	}
141
142	map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a");
143	map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h");
144	map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h");
145	map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h");
146	map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a");
147	map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a");
148	map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h");
149	if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 ||
150	    map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) {
151		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
152		goto cleanup;
153	}
154
155	link = bpf_program__attach(prog);
156	if (libbpf_get_error(link)) {
157		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
158		link = NULL;
159		goto cleanup;
160	}
161
162	test_map_in_map();
163
164cleanup:
165	bpf_link__destroy(link);
166	bpf_object__close(obj);
167	return 0;
168}
v5.4
  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"
 
 
 
 
 15
 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 */
 23
 24static const char * const test_names[] = {
 25	"Array of Array",
 26	"Hash of Array",
 27	"Hash of Hash",
 28};
 29
 30#define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
 31
 32static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
 33{
 34	struct bpf_map_info info = {};
 35	uint32_t info_len = sizeof(info);
 36	int ret, id;
 37
 38	ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
 39	assert(!ret);
 40
 41	ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
 42	assert(!ret);
 43	assert(id == info.id);
 44}
 45
 46static void populate_map(uint32_t port_key, int magic_result)
 47{
 48	int ret;
 49
 50	ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
 51	assert(!ret);
 52
 53	ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
 54				  BPF_NOEXIST);
 55	assert(!ret);
 56
 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);
 60
 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);
 64
 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);
 68}
 69
 70static void test_map_in_map(void)
 71{
 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;
 78
 79	port_key = rand() & 0x00FF;
 80	populate_map(port_key, magic_result);
 81
 82	in6.sin6_addr.s6_addr16[0] = 0xdead;
 83	in6.sin6_addr.s6_addr16[1] = 0xbeef;
 84	in6.sin6_port = port_key;
 85
 86	for (i = 0; i < NR_TESTS; i++) {
 87		printf("%s: ", test_names[i]);
 88
 89		in6.sin6_addr.s6_addr16[7] = i;
 90		ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
 91		assert(ret == -1 && errno == EBADF);
 92
 93		ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
 94		assert(!ret);
 95
 96		ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
 97					  &inline_result);
 98		assert(!ret);
 99
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);
104		}
105
106		bpf_map_delete_elem(REG_RESULT_H, &result_key);
107		bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
108
109		printf("Pass\n");
110	}
111}
112
113int main(int argc, char **argv)
114{
115	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 
 
116	char filename[256];
117
118	assert(!setrlimit(RLIMIT_MEMLOCK, &r));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
120	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
 
 
 
 
 
 
 
 
 
 
121
122	if (load_bpf_file(filename)) {
123		printf("%s", bpf_log_buf);
124		return 1;
 
 
125	}
126
127	test_map_in_map();
128
 
 
 
129	return 0;
130}