make java maps grab locks

The java bpfmap fd cache has some implications:
- this won't protect against two SingleWriterBpfMap instantiations
  for the same map (path) in the same process
- we don't release any locks on BpfMap destruction.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibb0b1f57974c307ea5e0fa699ce8d598c59f456e
This commit is contained in:
Maciej Żenczykowski 2024-06-14 15:15:23 -07:00
commit 9eca02a8fa
3 changed files with 22 additions and 2 deletions

View file

@ -52,6 +52,9 @@ public class BpfMap<K extends Struct, V extends Struct> implements IBpfMap<K, V>
public static final int BPF_F_RDONLY = 1 << 3;
public static final int BPF_F_WRONLY = 1 << 4;
// magic value for jni consumption, invalid from kernel point of view
public static final int BPF_F_RDWR_EXCLUSIVE = BPF_F_RDONLY | BPF_F_WRONLY;
public static final int BPF_MAP_TYPE_HASH = 1;
private static final int BPF_F_NO_PREALLOC = 1;

View file

@ -63,7 +63,7 @@ public class SingleWriterBpfMap<K extends Struct, V extends Struct> extends BpfM
public SingleWriterBpfMap(@NonNull final String path, final Class<K> key,
final Class<V> value) throws ErrnoException, NullPointerException {
super(path, BPF_F_RDWR, key, value);
super(path, BPF_F_RDWR_EXCLUSIVE, key, value);
// Populate cache with the current map contents.
K currentKey = super.getFirstKey();

View file

@ -35,7 +35,24 @@ static jint com_android_net_module_util_BpfMap_nativeBpfFdGet(JNIEnv *env, jclas
jstring path, jint mode, jint keySize, jint valueSize) {
ScopedUtfChars pathname(env, path);
jint fd = bpf::bpfFdGet(pathname.c_str(), static_cast<unsigned>(mode));
jint fd = -1;
switch (mode) {
case 0:
fd = bpf::mapRetrieveRW(pathname.c_str());
break;
case BPF_F_RDONLY:
fd = bpf::mapRetrieveRO(pathname.c_str());
break;
case BPF_F_WRONLY:
fd = bpf::mapRetrieveWO(pathname.c_str());
break;
case BPF_F_RDONLY|BPF_F_WRONLY:
fd = bpf::mapRetrieveExclusiveRW(pathname.c_str());
break;
default:
errno = EINVAL;
break;
}
if (fd < 0) {
jniThrowErrnoException(env, "nativeBpfFdGet", errno);