Merge "remove dead portions of BpfUtils.java" into main

This commit is contained in:
Maciej Żenczykowski
2023-10-25 22:53:49 +00:00
committed by Gerrit Code Review
4 changed files with 11 additions and 128 deletions

View File

@@ -1538,9 +1538,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** /**
* Get BPF program Id from CGROUP. See {@link BpfUtils#getProgramId}. * Get BPF program Id from CGROUP. See {@link BpfUtils#getProgramId}.
*/ */
public int getBpfProgramId(final int attachType, @NonNull final String cgroupPath) public int getBpfProgramId(final int attachType)
throws IOException { throws IOException {
return BpfUtils.getProgramId(attachType, cgroupPath); return BpfUtils.getProgramId(attachType);
} }
/** /**
@@ -3274,15 +3274,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
pw.increaseIndent(); pw.increaseIndent();
try { try {
pw.print("CGROUP_INET_INGRESS: "); pw.print("CGROUP_INET_INGRESS: ");
pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_INGRESS, BpfUtils.CGROUP_PATH)); pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_INGRESS));
pw.print("CGROUP_INET_EGRESS: "); pw.print("CGROUP_INET_EGRESS: ");
pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_EGRESS, BpfUtils.CGROUP_PATH)); pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_EGRESS));
pw.print("CGROUP_INET_SOCK_CREATE: "); pw.print("CGROUP_INET_SOCK_CREATE: ");
pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_SOCK_CREATE, BpfUtils.CGROUP_PATH)); pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET_SOCK_CREATE));
pw.print("CGROUP_INET4_BIND: "); pw.print("CGROUP_INET4_BIND: ");
pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET4_BIND, BpfUtils.CGROUP_PATH)); pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET4_BIND));
pw.print("CGROUP_INET6_BIND: "); pw.print("CGROUP_INET6_BIND: ");
pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET6_BIND, BpfUtils.CGROUP_PATH)); pw.println(mDeps.getBpfProgramId(BPF_CGROUP_INET6_BIND));
} catch (IOException e) { } catch (IOException e) {
pw.println(" IOException"); pw.println(" IOException");
} }
@@ -10816,7 +10816,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// If type can't be parsed, this throws NumberFormatException, which // If type can't be parsed, this throws NumberFormatException, which
// is passed back to adb who prints it. // is passed back to adb who prints it.
final int type = Integer.parseInt(getNextArg()); final int type = Integer.parseInt(getNextArg());
final int ret = BpfUtils.getProgramId(type, BpfUtils.CGROUP_PATH); final int ret = BpfUtils.getProgramId(type);
pw.println(ret); pw.println(ret);
return 0; return 0;
} }

View File

@@ -40,50 +40,19 @@ public class BpfUtils {
// on T+ devices as well, but this is not guaranteed. // on T+ devices as well, but this is not guaranteed.
public static final String CGROUP_PATH = "/sys/fs/cgroup/"; public static final String CGROUP_PATH = "/sys/fs/cgroup/";
/**
* Attach BPF program to CGROUP
*/
public static void attachProgram(int type, @NonNull String programPath,
@NonNull String cgroupPath, int flags) throws IOException {
native_attachProgramToCgroup(type, programPath, cgroupPath, flags);
}
/**
* Detach BPF program from CGROUP
*/
public static void detachProgram(int type, @NonNull String cgroupPath)
throws IOException {
native_detachProgramFromCgroup(type, cgroupPath);
}
/** /**
* Get BPF program Id from CGROUP. * Get BPF program Id from CGROUP.
* *
* Note: This requires a 4.19 kernel which is only guaranteed on V+. * Note: This requires a 4.19 kernel which is only guaranteed on V+.
* *
* @param attachType Bpf attach type. See bpf_attach_type in include/uapi/linux/bpf.h. * @param attachType Bpf attach type. See bpf_attach_type in include/uapi/linux/bpf.h.
* @param cgroupPath Path of cgroup.
* @return Positive integer for a Program Id. 0 if no program is attached. * @return Positive integer for a Program Id. 0 if no program is attached.
* @throws IOException if failed to open the cgroup directory or query bpf program. * @throws IOException if failed to open the cgroup directory or query bpf program.
*/ */
public static int getProgramId(int attachType, @NonNull String cgroupPath) throws IOException { public static int getProgramId(int attachType) throws IOException {
return native_getProgramIdFromCgroup(attachType, cgroupPath); return native_getProgramIdFromCgroup(attachType, CGROUP_PATH);
} }
/**
* Detach single BPF program from CGROUP
*/
public static void detachSingleProgram(int type, @NonNull String programPath,
@NonNull String cgroupPath) throws IOException {
native_detachSingleProgramFromCgroup(type, programPath, cgroupPath);
}
private static native boolean native_attachProgramToCgroup(int type, String programPath,
String cgroupPath, int flags) throws IOException;
private static native boolean native_detachProgramFromCgroup(int type, String cgroupPath)
throws IOException;
private static native boolean native_detachSingleProgramFromCgroup(int type,
String programPath, String cgroupPath) throws IOException;
private static native int native_getProgramIdFromCgroup(int type, String cgroupPath) private static native int native_getProgramIdFromCgroup(int type, String cgroupPath)
throws IOException; throws IOException;
} }

View File

@@ -30,86 +30,6 @@ namespace android {
using base::unique_fd; using base::unique_fd;
// If attach fails throw error and return false.
static jboolean com_android_net_module_util_BpfUtil_attachProgramToCgroup(JNIEnv *env,
jclass clazz, jint type, jstring bpfProgPath, jstring cgroupPath, jint flags) {
ScopedUtfChars dirPath(env, cgroupPath);
unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (cg_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to open the cgroup directory %s: %s",
dirPath.c_str(), strerror(errno));
return false;
}
ScopedUtfChars bpfProg(env, bpfProgPath);
unique_fd bpf_fd(bpf::retrieveProgram(bpfProg.c_str()));
if (bpf_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to retrieve bpf program from %s: %s",
bpfProg.c_str(), strerror(errno));
return false;
}
if (bpf::attachProgram((bpf_attach_type) type, bpf_fd, cg_fd, flags)) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to attach bpf program %s to %s: %s",
bpfProg.c_str(), dirPath.c_str(), strerror(errno));
return false;
}
return true;
}
// If detach fails throw error and return false.
static jboolean com_android_net_module_util_BpfUtil_detachProgramFromCgroup(JNIEnv *env,
jclass clazz, jint type, jstring cgroupPath) {
ScopedUtfChars dirPath(env, cgroupPath);
unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (cg_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to open the cgroup directory %s: %s",
dirPath.c_str(), strerror(errno));
return false;
}
if (bpf::detachProgram((bpf_attach_type) type, cg_fd)) {
jniThrowExceptionFmt(env, "Failed to detach bpf program from %s: %s",
dirPath.c_str(), strerror(errno));
return false;
}
return true;
}
// If detach single program fails throw error and return false.
static jboolean com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup(JNIEnv *env,
jclass clazz, jint type, jstring bpfProgPath, jstring cgroupPath) {
ScopedUtfChars dirPath(env, cgroupPath);
unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (cg_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to open the cgroup directory %s: %s",
dirPath.c_str(), strerror(errno));
return false;
}
ScopedUtfChars bpfProg(env, bpfProgPath);
unique_fd bpf_fd(bpf::retrieveProgram(bpfProg.c_str()));
if (bpf_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to retrieve bpf program from %s: %s",
bpfProg.c_str(), strerror(errno));
return false;
}
if (bpf::detachSingleProgram((bpf_attach_type) type, bpf_fd, cg_fd)) {
jniThrowExceptionFmt(env, "Failed to detach bpf program %s from %s: %s",
bpfProg.c_str(), dirPath.c_str(), strerror(errno));
return false;
}
return true;
}
static jint com_android_net_module_util_BpfUtil_getProgramIdFromCgroup(JNIEnv *env, static jint com_android_net_module_util_BpfUtil_getProgramIdFromCgroup(JNIEnv *env,
jclass clazz, jint type, jstring cgroupPath) { jclass clazz, jint type, jstring cgroupPath) {
@@ -138,12 +58,6 @@ static jint com_android_net_module_util_BpfUtil_getProgramIdFromCgroup(JNIEnv *e
*/ */
static const JNINativeMethod gMethods[] = { static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */ /* name, signature, funcPtr */
{ "native_attachProgramToCgroup", "(ILjava/lang/String;Ljava/lang/String;I)Z",
(void*) com_android_net_module_util_BpfUtil_attachProgramToCgroup },
{ "native_detachProgramFromCgroup", "(ILjava/lang/String;)Z",
(void*) com_android_net_module_util_BpfUtil_detachProgramFromCgroup },
{ "native_detachSingleProgramFromCgroup", "(ILjava/lang/String;Ljava/lang/String;)Z",
(void*) com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup },
{ "native_getProgramIdFromCgroup", "(ILjava/lang/String;)I", { "native_getProgramIdFromCgroup", "(ILjava/lang/String;)I",
(void*) com_android_net_module_util_BpfUtil_getProgramIdFromCgroup }, (void*) com_android_net_module_util_BpfUtil_getProgramIdFromCgroup },
}; };

View File

@@ -2299,7 +2299,7 @@ public class ConnectivityServiceTest {
} }
@Override @Override
public int getBpfProgramId(final int attachType, @NonNull final String cgroupPath) { public int getBpfProgramId(final int attachType) {
return 0; return 0;
} }