blob: e2cfd17872775ba8923cde290025109f1ec5d79f [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/*
2 * Copyright (C) 2016 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
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090017constexpr bool kVerboseLogging = false;
18#define LOG_TAG "res_stats"
19
Bernie Innocenti55864192018-08-30 04:05:20 +090020#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090021#include <stdbool.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090022#include <string.h>
23
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090024#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090025
Bernie Innocenti55864192018-08-30 04:05:20 +090026#include "resolv_stats.h"
27
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090028#define VLOG if (!kVerboseLogging) {} else LOG(INFO)
29
30#ifndef RESOLV_ALLOW_VERBOSE_LOGGING
31static_assert(kVerboseLogging == false,
32 "Verbose logging floods logs at high-rate and exposes privacy-sensitive information. "
33 "Do not enable in release builds.");
34#endif
Bernie Innocenti55864192018-08-30 04:05:20 +090035
36/* Calculate the round-trip-time from start time t0 and end time t1. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090037int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) {
Bernie Innocenti55864192018-08-30 04:05:20 +090038 // Divide ns by one million to get ms, multiply s by thousand to get ms (obvious)
39 long ms0 = t0->tv_sec * 1000 + t0->tv_nsec / 1000000;
40 long ms1 = t1->tv_sec * 1000 + t1->tv_nsec / 1000000;
41 return (int) (ms1 - ms0);
42}
43
44/* Create a sample for calculating server reachability statistics. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090045void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090046 VLOG << __func__ << ": rcode = " << rcode << ", sec = " << rtt;
Bernie Innocenti55864192018-08-30 04:05:20 +090047 sample->at = now;
48 sample->rcode = rcode;
49 sample->rtt = rtt;
50}
51
52/* Clears all stored samples for the given server. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090053void _res_stats_clear_samples(struct __res_stats* stats) {
Bernie Innocenti55864192018-08-30 04:05:20 +090054 stats->sample_count = stats->sample_next = 0;
55}
56
57/* Aggregates the reachability statistics for the given server based on on the stored samples. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090058void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
59 int* timeouts, int* internal_errors, int* rtt_avg,
60 time_t* last_sample_time) {
Bernie Innocenti55864192018-08-30 04:05:20 +090061 int s = 0; // successes
62 int e = 0; // errors
63 int t = 0; // timouts
64 int ie = 0; // internal errors
65 long rtt_sum = 0;
66 time_t last = 0;
67 int rtt_count = 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068 for (int i = 0; i < stats->sample_count; ++i) {
Bernie Innocenti55864192018-08-30 04:05:20 +090069 // Treat everything as an error that the code in send_dg() already considers a
70 // rejection by the server, i.e. SERVFAIL, NOTIMP and REFUSED. Assume that NXDOMAIN
71 // and NOTAUTH can actually occur for user queries. NOERROR with empty answer section
72 // is not treated as an error here either. FORMERR seems to sometimes be returned by
73 // some versions of BIND in response to DNSSEC or EDNS0. Whether to treat such responses
74 // as an indication of a broken server is unclear, though. For now treat such responses,
75 // as well as unknown codes as errors.
76 switch (stats->samples[i].rcode) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077 case NOERROR:
78 case NOTAUTH:
79 case NXDOMAIN:
80 ++s;
81 rtt_sum += stats->samples[i].rtt;
82 ++rtt_count;
83 break;
84 case RCODE_TIMEOUT:
85 ++t;
86 break;
87 case RCODE_INTERNAL_ERROR:
88 ++ie;
89 break;
90 case SERVFAIL:
91 case NOTIMP:
92 case REFUSED:
93 default:
94 ++e;
95 break;
Bernie Innocenti55864192018-08-30 04:05:20 +090096 }
97 }
98 *successes = s;
99 *errors = e;
100 *timeouts = t;
101 *internal_errors = ie;
102 /* If there was at least one successful sample, calculate average RTT. */
103 if (rtt_count) {
104 *rtt_avg = rtt_sum / rtt_count;
105 } else {
106 *rtt_avg = -1;
107 }
108 /* If we had at least one sample, populate last sample time. */
109 if (stats->sample_count > 0) {
110 if (stats->sample_next > 0) {
111 last = stats->samples[stats->sample_next - 1].at;
112 } else {
113 last = stats->samples[stats->sample_count - 1].at;
114 }
115 }
116 *last_sample_time = last;
117}
118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900120 int successes = -1;
121 int errors = -1;
122 int timeouts = -1;
123 int internal_errors = -1;
124 int rtt_avg = -1;
125 time_t last_sample_time = 0;
126 android_net_res_stats_aggregate(stats, &successes, &errors, &timeouts, &internal_errors,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127 &rtt_avg, &last_sample_time);
Bernie Innocenti55864192018-08-30 04:05:20 +0900128 if (successes >= 0 && errors >= 0 && timeouts >= 0) {
129 int total = successes + errors + timeouts;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900130 VLOG << "NS stats: S " << successes
131 << " + E " << errors
132 << " + T " << timeouts
133 << " + I " << internal_errors
134 << " = " << total
135 << ", rtt = " << rtt_avg
136 << ", min_samples = " << params->min_samples;
Bernie Innocenti55864192018-08-30 04:05:20 +0900137 if (total >= params->min_samples && (errors > 0 || timeouts > 0)) {
138 int success_rate = successes * 100 / total;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900139 VLOG << "success rate " << success_rate;
Bernie Innocenti55864192018-08-30 04:05:20 +0900140 if (success_rate < params->success_threshold) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900141 time_t now = time(NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900142 if (now - last_sample_time > params->sample_validity) {
143 // Note: It might be worth considering to expire old servers after their expiry
144 // date has been reached, however the code for returning the ring buffer to its
145 // previous non-circular state would induce additional complexity.
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900146 VLOG << "samples stale, retrying server";
Bernie Innocenti55864192018-08-30 04:05:20 +0900147 _res_stats_clear_samples(stats);
148 } else {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900149 VLOG << "too many resolution errors, ignoring server";
Bernie Innocenti55864192018-08-30 04:05:20 +0900150 return 0;
151 }
152 }
153 }
154 }
155 return 1;
156}
157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158void android_net_res_stats_get_usable_servers(const struct __res_params* params,
159 struct __res_stats stats[], int nscount,
160 bool usable_servers[]) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900161 unsigned usable_servers_found = 0;
162 for (int ns = 0; ns < nscount; ns++) {
163 bool usable = _res_stats_usable_server(params, &stats[ns]);
164 if (usable) {
165 ++usable_servers_found;
166 }
167 usable_servers[ns] = usable;
168 }
169 // If there are no usable servers, consider all of them usable.
170 // TODO: Explore other possibilities, such as enabling only the best N servers, etc.
171 if (usable_servers_found == 0) {
172 for (int ns = 0; ns < nscount; ns++) {
173 usable_servers[ns] = true;
174 }
175 }
176}