From 7e6a5d97430f0d178c5e2b211c0c194fdea0c00a Mon Sep 17 00:00:00 2001
From: yangchen <yangchen145@huawei.com>
Date: Fri, 27 Sep 2024 06:32:41 +0800
Subject: [PATCH] af_xdp: set rlimit unlimit when gazelle init

---
 src/lstack/core/lstack_init.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
index 37264a1..1d27f68 100644
--- a/src/lstack/core/lstack_init.c
+++ b/src/lstack/core/lstack_init.c
@@ -17,6 +17,7 @@
 #include <semaphore.h>
 #include <sys/ioctl.h>
 #include <sys/file.h>
+#include <sys/resource.h>
 #include <securec.h>
 #include <numa.h>
 #include <pthread.h>
@@ -104,6 +105,20 @@ static int32_t check_process_conflict(void)
     return 0;
 }
 
+/* Remove the memory resource limit of the current process.
+ * if the number of locked memory resources is exceeded, xdp_umem_create fails.
+ */
+static int set_rlimit_unlimited(void)
+{
+    struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
+
+    if (setrlimit(RLIMIT_MEMLOCK, &r) != 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
 void gazelle_exit(void)
 {
     wrap_api_exit();
@@ -240,6 +255,11 @@ __attribute__((constructor)) void gazelle_network_init(void)
 
     wrap_api_init();
 
+    if (set_rlimit_unlimited() != 0) {
+        LSTACK_PRE_LOG(LSTACK_INFO, "set rlimit unlimited failed\n");
+        LSTACK_EXIT(1, "set rlimit unlimited failed\n");
+    }
+
     /* check primary process start */
     check_process_start();
 
-- 
2.33.0

