From 23704bbd1416ed1a051b32d5d44e46dd654b8ffe Mon Sep 17 00:00:00 2001 From: Steffen Date: Mon, 13 Feb 2023 18:23:44 +0100 Subject: [PATCH 3/3] examples: Use t_error instead of glibc's error. On musl systems, liburing cannot build examples and tests due to it's usage of error.h. t_error copied from test/helpers.c. Replacing calls to error() with t_error(). Closes: #786 Signed-off-by: Steffen Winter --- examples/send-zerocopy.c | 61 +++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/examples/send-zerocopy.c b/examples/send-zerocopy.c index 7f5f2b1..6092af9 100644 --- a/examples/send-zerocopy.c +++ b/examples/send-zerocopy.c @@ -5,11 +5,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -57,6 +57,23 @@ static struct sockaddr_storage cfg_dst_addr; static char payload[IP_MAXPACKET] __attribute__((aligned(4096))); +/* + * Implementation of error(3), prints an error message and exits. + */ +static void t_error(int status, int errnum, const char *format, ...) +{ + va_list args; + va_start(args, format); + + vfprintf(stderr, format, args); + if (errnum) + fprintf(stderr, ": %s", strerror(errnum)); + + fprintf(stderr, "\n"); + va_end(args); + exit(status); +} + static unsigned long gettimeofday_ms(void) { struct timeval tv; @@ -68,7 +85,7 @@ static unsigned long gettimeofday_ms(void) static void do_setsockopt(int fd, int level, int optname, int val) { if (setsockopt(fd, level, optname, &val, sizeof(val))) - error(1, errno, "setsockopt %d.%d: %d", level, optname, val); + t_error(1, errno, "setsockopt %d.%d: %d", level, optname, val); } static void setup_sockaddr(int domain, const char *str_addr, @@ -84,7 +101,7 @@ static void setup_sockaddr(int domain, const char *str_addr, addr4->sin_port = htons(cfg_port); if (str_addr && inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1) - error(1, 0, "ipv4 parse error: %s", str_addr); + t_error(1, 0, "ipv4 parse error: %s", str_addr); break; case PF_INET6: memset(addr6, 0, sizeof(*addr6)); @@ -92,10 +109,10 @@ static void setup_sockaddr(int domain, const char *str_addr, addr6->sin6_port = htons(cfg_port); if (str_addr && inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1) - error(1, 0, "ipv6 parse error: %s", str_addr); + t_error(1, 0, "ipv6 parse error: %s", str_addr); break; default: - error(1, 0, "illegal domain"); + t_error(1, 0, "illegal domain"); } } @@ -105,12 +122,12 @@ static int do_setup_tx(int domain, int type, int protocol) fd = socket(domain, type, protocol); if (fd == -1) - error(1, errno, "socket t"); + t_error(1, errno, "socket t"); do_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 1 << 21); if (connect(fd, (void *) &cfg_dst_addr, cfg_alen)) - error(1, errno, "connect"); + t_error(1, errno, "connect"); return fd; } @@ -125,7 +142,7 @@ static inline struct io_uring_cqe *wait_cqe_fast(struct io_uring *ring) ret = io_uring_wait_cqe(ring, &cqe); if (ret) - error(1, ret, "wait cqe"); + t_error(1, ret, "wait cqe"); return cqe; } @@ -143,17 +160,17 @@ static void do_tx(int domain, int type, int protocol) ret = io_uring_queue_init(512, &ring, IORING_SETUP_COOP_TASKRUN); if (ret) - error(1, ret, "io_uring: queue init"); + t_error(1, ret, "io_uring: queue init"); if (cfg_fixed_files) { ret = io_uring_register_files(&ring, &fd, 1); if (ret < 0) - error(1, ret, "io_uring: files registration"); + t_error(1, ret, "io_uring: files registration"); } if (cfg_reg_ringfd) { ret = io_uring_register_ring_fd(&ring); if (ret < 0) - error(1, ret, "io_uring: io_uring_register_ring_fd"); + t_error(1, ret, "io_uring: io_uring_register_ring_fd"); } iov.iov_base = payload; @@ -161,7 +178,7 @@ static void do_tx(int domain, int type, int protocol) ret = io_uring_register_buffers(&ring, &iov, 1); if (ret) - error(1, ret, "io_uring: buffer registration"); + t_error(1, ret, "io_uring: buffer registration"); tstop = gettimeofday_ms() + cfg_runtime_ms; do { @@ -193,14 +210,14 @@ static void do_tx(int domain, int type, int protocol) ret = io_uring_submit(&ring); if (ret != cfg_nr_reqs) - error(1, ret, "submit"); + t_error(1, ret, "submit"); for (i = 0; i < cfg_nr_reqs; i++) { cqe = wait_cqe_fast(&ring); if (cqe->flags & IORING_CQE_F_NOTIF) { if (cqe->flags & IORING_CQE_F_MORE) - error(1, -EINVAL, "F_MORE notif"); + t_error(1, -EINVAL, "F_MORE notif"); compl_cqes--; i--; io_uring_cqe_seen(&ring, cqe); @@ -217,7 +234,7 @@ static void do_tx(int domain, int type, int protocol) fprintf(stderr, "Connection failure"); goto out_fail; } else if (cqe->res != -EAGAIN) { - error(1, cqe->res, "send failed"); + t_error(1, cqe->res, "send failed"); } io_uring_cqe_seen(&ring, cqe); } @@ -226,7 +243,7 @@ static void do_tx(int domain, int type, int protocol) out_fail: shutdown(fd, SHUT_RDWR); if (close(fd)) - error(1, errno, "close"); + t_error(1, errno, "close"); fprintf(stderr, "tx=%lu (MB=%lu), tx/s=%lu (MB/s=%lu)\n", packets, bytes >> 20, @@ -254,7 +271,7 @@ static void do_test(int domain, int type, int protocol) static void usage(const char *filepath) { - error(1, 0, "Usage: %s [-n] [-z] [-s] " + t_error(1, 0, "Usage: %s [-n] [-z] [-s] " "(-4|-6) [-t