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_" },
35         flag = true,
36         value = {
37             DATA_STALL_EVALUATION_TYPE_NONE,
38             DATA_STALL_EVALUATION_TYPE_DNS,
39             DATA_STALL_EVALUATION_TYPE_TCP, })
40     @Retention(RetentionPolicy.SOURCE)
41     public @interface EvaluationType {
42     }
43 
44     // Default configuration values for data stall detection.
45     public static final int DEFAULT_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD = 5;
46     public static final int DEFAULT_DATA_STALL_MIN_EVALUATE_TIME_MS = 60 * 1000;
47     public static final int DEFAULT_DATA_STALL_VALID_DNS_TIME_THRESHOLD_MS = 30 * 60 * 1000;
48     /**
49      * The threshold value for the number of consecutive dns timeout events received to be a
50      * signal of data stall. The number of consecutive timeouts needs to be {@code >=} this
51      * threshold to be considered a data stall. Set the value to {@code <= 0} to disable. Note
52      * that the value should be {@code > 0} if the DNS data stall detection is enabled.
53      *
54      */
55     public static final String CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD =
56             "data_stall_consecutive_dns_timeout_threshold";
57 
58     /**
59      * The minimal time interval in milliseconds for data stall reevaluation.
60      *
61      */
62     public static final String CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL =
63             "data_stall_min_evaluate_interval";
64 
65     /**
66      * DNS timeouts older than this timeout (in milliseconds) are not considered for detecting
67      * a data stall.
68      *
69      */
70     public static final String CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD =
71             "data_stall_valid_dns_time_threshold";
72 
73     /**
74      * Which data stall detection signal to use. This is a bitmask constructed by bitwise-or-ing
75      * (i.e. {@code |}) the DATA_STALL_EVALUATION_TYPE_* values.
76      *
77      * Type: int
78      * Valid values:
79      *   {@link #DATA_STALL_EVALUATION_TYPE_DNS} : Use dns as a signal.
80      *   {@link #DATA_STALL_EVALUATION_TYPE_TCP} : Use tcp info as a signal.
81      */
82     public static final String CONFIG_DATA_STALL_EVALUATION_TYPE = "data_stall_evaluation_type";
83     public static final int DEFAULT_DATA_STALL_EVALUATION_TYPES =
84             DATA_STALL_EVALUATION_TYPE_DNS | DATA_STALL_EVALUATION_TYPE_TCP;
85     // The default number of DNS events kept of the log kept for dns signal evaluation. Each event
86     // is represented by a {@link com.android.server.connectivity.NetworkMonitor#DnsResult} objects.
87     // It's also the size of array of {@link com.android.server.connectivity.nano.DnsEvent} kept in
88     // metrics. Note that increasing the size may cause statsd log buffer bust. Need to check the
89     // design in statsd when you try to increase the size.
90     public static final int DEFAULT_DNS_LOG_SIZE = 20;
91 
92     /**
93      * The time interval for polling tcp info to observe the tcp health.
94      */
95     public static String CONFIG_DATA_STALL_TCP_POLLING_INTERVAL = "data_stall_tcp_polling_interval";
96 
97     /**
98      * Default polling interval to observe the tcp health.
99      */
100     public static int DEFAULT_TCP_POLLING_INTERVAL_MS = 20_000;
101 
102     /**
103      * Default tcp packets fail rate to suspect as a data stall.
104      *
105      * Calculated by ((# of packets lost)+(# of packets retrans))/(# of packets sent)*100. Ideally,
106      * the percentage should be 100%. However, the ongoing packets may not be considered as neither
107      * lost or retrans yet. It will cause the percentage lower.
108      */
109     public static final int DEFAULT_TCP_PACKETS_FAIL_PERCENTAGE = 80;
110 
111     /**
112      * The percentage of tcp packets fail rate to be suspected as a data stall.
113      *
114      * Type: int
115      * Valid values: 0 to 100.
116      */
117     public static final String CONFIG_TCP_PACKETS_FAIL_PERCENTAGE = "tcp_packets_fail_percentage";
118 
119     /** Corresponds to enum from bionic/libc/include/netinet/tcp.h. */
120     public static final int TCP_ESTABLISHED = 1;
121     public static final int TCP_SYN_SENT = 2;
122     public static final int TCP_SYN_RECV = 3;
123     public static final int TCP_MONITOR_STATE_FILTER =
124             (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV);
125 
126     /**
127      * Threshold for the minimal tcp packets count to evaluate data stall via tcp info.
128      */
129     public static final int DEFAULT_DATA_STALL_MIN_PACKETS_THRESHOLD = 10;
130     public static final String CONFIG_MIN_PACKETS_THRESHOLD = "tcp_min_packets_threshold";
131 }
132