GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port: Fix UDP_GRO segment size is lost when reading with recvmmsg (#1… · lltopk/netty@24ff62b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-native-epoll/src
    • main/c
      • netty_epoll_native.c
    • test/java/io/netty/channel/epoll
      • EpollDatagramUnicastTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-native-epoll/src/main/c/netty_epoll_native.c‎

Copy file name to clipboardExpand all lines: transport-native-epoll/src/main/c/netty_epoll_native.c
+13-20Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,11 @@ static void init_packet(JNIEnv* env, jobject packet, struct msghdr* msg, int len
567567
init_packet_address(env, packet, (struct sockaddr_storage*) msg->msg_name, packetSenderAddrFieldId, packetSenderAddrLenFieldId, packetSenderScopeIdFieldId, packetSenderPortFieldId);
568568

569569
struct cmsghdr *cmsg = NULL;
570-
uint16_t gso_size = 0;
571-
uint16_t *gsosizeptr = NULL;
570+
int gso_size = 0;
572571
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
573572
if (cmsg->cmsg_level == SOL_UDP && cmsg->cmsg_type == UDP_GRO) {
574-
gsosizeptr = (uint16_t *) CMSG_DATA(cmsg);
575-
gso_size = *gsosizeptr;
573+
// The kernel reports the segment size as an int
574+
gso_size = *((int *) CMSG_DATA(cmsg));
576575
}
577576
#ifdef IP_RECVORIGDSTADDR
578577
else if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVORIGDSTADDR) {
@@ -588,7 +587,7 @@ static jint netty_epoll_native_recvmsg0(JNIEnv* env, jclass clazz, jint fd, jboo
588587
struct sockaddr_storage sock_address;
589588
int addrSize = sizeof(sock_address);
590589
// Enough space for GRO and IP_RECVORIGDSTADDR
591-
char control[CMSG_SPACE(sizeof(uint16_t)) + sizeof(struct sockaddr_storage)] = { 0 };
590+
char control[CMSG_SPACE(sizeof(int)) + sizeof(struct sockaddr_storage)] = { 0 };
592591
msg.msg_name = &sock_address;
593592
msg.msg_namelen = (socklen_t) addrSize;
594593
msg.msg_iov = (struct iovec*) (intptr_t) (*env)->GetLongField(env, packet, packetMemoryAddressFieldId);
@@ -615,15 +614,11 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo
615614
int addrSize = sizeof(addr);
616615
memset(addr, 0, addrSize);
617616
int storageSize = sizeof(struct sockaddr_storage);
618-
char* cntrlbuf = NULL;
619-
620-
#ifdef IP_RECVORIGDSTADDR
621-
int readLocalAddr = 0;
622-
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_RECVORIGDSTADDR,
623-
&readLocalAddr, sizeof(readLocalAddr)) != -1 && readLocalAddr != 0) {
624-
cntrlbuf = malloc(sizeof(char) * storageSize * len);
617+
int controlSize = CMSG_SPACE(sizeof(int)) + storageSize;
618+
char* controls = malloc(controlSize * len);
619+
if (controls == NULL) {
620+
return -ENOMEM;
625621
}
626-
#endif // IP_RECVORIGDSTADDR
627622

628623
int i;
629624

@@ -632,6 +627,7 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo
632627
if (packet == NULL) {
633628
// This should never happen but just handle it and return early. This way if GetObjectArrayElement(...)
634629
// did put an exception on the stack we will see it and not crash.
630+
free(controls);
635631
return -1;
636632
}
637633
msg[i].msg_hdr.msg_iov = (struct iovec*) (intptr_t) (*env)->GetLongField(env, packet, packetMemoryAddressFieldId);
@@ -640,10 +636,8 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo
640636
msg[i].msg_hdr.msg_name = addr + i;
641637
msg[i].msg_hdr.msg_namelen = (socklen_t) storageSize;
642638

643-
if (cntrlbuf != NULL) {
644-
msg[i].msg_hdr.msg_control = cntrlbuf + i * storageSize;
645-
msg[i].msg_hdr.msg_controllen = storageSize;
646-
}
639+
msg[i].msg_hdr.msg_control = controls + i * controlSize;
640+
msg[i].msg_hdr.msg_controllen = controlSize;
647641
}
648642

649643
ssize_t res;
@@ -661,8 +655,7 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo
661655
init_packet(env, packet, &msg[i].msg_hdr, msg[i].msg_len);
662656
}
663657
}
664-
// Free the control message buffer if needed.
665-
free(cntrlbuf);
658+
free(controls);
666659

667660
if (res < 0) {
668661
return -err;
@@ -837,7 +830,7 @@ static JNINativeMethod* createDynamicMethodsTable(const char* packagePrefix) {
837830
}
838831
memset(dynamicMethods, 0, size);
839832
memcpy(dynamicMethods, fixed_method_table, sizeof(fixed_method_table));
840-
833+
841834
JNINativeMethod* dynamicMethod = &dynamicMethods[fixed_method_table_size];
842835
NETTY_JNI_UTIL_PREPEND(packagePrefix, "io/netty/channel/epoll/NativeDatagramPacketArray$NativeDatagramPacket;II)I", dynamicTypeName, error);
843836
NETTY_JNI_UTIL_PREPEND("(IZ[L", dynamicTypeName, dynamicMethod->signature, error);
Collapse file

‎transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollDatagramUnicastTest.java‎

Copy file name to clipboardExpand all lines: transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollDatagramUnicastTest.java
+27-6Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
6565
}
6666

6767
public void testSendSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb) throws Throwable {
68-
testSegmentedDatagramPacket(sb, cb, false, false);
68+
testSegmentedDatagramPacket(sb, cb, false, false, false);
6969
}
7070

7171
@Test
@@ -79,7 +79,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
7979
}
8080

8181
public void testSendSegmentedDatagramPacketComposite(Bootstrap sb, Bootstrap cb) throws Throwable {
82-
testSegmentedDatagramPacket(sb, cb, true, false);
82+
testSegmentedDatagramPacket(sb, cb, true, false, false);
8383
}
8484

8585
@Test
@@ -93,7 +93,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
9393
}
9494

9595
public void testSendAndReceiveSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb) throws Throwable {
96-
testSegmentedDatagramPacket(sb, cb, false, true);
96+
testSegmentedDatagramPacket(sb, cb, false, true, false);
9797
}
9898

9999
@Test
@@ -107,11 +107,26 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
107107
}
108108

109109
public void testSendAndReceiveSegmentedDatagramPacketComposite(Bootstrap sb, Bootstrap cb) throws Throwable {
110-
testSegmentedDatagramPacket(sb, cb, true, true);
110+
testSegmentedDatagramPacket(sb, cb, true, true, false);
111111
}
112112

113-
private void testSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb, boolean composite, boolean gro)
113+
@Test
114+
public void testSendAndReceiveSegmentedDatagramPacketWithScatteringRead(TestInfo testInfo) throws Throwable {
115+
run(testInfo, new Runner<Bootstrap, Bootstrap>() {
116+
@Override
117+
public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
118+
testSendAndReceiveSegmentedDatagramPacketWithScatteringRead(bootstrap, bootstrap2);
119+
}
120+
});
121+
}
122+
123+
public void testSendAndReceiveSegmentedDatagramPacketWithScatteringRead(Bootstrap sb, Bootstrap cb)
114124
throws Throwable {
125+
testSegmentedDatagramPacket(sb, cb, false, true, true);
126+
}
127+
128+
private void testSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb, boolean composite, boolean gro,
129+
boolean scatteringRead) throws Throwable {
115130
if (!(cb.group() instanceof EpollEventLoopGroup)) {
116131
// Only supported for the native epoll transport.
117132
return;
@@ -143,7 +158,13 @@ public void channelRead0(ChannelHandlerContext ctx, Object msgs) {
143158
// Enable GRO and also ensure we can read everything with one read as otherwise
144159
// we will drop things on the floor.
145160
sb.option(EpollChannelOption.UDP_GRO, true);
146-
sb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(bufferCapacity));
161+
if (scatteringRead) {
162+
// Room for two batches, so the batch is read with recvmmsg instead of recvmsg.
163+
sb.option(EpollChannelOption.MAX_DATAGRAM_PAYLOAD_SIZE, bufferCapacity);
164+
sb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(2 * bufferCapacity));
165+
} else {
166+
sb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(bufferCapacity));
167+
}
147168
}
148169
sc = sb.handler(new SimpleChannelInboundHandler<DatagramPacket>() {
149170
@Override

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.