diff --git a/src/libknot/xdp/bpf-user.h b/src/libknot/xdp/bpf-user.h index 43964b75f9232e20a799e7ad1e55825ce946a8e8..80522e18c9ab372d91396c153453c7ca3ba2c19c 100644 --- a/src/libknot/xdp/bpf-user.h +++ b/src/libknot/xdp/bpf-user.h @@ -76,7 +76,7 @@ struct knot_xdp_socket { const struct kxsk_iface *iface; /*! If non-NULL, it's a mocked socket with this send function. */ - void *send_mock; + int (*send_mock)(struct knot_xdp_socket *, const knot_xdp_msg_t[], uint32_t, uint32_t *); /*! The kernel has to be woken up by a syscall indication. */ bool kernel_needs_wakeup; diff --git a/src/libknot/xdp/xdp.c b/src/libknot/xdp/xdp.c index 9921e99023534976ddaa19f8156499a2b7206088..80100b4c86db773ef120f98ba3fadedfc5c65553 100644 --- a/src/libknot/xdp/xdp.c +++ b/src/libknot/xdp/xdp.c @@ -199,23 +199,6 @@ int knot_xdp_init(knot_xdp_socket_t **socket, const char *if_name, int if_queue, return ret; } -_public_ -int knot_xdp_init_mock(knot_xdp_socket_t **socket, knot_xdp_send_mock_f send_mock) -{ - if (socket == NULL || send_mock == NULL) { - return KNOT_EINVAL; - } - - *socket = calloc(1, sizeof(**socket)); - if (*socket == NULL) { - return KNOT_ENOMEM; - } - - (*socket)->send_mock = send_mock; - - return KNOT_EOK; -} - _public_ void knot_xdp_deinit(knot_xdp_socket_t *socket) { @@ -355,8 +338,7 @@ int knot_xdp_send(knot_xdp_socket_t *socket, const knot_xdp_msg_t msgs[], return KNOT_EINVAL; } if (unlikely(socket->send_mock != NULL)) { - knot_xdp_send_mock_f send_mock = socket->send_mock; - int ret = send_mock(socket, msgs, count, sent); + int ret = socket->send_mock(socket, msgs, count, sent); for (uint32_t i = 0; i < count; ++i) { free_unsent(socket, &msgs[i]); } diff --git a/src/libknot/xdp/xdp.h b/src/libknot/xdp/xdp.h index 0c5f21e167e90d70186ecda8b90a18f560ea5bd2..019c451d0308763ffba79d97d245365f2e15c937 100644 --- a/src/libknot/xdp/xdp.h +++ b/src/libknot/xdp/xdp.h @@ -51,14 +51,6 @@ typedef enum { /*! \brief Context structure for one XDP socket. */ typedef struct knot_xdp_socket knot_xdp_socket_t; -/*! - * \brief A mocked XDP send function. - * - * \note This must correspond to the prototype of knot_xdp_send(). - */ -typedef int (*knot_xdp_send_mock_f)(knot_xdp_socket_t *, const knot_xdp_msg_t[], - uint32_t, uint32_t *); - /*! * \brief Initialize XDP socket. * @@ -73,16 +65,6 @@ typedef int (*knot_xdp_send_mock_f)(knot_xdp_socket_t *, const knot_xdp_msg_t[], int knot_xdp_init(knot_xdp_socket_t **socket, const char *if_name, int if_queue, uint32_t listen_port, knot_xdp_load_bpf_t load_bpf); -/*! - * \brief Initialize mocked XDP socket. - * - * \param socket XDP socket. - * \param send_mock Mocked send function. - * - * \return KNOT_E* - */ -int knot_xdp_init_mock(knot_xdp_socket_t **socket, knot_xdp_send_mock_f send_mock); - /*! * \brief De-init XDP socket. * diff --git a/tests/Makefile.am b/tests/Makefile.am index 60f025e02c49207fcb0a968d4a2be0ec46d05bf2..07ff76e5b6625d1004b158fb85802054285f44c7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -156,6 +156,7 @@ check_PROGRAMS += \ libknot/test_wire if ENABLE_XDP +AM_CPPFLAGS += $(libbpf_CFLAGS) check_PROGRAMS += \ libknot/test_xdp_tcp endif ENABLE_XDP diff --git a/tests/libknot/test_xdp_tcp.c b/tests/libknot/test_xdp_tcp.c index 03a6d00755b2553c53732b3544a16c5f61647a9a..3b0abb513ac6c3fb7e4e5a9adec4a82d439d002a 100644 --- a/tests/libknot/test_xdp_tcp.c +++ b/tests/libknot/test_xdp_tcp.c @@ -20,8 +20,9 @@ #include "contrib/macros.h" #include "libknot/error.h" #include "libknot/xdp/msg_init.h" - #include "libknot/xdp/tcp.c" +#include "libknot/xdp/tcp_iobuf.c" +#include "libknot/xdp/bpf-user.h" knot_tcp_table_t *test_table = NULL; #define TEST_TABLE_SIZE 100 @@ -449,6 +450,14 @@ void test_ibufs_size(void) clean_table(); } +static void init_mock(knot_xdp_socket_t **socket, void *send_mock) +{ + *socket = calloc(1, sizeof(**socket)); + if (*socket != NULL) { + (*socket)->send_mock = send_mock; + } +} + int main(int argc, char *argv[]) { UNUSED(argc); @@ -458,8 +467,7 @@ int main(int argc, char *argv[]) test_table = knot_tcp_table_new(TEST_TABLE_SIZE); assert(test_table != NULL); - int ret = knot_xdp_init_mock(&test_sock, mock_send); - assert(ret == KNOT_EOK); + init_mock(&test_sock, mock_send); test_syn(); test_establish(); @@ -471,8 +479,7 @@ int main(int argc, char *argv[]) test_ibufs_size(); knot_xdp_deinit(test_sock); - ret = knot_xdp_init_mock(&test_sock, mock_send_nocheck); - assert(ret == KNOT_EOK); + init_mock(&test_sock, mock_send_nocheck); test_many(); knot_xdp_deinit(test_sock);