Debian Codegraph @

test/rist/unit/test_recovery_depth.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for rist_recovery_depth_set() (exponent-based API).
4 *
5 * recovery-depth N sizes the Advanced retransmission ring to
6 * (UINT16_SIZE << N) packets. This test verifies the public setter's contract:
7 * - the default Advanced ring is RIST_SERVER_QUEUE_BUFFERS (== depth 3, 8x),
8 * - each depth maps to (UINT16_SIZE << depth) packets,
9 * - an Advanced sender's ring (sender_queue_max) is resized in place
10 * before rist_start(),
11 * - the Advanced receiver records the new capacity (recovery_queue_max),
12 * - Simple/Main store the request but never resize their 16-bit ring,
13 * - a NULL context is rejected.
14 *
15 * Test depths are kept modest (<= 6, i.e. <= 4M entries) to bound the
16 * allocations performed here.
17 *
18 * Includes rist-private.h to inspect the internal ring sizes; links
19 * against the public library for the symbols. */
20
21 #include "librist/librist.h"
22 #include "rist-private.h"
23
24 #include <stdatomic.h>
25 #include <inttypes.h>
26 #include <stdio.h>
27
28 #define DEPTH_PKTS(n) ((size_t)UINT16_SIZE << (n))
29 #define DEFAULT_PKTS ((size_t)RIST_SERVER_QUEUE_BUFFERS) /* depth 3, 524288 */
30
31 static int failures;
32
33 #define CHECK(cond, ...) \
34 do { \
35 if (!(cond)) { \
36 fprintf(stderr, "FAIL (%s:%d): ", __func__, __LINE__); \
37 fprintf(stderr, __VA_ARGS__); \
38 fprintf(stderr, "\n"); \
39 failures++; \
40 } \
41 } while (0)
42
43 static size_t sender_ring(struct rist_ctx *ctx)
44 {
45 return ctx->sender_ctx->sender_queue_max;
46 }
47
48 static size_t common_max(struct rist_ctx *ctx)
49 {
50 return rist_struct_get_common(ctx)->recovery_queue_max;
51 }
52
53 static void test_default_is_depth_3(void)
54 {
55 /* The compile-time default ring must equal depth RIST_RECOVERY_DEPTH_DEFAULT. */
56 CHECK(DEFAULT_PKTS == DEPTH_PKTS(RIST_RECOVERY_DEPTH_DEFAULT),
57 "RIST_SERVER_QUEUE_BUFFERS=%zu != depth %d (%zu)",
58 DEFAULT_PKTS, RIST_RECOVERY_DEPTH_DEFAULT, DEPTH_PKTS(RIST_RECOVERY_DEPTH_DEFAULT));
59 CHECK(RIST_RECOVERY_DEPTH_DEFAULT == 3, "default depth changed from 3");
60 }
61
62 static void test_advanced_sender(void)
63 {
64 struct rist_ctx *ctx = NULL;
65 if (rist_sender_create(&ctx, RIST_PROFILE_ADVANCED, 0, NULL) != 0 || !ctx) {
66 fprintf(stderr, "FAIL: could not create advanced sender\n");
67 failures++;
68 return;
69 }
70
71 CHECK(common_max(ctx) == DEFAULT_PKTS,
72 "default recovery_queue_max=%zu want %zu", common_max(ctx), DEFAULT_PKTS);
73 CHECK(sender_ring(ctx) == DEFAULT_PKTS,
74 "default sender_queue_max=%zu want %zu", sender_ring(ctx), DEFAULT_PKTS);
75
76 static const int depths[] = { 0, 1, 4, 6, 3 };
77 for (size_t i = 0; i < sizeof(depths) / sizeof(depths[0]); i++) {
78 int d = depths[i];
79 CHECK(rist_recovery_depth_set(ctx, (uint8_t)d) == 0, "set(%d) failed", d);
80 CHECK(sender_ring(ctx) == DEPTH_PKTS(d),
81 "depth %d sender_queue_max=%zu want %zu", d, sender_ring(ctx), DEPTH_PKTS(d));
82 CHECK(common_max(ctx) == DEPTH_PKTS(d),
83 "depth %d recovery_queue_max=%zu want %zu", d, common_max(ctx), DEPTH_PKTS(d));
84 }
85
86 rist_destroy(ctx);
87 }
88
89 static void test_advanced_receiver(void)
90 {
91 struct rist_ctx *ctx = NULL;
92 if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) {
93 fprintf(stderr, "FAIL: could not create advanced receiver\n");
94 failures++;
95 return;
96 }
97
98 CHECK(common_max(ctx) == DEFAULT_PKTS,
99 "default recovery_queue_max=%zu want %zu", common_max(ctx), DEFAULT_PKTS);
100
101 /* Receiver rings are created per-flow at peer-add time, so the setter
102 * just records the capacity the next flow will use. */
103 CHECK(rist_recovery_depth_set(ctx, 5) == 0, "set(5) failed");
104 CHECK(common_max(ctx) == DEPTH_PKTS(5),
105 "receiver recovery_queue_max=%zu want %zu", common_max(ctx), DEPTH_PKTS(5));
106
107 rist_destroy(ctx);
108 }
109
110 static void test_main_profile_sender(void)
111 {
112 struct rist_ctx *ctx = NULL;
113 if (rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, NULL) != 0 || !ctx) {
114 fprintf(stderr, "FAIL: could not create main sender\n");
115 failures++;
116 return;
117 }
118
119 size_t before = sender_ring(ctx);
120
121 /* The setter accepts the call and records the request, but a 16-bit
122 * profile must never grow its ring. */
123 CHECK(rist_recovery_depth_set(ctx, 6) == 0, "set on main failed");
124 CHECK(sender_ring(ctx) == before,
125 "main sender_queue_max changed: %zu -> %zu", before, sender_ring(ctx));
126
127 rist_destroy(ctx);
128 }
129
130 static void test_out_of_range_clamps(void)
131 {
132 /* Use a receiver: it records recovery_queue_max without allocating the
133 * (potentially huge) ring up front, so we can check the clamp 200 -> MAX
134 * without depending on the host's overcommit behavior. */
135 struct rist_ctx *ctx = NULL;
136 if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) {
137 fprintf(stderr, "FAIL: could not create advanced receiver (clamp)\n");
138 failures++;
139 return;
140 }
141 CHECK(rist_recovery_depth_set(ctx, 200) == 0, "set(200) (clamped) failed");
142 CHECK(common_max(ctx) == DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX),
143 "clamp: recovery_queue_max=%zu want %zu (depth %d)",
144 common_max(ctx), DEPTH_PKTS(RIST_RECOVERY_DEPTH_MAX), RIST_RECOVERY_DEPTH_MAX);
145 rist_destroy(ctx);
146 }
147
148 static void test_null_ctx(void)
149 {
150 CHECK(rist_recovery_depth_set(NULL, 6) == -1, "NULL ctx should return -1");
151 }
152
153 int main(void)
154 {
155 failures = 0;
156
157 test_default_is_depth_3();
158 test_advanced_sender();
159 test_advanced_receiver();
160 test_main_profile_sender();
161 test_out_of_range_clamps();
162 test_null_ctx();
163
164 if (failures == 0) {
165 printf("OK\n");
166 return 0;
167 }
168 fprintf(stderr, "Total failures: %d\n", failures);
169 return 1;
170 }