flow.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package nebula
  2. import (
  3. "log/slog"
  4. "runtime"
  5. "sync"
  6. "github.com/slackhq/nebula/config"
  7. "github.com/slackhq/nebula/noiseutil"
  8. )
  9. // Staged processing (tun.staged.workers) moves crypto off each tun reader and udp listener onto worker goroutines,
  10. // behind which an ordered stage sends or writes every batch in read order. It reuses the inline packet handlers:
  11. // staging changes ownership and scheduling, not policy. Each routine has its own workers and ordered stages, so order
  12. // is per routine. Full queues block the reader. A read error drains the batches in flight, while shutdown waits for
  13. // the crypto and then drops packets whose destination has closed. A TX slot holds one tun read and its ciphertext,
  14. // about 1.1 MiB; there are eight, or two more than the workers. An RX slot holds one recvmmsg batch, listen.batch
  15. // times 64 KiB with GRO, and the count keeps up to 384 entries in flight in four to six slots: six and 24 MiB at
  16. // listen.batch=64.
  17. // tun.staged.tx_slots and rx_slots override the counts. Small-MSS TX arenas can grow.
  18. // Relay data stays on the tun reader, regardless of the TX gate, so it never switches between reader and sender
  19. // NIC queues. Direct data keeps its adaptive placement; relay and direct batches both share tunnel counter credit.
  20. //
  21. // Metrics are registered only for active pipelines. flow.{tx,rx}.batches counts reads handed to the ordered stage and
  22. // flow.{tx,rx}.inline those the reader handled itself (TX inline also counts oversized reads); their rates show
  23. // whether staging is used. flow.tx.gate_switches counts transmit switches between inline and fanout mode.
  24. // stagedConfig is tun.staged. workers is per routine and direction; zero keeps processing inline.
  25. type stagedConfig struct {
  26. workers int
  27. tx bool
  28. rx bool
  29. // inlineBelow is the TX segment or RX packet count below which a read may skip the worker handoff, 0 never: a TX
  30. // read in inline mode, an RX read with nothing in flight
  31. inlineBelow int
  32. // txSlots and rxSlots are how many batches each routine can have in flight per direction
  33. txSlots, rxSlots int
  34. // gateHold is how many TX reads in a row must want the other mode before a routine switches between inline and
  35. // fanout transmit
  36. gateHold int
  37. }
  38. // stagedGateHold is the default tun.staged.gate_hold
  39. const stagedGateHold = 64
  40. const (
  41. // stagedTxSlots is the default TX slot count, unless the workers need more
  42. stagedTxSlots = 8
  43. // stagedRxEntries is how many recvmmsg entries the RX slots hold in flight by default
  44. stagedRxEntries = 6 * 64
  45. // stagedMinSlots lets the reader fill one batch while another is in flight
  46. stagedMinSlots = 2
  47. // stagedMinRxSlots keeps the default RX pipeline deep enough for large listen.batch values
  48. stagedMinRxSlots = 4
  49. // stagedMaxRxSlots is the measured default, a smaller listen.batch does not take more slots
  50. stagedMaxRxSlots = 6
  51. )
  52. // stagedSlots sizes the TX and RX slots. TX reads are bounded by the tun queue, so one slot per worker plus the one
  53. // being read and the one being sent, and at least the eight measured. RX slots keep stagedRxEntries in flight, four
  54. // to six, so a larger listen.batch means fewer slots and RX memory never exceeds the six measured. A positive override replaces either, down to stagedMinSlots.
  55. func stagedSlots(workers, batch, txOverride, rxOverride int) (tx, rx int) {
  56. tx = max(stagedTxSlots, workers+2)
  57. rx = min(max(stagedMinRxSlots, (stagedRxEntries+batch-1)/max(batch, 1)), stagedMaxRxSlots)
  58. if txOverride > 0 {
  59. tx = max(txOverride, stagedMinSlots)
  60. }
  61. if rxOverride > 0 {
  62. rx = max(rxOverride, stagedMinSlots)
  63. }
  64. return tx, rx
  65. }
  66. // parseStagedConfig enables staging on Linux only; queues and conns that cannot hand off batches also stay inline.
  67. func parseStagedConfig(c *config.C, l *slog.Logger) stagedConfig {
  68. sc := stagedConfig{workers: c.GetInt("tun.staged.workers", 0)}
  69. if sc.workers <= 0 {
  70. return stagedConfig{}
  71. }
  72. if runtime.GOOS != "linux" {
  73. l.Warn("tun.staged is only supported on Linux; packet processing stays inline")
  74. return stagedConfig{}
  75. }
  76. sc.tx, sc.rx = c.GetBool("tun.staged.tx", true), true
  77. // 256 is the measured crossover, 0 hands every batch to the workers
  78. sc.inlineBelow = max(c.GetInt("tun.staged.inline_below", 256), 0)
  79. if sc.tx && noiseutil.EncryptLockNeeded {
  80. l.Warn("tun.staged cannot encrypt in parallel with boringcrypto or fips140; transmit stays inline")
  81. sc.tx = false
  82. }
  83. sc.gateHold = max(c.GetInt("tun.staged.gate_hold", stagedGateHold), 1)
  84. sc.txSlots, sc.rxSlots = stagedSlots(sc.workers, c.GetInt("listen.batch", 64),
  85. c.GetInt("tun.staged.tx_slots", 0), c.GetInt("tun.staged.rx_slots", 0))
  86. l.Info("Staged packet processing enabled", "workers", sc.workers, "tx", sc.tx, "rx", sc.rx,
  87. "inlineBelow", sc.inlineBelow, "gateHold", sc.gateHold,
  88. "txSlots", sc.txSlots, "rxSlots", sc.rxSlots)
  89. return sc
  90. }
  91. // flowJob is one batch of crypto work. It owns its packet buffers until run returns; jobs and queues are reused.
  92. type flowJob interface{ run(nb []byte) }
  93. // flowPool runs one reader's crypto jobs. The reader is the only producer and closes the pool once its ordered stage
  94. // has drained.
  95. type flowPool struct {
  96. jobs chan flowJob
  97. wg sync.WaitGroup
  98. }
  99. func newFlowPool(workers, queueLen int) *flowPool {
  100. p := &flowPool{jobs: make(chan flowJob, queueLen)}
  101. for range workers {
  102. p.wg.Go(func() {
  103. nb := make([]byte, 12)
  104. for job := range p.jobs {
  105. job.run(nb)
  106. }
  107. })
  108. }
  109. return p
  110. }
  111. func (p *flowPool) close() { close(p.jobs); p.wg.Wait() }