Ashish Sharma | e0cec46 | 2012-02-28 17:43:58 -0800 | [diff] [blame] | 1 | <!-- |
| 2 | Copyright 2012 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 | |
| 17 | |
| 18 | In Android 4.0, statistics reported by Linux network interfaces are |
| 19 | recorded over time, and are used to enforce network quota limits, |
| 20 | render user-visible charts, and more. |
| 21 | |
| 22 | Each network device driver (Wi-Fi included) must follow the standard |
| 23 | kernel device lifecycle, and return correct statistics through |
| 24 | `dev_get_stats()`. In particular, statistics returned must remain |
| 25 | strictly monotonic while the interface is active. Drivers may reset |
| 26 | statistics only after successfully completing an `unregister_netdev()` |
| 27 | or the equivalent that generates a `NETDEV_UNREGISTER` event for |
| 28 | callbacks registered with `register_netdevice_notifier()` / |
| 29 | `register_inetaddr_notifier()` / `register_inet6addr_notifier()`. |
| 30 | |
| 31 | Mobile operators typically measure data usage at the Internet layer |
| 32 | (IP). To match this approach in Android 4.0, we rely on the fact that |
| 33 | for the kernel devices we care about the `rx_bytes` and `tx_bytes` |
| 34 | values returned by `dev_get_stats()` return exactly the Internet layer |
| 35 | (`IP`) bytes transferred. But we understand that for other devices it |
| 36 | might not be the case. For now, the feature relies on this |
| 37 | peculiarity. New drivers should have that property also, and the |
| 38 | `dev_get_stats()` values must not include any encapsulation overhead |
| 39 | of lower network layers (such as Ethernet headers), and should |
| 40 | preferably not include other traffic (such as ARP) unless it is |
| 41 | negligible. |
| 42 | |
| 43 | The Android framework only collects statistics from network interfaces |
| 44 | associated with a `NetworkStateTracker` in `ConnectivityService`. This |
| 45 | enables the framework to concretely identify each network interface, |
| 46 | including its type (such as `TYPE_MOBILE` or `TYPE_WIFI`) and |
| 47 | subscriber identity (such as IMSI). All network interfaces used to |
| 48 | route data should be represented by a `NetworkStateTracker` so that |
| 49 | statistics can be accounted correctly. |