)
const (
- packetAlignment uint32 = 4 // Number of bytes packets are aligned to in rings
- packetSizeMax = 0xffff // Maximum packet size
- packetCapacity = 0x800000 // Ring capacity, 8MiB
- packetTrailingSize = uint32(unsafe.Sizeof(packetHeader{})) + ((packetSizeMax + (packetAlignment - 1)) &^ (packetAlignment - 1)) - packetAlignment
- ioctlRegisterRings = (51820 << 16) | (0x970 << 2) | 0 /*METHOD_BUFFERED*/ | (0x3 /*FILE_READ_DATA | FILE_WRITE_DATA*/ << 14)
+ packetAlignment = 4 // Number of bytes packets are aligned to in rings
+ packetSizeMax = 0xffff // Maximum packet size
+ packetCapacity = 0x800000 // Ring capacity, 8MiB
+ packetTrailingSize = uint32(unsafe.Sizeof(packetHeader{})) + ((packetSizeMax + (packetAlignment - 1)) &^ (packetAlignment - 1)) - packetAlignment
+ ioctlRegisterRings = (51820 << 16) | (0x970 << 2) | 0 /*METHOD_BUFFERED*/ | (0x3 /*FILE_READ_DATA | FILE_WRITE_DATA*/ << 14)
+ rateMeasurementGranularity = uint64((time.Second / 2) / time.Nanosecond)
+ spinloopRateThreshold = 800000000 / 8 // 800mbps
+ spinloopDuration = uint64(time.Millisecond / 80 / time.Nanosecond) // ~1gbit/s
)
type packetHeader struct {
}
}
+type rateJuggler struct {
+ current uint64
+ nextByteCount uint64
+ nextStartTime int64
+ changing int32
+}
+
type NativeTun struct {
wt *wintun.Wintun
handle windows.Handle
events chan Event
errors chan error
forcedMTU int
+ rate rateJuggler
}
+//go:linkname procyield runtime.procyield
+func procyield(cycles uint32)
+
+//go:linkname nanotime runtime.nanotime
+func nanotime() int64
+
func packetAlign(size uint32) uint32 {
return (size + (packetAlignment - 1)) &^ (packetAlignment - 1)
}
tun.forcedMTU = mtu
}
-//go:linkname procyield runtime.procyield
-func procyield(cycles uint32)
-
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
return 0, os.ErrClosed
}
- start := time.Now()
+ start := nanotime()
+ shouldSpin := atomic.LoadUint64(&tun.rate.current) >= spinloopRateThreshold && uint64(start-atomic.LoadInt64(&tun.rate.nextStartTime)) <= rateMeasurementGranularity*2
var buffTail uint32
for {
buffTail = atomic.LoadUint32(&tun.rings.send.ring.tail)
if tun.close {
return 0, os.ErrClosed
}
- if time.Since(start) >= time.Millisecond/80 /* ~1gbit/s */ {
+ if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration {
windows.WaitForSingleObject(tun.rings.send.tailMoved, windows.INFINITE)
goto retry
}
copy(buff[offset:], packet.data[:packet.size])
buffHead = tun.rings.send.ring.wrap(buffHead + alignedPacketSize)
atomic.StoreUint32(&tun.rings.send.ring.head, buffHead)
+ tun.rate.update(uint64(packet.size))
return int(packet.size), nil
}
}
packetSize := uint32(len(buff) - offset)
+ tun.rate.update(uint64(packetSize))
alignedPacketSize := packetAlign(uint32(unsafe.Sizeof(packetHeader{})) + packetSize)
buffHead := atomic.LoadUint32(&tun.rings.receive.ring.head)
func (rb *ring) wrap(value uint32) uint32 {
return value & (packetCapacity - 1)
}
+
+func (rate *rateJuggler) update(packetLen uint64) {
+ now := nanotime()
+ total := atomic.AddUint64(&rate.nextByteCount, packetLen)
+ period := uint64(now - atomic.LoadInt64(&rate.nextStartTime))
+ if period >= rateMeasurementGranularity {
+ if !atomic.CompareAndSwapInt32(&rate.changing, 0, 1) {
+ return
+ }
+ atomic.StoreInt64(&rate.nextStartTime, now)
+ atomic.StoreUint64(&rate.current, total*uint64(time.Second/time.Nanosecond)/period)
+ atomic.StoreUint64(&rate.nextByteCount, 0)
+ atomic.StoreInt32(&rate.changing, 0)
+ }
+}