Skip to content

Commit 1ce868f

Browse files
albanyonghong-song
authored andcommitted
tools: add option --cgroupmap to tcp tools
List of tcp tools updated: tcpaccept, tcpconnect, tcptracer
1 parent 9422274 commit 1ce868f

File tree

7 files changed

+120
-10
lines changed

7 files changed

+120
-10
lines changed

docs/filtering_by_cgroups.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Examples of commands:
88
```
99
# ./opensnoop --cgroupmap /sys/fs/bpf/test01
1010
# ./execsnoop --cgroupmap /sys/fs/bpf/test01
11+
# ./tcpconnect --cgroupmap /sys/fs/bpf/test01
12+
# ./tcpaccept --cgroupmap /sys/fs/bpf/test01
13+
# ./tcptracer --cgroupmap /sys/fs/bpf/test01
1114
```
1215

1316
The commands above will only display results from processes that belong to one

tools/tcpaccept.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
./tcpaccept -t # include timestamps
3030
./tcpaccept -P 80,81 # only trace port 80 and 81
3131
./tcpaccept -p 181 # only trace PID 181
32+
./tcpaccept --cgroupmap ./mappath # only trace cgroups in this BPF map
3233
"""
3334
parser = argparse.ArgumentParser(
3435
description="Trace TCP accepts",
@@ -42,6 +43,8 @@
4243
help="trace this PID only")
4344
parser.add_argument("-P", "--port",
4445
help="comma-separated list of local ports to trace")
46+
parser.add_argument("--cgroupmap",
47+
help="trace cgroups in this BPF map only")
4548
parser.add_argument("--ebpf", action="store_true",
4649
help=argparse.SUPPRESS)
4750
args = parser.parse_args()
@@ -77,6 +80,11 @@
7780
char task[TASK_COMM_LEN];
7881
};
7982
BPF_PERF_OUTPUT(ipv6_events);
83+
84+
#if CGROUPSET
85+
BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH");
86+
#endif
87+
8088
"""
8189

8290
#
@@ -89,6 +97,13 @@
8997
bpf_text_kprobe = """
9098
int kretprobe__inet_csk_accept(struct pt_regs *ctx)
9199
{
100+
#if CGROUPSET
101+
u64 cgroupid = bpf_get_current_cgroup_id();
102+
if (cgroupset.lookup(&cgroupid) == NULL) {
103+
return 0;
104+
}
105+
#endif
106+
92107
struct sock *newsk = (struct sock *)PT_REGS_RC(ctx);
93108
u32 pid = bpf_get_current_pid_tgid() >> 32;
94109
@@ -184,6 +199,11 @@
184199
lports_if = ' && '.join(['lport != %d' % lport for lport in lports])
185200
bpf_text = bpf_text.replace('##FILTER_PORT##',
186201
'if (%s) { return 0; }' % lports_if)
202+
if args.cgroupmap:
203+
bpf_text = bpf_text.replace('CGROUPSET', '1')
204+
bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap)
205+
else:
206+
bpf_text = bpf_text.replace('CGROUPSET', '0')
187207
if debug or args.ebpf:
188208
print(bpf_text)
189209
if args.ebpf:

tools/tcpaccept_example.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,33 @@ TIME(s) PID COMM IP RADDR RPORT LADDR LPORT
3333
1.984 907 sshd 4 127.0.0.1 51250 127.0.0.1 22
3434

3535

36+
The --cgroupmap option filters based on a cgroup set. It is meant to be used
37+
with an externally created map.
38+
39+
# ./tcpaccept --cgroupmap /sys/fs/bpf/test01
40+
41+
For more details, see docs/filtering_by_cgroups.md
42+
43+
3644
USAGE message:
3745

3846
# ./tcpaccept -h
39-
usage: tcpaccept [-h] [-T] [-t] [-p PID] [-P PORTS]
47+
usage: tcpaccept.py [-h] [-T] [-t] [-p PID] [-P PORT] [--cgroupmap CGROUPMAP]
4048

4149
Trace TCP accepts
4250

4351
optional arguments:
44-
-h, --help show this help message and exit
45-
-T, --time include time column on output (HH:MM:SS)
46-
-t, --timestamp include timestamp on output
47-
-p PID, --pid PID trace this PID only
48-
-P PORTS, --port PORTS comma-separated list of local ports to trace
52+
-h, --help show this help message and exit
53+
-T, --time include time column on output (HH:MM:SS)
54+
-t, --timestamp include timestamp on output
55+
-p PID, --pid PID trace this PID only
56+
-P PORT, --port PORT comma-separated list of local ports to trace
57+
--cgroupmap CGROUPMAP
58+
trace cgroups in this BPF map only
4959

5060
examples:
5161
./tcpaccept # trace all TCP accept()s
5262
./tcpaccept -t # include timestamps
5363
./tcpaccept -P 80,81 # only trace port 80 and 81
5464
./tcpaccept -p 181 # only trace PID 181
65+
./tcpaccept --cgroupmap ./mappath # only trace cgroups in this BPF map

tools/tcpconnect.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
./tcpconnect -U # include UID
3838
./tcpconnect -u 1000 # only trace UID 1000
3939
./tcpconnect -c # count connects per src ip and dest ip/port
40+
./tcpconnect --cgroupmap ./mappath # only trace cgroups in this BPF map
4041
"""
4142
parser = argparse.ArgumentParser(
4243
description="Trace TCP connects",
@@ -54,6 +55,8 @@
5455
help="trace this UID only")
5556
parser.add_argument("-c", "--count", action="store_true",
5657
help="count connects per src ip and dest ip/port")
58+
parser.add_argument("--cgroupmap",
59+
help="trace cgroups in this BPF map only")
5760
parser.add_argument("--ebpf", action="store_true",
5861
help=argparse.SUPPRESS)
5962
args = parser.parse_args()
@@ -67,6 +70,10 @@
6770
6871
BPF_HASH(currsock, u32, struct sock *);
6972
73+
#if CGROUPSET
74+
BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH");
75+
#endif
76+
7077
// separate data structs for ipv4 and ipv6
7178
struct ipv4_data_t {
7279
u64 ts_us;
@@ -109,6 +116,13 @@
109116
110117
int trace_connect_entry(struct pt_regs *ctx, struct sock *sk)
111118
{
119+
#if CGROUPSET
120+
u64 cgroupid = bpf_get_current_cgroup_id();
121+
if (cgroupset.lookup(&cgroupid) == NULL) {
122+
return 0;
123+
}
124+
#endif
125+
112126
u64 pid_tgid = bpf_get_current_pid_tgid();
113127
u32 pid = pid_tgid >> 32;
114128
u32 tid = pid_tgid;
@@ -234,6 +248,11 @@
234248
if args.uid:
235249
bpf_text = bpf_text.replace('FILTER_UID',
236250
'if (uid != %s) { return 0; }' % args.uid)
251+
if args.cgroupmap:
252+
bpf_text = bpf_text.replace('CGROUPSET', '1')
253+
bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap)
254+
else:
255+
bpf_text = bpf_text.replace('CGROUPSET', '0')
237256

238257
bpf_text = bpf_text.replace('FILTER_PID', '')
239258
bpf_text = bpf_text.replace('FILTER_PORT', '')

tools/tcpconnect_example.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,32 @@ LADDR RADDR RPORT CONNECTS
6868
[...]
6969

7070

71+
The --cgroupmap option filters based on a cgroup set. It is meant to be used
72+
with an externally created map.
73+
74+
# ./tcpconnect --cgroupmap /sys/fs/bpf/test01
75+
76+
For more details, see docs/filtering_by_cgroups.md
77+
78+
7179
USAGE message:
7280

7381
# ./tcpconnect -h
74-
usage: tcpconnect [-h] [-c] [-t] [-p PID] [-P PORT]
82+
usage: tcpconnect.py [-h] [-t] [-p PID] [-P PORT] [-U] [-u UID] [-c]
83+
[--cgroupmap CGROUPMAP]
7584

7685
Trace TCP connects
7786

7887
optional arguments:
7988
-h, --help show this help message and exit
8089
-t, --timestamp include timestamp on output
8190
-p PID, --pid PID trace this PID only
82-
-P PORT, --port PORT
83-
comma-separated list of destination ports to trace.
91+
-P PORT, --port PORT comma-separated list of destination ports to trace.
8492
-U, --print-uid include UID on output
8593
-u UID, --uid UID trace this UID only
8694
-c, --count count connects per src ip and dest ip/port
95+
--cgroupmap CGROUPMAP
96+
trace cgroups in this BPF map only
8797

8898
examples:
8999
./tcpconnect # trace all TCP connect()s
@@ -94,3 +104,4 @@ examples:
94104
./tcpconnect -U # include UID
95105
./tcpconnect -u 1000 # only trace UID 1000
96106
./tcpconnect -c # count connects per src ip and dest ip/port
107+
./tcpconnect --cgroupmap ./mappath # only trace cgroups in this BPF map

tools/tcptracer.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# The following code should be replaced, and simplified, when static TCP probes
1212
# exist.
1313
#
14-
# Copyright 2017 Kinvolk GmbH
14+
# Copyright 2017-2020 Kinvolk GmbH
1515
#
1616
# Licensed under the Apache License, Version 2.0 (the "License")
1717
from __future__ import print_function
@@ -29,6 +29,8 @@
2929
help="trace this PID only")
3030
parser.add_argument("-N", "--netns", default=0, type=int,
3131
help="trace this Network Namespace only")
32+
parser.add_argument("--cgroupmap",
33+
help="trace cgroups in this BPF map only")
3234
parser.add_argument("-v", "--verbose", action="store_true",
3335
help="include Network Namespace in the output")
3436
parser.add_argument("--ebpf", action="store_true",
@@ -77,6 +79,10 @@
7779
};
7880
BPF_PERF_OUTPUT(tcp_ipv6_event);
7981
82+
#if CGROUPSET
83+
BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH");
84+
#endif
85+
8086
// tcp_set_state doesn't run in the context of the process that initiated the
8187
// connection so we need to store a map TUPLE -> PID to send the right PID on
8288
// the event
@@ -173,6 +179,13 @@
173179
174180
int trace_connect_v4_entry(struct pt_regs *ctx, struct sock *sk)
175181
{
182+
#if CGROUPSET
183+
u64 cgroupid = bpf_get_current_cgroup_id();
184+
if (cgroupset.lookup(&cgroupid) == NULL) {
185+
return 0;
186+
}
187+
#endif
188+
176189
u64 pid = bpf_get_current_pid_tgid();
177190
178191
##FILTER_PID##
@@ -220,6 +233,12 @@
220233
221234
int trace_connect_v6_entry(struct pt_regs *ctx, struct sock *sk)
222235
{
236+
#if CGROUPSET
237+
u64 cgroupid = bpf_get_current_cgroup_id();
238+
if (cgroupset.lookup(&cgroupid) == NULL) {
239+
return 0;
240+
}
241+
#endif
223242
u64 pid = bpf_get_current_pid_tgid();
224243
225244
##FILTER_PID##
@@ -352,6 +371,13 @@
352371
353372
int trace_close_entry(struct pt_regs *ctx, struct sock *skp)
354373
{
374+
#if CGROUPSET
375+
u64 cgroupid = bpf_get_current_cgroup_id();
376+
if (cgroupset.lookup(&cgroupid) == NULL) {
377+
return 0;
378+
}
379+
#endif
380+
355381
u64 pid = bpf_get_current_pid_tgid();
356382
357383
##FILTER_PID##
@@ -413,6 +439,13 @@
413439
414440
int trace_accept_return(struct pt_regs *ctx)
415441
{
442+
#if CGROUPSET
443+
u64 cgroupid = bpf_get_current_cgroup_id();
444+
if (cgroupset.lookup(&cgroupid) == NULL) {
445+
return 0;
446+
}
447+
#endif
448+
416449
struct sock *newsk = (struct sock *)PT_REGS_RC(ctx);
417450
u64 pid = bpf_get_current_pid_tgid();
418451
@@ -581,6 +614,11 @@ def print_ipv6_event(cpu, data, size):
581614

582615
bpf_text = bpf_text.replace('##FILTER_PID##', pid_filter)
583616
bpf_text = bpf_text.replace('##FILTER_NETNS##', netns_filter)
617+
if args.cgroupmap:
618+
bpf_text = bpf_text.replace('CGROUPSET', '1')
619+
bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap)
620+
else:
621+
bpf_text = bpf_text.replace('CGROUPSET', '0')
584622

585623
if args.ebpf:
586624
print(bpf_text)

tools/tcptracer_example.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ TIME(s) T PID COMM IP SADDR DADDR SPORT
3535
3.546 C 748 curl 6 [::1] [::1] 42592 80
3636
4.294 X 31002 telnet 4 192.168.1.2 192.168.1.1 42590 23
3737
```
38+
39+
40+
The --cgroupmap option filters based on a cgroup set. It is meant to be used
41+
with an externally created map.
42+
43+
# ./tcptracer --cgroupmap /sys/fs/bpf/test01
44+
45+
For more details, see docs/filtering_by_cgroups.md

0 commit comments

Comments
 (0)