selftests/bpf: Fix msg_verify_data in test_sockmap

Function msg_verify_data should have context of bytes_cnt and k instead of
assuming they are zero. Otherwise, test_sockmap with data integrity test
will report some errors. I also fix the logic related to size and index j

1/ 6  sockmap::txmsg test passthrough:FAIL
2/ 6  sockmap::txmsg test redirect:FAIL
7/12  sockmap::txmsg test apply:FAIL
10/11  sockmap::txmsg test push_data:FAIL
11/17  sockmap::txmsg test pull-data:FAIL
12/ 9  sockmap::txmsg test pop-data:FAIL
13/ 1  sockmap::txmsg test push/pop data:FAIL
...
Pass: 24 Fail: 52

After applying this patch, some of the errors are solved, but for push,
pull and pop, we may need more fixes to msg_verify_data, added a TODO

10/11  sockmap::txmsg test push_data:FAIL
11/17  sockmap::txmsg test pull-data:FAIL
12/ 9  sockmap::txmsg test pop-data:FAIL
...
Pass: 37 Fail: 15

Besides, added a custom errno EDATAINTEGRITY for msg_verify_data, we
shall not ignore the error in txmsg_cork case.

Fixes: 753fb2ee09 ("bpf: sockmap, add msg_peek tests to test_sockmap")
Fixes: 16edddfe3c ("selftests/bpf: test_sockmap, check test failure")
Acked-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Zijian Zhang <zijianzhang@bytedance.com>
Link: https://lore.kernel.org/r/20241012203731.1248619-2-zijianzhang@bytedance.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
This commit is contained in:
Zijian Zhang 2024-10-12 20:37:30 +00:00 committed by Martin KaFai Lau
parent 4a6f05d9fe
commit ee9b352ce4

View file

@ -56,6 +56,8 @@ static void running_handler(int a);
#define BPF_SOCKHASH_FILENAME "test_sockhash_kern.bpf.o" #define BPF_SOCKHASH_FILENAME "test_sockhash_kern.bpf.o"
#define CG_PATH "/sockmap" #define CG_PATH "/sockmap"
#define EDATAINTEGRITY 2001
/* global sockets */ /* global sockets */
int s1, s2, c1, c2, p1, p2; int s1, s2, c1, c2, p1, p2;
int test_cnt; int test_cnt;
@ -510,23 +512,25 @@ unwind_iov:
return -ENOMEM; return -ENOMEM;
} }
static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz) /* TODO: Add verification logic for push, pull and pop data */
static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz,
unsigned char *k_p, int *bytes_cnt_p)
{ {
int i, j = 0, bytes_cnt = 0; int i, j, bytes_cnt = *bytes_cnt_p;
unsigned char k = 0; unsigned char k = *k_p;
for (i = 0; i < msg->msg_iovlen; i++) { for (i = 0, j = 0; i < msg->msg_iovlen && size; i++, j = 0) {
unsigned char *d = msg->msg_iov[i].iov_base; unsigned char *d = msg->msg_iov[i].iov_base;
/* Special case test for skb ingress + ktls */ /* Special case test for skb ingress + ktls */
if (i == 0 && txmsg_ktls_skb) { if (i == 0 && txmsg_ktls_skb) {
if (msg->msg_iov[i].iov_len < 4) if (msg->msg_iov[i].iov_len < 4)
return -EIO; return -EDATAINTEGRITY;
if (memcmp(d, "PASS", 4) != 0) { if (memcmp(d, "PASS", 4) != 0) {
fprintf(stderr, fprintf(stderr,
"detected skb data error with skb ingress update @iov[%i]:%i \"%02x %02x %02x %02x\" != \"PASS\"\n", "detected skb data error with skb ingress update @iov[%i]:%i \"%02x %02x %02x %02x\" != \"PASS\"\n",
i, 0, d[0], d[1], d[2], d[3]); i, 0, d[0], d[1], d[2], d[3]);
return -EIO; return -EDATAINTEGRITY;
} }
j = 4; /* advance index past PASS header */ j = 4; /* advance index past PASS header */
} }
@ -536,7 +540,7 @@ static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz)
fprintf(stderr, fprintf(stderr,
"detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n", "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
i, j, d[j], k - 1, d[j+1], k); i, j, d[j], k - 1, d[j+1], k);
return -EIO; return -EDATAINTEGRITY;
} }
bytes_cnt++; bytes_cnt++;
if (bytes_cnt == chunk_sz) { if (bytes_cnt == chunk_sz) {
@ -546,6 +550,8 @@ static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz)
size--; size--;
} }
} }
*k_p = k;
*bytes_cnt_p = bytes_cnt;
return 0; return 0;
} }
@ -602,6 +608,8 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
float total_bytes, txmsg_pop_total; float total_bytes, txmsg_pop_total;
int fd_flags = O_NONBLOCK; int fd_flags = O_NONBLOCK;
struct timeval timeout; struct timeval timeout;
unsigned char k = 0;
int bytes_cnt = 0;
fd_set w; fd_set w;
fcntl(fd, fd_flags); fcntl(fd, fd_flags);
@ -696,7 +704,7 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
iov_length * cnt : iov_length * cnt :
iov_length * iov_count; iov_length * iov_count;
errno = msg_verify_data(&msg, recv, chunk_sz); errno = msg_verify_data(&msg, recv, chunk_sz, &k, &bytes_cnt);
if (errno) { if (errno) {
perror("data verify msg failed"); perror("data verify msg failed");
goto out_errno; goto out_errno;
@ -704,7 +712,9 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
if (recvp) { if (recvp) {
errno = msg_verify_data(&msg_peek, errno = msg_verify_data(&msg_peek,
recvp, recvp,
chunk_sz); chunk_sz,
&k,
&bytes_cnt);
if (errno) { if (errno) {
perror("data verify msg_peek failed"); perror("data verify msg_peek failed");
goto out_errno; goto out_errno;
@ -812,7 +822,7 @@ static int sendmsg_test(struct sockmap_options *opt)
s.bytes_sent, sent_Bps, sent_Bps/giga, s.bytes_sent, sent_Bps, sent_Bps/giga,
s.bytes_recvd, recvd_Bps, recvd_Bps/giga, s.bytes_recvd, recvd_Bps, recvd_Bps/giga,
peek_flag ? "(peek_msg)" : ""); peek_flag ? "(peek_msg)" : "");
if (err && txmsg_cork) if (err && err != -EDATAINTEGRITY && txmsg_cork)
err = 0; err = 0;
exit(err ? 1 : 0); exit(err ? 1 : 0);
} else if (rxpid == -1) { } else if (rxpid == -1) {