Merge "Pass buffer size to NetlinkUtils.netlinkSocketForProto" into main

This commit is contained in:
Maciej Żenczykowski 2024-01-31 01:42:49 +00:00 • committed by Gerrit Code Review
commit 2cb28ea360
6 changed files with 46 additions and 9 deletions

View file

@ -16,6 +16,7 @@
package com.android.networkstack.tethering;
import static com.android.net.module.util.netlink.NetlinkUtils.SOCKET_RECV_BUFSIZE;
import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP;
import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_REQUEST;
@ -198,7 +199,8 @@ public class OffloadHardwareInterface {
public NativeHandle createConntrackSocket(final int groups) {
final FileDescriptor fd;
try {
fd = NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_NETFILTER);
fd = NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_NETFILTER,
SOCKET_RECV_BUFSIZE);
} catch (ErrnoException e) {
mLog.e("Unable to create conntrack socket " + e);
return null;

View file

@ -15,6 +15,7 @@
*/
package com.android.server;
import static com.android.net.module.util.netlink.NetlinkUtils.SOCKET_RECV_BUFSIZE;
import static com.android.net.module.util.netlink.xfrm.XfrmNetlinkMessage.IPPROTO_ESP;
import static com.android.net.module.util.netlink.xfrm.XfrmNetlinkMessage.NETLINK_XFRM;
import static com.android.net.module.util.netlink.xfrm.XfrmNetlinkMessage.XFRM_MSG_NEWSA;
@ -106,7 +107,8 @@ public class IpSecXfrmController {
public static class Dependencies {
/** Get a new XFRM netlink socket and connect it */
public FileDescriptor newNetlinkSocket() throws ErrnoException, SocketException {
final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_XFRM);
final FileDescriptor fd =
NetlinkUtils.netlinkSocketForProto(NETLINK_XFRM, SOCKET_RECV_BUFSIZE);
NetlinkUtils.connectToKernel(fd);
return fd;
}

View file

@ -31,6 +31,7 @@ import static com.android.net.module.util.netlink.NetlinkConstants.stringForAddr
import static com.android.net.module.util.netlink.NetlinkConstants.stringForProtocol;
import static com.android.net.module.util.netlink.NetlinkUtils.DEFAULT_RECV_BUFSIZE;
import static com.android.net.module.util.netlink.NetlinkUtils.IO_TIMEOUT_MS;
import static com.android.net.module.util.netlink.NetlinkUtils.SOCKET_RECV_BUFSIZE;
import static com.android.net.module.util.netlink.NetlinkUtils.TCP_ALIVE_STATE_FILTER;
import static com.android.net.module.util.netlink.NetlinkUtils.connectToKernel;
import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP;
@ -281,7 +282,7 @@ public class InetDiagMessage extends NetlinkMessage {
int uid = INVALID_UID;
FileDescriptor fd = null;
try {
fd = NetlinkUtils.netlinkSocketForProto(NETLINK_INET_DIAG);
fd = NetlinkUtils.netlinkSocketForProto(NETLINK_INET_DIAG, SOCKET_RECV_BUFSIZE);
connectToKernel(fd);
uid = lookupUid(protocol, local, remote, fd);
} catch (ErrnoException | SocketException | IllegalArgumentException

View file

@ -86,6 +86,7 @@ public class NetlinkUtils {
public static final int DEFAULT_RECV_BUFSIZE = 8 * 1024;
public static final int SOCKET_RECV_BUFSIZE = 64 * 1024;
public static final int SOCKET_DUMP_RECV_BUFSIZE = 128 * 1024;
/**
* Return whether the input ByteBuffer contains enough remaining bytes for
@ -160,7 +161,7 @@ public class NetlinkUtils {
*/
public static void sendOneShotKernelMessage(int nlProto, byte[] msg) throws ErrnoException {
final String errPrefix = "Error in NetlinkSocket.sendOneShotKernelMessage";
final FileDescriptor fd = netlinkSocketForProto(nlProto);
final FileDescriptor fd = netlinkSocketForProto(nlProto, SOCKET_RECV_BUFSIZE);
try {
connectToKernel(fd);
@ -226,12 +227,18 @@ public class NetlinkUtils {
/**
* Create netlink socket with the given netlink protocol type.
*
* @param nlProto the netlink protocol
* @param bufferSize the receive buffer size to set when the value is not 0
*
* @return fd the fileDescriptor of the socket.
* @throws ErrnoException if the FileDescriptor not connect to be created successfully
*/
public static FileDescriptor netlinkSocketForProto(int nlProto) throws ErrnoException {
public static FileDescriptor netlinkSocketForProto(int nlProto, int bufferSize)
throws ErrnoException {
final FileDescriptor fd = Os.socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, nlProto);
Os.setsockoptInt(fd, SOL_SOCKET, SO_RCVBUF, SOCKET_RECV_BUFSIZE);
if (bufferSize > 0) {
Os.setsockoptInt(fd, SOL_SOCKET, SO_RCVBUF, bufferSize);
}
return fd;
}
@ -239,7 +246,7 @@ public class NetlinkUtils {
* Construct a netlink inet_diag socket.
*/
public static FileDescriptor createNetLinkInetDiagSocket() throws ErrnoException {
return Os.socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_INET_DIAG);
return netlinkSocketForProto(NETLINK_INET_DIAG, 0);
}
/**
@ -374,7 +381,7 @@ public class NetlinkUtils {
Consumer<T> func)
throws SocketException, InterruptedIOException, ErrnoException {
// Create socket
final FileDescriptor fd = netlinkSocketForProto(nlFamily);
final FileDescriptor fd = netlinkSocketForProto(nlFamily, SOCKET_DUMP_RECV_BUFSIZE);
try {
getAndProcessNetlinkDumpMessagesWithFd(fd, dumpRequestMessage, nlFamily,
msgClass, func);

View file

@ -21,6 +21,8 @@ import static android.system.OsConstants.AF_INET6;
import static android.system.OsConstants.AF_UNSPEC;
import static android.system.OsConstants.EACCES;
import static android.system.OsConstants.NETLINK_ROUTE;
import static android.system.OsConstants.SOL_SOCKET;
import static android.system.OsConstants.SO_RCVBUF;
import static com.android.net.module.util.netlink.NetlinkConstants.RTNL_FAMILY_IP6MR;
import static com.android.net.module.util.netlink.NetlinkUtils.DEFAULT_RECV_BUFSIZE;
import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP;
@ -33,6 +35,8 @@ import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import android.content.Context;
import android.net.util.SocketUtils;
import android.os.Build;
import android.system.ErrnoException;
import android.system.NetlinkSocketAddress;
import android.system.Os;
@ -43,6 +47,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.modules.utils.build.SdkLevel;
import com.android.net.module.util.Struct;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import libcore.io.IoUtils;
@ -204,4 +209,23 @@ public class NetlinkUtilsTest {
assertNotNull("Route doesn't contain destination: " + route, route.getDestination());
}
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R) // getsockoptInt requires > R
public void testNetlinkSocketForProto_defaultBufferSize() throws Exception {
final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE, 0);
final int bufferSize = Os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF) / 2;
assertTrue("bufferSize: " + bufferSize, bufferSize > 0); // whatever the default value is
SocketUtils.closeSocket(fd);
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R) // getsockoptInt requires > R
public void testNetlinkSocketForProto_setBufferSize() throws Exception {
final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE,
8000);
final int bufferSize = Os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF) / 2;
assertEquals(8000, bufferSize);
SocketUtils.closeSocket(fd);
}
}

View file

@ -60,7 +60,8 @@ public class TunInterfaceController {
public void createTunInterface() throws IOException {
mParcelTunFd = ParcelFileDescriptor.adoptFd(nativeCreateTunInterface(mIfName, MTU));
try {
mNetlinkSocket = NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_ROUTE);
mNetlinkSocket =
NetlinkUtils.netlinkSocketForProto(OsConstants.NETLINK_ROUTE, 0);
} catch (ErrnoException e) {
throw new IOException("Failed to create netlink socket", e);
}