环境:ATC是FaceBook开源的移动网络测试工具Augmented Traffic Control(ATC),能够方便的让我们模拟各种网络环境进行测试。
准备:
1:ubuntu16.04、usb无线网卡(一定要linux免驱,一定要用5G频段,2.4G频段的网卡被污染严重,开启后也基本无法使用),本地有线网络联网
root@k-virtual-machine:~# ifconfig
ens33 Link encap:以太网 硬件地址 00:0c:29:ec:ea:31
inet 地址:192.168.239.148 广播:192.168.239.255 掩码:255.255.255.0
inet6 地址: fe80::c014:ad7b:a8a9:1f7d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:116414 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:54197 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:132285621 (132.2 MB) 发送字节:13122298 (13.1 MB)
lo Link encap:本地环回
inet 地址:127.0.0.1 掩码:255.0.0.0
inet6 地址: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 跃点数:1
接收数据包:3878 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:3878 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:359220 (359.2 KB) 发送字节:359220 (359.2 KB)
wlx1cbfce3b6370 Link encap:以太网 硬件地址 1c:bf:ce:3b:63:70
inet 地址:192.168.0.1 广播:192.168.0.255 掩码:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:25219 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:29650 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:2884096 (2.8 MB) 发送字节:30362350 (30.3 MB)
2:查看usb是否识别网卡,无线网卡是否可用:
:~# lsusb
Bus 001 Device 002: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
3:查看是否支持ap,和managed模式(如果linux免驱,且支持软ap模式,应该都可以)
:~# iw list
Wiphy phy0
max # scan SSIDs: 4
max scan IEs length: 2257 bytes
Retry short long limit: 2
Coverage class: 0 (up to 0m)
Device supports RSN-IBSS.
Supported Ciphers:
* WEP40 (00-0f-ac:1)
* WEP104 (00-0f-ac:5)
* TKIP (00-0f-ac:2)
* CCMP (00-0f-ac:4)
* 00-0f-ac:10
* GCMP (00-0f-ac:8)
* 00-0f-ac:9
Available Antennas: TX 0 RX 0
Supported interface modes:
* IBSS
* managed
* AP
* AP/VLAN
* monitor
* mesh point
Band 1:
Capabilities: 0x17e
HT20/HT40
SM Power Save disabled
RX Greenfield
RX HT20 SGI
RX HT40 SGI
RX STBC 1-stream
Max AMSDU length: 3839 bytes
No DSSS/CCK HT40
Maximum RX AMPDU length 32767 bytes (exponent: 0x002)
Minimum RX AMPDU time spacing: 2 usec (0x04)
HT TX/RX MCS rate indexes supported: 0-7, 32
Bitrates (non-HT):
* 1.0 Mbps
* 2.0 Mbps (short preamble supported)
* 5.5 Mbps (short preamble supported)
* 11.0 Mbps (short preamble supported)
* 6.0 Mbps
* 9.0 Mbps
* 12.0 Mbps
* 18.0 Mbps
* 24.0 Mbps
* 36.0 Mbps
* 48.0 Mbps
* 54.0 Mbps
Frequencies:
* 2412 MHz [1] (20.0 dBm)
* 2417 MHz [2] (20.0 dBm)
* 2422 MHz [3] (20.0 dBm)
* 2427 MHz [4] (20.0 dBm)
* 2432 MHz [5] (20.0 dBm)
* 2437 MHz [6] (20.0 dBm)
* 2442 MHz [7] (20.0 dBm)
* 2447 MHz [8] (20.0 dBm)
* 2452 MHz [9] (20.0 dBm)
* 2457 MHz [10] (20.0 dBm)
* 2462 MHz [11] (20.0 dBm)
* 2467 MHz [12] (20.0 dBm) (no IR)
* 2472 MHz [13] (20.0 dBm) (no IR)
* 2484 MHz [14] (20.0 dBm) (no IR)
Supported commands:
* new_interface
* set_interface
* new_key
* start_ap
* new_station
* new_mpath
* set_mesh_config
* set_bss
* authenticate
* associate
* deauthenticate
* disassociate
* join_ibss
* join_mesh
* set_tx_bitrate_mask
* frame
* frame_wait_cancel
* set_wiphy_netns
* set_channel
* set_wds_peer
* probe_client
* set_noack_map
* register_beacons
* start_p2p_device
* set_mcast_rate
* connect
* disconnect
* Unknown command (104)
* Unknown command (121)
Supported TX frame types:
* IBSS: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* managed: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* AP: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* AP/VLAN: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* mesh point: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* P2P-client: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* P2P-GO: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
* P2P-device: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
Supported RX frame types:
* IBSS: 0x40 0xb0 0xc0 0xd0
* managed: 0x40 0xd0
* AP: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
* AP/VLAN: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
* mesh point: 0xb0 0xc0 0xd0
* P2P-client: 0x40 0xd0
* P2P-GO: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
* P2P-device: 0x40 0xd0
software interface modes (can always be added):
* AP/VLAN
* monitor
valid interface combinations:
* #{ AP, mesh point } <= 8,
total <= 8, #channels <= 1
HT Capability overrides:
* MCS: ff ff ff ff ff ff ff ff ff ff
* maximum A-MSDU length
* supported channel width
* short GI for 40 MHz
* max A-MPDU length exponent
* min MPDU start spacing
Device supports TX status socket option.
Device supports HT-IBSS.
Device supports SAE with AUTHENTICATE command
Device supports low priority scan.
Device supports scan flush.
Device supports AP scan.
Device supports per-vif TX power setting
Driver supports full state transitions for AP/GO clients
Driver supports a userspace MPM
3:安装需要的软件
apt-get install hostapd isc-dhcp-server git curl vim python-pip
4:查看hostapd是否安装成功(如果提示命令则为成功)
:~# hostapd
hostapd v2.4
User space daemon for IEEE 802.11 AP management,
IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi> and contributors
usage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] \
[-g <global ctrl_iface>] [-G <group>] \
<configuration file(s)>
options:
-h show this usage
-d show more debug messages (-dd for even more)
-B run daemon in the background
-e entropy file
-g global control interface path
-G group for control interfaces
-P PID file
-K include key data in debug messages
-f log output to debug file instead of stdout
-T = record to Linux tracing in addition to logging
(records all messages regardless of debug verbosity)
-t include timestamps in some debug messages
-v show hostapd version
5:查看dhcpd是否安装成功(没有报错,则表明成功)
:~# dhcpd
Internet Systems Consortium DHCP Server 4.3.3
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcpd.pid
Can't open /var/lib/dhcp/dhcpd.leases for append.
If you think you have received this message due to a bug rather
than a configuration issue please read the section on submitting
bugs on either our web page at www.isc.org or in the README file
before submitting a bug. These pages explain the proper
process and the information we find helpful for debugging..
exiting.
提示错误,找不到/var/lib/dhcp/dhcpd.leases文件,需要手工创建并设置权限
:~# ls -l /var/lib/dhcp/dhcpd.leases
-rw-r--r-- 1 dhcpd dhcpd 125 11月 3 10:01 /var/lib/dhcp/dhcpd.leases
:~# chmod 777 /var/lib/dhcp/dhcpd.leases
:~# dhcpd
Internet Systems Consortium DHCP Server 4.3.3
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcpd.pid
Wrote 0 leases to leases file.
No subnet declaration for wlx1cbfce3b6370 (no IPv4 addresses).
** Ignoring requests on wlx1cbfce3b6370. If this is not what
you want, please write a subnet declaration
in your dhcpd.conf file for the network segment
to which interface wlx1cbfce3b6370 is attached. **
No subnet declaration for ens33 (192.168.239.148).
** Ignoring requests on ens33. If this is not what
you want, please write a subnet declaration
in your dhcpd.conf file for the network segment
to which interface ens33 is attached. **
Not configured to listen on any interfaces!
If you think you have received this message due to a bug rather
than a configuration issue please read the section on submitting
bugs on either our web page at www.isc.org or in the README file
before submitting a bug. These pages explain the proper
process and the information we find helpful for debugging..
exiting.
此时确定安装成功,且没有报错
6:修改hostapd配置文件
:~# vim /etc/hostapd/hostapd.conf
添加如下内容
interface=wlx1cbfce3b6370 //无线网卡名称,可以通过ifconfig查看到
driver=nl80211
ssid=autotest //wifi名字
channel=10
hw_mode=g
macaddr_acl=0
auth_algs=3
wpa=2
wpa_passphrase=12345678 //密码
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=TKIP CCMP
7:启动hostapd
root@k-virtual-machine:~# nmcli radio wifi off
root@k-virtual-machine:~# rfkill unblock wlan
root@k-virtual-machine:~# killall hostapd
hostapd:没有发现操作
root@k-virtual-machine:~# hostapd -B /etc/hostapd/hostapd.conf
Configuration file: /etc/hostapd/hostapd.conf
Could not read interface wlan0 flags: No such device
nl80211: Driver does not support authentication/association or connect commands
nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Could not read interface wlan0 flags: No such device
nl80211 driver initialization failed.
wlan0: interface state UNINITIALIZED->DISABLED
wlan0: AP-DISABLED (表明启动成功)
查看dns
:~# nmcli dev show | grep IP4
IP4.地址[1]: 192.168.239.148/24
IP4.网关: 192.168.239.2
IP4.路由[1]: dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]: 192.168.239.2
IP4.域[1]: localdomain
IP4.WINS[1]: 192.168.239.2
IP4.地址[1]: 127.0.0.1/8
IP4.网关:
8:配置dhcpd
:~# vim /etc/dhcp/dhcpd.conf
添加到文件尾
subnet 192.168.0.0 netmask 255.255.255.0
{
range 192.168.0.2 192.168.0.10;
option routers 192.168.0.1;
option domain-name-servers 192.168.239.2;//填写上一步查询到的dns
}
9:配置无线网卡的出口ip
:~# ifconfig wlx1cbfce3b6370 192.168.0.1 netmask 255.255.255.0
10:启动dhcp
# mkdir /var/run/dhcp-server
# touch /var/run/dhcp-server/dhcpd.pid
# chmod 777 /var/run/dhcp-server/dhcpd.pid
# # dhcpd wlx1cbfce3b6370 -pf /var/run/dhcp-server/dhcpd.pid
Internet Systems Consortium DHCP Server 4.3.3
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcp-server/dhcpd.pid
Wrote 0 leases to leases file.
Listening on LPF/wlx1cbfce3b6370/1c:bf:ce:3b:63:70/192.168.0.0/24
Sending on LPF/wlx1cbfce3b6370/1c:bf:ce:3b:63:70/192.168.0.0/24
Sending on Socket/fallback/fallback-net (启动成功)
11:修改ip转发
# vim /etc/sysctl.conf
取消这一行的注释:
net.ipv4.ip_forward= 1
然后执行使之立即生效
# sudo sysctl -p
# iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
-------------------------------------------------
# sudo iptables -F
# sudo iptables -X
# sudo iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
# sudo iptables -A FORWARD -i ens33 -o wlx1cbfce3b6370 -m state --state RELATED,ESTABLISHED -j ACCEPT
# sudo iptables -A FORWARD -i wlx1cbfce3b6370 -o ens33 -j ACCEPT
12:升级pip
:~# pip install --upgrade pip
13:下载atc源码
# git clone https://github.com/facebook/augmented-traffic-control.git
# sudo pip install atc_thrift atcd django-atc-api django-atc-demo-ui django-atc-profile-storage
但是这样安装django的版本不一定是1.10的,(python -m django --version,查看安装的版本)
# sudo pip uninstall django
# sudo pip install django==1.10
14:创建工程目录
:~# cd augmented-traffic-control/
:~# django-admin startproject atcui
:~# cd atcui
15:配置setting.py
cd augmented-traffic-control/atcui/atcui/
# vim settings.py
INSTALLED_APPS = (
...
# Django ATC API
'rest_framework',
'atc_api',
# Django ATC Demo UI
'bootstrap_themes',
'django_static_jquery',
'atc_demo_ui',
# Django ATC Profile Storage
'atc_profile_storage',
)
16:配置urls.py
cd augmented-traffic-control/atcui/atcui/
# vim urls.py
...
...
from django.views.generic.base import RedirectView
from django.conf.urls import include
urlpatterns = [
...
# Django ATC API
url(r'^api/v1/', include('atc_api.urls')),
# Django ATC Demo UI
url(r'^atc_demo_ui/', include('atc_demo_ui.urls')),
# Django ATC profile storage
url(r'^api/v1/profiles/', include('atc_profile_storage.urls')),
url(r'^$', RedirectView.as_view(url='/atc_demo_ui/', permanent=False)),
]
17:启动
# cd augmented-traffic-control/atcui/
# python manage.py migrate
Operations to perform:
Apply all migrations: admin, atc_profile_storage, auth, contenttypes, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying atc_profile_storage.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
18:开启atc
# sudo atcd --atcd-wan ens33 --atcd-lan wlx1cbfce3b6370 --atcd-dont-drop-packets
INFO:AtcdVService.AtcdNBServerTask:AtcdNBServerTask Server Started on 127.0.0.1:9090
INFO:AtcdVService.AtcdLinuxShaper:Calling initialize_shaping_system
INFO:AtcdVService.AtcdLinuxShaper:Running /sbin/iptables -t mangle -F FORWARD
INFO:AtcdVService.AtcdLinuxShaper:deleting root QDisc on wlx1cbfce3b6370
INFO:AtcdVService.AtcdLinuxShaper:setting root qdisc on wlx1cbfce3b6370
INFO:AtcdVService.AtcdLinuxShaper:deleting root QDisc on ens33
INFO:AtcdVService.AtcdLinuxShaper:setting root qdisc on ens33
INFO:AtcdVService.AtcdLinuxShaper:Restoring shaped connection from DB
INFO:AtcdVService.AtcdLinuxShaper:Request startShaping TrafficControl(device=TrafficControlledDevice(controllingIP='192.168.0.2', controlledIP='192.168.0.2'), timeout=82930.08934998512, settings=TrafficControlSetting(down=Shaping(loss=Loss(percentage=0.0, correlation=0.0), delay=Delay(delay=100, jitter=0, correlation=0.0), rate=780, iptables_options=[], corruption=Corruption(percentage=0.0, correlation=0.0), reorder=Reorder(percentage=0.0, correlation=0.0, gap=0)), up=Shaping(loss=Loss(percentage=0.0, correlation=0.0), delay=Delay(delay=100, jitter=0, correlation=0.0), rate=330, iptables_options=[], corruption=Corruption(percentage=0.0, correlation=0.0), reorder=Reorder(percentage=0.0, correlation=0.0, gap=0))))
INFO:AtcdVService.AtcdLinuxShaper:Shaping ip 192.168.0.2 on interface ens33
INFO:AtcdVService.AtcdLinuxShaper:create new HTB class on IFID ens33, classid 1:2,parent 1:0, rate 330kbits
...
19:启动manager
# cd augmented-traffic-control/atcui
# python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
November 03, 2020 - 05:02:54
Django version 1.10, using settings 'atcui.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[03/Nov/2020 05:04:38] "GET /atc_demo_ui/ HTTP/1.1" 200 2332
[03/Nov/2020 05:04:38] "GET /static/css/atc.css HTTP/1.1" 200 538
[03/Nov/2020 05:04:38] "GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1" 200 36966
[03/Nov/2020 05:04:38] "GET /static/static_jquery/js/jquery.min.js HTTP/1.1" 200 95786
...
20:创建默认配置
# cd augmented-traffic-control/utils
# ./restore-profiles.sh localhost:8000
Added profile /root/augmented-traffic-control/utils/profiles/2G-DevelopingRural.json
Added profile /root/augmented-traffic-control/utils/profiles/2G-DevelopingUrban.json
Added profile /root/augmented-traffic-control/utils/profiles/3G-Average.json
Added profile /root/augmented-traffic-control/utils/profiles/3G-Good.json
Added profile /root/augmented-traffic-control/utils/profiles/Cable.json
Added profile /root/augmented-traffic-control/utils/profiles/DSL.json
Added profile /root/augmented-traffic-control/utils/profiles/Edge-Average.json
Added profile /root/augmented-traffic-control/utils/profiles/Edge-Good.json
Added profile /root/augmented-traffic-control/utils/profiles/Edge-Lossy.json
Added profile /root/augmented-traffic-control/utils/profiles/NoConnectivity.json
21:启动成功,手机连接autotest wifi,可以设置丢包率等信息