Skip to content

Commit 36b5d47

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: samples/bpf: Split off legacy stuff from bpf_helpers.h
Split off few legacy things from bpf_helpers.h into separate bpf_legacy.h file: - load_{byte|half|word}; - remove extra inner_idx and numa_node fields from bpf_map_def and introduce bpf_map_def_legacy for use in samples; - move BPF_ANNOTATE_KV_PAIR into bpf_legacy.h. Adjust samples and selftests accordingly by either including bpf_legacy.h and using bpf_map_def_legacy, or switching to BTF-defined maps altogether. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent cf0e971 commit 36b5d47

File tree

14 files changed

+91
-70
lines changed

14 files changed

+91
-70
lines changed

samples/bpf/hbm_kern.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,18 @@
5959
#define BYTES_PER_NS(delta, rate) ((((u64)(delta)) * (rate)) >> 20)
6060
#define BYTES_TO_NS(bytes, rate) div64_u64(((u64)(bytes)) << 20, (u64)(rate))
6161

62-
struct bpf_map_def SEC("maps") queue_state = {
63-
.type = BPF_MAP_TYPE_CGROUP_STORAGE,
64-
.key_size = sizeof(struct bpf_cgroup_storage_key),
65-
.value_size = sizeof(struct hbm_vqueue),
66-
};
67-
BPF_ANNOTATE_KV_PAIR(queue_state, struct bpf_cgroup_storage_key,
68-
struct hbm_vqueue);
69-
70-
struct bpf_map_def SEC("maps") queue_stats = {
71-
.type = BPF_MAP_TYPE_ARRAY,
72-
.key_size = sizeof(u32),
73-
.value_size = sizeof(struct hbm_queue_stats),
74-
.max_entries = 1,
75-
};
76-
BPF_ANNOTATE_KV_PAIR(queue_stats, int, struct hbm_queue_stats);
62+
struct {
63+
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
64+
__type(key, struct bpf_cgroup_storage_key);
65+
__type(value, struct hbm_vqueue);
66+
} queue_state SEC(".maps");
67+
68+
struct {
69+
__uint(type, BPF_MAP_TYPE_ARRAY);
70+
__uint(max_entries, 1);
71+
__type(key, u32);
72+
__type(value, struct hvm_queue_stats);
73+
} queue_stats SEC(".maps");
7774

7875
struct hbm_pkt_info {
7976
int cwnd;

samples/bpf/map_perf_test_kern.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,34 @@
99
#include <linux/version.h>
1010
#include <uapi/linux/bpf.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_legacy.h"
1213

1314
#define MAX_ENTRIES 1000
1415
#define MAX_NR_CPUS 1024
1516

16-
struct bpf_map_def SEC("maps") hash_map = {
17+
struct bpf_map_def_legacy SEC("maps") hash_map = {
1718
.type = BPF_MAP_TYPE_HASH,
1819
.key_size = sizeof(u32),
1920
.value_size = sizeof(long),
2021
.max_entries = MAX_ENTRIES,
2122
};
2223

23-
struct bpf_map_def SEC("maps") lru_hash_map = {
24+
struct bpf_map_def_legacy SEC("maps") lru_hash_map = {
2425
.type = BPF_MAP_TYPE_LRU_HASH,
2526
.key_size = sizeof(u32),
2627
.value_size = sizeof(long),
2728
.max_entries = 10000,
2829
};
2930

30-
struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
31+
struct bpf_map_def_legacy SEC("maps") nocommon_lru_hash_map = {
3132
.type = BPF_MAP_TYPE_LRU_HASH,
3233
.key_size = sizeof(u32),
3334
.value_size = sizeof(long),
3435
.max_entries = 10000,
3536
.map_flags = BPF_F_NO_COMMON_LRU,
3637
};
3738

38-
struct bpf_map_def SEC("maps") inner_lru_hash_map = {
39+
struct bpf_map_def_legacy SEC("maps") inner_lru_hash_map = {
3940
.type = BPF_MAP_TYPE_LRU_HASH,
4041
.key_size = sizeof(u32),
4142
.value_size = sizeof(long),
@@ -44,51 +45,51 @@ struct bpf_map_def SEC("maps") inner_lru_hash_map = {
4445
.numa_node = 0,
4546
};
4647

47-
struct bpf_map_def SEC("maps") array_of_lru_hashs = {
48+
struct bpf_map_def_legacy SEC("maps") array_of_lru_hashs = {
4849
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
4950
.key_size = sizeof(u32),
5051
.max_entries = MAX_NR_CPUS,
5152
};
5253

53-
struct bpf_map_def SEC("maps") percpu_hash_map = {
54+
struct bpf_map_def_legacy SEC("maps") percpu_hash_map = {
5455
.type = BPF_MAP_TYPE_PERCPU_HASH,
5556
.key_size = sizeof(u32),
5657
.value_size = sizeof(long),
5758
.max_entries = MAX_ENTRIES,
5859
};
5960

60-
struct bpf_map_def SEC("maps") hash_map_alloc = {
61+
struct bpf_map_def_legacy SEC("maps") hash_map_alloc = {
6162
.type = BPF_MAP_TYPE_HASH,
6263
.key_size = sizeof(u32),
6364
.value_size = sizeof(long),
6465
.max_entries = MAX_ENTRIES,
6566
.map_flags = BPF_F_NO_PREALLOC,
6667
};
6768

68-
struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
69+
struct bpf_map_def_legacy SEC("maps") percpu_hash_map_alloc = {
6970
.type = BPF_MAP_TYPE_PERCPU_HASH,
7071
.key_size = sizeof(u32),
7172
.value_size = sizeof(long),
7273
.max_entries = MAX_ENTRIES,
7374
.map_flags = BPF_F_NO_PREALLOC,
7475
};
7576

76-
struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
77+
struct bpf_map_def_legacy SEC("maps") lpm_trie_map_alloc = {
7778
.type = BPF_MAP_TYPE_LPM_TRIE,
7879
.key_size = 8,
7980
.value_size = sizeof(long),
8081
.max_entries = 10000,
8182
.map_flags = BPF_F_NO_PREALLOC,
8283
};
8384

84-
struct bpf_map_def SEC("maps") array_map = {
85+
struct bpf_map_def_legacy SEC("maps") array_map = {
8586
.type = BPF_MAP_TYPE_ARRAY,
8687
.key_size = sizeof(u32),
8788
.value_size = sizeof(long),
8889
.max_entries = MAX_ENTRIES,
8990
};
9091

91-
struct bpf_map_def SEC("maps") lru_hash_lookup_map = {
92+
struct bpf_map_def_legacy SEC("maps") lru_hash_lookup_map = {
9293
.type = BPF_MAP_TYPE_LRU_HASH,
9394
.key_size = sizeof(u32),
9495
.value_size = sizeof(long),

samples/bpf/parse_ldabs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/udp.h>
1313
#include <uapi/linux/bpf.h>
1414
#include "bpf_helpers.h"
15+
#include "bpf_legacy.h"
1516

1617
#define DEFAULT_PKTGEN_UDP_PORT 9
1718
#define IP_MF 0x2000

samples/bpf/sockex1_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <uapi/linux/if_packet.h>
44
#include <uapi/linux/ip.h>
55
#include "bpf_helpers.h"
6+
#include "bpf_legacy.h"
67

78
struct bpf_map_def SEC("maps") my_map = {
89
.type = BPF_MAP_TYPE_ARRAY,

samples/bpf/sockex2_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <uapi/linux/bpf.h>
22
#include "bpf_helpers.h"
3+
#include "bpf_legacy.h"
34
#include <uapi/linux/in.h>
45
#include <uapi/linux/if.h>
56
#include <uapi/linux/if_ether.h>

samples/bpf/sockex3_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include <uapi/linux/bpf.h>
88
#include "bpf_helpers.h"
9+
#include "bpf_legacy.h"
910
#include <uapi/linux/in.h>
1011
#include <uapi/linux/if.h>
1112
#include <uapi/linux/if_ether.h>

samples/bpf/tcbpf1_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <uapi/linux/filter.h>
99
#include <uapi/linux/pkt_cls.h>
1010
#include "bpf_helpers.h"
11+
#include "bpf_legacy.h"
1112

1213
/* compiler workaround */
1314
#define _htonl __builtin_bswap32

samples/bpf/test_map_in_map_kern.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,60 @@
1111
#include <uapi/linux/bpf.h>
1212
#include <uapi/linux/in6.h>
1313
#include "bpf_helpers.h"
14+
#include "bpf_legacy.h"
1415

1516
#define MAX_NR_PORTS 65536
1617

1718
/* map #0 */
18-
struct bpf_map_def SEC("maps") port_a = {
19+
struct bpf_map_def_legacy SEC("maps") port_a = {
1920
.type = BPF_MAP_TYPE_ARRAY,
2021
.key_size = sizeof(u32),
2122
.value_size = sizeof(int),
2223
.max_entries = MAX_NR_PORTS,
2324
};
2425

2526
/* map #1 */
26-
struct bpf_map_def SEC("maps") port_h = {
27+
struct bpf_map_def_legacy SEC("maps") port_h = {
2728
.type = BPF_MAP_TYPE_HASH,
2829
.key_size = sizeof(u32),
2930
.value_size = sizeof(int),
3031
.max_entries = 1,
3132
};
3233

3334
/* map #2 */
34-
struct bpf_map_def SEC("maps") reg_result_h = {
35+
struct bpf_map_def_legacy SEC("maps") reg_result_h = {
3536
.type = BPF_MAP_TYPE_HASH,
3637
.key_size = sizeof(u32),
3738
.value_size = sizeof(int),
3839
.max_entries = 1,
3940
};
4041

4142
/* map #3 */
42-
struct bpf_map_def SEC("maps") inline_result_h = {
43+
struct bpf_map_def_legacy SEC("maps") inline_result_h = {
4344
.type = BPF_MAP_TYPE_HASH,
4445
.key_size = sizeof(u32),
4546
.value_size = sizeof(int),
4647
.max_entries = 1,
4748
};
4849

4950
/* map #4 */ /* Test case #0 */
50-
struct bpf_map_def SEC("maps") a_of_port_a = {
51+
struct bpf_map_def_legacy SEC("maps") a_of_port_a = {
5152
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
5253
.key_size = sizeof(u32),
5354
.inner_map_idx = 0, /* map_fd[0] is port_a */
5455
.max_entries = MAX_NR_PORTS,
5556
};
5657

5758
/* map #5 */ /* Test case #1 */
58-
struct bpf_map_def SEC("maps") h_of_port_a = {
59+
struct bpf_map_def_legacy SEC("maps") h_of_port_a = {
5960
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
6061
.key_size = sizeof(u32),
6162
.inner_map_idx = 0, /* map_fd[0] is port_a */
6263
.max_entries = 1,
6364
};
6465

6566
/* map #6 */ /* Test case #2 */
66-
struct bpf_map_def SEC("maps") h_of_port_h = {
67+
struct bpf_map_def_legacy SEC("maps") h_of_port_h = {
6768
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
6869
.key_size = sizeof(u32),
6970
.inner_map_idx = 1, /* map_fd[1] is port_h */

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,17 @@
2121
*/
2222
#define SEC(NAME) __attribute__((section(NAME), used))
2323

24-
/* llvm builtin functions that eBPF C program may use to
25-
* emit BPF_LD_ABS and BPF_LD_IND instructions
26-
*/
27-
struct sk_buff;
28-
unsigned long long load_byte(void *skb,
29-
unsigned long long off) asm("llvm.bpf.load.byte");
30-
unsigned long long load_half(void *skb,
31-
unsigned long long off) asm("llvm.bpf.load.half");
32-
unsigned long long load_word(void *skb,
33-
unsigned long long off) asm("llvm.bpf.load.word");
34-
3524
/* a helper structure used by eBPF C program
36-
* to describe map attributes to elf_bpf loader
25+
* to describe BPF map attributes to libbpf loader
3726
*/
3827
struct bpf_map_def {
3928
unsigned int type;
4029
unsigned int key_size;
4130
unsigned int value_size;
4231
unsigned int max_entries;
4332
unsigned int map_flags;
44-
unsigned int inner_map_idx;
45-
unsigned int numa_node;
4633
};
4734

48-
#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
49-
struct ____btf_map_##name { \
50-
type_key key; \
51-
type_val value; \
52-
}; \
53-
struct ____btf_map_##name \
54-
__attribute__ ((section(".maps." #name), used)) \
55-
____btf_map_##name = { }
56-
5735
/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
5836
#if defined(__TARGET_ARCH_x86)
5937
#define bpf_target_x86
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2+
#ifndef __BPF_LEGACY__
3+
#define __BPF_LEGACY__
4+
5+
/*
6+
* legacy bpf_map_def with extra fields supported only by bpf_load(), do not
7+
* use outside of samples/bpf
8+
*/
9+
struct bpf_map_def_legacy {
10+
unsigned int type;
11+
unsigned int key_size;
12+
unsigned int value_size;
13+
unsigned int max_entries;
14+
unsigned int map_flags;
15+
unsigned int inner_map_idx;
16+
unsigned int numa_node;
17+
};
18+
19+
#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
20+
struct ____btf_map_##name { \
21+
type_key key; \
22+
type_val value; \
23+
}; \
24+
struct ____btf_map_##name \
25+
__attribute__ ((section(".maps." #name), used)) \
26+
____btf_map_##name = { }
27+
28+
/* llvm builtin functions that eBPF C program may use to
29+
* emit BPF_LD_ABS and BPF_LD_IND instructions
30+
*/
31+
unsigned long long load_byte(void *skb,
32+
unsigned long long off) asm("llvm.bpf.load.byte");
33+
unsigned long long load_half(void *skb,
34+
unsigned long long off) asm("llvm.bpf.load.half");
35+
unsigned long long load_word(void *skb,
36+
unsigned long long off) asm("llvm.bpf.load.word");
37+
38+
#endif
39+

tools/testing/selftests/bpf/progs/sockopt_sk.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ struct sockopt_sk {
1414
__u8 val;
1515
};
1616

17-
struct bpf_map_def SEC("maps") socket_storage_map = {
18-
.type = BPF_MAP_TYPE_SK_STORAGE,
19-
.key_size = sizeof(int),
20-
.value_size = sizeof(struct sockopt_sk),
21-
.map_flags = BPF_F_NO_PREALLOC,
22-
};
23-
BPF_ANNOTATE_KV_PAIR(socket_storage_map, int, struct sockopt_sk);
17+
struct {
18+
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
19+
__uint(map_flags, BPF_F_NO_PREALLOC);
20+
__type(key, int);
21+
__type(value, struct sockopt_sk);
22+
} socket_storage_map SEC(".maps");
2423

2524
SEC("cgroup/getsockopt")
2625
int _getsockopt(struct bpf_sockopt *ctx)

tools/testing/selftests/bpf/progs/tcp_rtt.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ struct tcp_rtt_storage {
1313
__u32 icsk_retransmits;
1414
};
1515

16-
struct bpf_map_def SEC("maps") socket_storage_map = {
17-
.type = BPF_MAP_TYPE_SK_STORAGE,
18-
.key_size = sizeof(int),
19-
.value_size = sizeof(struct tcp_rtt_storage),
20-
.map_flags = BPF_F_NO_PREALLOC,
21-
};
22-
BPF_ANNOTATE_KV_PAIR(socket_storage_map, int, struct tcp_rtt_storage);
16+
struct {
17+
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
18+
__uint(map_flags, BPF_F_NO_PREALLOC);
19+
__type(key, int);
20+
__type(value, struct tcp_rtt_storage);
21+
} socket_storage_map SEC(".maps");
2322

2423
SEC("sockops")
2524
int _sockops(struct bpf_sock_ops *ctx)

tools/testing/selftests/bpf/progs/test_btf_haskv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2018 Facebook */
33
#include <linux/bpf.h>
44
#include "bpf_helpers.h"
5+
#include "bpf_legacy.h"
56

67
int _version SEC("version") = 1;
78

tools/testing/selftests/bpf/progs/test_btf_newkv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2018 Facebook */
33
#include <linux/bpf.h>
44
#include "bpf_helpers.h"
5+
#include "bpf_legacy.h"
56

67
int _version SEC("version") = 1;
78

0 commit comments

Comments
 (0)