blob: 083255808be15a98af57bfa544cfca62eed3200b [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// 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. Bainbridge8bc905c2016-05-31 14:07:10 -070014package main
15
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070016import (
David K. Bainbridgeca68f062016-10-27 11:04:33 -070017 "bytes"
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070018 "math"
19 "net"
20 "strconv"
21 "strings"
22)
23
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070024type Storage interface {
David K. Bainbridgeca68f062016-10-27 11:04:33 -070025 Init(networkIn string, low string, high string) error
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070026 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
34type MemoryStorage struct {
35 allocated map[string]IPv4
36 available []IPv4
37 readIdx, writeIdx, size uint
38}
39
David K. Bainbridgeca68f062016-10-27 11:04:33 -070040func 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
58func (s *MemoryStorage) Init(networkIn string, low string, high string) error {
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070059 _, network, err := net.ParseCIDR(networkIn)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070060 if err != nil {
61 return err
62 }
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070063 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. Bainbridgeca68f062016-10-27 11:04:33 -070077 hostCount := int(math.Pow(2, float64(32-bits)))
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070078 s.readIdx = 0
79 s.writeIdx = 0
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070080 s.size = uint(hostCount)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070081 s.allocated = make(map[string]IPv4)
David K. Bainbridgeca68f062016-10-27 11:04:33 -070082 s.available = make([]IPv4, 0, hostCount)
83 ipLow := net.ParseIP(low)
84 ipHigh := net.ParseIP(high)
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070085 for i := 0; i < hostCount; i += 1 {
David K. Bainbridgeca68f062016-10-27 11:04:33 -070086 if inIPRange(ipLow, ipHigh, net.ParseIP(ip.String())) {
87 s.available = append(s.available, ip)
88 }
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070089 ip, err = ip.Next()
90 if err != nil {
91 return err
92 }
93 }
David K. Bainbridgeca68f062016-10-27 11:04:33 -070094 log.Debugf("AVAILABLE: %+v\n", s.available)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070095 return nil
96}
97
98func (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
106func (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
114func (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
123func (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
132func (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
138func (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}