\brief Attach a specific BPF program by name and target.
| 186 | |
| 187 | /// \brief Attach a specific BPF program by name and target. |
| 188 | int32_t wasm_bpf_program::attach_bpf_program(const char *name, |
| 189 | const char *attach_target) { |
| 190 | bpf_link *link; |
| 191 | if (!attach_target) { |
| 192 | // Auto-attach based on bpf_program__section_name. This works well for most |
| 193 | // BPF types, including kprobe, uprobe, fentry, lsm, etc. |
| 194 | link = |
| 195 | bpf_program__attach(bpf_object__find_program_by_name(obj.get(), name)); |
| 196 | } else { |
| 197 | bpf_object *o = obj.get(); |
| 198 | bpf_program *prog = bpf_object__find_program_by_name(o, name); |
| 199 | if (!prog) { |
| 200 | spdlog::error("[WasmEdge Wasm_bpf] get prog {} fail"sv, name); |
| 201 | return -1; |
| 202 | } |
| 203 | // TODO: attach dynamically based on bpf_program__section_name(prog) and |
| 204 | // attach_target to support more attach types that libbpf cannot auto |
| 205 | // attach. For example, if bpf_program__section_name(prog) is "xdp" and |
| 206 | // attach_target is "eth0", or attach sockops to a socket fd. For now, we |
| 207 | // will try auto attach as well. |
| 208 | link = |
| 209 | bpf_program__attach(bpf_object__find_program_by_name(obj.get(), name)); |
| 210 | } |
| 211 | if (!link) { |
| 212 | return static_cast<int32_t>(libbpf_get_error(link)); |
| 213 | } |
| 214 | links.emplace(std::unique_ptr<bpf_link, int32_t (*)(bpf_link *obj)>{ |
| 215 | link, bpf_link__destroy}); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /// \brief get map pointer by fd through iterating over all maps |
| 220 | bpf_map *wasm_bpf_program::map_ptr_by_fd(int fd) { |