Skip to content

Commit 8343c60

Browse files
add XDPLua map sample
1 parent 83978ec commit 8343c60

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

samples/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ xdpsock-objs := xdpsock_user.o
111111
xdp_fwd-objs := xdp_fwd_user.o
112112
task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
113113
xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS)
114-
# CONFIG_XDPLUA
114+
# CONFIG_XDP_LUA
115115
xdplua-objs := xdplua_user.o
116116

117117
# Tell kbuild to always build the programs
@@ -170,6 +170,8 @@ always += xdpsock_kern.o
170170
always += xdp_fwd_kern.o
171171
always += task_fd_query_kern.o
172172
always += xdp_sample_pkts_kern.o
173+
# CONFIG_XDP_LUA
174+
always += xdplua_map_kern.o
173175

174176
KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include
175177
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/

samples/bpf/map.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- Copyright (C) 2019-2020 Victor Nogueira <[email protected]>
3+
--
4+
-- This program is free software; you can redistribute it and/or
5+
-- modify it under the terms of the GNU General Public License as
6+
-- published by the Free Software Foundation version 2.
7+
--
8+
-- This program is distributed "as is" WITHOUT ANY WARRANTY of any
9+
-- kind, whether express or implied; without even the implied warranty
10+
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
-- GNU General Public License for more details.
12+
--
13+
xdp = require'xdp'
14+
15+
function lookup(map)
16+
local val = xdp.map_lookup(map, 1)
17+
print('val', val)
18+
end
19+
20+
function update(map)
21+
xdp.map_update(map, 1, 3)
22+
end

samples/bpf/xdplua_map_kern.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2019-2020 Victor Nogueira <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License as
6+
* published by the Free Software Foundation version 2.
7+
*
8+
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
9+
* kind, whether express or implied; without even the implied warranty
10+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
#define KBUILD_MODNAME "foo"
14+
#include <uapi/linux/bpf.h>
15+
#include "bpf_helpers.h"
16+
17+
struct bpf_map_def SEC("maps") test_map = {
18+
.type = BPF_MAP_TYPE_ARRAY,
19+
.key_size = sizeof(int),
20+
.value_size = sizeof(int),
21+
.max_entries = 20,
22+
};
23+
24+
SEC("xdp_lua_test_map")
25+
int xdp_lua_test_map_prog(struct xdp_md *ctx)
26+
{
27+
char lookupname[] = "lookup";
28+
char updatename[] = "update";
29+
30+
bpf_lua_setstate(ctx);
31+
32+
bpf_lua_pushmap(ctx, &test_map);
33+
bpf_lua_pcall(ctx, updatename, 1, 0);
34+
35+
bpf_lua_pushmap(ctx, &test_map);
36+
bpf_lua_pcall(ctx, lookupname, 1, 0);
37+
38+
return XDP_PASS;
39+
}
40+
41+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)