1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.util;
18 
19 import android.annotation.IntDef;
20 
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 
24 /**
25  * Collection of utilities for data stall.
26  */
27 public class DataStallUtils {
28     public static final int DATA_STALL_EVALUATION_TYPE_NONE = 0;
29     /** Detect data stall using dns timeout counts. */
30     public static final int DATA_STALL_EVALUATION_TYPE_DNS = 1 << 0;
31     /** Detect data stall using tcp connection fail rate. */
32     public static final int DATA_STALL_EVALUATION_TYPE_TCP = 1 << 1;
33 
34     @IntDef(prefix = { "DATA_STALL_EVALUATION_TYPE_" }, value = {
35         DATA_STALL_EVALUATION_TYPE_NONE,
36         DATA_STALL_EVALUATION_TYPE_DNS,
37         DATA_STALL_EVALUATION_TYPE_TCP,
38     })
39     @Retention(RetentionPolicy.SOURCE)
40     public @interface EvaluationType {
41     }
42 
43     // Default configuration values for data stall detection.
44     public static final int DEFAULT_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD = 5;
45     public static final int DEFAULT_DATA_STALL_MIN_EVALUATE_TIME_MS = 60 * 1000;
46     public static final int DEFAULT_DATA_STALL_VALID_DNS_TIME_THRESHOLD_MS = 30 * 60 * 1000;
47     /**
48      * The threshold value for the number of consecutive dns timeout events received to be a
49      * signal of data stall. The number of consecutive timeouts needs to be {@code >=} this
50      * threshold to be considered a data stall. Set the value to {@code <= 0} to disable. Note
51      * that the value should be {@code > 0} if the DNS data stall detection is enabled.
52      *
53      */
54     public static final String CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD =
55             "data_stall_consecutive_dns_timeout_threshold";
56 
57     /**
58      * The minimal time interval in milliseconds for data stall reevaluation.
59      *
60      */
61     public static final String CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL =
62             "data_stall_min_evaluate_interval";
63 
64     /**
65      * DNS timeouts older than this timeout (in milliseconds) are not considered for detecting
66      * a data stall.
67      *
68      */
69     public static final String CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD =
70             "data_stall_valid_dns_time_threshold";
71 
72     /**
73      * Which data stall detection signal to use. This is a bitmask constructed by bitwise-or-ing
74      * (i.e. {@code |}) the DATA_STALL_EVALUATION_TYPE_* values.
75      *
76      * Type: int
77      * Valid values:
78      *   {@link #DATA_STALL_EVALUATION_TYPE_DNS} : Use dns as a signal.
79      *   {@link #DATA_STALL_EVALUATION_TYPE_TCP} : Use tcp info as a signal.
80      */
81     public static final String CONFIG_DATA_STALL_EVALUATION_TYPE = "data_stall_evaluation_type";
82     public static final int DEFAULT_DATA_STALL_EVALUATION_TYPES =
83             DATA_STALL_EVALUATION_TYPE_DNS | DATA_STALL_EVALUATION_TYPE_TCP;
84     // The default number of DNS events kept of the log kept for dns signal evaluation. Each event
85     // is represented by a {@link com.android.server.connectivity.NetworkMonitor#DnsResult} objects.
86     // It's also the size of array of {@link com.android.server.connectivity.nano.DnsEvent} kept in
87     // metrics. Note that increasing the size may cause statsd log buffer bust. Need to check the
88     // design in statsd when you try to increase the size.
89     public static final int DEFAULT_DNS_LOG_SIZE = 20;
90 
91     /**
92      * The time interval for polling tcp info to observe the tcp health.
93      */
94     public static String CONFIG_DATA_STALL_TCP_POLLING_INTERVAL = "data_stall_tcp_polling_interval";
95 
96     /**
97      * Default polling interval to observe the tcp health.
98      */
99     public static int DEFAULT_TCP_POLLING_INTERVAL_MS = 20_000;
100 
101     /**
102      * Default tcp packets fail rate to suspect as a data stall.
103      *
104      * Calculated by ((# of packets lost)+(# of packets retrans))/(# of packets sent)*100. Ideally,
105      * the percentage should be 100%. However, the ongoing packets may not be considered as neither
106      * lost or retrans yet. It will cause the percentage lower.
107      */
108     public static final int DEFAULT_TCP_PACKETS_FAIL_PERCENTAGE = 80;
109 
110     /**
111      * The percentage of tcp packets fail rate to be suspected as a data stall.
112      *
113      * Type: int
114      * Valid values: 0 to 100.
115      */
116     public static final String CONFIG_TCP_PACKETS_FAIL_PERCENTAGE = "tcp_packets_fail_percentage";
117 
118     /** Corresponds to enum from bionic/libc/include/netinet/tcp.h. */
119     public static final int TCP_ESTABLISHED = 1;
120     public static final int TCP_SYN_SENT = 2;
121     public static final int TCP_SYN_RECV = 3;
122     public static final int TCP_MONITOR_STATE_FILTER =
123             (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
124 
125     /**
126      * Threshold for the minimal tcp packets count to evaluate data stall via tcp info.
127      */
128     public static final int DEFAULT_DATA_STALL_MIN_PACKETS_THRESHOLD = 10;
129     public static final String CONFIG_MIN_PACKETS_THRESHOLD = "tcp_min_packets_threshold";
130 }
131