Skip to content

Commit

Permalink
implement xdp binding in java
Browse files Browse the repository at this point in the history
  • Loading branch information
wkgcass committed Jul 28, 2024
1 parent 86c521a commit 1bc9e21
Show file tree
Hide file tree
Showing 38 changed files with 3,944 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ clean:
rm -f sample_kern.ll
rm -f sample_kern.o
rm -f libvpxdp.so
cd vpxdp-java && ./gradlew clean

.PHONY: xdptools
xdptools:
Expand All @@ -42,7 +43,7 @@ so: xdptools
rm -f libvpxdp.so
gcc -O2 $(INC_CMD) $(LD_CMD) \
-g -o libvpxdp.so -fPIC -shared \
vproxy_xdp.c vproxy_xdp_util.c vproxy_checksum.c \
vproxy_xdp.c vproxy_xdp_util.c vproxy_checksum.c expose_inline.c \
-lxdp -lelf

.PHONY: all
Expand All @@ -60,6 +61,10 @@ prepare:
run: sample_kern sample_user
LD_LIBRARY_PATH=$(LD_PATH) ./sample_user $(filter-out $@,$(MAKECMDGOALS))

.PHONY: run-java
run-java: so sample_kern
cd vpxdp-java && ./gradlew runSample --args=$(filter-out $@,$(MAKECMDGOALS))

.PHONY: docker-run
docker-run:
docker run --name=vpxdp-sample --rm \
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ Testing from another terminal:
```shell
ping 'fd00::1'
```

To run the java version sample:

```shell
make run-java veth0
```
21 changes: 21 additions & 0 deletions expose_inline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "vproxy_xdp.h"

void _vp_dummy(void) {}
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma redefine_extname _vp_dummy vp_dummy
#pragma GCC diagnostic error "-Wpragmas"

struct vp_chunk_info* _vp_chunk_seek(struct vp_chunk_array* chunks, uint64_t addroff) {
return vp_chunk_seek(chunks, addroff);
}
#pragma redefine_extname _vp_chunk_seek vp_chunk_seek

struct vp_chunk_info* _vp_chunk_fetch(struct vp_chunk_array* chunks) {
return vp_chunk_fetch(chunks);
}
#pragma redefine_extname _vp_chunk_fetch vp_chunk_fetch

void _vp_chunk_release(struct vp_chunk_array* chunks, struct vp_chunk_info* chunk) {
vp_chunk_release(chunks, chunk);
}
#pragma redefine_extname _vp_chunk_release vp_chunk_release
43 changes: 43 additions & 0 deletions vpxdp-java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

# pni
core/src/main/c-generated/*
!core/src/main/c-generated/.gitkeep
1 change: 1 addition & 0 deletions vpxdp-java/.java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
97 changes: 97 additions & 0 deletions vpxdp-java/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import io.vproxy.pni.exec.CompilationFlag

buildscript {
repositories {
mavenLocal()
mavenCentral()
}
def PNI_VERSION = '22.0.0.21'
ext.set("PNI_VERSION", PNI_VERSION)
dependencies {
classpath group: 'io.vproxy', name: 'pni-exec-jdk22', version: PNI_VERSION
}
}

plugins {
id 'java'
}

group = 'io.vproxy'
version = '1.0-SNAPSHOT'

subprojects {
apply plugin: 'java-library'

java {
sourceCompatibility = '22'
targetCompatibility = '22'
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.incremental = false
}
tasks.withType(JavaExec) {
jvmArgs += '--enable-native-access=ALL-UNNAMED'
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
api 'io.vproxy:pni-api-jdk22:' + PNI_VERSION
}
}

project(":pni-template") {
compileJava {
options.compilerArgs += '-parameters'
}

task pniGenerate() {
dependsOn compileJava

def workingDir = project.rootProject.rootDir.getAbsolutePath()
def opts = new io.vproxy.pni.exec.CompilerOptions()
.setClasspath(List.of(workingDir + '/pni-template/build/classes/java/main'))
.setJavaOutputBaseDirectory(workingDir + '/core/src/main/generated')
.setCOutputDirectory(workingDir + '/core/src/main/c-generated')
.setCompilationFlag(CompilationFlag.TYPE_NAME_PREFIX, "PNI")
.setCompilationFlag(CompilationFlag.ALWAYS_ALIGNED)

def gen = new io.vproxy.pni.exec.Generator(opts)

doLast {
gen.generate()
}
}
}

project(":core") {
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/generated']
}
}
}
}

project(":test") {
dependencies {
implementation project(":core")
implementation "io.vproxy:commons:1.4.0"
}

task runSample(type: JavaExec) {
classpath = sourceSets.test.runtimeClasspath
workingDir = project.rootProject.rootDir.getParentFile().getAbsolutePath()
systemProperty("java.library.path",
"./" + File.pathSeparator + "./submodules/xdp-tools/lib/libxdp/" +
File.pathSeparator + "/usr/lib/" + "uname -m".execute().text.trim() + "-linux-gnu")
main = "io.vproxy.vpxdp.SampleUser"
}
}
Empty file added vpxdp-java/core/build.gradle
Empty file.
Empty file.
148 changes: 148 additions & 0 deletions vpxdp-java/core/src/main/generated/io/vproxy/vpxdp/BPFMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package io.vproxy.vpxdp;

import io.vproxy.pni.*;
import io.vproxy.pni.hack.*;
import io.vproxy.pni.array.*;
import java.lang.foreign.*;
import java.lang.invoke.*;
import java.nio.ByteBuffer;

public class BPFMap extends AbstractNativeObject implements NativeObject {
public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(

);
public final MemorySegment MEMORY;

@Override
public MemorySegment MEMORY() {
return MEMORY;
}

public BPFMap(MemorySegment MEMORY) {
MEMORY = MEMORY.reinterpret(LAYOUT.byteSize());
this.MEMORY = MEMORY;
long OFFSET = 0;
}

public BPFMap(Allocator ALLOCATOR) {
this(ALLOCATOR.allocate(LAYOUT));
}

private static final MethodHandle addXskMH = PanamaUtils.lookupPNICriticalFunction(new PNILinkOptions().setCritical(true), int.class, "vp_xsk_add_into_map", MemorySegment.class /* self */, int.class /* key */, io.vproxy.vpxdp.XskInfo.LAYOUT.getClass() /* xsk */);

public int addXsk(int key, io.vproxy.vpxdp.XskInfo xsk) {
int RESULT;
try {
RESULT = (int) addXskMH.invokeExact(MEMORY, key, (MemorySegment) (xsk == null ? MemorySegment.NULL : xsk.MEMORY));
} catch (Throwable THROWABLE) {
throw PanamaUtils.convertInvokeExactException(THROWABLE);
}
return RESULT;
}

private static final MethodHandle addMacMH = PanamaUtils.lookupPNICriticalFunction(new PNILinkOptions().setCritical(true), int.class, "vp_mac_add_into_map", MemorySegment.class /* self */, MemorySegment.class /* mac */, String.class /* ifname */);

public int addMac(MemorySegment mac, PNIString ifname) {
int RESULT;
try {
RESULT = (int) addMacMH.invokeExact(MEMORY, (MemorySegment) (mac == null ? MemorySegment.NULL : mac), (MemorySegment) (ifname == null ? MemorySegment.NULL : ifname.MEMORY));
} catch (Throwable THROWABLE) {
throw PanamaUtils.convertInvokeExactException(THROWABLE);
}
return RESULT;
}

private static final MethodHandle removeMacMH = PanamaUtils.lookupPNICriticalFunction(new PNILinkOptions().setCritical(true), int.class, "vp_mac_remove_from_map", MemorySegment.class /* self */, MemorySegment.class /* mac */);

public int removeMac(MemorySegment mac) {
int RESULT;
try {
RESULT = (int) removeMacMH.invokeExact(MEMORY, (MemorySegment) (mac == null ? MemorySegment.NULL : mac));
} catch (Throwable THROWABLE) {
throw PanamaUtils.convertInvokeExactException(THROWABLE);
}
return RESULT;
}

@Override
public void toString(StringBuilder SB, int INDENT, java.util.Set<NativeObjectTuple> VISITED, boolean CORRUPTED_MEMORY) {
if (!VISITED.add(new NativeObjectTuple(this))) {
SB.append("<...>@").append(Long.toString(MEMORY.address(), 16));
return;
}
SB.append("BPFMap{\n");
SB.append(" ".repeat(INDENT)).append("}@").append(Long.toString(MEMORY.address(), 16));
}

public static class Array extends RefArray<BPFMap> {
public Array(MemorySegment buf) {
super(buf, BPFMap.LAYOUT);
}

public Array(Allocator allocator, long len) {
super(allocator, BPFMap.LAYOUT, len);
}

public Array(PNIBuf buf) {
super(buf, BPFMap.LAYOUT);
}

@Override
protected void elementToString(io.vproxy.vpxdp.BPFMap ELEM, StringBuilder SB, int INDENT, java.util.Set<NativeObjectTuple> VISITED, boolean CORRUPTED_MEMORY) {
ELEM.toString(SB, INDENT, VISITED, CORRUPTED_MEMORY);
}

@Override
protected String toStringTypeName() {
return "BPFMap.Array";
}

@Override
protected BPFMap construct(MemorySegment seg) {
return new BPFMap(seg);
}

@Override
protected MemorySegment getSegment(BPFMap value) {
return value.MEMORY;
}
}

public static class Func extends PNIFunc<BPFMap> {
private Func(io.vproxy.pni.CallSite<BPFMap> func) {
super(func);
}

private Func(io.vproxy.pni.CallSite<BPFMap> func, Options opts) {
super(func, opts);
}

private Func(MemorySegment MEMORY) {
super(MEMORY);
}

public static Func of(io.vproxy.pni.CallSite<BPFMap> func) {
return new Func(func);
}

public static Func of(io.vproxy.pni.CallSite<BPFMap> func, Options opts) {
return new Func(func, opts);
}

public static Func of(MemorySegment MEMORY) {
return new Func(MEMORY);
}

@Override
protected String toStringTypeName() {
return "BPFMap.Func";
}

@Override
protected BPFMap construct(MemorySegment seg) {
return new BPFMap(seg);
}
}
}
// metadata.generator-version: pni 21.0.0.20
// sha256:19a844af4853c26c65bfb7e29fe5590c1e0ba5383438b965842fadb603a045bd
Loading

0 comments on commit 1bc9e21

Please sign in to comment.