Skip to content

Commit

Permalink
bpfwrapper: use libxdp for redirect program
Browse files Browse the repository at this point in the history
Signed-off-by: Maryam Tahhan <[email protected]>
  • Loading branch information
maryamtahhan committed Oct 17, 2023
1 parent a147850 commit a16bb4f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
26 changes: 26 additions & 0 deletions examples/cndp-0-0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# A working example of BPF MAP PINNING
apiVersion: v1
kind: Pod
metadata:
name: cndp-0-0
annotations:
k8s.v1.cni.cncf.io/networks: afxdp-network
spec:
containers:
- name: cndp-0
command: ["/bin/bash"]
args: ["-c", "./jsonc_gen.sh -kp ; cndpfwd -c config.jsonc lb;"]
image: quay.io/mtahhan/cndp-map-pinning:latest
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
#capabilities:
#add:
# - NET_RAW
# - IPC_LOCK
# - BPF
resources:
requests:
afxdp/myPool: '1'
limits:
afxdp/myPool: '1'
58 changes: 29 additions & 29 deletions internal/bpf/bpfWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int Configure_busy_poll(int fd, int busy_timeout, int busy_budget) {
}

Log_Warning("%s: setsockopt failure, attempting to restore xsk to default state",
__FUNCTION__);
__FUNCTION__);

Log_Warning("%s: unsetting SO_BUSY_POLL on file descriptor %d", __FUNCTION__, fd);

Expand Down Expand Up @@ -191,6 +191,12 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
}
Log_Info("%s: if_index for interface %s is %d", __FUNCTION__, ifname, ifindex);

if (access(filename, O_RDONLY) < 0) {
Log_Error("%s:error accessing file %s: %s\n", __FUNCTION__, filename,
strerror(errno));
return err;
}

Log_Info("%s: starting setup of xdp-pass program on "
"interface %s (%d)",
__FUNCTION__, ifname, ifindex);
Expand Down Expand Up @@ -242,42 +248,36 @@ int Load_bpf_pin_xsk_map(char *ifname, char *pin_path) {
return err;
}

obj = bpf_object__open_file(filename, &bpf_opts);
err = libbpf_get_error(obj);
if (err) {
Log_Error("%s: Couldn't open file(%s)\n", __FUNCTION__, filename);
return err;
}

prog = bpf_object__find_program_by_name(obj, prog_name);
if (!prog) {
Log_Error("%s: Couldn't find xdp program in bpf object!\n", __FUNCTION__);
err = -ENOENT;
return err;
}
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
Log_Info("%s: starting setup of xdp-redirect program on "
"interface %s (%d)",
__FUNCTION__, ifname, ifindex);

err = bpf_object__load(obj);
/* Load the BPF program */
prog = xdp_program__open_file(filename, NULL, NULL);
err = libxdp_get_error(prog);
if (err) {
Log_Error("%s: Couldn't load BPF-OBJ file(%s) %s\n", __FUNCTION__, filename,
strerror(errno));
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;
}

Log_Info("%s: bpf: Attach prog to ifindex %d\n", __FUNCTION__, ifindex);
link = bpf_program__attach_xdp(prog, ifindex);
if (!link) {
Log_Error("%s:ERROR: failed to attach program to %s\n", __FUNCTION__, ifname);
/* Attach the program to the interface at the xdp hook */
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 err;
}

if (bpf_map__is_pinned(bpf_object__find_map_by_name(obj, "xsks_map"))) {
Log_Info("%s: xsk map pinned to %s\n", __FUNCTION__,
bpf_map__get_pin_path(bpf_object__find_map_by_name(obj, "xsks_map")));
} else {
Log_Error("%s: xsk map is not pinned \n", __FUNCTION__);
return -1;
}
// if (bpf_map__is_pinned(bpf_object__find_map_by_name(obj, "xsks_map"))) {
// Log_Info("%s: xsk map pinned to %s\n", __FUNCTION__,
// bpf_map__get_pin_path(bpf_object__find_map_by_name(obj, "xsks_map")));
// } else {
// Log_Error("%s: xsk map is not pinned \n", __FUNCTION__);
// return -1;
// }

return 0;
}

0 comments on commit a16bb4f

Please sign in to comment.