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

Commit 2a8c3f96 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Send normal termination metrics on wifi off

When turning wifi off, the interface gets torn down and empty
LinkProperties are received before wifi calls stop(). This causes a loss
of provisioning to be logged, instead of normal termination.

Watch interface link status up/down events, and when provisioning is
lost when the interface is down, consider it a normal termination.

Bug: 151796056
Test: manual: turn wifi off, observe events
Test: atest NetworkStackIntegrationTests (see also test-only change)
Change-Id: I9d086a199de0017aa425219d20882211423925e0
parent 342cb2c7
Loading
Loading
Loading
Loading
+13 −6
Original line number Original line Diff line number Diff line
@@ -389,6 +389,9 @@ public class IpClient extends StateMachine {
    private static final int CMD_COMPLETE_PRECONNECTION = 16;
    private static final int CMD_COMPLETE_PRECONNECTION = 16;
    private static final int CMD_UPDATE_L2INFORMATION = 17;
    private static final int CMD_UPDATE_L2INFORMATION = 17;


    private static final int ARG_LINKPROP_CHANGED_LINKSTATE_DOWN = 0;
    private static final int ARG_LINKPROP_CHANGED_LINKSTATE_UP = 1;

    // Internal commands to use instead of trying to call transitionTo() inside
    // Internal commands to use instead of trying to call transitionTo() inside
    // a given State's enter() method. Calling transitionTo() from enter/exit
    // a given State's enter() method. Calling transitionTo() from enter/exit
    // encounters a Log.wtf() that can cause trouble on eng builds.
    // encounters a Log.wtf() that can cause trouble on eng builds.
@@ -596,7 +599,9 @@ public class IpClient extends StateMachine {
        mLinkObserver = new IpClientLinkObserver(
        mLinkObserver = new IpClientLinkObserver(
                mContext, getHandler(),
                mContext, getHandler(),
                mInterfaceName,
                mInterfaceName,
                () -> sendMessage(EVENT_NETLINK_LINKPROPERTIES_CHANGED),
                (ifaceUp) -> sendMessage(EVENT_NETLINK_LINKPROPERTIES_CHANGED, ifaceUp
                        ? ARG_LINKPROP_CHANGED_LINKSTATE_UP
                        : ARG_LINKPROP_CHANGED_LINKSTATE_DOWN),
                config, mLog) {
                config, mLog) {
            @Override
            @Override
            public void onInterfaceAdded(String iface) {
            public void onInterfaceAdded(String iface) {
@@ -2055,12 +2060,14 @@ public class IpClient extends StateMachine {


                case EVENT_NETLINK_LINKPROPERTIES_CHANGED:
                case EVENT_NETLINK_LINKPROPERTIES_CHANGED:
                    // EVENT_NETLINK_LINKPROPERTIES_CHANGED message will be received in both of
                    // EVENT_NETLINK_LINKPROPERTIES_CHANGED message will be received in both of
                    // provisioning loss and normal user termination case (e.g. turn off wifi or
                    // provisioning loss and normal user termination cases (e.g. turn off wifi or
                    // switch to another wifi ssid), hence, checking current interface change
                    // switch to another wifi ssid), hence, checking the current interface link
                    // status (down or up) would help distinguish.
                    // state (down or up) helps distinguish the two cases: if the link state is
                    final boolean ifUp = (msg.arg1 != 0);
                    // down, provisioning is only lost because the link is being torn down (for
                    // example when turning off wifi), so treat it as a normal termination.
                    if (!handleLinkPropertiesUpdate(SEND_CALLBACKS)) {
                    if (!handleLinkPropertiesUpdate(SEND_CALLBACKS)) {
                        transitionToStoppingState(ifUp ? DisconnectCode.DC_PROVISIONING_FAIL
                        final boolean linkStateUp = (msg.arg1 == ARG_LINKPROP_CHANGED_LINKSTATE_UP);
                        transitionToStoppingState(linkStateUp ? DisconnectCode.DC_PROVISIONING_FAIL
                                : DisconnectCode.DC_NORMAL_TERMINATION);
                                : DisconnectCode.DC_NORMAL_TERMINATION);
                    }
                    }
                    break;
                    break;
+31 −8
Original line number Original line Diff line number Diff line
@@ -89,8 +89,13 @@ public class IpClientLinkObserver implements NetworkObserver {
    public interface Callback {
    public interface Callback {
        /**
        /**
         * Called when some properties of the link were updated.
         * Called when some properties of the link were updated.
         *
         * @param linkState Whether the interface link state is up as per the latest
         *                  {@link #onInterfaceLinkStateChanged(String, boolean)} callback. This
         *                  should only be used for metrics purposes, as it could be inconsistent
         *                  with {@link #getLinkProperties()} in particular.
         */
         */
        void update();
        void update(boolean linkState);
    }
    }


    /** Configuration parameters for IpClientLinkObserver. */
    /** Configuration parameters for IpClientLinkObserver. */
@@ -105,6 +110,7 @@ public class IpClientLinkObserver implements NetworkObserver {
    private final String mInterfaceName;
    private final String mInterfaceName;
    private final Callback mCallback;
    private final Callback mCallback;
    private final LinkProperties mLinkProperties;
    private final LinkProperties mLinkProperties;
    private boolean mInterfaceLinkState;
    private DnsServerRepository mDnsServerRepository;
    private DnsServerRepository mDnsServerRepository;
    private final AlarmManager mAlarmManager;
    private final AlarmManager mAlarmManager;
    private final Configuration mConfig;
    private final Configuration mConfig;
@@ -121,6 +127,7 @@ public class IpClientLinkObserver implements NetworkObserver {
        mLinkProperties = new LinkProperties();
        mLinkProperties = new LinkProperties();
        mLinkProperties.setInterfaceName(mInterfaceName);
        mLinkProperties.setInterfaceName(mInterfaceName);
        mConfig = config;
        mConfig = config;
        mInterfaceLinkState = true; // Assume up by default
        mDnsServerRepository = new DnsServerRepository(config.minRdnssLifetime);
        mDnsServerRepository = new DnsServerRepository(config.minRdnssLifetime);
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mNetlinkMonitor = new MyNetlinkMonitor(h, log, mTag);
        mNetlinkMonitor = new MyNetlinkMonitor(h, log, mTag);
@@ -149,7 +156,15 @@ public class IpClientLinkObserver implements NetworkObserver {
            // interface-specific messages (e.g., RTM_DELADDR) will not reach us, because the netd
            // interface-specific messages (e.g., RTM_DELADDR) will not reach us, because the netd
            // code that parses them will not be able to resolve the ifindex to an interface name.
            // code that parses them will not be able to resolve the ifindex to an interface name.
            clearLinkProperties();
            clearLinkProperties();
            mCallback.update();
            mCallback.update(getInterfaceLinkState());
        }
    }

    @Override
    public void onInterfaceLinkStateChanged(String iface, boolean state) {
        if (mInterfaceName.equals(iface)) {
            maybeLog("interfaceLinkStateChanged", iface + (state ? " up" : " down"));
            setInterfaceLinkState(state);
        }
        }
    }
    }


@@ -162,7 +177,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                changed = mLinkProperties.addLinkAddress(address);
                changed = mLinkProperties.addLinkAddress(address);
            }
            }
            if (changed) {
            if (changed) {
                mCallback.update();
                mCallback.update(getInterfaceLinkState());
            }
            }
        }
        }
    }
    }
@@ -176,7 +191,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                changed = mLinkProperties.removeLinkAddress(address);
                changed = mLinkProperties.removeLinkAddress(address);
            }
            }
            if (changed) {
            if (changed) {
                mCallback.update();
                mCallback.update(getInterfaceLinkState());
            }
            }
        }
        }
    }
    }
@@ -190,7 +205,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                changed = mLinkProperties.addRoute(route);
                changed = mLinkProperties.addRoute(route);
            }
            }
            if (changed) {
            if (changed) {
                mCallback.update();
                mCallback.update(getInterfaceLinkState());
            }
            }
        }
        }
    }
    }
@@ -204,7 +219,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                changed = mLinkProperties.removeRoute(route);
                changed = mLinkProperties.removeRoute(route);
            }
            }
            if (changed) {
            if (changed) {
                mCallback.update();
                mCallback.update(getInterfaceLinkState());
            }
            }
        }
        }
    }
    }
@@ -218,7 +233,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                synchronized (this) {
                synchronized (this) {
                    mDnsServerRepository.setDnsServersOn(mLinkProperties);
                    mDnsServerRepository.setDnsServersOn(mLinkProperties);
                }
                }
                mCallback.update();
                mCallback.update(getInterfaceLinkState());
            }
            }
        }
        }
    }
    }
@@ -243,6 +258,14 @@ public class IpClientLinkObserver implements NetworkObserver {
        mLinkProperties.setInterfaceName(mInterfaceName);
        mLinkProperties.setInterfaceName(mInterfaceName);
    }
    }


    private synchronized boolean getInterfaceLinkState() {
        return mInterfaceLinkState;
    }

    private synchronized void setInterfaceLinkState(boolean state) {
        mInterfaceLinkState = state;
    }

    /** Notifies this object of new interface parameters. */
    /** Notifies this object of new interface parameters. */
    public void setInterfaceParams(InterfaceParams params) {
    public void setInterfaceParams(InterfaceParams params) {
        mNetlinkMonitor.setIfindex(params.index);
        mNetlinkMonitor.setIfindex(params.index);
@@ -355,7 +378,7 @@ public class IpClientLinkObserver implements NetworkObserver {
                cancelPref64Alarm();
                cancelPref64Alarm();
            }
            }


            mCallback.update();
            mCallback.update(getInterfaceLinkState());
        }
        }


        private void processPref64Option(StructNdOptPref64 opt, final long now) {
        private void processPref64Option(StructNdOptPref64 opt, final long now) {