IP connectivity metrics: update ipconnectivity.proto

This patch adds several new fields to ipconnectivity.proto for
improving APF metrics, connect metrics, and link_layer inference.

Test: build, flashed, ran $ adb shell dumpsys connmetrics list proto
      updated metrics unit tests depending on proto schema
Bug: 34901696
Change-Id: I6f68e16f583a6b407f33f8a500133e100f3453c7
diff --git a/proto/src/ipconnectivity.proto b/proto/src/ipconnectivity.proto
index b5afc40..76c5418 100644
--- a/proto/src/ipconnectivity.proto
+++ b/proto/src/ipconnectivity.proto
@@ -9,6 +9,8 @@
 // NetworkId represents the id given by the system to a physical network on the
 // Android device. It is used to relates events to each other for devices with
 // multiple networks (WiFi, 4G, ...).
+// Deprecated since version 3, replaced by top-level network_id field in
+// IpConnectivityEvent.
 message NetworkId {
   // Every network gets assigned a network_id on creation based on order of
   // creation. Thus network_id N is assigned to the network created directly
@@ -91,7 +93,8 @@
 // This message is associated to android.net.metrics.NetworkEvent.
 message NetworkEvent {
   // The id of the network on which this event happened.
-  optional NetworkId network_id = 1;
+  // Deprecated since version 3.
+  optional NetworkId network_id = 1 [deprecated = true];
 
   // The type of network event, represented by NETWORK_* constants defined in
   // android.net.metrics.NetworkEvent.
@@ -107,7 +110,8 @@
 // This message is associated to android.net.metrics.ValidationProbeEvent.
 message ValidationProbeEvent {
   // The id of the network for which the probe was sent.
-  optional NetworkId network_id = 1;
+  // Deprecated since version 3.
+  optional NetworkId network_id = 1 [deprecated = true];
 
   // The time it took for that probe to complete or time out.
   optional int32 latency_ms = 2;
@@ -167,18 +171,28 @@
 
 // Represents latency and errno statistics of the connect() system call.
 // Since version 2.
+// Next tag: 7
 message ConnectStatistics {
   // The number of connect() operations recorded.
   optional int32 connect_count = 1;
 
+  // The number of connect() operations done in blocking mode.
+  // Since version 3.
+  optional int32 connect_blocking_count = 5;
+
   // The number of connect() operations with IPv6 socket address.
   optional int32 ipv6_addr_count = 2;
 
-  // The time it took for each successful connect() operation to complete.
-  // The number of repeated values can be less than connect_count in case of
-  // event rate-limiting.
+  // The time it took for successful blocking connect() operations to complete
+  // The number of repeated values can be less than connect_blocking_count in
+  // case of event rate-limiting.
   repeated int32 latencies_ms = 3;
 
+  // The time it took for successful connect() operation to complete in
+  // non-blocking mode. The number of repeated values can be less than
+  // connect_count - connect_blocking_count in case of event rate-limiting.
+  repeated int32 non_blocking_latencies_ms = 6;
+
   // Counts of all error values returned by failed connect() operations.
   // The Pair key field is the errno code. The Pair value field is the count
   // for that errno code.
@@ -212,10 +226,15 @@
 
 // Represents the generation of an Android Packet Filter program.
 // Since version 1.
+// Next tag: 8
 message ApfProgramEvent {
-  // Lifetime of the program in seconds.
+  // Maximum lifetime of the program in seconds.
   optional int64 lifetime = 1;
 
+  // Effective lifetime of the program in seconds from the time the
+  // program was installed to the time it was replaced or removed.
+  optional int64 effective_lifetime = 7;
+
   // Number of RAs filtered by the APF program.
   optional int32 filtered_ras = 2;
 
@@ -236,6 +255,7 @@
 // Represents Router Advertisement listening statistics for an interface with
 // Android Packet Filter enabled.
 // Since version 1.
+// Next tag: 12
 message ApfStatistics {
   // The time interval in milliseconds these stastistics cover.
   optional int64 duration_ms = 1;
@@ -261,6 +281,14 @@
 
   // The maximum APF program size in byte advertised by hardware.
   optional int32 max_program_size = 9;
+
+  // The total number of successful APF program updates triggered by any state
+  // change in ApfFilter. Since version 3.
+  optional int32 program_updates_all = 10;
+
+  // The total number of APF program updates triggered when disabling the
+  // multicast filter. Since version 3.
+  optional int32 program_updates_allowing_multicast = 11;
 }
 
 // Represents the reception of a Router Advertisement packet for an interface
@@ -308,7 +336,7 @@
 }
 
 // Represents one of the IP connectivity event defined in this file.
-// Next tag: 16
+// Next tag: 19
 message IpConnectivityEvent {
   // Time in ms when the event was recorded.
   optional int64 time_ms = 1;
@@ -318,7 +346,27 @@
   // Since version 2.
   optional LinkLayer link_layer = 15;
 
-  // Event type.
+  // Represents the id given by the system to a physical network on the device.
+  // Every network gets assigned a unique id on creation from a monotonic
+  // counter. The value 0 is never assigned to a network and means no network.
+  // It is used to correlate different types of events to each other for devices
+  // with multiple networks (WiFi, 4G, ...).
+  // Since version 3.
+  optional int32 network_id = 16;
+
+  // The interface name (wlan, rmnet, lo, ...) on which the event happened.
+  // Present if the link_layer field was not inferred from the if_name on
+  // the device, so that post-processing of the serialized proto can backfill
+  // link_layer. Since version 3.
+  optional string if_name = 17;
+
+  // The transport types of the network on which the event happened, expressed
+  // as a bit field of TRANSPORT_* constants as defined in NetworkCapabilities.
+  // Present if the link_layer field was not inferred from the transport types,
+  // so that post-processing of the serialized proto can backfill link_layer
+  // Since version 3.
+  optional int64 transports = 18;
+
   oneof event {
 
     // An event about the system default network.
@@ -371,9 +419,10 @@
   optional int32 dropped_events = 2;
 
   // The version number of the metrics events being collected.
-  //  nyc-dev: not populated, implicitly 0.
+  //  nyc:     not populated, implicitly 0.
   //  nyc-dr1: not populated, implicitly 1 (sailfish and marlin only).
   //  nyc-mr1: not populated, implicitly 1.
   //  nyc-mr2: 2.
+  //  oc:      3.
   optional int32 version = 3;
 };
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
index 415911e..11105d6 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
@@ -43,6 +43,7 @@
 import java.util.Arrays;
 import junit.framework.TestCase;
 
+// TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
 public class IpConnectivityEventBuilderTest extends TestCase {
 
     @SmallTest
@@ -58,8 +59,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  default_network_event <",
                 "    network_id <",
                 "      network_id: 102",
@@ -89,8 +93,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  dhcp_event <",
                 "    duration_ms: 192",
                 "    if_name: \"wlan0\"",
@@ -112,8 +119,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  dhcp_event <",
                 "    duration_ms: 0",
                 "    if_name: \"wlan0\"",
@@ -137,8 +147,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  dns_lookup_batch <",
                 "    event_types: 1",
                 "    event_types: 1",
@@ -185,8 +198,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  ip_provisioning_event <",
                 "    event_type: 1",
                 "    if_name: \"wlan0\"",
@@ -208,8 +224,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  ip_reachability_event <",
                 "    event_type: 512",
                 "    if_name: \"wlan0\"",
@@ -231,8 +250,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  network_event <",
                 "    event_type: 5",
                 "    latency_ms: 20410",
@@ -258,8 +280,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  validation_probe_event <",
                 "    latency_ms: 40730",
                 "    network_id <",
@@ -287,11 +312,15 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  apf_program_event <",
                 "    current_ras: 9",
                 "    drop_multicast: true",
+                "    effective_lifetime: 0",
                 "    filtered_ras: 7",
                 "    has_ipv4_addr: true",
                 "    lifetime: 200",
@@ -319,8 +348,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  apf_statistics <",
                 "    dropped_ras: 2",
                 "    duration_ms: 45000",
@@ -328,6 +360,8 @@
                 "    max_program_size: 2048",
                 "    parse_errors: 2",
                 "    program_updates: 4",
+                "    program_updates_all: 0",
+                "    program_updates_allowing_multicast: 0",
                 "    received_ras: 10",
                 "    zero_lifetime_ras: 1",
                 "  >",
@@ -351,8 +385,11 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 1",
+                "  transports: 0",
                 "  ra_event <",
                 "    dnssl_lifetime: -1",
                 "    prefix_preferred_lifetime: 300",
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
index f56f3f8..1f7c5f4 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
@@ -139,6 +139,7 @@
 
     @SmallTest
     public void testEndToEndLogging() {
+        // TODO: instead of comparing textpb to textpb, parse textpb and compare proto to proto.
         IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
 
         Parcelable[] events = {
@@ -158,16 +159,22 @@
         String want = joinLines(
                 "dropped_events: 0",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 100",
+                "  transports: 0",
                 "  ip_reachability_event <",
                 "    event_type: 512",
                 "    if_name: \"wlan0\"",
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 200",
+                "  transports: 0",
                 "  dhcp_event <",
                 "    duration_ms: 192",
                 "    if_name: \"wlan0\"",
@@ -175,8 +182,11 @@
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 300",
+                "  transports: 0",
                 "  default_network_event <",
                 "    network_id <",
                 "      network_id: 102",
@@ -191,8 +201,11 @@
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 400",
+                "  transports: 0",
                 "  ip_provisioning_event <",
                 "    event_type: 1",
                 "    if_name: \"wlan0\"",
@@ -200,8 +213,11 @@
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 500",
+                "  transports: 0",
                 "  validation_probe_event <",
                 "    latency_ms: 40730",
                 "    network_id <",
@@ -212,8 +228,11 @@
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 600",
+                "  transports: 0",
                 "  apf_statistics <",
                 "    dropped_ras: 2",
                 "    duration_ms: 45000",
@@ -221,13 +240,18 @@
                 "    max_program_size: 2048",
                 "    parse_errors: 2",
                 "    program_updates: 4",
+                "    program_updates_all: 0",
+                "    program_updates_allowing_multicast: 0",
                 "    received_ras: 10",
                 "    zero_lifetime_ras: 1",
                 "  >",
                 ">",
                 "events <",
+                "  if_name: \"\"",
                 "  link_layer: 0",
+                "  network_id: 0",
                 "  time_ms: 700",
+                "  transports: 0",
                 "  ra_event <",
                 "    dnssl_lifetime: -1",
                 "    prefix_preferred_lifetime: 300",
diff --git a/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java b/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
index cfd5598..637eaa3 100644
--- a/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
+++ b/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
@@ -213,9 +213,13 @@
 
         IpConnectivityEvent got = events.get(0);
         String want = String.join("\n",
+                "if_name: \"\"",
                 "link_layer: 0",
+                "network_id: 0",
                 "time_ms: 0",
+                "transports: 0",
                 "connect_statistics <",
+                "  connect_blocking_count: 0",
                 "  connect_count: 12",
                 "  errnos_counters <",
                 "    key: 1",