1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <stdbool.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <arpa/nameser.h>
19*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
20*8d67ca89SAndroid Build Coastguard Worker
21*8d67ca89SAndroid Build Coastguard Worker #include <async_safe/log.h>
22*8d67ca89SAndroid Build Coastguard Worker
23*8d67ca89SAndroid Build Coastguard Worker #include "isc/eventlib.h"
24*8d67ca89SAndroid Build Coastguard Worker #include "resolv_stats.h"
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker #define DBG 0
27*8d67ca89SAndroid Build Coastguard Worker
28*8d67ca89SAndroid Build Coastguard Worker /* Calculate the round-trip-time from start time t0 and end time t1. */
29*8d67ca89SAndroid Build Coastguard Worker int
_res_stats_calculate_rtt(const struct timespec * t1,const struct timespec * t0)30*8d67ca89SAndroid Build Coastguard Worker _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) {
31*8d67ca89SAndroid Build Coastguard Worker // Divide ns by one million to get ms, multiply s by thousand to get ms (obvious)
32*8d67ca89SAndroid Build Coastguard Worker long ms0 = t0->tv_sec * 1000 + t0->tv_nsec / 1000000;
33*8d67ca89SAndroid Build Coastguard Worker long ms1 = t1->tv_sec * 1000 + t1->tv_nsec / 1000000;
34*8d67ca89SAndroid Build Coastguard Worker return (int) (ms1 - ms0);
35*8d67ca89SAndroid Build Coastguard Worker }
36*8d67ca89SAndroid Build Coastguard Worker
37*8d67ca89SAndroid Build Coastguard Worker /* Create a sample for calculating server reachability statistics. */
38*8d67ca89SAndroid Build Coastguard Worker void
_res_stats_set_sample(struct __res_sample * sample,time_t now,int rcode,int rtt)39*8d67ca89SAndroid Build Coastguard Worker _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt)
40*8d67ca89SAndroid Build Coastguard Worker {
41*8d67ca89SAndroid Build Coastguard Worker if (DBG) {
42*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log(ANDROID_LOG_INFO, "libc", "rcode = %d, sec = %d", rcode, rtt);
43*8d67ca89SAndroid Build Coastguard Worker }
44*8d67ca89SAndroid Build Coastguard Worker sample->at = now;
45*8d67ca89SAndroid Build Coastguard Worker sample->rcode = rcode;
46*8d67ca89SAndroid Build Coastguard Worker sample->rtt = rtt;
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker /* Clears all stored samples for the given server. */
50*8d67ca89SAndroid Build Coastguard Worker void
_res_stats_clear_samples(struct __res_stats * stats)51*8d67ca89SAndroid Build Coastguard Worker _res_stats_clear_samples(struct __res_stats* stats)
52*8d67ca89SAndroid Build Coastguard Worker {
53*8d67ca89SAndroid Build Coastguard Worker stats->sample_count = stats->sample_next = 0;
54*8d67ca89SAndroid Build Coastguard Worker }
55*8d67ca89SAndroid Build Coastguard Worker
56*8d67ca89SAndroid Build Coastguard Worker /* Aggregates the reachability statistics for the given server based on on the stored samples. */
57*8d67ca89SAndroid Build Coastguard Worker void
android_net_res_stats_aggregate(struct __res_stats * stats,int * successes,int * errors,int * timeouts,int * internal_errors,int * rtt_avg,time_t * last_sample_time)58*8d67ca89SAndroid Build Coastguard Worker android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
59*8d67ca89SAndroid Build Coastguard Worker int* timeouts, int* internal_errors, int* rtt_avg, time_t* last_sample_time)
60*8d67ca89SAndroid Build Coastguard Worker {
61*8d67ca89SAndroid Build Coastguard Worker int s = 0; // successes
62*8d67ca89SAndroid Build Coastguard Worker int e = 0; // errors
63*8d67ca89SAndroid Build Coastguard Worker int t = 0; // timouts
64*8d67ca89SAndroid Build Coastguard Worker int ie = 0; // internal errors
65*8d67ca89SAndroid Build Coastguard Worker long rtt_sum = 0;
66*8d67ca89SAndroid Build Coastguard Worker time_t last = 0;
67*8d67ca89SAndroid Build Coastguard Worker int rtt_count = 0;
68*8d67ca89SAndroid Build Coastguard Worker for (int i = 0 ; i < stats->sample_count ; ++i) {
69*8d67ca89SAndroid Build Coastguard Worker // Treat everything as an error that the code in send_dg() already considers a
70*8d67ca89SAndroid Build Coastguard Worker // rejection by the server, i.e. SERVFAIL, NOTIMP and REFUSED. Assume that NXDOMAIN
71*8d67ca89SAndroid Build Coastguard Worker // and NOTAUTH can actually occur for user queries. NOERROR with empty answer section
72*8d67ca89SAndroid Build Coastguard Worker // is not treated as an error here either. FORMERR seems to sometimes be returned by
73*8d67ca89SAndroid Build Coastguard Worker // some versions of BIND in response to DNSSEC or EDNS0. Whether to treat such responses
74*8d67ca89SAndroid Build Coastguard Worker // as an indication of a broken server is unclear, though. For now treat such responses,
75*8d67ca89SAndroid Build Coastguard Worker // as well as unknown codes as errors.
76*8d67ca89SAndroid Build Coastguard Worker switch (stats->samples[i].rcode) {
77*8d67ca89SAndroid Build Coastguard Worker case NOERROR:
78*8d67ca89SAndroid Build Coastguard Worker case NOTAUTH:
79*8d67ca89SAndroid Build Coastguard Worker case NXDOMAIN:
80*8d67ca89SAndroid Build Coastguard Worker ++s;
81*8d67ca89SAndroid Build Coastguard Worker rtt_sum += stats->samples[i].rtt;
82*8d67ca89SAndroid Build Coastguard Worker ++rtt_count;
83*8d67ca89SAndroid Build Coastguard Worker break;
84*8d67ca89SAndroid Build Coastguard Worker case RCODE_TIMEOUT:
85*8d67ca89SAndroid Build Coastguard Worker ++t;
86*8d67ca89SAndroid Build Coastguard Worker break;
87*8d67ca89SAndroid Build Coastguard Worker case RCODE_INTERNAL_ERROR:
88*8d67ca89SAndroid Build Coastguard Worker ++ie;
89*8d67ca89SAndroid Build Coastguard Worker break;
90*8d67ca89SAndroid Build Coastguard Worker case SERVFAIL:
91*8d67ca89SAndroid Build Coastguard Worker case NOTIMP:
92*8d67ca89SAndroid Build Coastguard Worker case REFUSED:
93*8d67ca89SAndroid Build Coastguard Worker default:
94*8d67ca89SAndroid Build Coastguard Worker ++e;
95*8d67ca89SAndroid Build Coastguard Worker break;
96*8d67ca89SAndroid Build Coastguard Worker }
97*8d67ca89SAndroid Build Coastguard Worker }
98*8d67ca89SAndroid Build Coastguard Worker *successes = s;
99*8d67ca89SAndroid Build Coastguard Worker *errors = e;
100*8d67ca89SAndroid Build Coastguard Worker *timeouts = t;
101*8d67ca89SAndroid Build Coastguard Worker *internal_errors = ie;
102*8d67ca89SAndroid Build Coastguard Worker /* If there was at least one successful sample, calculate average RTT. */
103*8d67ca89SAndroid Build Coastguard Worker if (rtt_count) {
104*8d67ca89SAndroid Build Coastguard Worker *rtt_avg = rtt_sum / rtt_count;
105*8d67ca89SAndroid Build Coastguard Worker } else {
106*8d67ca89SAndroid Build Coastguard Worker *rtt_avg = -1;
107*8d67ca89SAndroid Build Coastguard Worker }
108*8d67ca89SAndroid Build Coastguard Worker /* If we had at least one sample, populate last sample time. */
109*8d67ca89SAndroid Build Coastguard Worker if (stats->sample_count > 0) {
110*8d67ca89SAndroid Build Coastguard Worker if (stats->sample_next > 0) {
111*8d67ca89SAndroid Build Coastguard Worker last = stats->samples[stats->sample_next - 1].at;
112*8d67ca89SAndroid Build Coastguard Worker } else {
113*8d67ca89SAndroid Build Coastguard Worker last = stats->samples[stats->sample_count - 1].at;
114*8d67ca89SAndroid Build Coastguard Worker }
115*8d67ca89SAndroid Build Coastguard Worker }
116*8d67ca89SAndroid Build Coastguard Worker *last_sample_time = last;
117*8d67ca89SAndroid Build Coastguard Worker }
118*8d67ca89SAndroid Build Coastguard Worker
119*8d67ca89SAndroid Build Coastguard Worker bool
_res_stats_usable_server(const struct __res_params * params,struct __res_stats * stats)120*8d67ca89SAndroid Build Coastguard Worker _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
121*8d67ca89SAndroid Build Coastguard Worker int successes = -1;
122*8d67ca89SAndroid Build Coastguard Worker int errors = -1;
123*8d67ca89SAndroid Build Coastguard Worker int timeouts = -1;
124*8d67ca89SAndroid Build Coastguard Worker int internal_errors = -1;
125*8d67ca89SAndroid Build Coastguard Worker int rtt_avg = -1;
126*8d67ca89SAndroid Build Coastguard Worker time_t last_sample_time = 0;
127*8d67ca89SAndroid Build Coastguard Worker android_net_res_stats_aggregate(stats, &successes, &errors, &timeouts, &internal_errors,
128*8d67ca89SAndroid Build Coastguard Worker &rtt_avg, &last_sample_time);
129*8d67ca89SAndroid Build Coastguard Worker if (successes >= 0 && errors >= 0 && timeouts >= 0) {
130*8d67ca89SAndroid Build Coastguard Worker int total = successes + errors + timeouts;
131*8d67ca89SAndroid Build Coastguard Worker if (DBG) {
132*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "NS stats: S %d + E %d + T %d + I %d "
133*8d67ca89SAndroid Build Coastguard Worker "= %d, rtt = %d, min_samples = %d\n", successes, errors, timeouts, internal_errors,
134*8d67ca89SAndroid Build Coastguard Worker total, rtt_avg, params->min_samples);
135*8d67ca89SAndroid Build Coastguard Worker }
136*8d67ca89SAndroid Build Coastguard Worker if (total >= params->min_samples && (errors > 0 || timeouts > 0)) {
137*8d67ca89SAndroid Build Coastguard Worker int success_rate = successes * 100 / total;
138*8d67ca89SAndroid Build Coastguard Worker if (DBG) {
139*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "success rate %d%%\n",
140*8d67ca89SAndroid Build Coastguard Worker success_rate);
141*8d67ca89SAndroid Build Coastguard Worker }
142*8d67ca89SAndroid Build Coastguard Worker if (success_rate < params->success_threshold) {
143*8d67ca89SAndroid Build Coastguard Worker // evNowTime() is used here instead of time() to stay consistent with the rest of
144*8d67ca89SAndroid Build Coastguard Worker // the code base
145*8d67ca89SAndroid Build Coastguard Worker time_t now = evNowTime().tv_sec;
146*8d67ca89SAndroid Build Coastguard Worker if (now - last_sample_time > params->sample_validity) {
147*8d67ca89SAndroid Build Coastguard Worker // Note: It might be worth considering to expire old servers after their expiry
148*8d67ca89SAndroid Build Coastguard Worker // date has been reached, however the code for returning the ring buffer to its
149*8d67ca89SAndroid Build Coastguard Worker // previous non-circular state would induce additional complexity.
150*8d67ca89SAndroid Build Coastguard Worker if (DBG) {
151*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log(ANDROID_LOG_INFO, "libc",
152*8d67ca89SAndroid Build Coastguard Worker "samples stale, retrying server\n");
153*8d67ca89SAndroid Build Coastguard Worker }
154*8d67ca89SAndroid Build Coastguard Worker _res_stats_clear_samples(stats);
155*8d67ca89SAndroid Build Coastguard Worker } else {
156*8d67ca89SAndroid Build Coastguard Worker if (DBG) {
157*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log(ANDROID_LOG_INFO, "libc",
158*8d67ca89SAndroid Build Coastguard Worker "too many resolution errors, ignoring server\n");
159*8d67ca89SAndroid Build Coastguard Worker }
160*8d67ca89SAndroid Build Coastguard Worker return 0;
161*8d67ca89SAndroid Build Coastguard Worker }
162*8d67ca89SAndroid Build Coastguard Worker }
163*8d67ca89SAndroid Build Coastguard Worker }
164*8d67ca89SAndroid Build Coastguard Worker }
165*8d67ca89SAndroid Build Coastguard Worker return 1;
166*8d67ca89SAndroid Build Coastguard Worker }
167*8d67ca89SAndroid Build Coastguard Worker
168*8d67ca89SAndroid Build Coastguard Worker void
android_net_res_stats_get_usable_servers(const struct __res_params * params,struct __res_stats stats[],int nscount,bool usable_servers[])169*8d67ca89SAndroid Build Coastguard Worker android_net_res_stats_get_usable_servers(const struct __res_params* params,
170*8d67ca89SAndroid Build Coastguard Worker struct __res_stats stats[], int nscount, bool usable_servers[]) {
171*8d67ca89SAndroid Build Coastguard Worker unsigned usable_servers_found = 0;
172*8d67ca89SAndroid Build Coastguard Worker for (int ns = 0; ns < nscount; ns++) {
173*8d67ca89SAndroid Build Coastguard Worker bool usable = _res_stats_usable_server(params, &stats[ns]);
174*8d67ca89SAndroid Build Coastguard Worker if (usable) {
175*8d67ca89SAndroid Build Coastguard Worker ++usable_servers_found;
176*8d67ca89SAndroid Build Coastguard Worker }
177*8d67ca89SAndroid Build Coastguard Worker usable_servers[ns] = usable;
178*8d67ca89SAndroid Build Coastguard Worker }
179*8d67ca89SAndroid Build Coastguard Worker // If there are no usable servers, consider all of them usable.
180*8d67ca89SAndroid Build Coastguard Worker // TODO: Explore other possibilities, such as enabling only the best N servers, etc.
181*8d67ca89SAndroid Build Coastguard Worker if (usable_servers_found == 0) {
182*8d67ca89SAndroid Build Coastguard Worker for (int ns = 0; ns < nscount; ns++) {
183*8d67ca89SAndroid Build Coastguard Worker usable_servers[ns] = true;
184*8d67ca89SAndroid Build Coastguard Worker }
185*8d67ca89SAndroid Build Coastguard Worker }
186*8d67ca89SAndroid Build Coastguard Worker }
187