Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 -

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

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

share|improve this question

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.

share|improve this answer
This is exactly what I am trying to do, but I am a bit confused on the bit math that would break down the large block into a series of blocks. Will have to bang away a bit more tomorrow. :) – Magic Hat Aug 11 '10 at 23:15
Start by enumerating all of the /32s in the range. For each /32, push it onto a stack, then attempt to merge the top two allocations in the stack (contingent upon them being the same size and valid after being merged). If it works, repeat this step until it doesn't work. After this enumeration is complete, the stack should contain what you want. Unless I missed something, which is entirely possible. – Drew Bloechl Aug 11 '10 at 23:38
I'll give this a shot and see how it plays out. Thanks Drew. – Magic Hat Aug 12 '10 at 13:25

There is not a CIDR that is bounded by - /22 is - /23 is - /24 is -

You could have two separate networks: /24 is - /25 is -

share|improve this answer
Downvote on six year old post without a comment as to why. Nice! – dbasnett May 13 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.