flow.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //
  19. // Metrics are registered only for active pipelines. flow.{tx,rx}.batches counts reads handed to the ordered stage and
  20. // flow.{tx,rx}.inline those the reader handled itself (TX inline also counts oversized reads); their rates show
  21. // whether staging is used. flow.tx.gate_switches counts transmit switches between inline and fanout mode.
  22. // stagedConfig is tun.staged. workers is per routine and direction; zero keeps processing inline.
  23. type stagedConfig struct {
  24. workers int
  25. tx bool
  26. rx bool
  27. // inlineBelow is the TX segment or RX packet count below which a read may skip the worker handoff, 0 never: a TX
  28. // read in inline mode, an RX read with nothing in flight
  29. inlineBelow int
  30. // txSlots and rxSlots are how many batches each routine can have in flight per direction
  31. txSlots, rxSlots int
  32. // gateHold is how many TX reads in a row must want the other mode before a routine switches between inline and
  33. // fanout transmit
  34. gateHold int
  35. }
  36. // stagedGateHold is the default tun.staged.gate_hold
  37. const stagedGateHold = 64
  38. const (
  39. // stagedTxSlots is the default TX slot count, unless the workers need more
  40. stagedTxSlots = 8
  41. // stagedRxEntries is how many recvmmsg entries the RX slots hold in flight by default
  42. stagedRxEntries = 6 * 64
  43. // stagedMinSlots lets the reader fill one batch while another is in flight
  44. stagedMinSlots = 2
  45. // stagedMinRxSlots keeps the default RX pipeline deep enough for large listen.batch values
  46. stagedMinRxSlots = 4
  47. // stagedMaxRxSlots is the measured default, a smaller listen.batch does not take more slots
  48. stagedMaxRxSlots = 6
  49. )
  50. // stagedSlots sizes the TX and RX slots. TX reads are bounded by the tun queue, so one slot per worker plus the one
  51. // being read and the one being sent, and at least the eight measured. RX slots keep stagedRxEntries in flight, four
  52. // 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.
  53. func stagedSlots(workers, batch, txOverride, rxOverride int) (tx, rx int) {
  54. tx = max(stagedTxSlots, workers+2)
  55. rx = min(max(stagedMinRxSlots, (stagedRxEntries+batch-1)/max(batch, 1)), stagedMaxRxSlots)
  56. if txOverride > 0 {
  57. tx = max(txOverride, stagedMinSlots)
  58. }
  59. if rxOverride > 0 {
  60. rx = max(rxOverride, stagedMinSlots)
  61. }
  62. return tx, rx
  63. }
  64. // parseStagedConfig enables staging on Linux only; queues and conns that cannot hand off batches also stay inline.
  65. func parseStagedConfig(c *config.C, l *slog.Logger) stagedConfig {
  66. sc := stagedConfig{workers: c.GetInt("tun.staged.workers", 0)}
  67. if sc.workers <= 0 {
  68. return stagedConfig{}
  69. }
  70. if runtime.GOOS != "linux" {
  71. l.Warn("tun.staged is only supported on Linux; packet processing stays inline")
  72. return stagedConfig{}
  73. }
  74. sc.tx, sc.rx = c.GetBool("tun.staged.tx", true), true
  75. // 256 is the measured crossover, 0 hands every batch to the workers
  76. sc.inlineBelow = max(c.GetInt("tun.staged.inline_below", 256), 0)
  77. if sc.tx && noiseutil.EncryptLockNeeded {
  78. l.Warn("tun.staged cannot encrypt in parallel with boringcrypto or fips140; transmit stays inline")
  79. sc.tx = false
  80. }
  81. sc.gateHold = max(c.GetInt("tun.staged.gate_hold", stagedGateHold), 1)
  82. sc.txSlots, sc.rxSlots = stagedSlots(sc.workers, c.GetInt("listen.batch", 64),
  83. c.GetInt("tun.staged.tx_slots", 0), c.GetInt("tun.staged.rx_slots", 0))
  84. l.Info("Staged packet processing enabled", "workers", sc.workers, "tx", sc.tx, "rx", sc.rx,
  85. "inlineBelow", sc.inlineBelow, "gateHold", sc.gateHold,
  86. "txSlots", sc.txSlots, "rxSlots", sc.rxSlots)
  87. return sc
  88. }
  89. // flowJob is one batch of crypto work. It owns its packet buffers until run returns; jobs and queues are reused.
  90. type flowJob interface{ run(nb []byte) }
  91. // flowPool runs one reader's crypto jobs. The reader is the only producer and closes the pool once its ordered stage
  92. // has drained.
  93. type flowPool struct {
  94. jobs chan flowJob
  95. wg sync.WaitGroup
  96. }
  97. func newFlowPool(workers, queueLen int) *flowPool {
  98. p := &flowPool{jobs: make(chan flowJob, queueLen)}
  99. for range workers {
  100. p.wg.Go(func() {
  101. nb := make([]byte, 12)
  102. for job := range p.jobs {
  103. job.run(nb)
  104. }
  105. })
  106. }
  107. return p
  108. }
  109. func (p *flowPool) close() { close(p.jobs); p.wg.Wait() }