config.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package subnet
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "math/big"
  20. "github.com/flannel-io/flannel/pkg/ip"
  21. )
  22. type Config struct {
  23. EnableIPv4 bool
  24. EnableIPv6 bool
  25. Network ip.IP4Net
  26. IPv6Network ip.IP6Net
  27. SubnetMin ip.IP4
  28. SubnetMax ip.IP4
  29. IPv6SubnetMin *ip.IP6
  30. IPv6SubnetMax *ip.IP6
  31. SubnetLen uint
  32. IPv6SubnetLen uint
  33. BackendType string `json:"-"`
  34. Backend json.RawMessage `json:",omitempty"`
  35. }
  36. func parseBackendType(be json.RawMessage) (string, error) {
  37. var bt struct {
  38. Type string
  39. }
  40. if len(be) == 0 {
  41. return "udp", nil
  42. } else if err := json.Unmarshal(be, &bt); err != nil {
  43. return "", fmt.Errorf("error decoding Backend property of config: %v", err)
  44. }
  45. return bt.Type, nil
  46. }
  47. func ParseConfig(s string) (*Config, error) {
  48. cfg := new(Config)
  49. // Enable ipv4 by default
  50. cfg.EnableIPv4 = true
  51. err := json.Unmarshal([]byte(s), cfg)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if !cfg.EnableIPv4 && !cfg.EnableIPv6 {
  56. return nil, fmt.Errorf("EnableIPv4 or EnableIPv6 option must be enabled one at least")
  57. }
  58. if cfg.EnableIPv4 {
  59. if cfg.SubnetLen > 0 {
  60. // SubnetLen needs to allow for a tunnel and bridge device on each host.
  61. if cfg.SubnetLen > 30 {
  62. return nil, errors.New("SubnetLen must be less than /31")
  63. }
  64. // SubnetLen needs to fit _more_ than twice into the Network.
  65. // the first subnet isn't used, so splitting into two one only provide one usable host.
  66. if cfg.SubnetLen < cfg.Network.PrefixLen+2 {
  67. return nil, errors.New("Network must be able to accommodate at least four subnets")
  68. }
  69. } else {
  70. // If the network is smaller than a /28 then the network isn't big enough for flannel so return an error.
  71. // Default to giving each host at least a /24 (as long as the network is big enough to support at least four hosts)
  72. // Otherwise, if the network is too small to give each host a /24 just split the network into four.
  73. if cfg.Network.PrefixLen > 28 {
  74. // Each subnet needs at least four addresses (/30) and the network needs to accommodate at least four
  75. // since the first subnet isn't used, so splitting into two would only provide one usable host.
  76. // So the min useful PrefixLen is /28
  77. return nil, errors.New("Network is too small. Minimum useful network prefix is /28")
  78. } else if cfg.Network.PrefixLen <= 22 {
  79. // Network is big enough to give each host a /24
  80. cfg.SubnetLen = 24
  81. } else {
  82. // Use +2 to provide four hosts per subnet.
  83. cfg.SubnetLen = cfg.Network.PrefixLen + 2
  84. }
  85. }
  86. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  87. if cfg.SubnetMin == ip.IP4(0) {
  88. // skip over the first subnet otherwise it causes problems. e.g.
  89. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  90. // conflicts with the broadcast address.
  91. cfg.SubnetMin = cfg.Network.IP + subnetSize
  92. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  93. return nil, errors.New("SubnetMin is not in the range of the Network")
  94. }
  95. if cfg.SubnetMax == ip.IP4(0) {
  96. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  97. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  98. return nil, errors.New("SubnetMax is not in the range of the Network")
  99. }
  100. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  101. mask := ip.IP4(0xFFFFFFFF << (32 - cfg.SubnetLen))
  102. if cfg.SubnetMin != cfg.SubnetMin&mask {
  103. return nil, fmt.Errorf("SubnetMin is not on a SubnetLen boundary: %v", cfg.SubnetMin)
  104. }
  105. if cfg.SubnetMax != cfg.SubnetMax&mask {
  106. return nil, fmt.Errorf("SubnetMax is not on a SubnetLen boundary: %v", cfg.SubnetMax)
  107. }
  108. }
  109. if cfg.EnableIPv6 {
  110. if cfg.IPv6SubnetLen > 0 {
  111. // SubnetLen needs to allow for a tunnel and bridge device on each host.
  112. if cfg.IPv6SubnetLen > 126 {
  113. return nil, errors.New("SubnetLen must be less than /127")
  114. }
  115. // SubnetLen needs to fit _more_ than twice into the Network.
  116. // the first subnet isn't used, so splitting into two one only provide one usable host.
  117. if cfg.IPv6SubnetLen < cfg.IPv6Network.PrefixLen+2 {
  118. return nil, errors.New("Network must be able to accommodate at least four subnets")
  119. }
  120. } else {
  121. // If the network is smaller than a /124 then the network isn't big enough for flannel so return an error.
  122. // Default to giving each host at least a /64 (as long as the network is big enough to support at least four hosts)
  123. // Otherwise, if the network is too small to give each host a /64 just split the network into four.
  124. if cfg.IPv6Network.PrefixLen > 124 {
  125. // Each subnet needs at least four addresses (/126) and the network needs to accommodate at least four
  126. // since the first subnet isn't used, so splitting into two would only provide one usable host.
  127. // So the min useful PrefixLen is /124
  128. return nil, errors.New("IPv6Network is too small. Minimum useful network prefix is /124")
  129. } else if cfg.IPv6Network.PrefixLen <= 62 {
  130. // Network is big enough to give each host a /64
  131. cfg.IPv6SubnetLen = 64
  132. } else {
  133. // Use +2 to provide four hosts per subnet.
  134. cfg.IPv6SubnetLen = cfg.IPv6Network.PrefixLen + 2
  135. }
  136. }
  137. ipv6SubnetSize := big.NewInt(0).Lsh(big.NewInt(1), 128-cfg.IPv6SubnetLen)
  138. if ip.IsEmpty(cfg.IPv6SubnetMin) {
  139. // skip over the first subnet otherwise it causes problems. e.g.
  140. // if Network is fc00::/48, having an interface with fc00::
  141. // conflicts with the broadcast address.
  142. cfg.IPv6SubnetMin = ip.GetIPv6SubnetMin(cfg.IPv6Network.IP, ipv6SubnetSize)
  143. } else if !cfg.IPv6Network.Contains(cfg.IPv6SubnetMin) {
  144. return nil, errors.New("IPv6SubnetMin is not in the range of the IPv6Network")
  145. }
  146. if ip.IsEmpty(cfg.IPv6SubnetMax) {
  147. cfg.IPv6SubnetMax = ip.GetIPv6SubnetMax(cfg.IPv6Network.Next().IP, ipv6SubnetSize)
  148. } else if !cfg.IPv6Network.Contains(cfg.IPv6SubnetMax) {
  149. return nil, errors.New("IPv6SubnetMax is not in the range of the IPv6Network")
  150. }
  151. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  152. mask := ip.Mask(int(cfg.IPv6SubnetLen))
  153. if !ip.CheckIPv6Subnet(cfg.IPv6SubnetMin, mask) {
  154. return nil, fmt.Errorf("IPv6SubnetMin is not on a SubnetLen boundary: %v", cfg.IPv6SubnetMin)
  155. }
  156. if !ip.CheckIPv6Subnet(cfg.IPv6SubnetMax, mask) {
  157. return nil, fmt.Errorf("IPv6SubnetMax is not on a SubnetLen boundary: %v", cfg.IPv6SubnetMax)
  158. }
  159. }
  160. bt, err := parseBackendType(cfg.Backend)
  161. if err != nil {
  162. return nil, err
  163. }
  164. cfg.BackendType = bt
  165. return cfg, nil
  166. }