Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | |
| 17 | #ifndef LOG_TAG |
| 18 | #define LOG_TAG "bpfloader" |
| 19 | #endif |
| 20 | |
| 21 | #include <arpa/inet.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 23 | #include <elf.h> |
| 24 | #include <error.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <linux/bpf.h> |
| 28 | #include <linux/unistd.h> |
| 29 | #include <net/if.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include <sys/mman.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 43 | #include <android-base/strings.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 44 | #include <android-base/unique_fd.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 45 | #include <libbpf_android.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 46 | #include <log/log.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 47 | #include <netdutils/Misc.h> |
| 48 | #include <netdutils/Slice.h> |
| 49 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 50 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 51 | using android::base::EndsWith; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 52 | using std::string; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 53 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 54 | #define BPF_PROG_PATH "/system/etc/bpf/" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 55 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 56 | void loadAllElfObjects(void) { |
| 57 | DIR* dir; |
| 58 | struct dirent* ent; |
| 59 | |
| 60 | if ((dir = opendir(BPF_PROG_PATH)) != NULL) { |
| 61 | while ((ent = readdir(dir)) != NULL) { |
| 62 | string s = ent->d_name; |
| 63 | if (!EndsWith(s, ".o")) continue; |
| 64 | |
| 65 | string progPath = BPF_PROG_PATH + s; |
| 66 | |
| 67 | int ret = android::bpf::loadProg(progPath.c_str()); |
| 68 | ALOGI("Attempted load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 69 | } |
| 70 | closedir(dir); |
| 71 | } |
| 72 | } |
| 73 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 74 | int main() { |
Chenbo Feng | b20d28d | 2019-04-17 14:33:02 -0700 | [diff] [blame] | 75 | if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) { |
| 76 | // Load all ELF objects, create programs and maps, and pin them |
| 77 | loadAllElfObjects(); |
| 78 | } |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 79 | |
| 80 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
| 81 | ALOGE("Failed to set bpf.progs_loaded property\n"); |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | return 0; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 86 | } |