Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* |
| 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 Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 17 | constexpr bool kVerboseLogging = false; |
| 18 | #define LOG_TAG "res_stats" |
| 19 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 20 | #include <arpa/nameser.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 21 | #include <stdbool.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 25 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 26 | #include "resolv_stats.h" |
| 27 | |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 28 | #define VLOG if (!kVerboseLogging) {} else LOG(INFO) |
| 29 | |
| 30 | #ifndef RESOLV_ALLOW_VERBOSE_LOGGING |
| 31 | static_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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | |
| 36 | /* Calculate the round-trip-time from start time t0 and end time t1. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 37 | int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 38 | // 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 45 | void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt) { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 46 | VLOG << __func__ << ": rcode = " << rcode << ", sec = " << rtt; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 47 | sample->at = now; |
| 48 | sample->rcode = rcode; |
| 49 | sample->rtt = rtt; |
| 50 | } |
| 51 | |
| 52 | /* Clears all stored samples for the given server. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 53 | void _res_stats_clear_samples(struct __res_stats* stats) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 58 | void 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 61 | 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 68 | for (int i = 0; i < stats->sample_count; ++i) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 69 | // 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 77 | 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | } |
| 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 127 | &rtt_avg, &last_sample_time); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 128 | if (successes >= 0 && errors >= 0 && timeouts >= 0) { |
| 129 | int total = successes + errors + timeouts; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 130 | 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | if (total >= params->min_samples && (errors > 0 || timeouts > 0)) { |
| 138 | int success_rate = successes * 100 / total; |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 139 | VLOG << "success rate " << success_rate; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 140 | if (success_rate < params->success_threshold) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 141 | time_t now = time(NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 142 | 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 Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 146 | VLOG << "samples stale, retrying server"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 147 | _res_stats_clear_samples(stats); |
| 148 | } else { |
Bernie Innocenti | e9ba09c | 2018-09-12 23:20:10 +0900 | [diff] [blame] | 149 | VLOG << "too many resolution errors, ignoring server"; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return 1; |
| 156 | } |
| 157 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 158 | void android_net_res_stats_get_usable_servers(const struct __res_params* params, |
| 159 | struct __res_stats stats[], int nscount, |
| 160 | bool usable_servers[]) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 161 | 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 | } |