config.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 {
  56. if cfg.SubnetLen > 0 {
  57. // SubnetLen needs to allow for a tunnel and bridge device on each host.
  58. if cfg.SubnetLen > 30 {
  59. return nil, errors.New("SubnetLen must be less than /31")
  60. }
  61. // SubnetLen needs to fit _more_ than twice into the Network.
  62. // the first subnet isn't used, so splitting into two one only provide one usable host.
  63. if cfg.SubnetLen < cfg.Network.PrefixLen+2 {
  64. return nil, errors.New("Network must be able to accommodate at least four subnets")
  65. }
  66. } else {
  67. // If the network is smaller than a /28 then the network isn't big enough for flannel so return an error.
  68. // Default to giving each host at least a /24 (as long as the network is big enough to support at least four hosts)
  69. // Otherwise, if the network is too small to give each host a /24 just split the network into four.
  70. if cfg.Network.PrefixLen > 28 {
  71. // Each subnet needs at least four addresses (/30) and the network needs to accommodate at least four
  72. // since the first subnet isn't used, so splitting into two would only provide one usable host.
  73. // So the min useful PrefixLen is /28
  74. return nil, errors.New("Network is too small. Minimum useful network prefix is /28")
  75. } else if cfg.Network.PrefixLen <= 22 {
  76. // Network is big enough to give each host a /24
  77. cfg.SubnetLen = 24
  78. } else {
  79. // Use +2 to provide four hosts per subnet.
  80. cfg.SubnetLen = cfg.Network.PrefixLen + 2
  81. }
  82. }
  83. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  84. if cfg.SubnetMin == ip.IP4(0) {
  85. // skip over the first subnet otherwise it causes problems. e.g.
  86. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  87. // conflicts with the broadcast address.
  88. cfg.SubnetMin = cfg.Network.IP + subnetSize
  89. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  90. return nil, errors.New("SubnetMin is not in the range of the Network")
  91. }
  92. if cfg.SubnetMax == ip.IP4(0) {
  93. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  94. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  95. return nil, errors.New("SubnetMax is not in the range of the Network")
  96. }
  97. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  98. mask := ip.IP4(0xFFFFFFFF << (32 - cfg.SubnetLen))
  99. if cfg.SubnetMin != cfg.SubnetMin&mask {
  100. return nil, fmt.Errorf("SubnetMin is not on a SubnetLen boundary: %v", cfg.SubnetMin)
  101. }
  102. if cfg.SubnetMax != cfg.SubnetMax&mask {
  103. return nil, fmt.Errorf("SubnetMax is not on a SubnetLen boundary: %v", cfg.SubnetMax)
  104. }
  105. }
  106. if cfg.EnableIPv6 {
  107. if cfg.IPv6SubnetLen > 0 {
  108. // SubnetLen needs to allow for a tunnel and bridge device on each host.
  109. if cfg.IPv6SubnetLen > 126 {
  110. return nil, errors.New("SubnetLen must be less than /127")
  111. }
  112. // SubnetLen needs to fit _more_ than twice into the Network.
  113. // the first subnet isn't used, so splitting into two one only provide one usable host.
  114. if cfg.IPv6SubnetLen < cfg.IPv6Network.PrefixLen+2 {
  115. return nil, errors.New("Network must be able to accommodate at least four subnets")
  116. }
  117. } else {
  118. // If the network is smaller than a /124 then the network isn't big enough for flannel so return an error.
  119. // Default to giving each host at least a /64 (as long as the network is big enough to support at least four hosts)
  120. // Otherwise, if the network is too small to give each host a /64 just split the network into four.
  121. if cfg.IPv6Network.PrefixLen > 124 {
  122. // Each subnet needs at least four addresses (/126) and the network needs to accommodate at least four
  123. // since the first subnet isn't used, so splitting into two would only provide one usable host.
  124. // So the min useful PrefixLen is /124
  125. return nil, errors.New("IPv6Network is too small. Minimum useful network prefix is /124")
  126. } else if cfg.IPv6Network.PrefixLen <= 62 {
  127. // Network is big enough to give each host a /64
  128. cfg.IPv6SubnetLen = 64
  129. } else {
  130. // Use +2 to provide four hosts per subnet.
  131. cfg.IPv6SubnetLen = cfg.IPv6Network.PrefixLen + 2
  132. }
  133. }
  134. ipv6SubnetSize := big.NewInt(0).Lsh(big.NewInt(1), 128-cfg.IPv6SubnetLen)
  135. if ip.IsEmpty(cfg.IPv6SubnetMin) {
  136. // skip over the first subnet otherwise it causes problems. e.g.
  137. // if Network is fc00::/48, having an interface with fc00::
  138. // conflicts with the broadcast address.
  139. cfg.IPv6SubnetMin = ip.GetIPv6SubnetMin(cfg.IPv6Network.IP, ipv6SubnetSize)
  140. } else if !cfg.IPv6Network.Contains(cfg.IPv6SubnetMin) {
  141. return nil, errors.New("IPv6SubnetMin is not in the range of the IPv6Network")
  142. }
  143. if ip.IsEmpty(cfg.IPv6SubnetMax) {
  144. cfg.IPv6SubnetMax = ip.GetIPv6SubnetMax(cfg.IPv6Network.Next().IP, ipv6SubnetSize)
  145. } else if !cfg.IPv6Network.Contains(cfg.IPv6SubnetMax) {
  146. return nil, errors.New("IPv6SubnetMax is not in the range of the IPv6Network")
  147. }
  148. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  149. mask := ip.Mask(int(cfg.IPv6SubnetLen))
  150. if !ip.CheckIPv6Subnet(cfg.IPv6SubnetMin, mask) {
  151. return nil, fmt.Errorf("IPv6SubnetMin is not on a SubnetLen boundary: %v", cfg.IPv6SubnetMin)
  152. }
  153. if !ip.CheckIPv6Subnet(cfg.IPv6SubnetMax, mask) {
  154. return nil, fmt.Errorf("IPv6SubnetMax is not on a SubnetLen boundary: %v", cfg.IPv6SubnetMax)
  155. }
  156. }
  157. bt, err := parseBackendType(cfg.Backend)
  158. if err != nil {
  159. return nil, err
  160. }
  161. cfg.BackendType = bt
  162. return cfg, nil
  163. }