| 1 | // Copyright 2015 CNI 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 disk |
| 16 | |
| 17 | import ( |
| 18 | "net" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "runtime" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/containernetworking/plugins/plugins/ipam/host-local/backend" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | lastIPFilePrefix = "last_reserved_ip." |
| 29 | LineBreak = "\r\n" |
| 30 | ) |
| 31 | |
| 32 | var defaultDataDir = "/var/lib/cni/networks" |
| 33 | |
| 34 | // Store is a simple disk-backed store that creates one file per IP |
| 35 | // address in a given directory. The contents of the file are the container ID. |
| 36 | type Store struct { |
| 37 | *FileLock |
| 38 | dataDir string |
| 39 | } |
| 40 | |
| 41 | // Store implements the Store interface |
| 42 | var _ backend.Store = &Store{} |
| 43 | |
| 44 | func New(network, dataDir string) (*Store, error) { |
| 45 | if dataDir == "" { |
| 46 | dataDir = defaultDataDir |
| 47 | } |
| 48 | dir := filepath.Join(dataDir, network) |
| 49 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | lk, err := NewFileLock(dir) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return &Store{lk, dir}, nil |
| 58 | } |
| 59 | |
| 60 | func (s *Store) Reserve(id string, ifname string, ip net.IP, rangeID string) (bool, error) { |
| 61 | fname := GetEscapedPath(s.dataDir, ip.String()) |
| 62 | |
| 63 | f, err := os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0o600) |
| 64 | if os.IsExist(err) { |
| 65 | return false, nil |
| 66 | } |
| 67 | if err != nil { |
| 68 | return false, err |
| 69 | } |
| 70 | if _, err := f.WriteString(strings.TrimSpace(id) + LineBreak + ifname); err != nil { |
| 71 | f.Close() |
| 72 | os.Remove(f.Name()) |
| 73 | return false, err |
| 74 | } |
| 75 | if err := f.Close(); err != nil { |
| 76 | os.Remove(f.Name()) |
| 77 | return false, err |
| 78 | } |
| 79 | // store the reserved ip in lastIPFile |
| 80 | ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID) |
| 81 | err = os.WriteFile(ipfile, []byte(ip.String()), 0o600) |
| 82 | if err != nil { |
| 83 | return false, err |
| 84 | } |
| 85 | return true, nil |
| 86 | } |
| 87 | |
| 88 | // LastReservedIP returns the last reserved IP if exists |
| 89 | func (s *Store) LastReservedIP(rangeID string) (net.IP, error) { |
| 90 | ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID) |
| 91 | data, err := os.ReadFile(ipfile) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | return net.ParseIP(string(data)), nil |
| 96 | } |
| 97 | |
| 98 | func (s *Store) FindByKey(match string) (bool, error) { |
| 99 | found := false |
| 100 | |
| 101 | err := filepath.Walk(s.dataDir, func(path string, info os.FileInfo, err error) error { |
| 102 | if err != nil || info.IsDir() { |
| 103 | return nil |
| 104 | } |
| 105 | data, err := os.ReadFile(path) |
| 106 | if err != nil { |
| 107 | return nil |
| 108 | } |
| 109 | if strings.TrimSpace(string(data)) == match { |
| 110 | found = true |
| 111 | } |
| 112 | return nil |
| 113 | }) |
| 114 | return found, err |
| 115 | } |
| 116 | |
| 117 | func (s *Store) FindByID(id string, ifname string) bool { |
| 118 | s.Lock() |
| 119 | defer s.Unlock() |
| 120 | |
| 121 | match := strings.TrimSpace(id) + LineBreak + ifname |
| 122 | found, err := s.FindByKey(match) |
| 123 | |
| 124 | // Match anything created by this id |
| 125 | if !found && err == nil { |
| 126 | match := strings.TrimSpace(id) |
| 127 | found, _ = s.FindByKey(match) |
| 128 | } |
| 129 | |
| 130 | return found |
| 131 | } |
| 132 | |
| 133 | func (s *Store) ReleaseByKey(match string) (bool, error) { |
| 134 | found := false |
| 135 | err := filepath.Walk(s.dataDir, func(path string, info os.FileInfo, err error) error { |
| 136 | if err != nil || info.IsDir() { |
| 137 | return nil |
| 138 | } |
| 139 | data, err := os.ReadFile(path) |
| 140 | if err != nil { |
| 141 | return nil |
| 142 | } |
| 143 | if strings.TrimSpace(string(data)) == match { |
| 144 | if err := os.Remove(path); err != nil { |
| 145 | return nil |
| 146 | } |
| 147 | found = true |
| 148 | } |
| 149 | return nil |
| 150 | }) |
| 151 | return found, err |
| 152 | } |
| 153 | |
| 154 | // N.B. This function eats errors to be tolerant and |
| 155 | // release as much as possible |
| 156 | func (s *Store) ReleaseByID(id string, ifname string) error { |
| 157 | match := strings.TrimSpace(id) + LineBreak + ifname |
| 158 | found, err := s.ReleaseByKey(match) |
| 159 | |
| 160 | // For backwards compatibility, look for files written by a previous version |
| 161 | if !found && err == nil { |
| 162 | match := strings.TrimSpace(id) |
| 163 | _, err = s.ReleaseByKey(match) |
| 164 | } |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | // GetByID returns the IPs which have been allocated to the specific ID |
| 169 | func (s *Store) GetByID(id string, ifname string) []net.IP { |
| 170 | var ips []net.IP |
| 171 | |
| 172 | match := strings.TrimSpace(id) + LineBreak + ifname |
| 173 | // matchOld for backwards compatibility |
| 174 | matchOld := strings.TrimSpace(id) |
| 175 | |
| 176 | // walk through all ips in this network to get the ones which belong to a specific ID |
| 177 | _ = filepath.Walk(s.dataDir, func(path string, info os.FileInfo, err error) error { |
| 178 | if err != nil || info.IsDir() { |
| 179 | return nil |
| 180 | } |
| 181 | data, err := os.ReadFile(path) |
| 182 | if err != nil { |
| 183 | return nil |
| 184 | } |
| 185 | if strings.TrimSpace(string(data)) == match || strings.TrimSpace(string(data)) == matchOld { |
| 186 | _, ipString := filepath.Split(path) |
| 187 | if ip := net.ParseIP(ipString); ip != nil { |
| 188 | ips = append(ips, ip) |
| 189 | } |
| 190 | } |
| 191 | return nil |
| 192 | }) |
| 193 | |
| 194 | return ips |
| 195 | } |
| 196 | |
| 197 | func GetEscapedPath(dataDir string, fname string) string { |
| 198 | if runtime.GOOS == "windows" { |
| 199 | fname = strings.ReplaceAll(fname, ":", "_") |
| 200 | } |
| 201 | return filepath.Join(dataDir, fname) |
| 202 | } |