controller_linux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package libnetwork
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "github.com/containerd/log"
  7. "github.com/docker/docker/libnetwork/iptables"
  8. "github.com/docker/docker/libnetwork/netlabel"
  9. "github.com/docker/docker/libnetwork/options"
  10. "github.com/docker/docker/libnetwork/osl"
  11. )
  12. // enabledIptablesVersions returns the iptables versions that are enabled
  13. // for the controller.
  14. func (c *Controller) enabledIptablesVersions() []iptables.IPVersion {
  15. c.mu.Lock()
  16. defer c.mu.Unlock()
  17. if c.cfg == nil {
  18. return nil
  19. }
  20. // parse map cfg["bridge"]["generic"]["EnableIPTable"]
  21. cfgBridge := c.cfg.DriverConfig("bridge")
  22. cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
  23. if !ok {
  24. return nil
  25. }
  26. var versions []iptables.IPVersion
  27. if enabled, ok := cfgGeneric["EnableIPTables"].(bool); enabled || !ok {
  28. // iptables is enabled unless user explicitly disabled it
  29. versions = append(versions, iptables.IPv4)
  30. }
  31. if enabled, _ := cfgGeneric["EnableIP6Tables"].(bool); enabled {
  32. versions = append(versions, iptables.IPv6)
  33. }
  34. return versions
  35. }
  36. // getDefaultOSLSandbox returns the controller's default [osl.Sandbox]. It
  37. // creates the sandbox if it does not yet exist.
  38. func (c *Controller) getDefaultOSLSandbox(key string) (*osl.Namespace, error) {
  39. var err error
  40. c.defOsSboxOnce.Do(func() {
  41. c.defOsSbox, err = osl.NewSandbox(key, false, false)
  42. })
  43. if err != nil {
  44. c.defOsSboxOnce = sync.Once{}
  45. return nil, fmt.Errorf("failed to create default sandbox: %v", err)
  46. }
  47. return c.defOsSbox, nil
  48. }
  49. // setupOSLSandbox sets the sandbox [osl.Sandbox], and applies operating-
  50. // specific configuration.
  51. //
  52. // Depending on the Sandbox settings, it may either use the Controller's
  53. // default sandbox, or configure a new one.
  54. func (c *Controller) setupOSLSandbox(sb *Sandbox) error {
  55. if sb.config.useDefaultSandBox {
  56. defSB, err := c.getDefaultOSLSandbox(sb.Key())
  57. if err != nil {
  58. return err
  59. }
  60. sb.osSbox = defSB
  61. }
  62. if sb.osSbox == nil && !sb.config.useExternalKey {
  63. newSB, err := osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox, false)
  64. if err != nil {
  65. return fmt.Errorf("failed to create new osl sandbox: %v", err)
  66. }
  67. sb.osSbox = newSB
  68. }
  69. if sb.osSbox != nil {
  70. // Apply operating specific knobs on the load balancer sandbox
  71. err := sb.osSbox.InvokeFunc(func() {
  72. sb.osSbox.ApplyOSTweaks(sb.oslTypes)
  73. })
  74. if err != nil {
  75. log.G(context.TODO()).Errorf("Failed to apply performance tuning sysctls to the sandbox: %v", err)
  76. }
  77. // Keep this just so performance is not changed
  78. sb.osSbox.ApplyOSTweaks(sb.oslTypes)
  79. }
  80. return nil
  81. }