Platform Engineering · Lesson F16

CIDR Notation and CIDR Calculations

The slash after an address, like the /24 in 10.0.0.0/24, is called CIDR notation. It is the shortest way to describe a whole network: its size, its first address, its last address, and how many machines fit. This lesson teaches the arithmetic, one worked example at a time.

Difficulty: Beginner · Know this first: F15 — Subnets: Cutting a Network into Pieces

Run it as:

One /24, 256 addresses, 254 hosts. The size every beginner meets first. Learn the arithmetic here.

When it breaks, I check

One thing to remember

Where the 32 bits go in a /24

In a /24, the 32 bits split into 24 network bits and 8 host bits. The bar below is exact: the network part takes three quarters of the address, the host part takes one quarter. Change the prefix and the split moves. That moving split is the whole subject of this lesson.

Extra diagram · from scratch to master

Counting a /24, address by address

Start with the slash number. Count the addresses, name the first and the last, and end with the hosts that can actually be used.

256 addresses254 usable hosts3 numbers to remember
/24
24 network bits
8 host bits
2 to the 8
256 addresses
the total
Network .0
first address
Hosts .1-.254
the usable range
Broadcast .255
last address
254 usable
the answer
Request / flowResponse · the way backFailure / dead end

Extra diagram · from scratch to master

Sizing a subnet for 50 hosts

Start with the need: 50 hosts. Add two, round up to a power of two, and read off the prefix. Then watch the trap at the end.

50 hosts needed/26 the answer1 trap to avoid
Need 50 hosts
the requirement
Add 2
52 total
Next power of 2
64
/26
the prefix
62 usable
12 spare
Overlap trap
10.0.0.128/25
Request / flowResponse · the way backFailure / dead end

Try it: change the inputs and watch it bend

The simulator below is a CIDR calculator. Type any CIDR, like 10.0.0.0/24, and it shows the network address, the broadcast address, the first and last usable hosts, the total addresses, and the usable hosts. Then tell it how many hosts you need, and it picks the smallest prefix that fits. Every value is computed live, and every value is exact.

e.g. 10.0.0.0/24

An address, a slash, and a prefix from 0 to 32. The calculator finds the real network address, even if you type a host address by mistake.

50

How many machines must fit. The calculator adds 2, rounds up to the next power of two, and picks the smallest prefix that fits.

Try this: (1) Type 10.0.0.0/24. Network 10.0.0.0, broadcast 10.0.0.255, hosts 10.0.0.1 to 10.0.0.254, 256 addresses, 254 usable. (2) Type 192.168.1.0/28. Network 192.168.1.0, broadcast 192.168.1.15, 16 addresses, 14 usable. (3) Type 10.0.0.0/20. Broadcast 10.0.15.255, 4,096 addresses, 4,094 usable. (4) Set hosts needed to 200. The answer is /24, with 254 usable. Set it to 1,000. The answer is /22, with 1,022 usable.

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Basic to master

I take the same idea four times here. First I read one CIDR. Then I size subnets for a need. Then I plan addressing for a whole cloud. Last I run it for the world.

Basic: Reading one CIDR

Definition: CIDR notation writes an address and a slash number, like 10.0.0.0/24. The slash number counts the network bits. Addresses equal 2 to the power of the host bits, and usable hosts equal addresses minus two, for the network and broadcast addresses.

Types:

  • /24, 256 addresses, 254 hosts.
  • /16, 65,536 addresses, 65,534 hosts.
  • /28, 16 addresses, 14 hosts.
  • /20, 4,096 addresses, 4,094 hosts.

Uses: I use this reading every time I see a CIDR in a config. The slash number tells me the size in one glance, and the size tells me what the network is for.

Dimensions: At one subnet the arithmetic is four steps and fits in your head. What breaks first is my memory of the minus two, and I notice it when a subnet comes up two hosts short. It costs two addresses per subnet. It costs real care to remember them. The trade is simple: two reserved addresses buy a network that can name itself and broadcast to itself.

Intermediate: Sizing subnets for a need

Definition: At this level I size subnets from a requirement. Add 2 for the network and broadcast addresses, round up to the next power of two, and subtract the host bits from 32 to get the prefix. Then I check the overlap against every network that already exists.

Types:

  • 50 hosts need a /26, with 62 usable.
  • 200 hosts need a /24, with 254 usable.
  • 1,000 hosts need a /22, with 1,022 usable.
  • 14 hosts fit exactly in a /28, with 14 usable.

Uses: I use this whenever a new subnet is requested. The requirement is a number of hosts. The answer is a prefix, and the check after it is the overlap check, every time.

Dimensions: As the requests multiply, the sizing stays easy and the overlap checking gets harder. What breaks is a subnet sized right and placed wrong, on top of an existing range, and I notice it the day the two networks must join. It costs one overlap check per new subnet. The trade is that the check costs a minute, and skipping it costs a renumbering.

Advanced: Planning addressing for a cloud

Definition: At this level I plan the whole VPC addressing before the first machine exists. One range for the VPC, one piece per purpose per zone, public and private subnets in every zone, and empty space held for growth. The plan is written down, and every cut comes from the plan.

Types:

  • One VPC range, usually a /16.
  • One /24 per purpose per zone.
  • Public subnets for what the internet may reach.
  • Private subnets for what it may not.

Uses: I use this when a new cloud environment is built. The plan fits on one page, and it saves the renumbering that follows an improvised cut. I check overlaps before I build, because fixing one after is a week of work.

Dimensions: At ten times the environments the plan is the system, and the empty space is the most valuable part of it. What breaks is a plan with no room to grow, and I notice it when a new team needs a subnet and every piece is full. It costs empty addresses held in reserve. The trade is that reserve space looks wasteful on paper, and it is the only thing that lets the cloud grow without renumbering.

Count hosts without a calculator. The number after the slash decides how many machines fit. A /30 holds 2 hosts. A /28 holds 14. I count in code, because the count is the first thing I get wrong by hand. Two hosts is enough for a link between two routers. It is not enough for an office.

$ python3 -c "
import ipaddress
for p in [30, 29, 28, 27, 24]:
    net = ipaddress.ip_network('192.168.1.0/' + str(p))
    print('/' + str(p), 'hosts:', len(list(net.hosts())))"
/30 hosts: 2
/29 hosts: 6
/28 hosts: 14
/27 hosts: 30
/24 hosts: 254

Every flag in the commands above, letter by letter.

  • python3 -c: -c means run the string that follows as code. The quotes hold the program. Without -c, python3 expects a filename.
  • One more flag worth knowing: python3 -m runs a module by name. python3 -m json.tool reads JSON and prints it indented.

Check it: run python3 -c "print(1+1)". It prints 2.

Try it: run the count above, then add /16 to the list. It holds 65,534 hosts. The jump from /24 to /16 is not a small step. It is 256 times bigger. That is why a typo in the prefix is a serious mistake.

The broadcast address is not a host. In a /24, the last address ends in 255, and it belongs to the network, not to a machine. Give it to a machine and that machine will not work, and the error will not say why. I check the broadcast address before I hand out the last few addresses.

$ python3 -c "
import ipaddress
net = ipaddress.ip_network('10.0.0.0/24')
print('broadcast:', net.broadcast_address)
print('last host:', list(net.hosts())[-1])"
broadcast: 10.0.0.255
last host: 10.0.0.254

Try it: run the code above. The last host is .254, not .255. The .255 address is the broadcast. A machine given .255 will answer nothing, and the fix is to give it .254 instead.

Master: Running it for the world

Definition: At this level CIDR planning is a managed resource across regions, clouds, and teams. Every range has an owner, a purpose, and a record. New ranges are reviewed before they are used. Overlaps are impossible by process, not by luck. And the plan is audited, because plans rot when nobody looks at them.

Types:

  • Address plans owned per region and per cloud.
  • Reviews before any new range is used.
  • Audits that find ranges nobody can explain.
  • Automation that cuts from the plan, never by hand.

Uses: I use this when addressing spans regions and clouds. It is also how I review addressing I did not build: I ask for the plan, I check the overlaps, and I look for the ranges nobody owns.

Dimensions: At a hundred times the scale the plan is the difference between a cloud that grows and a cloud that has to be rebuilt. What breaks is a range used by hand outside the plan, and I notice it when two environments must join and their ranges overlap. It costs process: reviews, records, and audits. The trade is that process costs time up front, and it buys a cloud that can keep growing without ever stopping to renumber.

Overlapping CIDRs. Two ranges that overlap will fight over the same addresses, and the fight shows up as machines that work sometimes and fail sometimes. I test for overlap before I add a new range. The check below says True when two ranges share addresses, and that True means stop and replan.

$ python3 -c "
import ipaddress
a = ipaddress.ip_network('10.0.0.0/25')
b = ipaddress.ip_network('10.0.0.0/24')
c = ipaddress.ip_network('10.0.1.0/24')
print('a overlaps b:', a.overlaps(b))
print('a overlaps c:', a.overlaps(c))"
a overlaps b: True
a overlaps c: False

Try it: run the check above. 10.0.0.0/25 sits inside 10.0.0.0/24, so they overlap, and that is a plan that will break. 10.0.1.0/24 is separate, so it is safe. Run this check on every new range before it goes live.

One size does not fit all. A /24 holds 254 usable addresses. A link between two routers needs 2. Handing that link a /24 wastes 252 addresses. VLSM is the fix. It sizes each subnet to fit its need, so nothing is wasted.

Take 192.168.1.0/24 and split it for four groups. The big group needs 100 hosts, so it gets a /25 with 126 usable addresses, 192.168.1.0 to 192.168.1.127. The next needs 50, so it gets a /26 with 62 usable, 192.168.1.128 to 192.168.1.191. The next needs 25, so it gets a /27 with 30 usable, 192.168.1.192 to 192.168.1.223. The last needs 10, so it gets a /28 with 14 usable, 192.168.1.224 to 192.168.1.239. The rest, 192.168.1.240 to 192.168.1.255, stays spare. I checked every number above with Python before I wrote it down.

The rule is simple. Round up to the next block size, and leave room to grow. The block sizes double: 16, 32, 64, 128. A group that needs 50 hosts takes a 64 block, not a 50 block, because blocks only come in powers of two.

Supernetting is the reverse. Small networks that sit next to each other can join into one bigger network. 10.0.0.0/24 and 10.0.1.0/24 sit next to each other, so they join into 10.0.0.0/23. Four in a row, 10.0.0.0 to 10.0.3.0, join into 10.0.0.0/22. One route replaces four in the routing table, and the table stays small and fast.

Try it: check the /25 count above with one command.

$ python3 -c "
import ipaddress
a = ipaddress.ip_network('192.168.1.0/25')
print('usable:', a.num_addresses - 2)"
usable: 126

Go deeper

The mask and the prefix are the same fact in two forms. /24 is 255.255.255.0. /16 is 255.255.0.0. /28 is 255.255.255.240. If you can convert one to the other, you can read any network config ever written, because old configs use masks and new ones use prefixes, and both mean the same thing.

The network address is the address with all host bits zero. The broadcast address is the address with all host bits one. That is why the calculator can find them with simple arithmetic: clear the host bits for the network, set them all for the broadcast. No table, no lookup, just bits.

Powers of two are the whole arithmetic. The sizes are 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024, 4,096, 65,536. If a size is not on that list, it is not a subnet size. That one list answers most sizing questions before you reach for a calculator.

A /32 is one address and nothing else. It names a single host, usually a single machine in a firewall rule or a route. It has 1 address and 0 usable hosts by the classic rule, because the network and the host are the same address. It is the smallest CIDR there is.

At 10x the subnets, the arithmetic stays easy and the bookkeeping gets hard. The calculations are simple. Remembering which range is which, and who owns it, is the work. I write every range down, with its purpose, the day it is created.

At 100x, the design changes. Ranges are cut by automation from a plan, and the plan is the source of truth. A human cutting by hand at that scale will overlap something. The automation checks the plan, and the plan is reviewed by a human. Both halves matter.

10.0.0.0/24   256 addresses   254 hosts   10.0.0.1 - 10.0.0.254
10.0.0.0/16   65,536 addresses   65,534 hosts   10.0.0.1 - 10.0.255.254
192.168.1.0/28   16 addresses   14 hosts   192.168.1.1 - 192.168.1.14
10.0.0.0/20   4,096 addresses   4,094 hosts   10.0.0.1 - 10.0.15.254

What the snippet means: four sizes, worked fully. Read each line left to right: the CIDR, the total addresses, the usable hosts, and the host range. The pattern to see is that the host range always starts one above the network address and ends one below the broadcast address.

Try it: the overlap test. Check whether 10.0.0.0/25 overlaps 10.0.0.0/24. It does, because the /25 is inside the /24. Check whether it overlaps 10.0.1.0/24. It does not. One True and one False, and the True one is the range you must not add. Run this test before every new subnet goes live.

$ python3 -c "
import ipaddress
a = ipaddress.ip_network('10.0.0.0/25')
b = ipaddress.ip_network('10.0.0.0/24')
c = ipaddress.ip_network('10.0.1.0/24')
print('a overlaps b:', a.overlaps(b))
print('a overlaps c:', a.overlaps(c))"
a overlaps b: True
a overlaps c: False

One time it broke

What broke

Two teams in one company each built a test environment in the cloud, and each picked 10.0.0.0/24, because it was the first range in the guide they both read. The environments worked fine for months, apart. Then the company decided to join them with a private link so the teams could share a database. The link came up, and nothing could reach anything. Both networks claimed the same 256 addresses, and the routers could not tell them apart.

Timeline

  • Jan 12,team A builds its test environment on 10.0.0.0/24. It works.
  • Feb 03,team B builds its test environment on 10.0.0.0/24. It works. Nobody compares the two ranges.
  • Mar 18,the company decides to join the two environments with a private link, so team B can use team A's database.
  • Mar 19,the link comes up. Packets addressed to 10.0.0.50 reach team A's machine from team A, and team B's machine from team B. Neither side can reach the other.
  • Mar 20,I compare the two address plans. Both are 10.0.0.0/24. The overlap is total: all 256 addresses exist twice.
  • Mar 21,team B renumbers to 10.0.1.0/24. It takes a day, and every config that mentioned an address is updated.

What changed after

Three things. First, a rule: no new environment is built without checking its range against every environment it might ever connect to. Second, a shared address plan, one page, listing every range and its owner, kept where every team can see it. Third, a habit: the overlap check happens before the build, not after. Renumbering a live environment costs a day. A ten-minute check costs ten minutes.

If you remember nothing else

Design exercise

Design the addressing for a new VPC. The VPC is 10.0.0.0/16. You need web subnets in two zones for 60 servers each, database subnets in two zones for 12 databases each, and a management subnet for 6 machines. Plan the cuts, and leave room to grow.

Clarifying questions to ask

  • What prefix does each subnet get, and why?
  • Which subnets are public and which are private?
  • How much of the /16 do you use, and how much stays empty?
  • What breaks if the web tier grows to 300 servers in one zone?
  • How do you prove, before building, that none of these ranges overlap?

Rough sizing

The web subnets need 60 hosts. 60 plus 2 is 62, and the next power of two is 64, which is a /26 with 62 usable hosts. That fits exactly, with 2 spare, which is too tight. I size up to /25, with 128 addresses and 126 usable hosts, for room to grow. The database subnets need 12 hosts. 12 plus 2 is 14, and the next power of two is 16, which is a /28 with 14 usable hosts. That fits with 2 spare, so I size up to /27, with 32 addresses and 30 usable. The management subnet needs 6 hosts, which fits a /29 with 8 addresses and 6 usable, and I size it to /28 for the same reason. The five subnets use 352 addresses out of 65,536. The web tier at 300 servers would outgrow a /25, which holds 126, so it would need a /23, with 512 addresses and 510 usable. I prove no overlap by listing every range with its first and last address and checking that no two ranges share an address.

Trade-offs to name

Sizing exactly to the need buys tight boundaries and costs the renumbering when the need grows. Sizing one step up buys room and costs addresses that sit empty. Two zones buy survival and cost a second copy of everything. And the overlap check is not optional: it costs ten minutes, and skipping it costs a day of renumbering later.

Next in this series
Lesson F17 — IPv6: The Address Space That Does Not Run Out