How to determine largest cidr in a range of IPs

cidrip

Say I've got a begin ip and an end ip. What would be the easiest way to figure out the largest cidr I could allocate from this range in order to reduce fragmentation?

For example, I have the range 10.10.1.0 – 10.10.2.128.

I request a /25. The easiest algorithm would give me 10.10.1.0/25 and be done with it, but then this fragments the /24 and doesn't allocate the /25 (10.10.2.0/25). What I would like to see is to allocate the 10.10.2.0/25 and leave 10.10.1.0-10.10.1.255 untouched.

Any ideas would be welcome. Been beating my head over this for a bit.

Best Answer

It sounds like you want something close to the buddy allocator, to borrow a page (ha ha) from memory management.

Step 1: Transform the range you have into a series of CIDR blocks that are as large as possible without crossing the range boundary or overlapping with another block.

Step 2: Given the allocation you're trying to fit, find the smallest possible block that will fit it. Ideally this will match it exactly, but if not, you will split up the smallest block you did find (potentially recursively) until you're at the right size block.

My wording isn't particularly elegant here but I hope you get the idea.

Related Topic