Skip to content

Commit

Permalink
New BPF map types
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Feb 16, 2022
1 parent ca27ce1 commit 4c073d1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/one/nio/os/bpf/Bpf.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ public class Bpf {

static native String mapGetInfo(int fd, int[] result /*type,id,key_size,value_size,max_entries,flags*/) throws IOException;

static native int mapCreate(int type, int keySize, int valueSize, int maxEntries, String name, int flags) throws IOException;
static native int mapCreate(int type, int keySize, int valueSize, int maxEntries, String name, int flags, int innerMapFd) throws IOException;

/* flags for lookup/update */
@Native static final int BPF_ANY = 0; // create new element or update existing
@Native static final int BPF_NOEXIST = 1; // create new element if it didn't exist
@Native static final int BPF_EXIST = 2; // update existing element
@Native static final int BPF_F_LOCK = 4; // spin_lock-ed map_lookup/map_update

/* flags for map creation */
public static final int BPF_F_MMAPABLE = (1 << 10);

static native boolean mapLookup(int fd, byte[] key, byte[] result, int flags) throws IOException;

static native boolean mapUpdate(int fd, byte[] key, byte[] value, int flags) throws IOException;
Expand Down
11 changes: 10 additions & 1 deletion src/one/nio/os/bpf/BpfMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ public static BpfMap getByFd(int fd) throws IOException {
}

public static BpfMap newMap(MapType type, int keySize, int valueSize, int maxEntries, String name, int flags) throws IOException {
int fd = Bpf.mapCreate(type.ordinal(), keySize, valueSize, maxEntries, name, flags);
int fd = Bpf.mapCreate(type.ordinal(), keySize, valueSize, maxEntries, name, flags, 0);
return getByFd(fd);
}

public static BpfMap newMapOfMaps(MapType type, int keySize, int maxEntries, String name, int flags, BpfMap innerMap) throws IOException {
int fd = Bpf.mapCreate(type.ordinal(), keySize, 4, maxEntries, name, flags, innerMap.fd());
return getByFd(fd);
}

Expand Down Expand Up @@ -156,6 +161,10 @@ public static byte[] bytes(long i) {
return ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putLong(i).array();
}

public static ByteBuffer bytes(byte[] value) {
return ByteBuffer.wrap(value).order(ByteOrder.nativeOrder());
}

public static IntBuffer ints(byte[] value) {
return ByteBuffer.wrap(value).order(ByteOrder.nativeOrder()).asIntBuffer();
}
Expand Down
2 changes: 1 addition & 1 deletion src/one/nio/os/bpf/Handle.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Handle(int fd) {
this.fd = fd;
}

protected int fd() {
public int fd() {
if (fd <= 0) {
throw new IllegalStateException(getClass().getSimpleName() + " is closed");
}
Expand Down
7 changes: 7 additions & 0 deletions src/one/nio/os/bpf/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public enum MapType {
PERCPU_CGROUP_STORAGE,
QUEUE,
STACK,
SK_STORAGE,
DEVMAP_HASH,
STRUCT_OPS,
RINGBUF,
INODE_STORAGE,
TASK_STORAGE,
BLOOM_FILTER
;

final boolean perCpu = name().contains("PERCPU_");
Expand Down
3 changes: 2 additions & 1 deletion src/one/nio/os/native/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,15 @@ Java_one_nio_os_bpf_Bpf_rawTracepointOpen(JNIEnv* env, jclass cls, jint prog_fd,

JNIEXPORT jint JNICALL
Java_one_nio_os_bpf_Bpf_mapCreate(JNIEnv* env, jclass cls, jint type, jint key_size, jint value_size,
jint max_entries, jstring name, jint flags) {
jint max_entries, jstring name, jint flags, jint inner_map_fd) {
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.map_type = type;
attr.key_size = key_size;
attr.value_size = value_size;
attr.max_entries = max_entries;
attr.map_flags = flags;
attr.inner_map_fd = inner_map_fd;

if (name != NULL) {
jsize name_len = (*env)->GetStringUTFLength(env, name);
Expand Down
41 changes: 41 additions & 0 deletions test/one/nio/os/bpf/BpfAttachTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 Odnoklassniki Ltd, Mail.Ru Group
*
* 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.
*/

package one.nio.os.bpf;

import one.nio.os.perf.*;

import java.io.IOException;

public class BpfAttachTest {
public static void main(String[] args) throws IOException {
BpfProg prog = BpfProg.load(args[0], ProgType.PERF_EVENT);

System.out.printf("Loaded %s %s id:%d%n", prog.type, prog.name, prog.id);

for (int mapId : prog.getMapIds()) {
try (BpfMap map2 = BpfMap.getById(mapId)) {
System.out.println("Map " + map2.name + " " + map2.type + " " + map2.id);
}
}

System.out.println("Creating perf counter");
PerfCounterGlobal perfCounter = Perf.openGlobal(PerfEvent.HW_CPU_CYCLES, Perf.ANY_PID, PerfOption.period(1_000_000_0L));

System.out.println("Attaching perf counter");
prog.attach(perfCounter);
}
}

0 comments on commit 4c073d1

Please sign in to comment.