Skip to content

Commit

Permalink
selftest: fix cgroup-legacy selftest
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldtinoco committed May 2, 2023
1 parent c291411 commit eba93d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
29 changes: 22 additions & 7 deletions selftest/cgroup-legacy/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>

struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(max_entries, 10);
__uint(max_entries, 1024);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(u32));
} perfbuffer SEC(".maps");
Expand All @@ -31,22 +32,36 @@ int cgroup__skb_ingress(struct __sk_buff *ctx)
}

struct iphdr ip = {0};
if (bpf_skb_load_bytes_relative(ctx, 0, &ip, sizeof(ip), BPF_HDR_START_NET))
struct icmphdr icmp = {0};

u32 s_iphdr = bpf_core_type_size(struct iphdr);
u32 s_icmphdr = bpf_core_type_size(struct icmphdr);

if (bpf_skb_load_bytes_relative(ctx, 0, &ip, s_iphdr, BPF_HDR_START_NET))
return 1;

struct icmphdr icmp = {0};
// clang-format off

switch (ip.protocol) {
case IPPROTO_ICMP:
if (bpf_skb_load_bytes_relative(
ctx, sizeof(ip), &icmp, sizeof(struct icmphdr), BPF_HDR_START_NET))
if (bpf_skb_load_bytes_relative(ctx,
s_iphdr,
&icmp,
s_icmphdr,
BPF_HDR_START_NET))
return 1;

u32 bleh = 20220823;
bpf_perf_event_output(ctx, &perfbuffer, BPF_F_CURRENT_CPU, &bleh, sizeof(bleh));
bpf_perf_event_output(ctx,
&perfbuffer,
BPF_F_CURRENT_CPU,
&bleh,
sizeof(bleh));
break;
}

// clang-format on

return 1;
}

Expand Down
9 changes: 6 additions & 3 deletions selftest/cgroup-legacy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ func main() {

cgroupRootDir := getCgroupV2RootDir()

// link, err := prog.AttachCgroup(cgroupRootDir)
link, err := prog.AttachCgroupLegacy(cgroupRootDir, bpf.BPFAttachTypeCgroupInetIngress)
if err != nil {
Error(err)
}

eventsChannel := make(chan []byte, 100)
lostChannel := make(chan uint64, 10)
eventsChannel := make(chan []byte, 1)
lostChannel := make(chan uint64, 1)

// initialize an eBPF perf buffer to receive events
bpfPerfBuffer, err := bpfModule.InitPerfBuf(
Expand All @@ -63,7 +64,7 @@ func main() {
defer stop()

// start eBPF perf buffer event polling
bpfPerfBuffer.Poll(300)
bpfPerfBuffer.Poll(5000)

go func() {
_, err := exec.Command("ping", "127.0.0.1", "-c 5", "-w 10").Output()
Expand All @@ -76,10 +77,12 @@ func main() {

testPassed := false
numberOfEventsReceived := 0

LOOP:
for {
select {
case raw := <-eventsChannel:

value := int(binary.LittleEndian.Uint32(raw))
if value == 20220823 {
fmt.Println("Received correct event.")
Expand Down

0 comments on commit eba93d9

Please sign in to comment.