|
| 1 | +From 03cea45be513a52a0c57c022594d6a5e98450642 Mon Sep 17 00:00:00 2001 |
| 2 | +From: zhangmingyi <zhangmingyi@huawei.com> |
| 3 | +Date: Thu, 9 Jan 2025 07:27:31 +0000 |
| 4 | +Subject: [PATCH 1/2] add helper strnstr strncmp parse_header_msg |
| 5 | + |
| 6 | +User messages need to be parsed when the bpf link establishment |
| 7 | +is delayed. add three helper func to parse and operate str. |
| 8 | + |
| 9 | +Signed-off-by: zhangmingyi <zhangmingyi@huawei.com> |
| 10 | +--- |
| 11 | + include/uapi/linux/bpf.h | 3 ++ |
| 12 | + net/core/filter.c | 67 ++++++++++++++++++++++++++++++++++ |
| 13 | + tools/include/uapi/linux/bpf.h | 3 ++ |
| 14 | + 3 files changed, 73 insertions(+) |
| 15 | + |
| 16 | +diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h |
| 17 | +index fcda8b986..bd2235fc4 100644 |
| 18 | +--- a/include/uapi/linux/bpf.h |
| 19 | ++++ b/include/uapi/linux/bpf.h |
| 20 | +@@ -4051,6 +4051,9 @@ union bpf_attr { |
| 21 | + FN(cpumask_op), \ |
| 22 | + FN(cpus_share_cache), \ |
| 23 | + FN(is_local_ipaddr), \ |
| 24 | ++ FN(km_header_strnstr), \ |
| 25 | ++ FN(km_header_strncmp), \ |
| 26 | ++ FN(parse_header_msg), \ |
| 27 | + /* */ |
| 28 | + |
| 29 | + /* integer value in 'imm' field of BPF_CALL instruction selects which helper |
| 30 | +diff --git a/net/core/filter.c b/net/core/filter.c |
| 31 | +index 431e778bb..4a5d944c9 100644 |
| 32 | +--- a/net/core/filter.c |
| 33 | ++++ b/net/core/filter.c |
| 34 | +@@ -6986,6 +6986,67 @@ const struct bpf_func_proto bpf_sock_ops_get_sk_rx_dst_proto = { |
| 35 | + }; |
| 36 | + |
| 37 | + #endif /* CONFIG_INET */ |
| 38 | ++typedef int (*bpf_parse_protocol_func)(struct bpf_sock_addr_kern* ctx); |
| 39 | ++bpf_parse_protocol_func parse_protocol_func = NULL; |
| 40 | ++EXPORT_SYMBOL(parse_protocol_func); |
| 41 | ++ |
| 42 | ++typedef int (*bpf_km_header_strnstr_func)(struct bpf_sock_addr_kern *ctx, const char *key, int key_sz, const char *subptr, int subptr_sz); |
| 43 | ++bpf_km_header_strnstr_func km_header_strnstr_func = NULL; |
| 44 | ++EXPORT_SYMBOL(km_header_strnstr_func); |
| 45 | ++ |
| 46 | ++typedef int (*bpf_km_header_strncmp_func)(const char *key, int key_sz, const char *target, int target_sz, int opt); |
| 47 | ++bpf_km_header_strncmp_func km_header_strncmp_func = NULL; |
| 48 | ++EXPORT_SYMBOL(km_header_strncmp_func); |
| 49 | ++ |
| 50 | ++BPF_CALL_1(bpf_parse_header_msg, struct bpf_sock_addr_kern *, ctx) |
| 51 | ++{ |
| 52 | ++ if (!parse_protocol_func) |
| 53 | ++ return -ENOTSUPP; |
| 54 | ++ return parse_protocol_func(ctx); |
| 55 | ++} |
| 56 | ++ |
| 57 | ++static const struct bpf_func_proto bpf_parse_header_msg_proto = { |
| 58 | ++ .func = bpf_parse_header_msg, |
| 59 | ++ .gpl_only = false, |
| 60 | ++ .ret_type = RET_INTEGER, |
| 61 | ++ .arg1_type = ARG_PTR_TO_CTX, |
| 62 | ++}; |
| 63 | ++ |
| 64 | ++BPF_CALL_5(bpf_km_header_strnstr, struct bpf_sock_addr_kern *, ctx, const char *, key, int , key_sz, const char *, subptr, int, subptr_sz) |
| 65 | ++{ |
| 66 | ++ if (!km_header_strnstr_func) |
| 67 | ++ return -ENOTSUPP; |
| 68 | ++ return km_header_strnstr_func(ctx, key, key_sz, subptr, subptr_sz); |
| 69 | ++} |
| 70 | ++ |
| 71 | ++static const struct bpf_func_proto bpf_km_header_strnstr_proto = { |
| 72 | ++ .func = bpf_km_header_strnstr, |
| 73 | ++ .gpl_only = false, |
| 74 | ++ .ret_type = RET_INTEGER, |
| 75 | ++ .arg1_type = ARG_PTR_TO_CTX, |
| 76 | ++ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
| 77 | ++ .arg3_type = ARG_CONST_SIZE, |
| 78 | ++ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
| 79 | ++ .arg5_type = ARG_CONST_SIZE, |
| 80 | ++}; |
| 81 | ++ |
| 82 | ++BPF_CALL_5(bpf_km_header_strncmp, const char *, key, int , key_sz, const char *, target, int, target_sz, int, opt) |
| 83 | ++{ |
| 84 | ++ if (!km_header_strncmp_func) |
| 85 | ++ return -ENOTSUPP; |
| 86 | ++ return km_header_strncmp_func(key, key_sz, target, target_sz, opt); |
| 87 | ++} |
| 88 | ++ |
| 89 | ++static const struct bpf_func_proto bpf_km_header_strncmp_proto = { |
| 90 | ++ .func = bpf_km_header_strncmp, |
| 91 | ++ .gpl_only = false, |
| 92 | ++ .ret_type = RET_INTEGER, |
| 93 | ++ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
| 94 | ++ .arg2_type = ARG_CONST_SIZE, |
| 95 | ++ .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
| 96 | ++ .arg4_type = ARG_CONST_SIZE, |
| 97 | ++ .arg5_type = ARG_ANYTHING, |
| 98 | ++}; |
| 99 | + |
| 100 | + bool bpf_helper_changes_pkt_data(void *func) |
| 101 | + { |
| 102 | +@@ -7139,6 +7200,12 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 103 | + default: |
| 104 | + return NULL; |
| 105 | + } |
| 106 | ++ case BPF_FUNC_parse_header_msg: |
| 107 | ++ return &bpf_parse_header_msg_proto; |
| 108 | ++ case BPF_FUNC_km_header_strnstr: |
| 109 | ++ return &bpf_km_header_strnstr_proto; |
| 110 | ++ case BPF_FUNC_km_header_strncmp: |
| 111 | ++ return &bpf_km_header_strncmp_proto; |
| 112 | + default: |
| 113 | + return bpf_sk_base_func_proto(func_id); |
| 114 | + } |
| 115 | +diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h |
| 116 | +index 588750c46..63069c2a8 100644 |
| 117 | +--- a/tools/include/uapi/linux/bpf.h |
| 118 | ++++ b/tools/include/uapi/linux/bpf.h |
| 119 | +@@ -4051,6 +4051,9 @@ union bpf_attr { |
| 120 | + FN(cpumask_op), \ |
| 121 | + FN(cpus_share_cache), \ |
| 122 | + FN(is_local_ipaddr), \ |
| 123 | ++ FN(km_header_strnstr), \ |
| 124 | ++ FN(km_header_strncmp), \ |
| 125 | ++ FN(parse_header_msg), \ |
| 126 | + /* */ |
| 127 | + |
| 128 | + /* integer value in 'imm' field of BPF_CALL instruction selects which helper |
| 129 | +-- |
| 130 | +2.33.0 |
0 commit comments