David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 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. |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 16 | import ( |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 17 | "bytes" |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 18 | "math" |
| 19 | "net" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 24 | type Storage interface { |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 25 | Init(networkIn string, low string, high string) error |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 26 | Get(mac string) (string, error) |
| 27 | GetAll() map[string]string |
| 28 | Put(mac, ip string) error |
| 29 | Remove(mac string) (string, error) |
| 30 | Dequeue() (string, error) |
| 31 | Enqueue(ip string) error |
| 32 | } |
| 33 | |
| 34 | type MemoryStorage struct { |
| 35 | allocated map[string]IPv4 |
| 36 | available []IPv4 |
| 37 | readIdx, writeIdx, size uint |
| 38 | } |
| 39 | |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 40 | func inIPRange(from net.IP, to net.IP, test net.IP) bool { |
| 41 | if from == nil || to == nil || test == nil { |
| 42 | return false |
| 43 | } |
| 44 | |
| 45 | from16 := from.To16() |
| 46 | to16 := to.To16() |
| 47 | test16 := test.To16() |
| 48 | if from16 == nil || to16 == nil || test16 == nil { |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | if bytes.Compare(test16, from16) >= 0 && bytes.Compare(test16, to16) <= 0 { |
| 53 | return true |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | func (s *MemoryStorage) Init(networkIn string, low string, high string) error { |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 59 | _, network, err := net.ParseCIDR(networkIn) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 60 | if err != nil { |
| 61 | return err |
| 62 | } |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 63 | start, _, err := net.ParseCIDR(network.String()) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | parts := strings.Split(network.String(), "/") |
| 69 | ip, err := ParseIP(start.String()) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | bits, err := strconv.Atoi(parts[1]) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 77 | hostCount := int(math.Pow(2, float64(32-bits))) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 78 | s.readIdx = 0 |
| 79 | s.writeIdx = 0 |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 80 | s.size = uint(hostCount) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 81 | s.allocated = make(map[string]IPv4) |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 82 | s.available = make([]IPv4, 0, hostCount) |
| 83 | ipLow := net.ParseIP(low) |
| 84 | ipHigh := net.ParseIP(high) |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 85 | for i := 0; i < hostCount; i += 1 { |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 86 | if inIPRange(ipLow, ipHigh, net.ParseIP(ip.String())) { |
| 87 | s.available = append(s.available, ip) |
| 88 | } |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 89 | ip, err = ip.Next() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | } |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame] | 94 | log.Debugf("AVAILABLE: %+v\n", s.available) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (s *MemoryStorage) Get(mac string) (string, error) { |
| 99 | ip, ok := s.allocated[mac] |
| 100 | if !ok { |
| 101 | return "", nil |
| 102 | } |
| 103 | return ip.String(), nil |
| 104 | } |
| 105 | |
| 106 | func (s *MemoryStorage) GetAll() map[string]string { |
| 107 | all := make(map[string]string) |
| 108 | for k, v := range s.allocated { |
| 109 | all[k] = v.String() |
| 110 | } |
| 111 | return all |
| 112 | } |
| 113 | |
| 114 | func (s *MemoryStorage) Put(mac, ip string) error { |
| 115 | data, err := ParseIP(ip) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | s.allocated[mac] = data |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | func (s *MemoryStorage) Remove(mac string) (string, error) { |
| 124 | ip, ok := s.allocated[mac] |
| 125 | if !ok { |
| 126 | return "", nil |
| 127 | } |
| 128 | delete(s.allocated, mac) |
| 129 | return ip.String(), nil |
| 130 | } |
| 131 | |
| 132 | func (s *MemoryStorage) Dequeue() (string, error) { |
| 133 | ip := s.available[s.readIdx] |
| 134 | s.readIdx = (s.readIdx + 1) % s.size |
| 135 | return ip.String(), nil |
| 136 | } |
| 137 | |
| 138 | func (s *MemoryStorage) Enqueue(ip string) error { |
| 139 | data, err := ParseIP(ip) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | s.available[s.writeIdx] = data |
| 144 | s.writeIdx = (s.writeIdx + 1) % s.size |
| 145 | return nil |
| 146 | } |