Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3869c0c4 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 7682599 from bc819a0e to tm-release

Change-Id: I334c52238d946232b24234b9bc7d875d78d7e395
parents 6799cfdb bc819a0e
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@
#include <sysutils/SocketClient.h>

#include "DnsResolver.h"
#include "Experiments.h"
#include "NetdPermissions.h"
#include "OperationLimiter.h"
#include "PrivateDnsConfiguration.h"
@@ -305,8 +306,8 @@ void initDnsEvent(NetworkDnsEventReported* event, const android_net_context& net

// Return 0 if the event should not be logged.
// Otherwise, return subsampling_denom
uint32_t getDnsEventSubsamplingRate(int netid, int returnCode) {
    uint32_t subsampling_denom = resolv_cache_get_subsampling_denom(netid, returnCode);
uint32_t getDnsEventSubsamplingRate(int netid, int returnCode, bool isMdns) {
    uint32_t subsampling_denom = resolv_cache_get_subsampling_denom(netid, returnCode, isMdns);
    if (subsampling_denom == 0) return 0;
    // Sample the event with a chance of 1 / denom.
    return (arc4random_uniform(subsampling_denom) == 0) ? subsampling_denom : 0;
@@ -333,7 +334,12 @@ void maybeLogQuery(int eventType, const android_net_context& netContext,
void reportDnsEvent(int eventType, const android_net_context& netContext, int latencyUs,
                    int returnCode, NetworkDnsEventReported& event, const std::string& query_name,
                    const std::vector<std::string>& ip_addrs = {}, int total_ip_addr_count = 0) {
    if (uint32_t rate = getDnsEventSubsamplingRate(netContext.dns_netid, returnCode)) {
    uint32_t rate = (query_name.ends_with(".local") &&
                     android::net::Experiments::getInstance()->getFlag("mdns_resolution", 1))
                            ? getDnsEventSubsamplingRate(netContext.dns_netid, returnCode, true)
                            : getDnsEventSubsamplingRate(netContext.dns_netid, returnCode, false);

    if (rate) {
        const std::string& dnsQueryStats = event.dns_query_events().SerializeAsString();
        stats::BytesField dnsQueryBytesField{dnsQueryStats.c_str(), dnsQueryStats.size()};
        event.set_return_code(static_cast<ReturnCode>(returnCode));
+3 −1
Original line number Diff line number Diff line
@@ -319,7 +319,9 @@ void ResolverController::dump(DumpWriter& dw, unsigned netId) {
            dw.println("No DNS servers defined");
        } else {
            dw.println("DnsEvent subsampling map: " +
                       android::base::Join(resolv_cache_dump_subsampling_map(netId), ' '));
                       android::base::Join(resolv_cache_dump_subsampling_map(netId, false), ' '));
            dw.println("DnsEvent subsampling map for MDNS: " +
                       android::base::Join(resolv_cache_dump_subsampling_map(netId, true), ' '));
            dw.println(
                    "DNS servers: # IP (total, successes, errors, timeouts, internal errors, "
                    "RTT avg, last sample)");
+23 −18
Original line number Diff line number Diff line
@@ -67,7 +67,6 @@

using aidl::android::net::IDnsResolver;
using aidl::android::net::ResolverOptionsParcel;
using android::base::StringAppendF;
using android::net::DnsQueryEvent;
using android::net::DnsStats;
using android::net::Experiments;
@@ -909,17 +908,19 @@ namespace {
// Sampling rate varies by return code; events to log are chosen randomly, with a
// probability proportional to the sampling rate.
constexpr const char DEFAULT_SUBSAMPLING_MAP[] = "default:8 0:400 2:110 7:110";
constexpr const char DEFAULT_MDNS_SUBSAMPLING_MAP[] = "default:1";

std::unordered_map<int, uint32_t> resolv_get_dns_event_subsampling_map() {
std::unordered_map<int, uint32_t> resolv_get_dns_event_subsampling_map(bool isMdns) {
    using android::base::ParseInt;
    using android::base::ParseUint;
    using android::base::Split;
    using server_configurable_flags::GetServerConfigurableFlag;
    std::unordered_map<int, uint32_t> sampling_rate_map{};
    std::vector<std::string> subsampling_vector =
            Split(GetServerConfigurableFlag("netd_native", "dns_event_subsample_map",
                                            DEFAULT_SUBSAMPLING_MAP),
                  " ");
    const char* flag = isMdns ? "mdns_event_subsample_map" : "dns_event_subsample_map";
    const char* defaultMap = isMdns ? DEFAULT_MDNS_SUBSAMPLING_MAP : DEFAULT_SUBSAMPLING_MAP;
    const std::vector<std::string> subsampling_vector =
            Split(GetServerConfigurableFlag("netd_native", flag, defaultMap), " ");

    for (const auto& pair : subsampling_vector) {
        std::vector<std::string> rate_denom = Split(pair, ":");
        int return_code;
@@ -1005,7 +1006,8 @@ struct Cache {
struct NetConfig {
    explicit NetConfig(unsigned netId) : netid(netId) {
        cache = std::make_unique<Cache>();
        dns_event_subsampling_map = resolv_get_dns_event_subsampling_map();
        dns_event_subsampling_map = resolv_get_dns_event_subsampling_map(false);
        mdns_event_subsampling_map = resolv_get_dns_event_subsampling_map(true);
    }
    int nameserverCount() { return nameserverSockAddrs.size(); }
    int setOptions(const ResolverOptionsParcel& resolverOptions) {
@@ -1036,6 +1038,7 @@ struct NetConfig {
    int wait_for_pending_req_timeout_count = 0;
    // Map format: ReturnCode:rate_denom
    std::unordered_map<int, uint32_t> dns_event_subsampling_map;
    std::unordered_map<int, uint32_t> mdns_event_subsampling_map;
    DnsStats dnsStats;

    // Customized hostname/address table will be stored in customizedTable.
@@ -1117,11 +1120,9 @@ void _resolv_cache_query_failed(unsigned netid, const void* query, int querylen,
}

static void cache_dump_mru_locked(Cache* cache) {
    std::string buf;

    StringAppendF(&buf, "MRU LIST (%2d): ", cache->num_entries);
    std::string buf = fmt::format("MRU LIST ({:2d}): ", cache->num_entries);
    for (Entry* e = cache->mru_list.mru_next; e != &cache->mru_list; e = e->mru_next) {
        StringAppendF(&buf, " %d", e->id);
        fmt::format_to(std::back_inserter(buf), " {}", e->id);
    }

    LOG(INFO) << __func__ << ": " << buf;
@@ -1793,17 +1794,20 @@ int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
    return info->revision_id;
}

std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid) {
std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid, bool is_mdns) {
    std::lock_guard guard(cache_mutex);
    NetConfig* netconfig = find_netconfig_locked(netid);
    if (netconfig == nullptr) return {};
    std::vector<std::string> result;
    for (const auto& pair : netconfig->dns_event_subsampling_map) {
    const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
                                             : netconfig->mdns_event_subsampling_map;
    result.reserve(subsampling_map.size());
    for (const auto& [return_code, rate_denom] : subsampling_map) {
        result.push_back(fmt::format("{}:{}",
                                     (pair.first == DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY)
                                     (return_code == DNSEVENT_SUBSAMPLING_MAP_DEFAULT_KEY)
                                             ? "default"
                                             : std::to_string(pair.first),
                                     pair.second));
                                             : std::to_string(return_code),
                                     rate_denom));
    }
    return result;
}
@@ -1812,11 +1816,12 @@ std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid) {
// a sampling factor derived from the netid and the return code.
//
// Returns the subsampling rate if the event should be sampled, or 0 if it should be discarded.
uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code) {
uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code, bool is_mdns) {
    std::lock_guard guard(cache_mutex);
    NetConfig* netconfig = find_netconfig_locked(netid);
    if (netconfig == nullptr) return 0;  // Don't log anything at all.
    const auto& subsampling_map = netconfig->dns_event_subsampling_map;
    const auto& subsampling_map = (!is_mdns) ? netconfig->dns_event_subsampling_map
                                             : netconfig->mdns_event_subsampling_map;
    auto search_returnCode = subsampling_map.find(return_code);
    uint32_t denom;
    if (search_returnCode != subsampling_map.end()) {
+41 −40
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@
#define RESOLV_ALLOW_VERBOSE_LOGGING 0
#endif

using android::base::StringAppendF;
using fmt::format_to;

struct res_sym {
    int number;            /* Identifying number, like T_MX */
@@ -142,30 +142,31 @@ static void do_section(ns_msg* handle, ns_sect section) {
    int buflen = 2048;
    ns_rr rr;
    std::string s;

    auto out = std::back_inserter(s);
    /*
     * Print answer records.
     */
    for (;;) {
        if (ns_parserr(handle, section, rrnum, &rr)) {
            if (errno != ENODEV) StringAppendF(&s, "ns_parserr: %s", strerror(errno));
            if (errno != ENODEV) format_to(out, "ns_parserr: {}", strerror(errno));

            LOG(VERBOSE) << s;
            return;
        }
        if (rrnum == 0) {
            int opcode = ns_msg_getflag(*handle, ns_f_opcode);
            StringAppendF(&s, ";; %s SECTION:\n", p_section(section, opcode));
            format_to(out, ";; {} SECTION:\n", p_section(section, opcode));
        }
        if (section == ns_s_qd)
            StringAppendF(&s, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr),
            format_to(out, ";;\t{}, type = {}, class = {}\n", ns_rr_name(rr),
                      p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
        else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
            size_t rdatalen;
            uint16_t optcode, optlen;

            rdatalen = ns_rr_rdlen(rr);
            StringAppendF(&s, "; EDNS: version: %" PRIu32 ", udp=%u, flags=%" PRIu32 "\n",
                          (rr.ttl >> 16) & 0xff, ns_rr_class(rr), rr.ttl & 0xffff);
            format_to(out, "; EDNS: version: {}, udp={}, flags={}\n", (rr.ttl >> 16) & 0xff,
                      ns_rr_class(rr), rr.ttl & 0xffff);
            const uint8_t* cp = ns_rr_rdata(rr);
            while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) {
                int i;
@@ -174,33 +175,33 @@ static void do_section(ns_msg* handle, ns_sect section) {
                GETSHORT(optlen, cp);

                if (optcode == NS_OPT_NSID) {
                    StringAppendF(&s, "; NSID: ");
                    format_to(out, "; NSID: ");
                    if (optlen == 0) {
                        StringAppendF(&s, "; NSID\n");
                        format_to(out, "; NSID\n");
                    } else {
                        StringAppendF(&s, "; NSID: ");
                        format_to(out, "; NSID: ");
                        for (i = 0; i < optlen; i++) {
                            StringAppendF(&s, "%02x ", cp[i]);
                            format_to(out, "{:02x} ", cp[i]);
                        }
                        StringAppendF(&s, " (");
                        format_to(out, " (");
                        for (i = 0; i < optlen; i++) {
                            StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
                            format_to(out, "{} ", isprint(cp[i]) ? cp[i] : '.');
                        }
                        StringAppendF(&s, ")\n");
                        format_to(out, ")\n");
                    }
                } else {
                    if (optlen == 0) {
                        StringAppendF(&s, "; OPT=%u\n", optcode);
                        format_to(out, "; OPT={}\n", optcode);
                    } else {
                        StringAppendF(&s, "; OPT=%u: ", optcode);
                        format_to(out, "; OPT={}: ", optcode);
                        for (i = 0; i < optlen; i++) {
                            StringAppendF(&s, "%02x ", cp[i]);
                            format_to(out, "{:02x} ", cp[i]);
                        }
                        StringAppendF(&s, " (");
                        format_to(out, " (");
                        for (i = 0; i < optlen; i++) {
                            StringAppendF(&s, "%c", isprint(cp[i]) ? cp[i] : '.');
                            format_to(out, "{}", isprint(cp[i]) ? cp[i] : '.');
                        }
                        StringAppendF(&s, ")\n");
                        format_to(out, ")\n");
                    }
                }
                rdatalen -= 4 + optlen;
@@ -215,16 +216,16 @@ static void do_section(ns_msg* handle, ns_sect section) {
                        buflen += 1024;
                        continue;
                    } else {
                        StringAppendF(&s, "buflen over 131072");
                        format_to(out, "buflen over 131072");
                        PLOG(VERBOSE) << s;
                        return;
                    }
                }
                StringAppendF(&s, "ns_sprintrr failed");
                format_to(out, "ns_sprintrr failed");
                PLOG(VERBOSE) << s;
                return;
            }
            StringAppendF(&s, ";; %s\n", buf.get());
            format_to(out, ";; {}\n", buf.get());
        }
        rrnum++;
    }
@@ -269,22 +270,22 @@ void res_pquery(std::span<const uint8_t> msg) {
    /*
     * Print header fields.
     */
    std::string s;
    StringAppendF(&s, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
                  p_rcode((int)rcode), id);
    StringAppendF(&s, ";; flags:");
    if (ns_msg_getflag(handle, ns_f_qr)) StringAppendF(&s, " qr");
    if (ns_msg_getflag(handle, ns_f_aa)) StringAppendF(&s, " aa");
    if (ns_msg_getflag(handle, ns_f_tc)) StringAppendF(&s, " tc");
    if (ns_msg_getflag(handle, ns_f_rd)) StringAppendF(&s, " rd");
    if (ns_msg_getflag(handle, ns_f_ra)) StringAppendF(&s, " ra");
    if (ns_msg_getflag(handle, ns_f_z)) StringAppendF(&s, " ??");
    if (ns_msg_getflag(handle, ns_f_ad)) StringAppendF(&s, " ad");
    if (ns_msg_getflag(handle, ns_f_cd)) StringAppendF(&s, " cd");
    StringAppendF(&s, "; %s: %d", p_section(ns_s_qd, (int)opcode), qdcount);
    StringAppendF(&s, ", %s: %d", p_section(ns_s_an, (int)opcode), ancount);
    StringAppendF(&s, ", %s: %d", p_section(ns_s_ns, (int)opcode), nscount);
    StringAppendF(&s, ", %s: %d", p_section(ns_s_ar, (int)opcode), arcount);
    std::string s = fmt::format(";; ->>HEADER<<- opcode: {}, status: {}, id: {}\n",
                                _res_opcodes[opcode], p_rcode((int)rcode), id);
    auto out = std::back_inserter(s);
    format_to(out, ";; flags:");
    if (ns_msg_getflag(handle, ns_f_qr)) format_to(out, " qr");
    if (ns_msg_getflag(handle, ns_f_aa)) format_to(out, " aa");
    if (ns_msg_getflag(handle, ns_f_tc)) format_to(out, " tc");
    if (ns_msg_getflag(handle, ns_f_rd)) format_to(out, " rd");
    if (ns_msg_getflag(handle, ns_f_ra)) format_to(out, " ra");
    if (ns_msg_getflag(handle, ns_f_z)) format_to(out, " ??");
    if (ns_msg_getflag(handle, ns_f_ad)) format_to(out, " ad");
    if (ns_msg_getflag(handle, ns_f_cd)) format_to(out, " cd");
    format_to(out, "; {}: {}", p_section(ns_s_qd, (int)opcode), qdcount);
    format_to(out, ", {}: {}", p_section(ns_s_an, (int)opcode), ancount);
    format_to(out, ", {}: {}", p_section(ns_s_ns, (int)opcode), nscount);
    format_to(out, ", {}: {}", p_section(ns_s_ar, (int)opcode), arcount);

    LOG(VERBOSE) << s;

+2 −2
Original line number Diff line number Diff line
@@ -50,8 +50,8 @@ void resolv_populate_res_for_net(ResState* statp);

std::vector<unsigned> resolv_list_caches();

std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid);
uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code);
std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid, bool is_mdns);
uint32_t resolv_cache_get_subsampling_denom(unsigned netid, int return_code, bool is_mdns);

typedef enum {
    RESOLV_CACHE_UNSUPPORTED, /* the cache can't handle that kind of queries */
Loading