Skip to content

Commit

Permalink
Network Traffic: Consider CLAT interfaces (IPv6)
Browse files Browse the repository at this point in the history
IPv6-only networks that support 464XLAT use CLAT to access IPv4
addresses, but the traffic that occurs over the CLAT interface is
tracked separately; it is not reflected at all in the stats of the
underlying interface, so prior to this change, IPv4 traffic on
applicable IPv6-only networks is not reflected in the network traffic
monitor, either.

While there is no exposed API to retrieve stacked interfaces, we can
still try to grab the stats for a CLAT interface (which always starts
with "v4-"), just in case it exists, and if so, add in those stats.

Test: Manual: While connected to an IPv6-only network, such as a
T-Mobile cellular data connection, attempt to load https://1.1.1.1
in a browser while the network traffic monitor is enabled. Reload
the page / choose different languages to cause traffic to occur,
and it should be reflected in the traffic monitor.

Issue: calyxos#792
Co-authored-by: Oliver Scott <olivercscott@gmail.com>
Change-Id: Ida768ebe73a47bb06da53aeb7b5c6882f0090e75
  • Loading branch information
2 people authored and mikeNG committed May 21, 2024
1 parent 360dad6 commit d0c004c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions sdk/src/java/org/lineageos/internal/statusbar/NetworkTraffic.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Stream;

public class NetworkTraffic extends TextView {
private static final String TAG = "NetworkTraffic";

private static final boolean DEBUG = false;

// This must match the interface prefix in Connectivity's clatd.c.
private static final String CLAT_PREFIX = "v4-";

private static final int MODE_DISABLED = 0;
private static final int MODE_UPSTREAM_ONLY = 1;
private static final int MODE_DOWNSTREAM_ONLY = 2;
Expand Down Expand Up @@ -163,12 +167,13 @@ private void recalculateStats() {
// Sum tx and rx bytes from all sources of interest
long txBytes = 0;
long rxBytes = 0;
// Add interface stats
for (LinkProperties linkProperties : mLinkPropertiesMap.values()) {
final String iface = linkProperties.getInterfaceName();
if (iface == null) {
continue;
}
// Add interface stats, including stats from Clat's IPv4 interface
// (for applicable IPv6 networks). Stats are 0 if it doesn't exist.
final String[] ifaces = mLinkPropertiesMap.values().stream()
.map(link -> link.getInterfaceName()).filter(iface -> iface != null)
.flatMap(iface -> Stream.of(iface, CLAT_PREFIX + iface))
.toArray(String[]::new);
for (String iface : ifaces) {
final long ifaceTxBytes = TrafficStats.getTxBytes(iface);
final long ifaceRxBytes = TrafficStats.getRxBytes(iface);
if (DEBUG) {
Expand Down

0 comments on commit d0c004c

Please sign in to comment.