blob: 966f3f63d08a3f2313a6ef803a290a976f81af38 [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
41#include <android-base/stringprintf.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070042#include <android-base/strings.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070043#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070044#include <libbpf_android.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070045#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070046#include <netdutils/Misc.h>
47#include <netdutils/Slice.h>
48#include "bpf/BpfUtils.h"
Chenbo Feng4c9e9ec2018-10-16 20:31:52 -070049#include "netdbpf/bpf_shared.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070050
Joel Fernandesd76a2002018-10-16 13:19:58 -070051using android::base::EndsWith;
Chenbo Feng75b410b2018-10-10 15:01:19 -070052using android::base::unique_fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -070053using std::string;
Chenbo Feng75b410b2018-10-10 15:01:19 -070054
Joel Fernandesd76a2002018-10-16 13:19:58 -070055#define BPF_PROG_PATH "/system/etc/bpf/"
Chenbo Feng75b410b2018-10-10 15:01:19 -070056
Yi Kong4a745692018-12-16 18:21:50 -080057#define CLEANANDEXIT(ret, mapPatterns) \
58 do { \
59 for (size_t i = 0; i < mapPatterns.size(); i++) { \
60 if (mapPatterns[i].fd > -1) { \
61 close(mapPatterns[i].fd); \
62 } \
63 } \
64 return ret; \
Chenbo Feng75b410b2018-10-10 15:01:19 -070065 } while (0)
66
67using android::bpf::BpfMapInfo;
68using android::bpf::BpfProgInfo;
69
Joel Fernandesd76a2002018-10-16 13:19:58 -070070void loadAllElfObjects(void) {
71 DIR* dir;
72 struct dirent* ent;
73
74 if ((dir = opendir(BPF_PROG_PATH)) != NULL) {
75 while ((ent = readdir(dir)) != NULL) {
76 string s = ent->d_name;
77 if (!EndsWith(s, ".o")) continue;
78
79 string progPath = BPF_PROG_PATH + s;
80
81 int ret = android::bpf::loadProg(progPath.c_str());
82 ALOGI("Attempted load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
83 }
84 closedir(dir);
85 }
86}
87
Chenbo Feng75b410b2018-10-10 15:01:19 -070088int main() {
Joel Fernandesd76a2002018-10-16 13:19:58 -070089 // Load all ELF objects, create programs and maps, and pin them
90 loadAllElfObjects();
Chenbo Feng75b410b2018-10-10 15:01:19 -070091}