VLSM & Summarization
Splitting a block into equal subnets wastes addresses when the networks have very different sizes. A point-to-point link between two routers needs 2 addresses; a staff floor might need 200. VLSM (Variable Length Subnet Masking) gives each network a subnet that fits. Its mirror image, summarization (supernetting), combines many small routes into one big one so routing tables stay short.
Planning with VLSM
The method:
- Sort the networks by size, largest first.
- For each, pick the smallest prefix that fits (hosts + 2).
- Allocate it at the next free address that's aligned to its block size.
Allocating largest first guarantees every block lands on a proper boundary without gaps.
import ipaddress
import math
def vlsm(block, needs):
free = ipaddress.ip_network(block)
cursor = int(free.network_address)
plan = []
for name, hosts in sorted(needs.items(), key=lambda kv: -kv[1]):
size = 2 ** math.ceil(math.log2(hosts + 2))
cursor = (cursor + size - 1) // size * size # align to the block size
net = ipaddress.ip_network((cursor, 32 - int(math.log2(size))))
if not net.subnet_of(free):
raise ValueError(f"{block} is too small for {name}")
plan.append((name, hosts, net))
cursor += size
return plan
needs = {"Sales": 100, "Engineering": 50, "Guest Wi-Fi": 25, "Servers": 12,
"Link R1-R2": 2, "Link R1-R3": 2}
used = 0
for name, hosts, net in vlsm("192.168.50.0/24", needs):
used += net.num_addresses
print(f"{name:<12} {hosts:>3} hosts -> {str(net):<18} ({net.num_addresses - 2} usable)")
print(f"used {used} of 256 addresses")Sales 100 hosts -> 192.168.50.0/25 (126 usable)
Engineering 50 hosts -> 192.168.50.128/26 (62 usable)
Guest Wi-Fi 25 hosts -> 192.168.50.192/27 (30 usable)
Servers 12 hosts -> 192.168.50.224/28 (14 usable)
Link R1-R2 2 hosts -> 192.168.50.240/30 (2 usable)
Link R1-R3 2 hosts -> 192.168.50.244/30 (2 usable)
used 248 of 256 addresses
With equal /25 subnets we could only have made two networks. VLSM fits all six and still leaves room for growth.
Checking a plan for overlaps
A common mistake in hand-made plans is overlapping subnets. ipaddress can check it:
import ipaddress
from itertools import combinations
plan = {
"A": "10.10.0.0/23",
"B": "10.10.2.0/24",
"C": "10.10.2.128/25", # oops: inside B
"D": "10.10.4.0/22",
}
nets = {name: ipaddress.ip_network(cidr) for name, cidr in plan.items()}
problems = 0
for (a, na), (b, nb) in combinations(nets.items(), 2):
if na.overlaps(nb):
problems += 1
print(f"overlap: {a} {na} and {b} {nb}")
print("problems found:", problems)
try:
ipaddress.ip_network("10.10.3.0/23")
except ValueError as err:
print("ValueError:", err)overlap: B 10.10.2.0/24 and C 10.10.2.128/25
problems found: 1
ValueError: 10.10.3.0/23 has host bits set
The last line shows another classic error: 10.10.3.0/23 isn't a valid network, because a /23 must start at an even third octet. The host bits are not zero.
Summarization
If a router reaches 172.16.0.0/24, 172.16.1.0/24, 172.16.2.0/24 and 172.16.3.0/24 through the same neighbour, it can advertise one route, 172.16.0.0/22, instead of four. The summary is the longest prefix that all the networks share:
import ipaddress
routes = ["172.16.0.0/24", "172.16.1.0/24", "172.16.2.0/24", "172.16.3.0/24"]
nets = [ipaddress.ip_network(r) for r in routes]
print("collapsed:", list(ipaddress.collapse_addresses(nets)))
for net in nets:
third = int(net.network_address.packed[2])
print(f"{str(net):<15} third octet {third:08b}")
def summary(nets):
first, last = int(nets[0].network_address), int(nets[-1].broadcast_address)
prefix = 32
while (first >> (32 - prefix)) != (last >> (32 - prefix)):
prefix -= 1
return ipaddress.ip_network((first, prefix), strict=False)
print("summary:", summary(nets))collapsed: [IPv4Network('172.16.0.0/22')]
172.16.0.0/24 third octet 00000000
172.16.1.0/24 third octet 00000001
172.16.2.0/24 third octet 00000010
172.16.3.0/24 third octet 00000011
summary: 172.16.0.0/22
The first 22 bits are identical in all four networks, so the summary is a /22.
Careful: a summary can cover too much
If the networks aren't contiguous, the summary includes addresses you don't own or reach through that router. collapse_addresses only merges exactly; a manual summary can over-claim:
import ipaddress
nets = [ipaddress.ip_network(n) for n in ["10.1.4.0/24", "10.1.5.0/24", "10.1.7.0/24"]]
print("exact:", [str(n) for n in ipaddress.collapse_addresses(nets)])
supernet = ipaddress.ip_network("10.1.4.0/22")
missing = [str(s) for s in supernet.subnets(new_prefix=24) if s not in nets]
print("summary 10.1.4.0/22 also claims:", missing)exact: ['10.1.4.0/23', '10.1.7.0/24']
summary 10.1.4.0/22 also claims: ['10.1.6.0/24']
Advertising 10.1.4.0/22 would attract traffic for 10.1.6.0/24 that this router can't deliver. That's why careful address planning (contiguous, aligned blocks per site) pays off: it makes clean summaries possible.
Tip: A good enterprise plan allocates one aligned block per site (for example a /16 per region, a /20 per site, a /24 per VLAN), so each site can be summarised to a single route.
Test yourself
In VLSM, why allocate the largest subnets first?
- Large subnets are more important
- It keeps every block aligned on its own boundary without gaps
- Routers require it
- Small subnets can't be allocated later
What single route summarizes 10.0.8.0/24, 10.0.9.0/24, 10.0.10.0/24 and 10.0.11.0/24?
- 10.0.8.0/21
- 10.0.8.0/22
- 10.0.0.0/16
- 10.0.8.0/23
Why is 192.168.1.0/23 an invalid network address?
- /23 is not a real prefix
- A /23 must start on an even third octet, so host bits are set
- 192.168 can't be subnetted
- It overlaps with 192.168.0.0/16