drouting: fix random GW selection when all weights are zero#3867
Open
wlp2s0 wants to merge 1 commit intoOpenSIPS:masterfrom
Open
drouting: fix random GW selection when all weights are zero#3867wlp2s0 wants to merge 1 commit intoOpenSIPS:masterfrom
wlp2s0 wants to merge 1 commit intoOpenSIPS:masterfrom
Conversation
When weight_sum is 0 (all remaining gateways have weight 0), the code was always selecting the first gateway (i = first) instead of randomly picking one among the equally-weighted candidates. Restore uniform random selection by computing: i = first + (unsigned int)((size - first) * ((double)rand() / ((double)RAND_MAX + 1.0))); The +1.0 on RAND_MAX prevents the index from going out of bounds (i.e. when rand() == RAND_MAX the result stays strictly below size-first). Closes: OpenSIPS#3863
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the
weight_based_sort()function in the drouting module so that gateways with weight 0 are selected randomly with uniform distribution, instead of always picking the first one.Details
When all remaining gateways in a routing group have
weight == 0, the totalweight_sumbecomes 0. In this case the code fell through to anelsebranch that was supposed to randomly pick one of the equally-weighted gateways. However the random selection line was commented out and replaced withi = first;, which deterministically selected the first gateway every time.This means that in any drouting rule where all gateways have weight 0 (or where the remaining unselected gateways all have weight 0), traffic was never distributed — it always went to the same gateway.
Solution
Restore and correct the random selection in the
elsebranch ofweight_based_sort():Key points:
+ firstoffset ensures we only pick among the remaining (not yet sorted) gateways.+ 1.0onRAND_MAXprevents an out-of-bounds index whenrand() == RAND_MAX(the result stays strictly in[first, size-1]).Compatibility
No configuration or API changes. The fix only affects the runtime behavior of weight-based gateway selection when
weight_sum == 0, restoring the intended uniform random distribution. Existing configurations with non-zero weights are completely unaffected.Closing issues
Closes #3863