blob: 9bc64510c92b9203ae4b2a1456937307b4a8e6f8 [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
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 Fernandesd76a2002018-10-16 13:19:58 -070022#include <dirent.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070023#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 Fernandesd3ec8712019-01-11 06:22:05 -050041#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070042#include <android-base/stringprintf.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070043#include <android-base/strings.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070044#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070045#include <libbpf_android.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070046#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070047#include <netdutils/Misc.h>
48#include <netdutils/Slice.h>
49#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070050
Joel Fernandesd76a2002018-10-16 13:19:58 -070051using android::base::EndsWith;
Joel Fernandesd76a2002018-10-16 13:19:58 -070052using std::string;
Chenbo Feng75b410b2018-10-10 15:01:19 -070053
Hungming Chen4b8e9822020-09-10 15:51:59 +080054#define BPF_PROG_PATH_MAINLINE_TETHERING "/apex/com.android.tethering/etc/bpf/"
55#define BPF_PROG_PATH_SYSTEM "/system/etc/bpf/"
Chenbo Feng75b410b2018-10-10 15:01:19 -070056
Hungming Chen4b8e9822020-09-10 15:51:59 +080057int loadAllElfObjects(const char* progDir) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070058 int retVal = 0;
Joel Fernandesd76a2002018-10-16 13:19:58 -070059 DIR* dir;
60 struct dirent* ent;
61
Hungming Chen4b8e9822020-09-10 15:51:59 +080062 if ((dir = opendir(progDir)) != NULL) {
Joel Fernandesd76a2002018-10-16 13:19:58 -070063 while ((ent = readdir(dir)) != NULL) {
64 string s = ent->d_name;
65 if (!EndsWith(s, ".o")) continue;
66
Hungming Chen4b8e9822020-09-10 15:51:59 +080067 string progPath(progDir);
68 progPath += s;
Joel Fernandesd76a2002018-10-16 13:19:58 -070069
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070070 bool critical;
71 int ret = android::bpf::loadProg(progPath.c_str(), &critical);
72 if (ret) {
73 if (critical) retVal = ret;
74 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
75 } else {
76 ALOGI("Loaded object: %s", progPath.c_str());
77 }
Joel Fernandesd76a2002018-10-16 13:19:58 -070078 }
79 closedir(dir);
80 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070081 return retVal;
Joel Fernandesd76a2002018-10-16 13:19:58 -070082}
83
Chenbo Feng75b410b2018-10-10 15:01:19 -070084int main() {
Maciej Żenczykowski567dc562020-06-10 15:56:07 -070085 // Load all ELF objects, create programs and maps, and pin them
Hungming Chen4b8e9822020-09-10 15:51:59 +080086 for (const auto dir : {BPF_PROG_PATH_MAINLINE_TETHERING, BPF_PROG_PATH_SYSTEM}) {
87 if (loadAllElfObjects(dir) != 0) {
88 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", dir);
89 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
90 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
91 "problems or startup script race.");
92 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
93 sleep(20);
94 return 2;
95 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070096 }
Joel Fernandesd3ec8712019-01-11 06:22:05 -050097
98 if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070099 ALOGE("Failed to set bpf.progs_loaded property");
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500100 return 1;
101 }
102
103 return 0;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700104}