Skip to content

Commit 13a42eb

Browse files
add XDPLua map sample
1 parent 7e405cf commit 13a42eb

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

samples/bpf/Makefile

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

117117
# Tell kbuild to always build the programs
@@ -174,6 +174,8 @@ always-y += ibumad_kern.o
174174
always-y += hbm_out_kern.o
175175
always-y += hbm_edt_kern.o
176176
always-y += xdpsock_kern.o
177+
# CONFIG_XDP_LUA
178+
always-y += xdplua_map_kern.o
177179

178180
ifeq ($(ARCH), arm)
179181
# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux

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 <victor.nogueira@ring-0.io>
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2019-2020 Victor Nogueira <victor.nogueira@ring-0.io>
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/bpf_helpers.h>
16+
17+
struct bpf_map_def SEC("maps") lua_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+
bpf_lua_pushmap(ctx, &lua_test_map);
32+
bpf_lua_pcall(ctx, updatename, 1, 0);
33+
34+
bpf_lua_pushmap(ctx, &lua_test_map);
35+
bpf_lua_pcall(ctx, lookupname, 1, 0);
36+
37+
return XDP_PASS;
38+
}
39+
40+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)