2026-07-25 17:58:54 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"hash/crc32"
|
|
|
|
|
"sync"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type rxStats struct {
|
|
|
|
|
frames atomic.Uint64
|
|
|
|
|
bytes atomic.Uint64
|
|
|
|
|
badMagic atomic.Uint64
|
2026-08-04 20:41:16 -07:00
|
|
|
badHdr atomic.Uint64
|
2026-07-25 17:58:54 -07:00
|
|
|
badLen atomic.Uint64
|
|
|
|
|
crcErr atomic.Uint64
|
2026-07-25 22:54:04 -07:00
|
|
|
rxErrs atomic.Uint64
|
2026-08-04 20:04:30 -07:00
|
|
|
|
|
|
|
|
// Frames counted into the interval their mac receive stamp falls in, rather
|
|
|
|
|
// than the interval a worker got round to draining them in.
|
|
|
|
|
newest atomic.Int64
|
|
|
|
|
buckets [rateBuckets]rxBucket
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One sample interval of arrivals, keyed by the mac's clock, with enough of them
|
|
|
|
|
// kept that a bucket is read long before its slot comes round again.
|
|
|
|
|
const (
|
|
|
|
|
rateBucketNs = int64(sampleInterval)
|
|
|
|
|
rateBuckets = 64
|
|
|
|
|
rateBucketSecs = float64(rateBucketNs) / 1e9
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type rxBucket struct {
|
|
|
|
|
epoch atomic.Int64
|
|
|
|
|
frames atomic.Uint64
|
|
|
|
|
bytes atomic.Uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only the owning worker writes its own buckets, so a slot coming round again is
|
|
|
|
|
// simply zeroed before it is claimed for the new epoch.
|
|
|
|
|
func (s *rxStats) observe(stamp int64, n uint64) {
|
|
|
|
|
e := stamp / rateBucketNs
|
|
|
|
|
b := &s.buckets[e&(rateBuckets-1)]
|
|
|
|
|
if b.epoch.Load() != e {
|
|
|
|
|
b.frames.Store(0)
|
|
|
|
|
b.bytes.Store(0)
|
|
|
|
|
b.epoch.Store(e)
|
|
|
|
|
}
|
|
|
|
|
b.frames.Add(1)
|
|
|
|
|
b.bytes.Add(n)
|
|
|
|
|
if e > s.newest.Load() {
|
|
|
|
|
s.newest.Store(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// What this worker counted into one epoch, or nothing if that epoch has already
|
|
|
|
|
// fallen out of the ring.
|
|
|
|
|
func (s *rxStats) bucket(e int64) (frames, bytes uint64) {
|
|
|
|
|
b := &s.buckets[e&(rateBuckets-1)]
|
|
|
|
|
if b.epoch.Load() != e {
|
|
|
|
|
return 0, 0
|
|
|
|
|
}
|
|
|
|
|
return b.frames.Load(), b.bytes.Load()
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type rxWorker struct {
|
|
|
|
|
fd int
|
|
|
|
|
batch int
|
|
|
|
|
spec *frameSpec
|
|
|
|
|
stats *rxStats
|
2026-07-25 18:33:36 -07:00
|
|
|
streams []lossWindow
|
2026-07-25 17:58:54 -07:00
|
|
|
ready *sync.WaitGroup
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *rxWorker) run(done *atomic.Bool) {
|
|
|
|
|
bufs := make([][]byte, w.batch)
|
|
|
|
|
for i := range bufs {
|
|
|
|
|
bufs[i] = make([]byte, maxFrame)
|
|
|
|
|
for j := 0; j < maxFrame; j += 4096 {
|
|
|
|
|
bufs[i][j] = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-04 20:04:30 -07:00
|
|
|
hdrs, oob := newRxMmsghdrs(bufs)
|
2026-07-25 17:58:54 -07:00
|
|
|
|
|
|
|
|
w.ready.Done()
|
|
|
|
|
|
|
|
|
|
for !done.Load() {
|
2026-08-04 20:04:30 -07:00
|
|
|
// The kernel overwrites each Controllen with what it wrote, so they are
|
|
|
|
|
// reset before every call.
|
|
|
|
|
for i := range hdrs {
|
|
|
|
|
hdrs[i].hdr.Controllen = cmsgLen
|
|
|
|
|
}
|
2026-07-25 17:58:54 -07:00
|
|
|
n, err := recvmmsg(w.fd, hdrs, unix.MSG_WAITFORONE)
|
|
|
|
|
if n <= 0 {
|
|
|
|
|
if err != nil && err != unix.EAGAIN && err != unix.EINTR {
|
2026-07-25 22:54:04 -07:00
|
|
|
w.stats.rxErrs.Add(1)
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
buf := bufs[i][:int(hdrs[i].len)]
|
2026-08-04 20:41:16 -07:00
|
|
|
p, st := parseHeader(buf)
|
|
|
|
|
if st != hdrOK {
|
|
|
|
|
if st == hdrForeign {
|
|
|
|
|
w.stats.badMagic.Add(1)
|
|
|
|
|
} else {
|
|
|
|
|
w.stats.badHdr.Add(1)
|
|
|
|
|
}
|
2026-07-25 17:58:54 -07:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
w.stats.frames.Add(1)
|
|
|
|
|
w.stats.bytes.Add(uint64(len(buf)))
|
2026-08-04 20:04:30 -07:00
|
|
|
if ts, ok := hwTimestamp(oob[i][:hdrs[i].hdr.Controllen]); ok {
|
|
|
|
|
w.stats.observe(ts, uint64(len(buf)))
|
|
|
|
|
}
|
2026-07-25 17:58:54 -07:00
|
|
|
|
2026-08-04 20:41:16 -07:00
|
|
|
// A sequence number the sender never reached got past the header
|
|
|
|
|
// checksum, so the frame is damaged whatever its payload says. Counted
|
|
|
|
|
// here rather than left to the payload check, which would report the
|
|
|
|
|
// same frame twice or, if only the header was hit, not at all.
|
|
|
|
|
if int(p.stream) < len(w.streams) && !w.streams[p.stream].observe(p.seq) {
|
|
|
|
|
w.stats.badHdr.Add(1)
|
|
|
|
|
continue
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-04 12:45:04 -07:00
|
|
|
want, ok := w.spec.expectedCRC(p.patIdx, p.payLen)
|
|
|
|
|
if !ok {
|
2026-07-25 17:58:54 -07:00
|
|
|
w.stats.badLen.Add(1)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
pay := buf[minFrame : minFrame+p.payLen]
|
2026-08-04 12:45:04 -07:00
|
|
|
if crc32.Checksum(pay, crcTable) != want {
|
2026-07-25 22:54:04 -07:00
|
|
|
w.stats.crcErr.Add(1)
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|