Skip to content

Commit 6a9b1d1

Browse files
committed
[posix] support non-critical read() call
macos tap devices will block the program when using critical read()
1 parent fd8c711 commit 6a9b1d1

File tree

9 files changed

+50
-5
lines changed

9 files changed

+50
-5
lines changed

base/src/main/c-generated/io_vproxy_vfd_posix_PosixNative.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_getIPv6Remote(PNIEnv_
6565
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_getUDSLocal(PNIEnv_SocketAddressUDS_st * env, int32_t fd, SocketAddressUDS_st * return_);
6666
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_getUDSRemote(PNIEnv_SocketAddressUDS_st * env, int32_t fd, SocketAddressUDS_st * return_);
6767
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_read(PNIEnv_int * env, int32_t fd, void * directBuffer, int32_t off, int32_t len);
68+
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_readBlocking(PNIEnv_int * env, int32_t fd, void * directBuffer, int32_t off, int32_t len);
6869
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_write(PNIEnv_int * env, int32_t fd, void * directBuffer, int32_t off, int32_t len);
6970
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_sendtoIPv4(PNIEnv_int * env, int32_t fd, void * directBuffer, int32_t off, int32_t len, int32_t addrHostOrder, int32_t port);
7071
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_sendtoIPv6(PNIEnv_int * env, int32_t fd, void * directBuffer, int32_t off, int32_t len, char * fullAddr, int32_t port);
@@ -80,5 +81,5 @@ JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_setCoreAffinityForCur
8081
}
8182
#endif
8283
#endif // _Included_io_vproxy_vfd_posix_PosixNative
83-
// metadata.generator-version: pni 21.0.0.12
84-
// sha256:fb5216887c255149d2b81e719096596542b69cf6d882a55bd40296387c1623c9
84+
// metadata.generator-version: pni 21.0.0.17
85+
// sha256:1e84eb47f36e590566f1bbbf692bdba8e228a6ba2a5900b4e62f016ecfb9a38b

base/src/main/c/io_vproxy_vfd_posix_GeneralPosix.c

+5
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_read
600600
return 0;
601601
}
602602

603+
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_readBlocking
604+
(PNIEnv_int* env, int32_t fd, void* directBuffer, int32_t off, int32_t len) {
605+
return Java_io_vproxy_vfd_posix_PosixNative_read(env, fd, directBuffer, off, len);
606+
}
607+
603608
JNIEXPORT int JNICALL Java_io_vproxy_vfd_posix_PosixNative_write
604609
(PNIEnv_int* env, int32_t fd, void* directBuffer, int32_t off, int32_t len) {
605610
if (len == 0) {

base/src/main/generated/io/vproxy/vfd/posix/PosixNative.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,23 @@ public int read(PNIEnv ENV, int fd, ByteBuffer directBuffer, int off, int len) t
708708
return ENV.returnInt();
709709
}
710710

711+
private static final MethodHandle readBlockingMH = PanamaUtils.lookupPNIFunction(new PNILinkOptions(), "Java_io_vproxy_vfd_posix_PosixNative_readBlocking", int.class /* fd */, ByteBuffer.class /* directBuffer */, int.class /* off */, int.class /* len */);
712+
713+
public int readBlocking(PNIEnv ENV, int fd, ByteBuffer directBuffer, int off, int len) throws java.io.IOException {
714+
ENV.reset();
715+
int ERR;
716+
try {
717+
ERR = (int) readBlockingMH.invokeExact(ENV.MEMORY, fd, PanamaUtils.format(directBuffer), off, len);
718+
} catch (Throwable THROWABLE) {
719+
throw PanamaUtils.convertInvokeExactException(THROWABLE);
720+
}
721+
if (ERR != 0) {
722+
ENV.throwIf(java.io.IOException.class);
723+
ENV.throwLast();
724+
}
725+
return ENV.returnInt();
726+
}
727+
711728
private static final MethodHandle writeMH = PanamaUtils.lookupPNIFunction(new PNILinkOptions().setCritical(true), "Java_io_vproxy_vfd_posix_PosixNative_write", int.class /* fd */, ByteBuffer.class /* directBuffer */, int.class /* off */, int.class /* len */);
712729

713730
public int write(PNIEnv ENV, int fd, ByteBuffer directBuffer, int off, int len) throws java.io.IOException {
@@ -879,5 +896,5 @@ public void setCoreAffinityForCurrentThread(PNIEnv ENV, long mask) throws java.i
879896
}
880897
}
881898
}
882-
// metadata.generator-version: pni 21.0.0.17
883-
// sha256:168e4c184415e27beb86430bef5d5d1c23c2b272c410d8f1933413fde1ee9f67
899+
// metadata.generator-version: pni 21.0.0.18
900+
// sha256:a6ab0d88c014cf3f59f372ce03960a5a63f19153fefec2b5565170cd42416346

base/src/main/java/io/vproxy/base/selector/wrap/blocking/BlockingDatagramFD.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private void doRead() {
289289

290290
var buf = Utils.allocateByteBuffer(readBufSize);
291291
try {
292-
fd.read(buf);
292+
fd.readBlocking(buf);
293293
} catch (IOException e) {
294294
lastReadException = e;
295295
isReading = false;

base/src/main/java/io/vproxy/vfd/ReadableByteStream.java

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
public interface ReadableByteStream {
77
int read(ByteBuffer dst) throws IOException;
8+
9+
default int readBlocking(ByteBuffer buf) throws IOException {
10+
return read(buf);
11+
}
812
}

base/src/main/java/io/vproxy/vfd/posix/GeneralPosix.java

+6
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ public int read(int fd, ByteBuffer directBuffer, int off, int len) throws IOExce
289289
fd, directBuffer, off, len);
290290
}
291291

292+
@Override
293+
public int readBlocking(int fd, ByteBuffer directBuffer, int off, int len) throws IOException {
294+
return PosixNative.get().readBlocking(VProxyThread.current().getEnv(),
295+
fd, directBuffer, off, len);
296+
}
297+
292298
@Override
293299
public int write(int fd, ByteBuffer directBuffer, int off, int len) throws IOException {
294300
return PosixNative.get().write(VProxyThread.current().getEnv(),

base/src/main/java/io/vproxy/vfd/posix/Posix.java

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public interface Posix {
9393

9494
int read(int fd, ByteBuffer directBuffer, int off, int len) throws IOException;
9595

96+
int readBlocking(int fd, ByteBuffer directBuffer, int off, int len) throws IOException;
97+
9698
int write(int fd, ByteBuffer directBuffer, int off, int len) throws IOException;
9799

98100
int sendtoIPv4(int fd, ByteBuffer directBuffer, int off, int len, int addrHostOrder, int port) throws IOException;

base/src/main/java/io/vproxy/vfd/posix/PosixNetworkFD.java

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public int read(ByteBuffer dst) throws IOException {
2828
return utilRead(dst, (buf, off, len) -> posix.read(fd, buf, off, len));
2929
}
3030

31+
public int readBlocking(ByteBuffer dst) throws IOException {
32+
checkFD();
33+
checkConnected();
34+
checkNotClosed();
35+
36+
return utilRead(dst, (buf, off, len) -> posix.readBlocking(fd, buf, off, len));
37+
}
38+
3139
public int write(ByteBuffer src) throws IOException {
3240
checkFD();
3341
checkConnected();

pni-template/src/main/java/io/vproxy/vfd/posix/PosixNative.java

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ interface PNIPosixNative {
143143
@LinkerOption.Critical
144144
int read(int fd, @Raw ByteBuffer directBuffer, int off, int len) throws IOException;
145145

146+
int readBlocking(int fd, @Raw ByteBuffer directBuffer, int off, int len) throws IOException;
147+
146148
@LinkerOption.Critical
147149
int write(int fd, @Raw ByteBuffer directBuffer, int off, int len) throws IOException;
148150

0 commit comments

Comments
 (0)