The BPF ELF tests sporadically fail with EINVAL when loading from
the temporary file. This is a race condition where the BPF loader
reads the file before the data is fully flushed to disk.

Add fsync() before close() in create_temp_bpf_file() to ensure the
BPF object data is visible on the filesystem before attempting to
load it.

Also fix two related issues found during review
- Add missing TEST_ASSERT for mempool creation in test_bpf_elf_tx_load
- Initialize port variable in test_bpf_elf_rx_load to avoid undefined
  behavior in cleanup path if null_vdev_setup fails early

Fixes: cf1e03f881af ("test/bpf: add ELF loading")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 app/test/test_bpf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/app/test/test_bpf.c b/app/test/test_bpf.c
index a7d56f8d86..03705075d8 100644
--- a/app/test/test_bpf.c
+++ b/app/test/test_bpf.c
@@ -3311,6 +3311,8 @@ create_temp_bpf_file(const uint8_t *data, size_t size, 
const char *name)
 
        /* Write BPF object data */
        written = write(fd, data, size);
+       if (written == (ssize_t)size)
+               fsync(fd);
        close(fd);
 
        if (written != (ssize_t)size) {
@@ -3580,6 +3582,7 @@ test_bpf_elf_tx_load(void)
        mb_pool = rte_pktmbuf_pool_create("bpf_tx_test_pool", BPF_TEST_POOLSIZE,
                                          0, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
                                          SOCKET_ID_ANY);
+       TEST_ASSERT(mb_pool != NULL, "failed to create mempool");
 
        ret = null_vdev_setup(null_dev, &port, mb_pool);
        if (ret != 0)
@@ -3664,7 +3667,7 @@ test_bpf_elf_rx_load(void)
        static const char null_dev[] = "net_null_bpf0";
        struct rte_mempool *pool = NULL;
        char *tmpfile = NULL;
-       uint16_t port;
+       uint16_t port = UINT16_MAX;
        int ret;
 
        printf("%s start\n", __func__);
-- 
2.51.0

Reply via email to