Skip to content

Commit 792bbe1

Browse files
authored
fix(backend): enable process filtering for the cgroup-skb backend (#246)
* fix(backend): enable process filtering for the `cgroup-skb` backend * improve tests
1 parent 020852d commit 792bbe1

35 files changed

+83
-220
lines changed

.circleci/config.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ jobs:
297297
name: test cgroup-skb
298298
command: |
299299
set -ex
300-
sudo bash testdata/test_cgroup_skb_base_pcap.sh ./ptcpdump
301-
sudo bash testdata/test_cgroup_skb_base_pcapng.sh ./ptcpdump
302-
sudo bash testdata/test_cgroup_skb_filter_ifindex.sh ./ptcpdump
300+
301+
sudo PTCPDUMP_EXTRA_ARGS='--backend cgroup-skb' bash testdata/test_pid_filter.sh ./ptcpdump
302+
sudo PTCPDUMP_EXTRA_ARGS='--backend cgroup-skb' bash testdata/test_pname_filter.sh ./ptcpdump
303+
sudo PTCPDUMP_EXTRA_ARGS='--backend cgroup-skb' bash testdata/test_sub_program.sh ./ptcpdump
303304
304305
workflows:
305306
e2e:

.github/workflows/test.yml

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,15 @@ jobs:
4747
run: |
4848
bash testdata/test_run_with_docker.sh "quay.io/ptcpdump/ptcpdump:latest"
4949
50-
test-backend:
51-
runs-on: ubuntu-latest
52-
needs: build
53-
timeout-minutes: 5
54-
55-
steps:
56-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
57-
- name: Retrieve stored ptcpdump executable
58-
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
59-
with:
60-
name: ptcpdump
61-
path: ptcpdump
62-
63-
- name: test cgroup-skb
64-
run: |
65-
set -ex
66-
chmod +x ./ptcpdump/ptcpdump
67-
sudo bash testdata/test_cgroup_skb_base_pcap.sh ./ptcpdump/ptcpdump
68-
sudo bash testdata/test_cgroup_skb_base_pcapng.sh ./ptcpdump/ptcpdump
69-
sudo bash testdata/test_cgroup_skb_filter_ifindex.sh ./ptcpdump/ptcpdump
70-
7150
e2e-test:
7251
name: e2e-test
7352
needs: build
7453
strategy:
7554
fail-fast: false
7655
matrix:
56+
backend:
57+
- 'tc'
58+
- 'cgroup-skb'
7759
kernel:
7860
# renovate: datasource=docker depName=quay.io/lvh-images/kernel-images
7961
- '4.19-20241031.113911'
@@ -98,6 +80,14 @@ jobs:
9880
steps:
9981
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
10082

83+
- run: |
84+
if [ "${{ matrix.backend }}" = "tc" ]; then
85+
PTCPDUMP_EXTRA_ARGS=''
86+
else
87+
PTCPDUMP_EXTRA_ARGS='--backend cgroup-skb'
88+
fi
89+
echo "PTCPDUMP_EXTRA_ARGS=${PTCPDUMP_EXTRA_ARGS}" >> $GITHUB_ENV
90+
10191
- name: Retrieve stored ptcpdump executable
10292
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
10393
with:
@@ -144,58 +134,58 @@ jobs:
144134
# btf_file=$(find /host/ -path "*btf*" -type f)
145135
# sudo cp $btf_file /var/lib/ptcpdump/btf/vmlinux
146136
147-
- name: Test default
137+
- name: Test default (ping)
148138
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
139+
if: ${{ !startsWith(matrix.backend, 'cgroup-skb') }}
149140
with:
150141
provision: 'false'
151142
cmd: |
152143
set -ex
153-
uname -a
154-
cat /etc/issue
144+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
155145
156146
bash /host/testdata/test_default.sh /host/ptcpdump/ptcpdump
157147
158-
- name: Test base
148+
- name: Test base (curl)
159149
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
150+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
160151
with:
161152
provision: 'false'
162153
cmd: |
163154
set -ex
164-
uname -a
165-
cat /etc/issue
155+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
166156
167157
bash /host/testdata/test_base.sh /host/ptcpdump/ptcpdump
168158
169159
- name: Test parent info
170160
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
161+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
171162
with:
172163
provision: 'false'
173164
cmd: |
174165
set -ex
175-
uname -a
176-
cat /etc/issue
166+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
177167
178168
bash /host/testdata/test_parent_info.sh /host/ptcpdump/ptcpdump
179169
180170
- name: Test filter by process name
181171
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
172+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
182173
with:
183174
provision: 'false'
184175
cmd: |
185176
set -ex
186-
uname -a
187-
cat /etc/issue
177+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
188178
189179
bash /host/testdata/test_pname_filter.sh /host/ptcpdump/ptcpdump
190180
191181
- name: Test filter by process id
192182
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
183+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
193184
with:
194185
provision: 'false'
195186
cmd: |
196187
set -ex
197-
uname -a
198-
cat /etc/issue
188+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
199189
200190
bash /host/testdata/test_pid_filter.sh /host/ptcpdump/ptcpdump
201191
@@ -205,8 +195,7 @@ jobs:
205195
provision: 'false'
206196
cmd: |
207197
set -ex
208-
uname -a
209-
cat /etc/issue
198+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
210199
211200
bash /host/testdata/test_read_pcap.sh /host/ptcpdump/ptcpdump
212201
@@ -216,8 +205,7 @@ jobs:
216205
provision: 'false'
217206
cmd: |
218207
set -ex
219-
uname -a
220-
cat /etc/issue
208+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
221209
222210
bash /host/testdata/test_write_pcap.sh /host/ptcpdump/ptcpdump
223211
@@ -227,74 +215,76 @@ jobs:
227215
provision: 'false'
228216
cmd: |
229217
set -ex
230-
uname -a
231-
cat /etc/issue
218+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
232219
233220
bash /host/testdata/test_write_stdout.sh /host/ptcpdump/ptcpdump
234221
235222
- name: Test exist connections
236223
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
224+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
237225
with:
238226
provision: 'false'
239227
cmd: |
240228
set -ex
241-
uname -a
242-
cat /etc/issue
229+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
243230
244231
bash /host/testdata/test_exist_connection.sh /host/ptcpdump/ptcpdump
245232
246233
- name: Test arp and icmp
247-
if: ${{ (!startsWith(matrix.kernel, '5.4')) && (!startsWith(matrix.kernel, '4.')) }}
234+
if: ${{ (!startsWith(matrix.kernel, '5.4')) && (!startsWith(matrix.kernel, '4.')) && (!startsWith(matrix.backend, 'cgroup-skb')) }}
248235
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
249236
with:
250237
provision: 'false'
251238
cmd: |
252239
set -ex
253-
uname -a
254-
cat /etc/issue
240+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
255241
256242
bash /host/testdata/test_arp.sh /host/ptcpdump/ptcpdump
257243
bash /host/testdata/test_icmp.sh /host/ptcpdump/ptcpdump
258244
259245
- name: Test netns
260-
# if: ${{ (!startsWith(matrix.kernel, '5.4')) && (!startsWith(matrix.kernel, '4.')) }}
246+
if: ${{ !startsWith(matrix.backend, 'cgroup-skb') }}
261247
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
262248
with:
263249
provision: 'false'
264250
cmd: |
265251
set -ex
252+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
253+
266254
bash /host/testdata/test_netns.sh /host/ptcpdump/ptcpdump
267255
268256
- name: Test netns newly
269-
# if: ${{ (!startsWith(matrix.kernel, '5.4')) && (!startsWith(matrix.kernel, '4.')) }}
257+
if: ${{ !startsWith(matrix.backend, 'cgroup-skb') }}
270258
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
271259
with:
272260
provision: 'false'
273261
cmd: |
274262
set -ex
263+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
264+
275265
bash /host/testdata/test_netns_newly_normal.sh /host/ptcpdump/ptcpdump
276266
bash /host/testdata/test_netns_newly_exec.sh /host/ptcpdump/ptcpdump
277267
278268
- name: Test run sub program
279269
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
270+
if: ${{ ( !( (startsWith(matrix.backend, 'cgroup-skb') && (startsWith(matrix.kernel, '5.4') || startsWith(matrix.kernel, '4.')) ) ) ) }}
280271
with:
281272
provision: 'false'
282273
cmd: |
283274
set -ex
284-
uname -a
285-
cat /etc/issue
275+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
286276
287277
bash /host/testdata/test_sub_program.sh /host/ptcpdump/ptcpdump
288278
bash /host/testdata/test_sub_curl_domain_program.sh /host/ptcpdump/ptcpdump
289279
290280
- name: Test NAT
291281
uses: cilium/little-vm-helper@e87948476ca97050b1f149ab2aec379d0de19b84 # v0.0.23
282+
if: ${{ !startsWith(matrix.backend, 'cgroup-skb') }}
292283
with:
293284
provision: 'false'
294285
cmd: |
295286
set -ex
296-
uname -a
297-
cat /etc/issue
287+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
298288
299289
for i in {1..10}; do
300290
bash /host/testdata/test_nat.sh /host/ptcpdump/ptcpdump && exit 0 || sleep 1
@@ -313,8 +303,7 @@ jobs:
313303
provision: 'false'
314304
cmd: |
315305
set -ex
316-
uname -a
317-
cat /etc/issue
306+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
318307
319308
ls -lh /host/testdata/gohttpapp
320309
@@ -332,8 +321,7 @@ jobs:
332321
provision: 'false'
333322
cmd: |
334323
set -ex
335-
uname -a
336-
cat /etc/issue
324+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
337325
338326
ls -lh /host/testdata/gohttpapp
339327
@@ -351,8 +339,7 @@ jobs:
351339
provision: 'false'
352340
cmd: |
353341
set -ex
354-
uname -a
355-
cat /etc/issue
342+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
356343
357344
ls -lh /host/testdata/gohttpapp
358345
@@ -370,8 +357,7 @@ jobs:
370357
provision: 'false'
371358
cmd: |
372359
set -ex
373-
uname -a
374-
cat /etc/issue
360+
export PTCPDUMP_EXTRA_ARGS="${{ env.PTCPDUMP_EXTRA_ARGS }}"
375361
376362
ls -lh /host/testdata/gohttpapp
377363

bpf/bpf_arm64_bpfel.o

15.7 KB
Binary file not shown.

bpf/bpf_legacy_arm64_bpfel.o

-584 Bytes
Binary file not shown.

bpf/bpf_legacy_x86_bpfel.o

-584 Bytes
Binary file not shown.

bpf/bpf_no_tracing_arm64_bpfel.o

15.9 KB
Binary file not shown.

bpf/bpf_no_tracing_x86_bpfel.o

15.9 KB
Binary file not shown.

bpf/bpf_x86_bpfel.o

15.7 KB
Binary file not shown.

bpf/ptcpdump.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,20 @@ static __always_inline int fill_packet_event_meta(struct __sk_buff *skb, bool cg
303303
if (cgroup_skb && egress) {
304304
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
305305
if (task && !is_kernel_thread(task)) {
306+
if (parent_process_filter(task) < 0) {
307+
if (process_filter(task) < 0) {
308+
goto outer;
309+
}
310+
}
306311
event_meta->l3_protocol = bpf_ntohs(skb->protocol);
307312
fill_process_meta_with_thread(task, pid_meta);
308313
if (pid_meta->pid > 0) {
309314
// debug_log("[ptcpdump][cgroup_sk] get_current_task success\n");
310315
return 0;
311316
}
312317
}
318+
outer:
319+
(void)0;
313320
// debug_log("[ptcpdump][cgroup_sk] get_current_task failed\n");
314321
}
315322

testdata/test_base.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ RNAME="${FILE_PREFIX}_base.read.txt"
1010

1111

1212
function test_ptcpdump() {
13-
timeout 30s ${CMD} -c 1 -v -i any --print -w "${FNAME}" \
13+
timeout 30s ${CMD} -c 1 -v -i any ${PTCPDUMP_EXTRA_ARGS} --print -w "${FNAME}" \
1414
'dst host 1.1.1.1 and tcp[tcpflags] = tcp-syn' | tee "${LNAME}" &
1515
sleep 10
1616
curl -m 10 1.1.1.1 &>/dev/null || true

0 commit comments

Comments
 (0)