Skip to content

Commit

Permalink
Add test case for link_dispatch
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Sep 21, 2023
1 parent 1f2c3f7 commit 033563e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/libxdp/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

USER_TARGETS := test_xsk_refcnt check_kern_compat test_xdp_frags test_dispatcher_versions
USER_TARGETS := test_xsk_refcnt check_kern_compat test_xdp_frags test_dispatcher_versions test_link_dispatch
BPF_TARGETS := xdp_dispatcher_v1 xdp_pass
USER_LIBS := -lpthread

Expand Down
12 changes: 11 additions & 1 deletion lib/libxdp/tests/test-libxdp.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)

ALL_TESTS="test_link_so test_link_a test_old_dispatcher test_xdp_frags test_xsk_prog_refcnt_bpffs test_xsk_prog_refcnt_legacy test_xsk_non_privileged"
ALL_TESTS="test_link_so test_link_a test_old_dispatcher test_xdp_frags test_xsk_prog_refcnt_bpffs test_xsk_prog_refcnt_legacy test_xsk_non_privileged test_link_dispatch"

TESTS_DIR=$(dirname "${BASH_SOURCE[0]}")

Expand Down Expand Up @@ -102,6 +102,16 @@ test_xsk_non_privileged()
ip link delete xdp_veth0
}

test_link_dispatch()
{
if test ! -f $TEST_PROG_DIR/test_link_dispatch; then
exit "$SKIPPED_TEST"
fi
ip link add xdp_veth0 type veth peer name xdp_veth1
check_run $TESTS_DIR/test_link_dispatch xdp_veth0
ip link delete xdp_veth0
}

cleanup_tests()
{
ip link del dev xdp_veth_big0 >/dev/null 2>&1
Expand Down
96 changes: 96 additions & 0 deletions lib/libxdp/tests/test_link_dispatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* SPDX-License-Identifier: GPL-2.0 */

#define _GNU_SOURCE

#include <errno.h>
#include <linux/err.h>
#include <net/if.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>

#include "test_utils.h"

#include <xdp/libxdp.h>
#include <bpf/libbpf.h>


static void usage(char *progname)
{
fprintf(stderr, "Usage: %s <ifname>\n", progname);
exit(EXIT_FAILURE);
}

static int check_link_detach(int ifindex) {
struct bpf_object *obj_prog = NULL;
struct bpf_program *prog;
struct xdp_multiprog *mp;

int ret, link;

obj_prog = bpf_object__open("xdp_pass.o");
if (!obj_prog) {
ret = -errno;
goto out;
}
prog = bpf_object__find_program_by_name(obj_prog, "xdp_pass");
if (!prog) {
ret = -errno;
goto out;
}

link = bpf_link_create(bpf_program__fd(prog), 0, 0, NULL);
if (link < 0) {
ret = -errno;
fprintf(stderr, "Couldn't create link: %s\n", strerror(-ret));
goto out;
}
mp = xdp_multiprog__get_from_ifindex(ifindex);
ret = libxdp_get_error(mp);
if (ret) {
fprintf(stderr, "Couldn't get multiprog on ifindex %d: %s\n",
ifindex, strerror(-ret));
goto out;
}

ret = xdp_multiprog__detach(mp);
out:
bpf_object__close(obj_prog);
return ret;
}

int main(int argc, char **argv)
{
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
int ifindex;

if (setrlimit(RLIMIT_MEMLOCK, &r)) {
fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
strerror(errno));
exit(EXIT_FAILURE);
}

char *envval;

envval = secure_getenv("VERBOSE_TESTS");

silence_libbpf_logging();
if (envval && envval[0] == '1')
verbose_libxdp_logging();
else
silence_libxdp_logging();

if (argc != 2)
usage(argv[0]);

ifindex = if_nametoindex(argv[1]);
if (!ifindex) {
fprintf(stderr, "Interface '%s' not found.\n", argv[1]);
usage(argv[0]);
}

return check_link_detach(ifindex);
}

0 comments on commit 033563e

Please sign in to comment.