You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/sending.go

383 lines
7.7 KiB

package kcp
9 years ago
import (
"sync"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
9 years ago
)
9 years ago
type SendingWindow struct {
start uint32
cap uint32
len uint32
last uint32
data []DataSegment
inuse []bool
prev []uint32
next []uint32
9 years ago
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
9 years ago
}
func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
9 years ago
window := &SendingWindow{
9 years ago
start: 0,
cap: size,
len: 0,
last: 0,
data: make([]DataSegment, size),
9 years ago
prev: make([]uint32, size),
next: make([]uint32, size),
inuse: make([]bool, size),
9 years ago
writer: writer,
onPacketLoss: onPacketLoss,
9 years ago
}
return window
}
7 years ago
func (sw *SendingWindow) Release() {
if sw == nil {
return
}
7 years ago
sw.len = 0
for _, seg := range sw.data {
seg.Release()
}
}
7 years ago
func (sw *SendingWindow) Len() int {
return int(sw.len)
9 years ago
}
7 years ago
func (sw *SendingWindow) IsEmpty() bool {
return sw.len == 0
}
7 years ago
func (sw *SendingWindow) Size() uint32 {
return sw.cap
}
7 years ago
func (sw *SendingWindow) IsFull() bool {
return sw.len == sw.cap
}
func (sw *SendingWindow) Push(number uint32) *buf.Buffer {
7 years ago
pos := (sw.start + sw.len) % sw.cap
sw.data[pos].Number = number
sw.data[pos].timeout = 0
sw.data[pos].transmit = 0
sw.inuse[pos] = true
if sw.len > 0 {
sw.next[sw.last] = pos
sw.prev[pos] = sw.last
9 years ago
}
7 years ago
sw.last = pos
sw.len++
return sw.data[pos].Data()
9 years ago
}
7 years ago
func (sw *SendingWindow) FirstNumber() uint32 {
return sw.data[sw.start].Number
9 years ago
}
7 years ago
func (sw *SendingWindow) Clear(una uint32) {
for !sw.IsEmpty() && sw.data[sw.start].Number < una {
sw.Remove(0)
9 years ago
}
}
7 years ago
func (sw *SendingWindow) Remove(idx uint32) bool {
if sw.IsEmpty() {
return false
}
7 years ago
pos := (sw.start + idx) % sw.cap
if !sw.inuse[pos] {
return false
}
7 years ago
sw.inuse[pos] = false
sw.totalInFlightSize--
if pos == sw.start && pos == sw.last {
sw.len = 0
sw.start = 0
sw.last = 0
} else if pos == sw.start {
delta := sw.next[pos] - sw.start
if sw.next[pos] < sw.start {
delta = sw.next[pos] + sw.cap - sw.start
9 years ago
}
7 years ago
sw.start = sw.next[pos]
sw.len -= delta
} else if pos == sw.last {
sw.last = sw.prev[pos]
9 years ago
} else {
7 years ago
sw.next[sw.prev[pos]] = sw.next[pos]
sw.prev[sw.next[pos]] = sw.prev[pos]
9 years ago
}
return true
9 years ago
}
7 years ago
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32) {
if sw.IsEmpty() {
return
}
7 years ago
sw.Visit(func(seg *DataSegment) bool {
if number == seg.Number || number-seg.Number > 0x7FFFFFFF {
return false
9 years ago
}
if seg.transmit > 0 && seg.timeout > rto/3 {
seg.timeout -= rto / 3
9 years ago
}
return true
})
}
7 years ago
func (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool) {
if sw.IsEmpty() {
return
}
7 years ago
for i := sw.start; ; i = sw.next[i] {
if !visitor(&sw.data[i]) || i == sw.last {
9 years ago
break
}
}
}
7 years ago
func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32) {
if sw.IsEmpty() {
9 years ago
return
}
var lost uint32
var inFlightSize uint32
9 years ago
7 years ago
sw.Visit(func(segment *DataSegment) bool {
if current-segment.timeout >= 0x7FFFFFFF {
return true
9 years ago
}
if segment.transmit == 0 {
// First time
7 years ago
sw.totalInFlightSize++
} else {
lost++
9 years ago
}
segment.timeout = current + rto
segment.Timestamp = current
segment.transmit++
7 years ago
sw.writer.Write(segment)
inFlightSize++
if inFlightSize >= maxInFlightSize {
return false
9 years ago
}
return true
})
9 years ago
7 years ago
if sw.onPacketLoss != nil && inFlightSize > 0 && sw.totalInFlightSize != 0 {
rate := lost * 100 / sw.totalInFlightSize
sw.onPacketLoss(rate)
}
9 years ago
}
9 years ago
type SendingWorker struct {
sync.RWMutex
conn *Connection
window *SendingWindow
firstUnacknowledged uint32
firstUnacknowledgedUpdated bool
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
9 years ago
}
func NewSendingWorker(kcp *Connection) *SendingWorker {
9 years ago
worker := &SendingWorker{
conn: kcp,
9 years ago
fastResend: 2,
remoteNextNumber: 32,
controlWindow: kcp.Config.GetSendingInFlightSize(),
9 years ago
}
worker.window = NewSendingWindow(kcp.Config.GetSendingBufferSize(), worker, worker.OnPacketLoss)
9 years ago
return worker
}
7 years ago
func (w *SendingWorker) Release() {
w.Lock()
w.window.Release()
w.Unlock()
}
7 years ago
func (w *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
w.Lock()
defer w.Unlock()
9 years ago
7 years ago
w.ProcessReceivingNextWithoutLock(nextNumber)
}
7 years ago
func (w *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
w.window.Clear(nextNumber)
w.FindFirstUnacknowledged()
9 years ago
}
7 years ago
func (w *SendingWorker) FindFirstUnacknowledged() {
first := w.firstUnacknowledged
if !w.window.IsEmpty() {
w.firstUnacknowledged = w.window.FirstNumber()
9 years ago
} else {
7 years ago
w.firstUnacknowledged = w.nextNumber
9 years ago
}
7 years ago
if first != w.firstUnacknowledged {
w.firstUnacknowledgedUpdated = true
}
9 years ago
}
7 years ago
func (w *SendingWorker) processAck(number uint32) bool {
8 years ago
// number < v.firstUnacknowledged || number >= v.nextNumber
7 years ago
if number-w.firstUnacknowledged > 0x7FFFFFFF || number-w.nextNumber < 0x7FFFFFFF {
return false
9 years ago
}
7 years ago
removed := w.window.Remove(number - w.firstUnacknowledged)
if removed {
7 years ago
w.FindFirstUnacknowledged()
}
return removed
9 years ago
}
7 years ago
func (w *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32) {
defer seg.Release()
7 years ago
w.Lock()
defer w.Unlock()
7 years ago
if w.remoteNextNumber < seg.ReceivingWindow {
w.remoteNextNumber = seg.ReceivingWindow
}
7 years ago
w.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
if seg.IsEmpty() {
return
}
9 years ago
var maxack uint32
var maxackRemoved bool
for _, number := range seg.NumberList {
7 years ago
removed := w.processAck(number)
9 years ago
if maxack < number {
maxack = number
maxackRemoved = removed
9 years ago
}
}
if maxackRemoved {
7 years ago
w.window.HandleFastAck(maxack, rto)
if current-seg.Timestamp < 10000 {
7 years ago
w.conn.roundTrip.Update(current-seg.Timestamp, current)
}
}
9 years ago
}
7 years ago
func (w *SendingWorker) Push(f buf.Supplier) bool {
w.Lock()
defer w.Unlock()
7 years ago
if w.window.IsFull() {
return false
9 years ago
}
7 years ago
7 years ago
b := w.window.Push(w.nextNumber)
w.nextNumber++
common.Must(b.Reset(f))
return true
9 years ago
}
7 years ago
func (w *SendingWorker) Write(seg Segment) error {
9 years ago
dataSeg := seg.(*DataSegment)
7 years ago
dataSeg.Conv = w.conn.meta.Conversation
dataSeg.SendingNext = w.firstUnacknowledged
8 years ago
dataSeg.Option = 0
7 years ago
if w.conn.State() == StateReadyToClose {
8 years ago
dataSeg.Option = SegmentOptionClose
9 years ago
}
7 years ago
return w.conn.output.Write(dataSeg)
}
7 years ago
func (w *SendingWorker) OnPacketLoss(lossRate uint32) {
if !w.conn.Config.Congestion || w.conn.roundTrip.Timeout() == 0 {
9 years ago
return
}
if lossRate >= 15 {
7 years ago
w.controlWindow = 3 * w.controlWindow / 4
} else if lossRate <= 5 {
7 years ago
w.controlWindow += w.controlWindow / 4
9 years ago
}
7 years ago
if w.controlWindow < 16 {
w.controlWindow = 16
9 years ago
}
7 years ago
if w.controlWindow > 2*w.conn.Config.GetSendingInFlightSize() {
w.controlWindow = 2 * w.conn.Config.GetSendingInFlightSize()
9 years ago
}
}
7 years ago
func (w *SendingWorker) Flush(current uint32) {
w.Lock()
9 years ago
7 years ago
cwnd := w.firstUnacknowledged + w.conn.Config.GetSendingInFlightSize()
if cwnd > w.remoteNextNumber {
cwnd = w.remoteNextNumber
9 years ago
}
7 years ago
if w.conn.Config.Congestion && cwnd > w.firstUnacknowledged+w.controlWindow {
cwnd = w.firstUnacknowledged + w.controlWindow
9 years ago
}
7 years ago
if !w.window.IsEmpty() {
w.window.Flush(current, w.conn.roundTrip.Timeout(), cwnd)
w.firstUnacknowledgedUpdated = false
}
7 years ago
updated := w.firstUnacknowledgedUpdated
w.firstUnacknowledgedUpdated = false
7 years ago
w.Unlock()
if updated {
7 years ago
w.conn.Ping(current, CommandPing)
}
9 years ago
}
7 years ago
func (w *SendingWorker) CloseWrite() {
w.Lock()
defer w.Unlock()
9 years ago
7 years ago
w.window.Clear(0xFFFFFFFF)
9 years ago
}
7 years ago
func (w *SendingWorker) IsEmpty() bool {
7 years ago
w.RLock()
7 years ago
defer w.RUnlock()
7 years ago
return w.window.IsEmpty()
}
7 years ago
func (w *SendingWorker) UpdateNecessary() bool {
return !w.IsEmpty()
}
func (w *SendingWorker) FirstUnacknowledged() uint32 {
w.RLock()
defer w.RUnlock()
return w.firstUnacknowledged
}