Skip to content
Tutorials Center
My learning

Topics

Lesson 71 of 79 ยท How the Web Works and Real-World Projects

Project: Subnet Planner

Build a subnet planner in Python: size VLSM subnets for an organization, allocate aligned blocks, verify the plan, find free space and print a full report.

Planning an address space is a classic job for a network engineer, and it is also a perfect first project: the rules are exact, the arithmetic is easy to get subtly wrong by hand, and a short program can check every step. In this project you will build a subnet planner. You give it a parent block such as 10.20.0.0/22 and a list of departments with their host counts. It decides the prefix for each, hands out aligned blocks, proves the plan has no overlaps, reports the free space and prints a table you could paste into documentation.

You already know the ideas from subnetting and CIDR and VLSM. Here you put them together into one tool. Everything runs live in Python with the standard ipaddress module, so you can edit the requirements and watch the plan change.

What the planner must do

A good planner is more than a calculator. Before writing code, list what it has to guarantee:

  1. Right-sized blocks. Every subnet must hold its hosts plus the network and broadcast addresses (point-to-point links may use a /31).
  2. Alignment. A block of 2^n addresses must start at a multiple of 2^n.
  3. No overlap. No address belongs to two subnets.
  4. Inside the parent. Everything fits in the organization's block, or the planner says clearly that it does not.
  5. Visible leftovers. The engineer must see how much is free and where.
flowchart LR
    A["Requests<br/>(name, hosts)"] --> B["Size each one<br/>(prefix length)"]
    B --> C["Sort largest first"]
    C --> D["Align and allocate<br/>from the parent"]
    D --> E["Verify<br/>(overlaps, fit)"]
    E --> F["Report and<br/>free space"]

The planner is a short pipeline. Each stage is a small function you can test on its own.

Step 1: choose a prefix for a host count

The first function answers: how long must the prefix be so that at least hosts addresses are usable? A block with h host bits has 2^h addresses, of which 2^h minus 2 are usable (network and broadcast are reserved). So you need the smallest h with 2^h at least hosts + 2. Python's int.bit_length() computes this without floating point: (hosts + 1).bit_length() is the number of bits needed to write hosts + 1, which is exactly the right h once you round up to a power of two. Subtracting from 32 gives the prefix. The max(2, ...) keeps every ordinary subnet at least a /30.

Python Step 1: from host count to prefix length

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the boundary cases. Six hosts fit exactly in a /29 (8 addresses, 6 usable), but seven hosts jump to a /28. A request for 254 hosts fits a /24, and 255 would need a /23. Boundary mistakes are the most common bug in hand-made plans, which is why the planner calls a function instead of trusting memory.

A /31 is only for links with exactly two routers (RFC 3021). On a /31, both addresses are usable and there is no broadcast address. A /32 is a single address, for example a loopback or a host route. For ordinary subnets with hosts, stay with /30 or shorter.

Step 2: allocate aligned blocks

The allocation algorithm is the VLSM method from the previous chapter. Sort requests from largest to smallest, keep a cursor (the next free address as an integer), and for each request round the cursor up to a multiple of the block size before placing the block. Sorting largest first guarantees that the rounding step never wastes much space, because every earlier block was at least as big as the one being placed.

The expression -(-cursor // size) * size rounds up to a multiple of size using integer division only. Python's // rounds down, so negating twice turns it into a "round up".

We keep each request in a small dataclass, so adding fields later (a VLAN number, a location) is trivial.

Python Step 2: allocate the blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at where the blocks land. Engineering needs 180 hosts, so it gets a whole /24. Guest Wi-Fi (100 hosts) gets a /25 that starts at 10.20.1.0, the next /25-aligned address. Every block begins exactly where the previous one ended, with no gaps, which is the payoff of sorting largest first.

flowchart TB
    P["10.20.0.0/22"] --> A["10.20.0.0/24<br/>Engineering"]
    P --> B["10.20.1.0/24"]
    B --> B1["10.20.1.0/25<br/>Guest Wi-Fi"]
    B --> B2["10.20.1.128/25"]
    B2 --> C1["10.20.1.128/26<br/>Sales"]
    B2 --> C2["10.20.1.192/26"]
    C2 --> D1["10.20.1.192/27<br/>Servers"]
    C2 --> D2["10.20.1.224/27 (Printers, Management, link)"]
    P --> F["10.20.2.0/23<br/>free"]

The plan is a binary tree: each block is split in halves, and a half becomes a subnet or is split again. The leftover /23 at the right stays whole.

Step 3: verify the plan

The allocator should never produce an invalid plan, but "should" is not a proof. A separate verifier checks the finished plan independently, so a bug in the allocator (or a hand edit to the plan) is caught. Two ideas make it short:

  • network.subnet_of(parent) tests containment.
  • a.overlaps(b) tests whether two networks share any address.

The planner also computes the free space. The address_exclude method of a network returns the pieces that remain after removing a smaller network from it; chaining it over all allocated blocks, then merging with collapse_addresses, gives the unused blocks in the cleanest possible form.

Python Step 3: verify the plan and find free space

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The free list shows 10.20.2.0/23 (512 addresses) and two tiny leftovers inside the last /24. That big block is the planner's answer to "where do we put the next department?". Because it is a clean /23, it can be given out as one subnet or split again.

Step 4: the complete planner with growth headroom

Real networks grow. A plan that is 97% full on day one is a plan that must be renumbered next year, and renumbering is painful because it touches DHCP scopes, firewall rules and static addresses. The final version adds two features:

  • A growth factor. Each non-link request is multiplied (say by 1.5) before it is sized.
  • A parent search. Instead of being told the parent block, the planner tries the smallest block that fits, starting at the first address of your address space, and grows it until the plan is possible.

The report prints the network, mask, gateway (by convention the first usable address), usable host count and how full the subnet is on day one. Run it and change the numbers: add a department, make Engineering 400 hosts, or set a growth of 2.0.

Python Step 4: the complete subnet planner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no headroom the whole organization fits in a /23, but Sales is 97% full on day one. With 50% headroom the plan needs a /22, and no ordinary subnet is more than about half full. That is the real engineering trade-off the tool makes visible: address space is cheap in a private 10.0.0.0/8 network, so lean toward headroom, while public IPv4 space is scarce and expensive, so plan it tightly.

The last line of the report matters too. Because the whole plan sits inside one aligned parent, you advertise one route upstream (10.20.0.0/22) instead of seven, which is exactly the aggregation benefit from the CIDR lesson.

Ideas to extend it

  • Support several sites. Give each site its own /22 from a /16 company block, so every site summarizes to a single route.
  • Reserve ranges. Mark the first 10 addresses of each subnet as static (gateways, printers) and the rest as the DHCP pool (see DHCP).
  • Add IPv6. Give every subnet a /64 from a /48 using the same tree idea (IPv6). IPv6 planning is easier, because you never size by host count.
  • Export. Print the plan as router configuration lines or a CSV for a spreadsheet.
  • Add tests. The verifier makes property testing easy: generate random requests and assert verify(...) returns no problems.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the planner sort the requests from largest to smallest before allocating?

    A block must start at a multiple of its own size. If big blocks go first, every later block is smaller, so the cursor is already close to a valid boundary and almost no addresses are skipped.
  2. 2. What prefix length does a subnet for 127 hosts need?

    A /25 has 128 addresses but only 126 usable. For 127 usable hosts you need the next size up, a /24.
  3. 3. Which check detects two subnets that share addresses?

    overlaps() is true when two networks have at least one address in common. subnet_of() only tests containment inside a parent.
  4. 4. Why add a growth factor to the host counts?

    Renumbering touches DHCP scopes, firewall rules and static configuration. Spare space is cheap in private ranges and avoids that work.
  5. 5. What is the benefit of placing all subnets inside one aligned parent block?

    An aligned parent block aggregates into one prefix, so the rest of the network needs one routing entry instead of one per subnet.

Summary

  • A planner is a pipeline: size each request, sort largest first, align and allocate, verify, then report.
  • The prefix for h hosts comes from 32 - (h + 1).bit_length() (at least a /30), with a /31 allowed on two-router links.
  • Blocks start at multiples of their own size, found by rounding the cursor up; largest-first order keeps gaps tiny.
  • A separate verifier (subnet_of, overlaps) and a free-space calculation (address_exclude, collapse_addresses) turn a plan from a hope into a checked result.
  • Growth headroom costs addresses but avoids renumbering, and one aligned parent block means one summarized route.

Next project: look at the bytes themselves with an Ethernet, IP and TCP packet parser.