selftests: drv-net: Factor out ksft C helpers

Factor ksft C helpers to a header so they can be used by other C-based
tests.

Signed-off-by: Joe Damato <jdamato@fastly.com>
Link: https://patch.msgid.link/20250424002746.16891-3-jdamato@fastly.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
pull/1112/head
Joe Damato 2025-04-24 00:27:32 +00:00 committed by Jakub Kicinski
parent f71c549b26
commit 2b6d490b82
2 changed files with 58 additions and 47 deletions

View File

@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0 */
#if !defined(__NET_KSFT_H__)
#define __NET_KSFT_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static inline void ksft_ready(void)
{
const char msg[7] = "ready\n";
char *env_str;
int fd;
env_str = getenv("KSFT_READY_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
env_str);
return;
}
} else {
fd = STDOUT_FILENO;
}
write(fd, msg, sizeof(msg));
if (fd != STDOUT_FILENO)
close(fd);
}
static inline void ksft_wait(void)
{
char *env_str;
char byte;
int fd;
env_str = getenv("KSFT_WAIT_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
env_str);
return;
}
} else {
/* Not running in KSFT env, wait for input from STDIN instead */
fd = STDIN_FILENO;
}
read(fd, &byte, sizeof(byte));
if (fd != STDIN_FILENO)
close(fd);
}
#endif

View File

@ -11,56 +11,11 @@
#include <net/if.h> #include <net/if.h>
#include <inttypes.h> #include <inttypes.h>
#include "ksft.h"
#define UMEM_SZ (1U << 16) #define UMEM_SZ (1U << 16)
#define NUM_DESC (UMEM_SZ / 2048) #define NUM_DESC (UMEM_SZ / 2048)
/* Move this to a common header when reused! */
static void ksft_ready(void)
{
const char msg[7] = "ready\n";
char *env_str;
int fd;
env_str = getenv("KSFT_READY_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
env_str);
return;
}
} else {
fd = STDOUT_FILENO;
}
write(fd, msg, sizeof(msg));
if (fd != STDOUT_FILENO)
close(fd);
}
static void ksft_wait(void)
{
char *env_str;
char byte;
int fd;
env_str = getenv("KSFT_WAIT_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
env_str);
return;
}
} else {
/* Not running in KSFT env, wait for input from STDIN instead */
fd = STDIN_FILENO;
}
read(fd, &byte, sizeof(byte));
if (fd != STDIN_FILENO)
close(fd);
}
/* this is a simple helper program that creates an XDP socket and does the /* this is a simple helper program that creates an XDP socket and does the
* minimum necessary to get bind() to succeed. * minimum necessary to get bind() to succeed.