]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - samples/bpf/libbpf.c
1 /* eBPF mini library */
4 #include <linux/unistd.h>
8 #include <net/ethernet.h>
10 #include <linux/if_packet.h>
11 #include <arpa/inet.h>
14 int bpf_prog_attach(int prog_fd
, int target_fd
, enum bpf_attach_type type
)
16 union bpf_attr attr
= {
17 .target_fd
= target_fd
,
18 .attach_bpf_fd
= prog_fd
,
22 return syscall(__NR_bpf
, BPF_PROG_ATTACH
, &attr
, sizeof(attr
));
25 int bpf_prog_detach(int target_fd
, enum bpf_attach_type type
)
27 union bpf_attr attr
= {
28 .target_fd
= target_fd
,
32 return syscall(__NR_bpf
, BPF_PROG_DETACH
, &attr
, sizeof(attr
));
35 int open_raw_sock(const char *name
)
37 struct sockaddr_ll sll
;
40 sock
= socket(PF_PACKET
, SOCK_RAW
| SOCK_NONBLOCK
| SOCK_CLOEXEC
, htons(ETH_P_ALL
));
42 printf("cannot create raw socket\n");
46 memset(&sll
, 0, sizeof(sll
));
47 sll
.sll_family
= AF_PACKET
;
48 sll
.sll_ifindex
= if_nametoindex(name
);
49 sll
.sll_protocol
= htons(ETH_P_ALL
);
50 if (bind(sock
, (struct sockaddr
*)&sll
, sizeof(sll
)) < 0) {
51 printf("bind to %s: %s\n", name
, strerror(errno
));
59 int perf_event_open(struct perf_event_attr
*attr
, int pid
, int cpu
,
60 int group_fd
, unsigned long flags
)
62 return syscall(__NR_perf_event_open
, attr
, pid
, cpu
,