blob: 3f86a569f20971a3793897573eefbadbe554a3d5 [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
Steven Morelanda48639e2022-02-07 23:15:48 +000041#include <android-base/logging.h>
Joel Fernandesd3ec8712019-01-11 06:22:05 -050042#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070043#include <android-base/stringprintf.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070044#include <android-base/strings.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070045#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070046#include <libbpf_android.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070047#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070048#include <netdutils/Misc.h>
49#include <netdutils/Slice.h>
50#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070051
Joel Fernandesd76a2002018-10-16 13:19:58 -070052using android::base::EndsWith;
Joel Fernandesd76a2002018-10-16 13:19:58 -070053using std::string;
Chenbo Feng75b410b2018-10-10 15:01:19 -070054
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080055struct {
56 const char* const dir;
57 const char* const prefix;
58} locations[] = {
Ken Chen6d697842022-01-17 17:22:34 +080059 // Tethering mainline module: tether offload
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080060 {
61 .dir = "/apex/com.android.tethering/etc/bpf/",
62 .prefix = "tethering/",
63 },
Ken Chen6d697842022-01-17 17:22:34 +080064 // Tethering mainline module: netd, clatd, ...etc
65 {
66 .dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
67 .prefix = "",
68 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080069 // Core operating system
70 {
71 .dir = "/system/etc/bpf/",
72 .prefix = "",
73 },
74};
Chenbo Feng75b410b2018-10-10 15:01:19 -070075
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080076int loadAllElfObjects(const char* const progDir, const char* const prefix) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070077 int retVal = 0;
Joel Fernandesd76a2002018-10-16 13:19:58 -070078 DIR* dir;
79 struct dirent* ent;
80
Hungming Chen4b8e9822020-09-10 15:51:59 +080081 if ((dir = opendir(progDir)) != NULL) {
Joel Fernandesd76a2002018-10-16 13:19:58 -070082 while ((ent = readdir(dir)) != NULL) {
83 string s = ent->d_name;
84 if (!EndsWith(s, ".o")) continue;
85
Hungming Chen4b8e9822020-09-10 15:51:59 +080086 string progPath(progDir);
87 progPath += s;
Joel Fernandesd76a2002018-10-16 13:19:58 -070088
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070089 bool critical;
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080090 int ret = android::bpf::loadProg(progPath.c_str(), &critical, prefix);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070091 if (ret) {
92 if (critical) retVal = ret;
93 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
94 } else {
95 ALOGI("Loaded object: %s", progPath.c_str());
96 }
Joel Fernandesd76a2002018-10-16 13:19:58 -070097 }
98 closedir(dir);
99 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700100 return retVal;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700101}
102
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800103void createSysFsBpfSubDir(const char* const prefix) {
104 if (*prefix) {
105 mode_t prevUmask = umask(0);
106
107 string s = "/sys/fs/bpf/";
108 s += prefix;
109
110 errno = 0;
111 int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
112 if (ret && errno != EEXIST) {
113 ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
114 }
115
116 umask(prevUmask);
117 }
118}
119
Steven Morelanda48639e2022-02-07 23:15:48 +0000120int main(int argc, char** argv) {
121 (void)argc;
122 android::base::InitLogging(argv, &android::base::KernelLogger);
123
Maciej Żenczykowski567dc562020-06-10 15:56:07 -0700124 // Load all ELF objects, create programs and maps, and pin them
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800125 for (const auto location : locations) {
126 createSysFsBpfSubDir(location.prefix);
127 if (loadAllElfObjects(location.dir, location.prefix) != 0) {
128 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800129 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
130 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
131 "problems or startup script race.");
132 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
133 sleep(20);
134 return 2;
135 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700136 }
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500137
138 if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700139 ALOGE("Failed to set bpf.progs_loaded property");
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500140 return 1;
141 }
142
143 return 0;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700144}