Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Modified bpfWrapper to properly attach the xdp program
Browse files Browse the repository at this point in the history
Signed-off-by: phansGithub <[email protected]>
  • Loading branch information
phansGithub committed Oct 10, 2023
1 parent 2426d7c commit d30cc6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 7 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ clangformat:
@echo "****** Clang Format ******"
@echo
-clang-format -i -style=file internal/bpf/*.c internal/bpf/*.h
# -clang-format -i -style=file internal/bpf/xdp-pass/*.c
@echo
@echo

Expand All @@ -43,7 +42,6 @@ buildc:
gcc ./internal/bpf/bpfWrapper.c -lxdp -c -o ./internal/bpf/bpfWrapper.o
ar rs ./internal/bpf/libwrapper.a ./internal/bpf/bpfWrapper.o &> /dev/null
@echo "****** Build xdp_pass ******"
# make -C ./internal/bpf/xdp-pass/
@echo
@echo
@echo
Expand All @@ -68,24 +66,24 @@ build: builddp buildcni
docker: ## Build docker image
@echo "****** Docker Image ******"
@echo
docker build -t afxdp-device-plugin -f images/amd64.dockerfile .
docker build -t localhost:5000/afxdp-device-plugin -f images/amd64.dockerfile .
@echo
@echo

podman: ## Build podman image
@echo "****** Podman Image ******"
@echo
podman build -t afxdp-device-plugin -f images/amd64.dockerfile .
podman build -t localhost:5000/afxdp-device-plugin -f images/amd64.dockerfile .
@echo
@echo

image:
if $(MAKE) podman; then \
echo "Podman build succeeded"; \
else \
echo "Podman build failed, trying docker.."; \
# if $(MAKE) podman; then \
# echo "Podman build succeeded"; \
# else \
# echo "Podman build failed, trying docker.."; \
$(MAKE) docker; \
fi
# fi

undeploy: ## Undeploy the Deamonset
@echo "****** Stop Daemonset ******"
Expand Down
20 changes: 13 additions & 7 deletions internal/bpf/bpfWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
int prog_fd = -1, err, ifindex;
char *filename = "/afxdp/xdp_pass.o";
struct bpf_object *obj;
struct xdp_program *prog;
__u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE;

Log_Info("%s: disovering if_index for interface %s", __FUNCTION__, ifname);
Expand All @@ -191,17 +192,22 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
__FUNCTION__, ifname, ifindex);

/* Load the BPF program */
err = bpf_xdp_query_id(ifindex, (int)xdp_flags, &prog_fd);
if (err < 0) {
Log_Error("%s: Couldn't load BPF-OBJ file(%s)\n", __FUNCTION__, filename);
return -1;
prog = xdp_program__open_file(filename, NULL, NULL);
err = libxdp_get_error(prog);
if (err) {
libxdp_strerror(err, "Couldn’t load XDP program",
sizeof("Couldn’t load XDP program"));
Log_Error("%s: Couldn’t load XDP program\n", __FUNCTION__, filename);
return err;
}

/* Attach the program to the interface at the xdp hook */
err = bpf_xdp_attach(ifindex, prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST, NULL);
if (err < 0) {
err = xdp_program__attach(prog, ifindex, XDP_FLAGS_UPDATE_IF_NOEXIST, 0);
if (err) {
libxdp_strerror(err, "Couldn't attach the xdp pass program",
sizeof("Couldn't attach the xdp pass program"));
Log_Error("%s: Couldn't attach the XDP PASS PROGRAM TO %s\n", __FUNCTION__, ifname);
return -1;
return err;
}

Log_Info("%s: xdp-pass program loaded on %s (%d)", __FUNCTION__, ifname, ifindex);
Expand Down

0 comments on commit d30cc6c

Please sign in to comment.