@@ -1336,53 +1336,59 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,
13361336 }
13371337}
13381338
1339- static void unix_redir_to_connected (int sotype , int sock_mapfd ,
1340- int verd_mapfd , enum redir_mode mode )
1339+ static void pairs_redir_to_connected (int cli0 , int peer0 , int cli1 , int peer1 ,
1340+ int sock_mapfd , int verd_mapfd , enum redir_mode mode )
13411341{
13421342 const char * log_prefix = redir_mode_str (mode );
1343- int c0 , c1 , p0 , p1 ;
13441343 unsigned int pass ;
13451344 int err , n ;
1346- int sfd [2 ];
13471345 u32 key ;
13481346 char b ;
13491347
13501348 zero_verdict_count (verd_mapfd );
13511349
1352- if (socketpair (AF_UNIX , sotype | SOCK_NONBLOCK , 0 , sfd ))
1353- return ;
1354- c0 = sfd [0 ], p0 = sfd [1 ];
1355-
1356- if (socketpair (AF_UNIX , sotype | SOCK_NONBLOCK , 0 , sfd ))
1357- goto close0 ;
1358- c1 = sfd [0 ], p1 = sfd [1 ];
1359-
1360- err = add_to_sockmap (sock_mapfd , p0 , p1 );
1350+ err = add_to_sockmap (sock_mapfd , peer0 , peer1 );
13611351 if (err )
1362- goto close ;
1352+ return ;
13631353
1364- n = write (c1 , "a" , 1 );
1354+ n = write (cli1 , "a" , 1 );
13651355 if (n < 0 )
13661356 FAIL_ERRNO ("%s: write" , log_prefix );
13671357 if (n == 0 )
13681358 FAIL ("%s: incomplete write" , log_prefix );
13691359 if (n < 1 )
1370- goto close ;
1360+ return ;
13711361
13721362 key = SK_PASS ;
13731363 err = xbpf_map_lookup_elem (verd_mapfd , & key , & pass );
13741364 if (err )
1375- goto close ;
1365+ return ;
13761366 if (pass != 1 )
13771367 FAIL ("%s: want pass count 1, have %d" , log_prefix , pass );
13781368
1379- n = recv_timeout (mode == REDIR_INGRESS ? p0 : c0 , & b , 1 , 0 , IO_TIMEOUT_SEC );
1369+ n = recv_timeout (mode == REDIR_INGRESS ? peer0 : cli0 , & b , 1 , 0 , IO_TIMEOUT_SEC );
13801370 if (n < 0 )
13811371 FAIL_ERRNO ("%s: recv_timeout" , log_prefix );
13821372 if (n == 0 )
13831373 FAIL ("%s: incomplete recv" , log_prefix );
1374+ }
1375+
1376+ static void unix_redir_to_connected (int sotype , int sock_mapfd ,
1377+ int verd_mapfd , enum redir_mode mode )
1378+ {
1379+ int c0 , c1 , p0 , p1 ;
1380+ int sfd [2 ];
1381+
1382+ if (socketpair (AF_UNIX , sotype | SOCK_NONBLOCK , 0 , sfd ))
1383+ return ;
1384+ c0 = sfd [0 ], p0 = sfd [1 ];
1385+
1386+ if (socketpair (AF_UNIX , sotype | SOCK_NONBLOCK , 0 , sfd ))
1387+ goto close0 ;
1388+ c1 = sfd [0 ], p1 = sfd [1 ];
1389+
1390+ pairs_redir_to_connected (c0 , p0 , c1 , p1 , sock_mapfd , verd_mapfd , mode );
13841391
1385- close :
13861392 xclose (c1 );
13871393 xclose (p1 );
13881394close0 :
@@ -1661,14 +1667,8 @@ static int inet_socketpair(int family, int type, int *s, int *c)
16611667static void udp_redir_to_connected (int family , int sock_mapfd , int verd_mapfd ,
16621668 enum redir_mode mode )
16631669{
1664- const char * log_prefix = redir_mode_str (mode );
16651670 int c0 , c1 , p0 , p1 ;
1666- unsigned int pass ;
1667- int err , n ;
1668- u32 key ;
1669- char b ;
1670-
1671- zero_verdict_count (verd_mapfd );
1671+ int err ;
16721672
16731673 err = inet_socketpair (family , SOCK_DGRAM , & p0 , & c0 );
16741674 if (err )
@@ -1677,32 +1677,8 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd,
16771677 if (err )
16781678 goto close_cli0 ;
16791679
1680- err = add_to_sockmap (sock_mapfd , p0 , p1 );
1681- if (err )
1682- goto close_cli1 ;
1683-
1684- n = write (c1 , "a" , 1 );
1685- if (n < 0 )
1686- FAIL_ERRNO ("%s: write" , log_prefix );
1687- if (n == 0 )
1688- FAIL ("%s: incomplete write" , log_prefix );
1689- if (n < 1 )
1690- goto close_cli1 ;
1691-
1692- key = SK_PASS ;
1693- err = xbpf_map_lookup_elem (verd_mapfd , & key , & pass );
1694- if (err )
1695- goto close_cli1 ;
1696- if (pass != 1 )
1697- FAIL ("%s: want pass count 1, have %d" , log_prefix , pass );
1680+ pairs_redir_to_connected (c0 , p0 , c1 , p1 , sock_mapfd , verd_mapfd , mode );
16981681
1699- n = recv_timeout (mode == REDIR_INGRESS ? p0 : c0 , & b , 1 , 0 , IO_TIMEOUT_SEC );
1700- if (n < 0 )
1701- FAIL_ERRNO ("%s: recv_timeout" , log_prefix );
1702- if (n == 0 )
1703- FAIL ("%s: incomplete recv" , log_prefix );
1704-
1705- close_cli1 :
17061682 xclose (c1 );
17071683 xclose (p1 );
17081684close_cli0 :
@@ -1747,15 +1723,9 @@ static void test_udp_redir(struct test_sockmap_listen *skel, struct bpf_map *map
17471723static void inet_unix_redir_to_connected (int family , int type , int sock_mapfd ,
17481724 int verd_mapfd , enum redir_mode mode )
17491725{
1750- const char * log_prefix = redir_mode_str (mode );
17511726 int c0 , c1 , p0 , p1 ;
1752- unsigned int pass ;
1753- int err , n ;
17541727 int sfd [2 ];
1755- u32 key ;
1756- char b ;
1757-
1758- zero_verdict_count (verd_mapfd );
1728+ int err ;
17591729
17601730 if (socketpair (AF_UNIX , SOCK_DGRAM | SOCK_NONBLOCK , 0 , sfd ))
17611731 return ;
@@ -1765,32 +1735,8 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd,
17651735 if (err )
17661736 goto close ;
17671737
1768- err = add_to_sockmap (sock_mapfd , p0 , p1 );
1769- if (err )
1770- goto close_cli1 ;
1771-
1772- n = write (c1 , "a" , 1 );
1773- if (n < 0 )
1774- FAIL_ERRNO ("%s: write" , log_prefix );
1775- if (n == 0 )
1776- FAIL ("%s: incomplete write" , log_prefix );
1777- if (n < 1 )
1778- goto close_cli1 ;
1779-
1780- key = SK_PASS ;
1781- err = xbpf_map_lookup_elem (verd_mapfd , & key , & pass );
1782- if (err )
1783- goto close_cli1 ;
1784- if (pass != 1 )
1785- FAIL ("%s: want pass count 1, have %d" , log_prefix , pass );
1786-
1787- n = recv_timeout (mode == REDIR_INGRESS ? p0 : c0 , & b , 1 , 0 , IO_TIMEOUT_SEC );
1788- if (n < 0 )
1789- FAIL_ERRNO ("%s: recv_timeout" , log_prefix );
1790- if (n == 0 )
1791- FAIL ("%s: incomplete recv" , log_prefix );
1738+ pairs_redir_to_connected (c0 , p0 , c1 , p1 , sock_mapfd , verd_mapfd , mode );
17921739
1793- close_cli1 :
17941740 xclose (c1 );
17951741 xclose (p1 );
17961742close :
@@ -1827,15 +1773,9 @@ static void inet_unix_skb_redir_to_connected(struct test_sockmap_listen *skel,
18271773static void unix_inet_redir_to_connected (int family , int type , int sock_mapfd ,
18281774 int verd_mapfd , enum redir_mode mode )
18291775{
1830- const char * log_prefix = redir_mode_str (mode );
18311776 int c0 , c1 , p0 , p1 ;
1832- unsigned int pass ;
1833- int err , n ;
18341777 int sfd [2 ];
1835- u32 key ;
1836- char b ;
1837-
1838- zero_verdict_count (verd_mapfd );
1778+ int err ;
18391779
18401780 err = inet_socketpair (family , SOCK_DGRAM , & p0 , & c0 );
18411781 if (err )
@@ -1845,32 +1785,8 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd,
18451785 goto close_cli0 ;
18461786 c1 = sfd [0 ], p1 = sfd [1 ];
18471787
1848- err = add_to_sockmap (sock_mapfd , p0 , p1 );
1849- if (err )
1850- goto close ;
1851-
1852- n = write (c1 , "a" , 1 );
1853- if (n < 0 )
1854- FAIL_ERRNO ("%s: write" , log_prefix );
1855- if (n == 0 )
1856- FAIL ("%s: incomplete write" , log_prefix );
1857- if (n < 1 )
1858- goto close ;
1859-
1860- key = SK_PASS ;
1861- err = xbpf_map_lookup_elem (verd_mapfd , & key , & pass );
1862- if (err )
1863- goto close ;
1864- if (pass != 1 )
1865- FAIL ("%s: want pass count 1, have %d" , log_prefix , pass );
1866-
1867- n = recv_timeout (mode == REDIR_INGRESS ? p0 : c0 , & b , 1 , 0 , IO_TIMEOUT_SEC );
1868- if (n < 0 )
1869- FAIL_ERRNO ("%s: recv_timeout" , log_prefix );
1870- if (n == 0 )
1871- FAIL ("%s: incomplete recv" , log_prefix );
1788+ pairs_redir_to_connected (c0 , p0 , c1 , p1 , sock_mapfd , verd_mapfd , mode );
18721789
1873- close :
18741790 xclose (c1 );
18751791 xclose (p1 );
18761792close_cli0 :
0 commit comments