|
| 1 | +// Copyright 2019-2024 The Liqo 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 | + |
| 15 | +package ipamcore |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net/netip" |
| 20 | + "slices" |
| 21 | +) |
| 22 | + |
| 23 | +// Ipam represents the IPAM core structure. |
| 24 | +type Ipam struct { |
| 25 | + roots []node |
| 26 | +} |
| 27 | + |
| 28 | +// NewIpam creates a new IPAM instance. |
| 29 | +func NewIpam(roots []string) (*Ipam, error) { |
| 30 | + ipamRootsPrefixes := make([]netip.Prefix, len(roots)) |
| 31 | + for i, root := range roots { |
| 32 | + ipamRootsPrefixes[i] = netip.MustParsePrefix(root) |
| 33 | + } |
| 34 | + |
| 35 | + if err := checkRoots(ipamRootsPrefixes); err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + ipamRoots := make([]node, len(roots)) |
| 40 | + for i := range ipamRootsPrefixes { |
| 41 | + ipamRoots[i] = newNode(ipamRootsPrefixes[i]) |
| 42 | + } |
| 43 | + |
| 44 | + ipam := &Ipam{ |
| 45 | + roots: ipamRoots, |
| 46 | + } |
| 47 | + |
| 48 | + return ipam, nil |
| 49 | +} |
| 50 | + |
| 51 | +// NetworkAcquire allocates a network of the given size. |
| 52 | +// It returns the allocated network or nil if no network is available. |
| 53 | +func (ipam *Ipam) NetworkAcquire(size int) *netip.Prefix { |
| 54 | + for i := range ipam.roots { |
| 55 | + if result := allocateNetwork(size, &ipam.roots[i]); result != nil { |
| 56 | + return result |
| 57 | + } |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// NetworkAcquireWithPrefix allocates a network with the given prefix. |
| 63 | +// It returns the allocated network or nil if the network is not available. |
| 64 | +func (ipam *Ipam) NetworkAcquireWithPrefix(prefix netip.Prefix) *netip.Prefix { |
| 65 | + for i := range ipam.roots { |
| 66 | + if result := allocateNetworkWithPrefix(prefix, &ipam.roots[i]); result != nil { |
| 67 | + return result |
| 68 | + } |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// NetworkRelease frees the network with the given prefix. |
| 74 | +// It returns the freed network or nil if the network is not found. |
| 75 | +func (ipam *Ipam) NetworkRelease(prefix netip.Prefix) *netip.Prefix { |
| 76 | + for i := range ipam.roots { |
| 77 | + if isPrefixChildOf(ipam.roots[i].prefix, prefix) { |
| 78 | + if result := networkRelease(prefix, &ipam.roots[i]); result != nil { |
| 79 | + return result |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// ListNetworks returns the list of allocated networks. |
| 87 | +func (ipam *Ipam) ListNetworks() []netip.Prefix { |
| 88 | + var networks []netip.Prefix |
| 89 | + for i := range ipam.roots { |
| 90 | + networks = append(networks, listNetworks(&ipam.roots[i])...) |
| 91 | + } |
| 92 | + return networks |
| 93 | +} |
| 94 | + |
| 95 | +// NetworkIsAvailable checks if the network with the given prefix is allocated. |
| 96 | +// It returns false if the network is allocated or there is no suitable pool, true otherwise. |
| 97 | +func (ipam *Ipam) NetworkIsAvailable(prefix netip.Prefix) bool { |
| 98 | + node, err := ipam.search(prefix) |
| 99 | + if err != nil { |
| 100 | + return false |
| 101 | + } |
| 102 | + if node == nil { |
| 103 | + return true |
| 104 | + } |
| 105 | + return !node.acquired |
| 106 | +} |
| 107 | + |
| 108 | +// IPAcquire allocates an IP address from the given prefix. |
| 109 | +// It returns the allocated IP address or nil if the IP address is not available. |
| 110 | +func (ipam *Ipam) IPAcquire(prefix netip.Prefix) (*netip.Addr, error) { |
| 111 | + node, err := ipam.search(prefix) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + if node != nil { |
| 116 | + return node.ipAcquire(), nil |
| 117 | + } |
| 118 | + |
| 119 | + return nil, nil |
| 120 | +} |
| 121 | + |
| 122 | +// IPAcquireWithAddr allocates the IP address from the given prefix. |
| 123 | +// It returns the allocated IP address or nil if the IP address is not available. |
| 124 | +func (ipam *Ipam) IPAcquireWithAddr(prefix netip.Prefix, addr netip.Addr) (*netip.Addr, error) { |
| 125 | + if !prefix.Contains(addr) { |
| 126 | + return nil, fmt.Errorf("address %s is not contained in prefix %s", addr, prefix) |
| 127 | + } |
| 128 | + node, err := ipam.search(prefix) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + if node != nil { |
| 133 | + return node.allocateIPWithAddr(addr), nil |
| 134 | + } |
| 135 | + return nil, nil |
| 136 | +} |
| 137 | + |
| 138 | +// IPRelease frees the IP address from the given prefix. |
| 139 | +// It returns the freed IP address or nil if the IP address is not found. |
| 140 | +func (ipam *Ipam) IPRelease(prefix netip.Prefix, addr netip.Addr) (*netip.Addr, error) { |
| 141 | + node, err := ipam.search(prefix) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + if node != nil { |
| 146 | + return node.ipRelease(addr), nil |
| 147 | + } |
| 148 | + return nil, nil |
| 149 | +} |
| 150 | + |
| 151 | +// ListIPs returns the list of allocated IP addresses from the given prefix. |
| 152 | +func (ipam *Ipam) ListIPs(prefix netip.Prefix) ([]netip.Addr, error) { |
| 153 | + node, err := ipam.search(prefix) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + if node != nil { |
| 158 | + return slices.Clone(node.ips), nil |
| 159 | + } |
| 160 | + return nil, nil |
| 161 | +} |
| 162 | + |
| 163 | +// IsAllocatedIP checks if the IP address is allocated from the given prefix. |
| 164 | +// It returns true if the IP address is allocated, false otherwise. |
| 165 | +func (ipam *Ipam) IsAllocatedIP(prefix netip.Prefix, addr netip.Addr) (bool, error) { |
| 166 | + node, err := ipam.search(prefix) |
| 167 | + if err != nil { |
| 168 | + return false, err |
| 169 | + } |
| 170 | + if node != nil { |
| 171 | + return node.isAllocatedIP(addr), nil |
| 172 | + } |
| 173 | + return false, nil |
| 174 | +} |
| 175 | + |
| 176 | +// ToGraphviz generates the Graphviz representation of the IPAM structure. |
| 177 | +func (ipam *Ipam) ToGraphviz() error { |
| 178 | + for i := range ipam.roots { |
| 179 | + _ = i |
| 180 | + if err := ipam.roots[i].toGraphviz(); err != nil { |
| 181 | + return fmt.Errorf("failed to generate Graphviz representation: %w", err) |
| 182 | + } |
| 183 | + } |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func (ipam *Ipam) search(prefix netip.Prefix) (*node, error) { |
| 188 | + for i := range ipam.roots { |
| 189 | + if !isPrefixChildOf(ipam.roots[i].prefix, prefix) { |
| 190 | + continue |
| 191 | + } |
| 192 | + if node := search(prefix, &ipam.roots[i]); node != nil { |
| 193 | + return node, nil |
| 194 | + } |
| 195 | + return nil, nil |
| 196 | + } |
| 197 | + return nil, fmt.Errorf("prefix %s not contained in roots", prefix) |
| 198 | +} |
| 199 | + |
| 200 | +func checkRoots(roots []netip.Prefix) error { |
| 201 | + for i := range roots { |
| 202 | + if err := checkHostBitsZero(roots[i]); err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + } |
| 206 | + return nil |
| 207 | +} |
0 commit comments