cidr_set.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "encoding/binary"
  16. "fmt"
  17. "math/big"
  18. "net"
  19. "sync"
  20. )
  21. type cidrSet struct {
  22. sync.Mutex
  23. clusterCIDR *net.IPNet
  24. clusterIP net.IP
  25. clusterMaskSize int
  26. maxCIDRs int
  27. nextCandidate int
  28. used big.Int
  29. subNetMaskSize int
  30. }
  31. func newCIDRSet(clusterCIDR *net.IPNet, subNetMaskSize int) *cidrSet {
  32. clusterMask := clusterCIDR.Mask
  33. clusterMaskSize, _ := clusterMask.Size()
  34. maxCIDRs := 1 << uint32(subNetMaskSize-clusterMaskSize)
  35. return &cidrSet{
  36. clusterCIDR: clusterCIDR,
  37. clusterIP: clusterCIDR.IP.To4(),
  38. clusterMaskSize: clusterMaskSize,
  39. maxCIDRs: maxCIDRs,
  40. subNetMaskSize: subNetMaskSize,
  41. }
  42. }
  43. func (s *cidrSet) allocateNext() (*net.IPNet, error) {
  44. s.Lock()
  45. defer s.Unlock()
  46. nextUnused := -1
  47. for i := 0; i < s.maxCIDRs; i++ {
  48. candidate := (i + s.nextCandidate) % s.maxCIDRs
  49. if s.used.Bit(candidate) == 0 {
  50. nextUnused = candidate
  51. break
  52. }
  53. }
  54. if nextUnused == -1 {
  55. return nil, errCIDRRangeNoCIDRsRemaining
  56. }
  57. s.nextCandidate = (nextUnused + 1) % s.maxCIDRs
  58. s.used.SetBit(&s.used, nextUnused, 1)
  59. j := uint32(nextUnused) << uint32(32-s.subNetMaskSize)
  60. ipInt := (binary.BigEndian.Uint32(s.clusterIP)) | j
  61. ip := make([]byte, 4)
  62. binary.BigEndian.PutUint32(ip, ipInt)
  63. return &net.IPNet{
  64. IP: ip,
  65. Mask: net.CIDRMask(s.subNetMaskSize, 32),
  66. }, nil
  67. }
  68. func (s *cidrSet) getBeginingAndEndIndices(cidr *net.IPNet) (begin, end int, err error) {
  69. begin, end = 0, s.maxCIDRs
  70. cidrMask := cidr.Mask
  71. maskSize, _ := cidrMask.Size()
  72. if !s.clusterCIDR.Contains(cidr.IP.Mask(s.clusterCIDR.Mask)) && !cidr.Contains(s.clusterCIDR.IP.Mask(cidr.Mask)) {
  73. return -1, -1, fmt.Errorf("cidr %v is out the range of cluster cidr %v", cidr, s.clusterCIDR)
  74. }
  75. if s.clusterMaskSize < maskSize {
  76. subNetMask := net.CIDRMask(s.subNetMaskSize, 32)
  77. begin, err = s.getIndexForCIDR(&net.IPNet{
  78. IP: cidr.IP.To4().Mask(subNetMask),
  79. Mask: subNetMask,
  80. })
  81. if err != nil {
  82. return -1, -1, err
  83. }
  84. ip := make([]byte, 4)
  85. ipInt := binary.BigEndian.Uint32(cidr.IP) | (^binary.BigEndian.Uint32(cidr.Mask))
  86. binary.BigEndian.PutUint32(ip, ipInt)
  87. end, err = s.getIndexForCIDR(&net.IPNet{
  88. IP: net.IP(ip).To4().Mask(subNetMask),
  89. Mask: subNetMask,
  90. })
  91. if err != nil {
  92. return -1, -1, err
  93. }
  94. }
  95. return begin, end, nil
  96. }
  97. func (s *cidrSet) release(cidr *net.IPNet) error {
  98. begin, end, err := s.getBeginingAndEndIndices(cidr)
  99. if err != nil {
  100. return err
  101. }
  102. s.Lock()
  103. defer s.Unlock()
  104. for i := begin; i <= end; i++ {
  105. s.used.SetBit(&s.used, i, 0)
  106. }
  107. return nil
  108. }
  109. func (s *cidrSet) occupy(cidr *net.IPNet) (err error) {
  110. begin, end, err := s.getBeginingAndEndIndices(cidr)
  111. if err != nil {
  112. return err
  113. }
  114. s.Lock()
  115. defer s.Unlock()
  116. for i := begin; i <= end; i++ {
  117. s.used.SetBit(&s.used, i, 1)
  118. }
  119. return nil
  120. }
  121. func (s *cidrSet) getIndexForCIDR(cidr *net.IPNet) (int, error) {
  122. cidrIndex := (binary.BigEndian.Uint32(s.clusterIP) ^ binary.BigEndian.Uint32(cidr.IP.To4())) >> uint32(32-s.subNetMaskSize)
  123. if cidrIndex >= uint32(s.maxCIDRs) {
  124. return 0, fmt.Errorf("CIDR: %v is out of the range of CIDR allocator", cidr)
  125. }
  126. return int(cidrIndex), nil
  127. }