Repository: kmesh-net/kmesh

Issue:
monitoring_enable global variable not setting for cgroup_skb prog
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!
-->

**What happened**:

**What you expected to happen**:

**How to reproduce it (as minimally and precisely as possible)**:

**Anything else we need to know?**:

**Environment**:

- Kmesh version:
- Kmesh mode(kmesh has `Kernel-Native Mode` and `Duel-Engine Mode`):
- Istio version:
- Kernel version:
- Others:


Discussion:
@hzxuzhonghu: Isn;t this has been fixed?  Can u elaborate@yp969803: I have checked it in local, and seen that the monitoring variable is not set@yp969803: i think not inclusion of stdbool.h header file in cgroup_skb.c is causing problem, let me check it@yp969803: Nope it is not working, @hzxuzhonghu @nlgwcy @LiZhenCheng9527 @yp969803: ```
         coredns-7925    [000] ..s21 103917.882612: bpf_trace_printk: enable monitoring 0
         coredns-7925    [000] ...11 103917.882619: bpf_trace_printk: enable monitoring 0
         coredns-7925    [000] ..s21 103917.882621: bpf_trace_printk: enable monitoring 0

```
these are debug logs

Solution patch:
diff --git a/bpf/include/bpf_helper_defs_ext.h b/bpf/include/bpf_helper_defs_ext.h
index a7c908d5..7b071400 100644
--- a/bpf/include/bpf_helper_defs_ext.h
+++ b/bpf/include/bpf_helper_defs_ext.h
@@ -10,6 +10,10 @@
  * By default, these IDs are in the 5.10 kernel with kmesh kernel patches.
  */
 
+#define bpf_km_header_strnstr_num 175
+#define bpf_km_header_strncmp_num 176
+#define bpf_parse_header_msg_num  177
+
 /*
  * Description
  *      Look for the string corresponding to the key in the results of the
@@ -19,7 +23,8 @@
  *      If found, return 1; otherwise, return 0.
  */
 static long (*bpf_km_header_strnstr)(
-    struct bpf_sock_addr *ctx, const char *key, int key_sz, const char *subptr, int subptr_sz) = (void *)175;
+    struct bpf_sock_addr *ctx, const char *key, int key_sz, const char *subptr, int subptr_sz) = (void *)
+    bpf_km_header_strnstr_num;
 
 /*
  * Description
@@ -30,8 +35,8 @@ static long (*bpf_km_header_strnstr)(
  * Return
  *      If the strings are same, return 0.
  */
-static long (*bpf_km_header_strncmp)(const char *key, int key_sz, const char *target, int target_sz, int opt) =
-    (void *)176;
+static long (*bpf_km_header_strncmp)(const char *key, int key_sz, const char *target, int target_sz, int opt) = (void *)
+    bpf_km_header_strncmp_num;
 
 /*
  * Description
@@ -43,4 +48,4 @@ static long (*bpf_km_header_strncmp)(const char *key, int key_sz, const char *ta
  *      A HTTP PROTO TYPE is returned on success.
  *      **PROTO_UNKNOW** is returned if failure.
  */
-static long (*bpf_parse_header_msg)(struct bpf_sock_addr *ctx) = (void *)177;
+static long (*bpf_parse_header_msg)(struct bpf_sock_addr *ctx) = (void *)bpf_parse_header_msg_num;
diff --git a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfeb.go b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfeb.go
index 68112e45..27cc98f9 100644
--- a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfeb.go
+++ b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfeb.go
@@ -117,13 +117,13 @@ type KmeshCgroupSkbMapSpecs struct {
 	KmPerfMap     *ebpf.MapSpec `ebpf:"km_perf_map"`
 	KmService     *ebpf.MapSpec `ebpf:"km_service"`
 	KmSockstorage *ebpf.MapSpec `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.MapSpec `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.MapSpec `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.MapSpec `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.MapSpec `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.MapSpec `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.MapSpec `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.MapSpec `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.MapSpec `ebpf:"map_of_tcp_probe"`
 }
 
 // KmeshCgroupSkbVariableSpecs contains global variables before they are loaded into the kernel.
@@ -165,13 +165,13 @@ type KmeshCgroupSkbMaps struct {
 	KmPerfMap     *ebpf.Map `ebpf:"km_perf_map"`
 	KmService     *ebpf.Map `ebpf:"km_service"`
 	KmSockstorage *ebpf.Map `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.Map `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.Map `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.Map `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.Map `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.Map `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.Map `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.Map `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.Map `ebpf:"map_of_tcp_probe"`
 }
 
 func (m *KmeshCgroupSkbMaps) Close() error {
@@ -187,13 +187,13 @@ func (m *KmeshCgroupSkbMaps) Close() error {
 		m.KmPerfMap,
 		m.KmService,
 		m.KmSockstorage,
+		m.KmTcpProbe,
 		m.KmTmpbuf,
 		m.KmWlpolicy,
 		m.KmeshMap1600,
 		m.KmeshMap192,
 		m.KmeshMap296,
 		m.KmeshMap64,
-		m.MapOfTcpProbe,
 	)
 }
 
diff --git a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfel.go b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfel.go
index d0c6103f..f242a80f 100644
--- a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfel.go
+++ b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskb_bpfel.go
@@ -117,13 +117,13 @@ type KmeshCgroupSkbMapSpecs struct {
 	KmPerfMap     *ebpf.MapSpec `ebpf:"km_perf_map"`
 	KmService     *ebpf.MapSpec `ebpf:"km_service"`
 	KmSockstorage *ebpf.MapSpec `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.MapSpec `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.MapSpec `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.MapSpec `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.MapSpec `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.MapSpec `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.MapSpec `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.MapSpec `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.MapSpec `ebpf:"map_of_tcp_probe"`
 }
 
 // KmeshCgroupSkbVariableSpecs contains global variables before they are loaded into the kernel.
@@ -165,13 +165,13 @@ type KmeshCgroupSkbMaps struct {
 	KmPerfMap     *ebpf.Map `ebpf:"km_perf_map"`
 	KmService     *ebpf.Map `ebpf:"km_service"`
 	KmSockstorage *ebpf.Map `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.Map `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.Map `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.Map `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.Map `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.Map `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.Map `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.Map `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.Map `ebpf:"map_of_tcp_probe"`
 }
 
 func (m *KmeshCgroupSkbMaps) Close() error {
@@ -187,13 +187,13 @@ func (m *KmeshCgroupSkbMaps) Close() error {
 		m.KmPerfMap,
 		m.KmService,
 		m.KmSockstorage,
+		m.KmTcpProbe,
 		m.KmTmpbuf,
 		m.KmWlpolicy,
 		m.KmeshMap1600,
 		m.KmeshMap192,
 		m.KmeshMap296,
 		m.KmeshMap64,
-		m.MapOfTcpProbe,
 	)
 }
 
diff --git a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfeb.go b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfeb.go
index f839db54..54f8b5e9 100644
--- a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfeb.go
+++ b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfeb.go
@@ -117,13 +117,13 @@ type KmeshCgroupSkbCompatMapSpecs struct {
 	KmPerfMap     *ebpf.MapSpec `ebpf:"km_perf_map"`
 	KmService     *ebpf.MapSpec `ebpf:"km_service"`
 	KmSockstorage *ebpf.MapSpec `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.MapSpec `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.MapSpec `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.MapSpec `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.MapSpec `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.MapSpec `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.MapSpec `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.MapSpec `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.MapSpec `ebpf:"map_of_tcp_probe"`
 }
 
 // KmeshCgroupSkbCompatVariableSpecs contains global variables before they are loaded into the kernel.
@@ -165,13 +165,13 @@ type KmeshCgroupSkbCompatMaps struct {
 	KmPerfMap     *ebpf.Map `ebpf:"km_perf_map"`
 	KmService     *ebpf.Map `ebpf:"km_service"`
 	KmSockstorage *ebpf.Map `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.Map `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.Map `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.Map `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.Map `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.Map `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.Map `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.Map `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.Map `ebpf:"map_of_tcp_probe"`
 }
 
 func (m *KmeshCgroupSkbCompatMaps) Close() error {
@@ -187,13 +187,13 @@ func (m *KmeshCgroupSkbCompatMaps) Close() error {
 		m.KmPerfMap,
 		m.KmService,
 		m.KmSockstorage,
+		m.KmTcpProbe,
 		m.KmTmpbuf,
 		m.KmWlpolicy,
 		m.KmeshMap1600,
 		m.KmeshMap192,
 		m.KmeshMap296,
 		m.KmeshMap64,
-		m.MapOfTcpProbe,
 	)
 }
 
diff --git a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfel.go b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfel.go
index dd0b30d6..7a0a905a 100644
--- a/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfel.go
+++ b/bpf/kmesh/bpf2go/dualengine/kmeshcgroupskbcompat_bpfel.go
@@ -117,13 +117,13 @@ type KmeshCgroupSkbCompatMapSpecs struct {
 	KmPerfMap     *ebpf.MapSpec `ebpf:"km_perf_map"`
 	KmService     *ebpf.MapSpec `ebpf:"km_service"`
 	KmSockstorage *ebpf.MapSpec `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.MapSpec `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.MapSpec `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.MapSpec `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.MapSpec `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.MapSpec `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.MapSpec `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.MapSpec `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.MapSpec `ebpf:"map_of_tcp_probe"`
 }
 
 // KmeshCgroupSkbCompatVariableSpecs contains global variables before they are loaded into the kernel.
@@ -165,13 +165,13 @@ type KmeshCgroupSkbCompatMaps struct {
 	KmPerfMap     *ebpf.Map `ebpf:"km_perf_map"`
 	KmService     *ebpf.Map `ebpf:"km_service"`
 	KmSockstorage *ebpf.Map `ebpf:"km_sockstorage"`
+	KmTcpProbe    *ebpf.Map `ebpf:"km_tcp_probe"`
 	KmTmpbuf      *ebpf.Map `ebpf:"km_tmpbuf"`
 	KmWlpolicy    *ebpf.Map `ebpf:"km_wlpolicy"`
 	KmeshMap1600  *ebpf.Map `ebpf:"kmesh_map1600"`
 	KmeshMap192   *ebpf.Map `ebpf:"kmesh_map192"`
 	KmeshMap296   *ebpf.Map `ebpf:"kmesh_map296"`
 	KmeshMap64    *ebpf.Map `ebpf:"kmesh_map64"`
-	MapOfTcpProbe *ebpf.Map `ebpf:"map_of_tcp_probe"`
 }
 
 func (m *KmeshCgroupSkbCompatMaps) Close() error {
@@ -187,13 +187,13 @@ func (m *KmeshCgroupSkbCompatMaps) Close() error {
 		m.KmPerfMap,
 		m.KmService,
 		m.KmSockstorage,
+		m.KmTcpProbe,
 		m.KmTmpbuf,
 		m.KmWlpolicy,
 		m.KmeshMap1600,
 		m.KmeshMap192,
 		m.KmeshMap296,
 		m.KmeshMap64,
-		m.MapOfTcpProbe,
 	)
 }
 
diff --git a/bpf/kmesh/probes/probe.h b/bpf/kmesh/probes/probe.h
index eb735d7b..f3042821 100644
--- a/bpf/kmesh/probes/probe.h
+++ b/bpf/kmesh/probes/probe.h
@@ -9,7 +9,7 @@
 
 #define LONG_CONN_THRESHOLD_TIME (5 * 1000000000ULL) // 5s
 
-volatile __u32 enable_monitoring = 0;
+volatile __u32 enable_monitoring = 1;
 
 static inline bool is_monitoring_enable()
 {
diff --git a/bpf/kmesh/probes/tcp_probe.h b/bpf/kmesh/probes/tcp_probe.h
index 77939b78..e516544c 100644
--- a/bpf/kmesh/probes/tcp_probe.h
+++ b/bpf/kmesh/probes/tcp_probe.h
@@ -5,6 +5,7 @@
 #define __KMESH_BPF_ACCESS_LOG_H__
 
 #include "bpf_common.h"
+#include "config.h"
 
 // direction
 enum {
diff --git a/bpf/kmesh/workload/cgroup_skb.c b/bpf/kmesh/workload/cgroup_skb.c
index a10a6549..00fb8408 100644
--- a/bpf/kmesh/workload/cgroup_skb.c
+++ b/bpf/kmesh/workload/cgroup_skb.c
@@ -5,9 +5,11 @@
 #include <sys/socket.h>
 #include <bpf/bpf_endian.h>
 #include <bpf/bpf_helpers.h>
+#include <stdbool.h>
 #include "bpf_log.h"
 #include "bpf_common.h"
 #include "probe.h"
+#include "config.h"
 
 SEC("cgroup_skb/ingress")
 int cgroup_skb_ingress_prog(struct __sk_buff *skb)
diff --git a/bpf/kmesh/workload/sockops.c b/bpf/kmesh/workload/sockops.c
index bceb7f42..4fe7751c 100644
--- a/bpf/kmesh/workload/sockops.c
+++ b/bpf/kmesh/workload/sockops.c
@@ -8,7 +8,6 @@
 #include <stdbool.h>
 #include "bpf_log.h"
 #include "workload.h"
-#include "config.h"
 #include "bpf_common.h"
 #include "probe.h"
 #include "config.h"
diff --git a/kmesh_bpf_env.sh b/kmesh_bpf_env.sh
index 7cfa4f80..248b4fcf 100644
--- a/kmesh_bpf_env.sh
+++ b/kmesh_bpf_env.sh
@@ -17,6 +17,6 @@ for name in ${helper_name[@]}; do
 	current_line=`grep -nr "FN($name)" $KERNEL_HEADER_LINUX_BPF | awk -F ":" {'print $1'}`
 	if [ -n "$current_line" ]; then
 		helper_id=`expr $current_line - $base_line`
-		sed -Ei "/$name/s/([0-9]+)[^0-9]*$/$helper_id;/" $ROOT_DIR/bpf/include/bpf_helper_defs_ext.h
+		sed -Ei "/${name}_num/s/([0-9]+)[^0-9]*$/$helper_id/" $ROOT_DIR/bpf/include/bpf_helper_defs_ext.h
 	fi
 done



Test patch:
diff --git a/pkg/status/status_server_test.go b/pkg/status/status_server_test.go
index eacdf57d..1e420b11 100644
--- a/pkg/status/status_server_test.go
+++ b/pkg/status/status_server_test.go
@@ -419,67 +419,6 @@ func TestServer_dumpAdsBpfMap(t *testing.T) {
 	})
 }
 
-func TestServerAccesslogHandler(t *testing.T) {
-	t.Run("change accesslog config info", func(t *testing.T) {
-		config := options.BpfConfig{
-			Mode:        constants.DualEngineMode,
-			BpfFsPath:   "/sys/fs/bpf",
-			Cgroup2Path: "/mnt/kmesh_cgroup2",
-		}
-		cleanup, loader := test.InitBpfMap(t, config)
-		defer cleanup()
-
-		server := &Server{
-			xdsClient: &controller.XdsClient{
-				WorkloadController: &workload.Controller{
-					MetricController: &telemetry.MetricController{},
-				},
-			},
-			loader: loader,
-		}
-		server.xdsClient.WorkloadController.MetricController.EnableAccesslog.Store(true)
-
-		url := fmt.Sprintf("%s?enable=%s", patternMonitoring, "true")
-		req := httptest.NewRequest(http.MethodPost, url, nil)
-		w := httptest.NewRecorder()
-		server.monitoringHandler(w, req)
-
-		url = fmt.Sprintf("%s?enable=%s", patternAccesslog, "false")
-		req = httptest.NewRequest(http.MethodPost, url, nil)
-		w = httptest.NewRecorder()
-		server.accesslogHandler(w, req)
-
-		assert.Equal(t, server.xdsClient.WorkloadController.GetAccesslogTrigger(), false)
-	})
-
-	t.Run("when monitoring is disable, cannot enable accesslog", func(t *testing.T) {
-		config := options.BpfConfig{
-			Mode:        constants.DualEngineMode,
-			BpfFsPath:   "/sys/fs/bpf",
-			Cgroup2Path: "/mnt/kmesh_cgroup2",
-		}
-		cleanup, loader := test.InitBpfMap(t, config)
-		defer cleanup()
-
-		server := &Server{
-			xdsClient: &controller.XdsClient{
-				WorkloadController: &workload.Controller{
-					MetricController: &telemetry.MetricController{},
-				},
-			},
-			loader: loader,
-		}
-		server.xdsClient.WorkloadController.MetricController.EnableAccesslog.Store(false)
-
-		url := fmt.Sprintf("%s?enable=%s", patternAccesslog, "true")
-		req := httptest.NewRequest(http.MethodPost, url, nil)
-		w := httptest.NewRecorder()
-		server.accesslogHandler(w, req)
-
-		assert.Equal(t, server.xdsClient.WorkloadController.GetAccesslogTrigger(), false)
-	})
-}
-
 func TestServerMetricHandler(t *testing.T) {
 	t.Run("change accesslog, workload metrics and connection metric config info", func(t *testing.T) {
 		config := options.BpfConfig{
@@ -546,11 +485,19 @@ func TestServerMetricHandler(t *testing.T) {
 			},
 			loader: loader,
 		}
+
 		server.xdsClient.WorkloadController.MetricController.EnableAccesslog.Store(false)
 
-		url := fmt.Sprintf("%s?enable=%s", patternAccesslog, "true")
+		url := fmt.Sprintf("%s?enable=%s", patternMonitoring, "false")
 		req := httptest.NewRequest(http.MethodPost, url, nil)
 		w := httptest.NewRecorder()
+		server.monitoringHandler(w, req)
+
+		assert.Equal(t, server.xdsClient.WorkloadController.GetMonitoringTrigger(), false)
+
+		url = fmt.Sprintf("%s?enable=%s", patternAccesslog, "true")
+		req = httptest.NewRequest(http.MethodPost, url, nil)
+		w = httptest.NewRecorder()
 		server.accesslogHandler(w, req)
 
 		assert.Equal(t, server.xdsClient.WorkloadController.GetAccesslogTrigger(), false)