Commit Graph

964 Commits

Author SHA1 Message Date
Maciej Żenczykowski 36867351ec offload ebpf - introduce 4.14+ ipv4 forwarding programs
We've backported the necessary support to all 4.14+ ACK kernels,
but we can't actually enforce that these changes will be picked
up by all devices.  Thus we can only make the full featured
implementations optional on [4.14..5.8) kernels, with a tcp-only
version for those 4.14+ devices where the full featured version
fails to load.

Note: there's still a fair bit of implementation work left
in the do_forward4() function itself.  This is really just
the skeleton.

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If78123e00d55a77f2ecd7da1547581797e23f9b2
2021-02-16 05:48:55 +00:00
Maciej Żenczykowski aefa0950b5 offload ebpf - do_forward4() - introduce updatetime argument
This will facilitate providing a tcp-only version of the programs
which due to TCP's very long timeouts will not need to use the
Linux 5.8+ bpf_ktime_get_boot_ns() helpers.

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1e49b6758d3754782ac6f8820e0c15aa20e4c61d
2021-02-15 12:33:01 +00:00
Maciej Żenczykowski b0ac41f0cd tethering offload ebpf - lower requirement from 5.9 to 5.8
As this is the actual version that is required,
ie. the version that supports bpf_ktime_get_boot_ns() helper.

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2ea4830597a0bed53950a5d0c483a47208959f35
2021-02-14 22:24:34 -08:00
Lorenzo Colitti af97e51c01 Merge "Load JNI in all classes that have native methods." 2021-02-12 03:19:00 +00:00
Maciej Żenczykowski c92e8ce636 fix TetheringCoverageTests
Test: atest TetheringCoverageTests now passes, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0d83de14784f323400a83ce282cae1a3e7b4b396
2021-02-11 16:06:46 -08:00
Lorenzo Colitti 6800811597 Load JNI in all classes that have native methods.
The tethering module uses JNI in various classes, but only calls
System.loadLibrary in TetheringService#makeTethering. This means
that:

1. Any test that uses a class that uses JNI must load the
   library itself.
2. Any code that runs before TetheringService#makeTethering could
   potentially crash if it uses JNI. We may never have such code
   though.

Instead, make every class that has a native method load the JNI
library itself at static initialization time. This guarantees
that the class will have the JNI code available in any context
(production, test, etc.)

System.loadLibrary is documented not to do anything if called
more than once with the same library name:
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#loadLibrary(java.lang.String)

and the implementation has a lock so it is safe to call from
multiple threads concurrently.

Test: builds, boots, tethering starts
Test: atest TetheringCoverageTests
Change-Id: I9c0147ae9a28877f416aaff387b426d304ae552d
2021-02-10 15:43:38 +09:00
Lorenzo Colitti d561b7f67c Add IPv4 counters.
Test: dumpsys tethering bpf
Change-Id: Ie21fee8ad12430ecd21c3903587d25b8dd2a96c4
2021-02-10 04:44:35 +00:00
Lorenzo Colitti 4e92da06fa Dump counters in "dumpsys tethering bpf".
This CL adds the counters collected by the tethering programs to
the output of "dumpsys tethering bpf". The counter names are
fetched by JNI code at startup and then read by the debugging
code on demand.

Example from my device:

  Forwarding errors:
    TCP_CONTROL_PACKET: 67
    NON_GLOBAL_SRC: 13
    NON_GLOBAL_DST: 17
    LOCAL_SRC_DST: 7

Test: manual (debug-only code)
Change-Id: I7ec7992612556a4bca5f1b6bb52c655d6d870201
2021-02-10 04:43:03 +00:00
Lorenzo Colitti 555472a186 Don't leak map fds in BpfCoordinator#dump.
Currently, the dump methods open BpfMap objects and never close
them. This leaks filedescriptors, and if dump is called often
enough, will crash the networkstack process.

Fix this by using try-with-resources statements that
automatically close the map when exiting the try block.

Change the signature of BpfMap#close from "throws Exception" to
"throws ErrnoException" since it does not throw any other type
of checked exceptions.

Test: "lsof | grep network_st" while running "dumpsys tethering bpf" in a loop
Change-Id: I66c407454c2715bf41bf3a2e81bd582f9ea5a905
2021-02-09 23:30:25 +09:00
Lorenzo Colitti 4431655a4d Merge "offload - finish ipv4 tethering" 2021-02-09 11:08:06 +00:00
Lorenzo Colitti 72ec3ba88a Improve BPF tethering counters.
Test: m
Change-Id: I53961e1e16bf90d76f16b9416c55b65e8374373d
2021-02-09 03:36:40 +00:00
Maciej Żenczykowski d46b3d8ef8 Merge changes I3fcd7eb8,I4bf2c762
* changes:
  Add error counters for various failures.
  Add a bpf_tethering_headers lib used by the programs and JNI.
2021-02-09 02:57:24 +00:00
Lorenzo Colitti b81584dbe5 Add error counters for various failures.
Currently, debugging the tethering programs is not easy because
in case of any failure they simply return TC_ACT_OK. This CL adds
a number of counters that the program can increment in the case
of interesting events such as malformed packets.

At the moment the counters are stored in a global tethering error
map, which is an ARRAY map of 32-bit counters. This should not
take up much space because there are only a dozen of these.

We might not need all of these counters. In future CLs we can
reduce the number of counters, or perhaps move them to a map of
maps so as to have separate counters on a per-interface basis.

Test: manual
Change-Id: I3fcd7eb8d318700092949ff2f39987bf4ba3656c
2021-02-09 02:56:52 +00:00
Lorenzo Colitti 734b14e07d Add a bpf_tethering_headers lib used by the programs and JNI.
This provides a place to store common definitions that can be
used by both the programs and by JNI code in the tethering
module.

Over time much of the tethering-specific code in bpf_shared.h
could move here.

Test: m
Bug: 177884581
Change-Id: I4bf2c762d61cd5db003d59880e6bf7d107ff1a07
2021-02-09 01:54:52 +00:00
Martin Stjernholm 199641d451 Revert "Don't crash Tethering service when WiFi feature is missing"
This reverts commit f6a1052f4e.

Reason for revert: Suspected reason for TetheringTests breakage
Bug: 179467044

Change-Id: I3a5ee621b32145b74a31d6c3e411bd789e395935
2021-02-05 11:47:48 +00:00
Tomasz Wasilczyk f6a1052f4e Don't crash Tethering service when WiFi feature is missing
Bug: 175430552
Test: disable WiFi feature on device with wlan0

Change-Id: I83012013ff6a276d9a5c70748b726289b007cb0a
2021-02-02 16:11:18 +00:00
Lucas Lin c0bae3c8c5 Merge "Add NetdStaticLibTestsLib into TetheringCoverageTests" 2021-02-02 03:38:37 +00:00
Maciej Żenczykowski ec5f67d97e offload - finish ipv4 tethering
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I88f14938c7c4f66190a3afa2acd9104284e27927
2021-01-27 17:54:07 -08:00
lucaslin 5dc7de221a Add NetdStaticLibTestsLib into TetheringCoverageTests
Add NetdStaticLibTestsLib into Tethering MTS , so that the tests
under client-libs can also be run when running tethering MTS.

Bug: 170598012
Test: atest TetheringCoverageTests
Change-Id: Idd47becaa4254ab80b74ad5f3ddcdd4b017a5e69
2021-01-27 17:54:56 +00:00
Treehugger Robot 0f7f9b6e40 Merge "Dump BPF offload rules for upstream IPv4 and IPv6." 2021-01-27 07:23:55 +00:00
Maciej Żenczykowski 7dfbcf58d2 merge Tether{Down,Up}stream6Value -> Tether6Value
Test: atest TetheringTests TetheringCoverageTests
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibdbba9ef6652dd11bb18eba316045dff440fcf9e
2021-01-27 06:41:59 +00:00
Lorenzo Colitti 61976f278f Dump BPF offload rules for upstream IPv4 and IPv6.
Add code to BpfCoordinator to dump upstream IPv4 and IPv6 rules.
For IPv4, currently only the upstream map is printed, because the
downstream map is expected to be symmetrical.

Example output:

=============
    21(21) -> 12(rmnet_data2) 86dd 00:00:00:00:00:00 00:00:00:00:00:00
    [wlan1]: iif(iface) oif(iface) v6addr srcmac dstmac
      12(rmnet_data2) 21(wlan1) /2001:240:2425:b5dc:21a9:995d:bc57:bc37 b2:b5:e0:24:21:c2 ca:06:8d:54:99:ad
    [IPv4]: iif(iface) oif(iface) src nat dst
      21(21) 12(rmnet_data2) 192.168.16.247:41544 -> 100.101.80.108:41544 -> 216.239.36.135:443
      21(21) 12(rmnet_data2) 192.168.16.247:42028 -> 100.101.80.108:42028 -> 8.8.4.4:853
      21(21) 12(rmnet_data2) 192.168.16.247:42032 -> 100.101.80.108:42032 -> 8.8.4.4:853
      21(21) 12(rmnet_data2) 192.168.16.247:42042 -> 100.101.80.108:42042 -> 8.8.4.4:853
      21(21) 12(rmnet_data2) 192.168.16.247:41816 -> 100.101.80.108:41816 -> 8.8.8.8:853
      21(21) 12(rmnet_data2) 192.168.16.247:42040 -> 100.101.80.108:42040 -> 8.8.4.4:853

=============

Also make it possible to do "dumpsys tethering bpf" and get only
the BPF dump.

Test: manual
Change-Id: I2aaa2fdda7d724994090c26feff585f24cd3283b
2021-01-27 14:30:51 +09:00
Lorenzo Colitti 0913a7a415 Merge changes Iaac422d7,Ia7638b31
* changes:
  Remove IpServer.Dependencies#getIfIndex.
  Address comments on aosp/1559686.
2021-01-27 03:04:23 +00:00
Maciej Żenczykowski cff64c8561 fix comments
Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic217e08484c39077bcc408195daa3311ed93ad28
2021-01-26 16:45:21 -08:00
Lorenzo Colitti f3b201f819 Remove IpServer.Dependencies#getIfIndex.
This code is unused.

Test: atest TetheringTests
Change-Id: Iaac422d72e8538b67798cb3ae3737deb7b426401
2021-01-27 00:53:59 +09:00
Lorenzo Colitti 51c1a95c82 Address comments on aosp/1559686.
Also remove some unnecessary line wrapping.

Test: atest TetheringTests
Change-Id: Ia7638b3198d7811cdbb34e959c50608cf1a656bf
2021-01-27 00:53:59 +09:00
Lorenzo Colitti 77262cea76 Add an isEmpty convenience method to BpfMap.
Test: new unit test
Change-Id: Ibec09f328e24111aee4760af2f75ea5a80ba84c3
2021-01-27 00:53:56 +09:00
Lorenzo Colitti 7bf39e56e1 Address a TODO in BpfMapTest.
Test: test-only change
Change-Id: I9a47234979cbb161dfcd0c97c54c0476aa753c5e
2021-01-27 00:53:02 +09:00
Lorenzo Colitti 5b1ed508cf Program the upstream IPv6 map in BpfCoordinator.
- Add methods to start and stop IPv6 forwarding upstream
- Populate the upstream IPv6 map when the first rule for any
  upstream/downstream pair is created.
- Clear the upstream IPv6 map when the last rule for any
  upstream/downstream pair is deleted.

Test: Added coverage to IpServerTest and BpfCoordinatorTest
Change-Id: Ib041081e95f5f449489ab63138de034222ffac8f
2021-01-27 00:52:09 +09:00
Lorenzo Colitti d69a886235 Rename TetherDownstream6Value to Tether6Value.
The two value types are identical so there is no need to have
separate classes for them.

Test: atest TetheringTests
Change-Id: Ia622b082d0a44373d21f51222f5e675e5bde08e0
2021-01-27 00:52:09 +09:00
Lorenzo Colitti 3c35666892 Merge changes from topic "bpf_tether4_rename"
* changes:
  merge Tether{Down,Up}stream4{Key,Value} - part 3 - fixups
  merge Tether{Down,Up}stream4{Key,Value} - part 2 - java
  merge Tether{Down,Up}stream4{Key,Value} - part 1 - C portion
2021-01-26 13:01:22 +00:00
Lorenzo Colitti a36d65da0a Mark the EntitlementManager PendingIntents as immutable.
These are read-only intents and should not be mutated by any
app receiving them.

Bug: 178324405
Bug: 178249531
Test: atest TetheringTests TetheringCoverageTests GtsTetheringTestCases
Change-Id: I5395e7c19e6453640ad790f2b9a9ba22bbcefa88
2021-01-26 15:14:26 +09:00
Lorenzo Colitti ae27ecf5ad Fix a WTF in IpServerTest.
Bug: 178324405
Test: atest TetheringTests
Change-Id: I2069ea79f982e6bb197388edd7820a05d1e651f1
2021-01-26 15:14:26 +09:00
Maciej Żenczykowski 911a7267f5 merge Tether{Down,Up}stream4{Key,Value} - part 3 - fixups
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia7840698e80ded33d8e0b59efe1ca7267254b892
2021-01-25 20:32:10 -08:00
Maciej Żenczykowski 32874eb6bc merge Tether{Down,Up}stream4{Key,Value} - part 2 - java
Generated via:
  git grep 'Tether(Down|Up)stream4(Key|Value)' | cut -d: -f1 | sort -u | while read i; do
    sed -r -i 's@TetherUpstream4Value@Tether4Value@g' "$i"
    sed -r -i 's@TetherDownstream4Value@Tether4Value@g' "$i"
    sed -r -i 's@TetherDownstream4Key@Tether4Key@g' "$i"
    sed -r -i 's@TetherUpstream4Key@Tether4Key@g' "$i"
  done

  cd Tethering/src/com/android/networkstack/tethering
  git mv TetherUpstream4Key.java Tether4Key.java
  git mv TetherUpstream4Value.java Tether4Value.java

  git diff TetherDownstream4Key.java Tether4Key.java
  git diff TetherDownstream4Value.java Tether4Value.java
  git rm TetherDownstream4Key.java
  git rm TetherDownstream4Value.java

Fixup resulting 'import' duplication
  mcedit Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java
  mcedit Tethering/apishim/30/com/android/networkstack/tethering/apishim/api30/BpfCoordinatorShimImpl.java
  mcedit Tethering/apishim/common/com/android/networkstack/tethering/apishim/common/BpfCoordinatorShim.java
  mcedit Tethering/tests/unit/src/android/net/ip/IpServerTest.java

Test: N/A, requires follow up commit
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1dfc3108ca4bbd0cefc3420bc7e421594b62619c
2021-01-25 20:32:05 -08:00
Maciej Żenczykowski 1feb8b4ac8 merge Tether{Down,Up}stream4{Key,Value} - part 1 - C portion
The keys are identical, and the values nearly so, this will make everyone's life easier.

Test: git grep 'Tether(Down|Up)stream4(Key|Value)' finds nothing
  (note this requires follow up commits)
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ifbff2c617ac5834ea80f827eaf89ca81e862baec
2021-01-25 20:31:11 -08:00
Lorenzo Colitti dc4189f0e4 Merge changes I8cd6e49b,Ibb52c7b7
* changes:
  [NFCT.TETHER.10] Add/delete IPv4 offload BPF rules to/from BPF map
  [NFCT.TETHER.9] Build IPv4 offload BPF rules for raw ip
2021-01-26 00:30:15 +00:00
Maciej Żenczykowski c2b0146045 offload - begin ipv4 tethering implementation
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0ad143be954d55d0ed39853031c7bd364378e1ec
2021-01-25 19:07:40 +00:00
Hungming Chen dd833dee31 [NFCT.TETHER.10] Add/delete IPv4 offload BPF rules to/from BPF map
Access the IPv4 downstream and upstream BPF map with the built rules.

Test: atest TetheringCoverageTests
Change-Id: I8cd6e49b377c72250988019eea57f93cccd78309
2021-01-25 20:13:47 +08:00
Maciej Żenczykowski 18552e8fb3 offload - ignore ethernet packets not destined for us
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icabd1db48a1189425a941384d534c0f18bf20827
2021-01-24 19:59:21 -08:00
Maciej Żenczykowski bf8ec1a21d rename do_forward to do_forward6
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I649dc490b1883a1e97f748370a77c956f1315e2e
2021-01-24 19:59:16 -08:00
Nucca Chen 2f1c2b25cd Merge changes I73f30477,I84db13ac
* changes:
  [NFCT.TETHER.8] Prepare the upstream information for IPv4 offload rule
  [NFCT.TETHER.7] Prepare the downstream information for IPv4 offload rule
2021-01-23 13:40:30 +00:00
Maciej Żenczykowski fc4f6540be bpf tether offload - do not offload TCP SYN/FIN/RST packets
We want connection establishment/shutdown to flow through
the kernel code path so connection tracking state is at least
somewhat correct.

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iee97baa65750188f3436937b16c9b320f0495a5a
2021-01-23 07:11:07 +00:00
Hungming Chen 347dd90620 [NFCT.TETHER.9] Build IPv4 offload BPF rules for raw ip
Build the upstream and upstream rules when the conntrack event is
received.

Test: atest TetheringCoverageTests
Change-Id: Ibb52c7b75812bd586091d809e260bc9206c06262
2021-01-22 19:37:37 +08:00
Hungming Chen 8784a60eab [NFCT.TETHER.8] Prepare the upstream information for IPv4 offload rule
Add upstream interface index and its IPv4 address mapping

Required for building IPv4 forwarding rule when a conntrack event is
received.

Test: atest TetheringCoverageTests
Change-Id: I73f304777704f09481b80d18751e0bab5bab2edc
2021-01-22 19:37:31 +08:00
Hungming Chen d71c06ec71 [NFCT.TETHER.7] Prepare the downstream information for IPv4 offload rule
Add and remove downstream client information to BpfCoordinator

Required for building IPv4 forwarding rule when a conntrack event is
received. The IpServer provides the following elements of a rule which
is not included in conntrack event:
- Downstream interface index
- Downstream Mac address
- Client IP address to Client Mac address

Test: atest TetheringCoverageTests
Change-Id: I84db13acc047ace5730d17f0d3dd99544f516084
2021-01-22 19:37:24 +08:00
Nucca Chen 168a96643d Merge "[NFCT.TETHER.1] Add conntrack monitor to BpfCoordinator" 2021-01-22 11:29:09 +00:00
Maciej Żenczykowski 30a2d8232b adjust to changes in map location
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idaaa1ede7c239655f5ec489532533c363d297864
2021-01-21 17:51:20 -08:00
Hungming Chen 46c30b1fd4 [NFCT.TETHER.1] Add conntrack monitor to BpfCoordinator
A preparation for starting monitoring conntrack event which is required
by IPv4 tethering offload.

Test: atest TetheringCoverageTests
Change-Id: Ied46aeca193554f52a90889dfdf92827e94845d6
2021-01-21 21:25:18 +08:00
Maciej Żenczykowski b199742d73 add placeholder ebpf programs for XDP tethering
Test: atest, TreeHugger, the programs load
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idcacf0502719336c1b6a959fe6b782f2bce9d6b5
2021-01-21 05:12:35 +00:00