1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Some common code for MSG_ZEROCOPY logic
4 * Copyright (C) 2023 SberDevices.
6 * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <linux/errqueue.h>
15 #include "msg_zerocopy_common.h"
17 void enable_so_zerocopy(int fd)
21 if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
27 void vsock_recv_completion(int fd, const bool *zerocopied)
29 struct sock_extended_err *serr;
30 struct msghdr msg = { 0 };
35 msg.msg_control = cmsg_data;
36 msg.msg_controllen = sizeof(cmsg_data);
38 res = recvmsg(fd, &msg, MSG_ERRQUEUE);
40 fprintf(stderr, "failed to read error queue: %zi\n", res);
44 cm = CMSG_FIRSTHDR(&msg);
46 fprintf(stderr, "cmsg: no cmsg\n");
50 if (cm->cmsg_level != SOL_VSOCK) {
51 fprintf(stderr, "cmsg: unexpected 'cmsg_level'\n");
55 if (cm->cmsg_type != VSOCK_RECVERR) {
56 fprintf(stderr, "cmsg: unexpected 'cmsg_type'\n");
60 serr = (void *)CMSG_DATA(cm);
61 if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
62 fprintf(stderr, "serr: wrong origin: %u\n", serr->ee_origin);
67 fprintf(stderr, "serr: wrong error code: %u\n", serr->ee_errno);
71 /* This flag is used for tests, to check that transmission was
72 * performed as expected: zerocopy or fallback to copy. If NULL
78 if (*zerocopied && (serr->ee_code & SO_EE_CODE_ZEROCOPY_COPIED)) {
79 fprintf(stderr, "serr: was copy instead of zerocopy\n");
83 if (!*zerocopied && !(serr->ee_code & SO_EE_CODE_ZEROCOPY_COPIED)) {
84 fprintf(stderr, "serr: was zerocopy instead of copy\n");