Sub topic 10
Variable-length subnet masking
This chapter assumes you can compute a network address, broadcast address, and usable host range from a prefix length. It also assumes you know block sizes and how to split a network into equal subnets.
Why equal subnets waste addresses
Equal subnetting gives every subnet the same prefix length. Suppose you have 192.168.1.0/24 and four segments that need 100, 50, 20, and 2 hosts. Equal subnets must all fit the largest segment, so each would be a /25. A /24 holds only two /25 subnets, so this plan fails. Even where it fits, a /25 for a two-host link wastes 124 addresses.
Variable-length subnet masking (VLSM) lets each subnet have its own prefix length. You size each subnet to its need and carve the pool into blocks of different sizes.
The method
- Add 2 to each host requirement for the network and broadcast addresses.
- Round each total up to the next power of two. The exponent is the number of host bits.
- Compute the prefix as 32 minus the host bits.
- Sort the requirements from largest to smallest.
- Assign subnets in that order, each starting where the previous one ended.
Worked example
Take the four segments above and the pool 192.168.1.0/24.
| Segment | Hosts | Hosts + 2 | Host bits | Prefix | Subnet | Usable range | Broadcast |
|---|---|---|---|---|---|---|---|
| A | 100 | 102 | 7 (128) | /25 | 192.168.1.0/25 | .1 to .126 | .127 |
| B | 50 | 52 | 6 (64) | /26 | 192.168.1.128/26 | .129 to .190 | .191 |
| C | 20 | 22 | 5 (32) | /27 | 192.168.1.192/27 | .193 to .222 | .223 |
| D | 2 | 4 | 2 (4) | /30 | 192.168.1.224/30 | .225 to .226 | .227 |
The addresses from 192.168.1.228 to 192.168.1.255 stay free for future subnets.
Why largest first
Each subnet must start at a multiple of its own block size. A /25 can only start at .0 or .128. If you place the /30 at .0 first, the next /25 has to start at .128, and the addresses from .4 to .127 are stranded. Allocating the largest blocks first keeps every later block aligned with no gaps.
Automating the allocation
This script performs the five steps and prints each subnet.
import ipaddress
import math
pool = ipaddress.ip_network("192.168.1.0/24")
needs = {"A": 100, "B": 50, "C": 20, "D": 2}
next_addr = int(pool.network_address)
for name, hosts in sorted(needs.items(), key=lambda kv: -kv[1]):
host_bits = math.ceil(math.log2(hosts + 2)) # +2 for network and broadcast
net = ipaddress.ip_network((next_addr, 32 - host_bits))
print(name, net)
next_addr += net.num_addresses
# A 192.168.1.0/25
# B 192.168.1.128/26
# C 192.168.1.192/27
# D 192.168.1.224/30The script does not check that the plan fits in the pool. If the total exceeds the pool size, next_addr runs past the pool's last address without any error. Compare it with pool.broadcast_address before you trust the result.
Common errors
- Forgetting the +2. A segment of 62 hosts needs a /26, which is 64 addresses. A /27 is too small.
- Rounding down instead of up. Always round to the next power of two that holds the requirement.
- Allocating in the order the requirements were listed. Sort first.
- Planning for today's hosts only. Add growth headroom before you round.
Exercise
Using VLSM, divide 10.0.0.0/24 into subnets for segments needing 60, 30, and 10 hosts. Give each subnet in CIDR notation.
Show answer
Add 2 and round up: 62 gives 64 addresses (/26), 32 gives 32 addresses (/27), 12 gives 16 addresses (/28). Allocate largest first: 10.0.0.0/26 (addresses 0 to 63), 10.0.0.64/27 (64 to 95), 10.0.0.96/28 (96 to 111). The addresses from 10.0.0.112 to 10.0.0.255 remain free.