2016-02-29 07:12:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <asm/unistd.h>
|
2016-02-29 07:52:26 +00:00
|
|
|
#include <errno.h>
|
2016-02-29 07:12:34 +00:00
|
|
|
#include <stdio.h>
|
2016-03-18 18:13:40 +00:00
|
|
|
#include <stdint.h>
|
2016-02-29 07:12:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-08-25 18:08:23 +00:00
|
|
|
static int load_simple_bpf(int prog_type, int ret) {
|
2016-02-29 07:52:26 +00:00
|
|
|
#ifdef __NR_bpf
|
2016-08-25 18:08:23 +00:00
|
|
|
// { return ret; }
|
2016-02-29 07:12:34 +00:00
|
|
|
__u64 __attribute__((aligned(8))) insns[] = {
|
2016-08-25 18:08:23 +00:00
|
|
|
0x00000000000000b7ull | ((__u64)ret<<32),
|
2016-02-29 07:12:34 +00:00
|
|
|
0x0000000000000095ull,
|
|
|
|
};
|
|
|
|
__u8 __attribute__((aligned(8))) license[] = "ASL2";
|
|
|
|
// Copied from a header file since libc is notoriously slow to update.
|
|
|
|
// The call will succeed or fail and that will be our indication on
|
|
|
|
// whether or not it is supported.
|
|
|
|
struct {
|
|
|
|
__u32 prog_type;
|
|
|
|
__u32 insn_cnt;
|
|
|
|
__u64 insns;
|
|
|
|
__u64 license;
|
|
|
|
__u32 log_level;
|
|
|
|
__u32 log_size;
|
|
|
|
__u64 log_buf;
|
|
|
|
__u32 kern_version;
|
|
|
|
} __attribute__((aligned(8))) attr = {
|
|
|
|
.prog_type = prog_type,
|
|
|
|
.insn_cnt = 2,
|
2016-03-18 18:13:40 +00:00
|
|
|
.insns = (uintptr_t)&insns,
|
|
|
|
.license = (uintptr_t)&license,
|
2016-02-29 07:12:34 +00:00
|
|
|
};
|
|
|
|
return syscall(__NR_bpf, 5, &attr, sizeof(attr));
|
2016-02-29 07:52:26 +00:00
|
|
|
#else
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
#endif
|
2016-02-29 07:12:34 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
type BpfProgType C.int
|
|
|
|
|
|
|
|
const (
|
|
|
|
BPF_PROG_TYPE_UNSPEC BpfProgType = iota
|
|
|
|
BPF_PROG_TYPE_SOCKET_FILTER
|
|
|
|
BPF_PROG_TYPE_KPROBE
|
|
|
|
BPF_PROG_TYPE_SCHED_CLS
|
|
|
|
BPF_PROG_TYPE_SCHED_ACT
|
2016-08-25 18:08:23 +00:00
|
|
|
BPF_PROG_TYPE_TRACEPOINT
|
|
|
|
BPF_PROG_TYPE_XDP
|
2016-02-29 07:12:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// loadSimpleBpf loads a trivial bpf program for testing purposes
|
2016-08-25 18:08:23 +00:00
|
|
|
func loadSimpleBpf(progType BpfProgType, ret int) (int, error) {
|
|
|
|
fd, err := C.load_simple_bpf(C.int(progType), C.int(ret))
|
2016-02-29 07:12:34 +00:00
|
|
|
return int(fd), err
|
|
|
|
}
|