summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/linked_maps1.c
blob: 00bf1ca959862b6b892513d8f1e09b0758667942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

struct my_key { long x; };
struct my_value { long x; };

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, struct my_key);
	__type(value, struct my_value);
	__uint(max_entries, 16);
} map1 SEC(".maps");

 /* Matches map2 definition in linked_maps2.c. Order of the attributes doesn't
  * matter.
  */
typedef struct {
	__uint(max_entries, 8);
	__type(key, int);
	__type(value, int);
	__uint(type, BPF_MAP_TYPE_ARRAY);
} map2_t;

extern map2_t map2 SEC(".maps");

/* This should be the winning map definition, but we have no way of verifying,
 * so we just make sure that it links and works without errors
 */
struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, int);
	__uint(max_entries, 16);
} map_weak __weak SEC(".maps");

int output_first1;
int output_second1;
int output_weak1;

SEC("raw_tp/sys_enter")
int BPF_PROG(handler_enter1)
{
	/* update values with key = 1 */
	int key = 1, val = 1;
	struct my_key key_struct = { .x = 1 };
	struct my_value val_struct = { .x = 1000 };

	bpf_map_update_elem(&map1, &key_struct, &val_struct, 0);
	bpf_map_update_elem(&map2, &key, &val, 0);
	bpf_map_update_elem(&map_weak, &key, &val, 0);

	return 0;
}

SEC("raw_tp/sys_exit")
int BPF_PROG(handler_exit1)
{
	/* lookup values with key = 2, set in another file */
	int key = 2, *val;
	struct my_key key_struct = { .x = 2 };
	struct my_value *value_struct;

	value_struct = bpf_map_lookup_elem(&map1, &key_struct);
	if (value_struct)
		output_first1 = value_struct->x;

	val = bpf_map_lookup_elem(&map2, &key);
	if (val)
		output_second1 = *val;

	val = bpf_map_lookup_elem(&map_weak, &key);
	if (val)
		output_weak1 = *val;

	return 0;
}

char LICENSE[] SEC("license") = "GPL";