Skip to content

Commit a92a31d

Browse files
ebpf: use btf style maps
Instead of defining the structs with bpf_map_def, now we use BTF style maps: https://docs.ebpf.io/linux/concepts/maps/#btf-style-maps
1 parent 59fec29 commit a92a31d

File tree

3 files changed

+53
-61
lines changed

3 files changed

+53
-61
lines changed

ebpf_prog/common_defs.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@
2222
typedef u64 pid_size_t;
2323
typedef u64 uid_size_t;
2424

25-
26-
//-------------------------------map definitions
27-
// which github.com/iovisor/gobpf/elf expects
28-
typedef struct bpf_map_def {
29-
unsigned int type;
30-
unsigned int key_size;
31-
unsigned int value_size;
32-
unsigned int max_entries;
33-
unsigned int map_flags;
34-
unsigned int pinning;
35-
char namespace[BUF_SIZE_MAP_NS];
36-
} bpf_map_def;
37-
3825
enum bpf_pin_type {
3926
PIN_NONE = 0,
4027
PIN_OBJECT_NS,

ebpf_prog/opensnitch-dns.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ struct addrinfo_args_cache {
7070
char node[256];
7171
};
7272
// define temporary array for data
73-
struct bpf_map_def SEC("maps/addrinfo_args_hash") addrinfo_args_hash = {
74-
.type = BPF_MAP_TYPE_HASH,
75-
.max_entries = 256, // max entries at any time
76-
.key_size = sizeof(u32),
77-
.value_size = sizeof(struct addrinfo_args_cache),
78-
};
73+
struct {
74+
__uint(type, BPF_MAP_TYPE_HASH);
75+
__type(key, u32);
76+
__type(value, struct addrinfo_args_cache);
77+
__uint(max_entries, 256); // max entries at any time
78+
} addrinfo_args_hash SEC(".maps");
7979

8080
// BPF output events
8181
struct {

ebpf_prog/opensnitch.c

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,53 +75,58 @@ struct sock_on_x86_32_t {
7575

7676

7777
// Add +1,+2,+3 etc. to map size helps to easier distinguish maps in bpftool's output
78-
struct bpf_map_def SEC("maps/tcpMap") tcpMap = {
79-
.type = BPF_MAP_TYPE_HASH,
80-
.key_size = sizeof(struct tcp_key_t),
81-
.value_size = sizeof(struct tcp_value_t),
82-
.max_entries = MAPSIZE+1,
83-
};
84-
struct bpf_map_def SEC("maps/tcpv6Map") tcpv6Map = {
85-
.type = BPF_MAP_TYPE_HASH,
86-
.key_size = sizeof(struct tcpv6_key_t),
87-
.value_size = sizeof(struct tcpv6_value_t),
88-
.max_entries = MAPSIZE+2,
89-
};
90-
struct bpf_map_def SEC("maps/udpMap") udpMap = {
91-
.type = BPF_MAP_TYPE_HASH,
92-
.key_size = sizeof(struct udp_key_t),
93-
.value_size = sizeof(struct udp_value_t),
94-
.max_entries = MAPSIZE+3,
95-
};
96-
struct bpf_map_def SEC("maps/udpv6Map") udpv6Map = {
97-
.type = BPF_MAP_TYPE_HASH,
98-
.key_size = sizeof(struct udpv6_key_t),
99-
.value_size = sizeof(struct udpv6_value_t),
100-
.max_entries = MAPSIZE+4,
101-
};
78+
struct {
79+
__uint(type, BPF_MAP_TYPE_HASH);
80+
__type(key, struct tcp_key_t);
81+
__type(value, struct tcp_value_t);
82+
__uint(max_entries, MAPSIZE+1);
83+
} tcpMap SEC(".maps");
84+
85+
struct {
86+
__uint(type, BPF_MAP_TYPE_HASH);
87+
__type(key, struct tcpv6_key_t);
88+
__type(value, struct tcpv6_value_t);
89+
__uint(max_entries, MAPSIZE+2);
90+
} tcpv6Map SEC(".maps");
91+
92+
struct {
93+
__uint(type, BPF_MAP_TYPE_HASH);
94+
__type(key, struct udp_key_t);
95+
__type(value, struct udp_value_t);
96+
__uint(max_entries, MAPSIZE+3);
97+
} udpMap SEC(".maps");
98+
99+
struct {
100+
__uint(type, BPF_MAP_TYPE_HASH);
101+
__type(key, struct udpv6_key_t);
102+
__type(value, struct udpv6_value_t);
103+
__uint(max_entries, MAPSIZE+4);
104+
} udpv6Map SEC(".maps");
102105

103106
// for TCP the IP-tuple can be copied from "struct sock" only upon return from tcp_connect().
104107
// We stash the socket here to look it up upon return.
105-
struct bpf_map_def SEC("maps/tcpsock") tcpsock = {
106-
.type = BPF_MAP_TYPE_HASH,
107-
.key_size = sizeof(u64),
108+
struct {
109+
__uint(type, BPF_MAP_TYPE_HASH);
110+
__type(key, u64);
108111
// using u64 instead of sizeof(struct sock *)
109112
// to avoid pointer size related quirks on x86_32
110-
.value_size = sizeof(u64),
111-
.max_entries = 300,
112-
};
113-
struct bpf_map_def SEC("maps/tcpv6sock") tcpv6sock = {
114-
.type = BPF_MAP_TYPE_HASH,
115-
.key_size = sizeof(u64),
116-
.value_size = sizeof(u64),
117-
.max_entries = 300,
118-
};
119-
struct bpf_map_def SEC("maps/icmpsock") icmpsock = {
120-
.type = BPF_MAP_TYPE_HASH,
121-
.key_size = sizeof(u64),
122-
.value_size = sizeof(u64),
123-
.max_entries = 300,
124-
};
113+
__type(value, u64);
114+
__uint(max_entries, 300);
115+
} tcpsock SEC(".maps");
116+
117+
struct {
118+
__uint(type, BPF_MAP_TYPE_HASH);
119+
__type(key, u64);
120+
__type(value, u64);
121+
__uint(max_entries, 300);
122+
} tcpv6sock SEC(".maps");
123+
124+
struct {
125+
__uint(type, BPF_MAP_TYPE_HASH);
126+
__type(key, u64);
127+
__type(value, u64);
128+
__uint(max_entries, 300);
129+
} icmpsock SEC(".maps");
125130

126131
// initializing variables with __builtin_memset() is required
127132
// for compatibility with bpf on kernel 4.4

0 commit comments

Comments
 (0)