Skip to content
Snippets Groups Projects
Commit ed353e90 authored by Ken Chen's avatar Ken Chen Committed by Gerrit Code Review
Browse files

Merge "Return error Status from BpfHandler::initPrograms()" into main

parents 9a966eab d6ea75ac
No related branches found
No related tags found
No related merge requests found
......@@ -53,14 +53,10 @@ static Status attachProgramToCgroup(const char* programPath, const unique_fd& cg
bpf_attach_type type) {
unique_fd cgroupProg(retrieveProgram(programPath));
if (!cgroupProg.ok()) {
const int err = errno;
ALOGE("Failed to get program from %s: %s", programPath, strerror(err));
return statusFromErrno(err, "cgroup program get failed");
return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath));
}
if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) {
const int err = errno;
ALOGE("Program from %s attach failed: %s", programPath, strerror(err));
return statusFromErrno(err, "program attach failed");
return statusFromErrno(errno, fmt::format("Program {} attach failed", programPath));
}
return netdutils::status::ok;
}
......@@ -68,50 +64,38 @@ static Status attachProgramToCgroup(const char* programPath, const unique_fd& cg
static Status checkProgramAccessible(const char* programPath) {
unique_fd prog(retrieveProgram(programPath));
if (!prog.ok()) {
const int err = errno;
ALOGE("Failed to get program from %s: %s", programPath, strerror(err));
return statusFromErrno(err, "program retrieve failed");
return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath));
}
return netdutils::status::ok;
}
static Status initPrograms(const char* cg2_path) {
if (!cg2_path) {
ALOGE("cg2_path is NULL");
return statusFromErrno(EINVAL, "cg2_path is NULL");
}
if (!cg2_path) return Status("cg2_path is NULL");
// This code was mainlined in T, so this should be trivially satisfied.
if (!modules::sdklevel::IsAtLeastT()) {
ALOGE("S- platform is unsupported");
abort();
}
if (!modules::sdklevel::IsAtLeastT()) return Status("S- platform is unsupported");
// S requires eBPF support which was only added in 4.9, so this should be satisfied.
if (!bpf::isAtLeastKernelVersion(4, 9, 0)) {
ALOGE("kernel version < 4.9.0 is unsupported");
abort();
return Status("kernel version < 4.9.0 is unsupported");
}
// U bumps the kernel requirement up to 4.14
if (modules::sdklevel::IsAtLeastU() && !bpf::isAtLeastKernelVersion(4, 14, 0)) {
ALOGE("U+ platform with kernel version < 4.14.0 is unsupported");
abort();
return Status("U+ platform with kernel version < 4.14.0 is unsupported");
}
if (modules::sdklevel::IsAtLeastV()) {
// V bumps the kernel requirement up to 4.19
// see also: //system/netd/tests/kernel_test.cpp TestKernel419
if (!bpf::isAtLeastKernelVersion(4, 19, 0)) {
ALOGE("V+ platform with kernel version < 4.19.0 is unsupported");
abort();
return Status("V+ platform with kernel version < 4.19.0 is unsupported");
}
// Technically already required by U, but only enforce on V+
// see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit
if (bpf::isKernel32Bit() && bpf::isAtLeastKernelVersion(5, 16, 0)) {
ALOGE("V+ platform with 32 bit kernel, version >= 5.16.0 is unsupported");
abort();
return Status("V+ platform with 32 bit kernel, version >= 5.16.0 is unsupported");
}
}
......@@ -123,14 +107,12 @@ static Status initPrograms(const char* cg2_path) {
// problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces.
// see also: //system/bpf/bpfloader/BpfLoader.cpp main()
if (bpf::isUserspace32bit() && bpf::isAtLeastKernelVersion(6, 2, 0)) {
ALOGE("32 bit userspace with Kernel version >= 6.2.0 is unsupported");
abort();
return Status("32 bit userspace with Kernel version >= 6.2.0 is unsupported");
}
// U mandates this mount point (though it should also be the case on T)
if (modules::sdklevel::IsAtLeastU() && !!strcmp(cg2_path, "/sys/fs/cgroup")) {
ALOGE("U+ platform with cg2_path != /sys/fs/cgroup is unsupported");
abort();
return Status("U+ platform with cg2_path != /sys/fs/cgroup is unsupported");
}
unique_fd cg_fd(open(cg2_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
......
......@@ -31,7 +31,7 @@ int libnetd_updatable_init(const char* cg2_path) {
android::netdutils::Status ret = sBpfHandler.init(cg2_path);
if (!android::netdutils::isOk(ret)) {
LOG(ERROR) << __func__ << ": BPF handler init failed";
LOG(ERROR) << __func__ << ": Failed. " << ret.code() << " " << ret.msg();
return -ret.code();
}
return 0;
......
......@@ -41,6 +41,9 @@ class [[nodiscard]] Status {
// Constructs an error Status, |code| must be non-zero.
Status(int code, std::string msg) : mCode(code), mMsg(std::move(msg)) { assert(!ok()); }
// Constructs an error Status with message. Error |code| is unspecified.
explicit Status(std::string msg) : Status(std::numeric_limits<int>::max(), std::move(msg)) {}
Status(android::base::Result<void> result)
: mCode(result.ok() ? 0 : static_cast<int>(result.error().code())),
mMsg(result.ok() ? "" : result.error().message()) {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment