Commit Graph

42 Commits

Author SHA1 Message Date
Maciej Żenczykowski
6e66a36367 Tethering/bpf_progs/offload - replace all TC_ACT_OK with TC_ACT_PIPE
The correct return code to keep on processing any further TC
attached programs is 'TC_ACT_PIPE' and not 'TC_ACT_OK' (which
is terminal).

Without this the ipv6 tether offload program causes termination
of processing and the ipv6 clatd offload program never actually
handles any packets (while tethering is active).

This results in lack of bpf xlat64 offloading for tethered ipv4
traffic on an ipv6-only (cellular) network.

This in turn means incoming TCP packets get GRO'ed, do not get
bpf offloaded, and get delivered to the clat daemon, which
due to them being bigger than the mtu (due to gro) cannot
handle them and discards them.

This results in poor performance, since tcp falls back to 1 mss/mtu
sized packet per rtt.

Tested via tethering a linux laptop on an ipv6-only cellular connection
and downloading the linux kernel from kernel.org via 'wget -6' and 'wget -4'.
Before:
  IPv6: over 2MB/s, observed:
    5805 packets, including 4 sackOK
  IPv4: under 1MB/s, observed:
    9300 packets, including 8 sackOK, 387 sack 1, 501 sack 2, 2310 sack 3
After:
  IPv6: over 7MB/s, observed:
    16702 packets, including 4 sackOK
  IPv4: over 9MB/s, observed:
    32755 packets, including 2 sackOK

Test: builds, TreeHugger, see above
Bug: 195624908
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I623dacb5a37dc689cea34499c3906c11fcaf946c
2021-08-24 23:24:19 +00:00
Maciej Żenczykowski
c29af79e26 ipv4 tethering offload - decrement ttl during forwarding
Bug: 192635131
Test: atest, TreeHugger, flashed on phone, forwarded ipv4 traffic
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I88e5293a54b7867a8e7dd76f6fd8e2a52d31e51a
2021-07-02 11:33:17 +00:00
Hungming Chen
3feb782282 bpf: Add interface index to BPF devmap
Add upstream and downstream interface index to BPF map and rename the
BPF map name from xdp_devmap to dev.

$ adb shell dumpsys tethering
    Device map:
      ifindex (iface) -> ifindex (iface)
        21 (21) -> 21 (21)
        25 (25) -> 25 (25)
        12 (rmnet_data2) -> 12 (rmnet_data2)

$ adb shell ip addr
12: rmnet_data2 ..
21: wlan1 ..
25: rndis0 ..

$ adb shell ls /sys/fs/bpf/tethering
map_offload_tether_dev_map

Test: atest TetheringCoverageTests

Change-Id: Ic49965f3374d9e196ee672ec2f0e9e08f3847deb
2021-05-12 19:57:18 +08:00
Nucca Chen
8ba40704f3 Merge changes I515be275,Ied159454,I5a29bdd1
* changes:
  bpf_progs - adjust for dstMac addition
  Populate the key destination mac address
  Set the limit whenever any IPv4 or IPv6 rule exists.
2021-04-07 14:08:37 +00:00
Maciej Żenczykowski
8e69ec111f bpf offload - make sure bpf code can access packet headers
If a tc ebpf program writes into a packet using direct packet access
then the packet will automatically be uncloned and pulled by
additional prologue inserted by the kernel itself.  See
tc_cls_act_prologue() & bpf_unclone_prologue() in kernel sources
(this is how the clat ebpf program works, which does DPA writes).

However in the forwarding programs we only *read* from the packets
using direct packet access, but never write.  All writes happen via
kernel bpf helpers (this is mostly an implementation detail: since
we need to use helpers for checksum updates, I decided to also use
checksums for the writes themselves).  As such the insert 'automatic
unclone/pull' logic doesn't trigger.

It is thus possible (it depends on the skb layout delivered by the
nic driver) for 0 bytes of the packet to be accessible for read
using direct packet access.  We thus need to explicitly try to pull
in the header of the packet so that we can inspect it.

In most cases (on most drivers for most packet types) this will
end up being a no-op (because the headers will already be in
the linear portion of the skb).  But on some drivers for some
packet types it ends up mattering.

Test: TreeHugger, makes icmpv6 tether forwarding work on bramble
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4b07e57728ce544ffb908527ea11ecc315e5acec
2021-04-06 21:50:17 +00:00
Maciej Żenczykowski
62733f5524 bpf_progs - adjust for dstMac addition
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I515be275d09dc7e6bae2564f7db2445ea15cc757
2021-04-02 05:12:33 +00:00
Maciej Żenczykowski
3f32a83207 fix whitespace in offload.c macro
Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibe325714f95a1accce8add16220a31ca15913816
2021-03-18 02:34:01 -07:00
Maciej Żenczykowski
90b81ac174 ebpf offload - beginnings of xdp implementation
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6956e78bd325b63dc6dc813440f9f80c29aada36
2021-03-10 14:02:50 +00:00
Maciej Żenczykowski
acddd4f183 ebpf offload - make it possible to boot on Q era 4.14/4.19 kernels
by marking programs as optional and providing appropriate stub implementations.

Test: TreeHugger
Bug: 181045068
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I021e7bcbfe4236242f517f067f89777fc08ecd8d
2021-03-10 13:59:46 +00:00
Maciej Żenczykowski
2278aed207 ebpf offload - noop program reorder
This is just a cut'n'paste reordering of programs.
Goal is to put rawip programs above ether ones.
This will enable next change to be easier to read.

Test: TreeHugger
Bug: 181045068
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icebf4bf0505136e97b7b6950fb0b790582eb495e
2021-03-10 13:59:00 +00:00
Maciej Żenczykowski
db2cff5cdf bpf offload - add a new 'tether_xdp_devmap'
It will map device ifindex to itself (but note that internally in the
kernel this is optimized into a map from ifindex to direct device
pointer), but only for xdp transmit capable devices (other devices
will not have an entry).

This will allow the use of bpf_redirect_map() from xdp tethering programs.

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I29684e6761727d1115e9b4d75486eccbca3d5e33
2021-03-01 23:42:43 -08:00
Treehugger Robot
fcd5ec1971 Merge "ebpf offload: bump size of ipv4 tethering maps" 2021-02-26 05:43:07 +00:00
Treehugger Robot
4945772ba7 Merge "ebpf offload: mark as critical for tethering not netd" 2021-02-26 05:39:07 +00:00
Maciej Żenczykowski
8549af9f17 ebpf offload: bump size of ipv4 tethering maps
For ipv6 we need 1 entry per client, so 64 seems like plenty,
while for ipv4 we need 1 entry per flow, so even 1024 seems
like it might not be enough, but it's much better than 64.

Nucca says:
  # cat proc/sys/net/netfilter/nf_conntrack_buckets
  65536
  # cat proc/sys/net/netfilter/nf_conntrack_max
  262144

  per https://www.kernel.org/doc/Documentation/networking/nf_conntrack-sysctl.txt
  the default “nf_conntrack_max” is “nf_conntrack_buckets * 4”.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib7d1d8c19bc688c442d842cf5c9f45cdf1241754
2021-02-25 19:11:39 -08:00
Maciej Żenczykowski
607d6dda0d ebpf offload: mark as critical for tethering not netd
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5bccfcbababb09a412ac1093513ad93c254b349b
2021-02-25 19:10:01 -08:00
Lorenzo Colitti
56be03eb28 Move definitions used by tethering bpf code into the module.
Bug: 167645754
Test: m com.android.tethering
Change-Id: Ia4fb1201e5f4e0c35baba7865b26b4cec0945a64
2021-02-24 12:16:05 +09:00
Maciej Żenczykowski
e982f091e1 ebpf offload - rename PUNT/DROP to TC_PUNT/TC_DROP
because it is not appropriate for use in XDP programs

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibd5dac9676bae7aa5f10fbcfd777291f72bec819
2021-02-18 06:40:33 +00:00
Maciej Żenczykowski
e4a726a5c6 ebpf offload - add support for tethering of ipv4 udp
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6229e33cb219e9acd14f5b880cfa2ea0a34442f4
2021-02-18 06:38:36 +00:00
Maciej Żenczykowski
82ee26be75 ebpf offload - do L4 header present check earlier
and more importantly unconditionally.  This requires less effort
on the part of the in-kernel bpf verifier.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibaa94bf096fc81c4d984dfabf515131b1c81ef09
2021-02-18 06:23:32 +00:00
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
d561b7f67c Add IPv4 counters.
Test: dumpsys tethering bpf
Change-Id: Ie21fee8ad12430ecd21c3903587d25b8dd2a96c4
2021-02-10 04:44:35 +00: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
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
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
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
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
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
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
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
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
Maciej Żenczykowski
088fe1983b add placeholder ipv4 ebpf tethering offload programs
Additionally better document tether stats/limit keys/value.

Test: atest, TreeHugger, the programs load
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1e1832fc4f5a6704e00ea99ca377988c52a82eb1
2021-01-21 05:12:21 +00:00
Maciej Żenczykowski
5b00fbd618 switch bpf programs to group network_stack
Test: atest, TreeHugger, the programs load
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3ab4383978acb2681a0fd5aee2700d12c62c2769
2021-01-20 18:09:32 +00:00
Maciej Żenczykowski
bca0c85822 Add tethering programs for upstream.
Test: atest, TreeHugger, the programs load
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I26bc60c1066ed7317bda48ec3fa701c36e0b46dd
2021-01-20 18:05:52 +00:00
Maciej Żenczykowski
770e0a7a8f refactor: TetherIngress becomes TetherDownstream6 (and friends)
Generated via:
  #!/bin/bash

  do_replace() {
    pushd "$1" >/dev/null
    shift
    git grep "$1" | cut -d: -f1 | sort -u | while read f; do
      sed -i -r "s@$1@$2@g" "${f}"
      git add "${f}"
    done
    popd >/dev/null
  }

  replace() {
    do_replace packages/modules/Connectivity/Tethering "$@"
    do_replace system/netd "$@"
  }

  do_reset() {
    pushd "$1" >/dev/null
    git rhh
    popd >/dev/null
  }

  main() {
    do_reset packages/modules/Connectivity/Tethering
    do_reset system/netd

    replace TetherIngressKey TetherDownstream6Key
    replace TetherIngressValue TetherDownstream6Value

    pushd packages/modules/Connectivity/Tethering/src/com/android/networkstack/tethering >/dev/null
    git mv TetherIngressKey.java TetherDownstream6Key.java
    git mv TetherIngressValue.java TetherDownstream6Value.java
    popd >/dev/null

    replace TETHER_INGRESS_PROG TETHER_DOWNSTREAM6_TC_PROG
    replace ingress_tether tether_downstream6
    replace ingress/tether_ tether_downstream6_
    replace TETHER_INGRESS_MAP TETHER_DOWNSTREAM6_MAP
    replace tether_ingress_map tether_downstream6_map

    replace getTetherIngressMapFd getTetherDownstream6MapFd
    replace getTetherIngressProgFd getTetherDownstream6TcProgFd

    replace mBpfIngressMap mBpfDownstream6Map
    replace bpfIngressMap bpfDownstream6Map
    replace printIngressMap printDownstream6Map
    replace 'BPF ingress map' 'BPF downstream ipv6 map'
  }

  main "$@"; exit

Followed by:
  cd system/netd
  # change regexp in tests/binder_test.cpp:3374
  git add tests/binder_test.cpp

(and whitespace adjustments to prevent lines from going over 100 characters)

Afterwards we have:
  $ adbz shell ls /sys/fs/bpf/tethering
  map_offload_tether_downstream6_map
  map_offload_tether_limit_map
  map_offload_tether_stats_map
  map_test_tether_downstream6_map
  prog_offload_schedcls_tether_downstream6_ether
  prog_offload_schedcls_tether_downstream6_rawip
  prog_test_xdp_drop_ipv4_udp_ether

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib04dc0c6e3d034461f3f84e09f6fa93a862e1bb2
2021-01-19 23:18:46 -08:00
Maciej Żenczykowski
6b7829f2c1 switch from SEC() to DEFINE_BPF_PROG()
I keep on failing to find this using grep because it
doesn't match how all the other programs are defined,
so change it for consistency.

Test: builds, atest, TreeHugger
Signed-off-by: Maciej Żenczykowski<maze@google.com>
Change-Id: Ib61b375bef84d2b489080866b2411c84880e4ef2
2021-01-20 05:18:50 +00:00
Maciej Żenczykowski
e9810ffef1 move bpf test map into a separate file
This allows for better separation of test vs production code:
we will add more test maps and programs here later.

Test: builds
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7b22e3e148ebf43fdf43dc68d0dea354f7627688
2021-01-18 18:36:00 -08:00
markchien
7052688dde Provide a easy way to access bpf maps from java
A wrapper for bpf map opening, reading/writing, and iteration.

Bug: 173167302
Test: atest BpfMapTest

Change-Id: I792b41978b322c9e4969cd7b6c35d6978ab86bc4
2021-01-11 17:43:27 +08:00
Hungming Chen
56c632caed Move BPF offload program from netd to mainline
Test: as the follows.
$ adb shell ls sys/fs/bpf | grep offload
map_offload_tether_ingress_map
map_offload_tether_limit_map
map_offload_tether_stats_map
prog_offload_schedcls_ingress_tether_ether
prog_offload_schedcls_ingress_tether_rawip

$ adb shell dumpsys netd --short
  TetherController
    BPF ingress map: iif(iface) v6addr -> oif(iface) srcmac ..
      13(rmnet_data3) 2401:e180:8842:6e3:bc6a:2e45:c30:f418 -> ..
    BPF stats (downlink): iif(iface) -> packets bytes errors
      13(rmnet_data3) -> 7 488 0
    BPF limit: iif(iface) -> bytes
      13(rmnet_data3) -> 9223372036854775807

Log:
bpfloader: Loaded object: /apex/com.android.tethering/etc/bpf/offload.o

Change-Id: I71b7efb1f4bd6eb1cd469a0e1e4a56342dc6e579
2020-09-16 20:38:14 +08:00