mirror of
https://github.com/intel/compute-runtime.git
synced 2026-01-07 21:27:04 +08:00
refactor: Create new helper in IPC blackbox tests
Create a single helper for exchanging IPC handles for all blackbox tests. Related-To: LOCI-3771 Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
84dec57f0a
commit
6dd7feb435
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (C) 2020-2022 Intel Corporation
|
||||
# Copyright (C) 2020-2023 Intel Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
#
|
||||
@@ -16,6 +16,14 @@ add_library(${L0_BLACK_BOX_TEST_SHARED_LIB} STATIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/zello_compile.h
|
||||
)
|
||||
|
||||
if(UNIX)
|
||||
target_sources(${L0_BLACK_BOX_TEST_SHARED_LIB}
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/zello_ipc_common.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/zello_ipc_common.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
if(NOT DEFINED L0_BLACK_BOX_OCLOC_HEADER_DIR)
|
||||
set(L0_BLACK_BOX_OCLOC_HEADER_DIR "${NEO_SOURCE_DIR}/shared/offline_compiler/source")
|
||||
endif()
|
||||
@@ -71,6 +79,9 @@ foreach(TEST_NAME ${TEST_TARGETS})
|
||||
if(${TEST_NAME} STREQUAL "zello_export_import_memory")
|
||||
continue()
|
||||
endif()
|
||||
if(${TEST_NAME} STREQUAL "zello_ipc_event")
|
||||
continue()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
int sendmsgForIpcHandle(int socket, int fd, char *payload) {
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = payload;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int recvmsgForIpcHandle(int socket, char *payload) {
|
||||
int fd = -1;
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = payload;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
memmove(&fd, CMSG_DATA(controlHeader), sizeof(int)); // NOLINT(clang-analyzer-core.NonNullParamChecker)
|
||||
return fd;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <level_zero/ze_api.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <sys/socket.h>
|
||||
|
||||
int sendmsgForIpcHandle(int socket, int fd, char *payload);
|
||||
int recvmsgForIpcHandle(int socket, char *payload);
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2022 Intel Corporation
|
||||
* Copyright (C) 2020-2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_common.h"
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@@ -18,53 +18,6 @@ int sv[CHILDPROCESSES][2];
|
||||
|
||||
size_t allocSize = 4194304;
|
||||
|
||||
static int sendmsgForIpcHandle(int socket, int fd) {
|
||||
char sendBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
|
||||
struct iovec msgBuffer = {};
|
||||
msgBuffer.iov_base = sendBuf;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on sendmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int recvmsgForIpcHandle(int socket) {
|
||||
int fd = -1;
|
||||
char recvBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = recvBuf;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on recvmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
memmove(&fd, CMSG_DATA(controlHeader), sizeof(int)); // NOLINT(clang-analyzer-core.NonNullParamChecker)
|
||||
return fd;
|
||||
}
|
||||
|
||||
inline void initializeProcess(ze_driver_handle_t &driverHandle,
|
||||
ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
@@ -142,7 +95,8 @@ void runClient(int commSocket, uint32_t clientId) {
|
||||
}
|
||||
|
||||
// receive the IPC handle for the memory from the other process
|
||||
int handle = recvmsgForIpcHandle(commSocket);
|
||||
char payload[ZE_MAX_IPC_HANDLE_SIZE];
|
||||
int handle = recvmsgForIpcHandle(commSocket, payload);
|
||||
if (handle < 0) {
|
||||
std::cerr << "Failing to get IPC memory handle from server\n";
|
||||
std::terminate();
|
||||
@@ -227,9 +181,9 @@ void runServer(bool &validRet) {
|
||||
|
||||
// transmit handle to the client
|
||||
int commSocket = sv[i][0];
|
||||
int ret = sendmsgForIpcHandle(commSocket, exportFd.fd);
|
||||
if (ret < 0) {
|
||||
std::cerr << "Failing to send IPC memory handle to client\n";
|
||||
char payload[ZE_MAX_IPC_HANDLE_SIZE];
|
||||
if (sendmsgForIpcHandle(commSocket, static_cast<int>(exportFd.fd), payload) < 0) {
|
||||
std::cerr << "Failing to send IPC event pool handle to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2022 Intel Corporation
|
||||
* Copyright (C) 2020-2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_common.h"
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@@ -18,55 +18,6 @@ int sv[CHILDPROCESSES][2];
|
||||
|
||||
size_t allocSize = 131072 + 7; // +7 to break alignment and make it harder
|
||||
|
||||
static int sendmsgForIpcHandle(int socket, int fd, char *payload) {
|
||||
char sendBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
memcpy(sendBuf, payload, sizeof(sendBuf));
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
|
||||
struct iovec msgBuffer = {};
|
||||
msgBuffer.iov_base = sendBuf;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on sendmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int recvmsgForIpcHandle(int socket, char *payload) {
|
||||
int fd = -1;
|
||||
char recvBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = recvBuf;
|
||||
msgBuffer.iov_len = ZE_MAX_IPC_HANDLE_SIZE;
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on recvmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
memmove(&fd, CMSG_DATA(controlHeader), sizeof(int)); // NOLINT(clang-analyzer-core.NonNullParamChecker)
|
||||
memmove(payload, recvBuf, sizeof(recvBuf));
|
||||
return fd;
|
||||
}
|
||||
|
||||
inline void initializeProcess(ze_driver_handle_t &driverHandle,
|
||||
ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2022 Intel Corporation
|
||||
* Copyright (C) 2020-2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_common.h"
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
@@ -18,62 +19,6 @@ uint8_t uinitializedPattern = 1;
|
||||
uint8_t expectedPattern = 7;
|
||||
size_t allocSize = 4096 + 7; // +7 to break alignment and make it harder
|
||||
|
||||
static int sendmsgFd(int socket, int fd) {
|
||||
char sendBuf[sizeof(ze_ipc_mem_handle_t)] = {};
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = sendBuf;
|
||||
msgBuffer.iov_len = sizeof(*sendBuf);
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int recvmsgFd(int socket) {
|
||||
int fd = -1;
|
||||
char recvBuf[sizeof(ze_ipc_mem_handle_t)] = {};
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = recvBuf;
|
||||
msgBuffer.iov_len = sizeof(recvBuf);
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
if (!CMSG_DATA(controlHeader)) {
|
||||
return -1;
|
||||
}
|
||||
memmove(&fd, CMSG_DATA(controlHeader), sizeof(int));
|
||||
return fd;
|
||||
}
|
||||
|
||||
inline void initializeProcess(ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
ze_command_queue_handle_t &cmdQueue,
|
||||
@@ -218,13 +163,13 @@ void runClient(int commSocket) {
|
||||
SUCCESS_OR_TERMINATE(zeCommandQueueSynchronize(cmdQueue, std::numeric_limits<uint64_t>::max()));
|
||||
|
||||
// get the dma_buf from the other process
|
||||
int dmaBufFd = recvmsgFd(commSocket);
|
||||
ze_ipc_mem_handle_t pIpcHandle = {};
|
||||
int dmaBufFd = recvmsgForIpcHandle(commSocket, pIpcHandle.data);
|
||||
if (dmaBufFd < 0) {
|
||||
std::cerr << "Failing to get dma_buf fd from server\n";
|
||||
std::cerr << "Failing to get IPC memory handle from server\n";
|
||||
std::terminate();
|
||||
}
|
||||
ze_ipc_mem_handle_t pIpcHandle;
|
||||
memcpy(&pIpcHandle, static_cast<void *>(&dmaBufFd), sizeof(dmaBufFd));
|
||||
memcpy(pIpcHandle.data, &dmaBufFd, sizeof(dmaBufFd));
|
||||
|
||||
// get a memory pointer to the BO associated with the dma_buf
|
||||
void *zeIpcBuffer;
|
||||
@@ -277,9 +222,9 @@ void runServer(int commSocket, bool &validRet) {
|
||||
|
||||
// Pass the dma_buf to the other process
|
||||
int dmaBufFd;
|
||||
memcpy(static_cast<void *>(&dmaBufFd), &pIpcHandle, sizeof(dmaBufFd));
|
||||
if (sendmsgFd(commSocket, static_cast<int>(dmaBufFd)) < 0) {
|
||||
std::cerr << "Failing to send dma_buf fd to client\n";
|
||||
memcpy(static_cast<void *>(&dmaBufFd), pIpcHandle.data, sizeof(dmaBufFd));
|
||||
if (sendmsgForIpcHandle(commSocket, dmaBufFd, pIpcHandle.data) < 0) {
|
||||
std::cerr << "Failing to send IPC handle to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 Intel Corporation
|
||||
* Copyright (C) 2021-2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_common.h"
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
@@ -18,8 +19,6 @@ int sv[CHILDPROCESSES][2];
|
||||
|
||||
size_t allocSize = 4096 + 7; // +7 to break alignment and make it harder
|
||||
|
||||
// Helpers to send and receive the IPC handles.
|
||||
//
|
||||
// L0 uses a vector of ZE_MAX_IPC_HANDLE_SIZE bytes to send the IPC handle
|
||||
// char data[ZE_MAX_IPC_HANDLE_SIZE];
|
||||
// First four bytes (which is the sizeof(int)) of it contain the file descriptor
|
||||
@@ -28,75 +27,10 @@ size_t allocSize = 4096 + 7; // +7 to break alignment and make it harder
|
||||
// For instance, the payload in the event pool's IPC handle contains the
|
||||
// number of events the pool has.
|
||||
|
||||
static int sendmsgForIpcHandle(int socket, char *payload) {
|
||||
int fd = 0;
|
||||
memcpy(&fd, payload, sizeof(fd));
|
||||
|
||||
char sendBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
memcpy(sendBuf, payload + sizeof(int), sizeof(sendBuf) - sizeof(int));
|
||||
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
|
||||
struct iovec msgBuffer = {};
|
||||
msgBuffer.iov_base = sendBuf;
|
||||
msgBuffer.iov_len = sizeof(*sendBuf);
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on sendmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int recvmsgForIpcHandle(int socket, char *payload) {
|
||||
int fd = -1;
|
||||
char recvBuf[ZE_MAX_IPC_HANDLE_SIZE] = {};
|
||||
char cmsgBuf[CMSG_SPACE(ZE_MAX_IPC_HANDLE_SIZE)];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = recvBuf;
|
||||
msgBuffer.iov_len = sizeof(recvBuf);
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
std::cerr << "Error on recvmsgForIpcHandle " << strerror(errno) << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
memcpy(&fd, CMSG_DATA(controlHeader), sizeof(int));
|
||||
|
||||
memcpy(payload, &fd, sizeof(fd));
|
||||
memcpy(payload + sizeof(int), recvBuf, sizeof(recvBuf) - sizeof(int));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline void initializeProcess(ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
ze_command_queue_handle_t &cmdQueue,
|
||||
ze_command_list_handle_t &cmdList) {
|
||||
void initializeProcess(ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
ze_command_queue_handle_t &cmdQueue,
|
||||
ze_command_list_handle_t &cmdList) {
|
||||
auto devices = zelloInitContextAndGetDevices(context);
|
||||
device = devices[0];
|
||||
|
||||
@@ -126,24 +60,26 @@ void runClient(int commSocket, uint32_t clientId) {
|
||||
|
||||
// receieve the IPC handle for the memory from the other process
|
||||
ze_ipc_mem_handle_t pIpcHandle = {};
|
||||
int ret = recvmsgForIpcHandle(commSocket, pIpcHandle.data);
|
||||
if (ret < 0) {
|
||||
int dmaBufFd = recvmsgForIpcHandle(commSocket, pIpcHandle.data);
|
||||
if (dmaBufFd < 0) {
|
||||
std::cerr << "Failing to get IPC memory handle from server\n";
|
||||
std::terminate();
|
||||
}
|
||||
memcpy(pIpcHandle.data, &dmaBufFd, sizeof(dmaBufFd));
|
||||
|
||||
// get the allocation associated with the IPC handle
|
||||
void *zeIpcBuffer;
|
||||
void *zeIpcBuffer = nullptr;
|
||||
SUCCESS_OR_TERMINATE(zeMemOpenIpcHandle(context, device, pIpcHandle,
|
||||
0u, &zeIpcBuffer));
|
||||
|
||||
// receieve the IPC handle for the event pool from the other process
|
||||
ze_ipc_event_pool_handle_t pIpcEventPoolHandle = {};
|
||||
ret = recvmsgForIpcHandle(commSocket, pIpcEventPoolHandle.data);
|
||||
if (ret < 0) {
|
||||
dmaBufFd = recvmsgForIpcHandle(commSocket, pIpcEventPoolHandle.data);
|
||||
if (dmaBufFd < 0) {
|
||||
std::cerr << "Failing to get IPC event pool handle from server\n";
|
||||
std::terminate();
|
||||
}
|
||||
memcpy(pIpcEventPoolHandle.data, &dmaBufFd, sizeof(dmaBufFd));
|
||||
|
||||
// get the event pool associated with the IPC handle
|
||||
ze_event_pool_handle_t eventPool = {};
|
||||
@@ -245,24 +181,25 @@ void runServer(bool &validRet) {
|
||||
SUCCESS_OR_TERMINATE(zeCommandListReset(cmdList));
|
||||
|
||||
// Get the IPC handle for the previously allocated pointer
|
||||
ze_ipc_mem_handle_t pIpcHandle;
|
||||
ze_ipc_mem_handle_t pIpcHandle{};
|
||||
SUCCESS_OR_TERMINATE(zeMemGetIpcHandle(context, zeBuffer, &pIpcHandle));
|
||||
|
||||
// Pass the IPC handle to the other process
|
||||
int commSocket = sv[i][0];
|
||||
int ret = sendmsgForIpcHandle(commSocket, pIpcHandle.data);
|
||||
if (ret < 0) {
|
||||
std::cerr << "Failing to send IPC memory handle to client\n";
|
||||
int dmaBufFd;
|
||||
memcpy(static_cast<void *>(&dmaBufFd), pIpcHandle.data, sizeof(dmaBufFd));
|
||||
if (sendmsgForIpcHandle(commSocket, dmaBufFd, pIpcHandle.data) < 0) {
|
||||
std::cerr << "Failing to send IPC handle to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
// Get the IPC handle for the event pool
|
||||
ze_ipc_event_pool_handle_t pIpcEventPoolHandle;
|
||||
ze_ipc_event_pool_handle_t pIpcEventPoolHandle{};
|
||||
SUCCESS_OR_TERMINATE(zeEventPoolGetIpcHandle(eventPool, &pIpcEventPoolHandle));
|
||||
|
||||
// Pass the IPC handle to the other process
|
||||
ret = sendmsgForIpcHandle(commSocket, pIpcEventPoolHandle.data);
|
||||
if (ret) {
|
||||
memcpy(static_cast<void *>(&dmaBufFd), &pIpcEventPoolHandle, sizeof(dmaBufFd));
|
||||
if (sendmsgForIpcHandle(commSocket, dmaBufFd, pIpcEventPoolHandle.data) < 0) {
|
||||
std::cerr << "Failing to send IPC event pool handle to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 Intel Corporation
|
||||
* Copyright (C) 2021-2023 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "zello_common.h"
|
||||
#include "zello_ipc_common.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@@ -18,57 +18,6 @@ size_t allocSize = 4096 + 7; // +7 to break alignment and make it harder
|
||||
uint32_t serverDevice = 0;
|
||||
uint32_t clientDevice = 1;
|
||||
|
||||
static int sendmsgFd(int socket, int fd, char *payload, size_t payloadLen) {
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = payload;
|
||||
msgBuffer.iov_len = payloadLen;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
controlHeader->cmsg_type = SCM_RIGHTS;
|
||||
controlHeader->cmsg_level = SOL_SOCKET;
|
||||
controlHeader->cmsg_len = CMSG_LEN(sizeof(fd));
|
||||
|
||||
*(int *)CMSG_DATA(controlHeader) = fd;
|
||||
ssize_t bytesSent = sendmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int recvmsgFd(int socket, char *payload, size_t payloadLen) {
|
||||
int fd = -1;
|
||||
char cmsgBuf[CMSG_SPACE(sizeof(ze_ipc_mem_handle_t))];
|
||||
|
||||
struct iovec msgBuffer;
|
||||
msgBuffer.iov_base = payload;
|
||||
msgBuffer.iov_len = payloadLen;
|
||||
|
||||
struct msghdr msgHeader = {};
|
||||
msgHeader.msg_iov = &msgBuffer;
|
||||
msgHeader.msg_iovlen = 1;
|
||||
msgHeader.msg_control = cmsgBuf;
|
||||
msgHeader.msg_controllen = CMSG_LEN(sizeof(fd));
|
||||
|
||||
ssize_t bytesSent = recvmsg(socket, &msgHeader, 0);
|
||||
if (bytesSent < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct cmsghdr *controlHeader = CMSG_FIRSTHDR(&msgHeader);
|
||||
memmove(&fd, CMSG_DATA(controlHeader), sizeof(int));
|
||||
return fd;
|
||||
}
|
||||
|
||||
inline void initializeProcess(ze_context_handle_t &context,
|
||||
ze_device_handle_t &device,
|
||||
ze_command_queue_handle_t &cmdQueue,
|
||||
@@ -165,8 +114,8 @@ void runClient(int commSocket) {
|
||||
|
||||
// receieve the IPC handle for the event pool from the other process
|
||||
ze_ipc_event_pool_handle_t pIpcEventPoolHandle = {};
|
||||
int dma_buf_fd = recvmsgFd(commSocket, pIpcEventPoolHandle.data, ZE_MAX_IPC_HANDLE_SIZE);
|
||||
if (dma_buf_fd < 0) {
|
||||
int dmaBufFd = recvmsgForIpcHandle(commSocket, pIpcEventPoolHandle.data);
|
||||
if (dmaBufFd < 0) {
|
||||
std::cerr << "Failing to get IPC event pool handle from server\n";
|
||||
std::terminate();
|
||||
}
|
||||
@@ -208,12 +157,12 @@ void runClient(int commSocket) {
|
||||
|
||||
// get the dma_buf from the other process
|
||||
ze_ipc_mem_handle_t pIpcHandle;
|
||||
dma_buf_fd = recvmsgFd(commSocket, pIpcHandle.data, ZE_MAX_IPC_HANDLE_SIZE);
|
||||
if (dma_buf_fd < 0) {
|
||||
dmaBufFd = recvmsgForIpcHandle(commSocket, pIpcHandle.data);
|
||||
if (dmaBufFd < 0) {
|
||||
std::cerr << "Failing to get dma_buf fd from server\n";
|
||||
std::terminate();
|
||||
}
|
||||
memcpy(&pIpcHandle, static_cast<void *>(&dma_buf_fd), sizeof(dma_buf_fd));
|
||||
memcpy(&pIpcHandle, static_cast<void *>(&dmaBufFd), sizeof(dmaBufFd));
|
||||
|
||||
// get a memory pointer to the BO associated with the dma_buf
|
||||
void *zeIpcBuffer;
|
||||
@@ -276,9 +225,9 @@ void runServer(int commSocket, bool &validRet) {
|
||||
SUCCESS_OR_TERMINATE(zeEventPoolGetIpcHandle(eventPool, &pIpcEventPoolHandle));
|
||||
|
||||
// Pass the IPC handle to the other process
|
||||
int dma_buf_fd;
|
||||
memcpy(static_cast<void *>(&dma_buf_fd), &pIpcEventPoolHandle, sizeof(dma_buf_fd));
|
||||
if (sendmsgFd(commSocket, static_cast<int>(dma_buf_fd), pIpcEventPoolHandle.data, ZE_MAX_IPC_HANDLE_SIZE) < 0) {
|
||||
int dmaBufFd;
|
||||
memcpy(static_cast<void *>(&dmaBufFd), &pIpcEventPoolHandle, sizeof(dmaBufFd));
|
||||
if (sendmsgForIpcHandle(commSocket, static_cast<int>(dmaBufFd), pIpcEventPoolHandle.data) < 0) {
|
||||
std::cerr << "Failing to send IPC event pool handle to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
@@ -301,8 +250,8 @@ void runServer(int commSocket, bool &validRet) {
|
||||
SUCCESS_OR_TERMINATE(zeMemGetIpcHandle(context, zeBuffer, &pIpcHandle));
|
||||
|
||||
// Pass the dma_buf to the other process
|
||||
memcpy(static_cast<void *>(&dma_buf_fd), &pIpcHandle, sizeof(dma_buf_fd));
|
||||
if (sendmsgFd(commSocket, static_cast<int>(dma_buf_fd), pIpcHandle.data, ZE_MAX_IPC_HANDLE_SIZE) < 0) {
|
||||
memcpy(static_cast<void *>(&dmaBufFd), &pIpcHandle, sizeof(dmaBufFd));
|
||||
if (sendmsgForIpcHandle(commSocket, static_cast<int>(dmaBufFd), pIpcHandle.data) < 0) {
|
||||
std::cerr << "Failing to send dma_buf fd to client\n";
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user