@@ -153,8 +153,66 @@ static void check_n_were_seen_once(int *fds, int fds_len, int n,
153
153
ASSERT_EQ (seen_once , n , "seen_once" );
154
154
}
155
155
156
+ static int accept_from_one (int * server_fds , int server_fds_len )
157
+ {
158
+ int i = 0 ;
159
+ int fd ;
160
+
161
+ for (; i < server_fds_len ; i ++ ) {
162
+ fd = accept (server_fds [i ], NULL , NULL );
163
+ if (fd >= 0 )
164
+ return fd ;
165
+ if (!ASSERT_EQ (errno , EWOULDBLOCK , "EWOULDBLOCK" ))
166
+ return -1 ;
167
+ }
168
+
169
+ return -1 ;
170
+ }
171
+
172
+ static int * connect_to_server (int family , int sock_type , const char * addr ,
173
+ __u16 port , int nr_connects , int * server_fds ,
174
+ int server_fds_len )
175
+ {
176
+ struct network_helper_opts opts = {
177
+ .timeout_ms = 0 ,
178
+ };
179
+ int * established_socks ;
180
+ int i ;
181
+
182
+ /* Make sure accept() doesn't block. */
183
+ for (i = 0 ; i < server_fds_len ; i ++ )
184
+ if (!ASSERT_OK (fcntl (server_fds [i ], F_SETFL , O_NONBLOCK ),
185
+ "fcntl(O_NONBLOCK)" ))
186
+ return NULL ;
187
+
188
+ established_socks = malloc (sizeof (int ) * nr_connects * 2 );
189
+ if (!ASSERT_OK_PTR (established_socks , "established_socks" ))
190
+ return NULL ;
191
+
192
+ i = 0 ;
193
+
194
+ while (nr_connects -- ) {
195
+ established_socks [i ] = connect_to_addr_str (family , sock_type ,
196
+ addr , port , & opts );
197
+ if (!ASSERT_OK_FD (established_socks [i ], "connect_to_addr_str" ))
198
+ goto error ;
199
+ i ++ ;
200
+ established_socks [i ] = accept_from_one (server_fds ,
201
+ server_fds_len );
202
+ if (!ASSERT_OK_FD (established_socks [i ], "accept_from_one" ))
203
+ goto error ;
204
+ i ++ ;
205
+ }
206
+
207
+ return established_socks ;
208
+ error :
209
+ free_fds (established_socks , i );
210
+ return NULL ;
211
+ }
212
+
156
213
static void remove_seen (int family , int sock_type , const char * addr , __u16 port ,
157
- int * socks , int socks_len , struct sock_count * counts ,
214
+ int * socks , int socks_len , int * established_socks ,
215
+ int established_socks_len , struct sock_count * counts ,
158
216
int counts_len , struct bpf_link * link , int iter_fd )
159
217
{
160
218
int close_idx ;
@@ -185,6 +243,7 @@ static void remove_seen(int family, int sock_type, const char *addr, __u16 port,
185
243
186
244
static void remove_unseen (int family , int sock_type , const char * addr ,
187
245
__u16 port , int * socks , int socks_len ,
246
+ int * established_socks , int established_socks_len ,
188
247
struct sock_count * counts , int counts_len ,
189
248
struct bpf_link * link , int iter_fd )
190
249
{
@@ -217,6 +276,7 @@ static void remove_unseen(int family, int sock_type, const char *addr,
217
276
218
277
static void remove_all (int family , int sock_type , const char * addr ,
219
278
__u16 port , int * socks , int socks_len ,
279
+ int * established_socks , int established_socks_len ,
220
280
struct sock_count * counts , int counts_len ,
221
281
struct bpf_link * link , int iter_fd )
222
282
{
@@ -244,7 +304,8 @@ static void remove_all(int family, int sock_type, const char *addr,
244
304
}
245
305
246
306
static void add_some (int family , int sock_type , const char * addr , __u16 port ,
247
- int * socks , int socks_len , struct sock_count * counts ,
307
+ int * socks , int socks_len , int * established_socks ,
308
+ int established_socks_len , struct sock_count * counts ,
248
309
int counts_len , struct bpf_link * link , int iter_fd )
249
310
{
250
311
int * new_socks = NULL ;
@@ -274,6 +335,7 @@ static void add_some(int family, int sock_type, const char *addr, __u16 port,
274
335
275
336
static void force_realloc (int family , int sock_type , const char * addr ,
276
337
__u16 port , int * socks , int socks_len ,
338
+ int * established_socks , int established_socks_len ,
277
339
struct sock_count * counts , int counts_len ,
278
340
struct bpf_link * link , int iter_fd )
279
341
{
@@ -302,10 +364,12 @@ static void force_realloc(int family, int sock_type, const char *addr,
302
364
303
365
struct test_case {
304
366
void (* test )(int family , int sock_type , const char * addr , __u16 port ,
305
- int * socks , int socks_len , struct sock_count * counts ,
367
+ int * socks , int socks_len , int * established_socks ,
368
+ int established_socks_len , struct sock_count * counts ,
306
369
int counts_len , struct bpf_link * link , int iter_fd );
307
370
const char * description ;
308
371
int ehash_buckets ;
372
+ int connections ;
309
373
int init_socks ;
310
374
int max_socks ;
311
375
int sock_type ;
@@ -416,6 +480,7 @@ static void do_resume_test(struct test_case *tc)
416
480
static const __u16 port = 10001 ;
417
481
struct nstoken * nstoken = NULL ;
418
482
struct bpf_link * link = NULL ;
483
+ int * established_fds = NULL ;
419
484
int err , iter_fd = -1 ;
420
485
const char * addr ;
421
486
int * fds = NULL ;
@@ -444,6 +509,14 @@ static void do_resume_test(struct test_case *tc)
444
509
tc -> init_socks );
445
510
if (!ASSERT_OK_PTR (fds , "start_reuseport_server" ))
446
511
goto done ;
512
+ if (tc -> connections ) {
513
+ established_fds = connect_to_server (tc -> family , tc -> sock_type ,
514
+ addr , port ,
515
+ tc -> connections , fds ,
516
+ tc -> init_socks );
517
+ if (!ASSERT_OK_PTR (established_fds , "connect_to_server" ))
518
+ goto done ;
519
+ }
447
520
skel -> rodata -> ports [0 ] = 0 ;
448
521
skel -> rodata -> ports [1 ] = 0 ;
449
522
skel -> rodata -> sf = tc -> family ;
@@ -465,13 +538,15 @@ static void do_resume_test(struct test_case *tc)
465
538
goto done ;
466
539
467
540
tc -> test (tc -> family , tc -> sock_type , addr , port , fds , tc -> init_socks ,
468
- counts , tc -> max_socks , link , iter_fd );
541
+ established_fds , tc -> connections * 2 , counts , tc -> max_socks ,
542
+ link , iter_fd );
469
543
done :
470
544
close_netns (nstoken );
471
545
SYS_NOFAIL ("ip netns del " TEST_CHILD_NS );
472
546
SYS_NOFAIL ("sysctl -w net.ipv4.tcp_child_ehash_entries=0" );
473
547
free (counts );
474
548
free_fds (fds , tc -> init_socks );
549
+ free_fds (established_fds , tc -> connections * 2 );
475
550
if (iter_fd >= 0 )
476
551
close (iter_fd );
477
552
bpf_link__destroy (link );
0 commit comments