Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Add exit judgement for cgroupv1 only host #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ package main
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -target bpf -cflags "-D__TARGET_ARCH_x86" bpf_sockops bpf/bpf_sockops.c

import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
v2 "github.com/containers/common/pkg/cgroupv2"
"golang.org/x/sys/unix"
"os"
"os/signal"
"path/filepath"
)

const (
Expand Down Expand Up @@ -53,13 +53,17 @@ func getCgroupPath() (string, error) {
enabled, err := v2.Enabled()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if v2 is enabled first to determine raising error or not, and check if v1 is enabled to determine the path. Otherwise in current change, it returns "/sys/fs/cgroup" when both v1 and v2 enabled.

if !enabled {
cgroupPath = filepath.Join(cgroupPath, "unified")
if _, err = os.Stat(cgroupPath); errors.Is(err, os.ErrNotExist) {
fmt.Println("tcpip-bypass needs CgroupV2 support on host!")
}
}
return cgroupPath, err
}

func loadProgram(prog BypassProgram) (BypassProgram, error) {
var err error
var options ebpf.CollectionOptions
var cgroupPath string

err = os.Mkdir(MapsPinpath, os.ModePerm)
if err != nil {
Expand All @@ -84,7 +88,7 @@ func loadProgram(prog BypassProgram) (BypassProgram, error) {
fmt.Println("Error load objects:", err)
}

if cgroupPath, err := getCgroupPath(); err == nil {
if cgroupPath, err = getCgroupPath(); err == nil {
prog.SockopsCgroup, err = link.AttachCgroup(link.CgroupOptions{
Path: cgroupPath,
Attach: ebpf.AttachCGroupSockOps,
Expand Down