Skip to content

Commit

Permalink
[iocp] implement iocp and support windows msquic
Browse files Browse the repository at this point in the history
  • Loading branch information
wkgcass committed Jul 20, 2024
1 parent 82b62da commit 5d50855
Show file tree
Hide file tree
Showing 119 changed files with 8,553 additions and 899 deletions.
32 changes: 27 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ else ifeq ($(LINUX_ARCH),amd64)
LINUX_ARCH := x86_64
endif

IS_WIN = 0
ifneq (,$(findstring MINGW,$(OS)))
IS_WIN = 1
else ifneq (,$(findstring Windows,$(OS)))
IS_WIN = 1
endif

DOCKER_PLUGIN_WORKDIR ?= "."

.PHONY: clean-jar
Expand All @@ -44,7 +51,7 @@ clean: clean-jar
rm -f ./*.build_artifacts.txt
rm -f ./module-info.class
rm -rf ./io
rm -rf ./submodules/msquic/build
cd submodules/msquic && make clean
rm -f ./*.so
rm -f ./*.dylib
rm -f ./*.dll
Expand Down Expand Up @@ -96,22 +103,25 @@ _add_linux_so_to_zip:
./io/vproxy/libpni-$(LINUX_ARCH).so
rm -r ./io

.PHONY: native-no-docker
native-no-docker: libpni vfdposix quic fubuki
cp ./submodules/msquic/build/bin/Release/libmsquic.2.2.4.dylib ./libmsquic.dylib
.PHONY: native
ifeq ($(OS),Linux)
native: libpni vfdposix vpxdp quic-all fubuki
else ifeq ($(OS),Darwin)
native: libpni-linux libpni vfdposix-linux vfdposix vpxdp-linux quic-all fubuki-linux fubuki
else
native: libpni vfdwindows
native: libpni vfdwindows quic fubuki
endif

.PHONY: jar-with-lib
ifeq ($(OS),Linux)
jar-with-lib: clean jar native _add_linux_so_to_zip
else
jar-with-lib: clean jar native _add_linux_so_to_zip jar-with-lib-no-docker
jar-with-lib: clean jar native _add_linux_so_to_zip jar-with-lib-skip-native
.PHONY: jar-with-lib-no-docker
jar-with-lib-no-docker: clean jar native jar-with-lib-skip-native
jar-with-lib-no-docker: clean jar native-no-docker jar-with-lib-skip-native
.PHONY: jar-with-lib-skip-native
jar-with-lib-skip-native: clean-jar jar
mkdir -p ./io/vproxy/
Expand Down Expand Up @@ -168,11 +178,19 @@ xdp-sample-kern:
cd ./base/src/main/c/xdp && make kern

.PHONY: msquic-java
ifeq (0,$(IS_WIN))
msquic-java: libpni
cd ./base/src/main/c && \
MSQUIC_LD=../../../../submodules/msquic/build/bin/Release \
MSQUIC_INC=../../../../submodules/msquic/src/inc \
/usr/bin/env bash ./make-quic.sh
else
msquic-java: libpni
cd ./base/src/main/c && \
MSQUIC_LD=../../../../submodules/msquic/artifacts/bin/windows/x64_Release_openssl \
MSQUIC_INC=../../../../submodules/msquic/src/inc \
/usr/bin/env bash ./make-quic.sh
endif
.PHONY: msquic
msquic:
cd ./submodules/msquic/ && make
Expand Down Expand Up @@ -244,7 +262,11 @@ fubuki-linux:
endif

.PHONY: quic
ifeq (0,$(IS_WIN))
quic: vfdposix msquic msquic-java
else
quic: vfdwindows msquic msquic-java
endif
.PHONY: quic-linux
quic-linux: vfdposix-linux msquic-linux msquic-java-linux
.PHONY: _quic-all-linux
Expand All @@ -263,7 +285,7 @@ quic-all: _quic-all-linux
endif

.PHONY: vfdwindows
vfdwindows:
vfdwindows: libpni
cd ./base/src/main/c && ./make-windows.sh

.PHONY: libpni
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/io/vproxy/app/app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import io.vproxy.app.vproxyx.Simple;
import io.vproxy.base.Config;
import io.vproxy.base.dns.Resolver;
import io.vproxy.base.util.LogType;
import io.vproxy.base.util.Logger;
import io.vproxy.base.util.MainUtils;
import io.vproxy.base.util.Utils;
import io.vproxy.base.util.*;
import io.vproxy.base.util.callback.Callback;
import io.vproxy.base.util.callback.JoinCallback;
import io.vproxy.base.util.thread.VProxyThread;
import io.vproxy.base.util.thread.VProxyThreadJsonParserCacheHolder;
import io.vproxy.pni.graal.GraalUtils;
import io.vproxy.r.org.graalvm.nativeimage.ImageInfoDelegate;
import io.vproxy.vfd.IPPort;
import io.vproxy.vproxyx.*;
import vjson.parser.ParserUtils;
Expand Down Expand Up @@ -59,6 +57,9 @@ private static void beforeStart() {
Utils.loadDynamicLibrary("pni");
} catch (Throwable t) {
Logger.warn(LogType.ALERT, "unable to load dynamic library: pni, native features cannot be used");
if (ImageInfoDelegate.inImageCode() && OS.isWindows()) {
Logger.warn(LogType.ALERT, "Tip: You may need MinGW UCRT64 (or libgcc_s_seh-1.dll,libwinpthread-1.dll) to make pni work");
}
}
GraalUtils.init();
ParserUtils.setParserCacheHolder(new VProxyThreadJsonParserCacheHolder());
Expand Down
11 changes: 6 additions & 5 deletions base/src/main/c-generated-graal/io_vproxy_msquic_MsQuicMod2.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ extern "C" {
#include <jni.h>
#include <pni.h>
#include "msquic.h"
#include "io_vproxy_msquic_QuicExtraApiTable.h"
#include "io_vproxy_msquic_CxPlatProcessEventLocals.h"
#include "io_vproxy_vfd_posix_AEFiredExtra.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadInit(struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals);
JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadBeforePoll(struct CxPlatProcessEventLocals * CxPlatProcessEventLocals);
JNIEXPORT uint8_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadAfterPoll(struct CxPlatProcessEventLocals * locals, int32_t num, aeFiredExtra * events);
JNIEXPORT int32_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadFinalize(struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals);
JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadInit(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals);
JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadBeforePoll(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatProcessEventLocals);
JNIEXPORT uint8_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadAfterPoll(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * locals, int32_t num, aeFiredExtra * events);
JNIEXPORT int32_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadFinalize(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals);

#ifdef __cplusplus
}
#endif
#endif // _Included_io_vproxy_msquic_MsQuicMod2
// metadata.generator-version: pni 22.0.0.17
// sha256:bac7d02e392bfb431ee628f5ff583914a2c4ead8e02aab37a6c759f926f65e01
// sha256:2b219ea4cf2ac3f697f0812ba12772ce8f80a7665378abfc2db67d632ff65844
26 changes: 12 additions & 14 deletions base/src/main/c-generated-graal/io_vproxy_msquic_MsQuicMod2.impl.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include "io_vproxy_msquic_MsQuicMod2.h"
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadInit(struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals) {
MsQuicCxPlatWorkerThreadInit(CxPlatWorkerThreadLocals);
JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadInit(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals) {
api->WorkerThreadInit(CxPlatWorkerThreadLocals);
}

JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadBeforePoll(struct CxPlatProcessEventLocals * CxPlatProcessEventLocals) {
MsQuicCxPlatWorkerThreadBeforePoll(CxPlatProcessEventLocals);
JNIEXPORT void JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadBeforePoll(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatProcessEventLocals) {
api->WorkerThreadBeforePoll(CxPlatProcessEventLocals);
}

JNIEXPORT uint8_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadAfterPoll(struct CxPlatProcessEventLocals * locals, int32_t num, aeFiredExtra * events) {
JNIEXPORT uint8_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadAfterPoll(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * locals, int32_t num, aeFiredExtra * events) {
locals->CqeCount = num;
for (int i = 0; i < num && i < CxPlatProcessCqesArraySize; ++i) {
#ifdef __linux__
Expand All @@ -22,22 +21,21 @@ JNIEXPORT uint8_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatW
#elif defined(__APPLE__)
locals->Cqes[i].udata = events[i].ud;
locals->Cqes[i].filter = events[i].mask;
#else
locals->CqeCount = 0;
printf("unsupported platform\n");
fflush(stdout);
#else // windows
locals->Cqes[i].lpOverlapped = events[i].ud;
locals->Cqes[i].dwNumberOfBytesTransferred = events[i].mask;
#endif
}
int ret = MsQuicCxPlatWorkerThreadAfterPoll(locals);
int ret = api->WorkerThreadAfterPoll(locals);
return ret;
}

JNIEXPORT int32_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_MsQuicCxPlatWorkerThreadFinalize(struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals) {
return MsQuicCxPlatWorkerThreadFinalize(CxPlatWorkerThreadLocals);
JNIEXPORT int32_t JNICALL JavaCritical_io_vproxy_msquic_MsQuicMod2_WorkerThreadFinalize(QUIC_EXTRA_API_TABLE * api, struct CxPlatProcessEventLocals * CxPlatWorkerThreadLocals) {
return api->WorkerThreadFinalize(CxPlatWorkerThreadLocals);
}

#ifdef __cplusplus
}
#endif
// metadata.generator-version: pni 22.0.0.17
// sha256:6549fdff39e4231b3a3a119bf2d43235cf4b858c232cc06445c16dcc3174c06e
// sha256:64b3263dffa56295007578ca4b4d9c65f902a6fb86dc6b406776f85755b93825
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class io_vproxy_vfd_posix_SocketAddressUnion */
#ifndef _Included_io_vproxy_vfd_posix_SocketAddressUnion
#define _Included_io_vproxy_vfd_posix_SocketAddressUnion
#ifdef __cplusplus
extern "C" {
#endif

union SocketAddressUnion;
typedef union SocketAddressUnion SocketAddressUnion;

#ifdef __cplusplus
}
#endif

#include <jni.h>
#include <pni.h>
#include "io_vproxy_vfd_posix_SocketAddressIPv4ST.h"
#include "io_vproxy_vfd_posix_SocketAddressIPv6ST.h"

#ifdef __cplusplus
extern "C" {
#endif

PNIEnvExpand(SocketAddressUnion, SocketAddressUnion *)
PNIBufExpand(SocketAddressUnion, SocketAddressUnion, 42)

union SocketAddressUnion {
SocketAddressIPv4_st v4;
SocketAddressIPv6_st v6;
};

#ifdef __cplusplus
}
#endif
#endif // _Included_io_vproxy_vfd_posix_SocketAddressUnion
// metadata.generator-version: pni 22.0.0.17
// sha256:64053b5106b961599d2072adef45802472adfc519b116532760ed3714408b4b8
31 changes: 31 additions & 0 deletions base/src/main/c-generated-graal/io_vproxy_vfd_windows_HANDLE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class io_vproxy_vfd_windows_HANDLE */
#ifndef _Included_io_vproxy_vfd_windows_HANDLE
#define _Included_io_vproxy_vfd_windows_HANDLE
#ifdef __cplusplus
extern "C" {
#endif

struct dummyHANDLE;
typedef struct dummyHANDLE dummyHANDLE;

#ifdef __cplusplus
}
#endif

#include <jni.h>
#include <pni.h>

#ifdef __cplusplus
extern "C" {
#endif

PNIEnvExpand(dummyHANDLE, dummyHANDLE *)
PNIBufExpand(dummyHANDLE, dummyHANDLE, (0 /* !!invalid!! */))

#ifdef __cplusplus
}
#endif
#endif // _Included_io_vproxy_vfd_windows_HANDLE
// metadata.generator-version: pni 22.0.0.17
// sha256:f024833918c089b23a7f4e0bcaaed7d38fca08bb8e416cc37405ffc9a2dd8c28
35 changes: 35 additions & 0 deletions base/src/main/c-generated-graal/io_vproxy_vfd_windows_IOCP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class io_vproxy_vfd_windows_IOCP */
#ifndef _Included_io_vproxy_vfd_windows_IOCP
#define _Included_io_vproxy_vfd_windows_IOCP
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#include <jni.h>
#include <pni.h>
#include "ioapiset.h"
#include "exception.h"
#include "io_vproxy_vfd_windows_HANDLE.h"
#include "io_vproxy_vfd_windows_OverlappedEntry.h"
#include "io_vproxy_vfd_windows_SOCKET.h"
#include "io_vproxy_vfd_windows_Overlapped.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_getQueuedCompletionStatusEx(PNIEnv_int * env, HANDLE handle, OVERLAPPED_ENTRY * completionPortEntries, uint32_t count, int32_t milliseconds, uint8_t alertable);
JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_createIoCompletionPort(PNIEnv_dummyHANDLE * env, HANDLE fileHandle, HANDLE existingCompletionPort, void * completionKey, int32_t numberOfConcurrentThreads);
JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_postQueuedCompletionStatus(PNIEnv_void * env, HANDLE completionPort, int32_t numberOfBytesTransferred, void * completionKey, OVERLAPPED * overlapped);

#ifdef __cplusplus
}
#endif
#endif // _Included_io_vproxy_vfd_windows_IOCP
// metadata.generator-version: pni 22.0.0.17
// sha256:2840a12c7829fe33cf2187e211c4d01b08b6bd44857bca9f05b0e9c63b8082f4
53 changes: 53 additions & 0 deletions base/src/main/c-generated-graal/io_vproxy_vfd_windows_IOCP.impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "io_vproxy_vfd_windows_IOCP.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_getQueuedCompletionStatusEx(PNIEnv_int * env, HANDLE handle, OVERLAPPED_ENTRY * completionPortEntries, uint32_t count, int32_t milliseconds, uint8_t alertable) {
ULONG nRemoved = 0;
BOOL ok = GetQueuedCompletionStatusEx(
handle, completionPortEntries, count,
&nRemoved, milliseconds, alertable
);
if (!ok) {
if (GetLastError() == WAIT_TIMEOUT) {
nRemoved = 0;
} else {
return throwIOExceptionBasedOnErrno(env);
}
}
env->return_ = nRemoved;
return 0;
}

JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_createIoCompletionPort(PNIEnv_dummyHANDLE * env, HANDLE fileHandle, HANDLE existingCompletionPort, void * completionKey, int32_t numberOfConcurrentThreads) {
HANDLE handle = CreateIoCompletionPort(
fileHandle, existingCompletionPort,
(ULONG_PTR)completionKey, numberOfConcurrentThreads
);
if (handle == INVALID_HANDLE_VALUE) {
return throwIOExceptionBasedOnErrno(env);
}
env->return_ = (void*)handle;
return 0;
}

JNIEXPORT int JNICALL Java_io_vproxy_vfd_windows_IOCP_postQueuedCompletionStatus(PNIEnv_void * env, HANDLE completionPort, int32_t numberOfBytesTransferred, void * completionKey, OVERLAPPED * overlapped) {
BOOL ok = PostQueuedCompletionStatus(
completionPort,
numberOfBytesTransferred,
(ULONG_PTR)completionKey,
overlapped
);
if (!ok) {
return throwIOExceptionBasedOnErrno(env);
}
return 0;
}

#ifdef __cplusplus
}
#endif
// metadata.generator-version: pni 22.0.0.17
// sha256:5730e82ee6793f27cb30fde6f9a909967bd0ff262a9f6e03304b7940c80314ad
30 changes: 30 additions & 0 deletions base/src/main/c-generated-graal/io_vproxy_vfd_windows_Overlapped.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class io_vproxy_vfd_windows_Overlapped */
#ifndef _Included_io_vproxy_vfd_windows_Overlapped
#define _Included_io_vproxy_vfd_windows_Overlapped
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#include <jni.h>
#include <pni.h>
#include "minwinbase.h"
#include "io_vproxy_vfd_windows_HANDLE.h"

#ifdef __cplusplus
extern "C" {
#endif

PNIEnvExpand(OVERLAPPED, OVERLAPPED *)
PNIBufExpand(OVERLAPPED, OVERLAPPED, 32)

#ifdef __cplusplus
}
#endif
#endif // _Included_io_vproxy_vfd_windows_Overlapped
// metadata.generator-version: pni 22.0.0.17
// sha256:64bedb16fd587e5ef172be5e417bf42a5ea1ca3335de6a325a3f967b75eee06b
Loading

0 comments on commit 5d50855

Please sign in to comment.