在 Android Automotive OS (AAOS) 中模拟网络

本文将介绍如何以可扩容且易于维护的方式在 Android Automotive 硬件设备上模拟不同的网络条件。这种网络模拟不针对特定的环境,并使用可在 Android Automotive 硬件设备上运行的一些常用 Linux 工具。

下面几部分将介绍如何在 Android Automotive 硬件设备上设置和运行网络模拟。

内核要求

如需在被测设备 (DUT) 上启用网络模拟,必须在内核配置文件中配置 Linux ifbnetem 模块,如下所示:

# Network simulation config fragment start
CONFIG_NET_SCH_NETEM=y
CONFIG_IFB=y
CONFIG_NET_ACT_MIRRED=y
# Network simulation config fragment end

设置模拟

所有网络模拟或节流模拟都必须在被测设备 (DUT) 上进行。此模拟使用 Linux tcNetEm 实用程序,基于控制政策和规则来控制网络接口控制器 (NIC) 上的网络流量。

如需设置模拟,请执行以下操作:

  1. 将 DUT 和主机服务器连接到互联网。
  2. 创建 NetworkSimulation.sh 脚本(从 NetworkSimulation.sh 脚本部分提供的代码复制该脚本),并将其下载到主机服务器上。
  3. 将主机服务器连接到 DUT。运行 adb devices -l,确保 DUT 显示在已连接设备的列表中。

下图是设置架构的示意图:

nw-sim

图 1. 设置架构

NetworkSimulation.sh 脚本

NetworkSimulation.sh 脚本文件包含用于运行网络模拟的 adb 命令。将以下内容复制到名为 NetworkSimulation.sh 的文件中:

  #!/bin/bash

  latency=$1
  bandwidth=$2
  packetloss=$3

  # root device and set it to permissive mode
  adb root
  adb shell setenforce 0

  #Clear the current tc control
  adb shell tc qdisc del dev ifb0 root
  adb shell ip link set dev ifb0 down
  adb shell tc qdisc del dev wlan0 ingress
  adb shell tc qdisc del dev wlan0 root

  # Create a virtual device for ingress
  adb shell ip link set dev wlan0 up
  adb shell ip link set dev ifb0 up
  adb shell tc qdisc del dev wlan0 clsact
  adb shell tc qdisc add dev wlan0 handle ffff: ingress
  adb shell tc filter add dev wlan0 parent ffff: protocol all u32 match u32 0 0 action mirred egress redirect dev ifb0

  # Throttle upload bandwidth / latency / packet loss
  adb shell tc qdisc add dev wlan0 root handle 1: htb default 11
  adb shell tc class add dev wlan0 parent 1: classid 1:1 htb rate "$bandwidth"
  adb shell tc class add dev wlan0 parent 1:1 classid 1:11 htb rate "$bandwidth"
  adb shell tc qdisc add dev wlan0 parent 1:11 handle 10: netem delay "$latency" loss "$packetloss"

  # Throttle download bandwidth
  adb shell tc qdisc add dev ifb0 root handle 1: htb default 10
  adb shell tc class add dev ifb0 parent 1: classid 1:1 htb rate "$bandwidth"
  adb shell tc class add dev ifb0 parent 1:1 classid 1:10 htb rate "$bandwidth"

执行模拟

为了执行网络模拟,NetworkSimulation.sh 脚本文件中的 adb 命令会使用命令行参数来设置所需的值。

如需指定要模拟的延迟时间、带宽和丢包率,请使用以下命令行参数运行 NetworkSimulation.sh 脚本:

  • 延迟时间,以毫秒为单位进行指定。
  • 带宽,以千比特或兆比特为单位进行指定。
  • 丢包率,以百分比进行指定。

例如,如需设置 300 毫秒的延迟时间、100 千比特的带宽和 50% 的丢包率,请运行以下命令:

bash NetworkSimulation.sh 300ms 100kbit 50%

如需设置 100 毫秒的延迟时间、1 兆比特的带宽和 0% 的丢包率,请运行以下命令:

bash NetworkSimulation.sh 100ms 1mbit 0%

验证模拟

执行 NetworkSimulation.sh 脚本后,使用 Linux pingcurl 命令验证网络模拟是否配置正确并能按预期运行。ping 命令用于验证延迟时间,curl 命令用于验证带宽。

例如,对于使用 bash NetworkSimulation.sh 100ms 500kbit 10% 执行的模拟,运行 ping 命令应该会有以下输出:

BUILD:/ # ping -c 20 www.google.com
PING www.google.com (172.217.5.100) 56(84) bytes of data.
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=1 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=2 ttl=119 time=105 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=3 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=5 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=6 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=7 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=9 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=10 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=11 ttl=119 time=185 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=12 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=13 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=14 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=15 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=16 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=17 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=18 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=19 ttl=119 time=103 ms
64 bytes from sfo03s07-in-f4.1e100.net (172.217.5.100): icmp_seq=20 ttl=119 time=103 ms

--- www.google.com ping statistics ---
20 packets transmitted, 18 received, 10% packet loss, time 19040ms
rtt min/avg/max/mdev = 103.394/108.307/185.756/18.791 ms

此示例显示,ping 报告的丢包率为 10%,平均延迟时间接近 108 毫秒。由于模拟中指定的延迟时间为 100 毫秒,此结果符合预期。报告的延迟时间与指定的值存在少许差异是正常情况。

对于同一个示例,运行 curl 命令应该会有以下输出。

BUILD:/sdcard/DCIM # curl https://images-assets.nasa.gov/image/PIA15416/PIA15416~orig.jpg -o foo.jpg
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 6598k  100 6598k    0     0  49220      0  0:02:17  0:02:17 --:--:-- 47574

此示例显示,curl 报告的平均下载速度为 49,220 字节/秒。由于模拟中指定的带宽为 500 千比特,此结果符合预期。报告的带宽与指定的值存在少许差异是正常情况。