From d3a5b078c4566c966280dad7a5efa7e74053b718 Mon Sep 17 00:00:00 2001
From: wo_cow <niuqianqian@huawei.com>
Date: Thu, 17 Nov 2022 16:29:47 +0800
Subject: [PATCH] adapt libbpf v0.8

---
 .../ebpf.probe/src/include/__bpf_usr.h        | 32 ++++++++++++++-----
 .../extends/ebpf.probe/src/include/bpf.h      |  4 ++-
 .../ebpf.probe/src/ioprobe/io_count.bpf.c     |  2 +-
 .../ebpf.probe/src/ioprobe/io_err.bpf.c       |  2 +-
 src/probes/extends/ebpf.probe/src/mk/var.mk   |  5 +++
 5 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/src/probes/extends/ebpf.probe/src/include/__bpf_usr.h b/src/probes/extends/ebpf.probe/src/include/__bpf_usr.h
index d75070c..95e7160 100644
--- a/src/probes/extends/ebpf.probe/src/include/__bpf_usr.h
+++ b/src/probes/extends/ebpf.probe/src/include/__bpf_usr.h
@@ -37,14 +37,6 @@
 
 #define bpf_section(NAME) __attribute__((section(NAME), used))
 
-#define UPROBE(func, type) \
-    bpf_section("uprobe/" #func) \
-    void ubpf_##func(struct type *ctx)
-
-#define URETPROBE(func, type) \
-    bpf_section("uretprobe/" #func) \
-    void ubpf_ret_##func(struct type *ctx)
-
 #define UPROBE_PARMS_STASH(func, ctx, prog_id) \
     do { \
         int ret; \
@@ -63,7 +55,30 @@
         } \
     } while (0)
 
+#if (CURRENT_LIBBPF_VERSION  >= LIBBPF_VERSION(0, 8))
+#define UPROBE(func, type) \
+    bpf_section("uprobe") \
+    void ubpf_##func(struct type *ctx)
+
+#define URETPROBE(func, type) \
+    bpf_section("uretprobe") \
+    void ubpf_ret_##func(struct type *ctx)
+#define UPROBE_RET(func, type, prog_id) \
+    bpf_section("uprobe") \
+    void __uprobe_bpf_##func(struct type *ctx) { \
+        UPROBE_PARMS_STASH(func, ctx, prog_id); \
+    } \
+    \
+    bpf_section("uretprobe") \
+    void __uprobe_ret_bpf_##func(struct type *ctx)
+#else
+#define UPROBE(func, type) \
+    bpf_section("uprobe/" #func) \
+    void ubpf_##func(struct type *ctx)
 
+#define URETPROBE(func, type) \
+    bpf_section("uretprobe/" #func) \
+    void ubpf_ret_##func(struct type *ctx)
 #define UPROBE_RET(func, type, prog_id) \
     bpf_section("uprobe/" #func) \
     void __uprobe_bpf_##func(struct type *ctx) { \
@@ -72,6 +87,7 @@
     \
     bpf_section("uretprobe/" #func) \
     void __uprobe_ret_bpf_##func(struct type *ctx)
+#endif
 
 #endif
 
diff --git a/src/probes/extends/ebpf.probe/src/include/bpf.h b/src/probes/extends/ebpf.probe/src/include/bpf.h
index cfeca26..5008c2a 100644
--- a/src/probes/extends/ebpf.probe/src/include/bpf.h
+++ b/src/probes/extends/ebpf.probe/src/include/bpf.h
@@ -19,10 +19,12 @@
 
 #include "common.h"
 
-
 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
 #define CURRENT_KERNEL_VERSION KERNEL_VERSION(KER_VER_MAJOR, KER_VER_MINOR, KER_VER_PATCH)
 
+#define LIBBPF_VERSION(a, b) (((a) << 8) + (b))
+#define CURRENT_LIBBPF_VERSION LIBBPF_VERSION(LIBBPF_VER_MAJOR, LIBBPF_VER_MINOR)
+
 #include "__share_map_match.h"
 #include "__obj_map.h"
 #include "__bpf_kern.h"
diff --git a/src/probes/extends/ebpf.probe/src/ioprobe/io_count.bpf.c b/src/probes/extends/ebpf.probe/src/ioprobe/io_count.bpf.c
index 042292d..8fd71cb 100644
--- a/src/probes/extends/ebpf.probe/src/ioprobe/io_count.bpf.c
+++ b/src/probes/extends/ebpf.probe/src/ioprobe/io_count.bpf.c
@@ -105,7 +105,7 @@ static __always_inline char is_write_bio(struct block_bio_queue_args *bio)
 }
 
 bpf_section("tracepoint/block/block_bio_queue")
-static void tracepoint_block_bio_queue(struct block_bio_queue_args *ctx)
+void tracepoint_block_bio_queue(struct block_bio_queue_args *ctx)
 {
     u32 bio_size;
     int major, minor;
diff --git a/src/probes/extends/ebpf.probe/src/ioprobe/io_err.bpf.c b/src/probes/extends/ebpf.probe/src/ioprobe/io_err.bpf.c
index d60a3d7..b48f766 100644
--- a/src/probes/extends/ebpf.probe/src/ioprobe/io_err.bpf.c
+++ b/src/probes/extends/ebpf.probe/src/ioprobe/io_err.bpf.c
@@ -112,7 +112,7 @@ static __always_inline void update_io_err(struct block_rq_complete_args* ctx, st
 }
 
 bpf_section("tracepoint/block/block_rq_complete")
-static void tracepoint_block_rq_complete(struct block_rq_complete_args *ctx)
+void tracepoint_block_rq_complete(struct block_rq_complete_args *ctx)
 {
     int major, minor;
     struct io_err_s *io_err = NULL;
diff --git a/src/probes/extends/ebpf.probe/src/mk/var.mk b/src/probes/extends/ebpf.probe/src/mk/var.mk
index e41603c..2522a6f 100644
--- a/src/probes/extends/ebpf.probe/src/mk/var.mk
+++ b/src/probes/extends/ebpf.probe/src/mk/var.mk
@@ -30,12 +30,17 @@ KER_VER_PATCH = $(shell echo $(KER_VER) | awk -F'.' '{print $$3}')
 RELEASE_INFOS = $(shell uname -r | awk -F'-' '{print $$2}')
 KER_RELEASE = $(shell echo $(RELEASE_INFOS) | awk -F'.' '{print $$1}')
 
+LIBBPF_VER = $(shell rpm -qa | grep libbpf-devel | awk -F'-' '{print $$3}')
+LIBBPF_VER_MAJOR = $(shell echo $(LIBBPF_VER) | awk -F'.' '{print $$1}')
+LIBBPF_VER_MINOR = $(shell echo $(LIBBPF_VER) | awk -F'.' '{print $$2}')
+
 LINK_TARGET ?= -lpthread -lbpf -lelf -llog4cplus -lz -lconfig
 EXTRA_CFLAGS ?= -g -O2 -Wall -fPIC
 EXTRA_CDEFINE ?= -D__TARGET_ARCH_$(ARCH)
 CFLAGS := $(EXTRA_CFLAGS) $(EXTRA_CDEFINE)
 CFLAGS += -DKER_VER_MAJOR=$(KER_VER_MAJOR) -DKER_VER_MINOR=$(KER_VER_MINOR) -DKER_VER_PATCH=$(KER_VER_PATCH)
 CFLAGS += -DKER_RELEASE=$(KER_RELEASE)
+CFLAGS += -DLIBBPF_VER_MAJOR=$(LIBBPF_VER_MAJOR) -DLIBBPF_VER_MINOR=$(LIBBPF_VER_MINOR)
 LDFLAGS += -Wl,--copy-dt-needed-entries -Wl,-z,relro,-z,now
 
 CXXFLAGS += -std=c++11 -g -O2 -Wall -fPIC
-- 
2.36.0.windows.1

