Include hvparams in ssconf files
[ganeti-local] / lib / network.py
index cc3bfd6..d78b717 100644 (file)
@@ -30,6 +30,21 @@ from bitarray import bitarray
 from ganeti import errors
 
 
+def _ComputeIpv4NumHosts(network_size):
+  """Derives the number of hosts in an IPv4 network from the size.
+
+  """
+  return 2 ** (32 - network_size)
+
+
+IPV4_NETWORK_MIN_SIZE = 30
+# FIXME: This limit is for performance reasons. Remove when refactoring
+# for performance tuning was successful.
+IPV4_NETWORK_MAX_SIZE = 16
+IPV4_NETWORK_MIN_NUM_HOSTS = _ComputeIpv4NumHosts(IPV4_NETWORK_MIN_SIZE)
+IPV4_NETWORK_MAX_NUM_HOSTS = _ComputeIpv4NumHosts(IPV4_NETWORK_MAX_SIZE)
+
+
 class AddressPool(object):
   """Address pool class, wrapping an C{objects.Network} object.
 
@@ -55,6 +70,19 @@ class AddressPool(object):
     self.net = network
 
     self.network = ipaddr.IPNetwork(self.net.network)
+    if self.network.numhosts > IPV4_NETWORK_MAX_NUM_HOSTS:
+      raise errors.AddressPoolError("A big network with %s host(s) is currently"
+                                    " not supported. please specify at most a"
+                                    " /%s network" %
+                                    (str(self.network.numhosts),
+                                     IPV4_NETWORK_MAX_SIZE))
+
+    if self.network.numhosts < IPV4_NETWORK_MIN_NUM_HOSTS:
+      raise errors.AddressPoolError("A network with only %s host(s) is too"
+                                    " small, please specify at least a /%s"
+                                    " network" %
+                                    (str(self.network.numhosts),
+                                     IPV4_NETWORK_MIN_SIZE))
     if self.net.gateway:
       self.gateway = ipaddr.IPAddress(self.net.gateway)
 
@@ -123,14 +151,12 @@ class AddressPool(object):
     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()
     all_res = self.reservations & self.ext_reservations
     assert not all_res.any()
 
     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: