溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Mac系統(tǒng)怎么配置弱網(wǎng)環(huán)境測試程序

發(fā)布時(shí)間:2020-06-03 13:38:59 來源:億速云 閱讀:1044 作者:Leah 欄目:軟件技術(shù)

怎么配置弱網(wǎng)環(huán)境測試程序?針對這個(gè)問題,今天小編總結(jié)這篇有關(guān)配置弱網(wǎng)環(huán)境的文章,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。

做音視頻開發(fā)時(shí)經(jīng)常需要模擬弱網(wǎng)環(huán)境,觀察 app 在弱網(wǎng)下的表現(xiàn),比如,丟包、延遲、抖動、限制帶寬條件等,Mac 系統(tǒng)有一個(gè)弱網(wǎng)工具 APP,叫做 “Network Link Conditioner ” ,支持可視化地完成弱網(wǎng)的模擬和配置,很好用,非常推薦你使用這個(gè)工具來完成弱網(wǎng)的模擬。


Mac系統(tǒng)怎么配置弱網(wǎng)環(huán)境測試程序


但是如果你期望使用命令行或者使用腳本來實(shí)現(xiàn)弱網(wǎng)環(huán)境的配置,就不得不研究一下它背后的原理了,本文就是介紹 Network Link Conditioner 背后使用的系統(tǒng)相關(guān)的命令和服務(wù),教大家如何使用命令行完成弱網(wǎng)環(huán)境的配置

Mac OS X 10.10 以后,系統(tǒng)主要使用的是 `PF (Packet Filter, the BSD firewall)` 和 `dummynet(the BSD traffic shaper)` 這兩個(gè)服務(wù)來模擬各種弱網(wǎng)環(huán)境。

dummynet

DUMMYNET(4)              BSD Kernel Interfaces Manual              DUMMYNET(4)

NAME
     dummynet -- traffic shaper, bandwidth manager and delay emulator

DESCRIPTION
     dummynet is a system facility that permits the control of traffic going
     through the various network interfaces, by applying bandwidth and queue
     size limitations, implementing different scheduling and queue management
     policies, and emulating delays and losses.

     The user interface for dummynet is implemented by the dnctl program, so
     the reader is referred to the dnctl(8) manpage for a complete description
     of the capabilities of dummynet and on how to use it.

SEE ALSO
     dnctl(8), setsockopt(2), bridge(4), ip(4), sysctl(8)

HISTORY
     dummynet was initially implemented as a testing tool for TCP congestion
     control by Luigi Rizzo <luigi@iet.unipi.it>, as described on ACM Computer
     Communication Review, Jan.97 issue.  Later it has been then modified to
     work at the ip and bridging level, integrated with the IPFW packet fil-
     ter, and extended to support multiple queueing and scheduling policies.

簡單來說,`dummynet` 是一個(gè)流量/帶寬/延時(shí)的控制工具,用戶可以使用 `dnctl` 命令來配置和與之交互。

dnctl

在線文檔:http://www.manpagez.com/man/8/dnctl/

DNCTL(8)                  BSD System Manager's Manual                 DNCTL(8)

NAME
     dnctl -- Traffic shaper control program

SYNOPSIS
     dnctl [-anqs] {list | show}
     dnctl [-f | -q] flush
     dnctl [-q] {delete} [number ...]
     dnctl {pipe | queue} number config config-options
     dnctl [-s [field]] {pipe | queue} {delete | list | show} [number ...]
     dnctl [-nq] [-p preproc [preproc-flags]] pathname

DESCRIPTION
     The dnctl utility is the user interface for controlling the dummynet(4)
     traffic shaper.

     dummynet operates by first using a packet filter to classify packets and
     divide them into flows, using any match pattern that can be used in dnctl
     rules.  Depending on local policies, a flow can contain packets for a
     single TCP connection, or from/to a given host, or entire subnet, or a
     protocol type, etc.

簡單來說,`dnctl `是一個(gè)命令行工具,用于配制`dummynet`服務(wù)。

$ dnctl {pipe | queue} number config config-options

`dnctl`  提供了 2 種流量控制機(jī)制,一個(gè)是 pipe,一個(gè)是 queue,前者主要用于固定帶寬條件下的弱網(wǎng)模擬,后者則可以試驗(yàn)不同 pipe 使如何搶占和共享可用帶寬的。通常我們選擇前者來簡單地模擬弱網(wǎng)。

`config-options` 的種類特別多,弱網(wǎng)條件的配置基本上都在這里了:

The following parameters can be configured for a pipe:

bw bandwidth
    Bandwidth, measured in [K|M]{bit/s|Byte/s}.
    A value of 0 (default) means unlimited bandwidth.

delay ms-delay
    Propagation delay, measured in milliseconds.

plr packet-loss-rate
    a floating-point number between 0 and 1, with 0 meaning no loss, 1 meaning 100% loss.

queue {slots | sizeKbytes}
    Queue size, in slots or KBytes.  Default value is 50 slots, which
    is the typical queue size for Ethernet devices.

綜上所述,我們來定義一個(gè) pipe number id 為 1,帶寬限制 100Kbit/s,delay 100ms,loss:50% 的弱網(wǎng)環(huán)境:

// 創(chuàng)建配置并顯示
$ sudo dnctl pipe 1 config bw 100Kbit/s delay 100 plr 0.5
$ sudo dnctl show

00001: 100.000 Kbit/s 100 ms 50 sl.plr 0.500000 0 queues (1 buckets) droptail
    mask: 0x00 0x00000000/0x0000 -> 0x00000000/0x0000

// 清空配置并顯示
$ sudo dnctl -q flush
$ sudo dnctl show

pf


我們再來看看另一個(gè)工具:`pf`,它是 Mac 系統(tǒng)的防火墻工具,我們利用它來把經(jīng)過系統(tǒng)的流量轉(zhuǎn)到我們的弱網(wǎng)環(huán)境進(jìn)行 filter 處理。


`pf` 主要使用配置文件保存防火墻規(guī)則,語法規(guī)范上比較嚴(yán)謹(jǐn),cat /etc/pf.conf,可看到以下已有內(nèi)容:

# This file contains the main ruleset, which gets automatically loaded
# at startup.  PF will not be automatically enabled, however.  Instead,
# each component which utilizes PF is responsible for enabling and disabling
# PF via -E and -X as documented in pfctl(8).  That will ensure that PF
# is disabled only when the last enable reference is released.
#
# Care must be taken to ensure that the main ruleset does not get flushed,
# as the nested anchors rely on the anchor point defined here. In addition,
# to the anchors loaded by this file, some system services would dynamically
# insert anchors into the main ruleset. These anchors will be added only when
# the system service is used and would removed on termination of the service.
#
# See pf.conf(5) for syntax.
#
# com.apple anchor point
#
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"

下面,我們需要來撰寫屬于我們自己的的規(guī)則。

1.  新建一個(gè) pf.conf 文件

$ touch pf.conf

2.  添加路由規(guī)則

規(guī)則文檔:https://www.openbsd.org/faq/pf/filter.html

action [direction] [log] [quick] [on interface] [af] [proto protocol]
       [from src_addr [port src_port]] [to dst_addr [port dst_port]]
       [flags tcp_flags] [state]

詳細(xì)參數(shù)含義可以參考文檔,這里簡單列出幾個(gè)關(guān)鍵的配置:

[direction]: 流量的方向,上行:out,下行:in
[proto protocol]:協(xié)議,tcp/udp/icmp 等
[from src_addr [port src_port]]:源 ip 和 port,默認(rèn)可以使用 any
[to dst_addr [port dst_port]]:目標(biāo) ip 和 port,默認(rèn)可以使用 any

這里我們主要示例一下如何添加規(guī)則到我們上面創(chuàng)建的 dummynet  pipe 1

$ vi pf.conf

# 本示例的 “上行 + 下行” 都配置了弱網(wǎng),也可以配置單向測測變化

# 測試 tcp,比如:curl www.baidu.com
dummynet in proto tcp from any to any pipe 1 
dummynet out proto tcp from any to any pipe 1

# 測試 udp,比如:音視頻通話
dummynet in proto udp from any to any pipe 1   
dummynet out proto udp from any to any pipe 1

# 測試 ping,比如:ping baidu.com
dummynet in proto icmp from any to any pipe 1
dummynet out proto icmp from any to any pipe 1

3.  啟動和加載 `PF` 配置


操作 `PF` 服務(wù),需要借助  `pfctl ` 命令。

# Mac 系統(tǒng)默認(rèn)把 `PF` 服務(wù)關(guān)閉了,啟動 `PF` 服務(wù)
$ sudo pfctl -e

# 加載自定義防火墻規(guī)則
$ sudo pfctl -f pf.conf

# 恢復(fù)原始的防火墻規(guī)則
$ sudo pfctl -f /etc/pf.conf 

# 注意:使用 pfctl 命令會經(jīng)常出現(xiàn)下面的 warning,沒有影響,忽略就好了。
"No ALTQ support in kernel"

關(guān)于Mac配置弱網(wǎng)環(huán)境就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI