Skip to content

Commit fab8e1d

Browse files
committed
optimize: optimize the define of struct bpf_args_t
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 8369b6c commit fab8e1d

20 files changed

Lines changed: 215 additions & 201 deletions

common.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
LIBBPF ?= /usr/include/bpf
22
COMPONENT := $(ROOT)/component
3-
COMMON_SHARED := $(ROOT)/shared/pkt_utils.c $(COMPONENT)/net_utils.c \
4-
$(COMPONENT)/arg_parse.c $(COMPONENT)/sys_utils.c
3+
COMMON_SHARED := $(ROOT)/shared/pkt_utils.c $(COMPONENT)/net_utils.c \
4+
$(COMPONENT)/arg_parse.c $(COMPONENT)/sys_utils.c \
5+
$(ROOT)/shared/bpf_utils.c
56

67
CFLAGS += -I./ -I$(ROOT)/shared/bpf/
78
BPF_CFLAGS = $(CFLAGS) -I$(LIBBPF) -Wno-unused-function

droptrace/droptrace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
#include <bpf/bpf.h>
1010

1111
#include <net_utils.h>
12-
#include <bpf_utils.h>
1312
#include <arg_parse.h>
1413
#include <pkt_utils.h>
1514
#include <parse_sym.h>
15+
#include <common_args.h>
1616

1717
#include "reasons.h"
1818
#include "./progs/shared.h"
19+
#include <bpf_utils.h>
20+
1921
#include "progs/probe.skel.h"
2022
#include "progs/trace.skel.h"
2123

@@ -102,10 +104,8 @@ static int parse_opts(int argc, char *argv[], bpf_args_t *args)
102104
int proto_l = 0;
103105
u16 proto;
104106

105-
#define E(name) &(args->pkt.enable_##name)
106-
#define R(name) &(args->pkt.name)
107107
option_item_t opts[] = {
108-
#include <common_args.h>
108+
COMMON_PROG_ARGS(&args->pkt),
109109
{
110110
.lname = "reason",
111111
.sname = 'r',

droptrace/progs/shared.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
#include <packet.h>
1+
#include <skb_shared.h>
22

33
typedef struct {
44
u64 location;
55
packet_t pkt;
66
u16 reason;
77
} event_t;
88

9-
struct bpf_args {
10-
pkt_args_t pkt;
11-
u16 reason;
12-
bool enable_reason;
13-
u32 limit;
14-
bool enable_limit;
15-
u32 limit_bucket;
16-
bool enable_limit_bucket;
17-
bool snmp_mode;
18-
u32 snmp_reasons[SKB_DROP_REASON_MAX];
19-
int current_budget;
20-
u64 last_ts;
21-
};
22-
typedef struct bpf_args bpf_args_t;
9+
#define DEFINE_BPF_ARGS() \
10+
u16 reason; \
11+
bool enable_reason; \
12+
u32 limit; \
13+
bool enable_limit; \
14+
u32 limit_bucket; \
15+
bool enable_limit_bucket; \
16+
bool snmp_mode; \
17+
u32 snmp_reasons[SKB_DROP_REASON_MAX]; \
18+
int current_budget; \
19+
u64 last_ts

nodetrace/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include <bpf/bpf.h>
1313

1414
#include <net_utils.h>
15-
#include <bpf_utils.h>
1615
#include <arg_parse.h>
1716
#include <pkt_utils.h>
1817

1918
#include "progs/shared.h"
19+
#include <bpf_utils.h>
2020

2121
static inline int tc_attach(int prog_fd, char *nic, char *pref,
2222
bool ingress)

nodetrace/mark.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-License-Identifier: MulanPSL-2.0
2+
#include <common_args.h>
23

34
#include "common.h"
45
#include "progs/shared.h"
@@ -33,10 +34,8 @@ static int parse_opts(int argc, char *argv[], bpf_args_t *args)
3334
int proto_l;
3435
u16 proto;
3536

36-
#define E(name) &(args->pkt.enable_##name)
37-
#define R(name) &(args->pkt.name)
3837
option_item_t opts[] = {
39-
#include <common_args.h>
38+
COMMON_PROG_ARGS(&args->pkt),
4039
{ .type = OPTION_BLANK },
4140
{
4241
.sname = 'i', .dest = &nic, .type = OPTION_STRING,

nodetrace/progs/shared.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#ifndef _H_SHARED
22
#define _H_SHARED
33

4-
#include <packet.h>
4+
#include <skb_shared.h>
5+
6+
#define DEFINE_BPF_ARGS() \
7+
bool quiet
58

69
typedef struct {
710
packet_t pkt;
@@ -15,10 +18,4 @@ enum {
1518
LOCALTION_ERR,
1619
};
1720

18-
struct bpf_args {
19-
pkt_args_t pkt;
20-
bool quiet;
21-
};
22-
typedef struct bpf_args bpf_args_t;
23-
2421
#endif

shared/bpf/macro.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef _H_BPF_MACRO
2+
#define _H_BPF_MACRO
3+
14
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
25
#define ETH_P_IPV6 0x86DD /* IPv6 over bluebook */
36

@@ -20,3 +23,5 @@
2023
#ifndef ARRAY_SIZE
2124
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
2225
#endif
26+
27+
#endif
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _H_BPF_PACKET
2-
#define _H_BPF_PACKET
1+
#ifndef _H_BPF_SKB_SHARED
2+
#define _H_BPF_SKB_SHARED
33

44
typedef struct {
55
u16 sport;
@@ -26,10 +26,6 @@ typedef struct __attribute__((__packed__)) {
2626
u32 ack;
2727
u8 flags;
2828
} tcp;
29-
#define field_sport field_tcp.sport
30-
#define field_dport field_tcp.dport
31-
#define field_flags field_tcp.flags
32-
#define field_tcp l4.tcp
3329
struct {
3430
u16 sport;
3531
u16 dport;

shared/bpf/skb_utils.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#ifndef _H_BPF_SKB_UTILS
22
#define _H_BPF_SKB_UTILS
33

4+
/* This file can only be used in eBPF program, can't be used in user space
5+
* code.
6+
*/
7+
48
#include "macro.h"
5-
#include "packet.h"
9+
#include "skb_shared.h"
10+
11+
typedef struct {
12+
pkt_args_t pkt;
13+
#ifdef DEFINE_BPF_ARGS
14+
DEFINE_BPF_ARGS();
15+
#endif
16+
} bpf_args_t;
617

718
struct {
819
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
@@ -22,12 +33,11 @@ struct {
2233
tmp; \
2334
})
2435

25-
struct bpf_args;
2636
#ifdef MAP_CONFIG
2737
struct {
2838
__uint(type, BPF_MAP_TYPE_ARRAY);
2939
__uint(key_size, sizeof(int));
30-
__uint(value_size, CONFIG_MAP_SIZE);
40+
__uint(value_size, sizeof(bpf_args_t));
3141
__uint(max_entries, 1);
3242
} m_config SEC(".maps");
3343

@@ -36,12 +46,12 @@ struct {
3646
void * _v = bpf_map_lookup_elem(&m_config, &_key); \
3747
if (!_v) \
3848
return 0; /* this can't happen */ \
39-
(struct bpf_args*)_v; \
49+
(bpf_args_t*)_v; \
4050
})
4151

4252
#define try_inline __attribute__((always_inline))
4353
#else
44-
extern struct bpf_args _bpf_args;
54+
bpf_args_t _bpf_args;
4555
#define CONFIG() &_bpf_args
4656
#define try_inline inline
4757
#endif
@@ -51,7 +61,7 @@ extern struct bpf_args _bpf_args;
5161

5262
#define ARGS_ENABLED(name) bpf_args->enable_##name
5363
#define ARGS_GET(name) bpf_args->name
54-
#define ARGS_GET_CONFIG(name) ((struct bpf_args *)CONFIG())->name
64+
#define ARGS_GET_CONFIG(name) ((bpf_args_t *)CONFIG())->name
5565
#define ARGS_CHECK(name, val) \
5666
(ARGS_ENABLED(name) && bpf_args->name != (val))
5767

@@ -137,10 +147,9 @@ static try_inline bool skb_l4_check(u16 l4, u16 l3)
137147
return l4 == 0xFFFF || l4 <= l3;
138148
}
139149

140-
#define CHECK_ATTR(attr) \
141-
((ARGS_CHECK(attr, d##attr) && \
142-
ARGS_CHECK(attr, s##attr)) || \
143-
ARGS_CHECK(s##attr, s##attr) || \
150+
#define CHECK_ATTR(attr) \
151+
((ARGS_CHECK(attr, d##attr) && ARGS_CHECK(attr, s##attr)) || \
152+
ARGS_CHECK(s##attr, s##attr) || \
144153
ARGS_CHECK(d##attr, d##attr))
145154

146155
static try_inline int probe_parse_ip(void *ip, parse_ctx_t *ctx)
@@ -299,7 +308,7 @@ static try_inline int probe_parse_skb(struct sk_buff *skb, packet_t *pkt)
299308
}
300309

301310
static try_inline int probe_parse_skb_no_filter(struct sk_buff *skb,
302-
packet_t *pkt)
311+
packet_t *pkt)
303312
{
304313
parse_ctx_t ctx = {
305314
.args = (void *)CONFIG(),
@@ -379,4 +388,4 @@ static try_inline int direct_parse_skb(struct __sk_buff *skb, packet_t *pkt,
379388
return 1;
380389
}
381390

382-
#endif
391+
#endif

shared/bpf_utils.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <bpf/libbpf.h>
2+
#include <bpf/bpf.h>
3+
#include <linux/perf_event.h>
4+
#include <sys/types.h>
5+
#include <sys/stat.h>
6+
#include <fcntl.h>
7+
#include <errno.h>
8+
#include <linux/hw_breakpoint.h>
9+
#include <sys/syscall.h>
10+
#include <sys/ioctl.h>
11+
#include <unistd.h>
12+
13+
#include <sys_utils.h>
14+
15+
#include "bpf_utils.h"
16+
17+
void
18+
perf_output_cond(int fd, perf_buffer_sample_fn cb, perf_buffer_lost_fn lost,
19+
bool *stop)
20+
{
21+
struct perf_buffer_opts pb_opts = {
22+
.sample_cb = cb,
23+
.lost_cb = lost,
24+
};
25+
struct perf_buffer *pb;
26+
int ret;
27+
28+
pb = perf_buffer__new(fd, 1024, &pb_opts);
29+
ret = libbpf_get_error(pb);
30+
if (ret) {
31+
printf("failed to setup perf_buffer: %d\n", ret);
32+
return;
33+
}
34+
35+
while ((ret = perf_buffer__poll(pb, 1000)) >= 0)
36+
if (stop && *stop)
37+
break;
38+
}
39+
40+
int compat_bpf_attach_kprobe(int fd, char *name, bool ret)
41+
{
42+
struct perf_event_attr attr = {};
43+
char buf[1024], target[128];
44+
int id, err;
45+
46+
attr.type = PERF_TYPE_TRACEPOINT;
47+
attr.sample_type = PERF_SAMPLE_RAW;
48+
attr.sample_period = 1;
49+
attr.wakeup_events = 1;
50+
51+
sprintf(target, "%s%s", ret ? "ret_" : "", name);
52+
sprintf(buf, "/sys/kernel/debug/tracing/events/kprobes/%s/id",
53+
target);
54+
if (file_exist(buf))
55+
goto exist;
56+
57+
sprintf(buf, "(echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events) 2>&1",
58+
ret ? 'r' : 'p', target, name);
59+
if (simple_exec(buf)) {
60+
pr_warn("failed to create kprobe: %s\n", target);
61+
return -1;
62+
}
63+
sprintf(buf, "/sys/kernel/debug/tracing/events/kprobes/%s/id",
64+
target);
65+
exist:;
66+
int efd = open(buf, O_RDONLY, 0);
67+
if (efd < 0) {
68+
pr_warn("failed to open event %s\n", name);
69+
return -1;
70+
}
71+
72+
err = read(efd, buf, sizeof(buf));
73+
if (err < 0 || err >= sizeof(buf)) {
74+
pr_warn("read from '%s' failed '%s'\n", target, strerror(errno));
75+
return -1;
76+
}
77+
78+
close(efd);
79+
80+
buf[err] = 0;
81+
id = atoi(buf);
82+
attr.config = id;
83+
84+
efd = syscall(SYS_perf_event_open, &attr, -1, 0, -1, 0);
85+
if (efd < 0) {
86+
pr_warn("event %d fd %d err %s\n", id, efd, strerror(errno));
87+
return -1;
88+
}
89+
ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
90+
ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)