[KA03] Support tcp keepalive offload

When offload is starting, socket will be switched to repair
mode. Read and write on the socket will not be allowed until
repair mode is turned off. If remote packet arrives, repair
mode will be turned off automatically and a callback will
be raised to indicate that socket is ready to read from.

Bug: 114151147
Test: -atest FrameworksNetTests
      -manual

Change-Id: I0c335865912e183e7ad32a8ea12188f02ccde5fd
This commit is contained in:
markchien
2018-12-27 22:49:51 +08:00
committed by Chalard Jean
parent 011b1f15c2
commit e5591ce2fa
11 changed files with 811 additions and 28 deletions

View File

@@ -22,18 +22,15 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.LinkProperties.CompareResult;
import android.net.LinkProperties.ProvisioningChange;
import android.net.RouteInfo;
import android.os.Parcel;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.system.OsConstants;
import android.util.ArraySet;
import com.android.internal.util.TestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -849,18 +846,6 @@ public class LinkPropertiesTest {
assertEquals(new ArraySet<>(expectRemoved), (new ArraySet<>(result.removed)));
}
private void assertParcelingIsLossless(LinkProperties source) {
Parcel p = Parcel.obtain();
source.writeToParcel(p, /* flags */ 0);
p.setDataPosition(0);
final byte[] marshalled = p.marshall();
p = Parcel.obtain();
p.unmarshall(marshalled, 0, marshalled.length);
p.setDataPosition(0);
LinkProperties dest = LinkProperties.CREATOR.createFromParcel(p);
assertEquals(source, dest);
}
@Test
public void testLinkPropertiesParcelable() throws Exception {
LinkProperties source = new LinkProperties();
@@ -882,12 +867,12 @@ public class LinkPropertiesTest {
source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96"));
assertParcelingIsLossless(source);
TestUtils.assertParcelingIsLossless(source, LinkProperties.CREATOR);
}
@Test
public void testParcelUninitialized() throws Exception {
LinkProperties empty = new LinkProperties();
assertParcelingIsLossless(empty);
TestUtils.assertParcelingIsLossless(empty, LinkProperties.CREATOR);
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.net.SocketKeepalive.InvalidPacketException;
import android.net.TcpKeepalivePacketData.TcpSocketInfo;
import com.android.internal.util.TestUtils;
import libcore.net.InetAddressUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.InetAddress;
import java.nio.ByteBuffer;
@RunWith(JUnit4.class)
public final class TcpKeepalivePacketDataTest {
@Before
public void setUp() {}
@Test
public void testV4TcpKeepalivePacket() {
final InetAddress srcAddr = InetAddressUtils.parseNumericAddress("192.168.0.1");
final InetAddress dstAddr = InetAddressUtils.parseNumericAddress("192.168.0.10");
final int srcPort = 1234;
final int dstPort = 4321;
final int seq = 0x11111111;
final int ack = 0x22222222;
final int wnd = 8000;
final int wndScale = 2;
TcpKeepalivePacketData resultData = null;
TcpSocketInfo testInfo = new TcpSocketInfo(
srcAddr, srcPort, dstAddr, dstPort, seq, ack, wnd, wndScale);
try {
resultData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
} catch (InvalidPacketException e) {
fail("InvalidPacketException: " + e);
}
assertEquals(testInfo.srcAddress, resultData.srcAddress);
assertEquals(testInfo.dstAddress, resultData.dstAddress);
assertEquals(testInfo.srcPort, resultData.srcPort);
assertEquals(testInfo.dstPort, resultData.dstPort);
assertEquals(testInfo.seq, resultData.tcpSeq);
assertEquals(testInfo.ack, resultData.tcpAck);
assertEquals(testInfo.rcvWndScale, resultData.tcpWndScale);
TestUtils.assertParcelingIsLossless(resultData, TcpKeepalivePacketData.CREATOR);
final byte[] packet = resultData.getPacket();
// IP version and TOS.
ByteBuffer buf = ByteBuffer.wrap(packet);
assertEquals(buf.getShort(), 0x4500);
// Source IP address.
byte[] ip = new byte[4];
buf = ByteBuffer.wrap(packet, 12, 4);
buf.get(ip);
assertArrayEquals(ip, srcAddr.getAddress());
// Destination IP address.
buf = ByteBuffer.wrap(packet, 16, 4);
buf.get(ip);
assertArrayEquals(ip, dstAddr.getAddress());
buf = ByteBuffer.wrap(packet, 20, 12);
// Source port.
assertEquals(buf.getShort(), srcPort);
// Destination port.
assertEquals(buf.getShort(), dstPort);
// Sequence number.
assertEquals(buf.getInt(), seq);
// Ack.
assertEquals(buf.getInt(), ack);
// Window size.
buf = ByteBuffer.wrap(packet, 34, 2);
assertEquals(buf.getShort(), wnd >> wndScale);
}
//TODO: add ipv6 test when ipv6 supported
}