From 49140b98e1beb917f239a9baab05ce852b08fa49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Wed, 7 Aug 2024 15:06:07 -0700 Subject: [PATCH] NetBpfLoad: combine into one file, part two MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This actually makes things build, and removes some duplication. Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: Ie5f95c7cc7f21525e649db0eebccb8e6933678b3 --- netbpfload/Android.bp | 5 +- netbpfload/NetBpfLoad.cpp | 206 +++++++++++--------------------------- 2 files changed, 62 insertions(+), 149 deletions(-) diff --git a/netbpfload/Android.bp b/netbpfload/Android.bp index 908bb13440..b8c0ce7d77 100644 --- a/netbpfload/Android.bp +++ b/netbpfload/Android.bp @@ -48,10 +48,7 @@ cc_binary { "libbase", "liblog", ], - srcs: [ - "loader.cpp", - "NetBpfLoad.cpp", - ], + srcs: ["NetBpfLoad.cpp"], apex_available: [ "com.android.tethering", "//apex_available:platform", diff --git a/netbpfload/NetBpfLoad.cpp b/netbpfload/NetBpfLoad.cpp index 4701896b47..3e84733c4f 100644 --- a/netbpfload/NetBpfLoad.cpp +++ b/netbpfload/NetBpfLoad.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2023 The Android Open Source Project + * Copyright (C) 2018-2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,62 @@ * limitations under the License. */ -#pragma once - -#include +#define LOG_TAG "NetBpfLoad" +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "BpfSyscallWrappers.h" +#include "bpf/BpfUtils.h" +#include "bpf/bpf_map_def.h" + +using android::base::EndsWith; +using android::base::StartsWith; +using android::base::unique_fd; +using std::ifstream; +using std::ios; +using std::optional; +using std::string; +using std::vector; namespace android { namespace bpf { @@ -69,81 +120,25 @@ struct Location { const char* const prefix = ""; }; -// BPF loader implementation. Loads an eBPF ELF object -int loadProg(const char* elfPath, bool* isCritical, const unsigned int bpfloader_ver, - const Location &location = {}); - -// Exposed for testing -unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal); - // Returns the build type string (from ro.build.type). -const std::string& getBuildType(); +const std::string& getBuildType() { + static std::string t = android::base::GetProperty("ro.build.type", "unknown"); + return t; +} // The following functions classify the 3 Android build types. inline bool isEng() { return getBuildType() == "eng"; } + inline bool isUser() { return getBuildType() == "user"; } + inline bool isUserdebug() { return getBuildType() == "userdebug"; } -} // namespace bpf -} // namespace android -/* - * Copyright (C) 2018-2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "NetBpfLoad" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "BpfSyscallWrappers.h" -#include "bpf/BpfUtils.h" -#include "bpf/bpf_map_def.h" -#include "loader.h" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - #define BPF_FS_PATH "/sys/fs/bpf/" // Size of the BPF log buffer for verifier logging @@ -152,22 +147,6 @@ inline bool isUserdebug() { // Unspecified attach type is 0 which is BPF_CGROUP_INET_INGRESS. #define BPF_ATTACH_TYPE_UNSPEC BPF_CGROUP_INET_INGRESS -using android::base::StartsWith; -using android::base::unique_fd; -using std::ifstream; -using std::ios; -using std::optional; -using std::string; -using std::vector; - -namespace android { -namespace bpf { - -const std::string& getBuildType() { - static std::string t = android::base::GetProperty("ro.build.type", "unknown"); - return t; -} - static unsigned int page_size = static_cast(getpagesize()); constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") { @@ -1284,69 +1263,6 @@ int loadProg(const char* const elfPath, bool* const isCritical, const unsigned i return ret; } -} // namespace bpf -} // namespace android -/* - * Copyright (C) 2017-2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LOG_TAG -#define LOG_TAG "NetBpfLoad" -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "BpfSyscallWrappers.h" -#include "bpf/BpfUtils.h" -#include "loader.h" - -namespace android { -namespace bpf { - -using base::StartsWith; -using base::EndsWith; -using std::string; -using std::vector; - static bool exists(const char* const path) { int v = access(path, F_OK); if (!v) return true;