Skip to content

Commit 54b73e6

Browse files
committed
nettrace: use OPTION_IPV4ORIPV6 for nettrace
Now, we can filter ipv4 or ipv6 address with the --addr argument. Such as --addr ::1 Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 68ee5b7 commit 54b73e6

4 files changed

Lines changed: 82 additions & 76 deletions

File tree

nodetrace/mark.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
3131

3232
static int parse_opts(int argc, char *argv[], bpf_args_t *args)
3333
{
34-
int proto_l;
35-
u16 proto;
34+
COMMON_PROG_ARGS_BEGIN()
3635

3736
option_item_t opts[] = {
38-
COMMON_PROG_ARGS(&args->pkt),
37+
COMMON_PROG_ARGS_DEFINE(&args->pkt),
3938
{ .type = OPTION_BLANK },
4039
{
4140
.sname = 'i', .dest = &nic, .type = OPTION_STRING,
@@ -54,11 +53,11 @@ static int parse_opts(int argc, char *argv[], bpf_args_t *args)
5453
.desc = "show help information",
5554
},
5655
};
57-
#undef E
58-
#undef R
5956

6057
if (parse_args(argc, argv, &prog_config, opts, ARRAY_SIZE(opts)))
6158
goto err;
59+
60+
COMMON_PROG_ARGS_END(&args->pkt)
6261
return 0;
6362
err:
6463
return -1;

shared/bpf/skb_shared.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,29 @@ typedef struct __attribute__((__packed__)) {
5757
#define TCP_FLAGS_RST (1 << 2)
5858
#define TCP_FLAGS_SYN (1 << 1)
5959

60+
#define APPLY_DEFINE_FIELD(dummy, a, b, ...) DEFINE_FIELD_##b
61+
#define DEFINE_FIELD_STD(type, name) \
62+
type name; \
63+
bool enable_##name;
64+
#define DEFINE_FIELD_ARRAY(type, name, size) \
65+
type name[size]; \
66+
bool enable_##name;
67+
#define DEFINE_FIELD(type, name, args...) \
68+
APPLY_DEFINE_FIELD(dummy, ##args, ARRAY, STD)(type, name, ##args)
69+
6070
/* used for packet filter condition */
6171
typedef struct {
62-
u32 saddr;
63-
bool enable_saddr;
64-
u32 daddr;
65-
bool enable_daddr;
66-
u32 addr;
67-
bool enable_addr;
68-
u8 saddr_v6[16];
69-
bool enable_saddr_v6;
70-
u8 daddr_v6[16];
71-
bool enable_daddr_v6;
72-
u8 addr_v6[16];
73-
bool enable_addr_v6;
74-
u16 sport;
75-
bool enable_sport;
76-
u16 dport;
77-
bool enable_dport;
78-
u16 port;
79-
bool enable_port;
80-
u16 l3_proto;
81-
bool enable_l3_proto;
82-
u8 l4_proto;
83-
bool enable_l4_proto;
72+
DEFINE_FIELD(u32, saddr)
73+
DEFINE_FIELD(u32, daddr)
74+
DEFINE_FIELD(u32, addr)
75+
DEFINE_FIELD(u8, saddr_v6, 16)
76+
DEFINE_FIELD(u8, daddr_v6, 16)
77+
DEFINE_FIELD(u8, addr_v6, 16)
78+
DEFINE_FIELD(u16, sport)
79+
DEFINE_FIELD(u16, dport)
80+
DEFINE_FIELD(u16, port)
81+
DEFINE_FIELD(u16, l3_proto)
82+
DEFINE_FIELD(u8, l4_proto)
8483
} pkt_args_t;
8584

8685
#define CONFIG_MAP_SIZE 1024

shared/common_args.h

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,33 @@
11

2-
#define COMMON_PROG_ARGS(args) \
2+
#define COMMON_PROG_ARGS_BEGIN() \
3+
u8 addr_buf[16], saddr_buf[16], daddr_buf[16]; \
4+
u16 addr_pf = 0, saddr_pf = 0, daddr_pf = 0; \
5+
int proto_l = 0; \
6+
u16 proto;
7+
8+
#define COMMON_PROG_ARGS_DEFINE(args) \
39
{ \
410
.lname = "saddr", \
511
.sname = 's', \
6-
.dest = &(args)->saddr, \
7-
.type = OPTION_IPV4, \
8-
.set = &(args)->enable_saddr, \
9-
.desc = "filter source ip address", \
10-
}, \
11-
{ \
12-
.lname = "saddr6", \
13-
.dest = &(args)->saddr_v6, \
14-
.type = OPTION_IPV6, \
15-
.set = &(args)->enable_saddr_v6, \
16-
.desc = "filter source ip v6 address", \
12+
.dest = saddr_buf, \
13+
.type = OPTION_IPV4ORIPV6, \
14+
.set = &saddr_pf, \
15+
.desc = "filter source ip/ipv6 address", \
1716
}, \
1817
{ \
1918
.lname = "daddr", \
2019
.sname = 'd', \
21-
.dest = &(args)->daddr, \
22-
.type = OPTION_IPV4, \
23-
.set = &(args)->enable_daddr, \
24-
.desc = "filter dest ip address", \
25-
}, \
26-
{ \
27-
.lname = "daddr6", \
28-
.dest = &(args)->daddr_v6, \
29-
.type = OPTION_IPV6, \
30-
.set = &(args)->enable_daddr_v6, \
31-
.desc = "filter dest ip v6 address", \
20+
.dest = daddr_buf, \
21+
.type = OPTION_IPV4ORIPV6, \
22+
.set = &daddr_pf, \
23+
.desc = "filter dest ip/ipv6 address", \
3224
}, \
3325
{ \
3426
.lname = "addr", \
35-
.dest = &(args)->addr, \
36-
.type = OPTION_IPV4, \
37-
.set = &(args)->enable_addr, \
38-
.desc = "filter source or dest ip address", \
39-
}, \
40-
{ \
41-
.lname = "addr6", \
42-
.dest = &(args)->addr_v6, \
43-
.type = OPTION_IPV6, \
44-
.set = &(args)->enable_addr_v6, \
45-
.desc = "filter source or dest ip v6 address", \
27+
.dest = addr_buf, \
28+
.type = OPTION_IPV4ORIPV6, \
29+
.set = &addr_pf, \
30+
.desc = "filter source or dest ip/ipv6 address", \
4631
}, \
4732
{ \
4833
.lname = "sport", \
@@ -76,3 +61,36 @@
7661
.set = &proto_l, \
7762
.desc = "filter L3/L4 protocol, such as 'tcp', 'arp'", \
7863
}
64+
65+
/* convert the args to the eBPF pkt_arg struct */
66+
#define FILL_ADDR_PROTO(name, subfix, args, pf) if (name##_pf == pf) { \
67+
memcpy(&(args)->name##subfix, name##_buf, \
68+
sizeof((args)->name##subfix)); \
69+
(args)->enable_##name##subfix = true; \
70+
if ((args)->enable_l3_proto && (args)->l3_proto != pf) { \
71+
pr_err("ip" #subfix " protocol is excepted!\n"); \
72+
goto err; \
73+
} \
74+
(args)->enable_l3_proto = true; \
75+
(args)->l3_proto = pf; \
76+
}
77+
#define FILL_ADDR(name, args) \
78+
FILL_ADDR_PROTO(name, _v6, args, ETH_P_IPV6) \
79+
FILL_ADDR_PROTO(name, , args, ETH_P_IP)
80+
81+
#define COMMON_PROG_ARGS_END(args) \
82+
switch (proto_l) { \
83+
case 3: \
84+
(args)->enable_l3_proto = true; \
85+
(args)->l3_proto = proto; \
86+
break; \
87+
case 4: \
88+
(args)->enable_l4_proto = true; \
89+
(args)->l4_proto = proto; \
90+
break; \
91+
default: \
92+
break; \
93+
} \
94+
FILL_ADDR(saddr, args) \
95+
FILL_ADDR(daddr, args) \
96+
FILL_ADDR(addr, args)

src/nettrace.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: MulanPSL-2.0
22

3+
#include <arpa/inet.h>
4+
35
#include <arg_parse.h>
46
#include <common_args.h>
57

@@ -18,11 +20,10 @@ static void do_parse_args(int argc, char *argv[])
1820
trace_args_t *trace_args = &trace_ctx.args;
1921
bpf_args_t *bpf_args = &trace_ctx.bpf_args;
2022
pkt_args_t *pkt_args = &bpf_args->pkt;
21-
int proto_l = 0;
22-
u16 proto;
23+
COMMON_PROG_ARGS_BEGIN()
2324

2425
option_item_t opts[] = {
25-
COMMON_PROG_ARGS(pkt_args),
26+
COMMON_PROG_ARGS_DEFINE(pkt_args),
2627
{
2728
.lname = "pid", .type = OPTION_U32,
2829
.dest = &bpf_args->pid, .set = &bpf_args->enable_pid,
@@ -134,18 +135,7 @@ static void do_parse_args(int argc, char *argv[])
134135
exit(0);
135136
}
136137

137-
switch (proto_l) {
138-
case 3:
139-
pkt_args->enable_l3_proto = true;
140-
pkt_args->l3_proto = proto;
141-
break;
142-
case 4:
143-
pkt_args->enable_l4_proto = true;
144-
pkt_args->l4_proto = proto;
145-
break;
146-
default:
147-
break;
148-
}
138+
COMMON_PROG_ARGS_END(pkt_args)
149139

150140
return;
151141
err:

0 commit comments

Comments
 (0)