From 06c056d3e45cfc49a2ce84aec3918af1ec171f84 Mon Sep 17 00:00:00 2001
From: Helga Velroyen <helgav@google.com>
Date: Wed, 10 Apr 2013 14:06:19 +0200
Subject: [PATCH] Limit the size of networks to /16

This patch introduces an upper limit to the size of the networks that can
be created.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/network.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/network.py b/lib/network.py
index 170a8b778..d78b71735 100644
--- a/lib/network.py
+++ b/lib/network.py
@@ -29,8 +29,20 @@ 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
-IPV4_NETWORK_MIN_NUM_HOSTS = 2 ** (32 - IPV4_NETWORK_MIN_SIZE)
+# 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):
@@ -58,6 +70,13 @@ 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"
-- 
GitLab