Re: [PATCH bpf-next v2 1/2] bpf: add bpf_strcasestr,bpf_strncasestr kfuncs

2025-10-18 Thread rt...@foxmail.com



On 10/7/25 7:23 AM, Andrii Nakryiko wrote:

On Sat, Oct 4, 2025 at 7:52 AM Rong Tao  wrote:

From: Rong Tao 

bpf_strcasestr() and bpf_strncasestr() functions perform same like
bpf_strstr() and bpf_strnstr() except ignoring the case of the
characters.

Signed-off-by: Rong Tao 
---
  kernel/bpf/helpers.c | 96 ++--
  1 file changed, 75 insertions(+), 21 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index c9fab9a356df..4df902e5f208 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3675,34 +3675,20 @@ __bpf_kfunc int bpf_strcspn(const char *s__ign, const 
char *reject__ign)
 return -EFAULT;
  }

-/**
- * bpf_strnstr - Find the first substring in a length-limited string
- * @s1__ign: The string to be searched
- * @s2__ign: The string to search for
- * @len: the maximum number of characters to search
- *
- * Return:
- * * >=0  - Index of the first character of the first occurrence of 
@s2__ign
- *  within the first @len characters of @s1__ign
- * * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
- * * %-EFAULT - Cannot read one of the strings
- * * %-E2BIG  - One of the strings is too large
- * * %-ERANGE - One of the strings is outside of kernel address space
- */
-__bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t 
len)
+int __bpf_strnstr(const char *s1, const char *s2, size_t len, bool ignore_case)

keep it static?


Thanks, fixed in v3, please review.

Rong Tao



pw-bot: cr


[...]





Re: [PATCH bpf-next 1/2] bpf: add bpf_strcasestr,bpf_strncasestr kfuncs

2025-10-17 Thread rt...@foxmail.com



On 10/4/25 10:28 PM, Alexei Starovoitov wrote:

On Sat, Oct 4, 2025 at 6:37 AM Rong Tao  wrote:

-__bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t 
len)
+__bpf_kfunc int __bpf_strnstr(const char *s1, const char *s2, size_t len,
+ bool ignore_case)
  {

Still __bpf_kfunc ?

Sorry about that, i'll fix it right now.


pw-bot: cr





Re: [PATCH bpf-next v2 2/2] selftests/bpf: Test bpf_strcasestr,bpf_strncasestr kfuncs

2025-10-06 Thread rt...@foxmail.com



On 10/7/25 8:29 AM, Eduard Zingerman wrote:

On Sat, 2025-10-04 at 22:47 +0800, Rong Tao wrote:

From: Rong Tao 

Add tests for new kfuncs bpf_strcasestr() and bpf_strncasestr().

Signed-off-by: Rong Tao 
---

Acked-by: Eduard Zingerman 

[...]


diff --git a/tools/testing/selftests/bpf/progs/string_kfuncs_success.c 
b/tools/testing/selftests/bpf/progs/string_kfuncs_success.c
index 2e3498e37b9c..d21330b4cc3b 100644
--- a/tools/testing/selftests/bpf/progs/string_kfuncs_success.c
+++ b/tools/testing/selftests/bpf/progs/string_kfuncs_success.c
@@ -33,8 +33,12 @@ __test(11) int test_strnlen(void *ctx) { return 
bpf_strnlen(str, 12); }
  __test(5) int test_strspn(void *ctx) { return bpf_strspn(str, "ehlo"); }
  __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
  __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); 
}
+__test(6) int test_strcasestr_found1(void *ctx) { return bpf_strcasestr(str, 
"world"); }
+__test(6) int test_strcasestr_found2(void *ctx) { return bpf_strcasestr(str, 
"WORLD"); }

Nit: I'd compress these two tests into one:
  __test(6) int test_strcasestr_found1(void *ctx) { return bpf_strcasestr(str, 
"woRLD"); }
  (and did the same for (str, "hello") variants below).


I just submit v3, please review, thanks.

Rong Tao




  __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, 
"hi"); }
+__test(-ENOENT) int test_strcasestr_notfound(void *ctx) { return bpf_strcasestr(str, 
"hi"); }
  __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
+__test(0) int test_strcasestr_empty(void *ctx) { return bpf_strcasestr(str, 
""); }
  __test(0) int test_strnstr_found1(void *ctx) { return bpf_strnstr("", "", 0); 
}
  __test(0) int test_strnstr_found2(void *ctx) { return bpf_strnstr(str, 
"hello", 5); }
  __test(0) int test_strnstr_found3(void *ctx) { return bpf_strnstr(str, 
"hello", 6); }
@@ -42,5 +46,14 @@ __test(-ENOENT) int test_strnstr_notfound1(void *ctx) { 
return bpf_strnstr(str,
  __test(-ENOENT) int test_strnstr_notfound2(void *ctx) { return bpf_strnstr(str, 
"hello", 4); }
  __test(-ENOENT) int test_strnstr_notfound3(void *ctx) { return bpf_strnstr("", 
"a", 0); }
  __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); 
}
+__test(0) int test_strncasestr_found1(void *ctx) { return bpf_strncasestr("", 
"", 0); }
+__test(0) int test_strncasestr_found2(void *ctx) { return bpf_strncasestr(str, 
"hello", 5); }
+__test(0) int test_strncasestr_found3(void *ctx) { return bpf_strncasestr(str, 
"hello", 6); }
+__test(0) int test_strncasestr_found4(void *ctx) { return bpf_strncasestr(str, 
"HELLO", 5); }
+__test(0) int test_strncasestr_found5(void *ctx) { return bpf_strncasestr(str, 
"HELLO", 6); }
+__test(-ENOENT) int test_strncasestr_notfound1(void *ctx) { return bpf_strncasestr(str, 
"hi", 10); }
+__test(-ENOENT) int test_strncasestr_notfound2(void *ctx) { return bpf_strncasestr(str, 
"hello", 4); }
+__test(-ENOENT) int test_strncasestr_notfound3(void *ctx) { return bpf_strncasestr("", 
"a", 0); }
+__test(0) int test_strncasestr_empty(void *ctx) { return bpf_strncasestr(str, 
"", 1); }
  
  char _license[] SEC("license") = "GPL";