Skip to content

Commit 5698338

Browse files
committed
同步主线,增加TCP选项!
2 parents 21667b5 + bab23e0 commit 5698338

21 files changed

Lines changed: 867 additions & 270 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ _testmain.go
2424
*.prof
2525
client/client
2626
server/server
27+
build/*
28+
.DS_Store

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: go
22
go:
3-
- 1.9.x
43
- 1.10.x
54
- 1.11.x
5+
- 1.12.x
66
before_install:
77
- go get github.com/mattn/goveralls
88
- go get golang.org/x/tools/cmd/cover

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ FROM golang:alpine as builder
22
MAINTAINER xtaci <daniel820313@gmail.com>
33
RUN apk update && \
44
apk upgrade && \
5-
apk add git
5+
apk add git gcc libc-dev linux-headers
66
RUN go get -ldflags "-X main.VERSION=$(date -u +%Y%m%d) -s -w" github.com/xtaci/kcptun/client && go get -ldflags "-X main.VERSION=$(date -u +%Y%m%d) -s -w" github.com/xtaci/kcptun/server
77

8-
FROM alpine:3.6
8+
FROM alpine:3.9
99
COPY --from=builder /go/bin /bin
1010
EXPOSE 29900/udp
1111
EXPOSE 12948

README.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ All precompiled releases are genereated from `build-release.sh` script.
6969

7070
### Performance
7171

72-
<img src="fast.png" alt="fast.com" height="256px" />
72+
<img src="fast.png" alt="fast.com" height="256px" />
73+
74+
![bandwidth](bw.png)
75+
76+
![flame](flame.png)
77+
78+
> Practical bandwidth graph with parameters: -mode fast3 -ds 10 -ps 3
79+
80+
7381

7482
### Basic Tuning Guide
7583

@@ -92,7 +100,15 @@ All precompiled releases are genereated from `build-release.sh` script.
92100
93101
> *fast3 > fast2 > fast > normal > default*
94102
103+
#### HOLB
104+
105+
Since streams are multiplexed into a single physical channel, head of line blocking may appear under certain circumstances, by
106+
increasing `-smuxbuf` to a larger value (default 4MB) may mitigate this problem, obviously this will costs more memory.
95107

108+
#### Slow Devices
109+
110+
kcptun made use of **ReedSolomon-Codes** to recover lost packets, which requires massive amount of computation, a low-end ARM device cannot satisfy kcptun well. To unleash the full potential of kcptun, a multi-core x86 homeserver CPU like AMD Opteron is recommended.
111+
If you insist on running under some ARM routers, you'd better turn off `FEC` and use `salsa20` as the encryption method.
96112

97113
### Expert Tuning Guide
98114

@@ -103,15 +119,15 @@ All precompiled releases are genereated from `build-release.sh` script.
103119
#### Usage
104120

105121
```
106-
$ ./client_darwin_amd64 -h
122+
xtaci@gw:~$ ./client_linux_amd64 -h
107123
NAME:
108124
kcptun - client(with SMUX)
109125
110126
USAGE:
111-
client_darwin_amd64 [global options] command [command options] [arguments...]
127+
client_linux_amd64 [global options] command [command options] [arguments...]
112128
113129
VERSION:
114-
20180922
130+
20190409
115131
116132
COMMANDS:
117133
help, h Shows a list of commands or help for one command
@@ -132,25 +148,26 @@ GLOBAL OPTIONS:
132148
--parityshard value, --ps value set reed-solomon erasure coding - parityshard (default: 3)
133149
--dscp value set DSCP(6bit) (default: 0)
134150
--nocomp disable compression
135-
--sockbuf value (default: 4194304)
136-
--keepalive value (default: 10)
151+
--sockbuf value per-socket buffer in bytes (default: 4194304)
152+
--smuxbuf value the overall de-mux buffer in bytes (default: 4194304)
153+
--keepalive value seconds between heartbeats (default: 10)
137154
--snmplog value collect snmp to file, aware of timeformat in golang, like: ./snmp-20060102.log
138155
--snmpperiod value snmp collect period, in seconds (default: 60)
139156
--log value specify a log file to output, default goes to stderr
140157
--quiet to suppress the 'stream open/close' messages
141158
-c value config from json file, which will override the command from shell
142159
--help, -h show help
143160
--version, -v print the version
144-
145-
$ ./server_darwin_amd64 -h
161+
162+
xtaci@gw:~$ ./server_linux_amd64 -h
146163
NAME:
147164
kcptun - server(with SMUX)
148165
149166
USAGE:
150-
server_darwin_amd64 [global options] command [command options] [arguments...]
167+
server_linux_amd64 [global options] command [command options] [arguments...]
151168
152169
VERSION:
153-
20180922
170+
20190409
154171
155172
COMMANDS:
156173
help, h Shows a list of commands or help for one command
@@ -168,8 +185,9 @@ GLOBAL OPTIONS:
168185
--parityshard value, --ps value set reed-solomon erasure coding - parityshard (default: 3)
169186
--dscp value set DSCP(6bit) (default: 0)
170187
--nocomp disable compression
171-
--sockbuf value (default: 4194304)
172-
--keepalive value (default: 10)
188+
--sockbuf value per-socket buffer in bytes (default: 4194304)
189+
--smuxbuf value the overall de-mux buffer in bytes (default: 4194304)
190+
--keepalive value seconds between heartbeats (default: 10)
173191
--snmplog value collect snmp to file, aware of timeformat in golang, like: ./snmp-20060102.log
174192
--snmpperiod value snmp collect period, in seconds (default: 60)
175193
--pprof start profiling server on :6060
@@ -249,11 +267,17 @@ aes-128-cfb 847216.79k 850770.86k 853712.05k 859912.39k 854565.80k
249267
The encrytion performance in kcptun is as fast as in openssl library(if not faster).
250268

251269

252-
#### Memory Usage Control
270+
#### Memory Control
253271

254272
Routers, mobile devices are susceptible to memory consumption; by setting GOGC environment(eg: GOGC=20) will make the garbage collector to recycle faster.
255273
Reference: https://blog.golang.org/go15gc
256274

275+
Primary memory allocation are done from a global buffer pool *xmit.Buf*, in kcp-go, when we need to allocate some bytes, we can get from that pool, and a *fixed-capacity* 1500 bytes(mtuLimit) will be returned, the *rx queue*, *tx queue* and *fec queue* all receive bytes from there, and they will return the bytes to the pool after using to prevent *unnecessary zer0ing* of bytes.
276+
The pool mechanism maintained a *high watermark* for slice objects, these *in-flight* objects from the pool will survive from the perodical garbage collection, meanwhile the pool kept the ability to return the memory to runtime if in idle, `-sndwnd`,`-rcvwnd`,`-ds`, `-ps`, these parameters affect this *high watermark*, the larger the value, the bigger the memory consumption will be.
277+
278+
`-smuxbuf` also affects the maximum memory consumption, this parameter maintains a subtle balance between *concurrency* and *resource*, you can increase this value(default 4MB) to boost concurrency if you have many clients to serve and you get a powerful server at the same time, and also you can decrease this value to serve only 1 or 2 clients and hope this program can run under some embeded SoC system with limited memory and only you can access. (Notice that the `-smuxbuf` value is not proprotional to concurrency, you need to test.)
279+
280+
257281
#### Compression
258282

259283
kcptun has builtin snappy algorithms for compressing streams:
@@ -274,29 +298,30 @@ Compression is enabled by default, you can disable it by setting ```-nocomp``` o
274298
#### SNMP
275299

276300
```go
277-
// Snmp defines network statistics indicator
278301
type Snmp struct {
279-
BytesSent uint64 // raw bytes sent
280-
BytesReceived uint64
281-
MaxConn uint64
282-
ActiveOpens uint64
283-
PassiveOpens uint64
284-
CurrEstab uint64 // count of connections for now
285-
InErrs uint64 // udp read errors
302+
BytesSent uint64 // bytes sent from upper level
303+
BytesReceived uint64 // bytes received to upper level
304+
MaxConn uint64 // max number of connections ever reached
305+
ActiveOpens uint64 // accumulated active open connections
306+
PassiveOpens uint64 // accumulated passive open connections
307+
CurrEstab uint64 // current number of established connections
308+
InErrs uint64 // UDP read errors reported from net.PacketConn
286309
InCsumErrors uint64 // checksum errors from CRC32
287-
KCPInErrors uint64 // packet iput errors from kcp
288-
InSegs uint64
289-
OutSegs uint64
290-
InBytes uint64 // udp bytes received
291-
OutBytes uint64 // udp bytes sent
292-
RetransSegs uint64
293-
FastRetransSegs uint64
294-
EarlyRetransSegs uint64
310+
KCPInErrors uint64 // packet iput errors reported from KCP
311+
InPkts uint64 // incoming packets count
312+
OutPkts uint64 // outgoing packets count
313+
InSegs uint64 // incoming KCP segments
314+
OutSegs uint64 // outgoing KCP segments
315+
InBytes uint64 // UDP bytes received
316+
OutBytes uint64 // UDP bytes sent
317+
RetransSegs uint64 // accmulated retransmited segments
318+
FastRetransSegs uint64 // accmulated fast retransmitted segments
319+
EarlyRetransSegs uint64 // accmulated early retransmitted segments
295320
LostSegs uint64 // number of segs infered as lost
296321
RepeatSegs uint64 // number of segs duplicated
297322
FECRecovered uint64 // correct packets recovered from FEC
298323
FECErrs uint64 // incorrect packets recovered from FEC
299-
FECSegs uint64 // FEC segments received
324+
FECParityShards uint64 // FEC segments received
300325
FECShortShards uint64 // number of data shards that's not enough for recovery
301326
}
302327
```
@@ -339,6 +364,7 @@ The parameters below **MUST** be **IDENTICAL** on **BOTH** side:
339364
1. http://http2.github.io/ -- What is HTTP/2?
340365
1. http://www.lartc.org/ -- Linux Advanced Routing & Traffic Control
341366
1. https://en.wikipedia.org/wiki/Noisy-channel_coding_theorem -- Noisy channel coding theorem
367+
1. https://zhuanlan.zhihu.com/p/53849089 -- kcptun开发小记
342368

343369
### Donate
344370

build-release-cgo.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
3+
BUILD_DIR=$(dirname "$0")/build
4+
mkdir -p $BUILD_DIR
5+
cd $BUILD_DIR
6+
7+
sum="sha1sum"
8+
9+
if [ "$GO111MODULE" != "on" ]; then
10+
echo "GO111MODULE is off"
11+
else
12+
echo "GO111MODULE is on"
13+
fi
14+
15+
echo "Prerequisites for cross-compiling were written in build-release.sh"
16+
17+
# required library for cross-compiling
18+
# sudo apt-get install -y automake autogen build-essential ca-certificates gcc-5-arm-linux-gnueabi g++-5-arm-linux-gnueabi libc6-dev-armel-cross gcc-5-arm-linux-gnueabihf g++-5-arm-linux-gnueabihf libc6-dev-armhf-cross gcc-5-aarch64-linux-gnu g++-5-aarch64-linux-gnu libc6-dev-arm64-cross gcc-5-mips-linux-gnu g++-5-mips-linux-gnu libc6-dev-mips-cross gcc-5-mipsel-linux-gnu g++-5-mipsel-linux-gnu libc6-dev-mipsel-cross gcc-5-mips64-linux-gnuabi64 g++-5-mips64-linux-gnuabi64 libc6-dev-mips64-cross gcc-5-mips64el-linux-gnuabi64 g++-5-mips64el-linux-gnuabi64 libc6-dev-mips64el-cross gcc-5-multilib g++-5-multilib gcc-mingw-w64 g++-mingw-w64 clang llvm-dev libtool libxml2-dev uuid-dev libssl-dev swig openjdk-8-jdk pkg-config patch make xz-utils cpio wget zip unzip p7zip git mercurial bzr texinfo help2man --no-install-recommends
19+
20+
# if error message:
21+
# /usr/include/linux/errno.h:1:23: fatal error: asm/errno.h: No such file or directory
22+
# try:
23+
# ln -s /usr/include/asm-generic /usr/include/asm
24+
25+
if ! hash sha1sum 2>/dev/null; then
26+
if ! hash shasum 2>/dev/null; then
27+
echo "I can't see 'sha1sum' or 'shasum'"
28+
echo "Please install one of them!"
29+
exit
30+
fi
31+
sum="shasum"
32+
fi
33+
34+
UPX=false
35+
if hash upx 2>/dev/null; then
36+
UPX=true
37+
fi
38+
39+
VERSION=`date -u +%Y%m%d`
40+
LDFLAGS="-X main.VERSION=$VERSION -s -w"
41+
LDFLAGS_LINUX='-X main.VERSION='$VERSION' -s -w -linkmode "external" -extldflags "-static"'
42+
LDFLAGS_LINUX32='-X main.VERSION='$VERSION' -s -w -linkmode "external" -extldflags "-static -m32 -L/usr/lib32"'
43+
echo "-ldflag for linux/amd64:" $LDFLAGS_LINUX
44+
echo "-ldflag for linux/386:" $LDFLAGS_LINUX32
45+
echo "-ldflag for other:" $LDFLAGS
46+
47+
echo "=== Building ==="
48+
49+
# 386
50+
OSES=(linux windows)
51+
for os in ${OSES[@]}; do
52+
suffix=""
53+
if [ "$os" == "windows" ]
54+
then
55+
suffix=".exe"
56+
fi
57+
58+
if [ "$os" == "linux" ];then
59+
CC=gcc-5 CGO_ENABLED=1 GOOS=$os GOARCH=386 CGO_CFLAGS="-m32 -L/usr/lib32" CGO_CXXFLAGS="-m32 -L/usr/lib32" go build -ldflags "$LDFLAGS_LINUX32" -o client_${os}_386${suffix} github.com/flynaj/kcptun/client
60+
CC=gcc-5 CGO_ENABLED=1 GOOS=$os GOARCH=386 CGO_CFLAGS="-m32 -L/usr/lib32" CGO_CXXFLAGS="-m32 -L/usr/lib32" go build -ldflags "$LDFLAGS_LINUX32" -o server_${os}_386${suffix} github.com/flynaj/kcptun/server
61+
else
62+
CGO_ENABLED=0 GOOS=$os GOARCH=386 go build -ldflags "$LDFLAGS" -o client_${os}_386${suffix} github.com/flynaj/kcptun/client
63+
CGO_ENABLED=0 GOOS=$os GOARCH=386 go build -ldflags "$LDFLAGS" -o server_${os}_386${suffix} github.com/flynaj/kcptun/server
64+
fi
65+
66+
if $UPX; then upx -9 client_${os}_386${suffix} server_${os}_386${suffix};fi
67+
tar -zcf kcptun-${os}-386-$VERSION.tar.gz client_${os}_386${suffix} server_${os}_386${suffix}
68+
$sum kcptun-${os}-386-$VERSION.tar.gz
69+
done
70+
71+
# AMD64
72+
OSES=(linux darwin windows freebsd)
73+
for os in ${OSES[@]}; do
74+
suffix=""
75+
if [ "$os" == "windows" ]
76+
then
77+
suffix=".exe"
78+
fi
79+
80+
if [ "$os" == "linux" ];then
81+
CC=gcc-5 CGO_ENABLED=1 GOOS=$os GOARCH=amd64 go build -ldflags "$LDFLAGS_LINUX" -o client_${os}_amd64${suffix} github.com/flynaj/kcptun/client
82+
CC=gcc-5 CGO_ENABLED=1 GOOS=$os GOARCH=amd64 go build -ldflags "$LDFLAGS_LINUX" -o server_${os}_amd64${suffix} github.com/flynaj/kcptun/server
83+
else
84+
CGO_ENABLED=0 GOOS=$os GOARCH=amd64 go build -ldflags "$LDFLAGS" -o client_${os}_amd64${suffix} github.com/flynaj/kcptun/client
85+
CGO_ENABLED=0 GOOS=$os GOARCH=amd64 go build -ldflags "$LDFLAGS" -o server_${os}_amd64${suffix} github.com/flynaj/kcptun/server
86+
fi
87+
88+
if $UPX; then upx -9 client_${os}_amd64${suffix} server_${os}_amd64${suffix};fi
89+
tar -zcf kcptun-${os}-amd64-$VERSION.tar.gz client_${os}_amd64${suffix} server_${os}_amd64${suffix}
90+
$sum kcptun-${os}-amd64-$VERSION.tar.gz
91+
done
92+
93+
# ARM-5
94+
#CC=arm-linux-gnueabi-gcc-5 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go install std
95+
CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go build -ldflags "$LDFLAGS_LINUX" -o client_linux_arm5 github.com/flynaj/kcptun/client
96+
CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go build -ldflags "$LDFLAGS_LINUX" -o server_linux_arm5 github.com/flynaj/kcptun/server
97+
if $UPX; then upx -9 client_linux_arm5 server_linux_arm5;fi
98+
tar -zcf kcptun-linux-arm5-$VERSION.tar.gz client_linux_arm5 server_linux_arm5
99+
$sum kcptun-linux-arm5-$VERSION.tar.gz
100+
101+
# ARM-6
102+
#CC=arm-linux-gnueabi-gcc-5 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go install std
103+
CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go build -ldflags "$LDFLAGS_LINUX" -o client_linux_arm6 github.com/flynaj/kcptun/client
104+
CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go build -ldflags "$LDFLAGS_LINUX" -o server_linux_arm6 github.com/flynaj/kcptun/server
105+
if $UPX; then upx -9 client_linux_arm6 server_linux_arm6;fi
106+
tar -zcf kcptun-linux-arm6-$VERSION.tar.gz client_linux_arm6 server_linux_arm6
107+
$sum kcptun-linux-arm6-$VERSION.tar.gz
108+
109+
# ARM-7
110+
ARMS=(7)
111+
#CC=arm-linux-gnueabihf-gcc-5 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a" CGO_CXXFLAGS="-march=armv7-a" go install std
112+
CC=arm-linux-gnueabihf-gcc-5 CXX=arm-linux-gnueabihf-g++-5 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a -fPIC" CGO_CXXFLAGS="-march=armv7-a -fPIC" go build -ldflags "$LDFLAGS_LINUX" -o client_linux_arm7 github.com/flynaj/kcptun/client
113+
CC=arm-linux-gnueabihf-gcc-5 CXX=arm-linux-gnueabihf-g++-5 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a -fPIC" CGO_CXXFLAGS="-march=armv7-a -fPIC" go build -ldflags "$LDFLAGS_LINUX" -o server_linux_arm7 github.com/flynaj/kcptun/server
114+
if $UPX; then upx -9 client_linux_arm7 server_linux_arm7;fi
115+
tar -zcf kcptun-linux-arm7-$VERSION.tar.gz client_linux_arm7 server_linux_arm7
116+
$sum kcptun-linux-arm7-$VERSION.tar.gz
117+
118+
# ARM64
119+
CC=aarch64-linux-gnu-gcc-5 CXX=aarch64-linux-gnu-g++-5 GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -ldflags "$LDFLAGS_LINUX" -o client_linux_arm64 github.com/flynaj/kcptun/client
120+
CC=aarch64-linux-gnu-gcc-5 CXX=aarch64-linux-gnu-g++-5 GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -ldflags "$LDFLAGS_LINUX" -o server_linux_arm64 github.com/flynaj/kcptun/server
121+
if $UPX; then upx -9 client_linux_arm64 server_linux_arm64*;fi
122+
tar -zcf kcptun-linux-arm64-$VERSION.tar.gz client_linux_arm64 server_linux_arm64
123+
$sum kcptun-linux-arm64-$VERSION.tar.gz
124+
125+
#MIPS32LE
126+
CC=mipsel-linux-gnu-gcc-5 CXX=mipsel-linux-gnu-g++-5 GOOS=linux GOARCH=mipsle CGO_ENABLED=1 GOMIPS=softfloat go build -ldflags "$LDFLAGS_LINUX" -o client_linux_mipsle github.com/flynaj/kcptun/client
127+
CC=mipsel-linux-gnu-gcc-5 CXX=mipsel-linux-gnu-g++-5 GOOS=linux GOARCH=mipsle CGO_ENABLED=1 GOMIPS=softfloat go build -ldflags "$LDFLAGS_LINUX" -o server_linux_mipsle github.com/flynaj/kcptun/server
128+
129+
#MIPS32
130+
CC=mips-linux-gnu-gcc-5 CXX=mips-linux-gnu-g++-5 GOOS=linux GOARCH=mips CGO_ENABLED=1 GOMIPS=softfloat go build -ldflags "$LDFLAGS_LINUX" -o client_linux_mips github.com/flynaj/kcptun/client
131+
CC=mips-linux-gnu-gcc-5 CXX=mips-linux-gnu-g++-5 GOOS=linux GOARCH=mips CGO_ENABLED=1 GOMIPS=softfloat go build -ldflags "$LDFLAGS_LINUX" -o server_linux_mips github.com/flynaj/kcptun/server
132+
133+
if $UPX; then upx -9 client_linux_mips* server_linux_mips*;fi
134+
tar -zcf kcptun-linux-mipsle-$VERSION.tar.gz client_linux_mipsle server_linux_mipsle
135+
tar -zcf kcptun-linux-mips-$VERSION.tar.gz client_linux_mips server_linux_mips
136+
$sum kcptun-linux-mipsle-$VERSION.tar.gz
137+
$sum kcptun-linux-mips-$VERSION.tar.gz
138+
139+
echo "=== Building Completed ==="

0 commit comments

Comments
 (0)