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

Commit 7a50f12

Browse files
authored
Updated the libbpf version and made changes to the relevant files (#74)
* Replaced Libbpf with Libxdp * Renamed HasLibbpf function to HasLibxdp and modified functionality to reflect * CNI statically built so it no longer depends on host libraries * CI updates to accommodate all the above --------- Signed-off-by: phansGithub <[email protected]>
1 parent 93d43de commit 7a50f12

File tree

9 files changed

+128
-76
lines changed

9 files changed

+128
-76
lines changed

.github/workflows/public-ci.yml

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,23 @@ jobs:
3838
with:
3939
go-version: 1.19
4040

41-
- name: Install dependencies
42-
run: sudo apt-get update && sudo apt install libbpf-dev clang llvm gcc-multilib
41+
- name: Install libbpf and libxdp
42+
run: |
43+
sudo apt update
44+
sudo apt install -y wget build-essential golang
45+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf1_1.1.0-1_amd64.deb
46+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf-dev_1.1.0-1_amd64.deb
47+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp1_1.3.0-2ubuntu2_amd64.deb
48+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp-dev_1.3.0-2ubuntu2_amd64.deb
49+
sudo apt install -y ./libbpf1_1.1.0-1_amd64.deb
50+
sudo apt install -y ./libbpf-dev_1.1.0-1_amd64.deb
51+
sudo apt install -y ./libxdp1_1.3.0-2ubuntu2_amd64.deb
52+
sudo apt install -y ./libxdp-dev_1.3.0-2ubuntu2_amd64.deb
53+
sudo apt install -y clang
54+
sudo apt install -y llvm
55+
sudo apt install -y gcc-multilib
56+
57+
4358

4459
- name: Run build
4560
run: make build
@@ -54,8 +69,18 @@ jobs:
5469
with:
5570
go-version: 1.19
5671

57-
- name: Install dependencies
58-
run: sudo apt-get update && sudo apt install libbpf-dev clang llvm gcc-multilib
72+
- name: Install libbpf and libxdp
73+
run: |
74+
sudo apt update
75+
sudo apt install -y wget build-essential golang
76+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf1_1.1.0-1_amd64.deb
77+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf-dev_1.1.0-1_amd64.deb
78+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp1_1.3.0-2ubuntu2_amd64.deb
79+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp-dev_1.3.0-2ubuntu2_amd64.deb
80+
sudo apt install -y ./libbpf1_1.1.0-1_amd64.deb
81+
sudo apt install -y ./libbpf-dev_1.1.0-1_amd64.deb
82+
sudo apt install -y ./libxdp1_1.3.0-2ubuntu2_amd64.deb
83+
sudo apt install -y ./libxdp-dev_1.3.0-2ubuntu2_amd64.deb
5984
6085
- name: Install staticcheck
6186
run: go install honnef.co/go/tools/cmd/staticcheck@latest
@@ -73,8 +98,21 @@ jobs:
7398
with:
7499
go-version: 1.19
75100

76-
- name: Install dependencies
77-
run: sudo apt-get update && sudo apt install libbpf-dev clang llvm gcc-multilib
101+
- name: Install libbpf and libxdp
102+
run: |
103+
sudo apt update
104+
sudo apt install -y wget build-essential golang
105+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf1_1.1.0-1_amd64.deb
106+
sudo wget http://security.ubuntu.com/ubuntu/pool/main/libb/libbpf/libbpf-dev_1.1.0-1_amd64.deb
107+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp1_1.3.0-2ubuntu2_amd64.deb
108+
sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/main/x/xdp-tools/libxdp-dev_1.3.0-2ubuntu2_amd64.deb
109+
sudo apt install -y ./libbpf1_1.1.0-1_amd64.deb
110+
sudo apt install -y ./libbpf-dev_1.1.0-1_amd64.deb
111+
sudo apt install -y ./libxdp1_1.3.0-2ubuntu2_amd64.deb
112+
sudo apt install -y ./libxdp-dev_1.3.0-2ubuntu2_amd64.deb
113+
sudo apt install -y clang
114+
sudo apt install -y llvm
115+
sudo apt install -y gcc-multilib
78116
79117
- name: unit-tests
80118
run: make test

Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ format: clangformat
4040
buildc:
4141
@echo "****** Build BPF ******"
4242
@echo
43-
gcc ./internal/bpf/bpfWrapper.c -lbpf -c -o ./internal/bpf/bpfWrapper.o
43+
gcc ./internal/bpf/bpfWrapper.c -lxdp -c -o ./internal/bpf/bpfWrapper.o
4444
ar rs ./internal/bpf/libwrapper.a ./internal/bpf/bpfWrapper.o &> /dev/null
4545
@echo "****** Build xdp_pass ******"
4646
make -C ./internal/bpf/xdp-pass/
@@ -58,7 +58,7 @@ builddp: buildc
5858
buildcni: buildc
5959
@echo "****** Build CNI ******"
6060
@echo
61-
go build -o ./bin/afxdp ./cmd/cni
61+
go build -ldflags="-extldflags=-static" -tags netgo -o ./bin/afxdp ./cmd/cni
6262
@echo
6363
@echo
6464

@@ -68,24 +68,24 @@ build: builddp buildcni
6868
docker: ## Build docker image
6969
@echo "****** Docker Image ******"
7070
@echo
71-
docker build -t afxdp-device-plugin -f images/amd64.dockerfile .
71+
docker build -t localhost:5000/afxdp-device-plugin -f images/amd64.dockerfile .
7272
@echo
7373
@echo
7474

7575
podman: ## Build podman image
7676
@echo "****** Podman Image ******"
7777
@echo
78-
podman build -t afxdp-device-plugin -f images/amd64.dockerfile .
78+
podman build -t localhost:5000/afxdp-device-plugin -f images/amd64.dockerfile .
7979
@echo
8080
@echo
8181

8282
image:
83-
if $(MAKE) podman; then \
84-
echo "Podman build succeeded"; \
85-
else \
86-
echo "Podman build failed, trying docker.."; \
83+
# if $(MAKE) podman; then \
84+
# echo "Podman build succeeded"; \
85+
# else \
86+
# echo "Podman build failed, trying docker.."; \
8787
$(MAKE) docker; \
88-
fi
88+
# fi
8989

9090
undeploy: ## Undeploy the Deamonset
9191
@echo "****** Stop Daemonset ******"

cmd/deviceplugin/main.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,23 @@ func configureLogging(cfg deviceplugin.PluginConfig) error {
185185
// On each Kind node
186186
// Create a bridge afxdp-kind-br
187187
// Create 4 vethpairs starting at veth6
188-
// +===============+
189-
// | afxdp-kind-br |
190-
// | +---------| +---------+
191-
// | | veth7 | <=====> | veth6 |
192-
// | +---------| +---------+
193-
// | +---------| +---------+
194-
// | | veth9 | <=====> | veth8 |
195-
// | +---------| +---------+
196-
// | +---------| +---------+
197-
// | | veth11 | <=====> | veth10 |
198-
// | +---------| +---------+
199-
// | +---------| +---------+
200-
// | | veth13 | <=====> | veth12 |
201-
// | +---------| +---------+
202-
// +===============+
188+
//
189+
// +===============+
190+
// | afxdp-kind-br |
191+
// | +---------| +---------+
192+
// | | veth7 | <=====> | veth6 |
193+
// | +---------| +---------+
194+
// | +---------| +---------+
195+
// | | veth9 | <=====> | veth8 |
196+
// | +---------| +---------+
197+
// | +---------| +---------+
198+
// | | veth11 | <=====> | veth10 |
199+
// | +---------| +---------+
200+
// | +---------| +---------+
201+
// | | veth13 | <=====> | veth12 |
202+
// | +---------| +---------+
203+
// +===============+
204+
//
203205
// The "even" veth of the pair will be added to the device plugin resource pool.
204206
// and plumbed to the Pod.
205207
func configureKindSecondaryNetwork() error {
@@ -249,7 +251,7 @@ func checkHost(host host.Handler) (bool, error) {
249251

250252
// libbpf
251253
logging.Debugf("Checking host for Libbpf")
252-
bpfInstalled, libs, err := host.HasLibbpf()
254+
bpfInstalled, libs, err := host.HasLibxdp()
253255
if err != nil {
254256
err := fmt.Errorf("libbpf not found on host")
255257
return false, err

images/amd64.dockerfile

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.20@sha256:52921e63cc544c79c111db1d8461d8ab9070992d9c636e1573176642690c14b5 as cnibuilder
15+
FROM golang:1.20@sha256:efe38cb419e2b2012f66d1782d2efe2fd8884c71d9f342581e1697ba9047b5f8 as cnibuilder
1616
COPY . /usr/src/afxdp_k8s_plugins
1717
WORKDIR /usr/src/afxdp_k8s_plugins
18-
RUN apt-get update && apt-get -y install --no-install-recommends libbpf-dev=1:0.3-2 \
19-
&& apt-get -y install --no-install-recommends clang=1:11.0-51+nmu5 llvm=1:11.0-51+nmu5 gcc-multilib=4:10.2.1-1 \
20-
&& make buildcni
18+
RUN apt-get update \
19+
&& apt-get -y install --no-install-recommends libxdp-dev=1.3.1-1 \
20+
&& apt-get -y install -o APT::Keep-Downloaded-Packages=false --no-install-recommends clang=1:14.0-55.6 \
21+
&& apt-get -y install -o APT::Keep-Downloaded-Packages=false --no-install-recommends llvm=1:14.0-55.6 \
22+
&& apt-get -y install -o APT::Keep-Downloaded-Packages=false --no-install-recommends gcc-multilib=4:12.2.0-3 \
23+
&& make buildcni
2124

22-
FROM golang:1.20-alpine@sha256:87d0a3309b34e2ca732efd69fb899d3c420d3382370fd6e7e6d2cb5c930f27f9 as dpbuilder
25+
FROM golang:1.20-alpine@sha256:ebceb16dc094769b6e2a393d51e0417c19084ba20eb8967fb3f7675c32b45774 as dpbuilder
2326
COPY . /usr/src/afxdp_k8s_plugins
2427
WORKDIR /usr/src/afxdp_k8s_plugins
25-
RUN apk add --no-cache build-base~=0.5 libbsd-dev~=0.11 \
26-
&& apk add --no-cache libbpf-dev~=0.5 --repository=https://dl-cdn.alpinelinux.org/alpine/v3.15/community \
27-
&& apk add --no-cache llvm~=15.0.7-r0 clang~=15.0.7-r0 \
28-
&& make builddp
28+
RUN apk add --no-cache build-base~=0.5-r3 \
29+
&& apk add --no-cache libbsd-dev~=0.11.7 \
30+
&& apk add --no-cache libxdp-dev~=1.2.10-r0 \
31+
&& apk add --no-cache llvm15~=15.0.7-r0 \
32+
&& apk add --no-cache clang15~=15.0.7-r0 \
33+
&& make builddp
2934

30-
FROM amd64/alpine:3.17@sha256:e2e16842c9b54d985bf1ef9242a313f36b856181f188de21313820e177002501
31-
RUN apk --no-cache -U add iproute2-rdma~=6.0 acl~=2.3 \
32-
&& apk --no-cache -U add libbpf~=0.5 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.15/community
35+
FROM amd64/alpine:3.18@sha256:25fad2a32ad1f6f510e528448ae1ec69a28ef81916a004d3629874104f8a7f70
36+
RUN apk --no-cache -U add iproute2-rdma~=6.3.0-r0 acl~=2.3 \
37+
&& apk add --no-cache libxdp~=1.2.10-r0
3338
COPY --from=cnibuilder /usr/src/afxdp_k8s_plugins/bin/afxdp /afxdp/afxdp
3439
COPY --from=dpbuilder /usr/src/afxdp_k8s_plugins/bin/afxdp-dp /afxdp/afxdp-dp
3540
COPY --from=dpbuilder /usr/src/afxdp_k8s_plugins/images/entrypoint.sh /afxdp/entrypoint.sh

internal/bpf/bpfWrapper.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
#include <bpf/xsk.h> // for xsk_setup_xdp_prog, bpf_set_link_xdp_fd
1816
#include <linux/if_link.h> // for XDP_FLAGS_DRV_MODE
1917
#include <net/if.h> // for if_nametoindex
18+
#include <xdp/libxdp.h>
19+
#include <xdp/xsk.h> // for xsk_setup_xdp_prog, bpf_set_link_xdp_fd
2020

2121
#include "bpfWrapper.h"
2222
#include "log.h"
@@ -40,12 +40,12 @@ int Load_bpf_send_xsk_map(char *ifname) {
4040
} else {
4141
Log_Info("%s: if_index for interface %s is %d", __FUNCTION__, ifname, if_index);
4242
}
43-
4443
Log_Info("%s: starting setup of xdp program on "
4544
"interface %s (%d)",
4645
__FUNCTION__, ifname, if_index);
4746

4847
err = xsk_setup_xdp_prog(if_index, &fd);
48+
Log_Info("Error value: %d", err);
4949
if (err) {
5050
Log_Error("%s: setup of xdp program failed, "
5151
"returned: %d",
@@ -59,7 +59,7 @@ int Load_bpf_send_xsk_map(char *ifname) {
5959
__FUNCTION__, ifname, if_index, fd);
6060
return fd;
6161
}
62-
62+
Log_Info("FD value: %d", fd);
6363
return -1;
6464
}
6565

@@ -136,6 +136,7 @@ int Configure_busy_poll(int fd, int busy_timeout, int busy_budget) {
136136
int Clean_bpf(char *ifname) {
137137
int if_index, err;
138138
int fd = -1;
139+
struct xdp_multiprog *mp = NULL;
139140

140141
Log_Info("%s: disovering if_index for interface %s", __FUNCTION__, ifname);
141142

@@ -150,19 +151,19 @@ int Clean_bpf(char *ifname) {
150151
Log_Info("%s: starting removal of xdp program on interface %s (%d)", __FUNCTION__, ifname,
151152
if_index);
152153

153-
err = bpf_set_link_xdp_fd(if_index, fd, XDP_FLAGS_UPDATE_IF_NOEXIST);
154+
mp = xdp_multiprog__get_from_ifindex(if_index);
155+
if (!mp) {
156+
Log_Error("%s: unable to receive correct multi_prog reference : %s", __FUNCTION__,
157+
mp);
158+
return -1;
159+
}
160+
161+
err = xdp_multiprog__detach(mp);
154162
if (err) {
155-
if (err == EBUSY_CODE_WARNING) {
156-
// unloading of XDP program found to return EBUSY error of -16 on certain
157-
// host libbpf versions. doesn't break functionality and this problem is
158-
// being investigated.
159-
Log_Warning("%s: Removal of xdp program is reporting error code: (%d)",
160-
__FUNCTION__, err);
161-
} else {
162-
Log_Error("%s: Removal of xdp program failed, returned: (%d)", __FUNCTION__,
163-
err);
164-
return 1;
165-
}
163+
Log_Error("%s: Removal of xdp program failed, returned: "
164+
"returned: %d",
165+
__FUNCTION__, err);
166+
return -1;
166167
}
167168

168169
Log_Info("%s: removed xdp program from interface %s (%d)", __FUNCTION__, ifname, if_index);
@@ -173,6 +174,7 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
173174
int prog_fd = -1, err, ifindex;
174175
char *filename = "/afxdp/xdp_pass.o";
175176
struct bpf_object *obj;
177+
struct xdp_program *prog;
176178
__u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE;
177179

178180
Log_Info("%s: disovering if_index for interface %s", __FUNCTION__, ifname);
@@ -190,17 +192,22 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
190192
__FUNCTION__, ifname, ifindex);
191193

192194
/* Load the BPF program */
193-
err = bpf_prog_load(filename, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
194-
if (err < 0) {
195-
Log_Error("%s: Couldn't load BPF-OBJ file(%s)\n", __FUNCTION__, filename);
196-
return -1;
195+
prog = xdp_program__open_file(filename, NULL, NULL);
196+
err = libxdp_get_error(prog);
197+
if (err) {
198+
libxdp_strerror(err, "Couldn’t load XDP program",
199+
sizeof("Couldn’t load XDP program"));
200+
Log_Error("%s: Couldn’t load XDP program\n", __FUNCTION__, filename);
201+
return err;
197202
}
198203

199204
/* Attach the program to the interface at the xdp hook */
200-
err = bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags);
201-
if (err < 0) {
205+
err = xdp_program__attach(prog, ifindex, XDP_FLAGS_UPDATE_IF_NOEXIST, 0);
206+
if (err) {
207+
libxdp_strerror(err, "Couldn't attach the xdp pass program",
208+
sizeof("Couldn't attach the xdp pass program"));
202209
Log_Error("%s: Couldn't attach the XDP PASS PROGRAM TO %s\n", __FUNCTION__, ifname);
203-
return -1;
210+
return err;
204211
}
205212

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

internal/bpf/bpfWrapper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package bpf
1818

19-
//#include <bpf/libbpf.h>
20-
//#include <bpf/xsk.h>
19+
//#include <xdp/libxdp.h>
20+
//#include <xdp/xsk.h>
2121
//#cgo CFLAGS: -I.
22-
//#cgo LDFLAGS: -L. -lbpf
22+
//#cgo LDFLAGS: -L. -lxdp -lbpf -lelf -lz
2323
//#include "bpfWrapper.h"
2424
//#include "log.h"
2525
import "C"

internal/cni/cni.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,4 @@ func extractIP(result *current.Result) (string, error) {
503503
err := fmt.Errorf("extractIP(): ip is an empty string")
504504

505505
return resultIP, err
506-
}
506+
}

internal/host/host.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package host
1717

1818
import (
1919
"errors"
20-
logging "github.com/sirupsen/logrus"
2120
"io/ioutil"
2221
"os"
2322
"os/exec"
2423
"strings"
24+
25+
logging "github.com/sirupsen/logrus"
2526
)
2627

2728
/*
@@ -33,7 +34,7 @@ type Handler interface {
3334
AllowsUnprivilegedBpf() (bool, error)
3435
KernelVersion() (string, error)
3536
HasEthtool() (bool, string, error)
36-
HasLibbpf() (bool, []string, error)
37+
HasLibxdp() (bool, []string, error)
3738
HasDevlink() (bool, string, error)
3839
Hostname() (string, error)
3940
}
@@ -73,7 +74,7 @@ func (r *handler) KernelVersion() (string, error) {
7374
HasLibbpf checks if the host has libbpf installed and returns a boolean.
7475
It also returns a string array of libbpf libraries found under /usr/lib(64)/
7576
*/
76-
func (r *handler) HasLibbpf() (bool, []string, error) {
77+
func (r *handler) HasLibxdp() (bool, []string, error) {
7778
libPaths := []string{"/usr/lib/", "/usr/lib64/"}
7879
foundLibbpf := false
7980
var foundLibs []string
@@ -90,7 +91,7 @@ func (r *handler) HasLibbpf() (bool, []string, error) {
9091
}
9192

9293
for _, file := range files {
93-
if strings.Contains(file.Name(), "libbpf.so") {
94+
if strings.Contains(file.Name(), "libxdp.so") {
9495
foundLibbpf = true
9596
foundLibs = append(foundLibs, path+file.Name())
9697
}
@@ -254,4 +255,3 @@ func GivePermissions(filepath, uid, permissions string) error {
254255
logging.Infof("Socket access granted to UID %s", uid)
255256
return nil
256257
}
257-

0 commit comments

Comments
 (0)