summaryrefslogtreecommitdiffstats
path: root/samples/bpf/libbpf.c
diff options
context:
space:
mode:
Diffstat (limited to 'samples/bpf/libbpf.c')
-rw-r--r--samples/bpf/libbpf.c111
1 files changed, 0 insertions, 111 deletions
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c
index 6f076abdca35..3391225ad7e9 100644
--- a/samples/bpf/libbpf.c
+++ b/samples/bpf/libbpf.c
@@ -4,8 +4,6 @@
4#include <linux/unistd.h> 4#include <linux/unistd.h>
5#include <unistd.h> 5#include <unistd.h>
6#include <string.h> 6#include <string.h>
7#include <linux/netlink.h>
8#include <linux/bpf.h>
9#include <errno.h> 7#include <errno.h>
10#include <net/ethernet.h> 8#include <net/ethernet.h>
11#include <net/if.h> 9#include <net/if.h>
@@ -13,96 +11,6 @@
13#include <arpa/inet.h> 11#include <arpa/inet.h>
14#include "libbpf.h" 12#include "libbpf.h"
15 13
16static __u64 ptr_to_u64(void *ptr)
17{
18 return (__u64) (unsigned long) ptr;
19}
20
21int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
22 int max_entries, int map_flags)
23{
24 union bpf_attr attr = {
25 .map_type = map_type,
26 .key_size = key_size,
27 .value_size = value_size,
28 .max_entries = max_entries,
29 .map_flags = map_flags,
30 };
31
32 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
33}
34
35int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags)
36{
37 union bpf_attr attr = {
38 .map_fd = fd,
39 .key = ptr_to_u64(key),
40 .value = ptr_to_u64(value),
41 .flags = flags,
42 };
43
44 return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
45}
46
47int bpf_map_lookup_elem(int fd, void *key, void *value)
48{
49 union bpf_attr attr = {
50 .map_fd = fd,
51 .key = ptr_to_u64(key),
52 .value = ptr_to_u64(value),
53 };
54
55 return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
56}
57
58int bpf_map_delete_elem(int fd, void *key)
59{
60 union bpf_attr attr = {
61 .map_fd = fd,
62 .key = ptr_to_u64(key),
63 };
64
65 return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
66}
67
68int bpf_map_get_next_key(int fd, void *key, void *next_key)
69{
70 union bpf_attr attr = {
71 .map_fd = fd,
72 .key = ptr_to_u64(key),
73 .next_key = ptr_to_u64(next_key),
74 };
75
76 return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
77}
78
79#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
80
81int bpf_load_program(enum bpf_prog_type prog_type,
82 const struct bpf_insn *insns, int prog_len,
83 const char *license, int kern_version,
84 char *log_buf, size_t log_buf_sz)
85{
86 union bpf_attr attr = {
87 .prog_type = prog_type,
88 .insns = ptr_to_u64((void *) insns),
89 .insn_cnt = prog_len / sizeof(struct bpf_insn),
90 .license = ptr_to_u64((void *) license),
91 .log_buf = ptr_to_u64(log_buf),
92 .log_size = log_buf_sz,
93 .log_level = 1,
94 };
95
96 /* assign one field outside of struct init to make sure any
97 * padding is zero initialized
98 */
99 attr.kern_version = kern_version;
100
101 log_buf[0] = 0;
102
103 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
104}
105
106int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) 14int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
107{ 15{
108 union bpf_attr attr = { 16 union bpf_attr attr = {
@@ -124,25 +32,6 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
124 return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr)); 32 return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
125} 33}
126 34
127int bpf_obj_pin(int fd, const char *pathname)
128{
129 union bpf_attr attr = {
130 .pathname = ptr_to_u64((void *)pathname),
131 .bpf_fd = fd,
132 };
133
134 return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
135}
136
137int bpf_obj_get(const char *pathname)
138{
139 union bpf_attr attr = {
140 .pathname = ptr_to_u64((void *)pathname),
141 };
142
143 return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
144}
145
146int open_raw_sock(const char *name) 35int open_raw_sock(const char *name)
147{ 36{
148 struct sockaddr_ll sll; 37 struct sockaddr_ll sll;