Attention is currently required from: Arne Schwabe, Ralf Lici.

Hello Arne Schwabe, Ralf Lici,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1746?usp=email

to look at the new patch set (#26).


Change subject: probe: Add probe-result ranking for server selection
......................................................................

probe: Add probe-result ranking for server selection

Add probe_rank_results(), which orders probe results best-first:
responders before non-responders, responders grouped by priority (lowest
value first), and within a priority group the candidates -- those whose
RTT is within the fastest target's margin -- ahead of the rest, ordered
by DNS-SRV (RFC 2782) weighted-random selection so load is distributed
by weight. Non-candidates follow by RTT.

probe_effective_margin() is the band: the client's own setting when it
configured one, else the max_latency_diff the fastest target advertised.

The candidates are a prefix of the RTT-sorted run, so the split needs a
count rather than a partition, and the ranking allocates nothing.

The RNG is a parameter rather than a direct get_random() call, so this
module stays free of the crypto layer and the weighted ordering is
deterministically testable.

Exercised by unit tests; the client probe path calls it in a follow-up.

Change-Id: I55da68cc341bcfc707fd34ca2f84ff9b6a55501f
Signed-off-by: Lev Stipakov <[email protected]>
---
M src/openvpn/probe.c
M src/openvpn/probe.h
M tests/unit_tests/openvpn/test_pkt.c
M tests/unit_tests/openvpn/test_probe.c
M tests/unit_tests/openvpn/test_probe.h
5 files changed, 468 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/46/1746/26

diff --git a/src/openvpn/probe.c b/src/openvpn/probe.c
index 27de23a..d7211be 100644
--- a/src/openvpn/probe.c
+++ b/src/openvpn/probe.c
@@ -128,3 +128,137 @@
     }
     return probe_timestamp_in_window(req->timestamp, now, window_secs) ? 
PROBE_OK : PROBE_STALE;
 }
+
+/* Base ordering: responders before non-responders, then by priority (lower
+ * first), then by RTT (lower first), then by original index for determinism.
+ * This groups responders into priority runs pre-sorted by RTT, which the
+ * candidate-band step below relies on (run[0] is the fastest in its group). */
+static int
+probe_result_compare(const void *a, const void *b)
+{
+    const struct probe_result *ra = a;
+    const struct probe_result *rb = b;
+
+    if (ra->responded && rb->responded)
+    {
+        /* both answered: lowest priority first, then lowest RTT */
+        if (ra->reply.priority != rb->reply.priority)
+        {
+            return ra->reply.priority < rb->reply.priority ? -1 : 1;
+        }
+        if (ra->rtt_ms != rb->rtt_ms)
+        {
+            return ra->rtt_ms < rb->rtt_ms ? -1 : 1;
+        }
+    }
+    else if (ra->responded)
+    {
+        return -1; /* only a answered: it ranks first */
+    }
+    else if (rb->responded)
+    {
+        return 1; /* only b answered */
+    }
+
+    /* neither answered, or all keys equal: keep the configured order */
+    return ra->index - rb->index;
+}
+
+int
+probe_effective_margin(const struct probe_result *r, int client_margin)
+{
+    if (client_margin >= 0)
+    {
+        return client_margin;              /* the client's own setting is 
authoritative */
+    }
+    return (int)r->reply.max_latency_diff; /* else what the fastest target 
advertised */
+}
+
+/* Reorder cand[0..nc) into DNS-SRV (RFC 2782) weighted-random order by
+ * reply.weight: each position is filled by a remaining entry chosen with
+ * probability proportional to its weight. When all remaining weights are 0 the
+ * current (RTT-sorted) order is kept. */
+static void
+probe_weighted_order(struct probe_result *cand, int nc, int64_t (*rng)(void))
+{
+    for (int pos = 0; pos < nc; pos++)
+    {
+        long sum = 0;
+        for (int k = pos; k < nc; k++)
+        {
+            sum += cand[k].reply.weight;
+        }
+        int chosen = pos;
+        if (sum > 0)
+        {
+            int64_t r = rng() % sum; /* uniform in [0, sum) */
+            long acc = 0;
+            for (int k = pos; k < nc; k++)
+            {
+                acc += cand[k].reply.weight;
+                if (acc > r)
+                {
+                    chosen = k;
+                    break;
+                }
+            }
+        }
+        /* Move the chosen entry to pos, keeping the rest in RTT order (RFC 
2782
+         * removes the selected entry from the list). */
+        struct probe_result sel = cand[chosen];
+        for (int k = chosen; k > pos; k--)
+        {
+            cand[k] = cand[k - 1];
+        }
+        cand[pos] = sel;
+    }
+}
+
+/* Reorder one priority run (run[0..m), already RTT-sorted) in place: the
+ * candidates compete on weight, the rest keep their RTT order behind them. */
+static void
+probe_order_priority_run(struct probe_result *run, int m, int client_margin, 
int64_t (*rng)(void))
+{
+    if (m <= 1)
+    {
+        return;
+    }
+
+    /* The fastest target's margin sets the band. run is RTT-sorted, so the
+     * candidates are exactly its prefix; the band is inclusive, so the fastest
+     * is always one, also when it advertises 0. */
+    const unsigned int margin = (unsigned int)probe_effective_margin(&run[0], 
client_margin);
+    int nc = 0;
+    while (nc < m && run[nc].rtt_ms - run[0].rtt_ms <= margin)
+    {
+        nc++;
+    }
+
+    probe_weighted_order(run, nc, rng);
+}
+
+void
+probe_rank_results(struct probe_result *results, int n, int client_margin, 
int64_t (*rng)(void))
+{
+    if (n <= 1)
+    {
+        return;
+    }
+
+    /* Base order: responders first, grouped by priority, RTT-sorted within. */
+    qsort(results, (size_t)n, sizeof(*results), probe_result_compare);
+
+    /* Reorder each priority run of responders by candidate-band + weight. */
+    int i = 0;
+    while (i < n && results[i].responded)
+    {
+        int j = i;
+        while (j < n && results[j].responded
+               && results[j].reply.priority == results[i].reply.priority)
+        {
+            j++;
+        }
+        probe_order_priority_run(results + i, j - i, client_margin, rng);
+        i = j;
+    }
+}
diff --git a/src/openvpn/probe.h b/src/openvpn/probe.h
index 108ef58..cf600fc 100644
--- a/src/openvpn/probe.h
+++ b/src/openvpn/probe.h
@@ -209,4 +209,49 @@
 enum probe_verdict probe_request_check(struct buffer *payload, uint64_t now, 
uint64_t window_secs,
                                        struct probe_request *req);

+/* Outcome of probing one target, used to order targets best-first. @index is
+ * the caller's identifier for the probed target; @rtt_ms and @reply are only
+ * meaningful when @responded is true. */
+struct probe_result
+{
+    int index;
+    bool responded;
+    unsigned int rtt_ms;      /* probe round-trip time in ms */
+    struct probe_reply reply; /* the values the server advertised */
+};
+
+/**
+ * Order results best-first, in place, per the server-probe selection policy:
+ *   - targets that responded rank before those that did not (non-responders
+ *     keep their original relative order, last);
+ *   - responders are grouped by priority, lowest priority value first (an
+ *     absolute ordering, never overridden by latency or weight);
+ *   - within a priority group, the "candidates" are the responders whose RTT 
is
+ *     no more than the fastest target's margin larger than its RTT (see
+ *     probe_effective_margin()), so the fastest is always one. Candidates are
+ *     ordered ahead of non-candidates;
+ *   - candidates are ordered by DNS-SRV (RFC 2782) weighted-random selection 
by
+ *     weight, so a server is chosen first with probability proportional to its
+ *     weight (load distribution). Non-candidates follow, ordered by RTT.
+ *
+ * @param results        results to reorder in place
+ * @param n              number of results
+ * @param client_margin  client's candidate-band margin in ms, or < 0 if the
+ *                       client did not set one (see probe_effective_margin())
+ * @param rng            returns a non-negative random value (e.g. get_random);
+ *                       injected so this module stays free of the crypto layer
+ *                       and the weighted ordering is deterministically 
testable
+ */
+void probe_rank_results(struct probe_result *results, int n, int client_margin,
+                        int64_t (*rng)(void));
+
+/**
+ * The candidate-band margin (ms) of a priority group -- how much slower than
+ * the group's fastest target another target may be and still be a candidate.
+ * The client's own setting is authoritative; when client_margin < 0 the
+ * max_latency_diff advertised by the fastest target (r) applies to the whole
+ * group. 0 then admits only targets tying the fastest.
+ */
+int probe_effective_margin(const struct probe_result *r, int client_margin);
+
 #endif /* ifndef PROBE_H */
diff --git a/tests/unit_tests/openvpn/test_pkt.c 
b/tests/unit_tests/openvpn/test_pkt.c
index cfb3a3b..dc5589f 100644
--- a/tests/unit_tests/openvpn/test_pkt.c
+++ b/tests/unit_tests/openvpn/test_pkt.c
@@ -987,7 +987,19 @@
         cmocka_unit_test(test_probe_reply_find_skips_unknown),
         cmocka_unit_test(test_probe_reply_find_rejects_unknown_mandatory),
         cmocka_unit_test(test_probe_reply_find_missing),
-        cmocka_unit_test(test_probe_reply_find_request_tlv)
+        cmocka_unit_test(test_probe_reply_find_request_tlv),
+        cmocka_unit_test(test_rank_responder_before_nonresponder),
+        cmocka_unit_test(test_rank_by_priority),
+        cmocka_unit_test(test_rank_candidate_band),
+        cmocka_unit_test(test_rank_advertised_margin),
+        cmocka_unit_test(test_rank_client_margin_overrides_advertised),
+        cmocka_unit_test(test_rank_advertised_zero_margin),
+        cmocka_unit_test(test_rank_slower_servers_margin_has_no_effect),
+        cmocka_unit_test(test_rank_zero_margin_weights_ties),
+        cmocka_unit_test(test_rank_margin_boundary_is_inclusive),
+        cmocka_unit_test(test_rank_weighted_selection),
+        cmocka_unit_test(test_rank_weighted_keeps_rtt_order_of_rest),
+        cmocka_unit_test(test_rank_nonresponders_last)
     };

     return cmocka_run_group_tests_name("pkt tests", tests, NULL, NULL);
diff --git a/tests/unit_tests/openvpn/test_probe.c 
b/tests/unit_tests/openvpn/test_probe.c
index d1272117..335f8d6 100644
--- a/tests/unit_tests/openvpn/test_probe.c
+++ b/tests/unit_tests/openvpn/test_probe.c
@@ -708,3 +708,255 @@

     gc_free(&gc);
 }
+
+/* Deterministic RNG stubs for the weighted-selection ordering. rank_rng_zero
+ * makes the weighted draw always pick the first remaining candidate, 
preserving
+ * order; rank_rng_fixed returns a value we set to land in a chosen weight 
slice. */
+static int64_t
+rank_rng_zero(void)
+{
+    return 0;
+}
+
+static int64_t rank_rng_value;
+static int64_t
+rank_rng_fixed(void)
+{
+    return rank_rng_value;
+}
+
+/* Picking a weighted entry must not disturb the RTT order of the entries it
+ * was picked ahead of: RFC 2782 removes the selection, it does not swap. */
+void
+test_rank_weighted_keeps_rtt_order_of_rest(void **state)
+{
+    /* same priority, all in band; only the slowest advertises a weight, so it
+     * is drawn first and the two weight-0 entries must follow in RTT order. */
+    struct probe_result r[] = {
+        { .index = 0, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 0 } },
+        { .index = 1, .responded = true, .rtt_ms = 30, .reply = { .priority = 
10, .weight = 0 } },
+        { .index = 2, .responded = true, .rtt_ms = 40, .reply = { .priority = 
10, .weight = 10 } },
+    };
+
+    rank_rng_value = 0;
+    probe_rank_results(r, 3, 100, rank_rng_fixed);
+
+    assert_int_equal(r[0].index, 2); /* the only weighted entry wins the draw 
*/
+    assert_int_equal(r[1].index, 0); /* 20 ms before 30 ms */
+    assert_int_equal(r[2].index, 1);
+}
+
+/* Responders rank ahead of non-responders regardless of index order. */
+void
+test_rank_responder_before_nonresponder(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0, .responded = false },
+        { .index = 1, .responded = true, .reply = { .priority = 100, .weight = 
50 } },
+    };
+    probe_rank_results(r, 2, 10, rank_rng_zero);
+    assert_int_equal(r[0].index, 1);
+    assert_int_equal(r[1].index, 0);
+}
+
+/* Among responders, the lowest priority value wins (an absolute ordering). */
+void
+test_rank_by_priority(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0, .responded = true, .reply = { .priority = 20, .weight = 
50 } },
+        { .index = 1, .responded = true, .reply = { .priority = 5, .weight = 
50 } },
+        { .index = 2, .responded = true, .reply = { .priority = 10, .weight = 
50 } },
+    };
+    probe_rank_results(r, 3, 10, rank_rng_zero);
+    assert_int_equal(r[0].index, 1); /* priority 5 */
+    assert_int_equal(r[1].index, 2); /* priority 10 */
+    assert_int_equal(r[2].index, 0); /* priority 20 */
+}
+
+/* Within a priority, only servers within the latency margin of the fastest are
+ * candidates; a slower (out-of-band) server ranks behind a faster one no 
matter
+ * how large its weight. */
+void
+test_rank_candidate_band(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0,
+          .responded = true,
+          .rtt_ms = 100,
+          .reply = { .priority = 10, .weight = 1000 } },
+        { .index = 1, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 1 } },
+    };
+    /* margin 10ms: 20ms is fastest; 100ms is 80ms slower -> out of band */
+    probe_rank_results(r, 2, 10, rank_rng_zero);
+    assert_int_equal(r[0].index, 1); /* fast, in-band, despite tiny weight */
+    assert_int_equal(r[1].index, 0); /* slow, out-of-band, despite huge weight 
*/
+}
+
+/* A server widens its own band via the advertised max_latency_diff, joining 
the
+ * candidate set even when it is well behind the fastest; it then participates 
in
+ * the weighted selection. */
+void
+test_rank_advertised_margin(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0,
+          .responded = true,
+          .rtt_ms = 20,
+          .reply = { .priority = 10, .weight = 1, .max_latency_diff = 200 } },
+        { .index = 1,
+          .responded = true,
+          .rtt_ms = 100,
+          .reply = { .priority = 10, .weight = 1000 } },
+    };
+    /* Client did not set a margin (-1), so the fastest server's advertised
+     * value applies: it says 200, so the 100ms server is a candidate 80ms
+     * behind it; with weight 1000 (slice [1,1001)) a draw of 500 selects it
+     * first. */
+    rank_rng_value = 500;
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 1);
+}
+
+/* A client margin, even 0, overrides whatever the servers advertise. */
+void
+test_rank_client_margin_overrides_advertised(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 1 } },
+        { .index = 1,
+          .responded = true,
+          .rtt_ms = 100,
+          .reply = { .priority = 10, .weight = 1000, .max_latency_diff = 200 } 
},
+    };
+    /* With the advertised 200 ms the slow server would be a candidate (see
+     * test_rank_advertised_margin); the client's 0 leaves only the fastest. */
+    rank_rng_value = 500;
+    probe_rank_results(r, 2, 0, rank_rng_fixed);
+    assert_int_equal(r[0].index, 0);
+    assert_int_equal(r[1].index, 1);
+}
+
+/* A slower server's own margin does not admit it: with the fastest advertising
+ * 0, the 100ms server is no candidate however wide its margin or heavy its
+ * weight. */
+void
+test_rank_slower_servers_margin_has_no_effect(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 1 } },
+        { .index = 1,
+          .responded = true,
+          .rtt_ms = 100,
+          .reply = { .priority = 10, .weight = 1000, .max_latency_diff = 200 } 
},
+    };
+    rank_rng_value = 500;
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 0);
+}
+
+/* Among candidates, weight drives RFC-2782 proportional selection: a draw is
+ * mapped to the server whose cumulative weight slice it falls in. */
+void
+test_rank_weighted_selection(void **state)
+{
+    /* equal priority and RTT -> both in band; weights 30 and 70, sum 100:
+     * index 0 owns [0,30), index 1 owns [30,100). */
+    const struct probe_result base[] = {
+        { .index = 0, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 30 } },
+        { .index = 1, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 70 } },
+    };
+    struct probe_result r[2];
+
+    memcpy(r, base, sizeof(base));
+    rank_rng_value = 10; /* falls in index 0's slice */
+    probe_rank_results(r, 2, 50, rank_rng_fixed);
+    assert_int_equal(r[0].index, 0);
+
+    memcpy(r, base, sizeof(base));
+    rank_rng_value = 50; /* falls in index 1's slice */
+    probe_rank_results(r, 2, 50, rank_rng_fixed);
+    assert_int_equal(r[0].index, 1);
+}
+
+/* The fastest advertising 0 means "compare on latency alone": the band is
+ * empty, so weight cannot float a slower peer ahead of it. The same pair with 
a
+ * band advertised does let weight decide. */
+void
+test_rank_advertised_zero_margin(void **state)
+{
+    const struct probe_result base[] = {
+        { .index = 0, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 1 } },
+        { .index = 1, .responded = true, .rtt_ms = 40, .reply = { .priority = 
10, .weight = 1000 } },
+    };
+    struct probe_result r[2];
+
+    memcpy(r, base, sizeof(base));
+    rank_rng_value = 500;
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 0); /* both advertise 0 -> fastest wins */
+
+    memcpy(r, base, sizeof(base));
+    r[0].reply.max_latency_diff = 50;
+    r[1].reply.max_latency_diff = 50;
+    rank_rng_value = 500;            /* falls in the weight-1000 slice 
[1,1001) */
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 1); /* 20ms behind but inside a 50ms band */
+}
+
+/* Targets tied at the fastest RTT are all candidates even at margin 0, so
+ * weight still distributes load between them. */
+void
+test_rank_zero_margin_weights_ties(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0,
+          .responded = true,
+          .rtt_ms = 20,
+          .reply = { .priority = 10, .weight = 1, .max_latency_diff = 0 } },
+        { .index = 1,
+          .responded = true,
+          .rtt_ms = 20,
+          .reply = { .priority = 10, .weight = 1000, .max_latency_diff = 0 } },
+    };
+    rank_rng_value = 500; /* falls in the weight-1000 slice [1,1001) */
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 1);
+}
+
+/* A target exactly its advertised margin behind the fastest is still a
+ * candidate ("no more than max_latency_diff larger than the lowest RTT"). */
+void
+test_rank_margin_boundary_is_inclusive(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0,
+          .responded = true,
+          .rtt_ms = 20,
+          .reply = { .priority = 10, .weight = 1, .max_latency_diff = 10 } },
+        { .index = 1,
+          .responded = true,
+          .rtt_ms = 30,
+          .reply = { .priority = 10, .weight = 1000, .max_latency_diff = 10 } 
},
+    };
+    rank_rng_value = 500;
+    probe_rank_results(r, 2, -1, rank_rng_fixed);
+    assert_int_equal(r[0].index, 1); /* exactly 10ms behind, in band, wins on 
weight */
+}
+
+/* Non-responders are placed last, keeping their original relative order. */
+void
+test_rank_nonresponders_last(void **state)
+{
+    struct probe_result r[] = {
+        { .index = 0, .responded = false },
+        { .index = 1, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 50 } },
+        { .index = 2, .responded = false },
+        { .index = 3, .responded = true, .rtt_ms = 20, .reply = { .priority = 
10, .weight = 50 } },
+    };
+    probe_rank_results(r, 4, 10, rank_rng_zero);
+    assert_int_equal(r[0].index, 1); /* responder (rng_zero keeps order) */
+    assert_int_equal(r[1].index, 3); /* responder */
+    assert_int_equal(r[2].index, 0); /* non-responder, original order kept */
+    assert_int_equal(r[3].index, 2);
+}
diff --git a/tests/unit_tests/openvpn/test_probe.h 
b/tests/unit_tests/openvpn/test_probe.h
index 52864cc..ee61754 100644
--- a/tests/unit_tests/openvpn/test_probe.h
+++ b/tests/unit_tests/openvpn/test_probe.h
@@ -79,4 +79,28 @@

 void test_probe_reply_find_request_tlv(void **state);

+void test_rank_responder_before_nonresponder(void **state);
+
+void test_rank_by_priority(void **state);
+
+void test_rank_candidate_band(void **state);
+
+void test_rank_advertised_margin(void **state);
+
+void test_rank_client_margin_overrides_advertised(void **state);
+
+void test_rank_advertised_zero_margin(void **state);
+
+void test_rank_slower_servers_margin_has_no_effect(void **state);
+
+void test_rank_zero_margin_weights_ties(void **state);
+
+void test_rank_margin_boundary_is_inclusive(void **state);
+
+void test_rank_weighted_selection(void **state);
+
+void test_rank_weighted_keeps_rtt_order_of_rest(void **state);
+
+void test_rank_nonresponders_last(void **state);
+
 #endif

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1746?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I55da68cc341bcfc707fd34ca2f84ff9b6a55501f
Gerrit-Change-Number: 1746
Gerrit-PatchSet: 26
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to