File: bpf_linux.go

package info (click to toggle)
golang-github-vishvananda-netlink 0.0~git20160306.0.4fdf23c-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 476 kB
  • sloc: makefile: 24
file content (59 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package netlink

/*
#include <asm/types.h>
#include <asm/unistd.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

static int load_simple_bpf(int prog_type) {
#ifdef __NR_bpf
	// { return 1; }
	__u64 __attribute__((aligned(8))) insns[] = {
		0x00000001000000b7ull,
		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,
		.insns = (__u64)&insns,
		.license = (__u64)&license,
	};
	return syscall(__NR_bpf, 5, &attr, sizeof(attr));
#else
	errno = EINVAL;
	return -1;
#endif
}
*/
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
)

// loadSimpleBpf loads a trivial bpf program for testing purposes
func loadSimpleBpf(progType BpfProgType) (int, error) {
	fd, err := C.load_simple_bpf(C.int(progType))
	return int(fd), err
}