sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / tools / lib / bpf / bpf.c
blob3ddb58a36d3c2534ce6207581a4fee1730c6e6a0
1 /*
2 * common eBPF ELF operations.
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License (not later!)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses>
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <asm/unistd.h>
26 #include <linux/bpf.h>
27 #include "bpf.h"
30 * When building perf, unistd.h is overrided. __NR_bpf is
31 * required to be defined explicitly.
33 #ifndef __NR_bpf
34 # if defined(__i386__)
35 # define __NR_bpf 357
36 # elif defined(__x86_64__)
37 # define __NR_bpf 321
38 # elif defined(__aarch64__)
39 # define __NR_bpf 280
40 # else
41 # error __NR_bpf not defined. libbpf does not support your arch.
42 # endif
43 #endif
45 static __u64 ptr_to_u64(void *ptr)
47 return (__u64) (unsigned long) ptr;
50 static int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
51 unsigned int size)
53 return syscall(__NR_bpf, cmd, attr, size);
56 int bpf_create_map(enum bpf_map_type map_type, int key_size,
57 int value_size, int max_entries, __u32 map_flags)
59 union bpf_attr attr;
61 memset(&attr, '\0', sizeof(attr));
63 attr.map_type = map_type;
64 attr.key_size = key_size;
65 attr.value_size = value_size;
66 attr.max_entries = max_entries;
67 attr.map_flags = map_flags;
69 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
72 int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
73 size_t insns_cnt, char *license,
74 __u32 kern_version, char *log_buf, size_t log_buf_sz)
76 int fd;
77 union bpf_attr attr;
79 bzero(&attr, sizeof(attr));
80 attr.prog_type = type;
81 attr.insn_cnt = (__u32)insns_cnt;
82 attr.insns = ptr_to_u64(insns);
83 attr.license = ptr_to_u64(license);
84 attr.log_buf = ptr_to_u64(NULL);
85 attr.log_size = 0;
86 attr.log_level = 0;
87 attr.kern_version = kern_version;
89 fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
90 if (fd >= 0 || !log_buf || !log_buf_sz)
91 return fd;
93 /* Try again with log */
94 attr.log_buf = ptr_to_u64(log_buf);
95 attr.log_size = log_buf_sz;
96 attr.log_level = 1;
97 log_buf[0] = 0;
98 return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
101 int bpf_map_update_elem(int fd, void *key, void *value,
102 __u64 flags)
104 union bpf_attr attr;
106 bzero(&attr, sizeof(attr));
107 attr.map_fd = fd;
108 attr.key = ptr_to_u64(key);
109 attr.value = ptr_to_u64(value);
110 attr.flags = flags;
112 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
115 int bpf_map_lookup_elem(int fd, void *key, void *value)
117 union bpf_attr attr;
119 bzero(&attr, sizeof(attr));
120 attr.map_fd = fd;
121 attr.key = ptr_to_u64(key);
122 attr.value = ptr_to_u64(value);
124 return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
127 int bpf_map_delete_elem(int fd, void *key)
129 union bpf_attr attr;
131 bzero(&attr, sizeof(attr));
132 attr.map_fd = fd;
133 attr.key = ptr_to_u64(key);
135 return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
138 int bpf_map_get_next_key(int fd, void *key, void *next_key)
140 union bpf_attr attr;
142 bzero(&attr, sizeof(attr));
143 attr.map_fd = fd;
144 attr.key = ptr_to_u64(key);
145 attr.next_key = ptr_to_u64(next_key);
147 return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
150 int bpf_obj_pin(int fd, const char *pathname)
152 union bpf_attr attr;
154 bzero(&attr, sizeof(attr));
155 attr.pathname = ptr_to_u64((void *)pathname);
156 attr.bpf_fd = fd;
158 return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
161 int bpf_obj_get(const char *pathname)
163 union bpf_attr attr;
165 bzero(&attr, sizeof(attr));
166 attr.pathname = ptr_to_u64((void *)pathname);
168 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
171 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
173 union bpf_attr attr;
175 bzero(&attr, sizeof(attr));
176 attr.target_fd = target_fd;
177 attr.attach_bpf_fd = prog_fd;
178 attr.attach_type = type;
180 return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
183 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
185 union bpf_attr attr;
187 bzero(&attr, sizeof(attr));
188 attr.target_fd = target_fd;
189 attr.attach_type = type;
191 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));