Skip to content

Commit 2593a0a

Browse files
jdamato-fslykuba-moo
authored andcommitted
selftests: drv-net: Test that NAPI ID is non-zero
Test that the SO_INCOMING_NAPI_ID of a network file descriptor is non-zero. This ensures that either the core networking stack or, in some cases like netdevsim, the driver correctly sets the NAPI ID. Signed-off-by: Joe Damato <jdamato@fastly.com> Link: https://patch.msgid.link/20250424002746.16891-4-jdamato@fastly.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 2b6d490 commit 2593a0a

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
napi_id_helper
23
xdp_helper

tools/testing/selftests/drivers/net/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ TEST_INCLUDES := $(wildcard lib/py/*.py) \
66
../../net/net_helper.sh \
77
../../net/lib.sh \
88

9-
TEST_GEN_FILES := xdp_helper
9+
TEST_GEN_FILES := \
10+
napi_id_helper \
11+
xdp_helper \
12+
# end of TEST_GEN_FILES
1013

1114
TEST_PROGS := \
15+
napi_id.py \
1216
netcons_basic.sh \
1317
netcons_fragmented_msg.sh \
1418
netcons_overflow.sh \
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
from lib.py import ksft_run, ksft_exit
5+
from lib.py import ksft_eq, NetDrvEpEnv
6+
from lib.py import bkg, cmd, rand_port, NetNSEnter
7+
8+
def test_napi_id(cfg) -> None:
9+
port = rand_port()
10+
listen_cmd = f"{cfg.test_dir}/napi_id_helper {cfg.addr_v['4']} {port}"
11+
12+
with bkg(listen_cmd, ksft_wait=3) as server:
13+
cmd(f"echo a | socat - TCP:{cfg.addr_v['4']}:{port}", host=cfg.remote, shell=True)
14+
15+
ksft_eq(0, server.ret)
16+
17+
def main() -> None:
18+
with NetDrvEpEnv(__file__) as cfg:
19+
ksft_run([test_napi_id], args=(cfg,))
20+
ksft_exit()
21+
22+
if __name__ == "__main__":
23+
main()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <errno.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <unistd.h>
8+
#include <arpa/inet.h>
9+
#include <sys/socket.h>
10+
11+
#include "ksft.h"
12+
13+
int main(int argc, char *argv[])
14+
{
15+
struct sockaddr_in address;
16+
unsigned int napi_id;
17+
unsigned int port;
18+
socklen_t optlen;
19+
char buf[1024];
20+
int opt = 1;
21+
int server;
22+
int client;
23+
int ret;
24+
25+
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
26+
if (server < 0) {
27+
perror("socket creation failed");
28+
if (errno == EAFNOSUPPORT)
29+
return -1;
30+
return 1;
31+
}
32+
33+
port = atoi(argv[2]);
34+
35+
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
36+
perror("setsockopt");
37+
return 1;
38+
}
39+
40+
address.sin_family = AF_INET;
41+
inet_pton(AF_INET, argv[1], &address.sin_addr);
42+
address.sin_port = htons(port);
43+
44+
if (bind(server, (struct sockaddr *)&address, sizeof(address)) < 0) {
45+
perror("bind failed");
46+
return 1;
47+
}
48+
49+
if (listen(server, 1) < 0) {
50+
perror("listen");
51+
return 1;
52+
}
53+
54+
ksft_ready();
55+
56+
client = accept(server, NULL, 0);
57+
if (client < 0) {
58+
perror("accept");
59+
return 1;
60+
}
61+
62+
optlen = sizeof(napi_id);
63+
ret = getsockopt(client, SOL_SOCKET, SO_INCOMING_NAPI_ID, &napi_id,
64+
&optlen);
65+
if (ret != 0) {
66+
perror("getsockopt");
67+
return 1;
68+
}
69+
70+
read(client, buf, 1024);
71+
72+
ksft_wait();
73+
74+
if (napi_id == 0) {
75+
fprintf(stderr, "napi ID is 0\n");
76+
return 1;
77+
}
78+
79+
close(client);
80+
close(server);
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)