Skip to content

Commit 23f0450

Browse files
Only probe ipv6 if present (#227)
* Only probe ipv6 if present * Add testing support for non-ipv6 kernels * Add tests for new feature array. * Only test ipv6 connections when ipv6 is enabled in the kernel.
1 parent 4a12560 commit 23f0450

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

non-GPL/Events/EventsTrace/EventsTrace.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,8 @@ static int event_ctx_callback(struct ebpf_event_header *evt_hdr)
12111211
static void print_init_msg(uint64_t features)
12121212
{
12131213
printf("{\"probes_initialized\": true, \"features\": {");
1214-
printf("\"bpf_tramp\": %s", (features & EBPF_FEATURE_BPF_TRAMP) ? "true" : "false");
1214+
printf("\"bpf_tramp\": %s,", (features & EBPF_FEATURE_BPF_TRAMP) ? "true" : "false");
1215+
printf("\"ipv6\": %s", (features & EBPF_FEATURE_IPV6) ? "true" : "false");
12151216
printf("}}\n");
12161217
}
12171218

non-GPL/Events/Lib/EbpfEvents.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdbool.h>
1818
#include <stdio.h>
1919
#include <sys/resource.h>
20+
#include <sys/socket.h>
2021
#include <sys/utsname.h>
2122
#include <unistd.h>
2223

@@ -320,6 +321,7 @@ static int probe_set_autoload(struct btf *btf, struct EventProbe_bpf *obj, uint6
320321
{
321322
int err = 0;
322323
bool has_bpf_tramp = features & EBPF_FEATURE_BPF_TRAMP;
324+
bool has_ipv6 = features & EBPF_FEATURE_IPV6;
323325

324326
// do_renameat2 kprobe and fentry probe are mutually exclusive.
325327
// disable auto-loading of kprobe if `do_renameat2` exists in BTF and
@@ -333,10 +335,16 @@ static int probe_set_autoload(struct btf *btf, struct EventProbe_bpf *obj, uint6
333335
// tcp_v6_connect kprobes and fexit probe are mutually exclusive.
334336
// disable auto-loading of kprobes if `tcp_v6_connect` exists in BTF and
335337
// if bpf trampolines are supported on the current arch, and vice-versa.
336-
if (has_bpf_tramp && BTF_FUNC_EXISTS(btf, tcp_v6_connect)) {
338+
if (has_ipv6) {
339+
if (has_bpf_tramp && BTF_FUNC_EXISTS(btf, tcp_v6_connect)) {
340+
err = err ?: bpf_program__set_autoload(obj->progs.kprobe__tcp_v6_connect, false);
341+
err = err ?: bpf_program__set_autoload(obj->progs.kretprobe__tcp_v6_connect, false);
342+
} else {
343+
err = err ?: bpf_program__set_autoload(obj->progs.fexit__tcp_v6_connect, false);
344+
}
345+
} else {
337346
err = err ?: bpf_program__set_autoload(obj->progs.kprobe__tcp_v6_connect, false);
338347
err = err ?: bpf_program__set_autoload(obj->progs.kretprobe__tcp_v6_connect, false);
339-
} else {
340348
err = err ?: bpf_program__set_autoload(obj->progs.fexit__tcp_v6_connect, false);
341349
}
342350

@@ -531,13 +539,27 @@ static bool system_has_bpf_tramp(void)
531539
return ret;
532540
}
533541

542+
static bool system_has_ipv6(void)
543+
{
544+
int fd;
545+
bool supported = false;
546+
if ((fd = socket(AF_INET6, SOCK_STREAM, 0)) != -1) {
547+
close(fd);
548+
supported = true;
549+
}
550+
return supported;
551+
}
552+
534553
static uint64_t detect_system_features(void)
535554
{
536555
uint64_t features = 0;
537556

538557
if (system_has_bpf_tramp())
539558
features |= EBPF_FEATURE_BPF_TRAMP;
540559

560+
if (system_has_ipv6())
561+
features |= EBPF_FEATURE_IPV6;
562+
541563
return features;
542564
}
543565

non-GPL/Events/Lib/EbpfEvents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
enum ebpf_kernel_feature {
1919
EBPF_FEATURE_BPF_TRAMP = (1 << 0),
20+
EBPF_FEATURE_IPV6 = (1 << 1),
2021
};
2122

2223
/* Opaque context */

testing/testrunner/ebpf_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func FeaturesCorrect(t *testing.T, et *Runner) {
7070
default:
7171
t.Fatalf("unknown arch %s, please add to the TestFeaturesCorrect test", arch)
7272
}
73+
74+
// test for IPv6 support feature
75+
if _, err := os.Stat("/proc/sys/net/ipv6"); err == nil {
76+
require.True(t, et.InitMsg.Features.IPv6)
77+
} else {
78+
require.False(t, et.InitMsg.Features.IPv6)
79+
}
7380
}
7481

7582
func ForkExit(t *testing.T, et *Runner) {
@@ -766,21 +773,33 @@ func TestEbpf(t *testing.T) {
766773
{"Tcpv4ConnectionAttempt", Tcpv4ConnectionAttempt, []string{"--net-conn-attempt"}, false},
767774
{"Tcpv4ConnectionAccept", Tcpv4ConnectionAccept, []string{"--net-conn-accept"}, false},
768775
{"Tcpv4ConnectionClose", Tcpv4ConnectionClose, []string{"--net-conn-close"}, false},
769-
{"Tcpv6ConnectionAttempt", Tcpv6ConnectionAttempt, []string{"--net-conn-attempt"}, false},
770-
{"Tcpv6ConnectionAccept", Tcpv6ConnectionAccept, []string{"--net-conn-accept"}, false},
771-
{"Tcpv6ConnectionClose", Tcpv6ConnectionClose, []string{"--net-conn-close"}, false},
772776
{"DNSMonitor", DNSMonitor, []string{"--net-conn-dns-pkt"}, false},
773777
{"Ptrace", Ptrace, []string{"--process-ptrace"}, false},
774778
{"Shmget", Shmget, []string{"--process-shmget"}, false},
775779
{"MemfdCreate", MemfdCreate, []string{"--process-memfd-create", "--process-exec"}, false},
776-
777780
{"TcFilter", TcFilter, []string{}, false},
778-
779781
{"FileCreateContainer", FileCreateContainer, []string{"--file-create"}, true},
780782
{"FileRenameContainer", FileRenameContainer, []string{"--file-rename"}, true},
781783
{"FileDeleteContainer", FileDeleteContainer, []string{"--file-delete"}, true},
782784
}
783785

786+
// Conditionally add IPv6 tests if IPv6 is supported
787+
if _, err := os.Stat("/proc/sys/net/ipv6"); err == nil {
788+
// Add all IPv6 test cases
789+
ipv6Tests := []struct {
790+
name string
791+
handle func(t *testing.T, et *Runner)
792+
args []string
793+
requireOverlayFS bool
794+
}{
795+
{"Tcpv6ConnectionAttempt", Tcpv6ConnectionAttempt, []string{"--net-conn-attempt"}, false},
796+
{"Tcpv6ConnectionAccept", Tcpv6ConnectionAccept, []string{"--net-conn-accept"}, false},
797+
{"Tcpv6ConnectionClose", Tcpv6ConnectionClose, []string{"--net-conn-close"}, false},
798+
}
799+
800+
testCases = append(testCases, ipv6Tests...)
801+
}
802+
784803
failed := false
785804

786805
for _, test := range testCases {

testing/testrunner/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type InitMsg struct {
4242
InitSuccess bool `json:"probes_initialized"`
4343
Features struct {
4444
BpfTramp bool `json:"bpf_tramp"`
45+
IPv6 bool `json:"ipv6"`
4546
} `json:"features"`
4647
}
4748

0 commit comments

Comments
 (0)