1 /*
2 * Copyright (c) 2015 PLUMgrid, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "bpf_common.h"
17 #include "bpf_module.h"
18
19 extern "C" {
bpf_module_create_b(const char * filename,const char * proto_filename,unsigned flags)20 void * bpf_module_create_b(const char *filename, const char *proto_filename, unsigned flags) {
21 auto mod = new ebpf::BPFModule(flags);
22 if (mod->load_b(filename, proto_filename) != 0) {
23 delete mod;
24 return nullptr;
25 }
26 return mod;
27 }
28
bpf_module_create_c(const char * filename,unsigned flags,const char * cflags[],int ncflags)29 void * bpf_module_create_c(const char *filename, unsigned flags, const char *cflags[], int ncflags) {
30 auto mod = new ebpf::BPFModule(flags);
31 if (mod->load_c(filename, cflags, ncflags) != 0) {
32 delete mod;
33 return nullptr;
34 }
35 return mod;
36 }
37
bpf_module_create_c_from_string(const char * text,unsigned flags,const char * cflags[],int ncflags)38 void * bpf_module_create_c_from_string(const char *text, unsigned flags, const char *cflags[], int ncflags) {
39 auto mod = new ebpf::BPFModule(flags);
40 if (mod->load_string(text, cflags, ncflags) != 0) {
41 delete mod;
42 return nullptr;
43 }
44 return mod;
45 }
46
bpf_module_destroy(void * program)47 void bpf_module_destroy(void *program) {
48 auto mod = static_cast<ebpf::BPFModule *>(program);
49 if (!mod) return;
50 delete mod;
51 }
52
bpf_num_functions(void * program)53 size_t bpf_num_functions(void *program) {
54 auto mod = static_cast<ebpf::BPFModule *>(program);
55 if (!mod) return 0;
56 return mod->num_functions();
57 }
58
bpf_function_name(void * program,size_t id)59 const char * bpf_function_name(void *program, size_t id) {
60 auto mod = static_cast<ebpf::BPFModule *>(program);
61 if (!mod) return nullptr;
62 return mod->function_name(id);
63 }
64
bpf_function_start(void * program,const char * name)65 void * bpf_function_start(void *program, const char *name) {
66 auto mod = static_cast<ebpf::BPFModule *>(program);
67 if (!mod) return nullptr;
68 return mod->function_start(name);
69 }
70
bpf_function_start_id(void * program,size_t id)71 void * bpf_function_start_id(void *program, size_t id) {
72 auto mod = static_cast<ebpf::BPFModule *>(program);
73 if (!mod) return nullptr;
74 return mod->function_start(id);
75 }
76
bpf_function_size(void * program,const char * name)77 size_t bpf_function_size(void *program, const char *name) {
78 auto mod = static_cast<ebpf::BPFModule *>(program);
79 if (!mod) return 0;
80 return mod->function_size(name);
81 }
82
bpf_function_size_id(void * program,size_t id)83 size_t bpf_function_size_id(void *program, size_t id) {
84 auto mod = static_cast<ebpf::BPFModule *>(program);
85 if (!mod) return 0;
86 return mod->function_size(id);
87 }
88
bpf_module_license(void * program)89 char * bpf_module_license(void *program) {
90 auto mod = static_cast<ebpf::BPFModule *>(program);
91 if (!mod) return nullptr;
92 return mod->license();
93 }
94
bpf_module_kern_version(void * program)95 unsigned bpf_module_kern_version(void *program) {
96 auto mod = static_cast<ebpf::BPFModule *>(program);
97 if (!mod) return 0;
98 return mod->kern_version();
99 }
100
bpf_num_tables(void * program)101 size_t bpf_num_tables(void *program) {
102 auto mod = static_cast<ebpf::BPFModule *>(program);
103 if (!mod) return -1;
104 return mod->num_tables();
105 }
106
bpf_table_id(void * program,const char * table_name)107 size_t bpf_table_id(void *program, const char *table_name) {
108 auto mod = static_cast<ebpf::BPFModule *>(program);
109 if (!mod) return ~0ull;
110 return mod->table_id(table_name);
111 }
112
bpf_table_fd(void * program,const char * table_name)113 int bpf_table_fd(void *program, const char *table_name) {
114 auto mod = static_cast<ebpf::BPFModule *>(program);
115 if (!mod) return -1;
116 return mod->table_fd(table_name);
117 }
118
bpf_table_fd_id(void * program,size_t id)119 int bpf_table_fd_id(void *program, size_t id) {
120 auto mod = static_cast<ebpf::BPFModule *>(program);
121 if (!mod) return -1;
122 return mod->table_fd(id);
123 }
124
bpf_table_type(void * program,const char * table_name)125 int bpf_table_type(void *program, const char *table_name) {
126 auto mod = static_cast<ebpf::BPFModule *>(program);
127 if (!mod) return -1;
128 return mod->table_type(table_name);
129 }
130
bpf_table_type_id(void * program,size_t id)131 int bpf_table_type_id(void *program, size_t id) {
132 auto mod = static_cast<ebpf::BPFModule *>(program);
133 if (!mod) return -1;
134 return mod->table_type(id);
135 }
136
bpf_table_max_entries(void * program,const char * table_name)137 size_t bpf_table_max_entries(void *program, const char *table_name) {
138 auto mod = static_cast<ebpf::BPFModule *>(program);
139 if (!mod) return 0;
140 return mod->table_max_entries(table_name);
141 }
142
bpf_table_max_entries_id(void * program,size_t id)143 size_t bpf_table_max_entries_id(void *program, size_t id) {
144 auto mod = static_cast<ebpf::BPFModule *>(program);
145 if (!mod) return 0;
146 return mod->table_max_entries(id);
147 }
148
bpf_table_flags(void * program,const char * table_name)149 int bpf_table_flags(void *program, const char *table_name) {
150 auto mod = static_cast<ebpf::BPFModule *>(program);
151 if (!mod) return -1;
152 return mod->table_flags(table_name);
153 }
154
bpf_table_flags_id(void * program,size_t id)155 int bpf_table_flags_id(void *program, size_t id) {
156 auto mod = static_cast<ebpf::BPFModule *>(program);
157 if (!mod) return -1;
158 return mod->table_flags(id);
159 }
160
bpf_table_name(void * program,size_t id)161 const char * bpf_table_name(void *program, size_t id) {
162 auto mod = static_cast<ebpf::BPFModule *>(program);
163 if (!mod) return nullptr;
164 return mod->table_name(id);
165 }
166
bpf_table_key_desc(void * program,const char * table_name)167 const char * bpf_table_key_desc(void *program, const char *table_name) {
168 auto mod = static_cast<ebpf::BPFModule *>(program);
169 if (!mod) return nullptr;
170 return mod->table_key_desc(table_name);
171 }
172
bpf_table_key_desc_id(void * program,size_t id)173 const char * bpf_table_key_desc_id(void *program, size_t id) {
174 auto mod = static_cast<ebpf::BPFModule *>(program);
175 if (!mod) return nullptr;
176 return mod->table_key_desc(id);
177 }
178
bpf_table_leaf_desc(void * program,const char * table_name)179 const char * bpf_table_leaf_desc(void *program, const char *table_name) {
180 auto mod = static_cast<ebpf::BPFModule *>(program);
181 if (!mod) return nullptr;
182 return mod->table_leaf_desc(table_name);
183 }
184
bpf_table_leaf_desc_id(void * program,size_t id)185 const char * bpf_table_leaf_desc_id(void *program, size_t id) {
186 auto mod = static_cast<ebpf::BPFModule *>(program);
187 if (!mod) return nullptr;
188 return mod->table_leaf_desc(id);
189 }
190
bpf_table_key_size(void * program,const char * table_name)191 size_t bpf_table_key_size(void *program, const char *table_name) {
192 auto mod = static_cast<ebpf::BPFModule *>(program);
193 if (!mod) return 0;
194 return mod->table_key_size(table_name);
195 }
196
bpf_table_key_size_id(void * program,size_t id)197 size_t bpf_table_key_size_id(void *program, size_t id) {
198 auto mod = static_cast<ebpf::BPFModule *>(program);
199 if (!mod) return 0;
200 return mod->table_key_size(id);
201 }
202
bpf_table_leaf_size(void * program,const char * table_name)203 size_t bpf_table_leaf_size(void *program, const char *table_name) {
204 auto mod = static_cast<ebpf::BPFModule *>(program);
205 if (!mod) return 0;
206 return mod->table_leaf_size(table_name);
207 }
208
bpf_table_leaf_size_id(void * program,size_t id)209 size_t bpf_table_leaf_size_id(void *program, size_t id) {
210 auto mod = static_cast<ebpf::BPFModule *>(program);
211 if (!mod) return 0;
212 return mod->table_leaf_size(id);
213 }
214
bpf_table_key_snprintf(void * program,size_t id,char * buf,size_t buflen,const void * key)215 int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key) {
216 auto mod = static_cast<ebpf::BPFModule *>(program);
217 if (!mod) return -1;
218 return mod->table_key_printf(id, buf, buflen, key);
219 }
bpf_table_leaf_snprintf(void * program,size_t id,char * buf,size_t buflen,const void * leaf)220 int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf) {
221 auto mod = static_cast<ebpf::BPFModule *>(program);
222 if (!mod) return -1;
223 return mod->table_leaf_printf(id, buf, buflen, leaf);
224 }
225
bpf_table_key_sscanf(void * program,size_t id,const char * buf,void * key)226 int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key) {
227 auto mod = static_cast<ebpf::BPFModule *>(program);
228 if (!mod) return -1;
229 return mod->table_key_scanf(id, buf, key);
230 }
bpf_table_leaf_sscanf(void * program,size_t id,const char * buf,void * leaf)231 int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf) {
232 auto mod = static_cast<ebpf::BPFModule *>(program);
233 if (!mod) return -1;
234 return mod->table_leaf_scanf(id, buf, leaf);
235 }
236
237 }
238