linux-stable/tools/testing/vsock/msg_zerocopy_common.c
Arseniy Krasnov bc36442ef3 test/vsock: MSG_ZEROCOPY flag tests
This adds three tests for MSG_ZEROCOPY feature:
1) SOCK_STREAM tx with different buffers.
2) SOCK_SEQPACKET tx with different buffers.
3) SOCK_STREAM test to read empty error queue of the socket.

Patch also works as preparation for the next patches for tools in this
patchset: vsock_perf and vsock_uring_test:
1) Adds several new functions to util.c - they will be also used by
   vsock_uring_test.
2) Adds two new functions for MSG_ZEROCOPY handling to a new source
   file - such source will be shared between vsock_test, vsock_perf and
   vsock_uring_test, thus avoiding code copy-pasting.

Signed-off-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2023-10-15 13:19:42 +01:00

87 lines
2 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/* Some common code for MSG_ZEROCOPY logic
*
* Copyright (C) 2023 SberDevices.
*
* Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/errqueue.h>
#include "msg_zerocopy_common.h"
void enable_so_zerocopy(int fd)
{
int val = 1;
if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
}
void vsock_recv_completion(int fd, const bool *zerocopied)
{
struct sock_extended_err *serr;
struct msghdr msg = { 0 };
char cmsg_data[128];
struct cmsghdr *cm;
ssize_t res;
msg.msg_control = cmsg_data;
msg.msg_controllen = sizeof(cmsg_data);
res = recvmsg(fd, &msg, MSG_ERRQUEUE);
if (res) {
fprintf(stderr, "failed to read error queue: %zi\n", res);
exit(EXIT_FAILURE);
}
cm = CMSG_FIRSTHDR(&msg);
if (!cm) {
fprintf(stderr, "cmsg: no cmsg\n");
exit(EXIT_FAILURE);
}
if (cm->cmsg_level != SOL_VSOCK) {
fprintf(stderr, "cmsg: unexpected 'cmsg_level'\n");
exit(EXIT_FAILURE);
}
if (cm->cmsg_type != VSOCK_RECVERR) {
fprintf(stderr, "cmsg: unexpected 'cmsg_type'\n");
exit(EXIT_FAILURE);
}
serr = (void *)CMSG_DATA(cm);
if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
fprintf(stderr, "serr: wrong origin: %u\n", serr->ee_origin);
exit(EXIT_FAILURE);
}
if (serr->ee_errno) {
fprintf(stderr, "serr: wrong error code: %u\n", serr->ee_errno);
exit(EXIT_FAILURE);
}
/* This flag is used for tests, to check that transmission was
* performed as expected: zerocopy or fallback to copy. If NULL
* - don't care.
*/
if (!zerocopied)
return;
if (*zerocopied && (serr->ee_code & SO_EE_CODE_ZEROCOPY_COPIED)) {
fprintf(stderr, "serr: was copy instead of zerocopy\n");
exit(EXIT_FAILURE);
}
if (!*zerocopied && !(serr->ee_code & SO_EE_CODE_ZEROCOPY_COPIED)) {
fprintf(stderr, "serr: was zerocopy instead of copy\n");
exit(EXIT_FAILURE);
}
}