GeezerVison is showing github.com — Port: Fix UDP_GRO segment size is lost when reading with recvmmsg (#1… · lltopk/netty@24ff62b
File treeExpand file treeCollapse file tree Open diff view settings transport-native-epoll/src test/java/io/netty/channel/epoll
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -567,12 +567,11 @@ static void init_packet(JNIEnv* env, jobject packet, struct msghdr* msg, int len | 567 | 567 | init_packet_address(env, packet, (struct sockaddr_storage*) msg->msg_name, packetSenderAddrFieldId, packetSenderAddrLenFieldId, packetSenderScopeIdFieldId, packetSenderPortFieldId); | 568 | 568 |
| 569 | 569 | struct cmsghdr *cmsg = NULL; | 570 | | - uint16_t gso_size = 0; | 571 | | - uint16_t *gsosizeptr = NULL; | | 570 | + int gso_size = 0; | 572 | 571 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) { | 573 | 572 | 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)); | 576 | 575 | } | 577 | 576 | #ifdef IP_RECVORIGDSTADDR | 578 | 577 | 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 | 588 | 587 | struct sockaddr_storage sock_address; | 589 | 588 | int addrSize = sizeof(sock_address); | 590 | 589 | // 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 }; | 592 | 591 | msg.msg_name = &sock_address; | 593 | 592 | msg.msg_namelen = (socklen_t) addrSize; | 594 | 593 | 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 | 615 | 614 | int addrSize = sizeof(addr); | 616 | 615 | memset(addr, 0, addrSize); | 617 | 616 | 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; | 625 | 621 | } | 626 | | -#endif // IP_RECVORIGDSTADDR | 627 | 622 |
| 628 | 623 | int i; | 629 | 624 |
| @@ -632,6 +627,7 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo | 632 | 627 | if (packet == NULL) { | 633 | 628 | // This should never happen but just handle it and return early. This way if GetObjectArrayElement(...) | 634 | 629 | // did put an exception on the stack we will see it and not crash. | | 630 | + free(controls); | 635 | 631 | return -1; | 636 | 632 | } | 637 | 633 | 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 | 640 | 636 | msg[i].msg_hdr.msg_name = addr + i; | 641 | 637 | msg[i].msg_hdr.msg_namelen = (socklen_t) storageSize; | 642 | 638 |
| 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; | 647 | 641 | } | 648 | 642 |
| 649 | 643 | ssize_t res; | @@ -661,8 +655,7 @@ static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jbo | 661 | 655 | init_packet(env, packet, &msg[i].msg_hdr, msg[i].msg_len); | 662 | 656 | } | 663 | 657 | } | 664 | | - // Free the control message buffer if needed. | 665 | | - free(cntrlbuf); | | 658 | + free(controls); | 666 | 659 |
| 667 | 660 | if (res < 0) { | 668 | 661 | return -err; | @@ -837,7 +830,7 @@ static JNINativeMethod* createDynamicMethodsTable(const char* packagePrefix) { | 837 | 830 | } | 838 | 831 | memset(dynamicMethods, 0, size); | 839 | 832 | memcpy(dynamicMethods, fixed_method_table, sizeof(fixed_method_table)); | 840 | | - | | 833 | + | 841 | 834 | JNINativeMethod* dynamicMethod = &dynamicMethods[fixed_method_table_size]; | 842 | 835 | NETTY_JNI_UTIL_PREPEND(packagePrefix, "io/netty/channel/epoll/NativeDatagramPacketArray$NativeDatagramPacket;II)I", dynamicTypeName, error); | 843 | 836 | NETTY_JNI_UTIL_PREPEND("(IZ[L", dynamicTypeName, dynamicMethod->signature, error); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -65,7 +65,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable { | 65 | 65 | } | 66 | 66 |
| 67 | 67 | public void testSendSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb) throws Throwable { | 68 | | - testSegmentedDatagramPacket(sb, cb, false, false); | | 68 | + testSegmentedDatagramPacket(sb, cb, false, false, false); | 69 | 69 | } | 70 | 70 |
| 71 | 71 | @Test | @@ -79,7 +79,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable { | 79 | 79 | } | 80 | 80 |
| 81 | 81 | public void testSendSegmentedDatagramPacketComposite(Bootstrap sb, Bootstrap cb) throws Throwable { | 82 | | - testSegmentedDatagramPacket(sb, cb, true, false); | | 82 | + testSegmentedDatagramPacket(sb, cb, true, false, false); | 83 | 83 | } | 84 | 84 |
| 85 | 85 | @Test | @@ -93,7 +93,7 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable { | 93 | 93 | } | 94 | 94 |
| 95 | 95 | public void testSendAndReceiveSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb) throws Throwable { | 96 | | - testSegmentedDatagramPacket(sb, cb, false, true); | | 96 | + testSegmentedDatagramPacket(sb, cb, false, true, false); | 97 | 97 | } | 98 | 98 |
| 99 | 99 | @Test | @@ -107,11 +107,26 @@ public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable { | 107 | 107 | } | 108 | 108 |
| 109 | 109 | public void testSendAndReceiveSegmentedDatagramPacketComposite(Bootstrap sb, Bootstrap cb) throws Throwable { | 110 | | - testSegmentedDatagramPacket(sb, cb, true, true); | | 110 | + testSegmentedDatagramPacket(sb, cb, true, true, false); | 111 | 111 | } | 112 | 112 |
| 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) | 114 | 124 | 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 { | 115 | 130 | if (!(cb.group() instanceof EpollEventLoopGroup)) { | 116 | 131 | // Only supported for the native epoll transport. | 117 | 132 | return; | @@ -143,7 +158,13 @@ public void channelRead0(ChannelHandlerContext ctx, Object msgs) { | 143 | 158 | // Enable GRO and also ensure we can read everything with one read as otherwise | 144 | 159 | // we will drop things on the floor. | 145 | 160 | 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 | + } | 147 | 168 | } | 148 | 169 | sc = sb.handler(new SimpleChannelInboundHandler<DatagramPacket>() { | 149 | 170 | @Override | |
|