flow.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package nebula
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "runtime"
  6. "sync"
  7. "time"
  8. "github.com/rcrowley/go-metrics"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/noiseutil"
  11. )
  12. // Staged processing (tun.staged.workers) moves crypto off each tun reader and udp listener onto worker goroutines,
  13. // behind which an ordered stage sends or writes every batch in read order. It reuses the inline packet handlers:
  14. // staging changes ownership and scheduling, not policy. Each routine has its own workers and ordered stages, so order
  15. // is per routine. Full queues block the reader. A read error drains the batches in flight, while shutdown waits for
  16. // the crypto and then drops packets whose destination has closed. TX uses eight slots and RX six; at listen.batch=64
  17. // with GRO their buffers take about 9 MiB and 24 MiB per routine. 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.
  20. // flow.stage.<stage>.q<queue>.busy_ns locates the limiting stage: tx_read, tx_encrypt, tx_send, rx_read, rx_decrypt
  21. // or rx_write. Take rates; 1e9 ns/s fills a serial stage. These are elapsed times that exclude read syscalls and
  22. // downstream waits; descheduling inflates them and worker times add up across the pool. Small RX batches sent to the
  23. // writer include decryption in writer time.
  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. }
  30. // parseStagedConfig enables staging on Linux only; queues and conns that cannot hand off batches also stay inline.
  31. func parseStagedConfig(c *config.C, l *slog.Logger) stagedConfig {
  32. sc := stagedConfig{workers: c.GetInt("tun.staged.workers", 0)}
  33. if sc.workers <= 0 {
  34. return stagedConfig{}
  35. }
  36. if runtime.GOOS != "linux" {
  37. l.Warn("tun.staged is only supported on Linux; packet processing stays inline")
  38. return stagedConfig{}
  39. }
  40. sc.tx, sc.rx = true, true
  41. if noiseutil.EncryptLockNeeded {
  42. l.Warn("tun.staged cannot encrypt in parallel with boringcrypto or fips140; transmit stays inline")
  43. sc.tx = false
  44. }
  45. l.Info("Staged packet processing enabled", "workers", sc.workers, "tx", sc.tx, "rx", sc.rx)
  46. return sc
  47. }
  48. // flowJob is one batch of crypto work. It owns its packet buffers until run returns; jobs and queues are reused.
  49. type flowJob interface{ run(nb []byte) }
  50. // flowPool runs one reader's crypto jobs. The reader is the only producer and closes the pool once its ordered stage
  51. // has drained.
  52. type flowPool struct {
  53. jobs chan flowJob
  54. wg sync.WaitGroup
  55. }
  56. func newFlowPool(workers, queueLen int, busy metrics.Counter) *flowPool {
  57. p := &flowPool{jobs: make(chan flowJob, queueLen)}
  58. for range workers {
  59. p.wg.Go(func() {
  60. nb := make([]byte, 12)
  61. for job := range p.jobs {
  62. t := time.Now()
  63. job.run(nb)
  64. busy.Inc(int64(time.Since(t)))
  65. }
  66. })
  67. }
  68. return p
  69. }
  70. func (p *flowPool) close() { close(p.jobs); p.wg.Wait() }
  71. // stageBusy is the busy time counter of one stage of routine q.
  72. func stageBusy(stage string, q int) metrics.Counter {
  73. return metrics.GetOrRegisterCounter(fmt.Sprintf("flow.stage.%s.q%d.busy_ns", stage, q), nil)
  74. }