Server-網絡加速Kcptun


KCPTun是一個使用UDP來實現網絡加速傳輸速度的開源軟件。

 

KCPTun網絡結構

 

 

Kcptun結構圖

 

TCP協議是可靠的數據流傳輸協議。簡單來說,如果數據傳輸過程中發生了丟包,TCP協議會重新發送相應數據包,如果數據包到達順序與發送順序不一致,TCP協議會進行數據包重組,即:TCP協議可以通過控制幀來保證數據流的傳輸順序和正確性。但TCP協議的控制機制比較復雜,在線路質量差導致丟包率極高時,傳輸效率就會指數級下降。

UDP協議是數據報協議,由於比TCP簡單得多,傳輸效率和延遲率都要優於TCP協議,但UDP協議不是可靠的傳輸協議,不能保證數據正確與可達,所以只能應用在一些對單個數據包的正確與可達不是要求很嚴格(比如:IM)、但對數據傳輸延遲率有很高要求(比如:視頻流或者多人在線游戲)的場合。

有沒有一種傳輸協議既可信又能保證傳輸效率呢?這就是R-UDP:可信UDP協議,一種在UDP協議基礎上增加了部分TCP的控制邏輯來保證數據正確完整的協議。目前已經有很多可用庫(rUDP、enet等等),雖然沒有成為像TCP/UDP這樣的標准協議,但已經廣泛運用到了大型多人在線游戲等領域。KCPTun就是利用這種機制實現UDP可靠傳輸(KCP over UDP),來大幅加速網絡傳輸效率的。

 

網絡測試

丟包率/網絡延時測試:

ping -n 100 www.baidu.com,windows –n/linux -c

 

網絡丟包率對傳輸速率的影響

 

總結:

在丟包率比較大的情況下加速才比較明顯,網絡丟包率很小或者不丟包時反而會降低傳輸效率。

 

Kcptun網絡加速

Kcptun

集成加速Shadowsocks

Windows&Linux64位版本

https://github.com/xtaci/kcptun/releases/download/v20180926/kcptun-linux-amd64-20180926.tar.gz

https://github.com/xtaci/kcptun/releases/download/v20180926/kcptun-windows-amd64-20180926.tar.gz

https://github.com/xtaci/kcptun/releases

解壓 tar -zxvf xxxx.tar.gz

WindowsClient客戶端命令

client_windows_amd64 -r "172.245.210.123:61250" -l ":61240" -mode fast2

LinuxServer服務端命令

/home/soft/server_linux_amd64 -t "127.0.0.1:61240" -l ":61250" -mode fast2

速度

fast3 > fast2 > fast > normal > default

 

參數調試

// FEC關閉

-datashard 0 -parityshard 0

// 調節發送/接受窗口

-sndwnd 2048 -rcvwnd 512

// 高有效載荷

-mtu 1300 -sndwnd 2048 -rcvwnd 512 -datashard 0 -parityshard 0 -mode normal -nodelay 1 -resend 0 -nc 1 -interval 500

參數issue:

        https://github.com/xtaci/kcptun/issues/251  參數組合分享

        https://github.com/xtaci/kcptun/issues/342  增加SNMP信息

        https://github.com/xtaci/kcptun/issues/329  有效載荷

參數解析

*      --nocomp    true/false  // 是否壓縮

 *      --localaddr value, -l value      // 本地地址

 *      --remoteaddr value, -r value     // kcp server address

 *      // 加密算法

 * --crypt value   // aes, aes-128, aes-192, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, xor, none (default: "aes")

 *          -crypt none // 不加密

 *      --key value     // 加密密碼?

 *      --mode value    // 模式 profiles: fast3, fast2, fast, normal (default: "fast")

 *      --conn value    // 設置服務器的UDP連接數(默認值:1)

 * --autoexpire value // 設置單個UDP連接的自動過期時間(以秒為單位),禁用0(默認值:0)

 *      --mtu value     // 設置UDP數據包的最大傳輸單元(默認值:1350)

 *      --sndwnd value  // 設置發送窗口大小(數據包數量)(默認值:128)

 *      --rcvwnd value  // 設置接收窗口大小(數據包數量)(默認值:512)

 *      --datashard value, --ds value    // 設置reed-solomon刪除編碼 - 數據庫 (default: 10)

 * --parityshard value, --ps value  // 設置reed-solomon擦除編碼 - 奇偶校驗分片(默認值:3)

 *          // fec占比parityshard/datashard parityshard=0關閉FEC

 *      --dscp value    // set DSCP(6bit) (default: 0)

 *          常用值:https://en.wikipedia.org/wiki/Differentiated_services#Commonly_used_DSCP_values

 *      --snmplog value     // 收集snmp文件,了解golang的時間格式,如:./snmp-20060102.log

 *      --snmpperiod value  // snmp collect period, in seconds (default: 60)

 *      --log value         // specify a log file to output, default goes to stderr

 *      -c value            // config from json file, which will override the command from shell

 *      --help, -h          // show help

 *      --version, -v       // print the version

 *      手動控制

 *      -mode manual -nodelay 1 -interval 20 -resend 2 -nc 1

 *          nodelay:是否啟用節點模式,未啟用0; 1啟用。

 *          interval :協議內部工作間隔,以毫秒為單位,例如10毫秒或20毫秒。

 *              interval 的作用是控制 flush 的時間間隔,主要控制 cpu 的耗用

 *              interval設的大些可以增加有效數據的載荷

 *          resend :快重傳模式,0表示默認關閉,2可以設置(2個ACK跨度將導致直接重傳)

 *          nc :是否關閉流量控制,0代表默認“不關閉”,1代表“關閉”。

 *      總結:

 *          場景:丟包率小/大延時網絡場景

 *          FEC/MTU/發送-接收窗口參數對帶寬影響大

 *          crypt/nocomp加密壓縮影響性能及安全性

 *          手動控制參數影響小

 *      REF:

 *      https://github.com/xtaci/kcptun

 *     https://github.com/skywind3000/kcp/blob/master/README.en.md#protocol-configuration

官方資源

https://github.com/xtaci/kcptun

https://github.com/skywind3000/kcp

 

 

 

 

Ref:

     使用KCPTun加速Shadowsocks代理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM