Skip to content

Commit 36ceb19

Browse files
Add bpf_lua_tostring function
1 parent e20f358 commit 36ceb19

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,15 @@ union bpf_attr {
29672967
* The converted Lua value at the given index, if the value is
29682968
* convertible to an integer(see the Lua manual for more details
29692969
* on type conversion); otherwise returns 0
2970+
*
2971+
* void bpf_lua_tostring(void *ctx, const char *str, u32 size, int index)
2972+
* Description
2973+
* Converts the Lua value at the given index of the Lua
2974+
* stack to a C string and copies size bytes of it to
2975+
* value pointed by str
2976+
* Return
2977+
* 1 if the value at the given index of the Lua stack is a
2978+
* string; otherwise it returns 0
29702979
*/
29712980
#define __BPF_FUNC_MAPPER(FN) \
29722981
FN(unspec), \
@@ -3101,7 +3110,8 @@ union bpf_attr {
31013110
FN(lua_pushstring), \
31023111
FN(lua_toboolean), \
31033112
FN(lua_tointeger), \
3104-
FN(lua_newpacket),
3113+
FN(lua_newpacket), \
3114+
FN(lua_tostring),
31053115
/* #endif CONFIG_XDP_LUA */
31063116

31073117
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

net/core/filter.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,26 @@ static const struct bpf_func_proto bpf_lua_newpacket_proto = {
60926092
.arg1_type = ARG_PTR_TO_CTX,
60936093
.arg2_type = ARG_ANYTHING,
60946094
};
6095+
6096+
BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) {
6097+
if (lua_isstring(ctx->L, index)) {
6098+
strncpy(str, lua_tostring(ctx->L, index), size);
6099+
return 1;
6100+
}
6101+
6102+
return 0;
6103+
}
6104+
6105+
static const struct bpf_func_proto bpf_lua_tostring_proto = {
6106+
.func = bpf_lua_tostring,
6107+
.gpl_only = false,
6108+
.pkt_access = false,
6109+
.ret_type = RET_INTEGER,
6110+
.arg1_type = ARG_PTR_TO_CTX,
6111+
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
6112+
.arg3_type = ARG_CONST_SIZE,
6113+
.arg4_type = ARG_ANYTHING,
6114+
};
60956115
#endif /* CONFIG_XDP_LUA */
60966116

60976117
bool bpf_helper_changes_pkt_data(void *func)
@@ -6197,6 +6217,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
61976217
return &bpf_lua_tointeger_proto;
61986218
case BPF_FUNC_lua_newpacket:
61996219
return &bpf_lua_newpacket_proto;
6220+
case BPF_FUNC_lua_tostring:
6221+
return &bpf_lua_tostring_proto;
62006222
#endif /* CONFIG_XDP_LUA */
62016223
default:
62026224
return NULL;

tools/include/uapi/linux/bpf.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2972,6 +2972,15 @@ union bpf_attr {
29722972
* Description
29732973
* Create new luaunpack user data buffer pointing to
29742974
* the captured packet at the specified offset
2975+
*
2976+
* void bpf_lua_tostring(void *ctx, const char *str, u32 size, int index)
2977+
* Description
2978+
* Converts the Lua value at the given index of the Lua
2979+
* stack to a C string and copies size bytes of it to
2980+
* value pointed by str
2981+
* Return
2982+
* 1 if the value at the given index of the Lua stack is a
2983+
* string; otherwise it returns 0
29752984
*/
29762985
#define __BPF_FUNC_MAPPER(FN) \
29772986
FN(unspec), \
@@ -3106,7 +3115,8 @@ union bpf_attr {
31063115
FN(lua_pushstring), \
31073116
FN(lua_toboolean), \
31083117
FN(lua_tointeger), \
3109-
FN(lua_newpacket),
3118+
FN(lua_newpacket), \
3119+
FN(lua_tostring),
31103120
/* #endif CONFIG_XDP_LUA */
31113121

31123122
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)