hostconfig_unix.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // +build !windows,!solaris
  2. package runconfig
  3. import (
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/pkg/sysinfo"
  9. )
  10. // DefaultDaemonNetworkMode returns the default network stack the daemon should
  11. // use.
  12. func DefaultDaemonNetworkMode() container.NetworkMode {
  13. return container.NetworkMode("bridge")
  14. }
  15. // IsPreDefinedNetwork indicates if a network is predefined by the daemon
  16. func IsPreDefinedNetwork(network string) bool {
  17. n := container.NetworkMode(network)
  18. return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault() || network == "ingress"
  19. }
  20. // ValidateNetMode ensures that the various combinations of requested
  21. // network settings are valid.
  22. func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
  23. // We may not be passed a host config, such as in the case of docker commit
  24. if hc == nil {
  25. return nil
  26. }
  27. parts := strings.Split(string(hc.NetworkMode), ":")
  28. if parts[0] == "container" {
  29. if len(parts) < 2 || parts[1] == "" {
  30. return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
  31. }
  32. }
  33. if hc.NetworkMode.IsContainer() && c.Hostname != "" {
  34. return ErrConflictNetworkHostname
  35. }
  36. if hc.UTSMode.IsHost() && c.Hostname != "" {
  37. return ErrConflictUTSHostname
  38. }
  39. if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
  40. return ErrConflictHostNetworkAndLinks
  41. }
  42. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  43. return ErrConflictContainerNetworkAndLinks
  44. }
  45. if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
  46. return ErrConflictNetworkAndDNS
  47. }
  48. if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
  49. return ErrConflictNetworkHosts
  50. }
  51. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
  52. return ErrConflictContainerNetworkAndMac
  53. }
  54. if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
  55. return ErrConflictNetworkPublishPorts
  56. }
  57. if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
  58. return ErrConflictNetworkExposePorts
  59. }
  60. return nil
  61. }
  62. // ValidateIsolation performs platform specific validation of
  63. // isolation in the hostconfig structure. Linux only supports "default"
  64. // which is LXC container isolation
  65. func ValidateIsolation(hc *container.HostConfig) error {
  66. // We may not be passed a host config, such as in the case of docker commit
  67. if hc == nil {
  68. return nil
  69. }
  70. if !hc.Isolation.IsValid() {
  71. return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
  72. }
  73. return nil
  74. }
  75. // ValidateQoS performs platform specific validation of the QoS settings
  76. func ValidateQoS(hc *container.HostConfig) error {
  77. // We may not be passed a host config, such as in the case of docker commit
  78. if hc == nil {
  79. return nil
  80. }
  81. if hc.IOMaximumBandwidth != 0 {
  82. return fmt.Errorf("invalid QoS settings: %s does not support --io-maxbandwidth", runtime.GOOS)
  83. }
  84. if hc.IOMaximumIOps != 0 {
  85. return fmt.Errorf("invalid QoS settings: %s does not support --io-maxiops", runtime.GOOS)
  86. }
  87. return nil
  88. }
  89. // ValidateResources performs platform specific validation of the resource settings
  90. // cpu-rt-runtime and cpu-rt-period can not be greater than their parent, cpu-rt-runtime requires sys_nice
  91. func ValidateResources(hc *container.HostConfig, si *sysinfo.SysInfo) error {
  92. // We may not be passed a host config, such as in the case of docker commit
  93. if hc == nil {
  94. return nil
  95. }
  96. if hc.Resources.CPURealtimePeriod > 0 && !si.CPURealtimePeriod {
  97. return fmt.Errorf("invalid --cpu-rt-period: Your kernel does not support cgroup rt period")
  98. }
  99. if hc.Resources.CPURealtimeRuntime > 0 && !si.CPURealtimeRuntime {
  100. return fmt.Errorf("invalid --cpu-rt-runtime: Your kernel does not support cgroup rt runtime")
  101. }
  102. if hc.Resources.CPURealtimePeriod != 0 && hc.Resources.CPURealtimeRuntime != 0 && hc.Resources.CPURealtimeRuntime > hc.Resources.CPURealtimePeriod {
  103. return fmt.Errorf("invalid --cpu-rt-runtime: rt runtime cannot be higher than rt period")
  104. }
  105. return nil
  106. }