1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/kcp/sending.go

452 lines
9.5 KiB
Go
Raw Normal View History

2016-06-26 17:51:17 -04:00
package kcp
2016-07-03 16:14:38 -04:00
import (
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
)
2016-07-01 05:57:13 -04:00
type SendingWindow struct {
start uint32
cap uint32
len uint32
last uint32
data []*DataSegment
prev []uint32
next []uint32
2016-07-04 09:34:14 -04:00
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
2016-07-01 05:57:13 -04:00
}
2016-07-04 09:54:18 -04:00
func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
2016-07-01 05:57:13 -04:00
window := &SendingWindow{
2016-07-03 16:14:38 -04:00
start: 0,
cap: size,
len: 0,
last: 0,
data: make([]*DataSegment, size),
prev: make([]uint32, size),
next: make([]uint32, size),
writer: writer,
onPacketLoss: onPacketLoss,
2016-07-01 05:57:13 -04:00
}
return window
}
func (this *SendingWindow) Len() int {
return int(this.len)
}
2016-07-12 17:54:54 -04:00
func (this *SendingWindow) IsEmpty() bool {
return this.len == 0
}
2016-07-04 09:54:18 -04:00
func (this *SendingWindow) Size() uint32 {
return this.cap
}
func (this *SendingWindow) IsFull() bool {
return this.len == this.cap
}
2016-07-01 05:57:13 -04:00
func (this *SendingWindow) Push(seg *DataSegment) {
pos := (this.start + this.len) % this.cap
this.data[pos] = seg
if this.len > 0 {
this.next[this.last] = pos
this.prev[pos] = this.last
}
this.last = pos
this.len++
}
func (this *SendingWindow) First() *DataSegment {
return this.data[this.start]
}
func (this *SendingWindow) Clear(una uint32) {
2016-07-12 17:54:54 -04:00
for !this.IsEmpty() && this.data[this.start].Number < una {
2016-07-01 06:12:32 -04:00
this.Remove(0)
2016-07-01 05:57:13 -04:00
}
}
func (this *SendingWindow) Remove(idx uint32) {
2016-07-01 17:27:57 -04:00
if this.len == 0 {
return
}
2016-07-01 05:57:13 -04:00
pos := (this.start + idx) % this.cap
seg := this.data[pos]
2016-07-01 06:12:32 -04:00
if seg == nil {
return
}
2016-07-04 09:34:14 -04:00
this.totalInFlightSize--
2016-07-01 05:57:13 -04:00
seg.Release()
this.data[pos] = nil
2016-07-01 16:54:59 -04:00
if pos == this.start && pos == this.last {
this.len = 0
this.start = 0
this.last = 0
} else if pos == this.start {
delta := this.next[pos] - this.start
if this.next[pos] < this.start {
delta = this.next[pos] + this.cap - this.start
2016-07-01 05:57:13 -04:00
}
2016-07-01 16:54:59 -04:00
this.start = this.next[pos]
this.len -= delta
2016-07-01 05:57:13 -04:00
} else if pos == this.last {
this.last = this.prev[pos]
} else {
this.next[this.prev[pos]] = this.next[pos]
this.prev[this.next[pos]] = this.prev[pos]
}
}
func (this *SendingWindow) HandleFastAck(number uint32) {
2016-07-01 17:27:57 -04:00
if this.len == 0 {
return
}
2016-07-01 06:12:32 -04:00
2016-07-01 05:57:13 -04:00
for i := this.start; ; i = this.next[i] {
seg := this.data[i]
2016-07-05 15:47:35 -04:00
if number-seg.Number > 0x7FFFFFFF {
2016-07-01 05:57:13 -04:00
break
}
if number != seg.Number {
seg.ackSkipped++
}
if i == this.last {
break
}
}
}
2016-07-04 09:54:18 -04:00
func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32, maxInFlightSize uint32) {
2016-07-12 17:54:54 -04:00
if this.IsEmpty() {
2016-07-03 16:14:38 -04:00
return
2016-07-01 06:12:32 -04:00
}
2016-07-04 09:34:14 -04:00
var lost uint32
2016-07-04 07:37:42 -04:00
var inFlightSize uint32
2016-07-01 05:57:13 -04:00
for i := this.start; ; i = this.next[i] {
segment := this.data[i]
needsend := false
if segment.transmit == 0 {
needsend = true
segment.transmit++
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-04 09:34:14 -04:00
this.totalInFlightSize++
2016-07-05 15:47:35 -04:00
} else if current-segment.timeout < 0x7FFFFFFF {
2016-07-01 05:57:13 -04:00
needsend = true
segment.transmit++
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-04 09:34:14 -04:00
lost++
2016-07-03 16:14:38 -04:00
} else if segment.ackSkipped >= resend {
2016-07-01 05:57:13 -04:00
needsend = true
segment.transmit++
segment.ackSkipped = 0
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-01 05:57:13 -04:00
}
if needsend {
2016-07-05 17:02:52 -04:00
segment.Timestamp = current
2016-07-03 16:14:38 -04:00
this.writer.Write(segment)
2016-07-04 07:37:42 -04:00
inFlightSize++
2016-07-04 09:54:18 -04:00
if inFlightSize >= maxInFlightSize {
2016-07-04 07:37:42 -04:00
break
}
2016-07-01 05:57:13 -04:00
}
if i == this.last {
break
}
}
2016-08-07 02:45:03 -04:00
if this.onPacketLoss != nil && inFlightSize > 0 && this.totalInFlightSize != 0 {
2016-07-04 09:34:14 -04:00
rate := lost * 100 / this.totalInFlightSize
this.onPacketLoss(rate)
}
2016-07-01 05:57:13 -04:00
}
2016-06-26 17:51:17 -04:00
type SendingQueue struct {
start uint32
cap uint32
len uint32
2016-07-13 15:47:40 -04:00
list []*alloc.Buffer
2016-06-26 17:51:17 -04:00
}
func NewSendingQueue(size uint32) *SendingQueue {
return &SendingQueue{
start: 0,
cap: size,
2016-07-13 15:47:40 -04:00
list: make([]*alloc.Buffer, size),
2016-06-26 17:51:17 -04:00
len: 0,
}
}
func (this *SendingQueue) IsFull() bool {
return this.len == this.cap
}
func (this *SendingQueue) IsEmpty() bool {
return this.len == 0
}
2016-07-13 15:47:40 -04:00
func (this *SendingQueue) Pop() *alloc.Buffer {
2016-06-26 17:51:17 -04:00
if this.IsEmpty() {
return nil
}
seg := this.list[this.start]
this.list[this.start] = nil
this.len--
this.start++
if this.start == this.cap {
this.start = 0
}
2016-07-14 11:38:20 -04:00
if this.IsEmpty() {
this.start = 0
}
2016-06-26 17:51:17 -04:00
return seg
}
2016-08-06 19:01:44 -04:00
func (this *SendingQueue) Last() *alloc.Buffer {
if this.IsEmpty() {
return nil
}
return this.list[(this.start+this.len-1+this.cap)%this.cap]
}
2016-07-13 15:47:40 -04:00
func (this *SendingQueue) Push(seg *alloc.Buffer) {
2016-06-26 17:51:17 -04:00
if this.IsFull() {
return
}
this.list[(this.start+this.len)%this.cap] = seg
this.len++
}
func (this *SendingQueue) Clear() {
for i := uint32(0); i < this.len; i++ {
this.list[(i+this.start)%this.cap].Release()
this.list[(i+this.start)%this.cap] = nil
}
this.start = 0
this.len = 0
}
func (this *SendingQueue) Len() uint32 {
return this.len
}
2016-07-03 16:14:38 -04:00
type SendingWorker struct {
2016-07-12 11:56:36 -04:00
sync.RWMutex
2016-07-05 17:02:52 -04:00
conn *Connection
2016-07-03 16:14:38 -04:00
window *SendingWindow
queue *SendingQueue
firstUnacknowledged uint32
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
updated bool
}
2016-07-05 17:02:52 -04:00
func NewSendingWorker(kcp *Connection) *SendingWorker {
2016-07-03 16:14:38 -04:00
worker := &SendingWorker{
2016-07-05 17:02:52 -04:00
conn: kcp,
2016-07-03 16:14:38 -04:00
queue: NewSendingQueue(effectiveConfig.GetSendingQueueSize()),
fastResend: 2,
remoteNextNumber: 32,
2016-07-04 09:54:18 -04:00
controlWindow: effectiveConfig.GetSendingInFlightSize(),
2016-07-03 16:14:38 -04:00
}
2016-07-04 09:54:18 -04:00
worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), worker, worker.OnPacketLoss)
2016-07-03 16:14:38 -04:00
return worker
}
func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
this.Lock()
defer this.Unlock()
2016-07-06 10:36:15 -04:00
this.ProcessReceivingNextWithoutLock(nextNumber)
}
func (this *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
2016-07-03 16:14:38 -04:00
this.window.Clear(nextNumber)
this.FindFirstUnacknowledged()
}
// @Private
func (this *SendingWorker) FindFirstUnacknowledged() {
prevUna := this.firstUnacknowledged
2016-07-12 17:54:54 -04:00
if !this.window.IsEmpty() {
2016-07-03 16:14:38 -04:00
this.firstUnacknowledged = this.window.First().Number
} else {
this.firstUnacknowledged = this.nextNumber
}
if this.firstUnacknowledged != prevUna {
this.updated = true
}
}
2016-07-06 10:36:15 -04:00
// @Private
2016-07-03 16:14:38 -04:00
func (this *SendingWorker) ProcessAck(number uint32) {
2016-07-12 07:43:25 -04:00
// number < this.firstUnacknowledged || number >= this.nextNumber
if number-this.firstUnacknowledged > 0x7FFFFFFF || number-this.nextNumber < 0x7FFFFFFF {
2016-07-03 16:14:38 -04:00
return
}
this.window.Remove(number - this.firstUnacknowledged)
this.FindFirstUnacknowledged()
}
2016-07-15 15:41:15 -04:00
func (this *SendingWorker) FillWindow(current uint32) {
2016-07-14 11:38:20 -04:00
for !this.queue.IsEmpty() && !this.window.IsFull() {
seg := NewDataSegment()
seg.Data = this.queue.Pop()
seg.Number = this.nextNumber
seg.timeout = current
seg.ackSkipped = 0
seg.transmit = 0
this.window.Push(seg)
this.nextNumber++
}
2016-07-15 15:41:15 -04:00
}
func (this *SendingWorker) ProcessSegment(current uint32, seg *AckSegment) {
defer seg.Release()
this.Lock()
defer this.Unlock()
if this.remoteNextNumber < seg.ReceivingWindow {
this.remoteNextNumber = seg.ReceivingWindow
}
this.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
if current-seg.Timestamp < 10000 {
this.conn.roundTrip.Update(current-seg.Timestamp, current)
}
2016-07-14 11:38:20 -04:00
2016-07-03 16:14:38 -04:00
var maxack uint32
for i := 0; i < int(seg.Count); i++ {
number := seg.NumberList[i]
2016-07-03 16:14:38 -04:00
this.ProcessAck(number)
if maxack < number {
maxack = number
}
}
2016-07-06 10:36:15 -04:00
2016-07-03 16:14:38 -04:00
this.window.HandleFastAck(maxack)
2016-07-15 17:23:21 -04:00
this.FillWindow(current)
2016-07-03 16:14:38 -04:00
}
func (this *SendingWorker) Push(b []byte) int {
nBytes := 0
2016-07-05 18:02:22 -04:00
this.Lock()
defer this.Unlock()
2016-08-06 19:01:44 -04:00
if !this.queue.IsEmpty() {
lastSeg := this.queue.Last()
if lastSeg.Len() < int(this.conn.mss) {
delta := int(this.conn.mss) - lastSeg.Len()
if delta > len(b) {
delta = len(b)
}
lastSeg.Append(b[:delta])
b = b[delta:]
nBytes += delta
}
}
2016-07-03 16:14:38 -04:00
for len(b) > 0 && !this.queue.IsFull() {
var size int
2016-07-05 17:02:52 -04:00
if len(b) > int(this.conn.mss) {
size = int(this.conn.mss)
2016-07-03 16:14:38 -04:00
} else {
size = len(b)
}
2016-07-28 10:24:15 -04:00
this.queue.Push(AllocateBuffer().Clear().Append(b[:size]))
2016-07-03 16:14:38 -04:00
b = b[size:]
nBytes += size
}
return nBytes
}
2016-07-06 10:36:15 -04:00
// @Private
2016-07-04 08:17:42 -04:00
func (this *SendingWorker) Write(seg Segment) {
2016-07-03 16:14:38 -04:00
dataSeg := seg.(*DataSegment)
2016-07-05 17:02:52 -04:00
dataSeg.Conv = this.conn.conv
2016-07-03 16:14:38 -04:00
dataSeg.SendingNext = this.firstUnacknowledged
2016-07-14 16:52:00 -04:00
dataSeg.Option = 0
2016-07-05 18:02:22 -04:00
if this.conn.State() == StateReadyToClose {
2016-07-14 16:52:00 -04:00
dataSeg.Option = SegmentOptionClose
2016-07-03 16:14:38 -04:00
}
2016-07-05 17:02:52 -04:00
this.conn.output.Write(dataSeg)
2016-07-03 16:14:38 -04:00
this.updated = false
}
func (this *SendingWorker) PingNecessary() bool {
2016-07-12 11:56:36 -04:00
this.RLock()
defer this.RUnlock()
2016-07-03 16:14:38 -04:00
return this.updated
}
2016-07-12 11:56:36 -04:00
func (this *SendingWorker) MarkPingNecessary(b bool) {
this.Lock()
defer this.Unlock()
this.updated = b
}
2016-07-04 09:34:14 -04:00
func (this *SendingWorker) OnPacketLoss(lossRate uint32) {
2016-07-05 17:53:46 -04:00
if !effectiveConfig.Congestion || this.conn.roundTrip.Timeout() == 0 {
2016-07-03 16:14:38 -04:00
return
}
2016-07-04 09:34:14 -04:00
if lossRate >= 15 {
2016-07-03 16:14:38 -04:00
this.controlWindow = 3 * this.controlWindow / 4
2016-07-04 09:34:14 -04:00
} else if lossRate <= 5 {
2016-07-03 16:14:38 -04:00
this.controlWindow += this.controlWindow / 4
}
2016-07-04 09:54:18 -04:00
if this.controlWindow < 16 {
this.controlWindow = 16
2016-07-03 16:14:38 -04:00
}
2016-07-04 09:54:18 -04:00
if this.controlWindow > 2*effectiveConfig.GetSendingInFlightSize() {
this.controlWindow = 2 * effectiveConfig.GetSendingInFlightSize()
2016-07-03 16:14:38 -04:00
}
}
2016-07-05 17:02:52 -04:00
func (this *SendingWorker) Flush(current uint32) {
2016-07-03 16:14:38 -04:00
this.Lock()
defer this.Unlock()
2016-07-04 09:54:18 -04:00
cwnd := this.firstUnacknowledged + effectiveConfig.GetSendingInFlightSize()
2016-07-03 16:14:38 -04:00
if cwnd > this.remoteNextNumber {
cwnd = this.remoteNextNumber
}
if effectiveConfig.Congestion && cwnd > this.firstUnacknowledged+this.controlWindow {
cwnd = this.firstUnacknowledged + this.controlWindow
}
2016-07-15 15:41:15 -04:00
this.FillWindow(current)
2016-07-05 17:53:46 -04:00
this.window.Flush(current, this.conn.fastresend, this.conn.roundTrip.Timeout(), cwnd)
2016-07-03 16:14:38 -04:00
}
func (this *SendingWorker) CloseWrite() {
this.Lock()
defer this.Unlock()
this.window.Clear(0xFFFFFFFF)
this.queue.Clear()
}
2016-07-12 17:54:54 -04:00
func (this *SendingWorker) IsEmpty() bool {
this.RLock()
defer this.RUnlock()
return this.window.IsEmpty() && this.queue.IsEmpty()
}