1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/transport/internet/kcp/sending.go

371 lines
7.4 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 (
2018-07-08 15:08:17 -04:00
"container/list"
2016-07-03 16:14:38 -04:00
"sync"
2017-12-03 16:53:00 -05:00
2017-12-16 19:22:39 -05:00
"v2ray.com/core/common"
2017-12-03 16:53:00 -05:00
"v2ray.com/core/common/buf"
2016-07-03 16:14:38 -04:00
)
2016-07-01 05:57:13 -04:00
type SendingWindow struct {
2018-07-08 15:08:17 -04:00
cache *list.List
2016-07-04 09:34:14 -04:00
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
2016-07-01 05:57:13 -04:00
}
2018-07-08 15:08:17 -04:00
func NewSendingWindow(writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
2016-07-01 05:57:13 -04:00
window := &SendingWindow{
2018-07-08 15:08:17 -04:00
cache: list.New(),
2016-07-03 16:14:38 -04:00
writer: writer,
onPacketLoss: onPacketLoss,
2016-07-01 05:57:13 -04:00
}
return window
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) Release() {
if sw == nil {
2016-11-21 16:41:12 -05:00
return
}
2018-07-08 15:08:17 -04:00
for sw.cache.Len() > 0 {
seg := sw.cache.Front().Value.(*DataSegment)
2016-11-21 16:41:12 -05:00
seg.Release()
2018-07-08 15:08:17 -04:00
sw.cache.Remove(sw.cache.Front())
2016-11-21 16:41:12 -05:00
}
}
2018-07-08 15:08:17 -04:00
func (sw *SendingWindow) Len() uint32 {
return uint32(sw.cache.Len())
2016-07-01 05:57:13 -04:00
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) IsEmpty() bool {
2018-07-08 15:08:17 -04:00
return sw.cache.Len() == 0
2016-07-04 09:54:18 -04:00
}
2018-07-28 21:54:15 -04:00
func (sw *SendingWindow) Push(number uint32, b *buf.Buffer) {
2018-07-08 15:08:17 -04:00
seg := NewDataSegment()
seg.Number = number
2018-07-28 21:54:15 -04:00
seg.payload = b
2018-07-08 15:08:17 -04:00
sw.cache.PushBack(seg)
2016-07-01 05:57:13 -04:00
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) FirstNumber() uint32 {
2018-07-08 15:08:17 -04:00
return sw.cache.Front().Value.(*DataSegment).Number
2016-07-01 05:57:13 -04:00
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) Clear(una uint32) {
2018-07-08 15:08:17 -04:00
for !sw.IsEmpty() {
seg := sw.cache.Front().Value.(*DataSegment)
if seg.Number >= una {
break
2016-07-01 05:57:13 -04:00
}
2018-07-08 15:08:17 -04:00
seg.Release()
sw.cache.Remove(sw.cache.Front())
2016-07-01 05:57:13 -04:00
}
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32) {
if sw.IsEmpty() {
2016-07-01 17:27:57 -04:00
return
}
2016-07-01 06:12:32 -04:00
2017-12-03 08:56:00 -05:00
sw.Visit(func(seg *DataSegment) bool {
2016-11-18 10:19:13 -05:00
if number == seg.Number || number-seg.Number > 0x7FFFFFFF {
return false
2016-07-01 05:57:13 -04:00
}
2016-11-18 10:19:13 -05:00
if seg.transmit > 0 && seg.timeout > rto/3 {
seg.timeout -= rto / 3
2016-07-01 05:57:13 -04:00
}
2016-11-18 10:19:13 -05:00
return true
})
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool) {
if sw.IsEmpty() {
2016-12-06 18:31:01 -05:00
return
}
2018-07-08 15:08:17 -04:00
for e := sw.cache.Front(); e != nil; e = e.Next() {
seg := e.Value.(*DataSegment)
if !visitor(seg) {
2016-07-01 05:57:13 -04:00
break
}
}
}
2017-12-03 08:56:00 -05:00
func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32) {
if sw.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
2017-12-03 08:56:00 -05:00
sw.Visit(func(segment *DataSegment) bool {
2016-11-18 10:19:13 -05:00
if current-segment.timeout >= 0x7FFFFFFF {
return true
2016-07-01 05:57:13 -04:00
}
2016-11-18 10:19:13 -05:00
if segment.transmit == 0 {
// First time
2017-12-03 08:56:00 -05:00
sw.totalInFlightSize++
2016-11-18 10:19:13 -05:00
} else {
lost++
2016-07-01 05:57:13 -04:00
}
2016-11-18 10:19:13 -05:00
segment.timeout = current + rto
segment.Timestamp = current
segment.transmit++
2017-12-03 08:56:00 -05:00
sw.writer.Write(segment)
2016-11-18 10:19:13 -05:00
inFlightSize++
if inFlightSize >= maxInFlightSize {
return false
2016-07-01 05:57:13 -04:00
}
2016-11-18 10:19:13 -05:00
return true
})
2016-07-01 05:57:13 -04:00
2017-12-03 08:56:00 -05:00
if sw.onPacketLoss != nil && inFlightSize > 0 && sw.totalInFlightSize != 0 {
rate := lost * 100 / sw.totalInFlightSize
sw.onPacketLoss(rate)
2016-07-04 09:34:14 -04:00
}
2016-07-01 05:57:13 -04:00
}
2018-07-08 15:08:17 -04:00
func (sw *SendingWindow) Remove(number uint32) bool {
if sw.IsEmpty() {
return false
}
for e := sw.cache.Front(); e != nil; e = e.Next() {
seg := e.Value.(*DataSegment)
if seg.Number > number {
return false
} else if seg.Number == number {
2018-07-08 16:08:26 -04:00
if sw.totalInFlightSize > 0 {
sw.totalInFlightSize--
}
seg.Release()
sw.cache.Remove(e)
2018-07-08 15:08:17 -04:00
return true
}
}
return false
}
2016-07-03 16:14:38 -04:00
type SendingWorker struct {
2016-07-12 11:56:36 -04:00
sync.RWMutex
2016-10-11 06:24:19 -04:00
conn *Connection
window *SendingWindow
firstUnacknowledged uint32
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
2018-07-08 15:08:17 -04:00
windowSize uint32
firstUnacknowledgedUpdated bool
closed bool
2016-07-03 16:14:38 -04:00
}
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
fastResend: 2,
remoteNextNumber: 32,
2016-10-02 17:43:58 -04:00
controlWindow: kcp.Config.GetSendingInFlightSize(),
2018-07-08 15:08:17 -04:00
windowSize: kcp.Config.GetSendingBufferSize(),
2016-07-03 16:14:38 -04:00
}
2018-07-08 15:08:17 -04:00
worker.window = NewSendingWindow(worker, worker.OnPacketLoss)
2016-07-03 16:14:38 -04:00
return worker
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) Release() {
w.Lock()
w.window.Release()
w.closed = true
2018-01-17 11:36:14 -05:00
w.Unlock()
2016-11-21 16:41:12 -05:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
w.Lock()
defer w.Unlock()
2016-07-03 16:14:38 -04:00
2018-01-17 11:36:14 -05:00
w.ProcessReceivingNextWithoutLock(nextNumber)
2016-07-06 10:36:15 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
w.window.Clear(nextNumber)
w.FindFirstUnacknowledged()
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) FindFirstUnacknowledged() {
first := w.firstUnacknowledged
if !w.window.IsEmpty() {
w.firstUnacknowledged = w.window.FirstNumber()
2016-07-03 16:14:38 -04:00
} else {
2018-01-17 11:36:14 -05:00
w.firstUnacknowledged = w.nextNumber
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
if first != w.firstUnacknowledged {
w.firstUnacknowledgedUpdated = true
2016-10-11 06:24:19 -04:00
}
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) processAck(number uint32) bool {
2016-11-27 15:39:09 -05:00
// number < v.firstUnacknowledged || number >= v.nextNumber
2018-01-17 11:36:14 -05:00
if number-w.firstUnacknowledged > 0x7FFFFFFF || number-w.nextNumber < 0x7FFFFFFF {
2016-11-13 16:27:58 -05:00
return false
2016-07-03 16:14:38 -04:00
}
2018-07-08 15:08:17 -04:00
removed := w.window.Remove(number)
2016-11-13 16:27:58 -05:00
if removed {
2018-01-17 11:36:14 -05:00
w.FindFirstUnacknowledged()
2016-11-13 16:27:58 -05:00
}
return removed
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32) {
2016-07-15 15:41:15 -04:00
defer seg.Release()
2018-01-17 11:36:14 -05:00
w.Lock()
defer w.Unlock()
2016-07-15 15:41:15 -04:00
if w.closed {
return
}
2018-01-17 11:36:14 -05:00
if w.remoteNextNumber < seg.ReceivingWindow {
w.remoteNextNumber = seg.ReceivingWindow
2016-07-15 15:41:15 -04:00
}
2018-01-17 11:36:14 -05:00
w.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
2016-07-14 11:38:20 -04:00
2016-12-21 09:37:16 -05:00
if seg.IsEmpty() {
2016-12-02 15:40:58 -05:00
return
}
2016-07-03 16:14:38 -04:00
var maxack uint32
2016-11-13 16:27:58 -05:00
var maxackRemoved bool
2016-12-21 09:37:16 -05:00
for _, number := range seg.NumberList {
2018-01-17 11:36:14 -05:00
removed := w.processAck(number)
2016-07-03 16:14:38 -04:00
if maxack < number {
maxack = number
2016-11-13 16:27:58 -05:00
maxackRemoved = removed
2016-07-03 16:14:38 -04:00
}
}
2016-07-06 10:36:15 -04:00
2016-11-13 16:27:58 -05:00
if maxackRemoved {
2018-01-17 11:36:14 -05:00
w.window.HandleFastAck(maxack, rto)
2016-11-13 16:27:58 -05:00
if current-seg.Timestamp < 10000 {
2018-01-17 11:36:14 -05:00
w.conn.roundTrip.Update(current-seg.Timestamp, current)
2016-11-13 16:27:58 -05:00
}
}
2016-07-03 16:14:38 -04:00
}
2018-07-28 21:54:15 -04:00
func (w *SendingWorker) Push(mb *buf.MultiBuffer) bool {
2018-01-17 11:36:14 -05:00
w.Lock()
defer w.Unlock()
2016-08-25 05:41:05 -04:00
if w.closed {
return false
}
2018-07-08 15:08:17 -04:00
if w.window.Len() > w.windowSize {
2017-12-16 19:22:39 -05:00
return false
2016-07-03 16:14:38 -04:00
}
2017-12-05 12:04:34 -05:00
2018-07-28 21:54:15 -04:00
b := buf.New()
common.Must(b.Reset(func(v []byte) (int, error) {
return mb.Read(v[:w.conn.mss])
}))
w.window.Push(w.nextNumber, b)
2018-01-17 11:36:14 -05:00
w.nextNumber++
2017-12-16 19:22:39 -05:00
return true
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) Write(seg Segment) error {
2016-07-03 16:14:38 -04:00
dataSeg := seg.(*DataSegment)
2018-01-17 11:36:14 -05:00
dataSeg.Conv = w.conn.meta.Conversation
dataSeg.SendingNext = w.firstUnacknowledged
2016-07-14 16:52:00 -04:00
dataSeg.Option = 0
2018-01-17 11:36:14 -05:00
if w.conn.State() == StateReadyToClose {
2016-07-14 16:52:00 -04:00
dataSeg.Option = SegmentOptionClose
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
return w.conn.output.Write(dataSeg)
2016-07-12 11:56:36 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) OnPacketLoss(lossRate uint32) {
if !w.conn.Config.Congestion || w.conn.roundTrip.Timeout() == 0 {
2016-07-03 16:14:38 -04:00
return
}
2016-07-04 09:34:14 -04:00
if lossRate >= 15 {
2018-01-17 11:36:14 -05:00
w.controlWindow = 3 * w.controlWindow / 4
2016-07-04 09:34:14 -04:00
} else if lossRate <= 5 {
2018-01-17 11:36:14 -05:00
w.controlWindow += w.controlWindow / 4
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
if w.controlWindow < 16 {
w.controlWindow = 16
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
if w.controlWindow > 2*w.conn.Config.GetSendingInFlightSize() {
w.controlWindow = 2 * w.conn.Config.GetSendingInFlightSize()
2016-07-03 16:14:38 -04:00
}
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) Flush(current uint32) {
w.Lock()
2016-07-03 16:14:38 -04:00
if w.closed {
w.Unlock()
return
}
2018-01-17 11:36:14 -05:00
cwnd := w.firstUnacknowledged + w.conn.Config.GetSendingInFlightSize()
if cwnd > w.remoteNextNumber {
cwnd = w.remoteNextNumber
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
if w.conn.Config.Congestion && cwnd > w.firstUnacknowledged+w.controlWindow {
cwnd = w.firstUnacknowledged + w.controlWindow
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
if !w.window.IsEmpty() {
w.window.Flush(current, w.conn.roundTrip.Timeout(), cwnd)
w.firstUnacknowledgedUpdated = false
2016-08-24 09:47:14 -04:00
}
2016-10-11 06:24:19 -04:00
2018-01-17 11:36:14 -05:00
updated := w.firstUnacknowledgedUpdated
w.firstUnacknowledgedUpdated = false
2017-02-17 18:04:25 -05:00
2018-01-17 11:36:14 -05:00
w.Unlock()
2017-02-17 18:04:25 -05:00
if updated {
2018-01-17 11:36:14 -05:00
w.conn.Ping(current, CommandPing)
2017-02-17 18:04:25 -05:00
}
2016-07-03 16:14:38 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) CloseWrite() {
w.Lock()
defer w.Unlock()
2016-07-03 16:14:38 -04:00
2018-01-17 11:36:14 -05:00
w.window.Clear(0xFFFFFFFF)
2016-07-03 16:14:38 -04:00
}
2016-07-12 17:54:54 -04:00
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) IsEmpty() bool {
2018-02-05 17:39:04 -05:00
w.RLock()
2018-01-17 11:36:14 -05:00
defer w.RUnlock()
2016-07-12 17:54:54 -04:00
2018-01-17 11:36:14 -05:00
return w.window.IsEmpty()
2016-07-12 17:54:54 -04:00
}
2018-01-17 11:36:14 -05:00
func (w *SendingWorker) UpdateNecessary() bool {
return !w.IsEmpty()
}
2017-02-17 18:04:25 -05:00
func (w *SendingWorker) FirstUnacknowledged() uint32 {
w.RLock()
defer w.RUnlock()
return w.firstUnacknowledged
}