Skip to content

Commit fe1927c

Browse files
Add bpf_lua_tostring function
1 parent 48884fc commit fe1927c

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,7 @@ union bpf_attr {
22312231
FN(lua_dataref), \
22322232
FN(lua_dataunref), \
22332233
FN(lua_pcall), \
2234+
FN(lua_tostring), \
22342235
FN(lua_pop), \
22352236
FN(lua_pushinteger), \
22362237
FN(lua_pushlightuserdata), \

net/core/filter.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5011,6 +5011,26 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = {
50115011
.arg2_type = ARG_ANYTHING,
50125012
};
50135013

5014+
BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) {
5015+
if (lua_isstring(ctx->L, index)) {
5016+
strncpy(str, lua_tostring(ctx->L, index), size);
5017+
return 1;
5018+
}
5019+
5020+
return 0;
5021+
}
5022+
5023+
static const struct bpf_func_proto bpf_lua_tostring_proto = {
5024+
.func = bpf_lua_tostring,
5025+
.gpl_only = false,
5026+
.pkt_access = false,
5027+
.ret_type = RET_INTEGER,
5028+
.arg1_type = ARG_PTR_TO_CTX,
5029+
.arg2_type = ARG_PTR_TO_UNINIT_MEM,
5030+
.arg3_type = ARG_CONST_SIZE,
5031+
.arg4_type = ARG_ANYTHING,
5032+
};
5033+
50145034
BPF_CALL_2(bpf_lua_newpacket, struct xdp_buff *, ctx, int, offset) {
50155035
if (offset + ctx->data < ctx->data_end) {
50165036
return lunpack_newpacket(ctx->L, ctx->data + offset,
@@ -5108,6 +5128,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
51085128
return &bpf_lua_pushstring_proto;
51095129
case BPF_FUNC_lua_toboolean:
51105130
return &bpf_lua_toboolean_proto;
5131+
case BPF_FUNC_lua_tostring:
5132+
return &bpf_lua_tostring_proto;
51115133
case BPF_FUNC_lua_tointeger:
51125134
return &bpf_lua_tointeger_proto;
51135135
case BPF_FUNC_lua_newpacket:

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,7 @@ union bpf_attr {
22312231
FN(lua_dataref), \
22322232
FN(lua_dataunref), \
22332233
FN(lua_pcall), \
2234+
FN(lua_tostring), \
22342235
FN(lua_pop), \
22352236
FN(lua_pushinteger), \
22362237
FN(lua_pushlightuserdata), \

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ static void (*bpf_lua_dataunref)(void *ctx, int data_ref) =
152152
static int (*bpf_lua_pcall)(void *ctx, char *funcname, int num_args,
153153
int num_rets) =
154154
(void *) BPF_FUNC_lua_pcall;
155+
static int (*bpf_lua_tostring)(void *ctx, char *sslsni, u32 size, int index) =
156+
(void *)BPF_FUNC_lua_tostring;
155157
static void (*bpf_lua_pop)(void *ctx, int index) =
156158
(void *)BPF_FUNC_lua_pop;
157159
static void (*bpf_lua_pushinteger)(void *ctx, int num) =

0 commit comments

Comments
 (0)