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

Commit b8d156e6 authored by Ken Chen's avatar Ken Chen
Browse files

Not report DNS failures caused by network restriction errors

Failures caused by network access restrictions account for a large
portion of metrics traffic (~27%). The change stops reporting this type
of errors to cloud.

Bug: 288340533
Test: Manually test
      1. Install a test app that keeps sending DNS in background.
      2. Force enable doze mode by adb commands.
      3. Check that DNS query results of the test app is failed.
      4. Use statsd_testdrive to confirm that query failures are not
         reported to statsd when network for the app is blocked.
Change-Id: I00d9d633e8cff2a75e9d2b6c55562a7cba5f39ad
parent b900105e
Loading
Loading
Loading
Loading
+20 −14
Original line number Diff line number Diff line
@@ -322,9 +322,11 @@ 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) {
    uint32_t rate =
            (query_name.ends_with(".local") && is_mdns_supported_network(netContext.dns_netid) &&
                    bool skipStats, const std::vector<std::string>& ip_addrs = {},
                    int total_ip_addr_count = 0) {
    int32_t rate =
            skipStats ? 0
            : (query_name.ends_with(".local") && is_mdns_supported_network(netContext.dns_netid) &&
               android::net::Experiments::getInstance()->getFlag("mdns_resolution", 1))
                    ? getDnsEventSubsamplingRate(netContext.dns_netid, returnCode, true)
                    : getDnsEventSubsamplingRate(netContext.dns_netid, returnCode, false);
@@ -904,7 +906,8 @@ void DnsProxyListener::GetAddrInfoHandler::run() {
    int32_t rv = 0;
    NetworkDnsEventReported event;
    initDnsEvent(&event, mNetContext);
    if (isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid)) {
    const bool isUidBlocked = isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid);
    if (isUidBlocked) {
        LOG(INFO) << "GetAddrInfoHandler::run: network access blocked";
        rv = EAI_FAIL;
    } else if (startQueryLimiter(uid)) {
@@ -952,7 +955,7 @@ void DnsProxyListener::GetAddrInfoHandler::run() {
    std::vector<std::string> ip_addrs;
    const int total_ip_addr_count = extractGetAddrInfoAnswers(result, &ip_addrs);
    reportDnsEvent(INetdEventListener::EVENT_GETADDRINFO, mNetContext, latencyUs, rv, event, mHost,
                   ip_addrs, total_ip_addr_count);
                   isUidBlocked, ip_addrs, total_ip_addr_count);
    freeaddrinfo(result);
}

@@ -1116,7 +1119,8 @@ void DnsProxyListener::ResNSendHandler::run() {
    int ansLen = -1;
    NetworkDnsEventReported event;
    initDnsEvent(&event, mNetContext);
    if (isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid)) {
    const bool isUidBlocked = isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid);
    if (isUidBlocked) {
        LOG(INFO) << "ResNSendHandler::run: network access blocked";
        ansLen = -ECONNREFUSED;
    } else if (startQueryLimiter(uid)) {
@@ -1147,7 +1151,7 @@ void DnsProxyListener::ResNSendHandler::run() {
        }
        if (rr_type == ns_t_a || rr_type == ns_t_aaaa) {
            reportDnsEvent(INetdEventListener::EVENT_RES_NSEND, mNetContext, latencyUs,
                           resNSendToAiError(ansLen, rcode), event, rr_name);
                           resNSendToAiError(ansLen, rcode), event, rr_name, isUidBlocked);
        }
        return;
    }
@@ -1177,8 +1181,8 @@ void DnsProxyListener::ResNSendHandler::run() {
        const int total_ip_addr_count =
                extractResNsendAnswers(std::span(ansBuf.data(), ansLen), rr_type, &ip_addrs);
        reportDnsEvent(INetdEventListener::EVENT_RES_NSEND, mNetContext, latencyUs,
                       resNSendToAiError(ansLen, rcode), event, rr_name, ip_addrs,
                       total_ip_addr_count);
                       resNSendToAiError(ansLen, rcode), event, rr_name, /*skipStats=*/false,
                       ip_addrs, total_ip_addr_count);
    }
}

@@ -1320,7 +1324,8 @@ void DnsProxyListener::GetHostByNameHandler::run() {
    int32_t rv = 0;
    NetworkDnsEventReported event;
    initDnsEvent(&event, mNetContext);
    if (isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid)) {
    const bool isUidBlocked = isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid);
    if (isUidBlocked) {
        LOG(INFO) << "GetHostByNameHandler::run: network access blocked";
        rv = EAI_FAIL;
    } else if (startQueryLimiter(uid)) {
@@ -1364,7 +1369,7 @@ void DnsProxyListener::GetHostByNameHandler::run() {
    std::vector<std::string> ip_addrs;
    const int total_ip_addr_count = extractGetHostByNameAnswers(hp, &ip_addrs);
    reportDnsEvent(INetdEventListener::EVENT_GETHOSTBYNAME, mNetContext, latencyUs, rv, event,
                   mName, ip_addrs, total_ip_addr_count);
                   mName, isUidBlocked, ip_addrs, total_ip_addr_count);
}

std::string DnsProxyListener::GetHostByNameHandler::threadName() {
@@ -1483,7 +1488,8 @@ void DnsProxyListener::GetHostByAddrHandler::run() {
    NetworkDnsEventReported event;
    initDnsEvent(&event, mNetContext);

    if (isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid)) {
    const bool isUidBlocked = isUidNetworkingBlocked(mNetContext.uid, mNetContext.dns_netid);
    if (isUidBlocked) {
        LOG(INFO) << "GetHostByAddrHandler::run: network access blocked";
        rv = EAI_FAIL;
    } else if (startQueryLimiter(uid)) {
@@ -1529,7 +1535,7 @@ void DnsProxyListener::GetHostByAddrHandler::run() {
    }

    reportDnsEvent(INetdEventListener::EVENT_GETHOSTBYADDR, mNetContext, latencyUs, rv, event,
                   (hp && hp->h_name) ? hp->h_name : "null", {}, 0);
                   (hp && hp->h_name) ? hp->h_name : "null", isUidBlocked, {}, 0);
}

std::string DnsProxyListener::GetHostByAddrHandler::threadName() {