Skip to content

Commit 6f6d33f

Browse files
jrfastabdavem330
authored andcommitted
bpf: selftests add sockmap tests
This generates a set of sockets, attaches BPF programs, and sends some simple traffic using basic send/recv pattern. Additionally, we do a bunch of negative tests to ensure adding/removing socks out of the sockmap fail correctly. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 41bc94f commit 6f6d33f

File tree

7 files changed

+443
-39
lines changed

7 files changed

+443
-39
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,3 +1744,32 @@ long libbpf_get_error(const void *ptr)
17441744
return PTR_ERR(ptr);
17451745
return 0;
17461746
}
1747+
1748+
int bpf_prog_load(const char *file, enum bpf_prog_type type,
1749+
struct bpf_object **pobj, int *prog_fd)
1750+
{
1751+
struct bpf_program *prog;
1752+
struct bpf_object *obj;
1753+
int err;
1754+
1755+
obj = bpf_object__open(file);
1756+
if (IS_ERR(obj))
1757+
return -ENOENT;
1758+
1759+
prog = bpf_program__next(NULL, obj);
1760+
if (!prog) {
1761+
bpf_object__close(obj);
1762+
return -ENOENT;
1763+
}
1764+
1765+
bpf_program__set_type(prog, type);
1766+
err = bpf_object__load(obj);
1767+
if (err) {
1768+
bpf_object__close(obj);
1769+
return -EINVAL;
1770+
}
1771+
1772+
*pobj = obj;
1773+
*prog_fd = bpf_program__fd(prog);
1774+
return 0;
1775+
}

tools/lib/bpf/libbpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,6 @@ int bpf_map__pin(struct bpf_map *map, const char *path);
243243

244244
long libbpf_get_error(const void *ptr);
245245

246+
int bpf_prog_load(const char *file, enum bpf_prog_type type,
247+
struct bpf_object **pobj, int *prog_fd);
246248
#endif

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
1515
test_align
1616

1717
TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
18-
test_pkt_md_access.o test_xdp_redirect.o
18+
test_pkt_md_access.o test_xdp_redirect.o sockmap_parse_prog.o sockmap_verdict_prog.o
1919

2020
TEST_PROGS := test_kmod.sh test_xdp_redirect.sh
2121

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <linux/bpf.h>
2+
#include "bpf_helpers.h"
3+
#include "bpf_util.h"
4+
#include "bpf_endian.h"
5+
6+
int _version SEC("version") = 1;
7+
8+
#define bpf_printk(fmt, ...) \
9+
({ \
10+
char ____fmt[] = fmt; \
11+
bpf_trace_printk(____fmt, sizeof(____fmt), \
12+
##__VA_ARGS__); \
13+
})
14+
15+
SEC("sk_skb1")
16+
int bpf_prog1(struct __sk_buff *skb)
17+
{
18+
void *data_end = (void *)(long) skb->data_end;
19+
void *data = (void *)(long) skb->data;
20+
__u32 lport = skb->local_port;
21+
__u32 rport = skb->remote_port;
22+
char *d = data;
23+
24+
if (data + 8 > data_end)
25+
return skb->len;
26+
27+
/* This write/read is a bit pointless but tests the verifier and
28+
* strparser handler for read/write pkt data and access into sk
29+
* fields.
30+
*/
31+
d[0] = 1;
32+
33+
bpf_printk("data[0] = (%u): local_port %i remote %i\n",
34+
d[0], lport, bpf_ntohl(rport));
35+
return skb->len;
36+
}
37+
38+
char _license[] SEC("license") = "GPL";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <linux/bpf.h>
2+
#include "bpf_helpers.h"
3+
#include "bpf_util.h"
4+
#include "bpf_endian.h"
5+
6+
int _version SEC("version") = 1;
7+
8+
#define bpf_printk(fmt, ...) \
9+
({ \
10+
char ____fmt[] = fmt; \
11+
bpf_trace_printk(____fmt, sizeof(____fmt), \
12+
##__VA_ARGS__); \
13+
})
14+
15+
struct bpf_map_def SEC("maps") sock_map = {
16+
.type = BPF_MAP_TYPE_SOCKMAP,
17+
.key_size = sizeof(int),
18+
.value_size = sizeof(int),
19+
.max_entries = 20,
20+
};
21+
22+
SEC("sk_skb2")
23+
int bpf_prog2(struct __sk_buff *skb)
24+
{
25+
void *data_end = (void *)(long) skb->data_end;
26+
void *data = (void *)(long) skb->data;
27+
__u32 lport = skb->local_port;
28+
__u32 rport = skb->remote_port;
29+
char *d = data;
30+
31+
if (data + 8 > data_end)
32+
return SK_DROP;
33+
34+
d[0] = 0xd;
35+
d[1] = 0xe;
36+
d[2] = 0xa;
37+
d[3] = 0xd;
38+
d[4] = 0xb;
39+
d[5] = 0xe;
40+
d[6] = 0xe;
41+
d[7] = 0xf;
42+
43+
bpf_printk("data[0] = (%u): local_port %i remote %i\n",
44+
d[0], lport, bpf_ntohl(rport));
45+
return bpf_sk_redirect_map(&sock_map, 5, 0);
46+
}
47+
48+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)