BpfLoader.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef LOG_TAG
  17. #define LOG_TAG "bpfloader"
  18. #endif
  19. #include <arpa/inet.h>
  20. #include <dirent.h>
  21. #include <elf.h>
  22. #include <error.h>
  23. #include <fcntl.h>
  24. #include <inttypes.h>
  25. #include <linux/bpf.h>
  26. #include <linux/unistd.h>
  27. #include <net/if.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/mman.h>
  34. #include <sys/socket.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <android-base/properties.h>
  38. #include <android-base/stringprintf.h>
  39. #include <android-base/strings.h>
  40. #include <android-base/unique_fd.h>
  41. #include <libbpf_android.h>
  42. #include <log/log.h>
  43. #include <netdutils/Misc.h>
  44. #include <netdutils/Slice.h>
  45. #include "bpf/BpfUtils.h"
  46. using android::base::EndsWith;
  47. using android::base::unique_fd;
  48. using std::string;
  49. #define BPF_PROG_PATH "/system/etc/bpf/"
  50. #define CLEANANDEXIT(ret, mapPatterns) \
  51. do { \
  52. for (size_t i = 0; i < mapPatterns.size(); i++) { \
  53. if (mapPatterns[i].fd > -1) { \
  54. close(mapPatterns[i].fd); \
  55. } \
  56. } \
  57. return ret; \
  58. } while (0)
  59. using android::bpf::BpfMapInfo;
  60. using android::bpf::BpfProgInfo;
  61. void loadAllElfObjects(void) {
  62. DIR* dir;
  63. struct dirent* ent;
  64. if ((dir = opendir(BPF_PROG_PATH)) != NULL) {
  65. while ((ent = readdir(dir)) != NULL) {
  66. string s = ent->d_name;
  67. if (!EndsWith(s, ".o")) continue;
  68. string progPath = BPF_PROG_PATH + s;
  69. int ret = android::bpf::loadProg(progPath.c_str());
  70. ALOGI("Attempted load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
  71. }
  72. closedir(dir);
  73. }
  74. }
  75. int main() {
  76. std::string value = android::base::GetProperty("bpf.progs_loaded", "");
  77. if (value == "1") {
  78. ALOGI("Property bpf.progs_loaded is set, progs already loaded.\n");
  79. return 0;
  80. }
  81. if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) {
  82. // Load all ELF objects, create programs and maps, and pin them
  83. loadAllElfObjects();
  84. }
  85. if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
  86. ALOGE("Failed to set bpf.progs_loaded property\n");
  87. return 1;
  88. }
  89. return 0;
  90. }