network.py 6.1 KB
Newer Older
1
2
3
#
#

4
# Copyright (C) 2011, 2012 Google Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Ip address pool management functions.

"""

import ipaddr

from bitarray import bitarray

from ganeti import errors

32

33
34
35
36
37
38
39
class AddressPool(object):
  """Address pool class, wrapping an objects.Network object

  This class provides methods to manipulate address pools, backed by
  L{objects.Network} objects.

  """
40
41
42
  FREE = bitarray('0')
  RESERVED = bitarray('1')

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  def __init__(self, network):
    """Initialize a new IPv4 address pool from an objects.Network object

    @type network: L{objects.Network}
    @param network: the network object from which the pool will be generated

    """
    self.network = None
    self.gateway = None
    self.network6 = None
    self.gateway6 = None

    self.net = network

    self.network = ipaddr.IPNetwork(self.net.network)
    if self.net.gateway:
      self.gateway = ipaddr.IPAddress(self.net.gateway)

    if self.net.network6:
      self.network6 = ipaddr.IPv6Network(self.net.network6)
    if self.net.gateway6:
      self.gateway6 = ipaddr.IPv6Address(self.net.gateway6)

    if self.net.reservations:
      self.reservations = bitarray(self.net.reservations)
    else:
      self.reservations = bitarray(self.network.numhosts)
70
      # pylint: disable=E1103
71
72
73
74
75
76
      self.reservations.setall(False)

    if self.net.ext_reservations:
      self.ext_reservations = bitarray(self.net.ext_reservations)
    else:
      self.ext_reservations = bitarray(self.network.numhosts)
77
      # pylint: disable=E1103
78
79
80
81
82
      self.ext_reservations.setall(False)

    assert len(self.reservations) == self.network.numhosts
    assert len(self.ext_reservations) == self.network.numhosts

83
  def Contains(self, address):
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    if address is None:
      return False
    addr = ipaddr.IPAddress(address)

    return addr in self.network

  def _GetAddrIndex(self, address):
    addr = ipaddr.IPAddress(address)

    if not addr in self.network:
      raise errors.AddressPoolError("%s does not contain %s" %
                                    (self.network, addr))

    return int(addr) - int(self.network.network)

99
  def Update(self):
100
    """Write address pools back to the network object"""
101
    # pylint: disable=E1103
102
103
104
105
106
107
108
109
110
    self.net.ext_reservations = self.ext_reservations.to01()
    self.net.reservations = self.reservations.to01()

  def _Mark(self, address, value=True, external=False):
    idx = self._GetAddrIndex(address)
    if external:
      self.ext_reservations[idx] = value
    else:
      self.reservations[idx] = value
111
    self.Update()
112
113

  def _GetSize(self):
114
    return 2 ** (32 - self.network.prefixlen)
115
116
117
118
119
120
121
122
123
124

  @property
  def all_reservations(self):
    """Return a combined map of internal + external reservations."""
    return (self.reservations | self.ext_reservations)

  def Validate(self):
    assert self.net.family == 4
    assert len(self.reservations) == self._GetSize()
    assert len(self.ext_reservations) == self._GetSize()
125
126
    all_res = self.reservations & self.ext_reservations
    assert not all_res.any()
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

    if self.gateway is not None:
      assert self.net.family == self.gateway.version
      assert self.gateway in self.network

    if self.network6 and self.gateway6:
      assert self.gateway6 in self.network6

    return True

  def IsFull(self):
    """Check whether the network is full"""
    return self.all_reservations.all()

  def GetReservedCount(self):
    """Get the count of reserved addresses"""
    return self.all_reservations.count(True)

  def GetFreeCount(self):
    """Get the count of unused addresses"""
    return self.all_reservations.count(False)

  def GetMap(self):
    """Return a textual representation of the network's occupation status."""
    return self.all_reservations.to01().replace("1", "X").replace("0", ".")

  def IsReserved(self, address):
    """Checks if the given IP is reserved"""
    idx = self._GetAddrIndex(address)
    return self.all_reservations[idx]

  def Reserve(self, address, external=False):
    """Mark an address as used."""
    if self.IsReserved(address):
      raise errors.AddressPoolError("%s is already reserved" % address)
    self._Mark(address, external=external)

  def Release(self, address, external=False):
    """Release a given address reservation."""
    self._Mark(address, value=False, external=external)

  def GetFreeAddress(self):
    """Returns the first available address."""
    if self.IsFull():
      raise errors.AddressPoolError("%s is full" % self.network)

    idx = self.all_reservations.index(False)
    address = str(self.network[idx])
    self.Reserve(address)
    return address

  def GenerateFree(self):
179
180
181
182
183
184
    """Returns the first free address of the network if any or
       raises an error if it is full.

    """
    if self.IsFull():
      raise errors.AddressPoolError("%s is full" % self.network)
185
    idx = self.all_reservations.search(self.FREE, 1)
186
    return str(self.network[idx])
187
188
189

  def GetExternalReservations(self):
    """Returns a list of all externally reserved addresses"""
190
191
    # pylint: disable=E1103
    idxs = self.ext_reservations.search(self.RESERVED)
192
193
194
195
196
197
198
199
200
201
    return [str(self.network[idx]) for idx in idxs]

  @classmethod
  def InitializeNetwork(cls, net):
    """Initialize an L{objects.Network} object

    Reserve the network, broadcast and gateway IPs

    """
    obj = cls(net)
202
    obj.Update()
203
204
205
206
207
208
    for ip in [obj.network[0], obj.network[-1]]:
      obj.Reserve(ip, external=True)
    if obj.net.gateway is not None:
      obj.Reserve(obj.net.gateway, external=True)
    obj.Validate()
    return obj