iovisor / gobpf

Go bindings for creating BPF programs.
Apache License 2.0
2.14k stars 314 forks source link

elf: Make build compatible with RHEL7 #305

Open anujsrv opened 2 years ago

anujsrv commented 2 years ago

Defining SO_ATTACH_BPF and SO_DETACH_BPF to fix go build and go install on RHEL7 OS. For reference: Kernel version on RHEL7 is 3.10.x with eBPF capabilities backported. (https://access.redhat.com/articles/3078 & https://www.redhat.com/en/blog/introduction-ebpf-red-hat-enterprise-linux-7)

But SO_ATTACH_BPF and SO_DETACH_BPF capabilities were added since kernel version 3.19 onwards.

Without these changes, this is the error I get on my RHEL7 machine (with 3.10.0-1160 kernel) -

$ git branch -l
* master
  socket_compat
$ go build -a ./elf/
# github.com/iovisor/gobpf/elf
elf/module.go: In function 'bpf_attach_socket':
elf/module.go:96:38: error: 'SO_ATTACH_BPF' undeclared (first use in this function)
  return setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
                                      ^
elf/module.go:96:38: note: each undeclared identifier is reported only once for each function it appears in
elf/module.go: In function 'bpf_detach_socket':
elf/module.go:101:38: error: 'SO_DETACH_BPF' undeclared (first use in this function)
  return setsockopt(sock, SOL_SOCKET, SO_DETACH_BPF, &fd, sizeof(fd));
anujsrv commented 2 years ago

@schu Can you have a look at this? Appreciate it.

fanfanbj commented 1 year ago

/assign