Commit 6fd28865 authored by John Fastabend's avatar John Fastabend Committed by David S. Miller

bpf: additional sockmap self tests

Add some more sockmap tests to cover,

 - forwarding to NULL entries
 - more than two maps to test list ops
 - forwarding to different map
Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d26e597d
...@@ -19,16 +19,16 @@ int bpf_prog1(struct __sk_buff *skb) ...@@ -19,16 +19,16 @@ int bpf_prog1(struct __sk_buff *skb)
void *data = (void *)(long) skb->data; void *data = (void *)(long) skb->data;
__u32 lport = skb->local_port; __u32 lport = skb->local_port;
__u32 rport = skb->remote_port; __u32 rport = skb->remote_port;
char *d = data; __u8 *d = data;
if (data + 8 > data_end) if (data + 10 > data_end)
return skb->len; return skb->len;
/* This write/read is a bit pointless but tests the verifier and /* This write/read is a bit pointless but tests the verifier and
* strparser handler for read/write pkt data and access into sk * strparser handler for read/write pkt data and access into sk
* fields. * fields.
*/ */
d[0] = 1; d[7] = 1;
bpf_printk("parse: data[0] = (%u): local_port %i remote %i\n", bpf_printk("parse: data[0] = (%u): local_port %i remote %i\n",
d[0], lport, bpf_ntohl(rport)); d[0], lport, bpf_ntohl(rport));
......
...@@ -12,7 +12,14 @@ int _version SEC("version") = 1; ...@@ -12,7 +12,14 @@ int _version SEC("version") = 1;
##__VA_ARGS__); \ ##__VA_ARGS__); \
}) })
struct bpf_map_def SEC("maps") sock_map = { struct bpf_map_def SEC("maps") sock_map_rx = {
.type = BPF_MAP_TYPE_SOCKMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 20,
};
struct bpf_map_def SEC("maps") sock_map_tx = {
.type = BPF_MAP_TYPE_SOCKMAP, .type = BPF_MAP_TYPE_SOCKMAP,
.key_size = sizeof(int), .key_size = sizeof(int),
.value_size = sizeof(int), .value_size = sizeof(int),
...@@ -26,11 +33,15 @@ int bpf_prog2(struct __sk_buff *skb) ...@@ -26,11 +33,15 @@ int bpf_prog2(struct __sk_buff *skb)
void *data = (void *)(long) skb->data; void *data = (void *)(long) skb->data;
__u32 lport = skb->local_port; __u32 lport = skb->local_port;
__u32 rport = skb->remote_port; __u32 rport = skb->remote_port;
char *d = data; __u8 *d = data;
__u8 sk, map;
if (data + 8 > data_end) if (data + 8 > data_end)
return SK_DROP; return SK_DROP;
map = d[0];
sk = d[1];
d[0] = 0xd; d[0] = 0xd;
d[1] = 0xe; d[1] = 0xe;
d[2] = 0xa; d[2] = 0xa;
...@@ -40,9 +51,11 @@ int bpf_prog2(struct __sk_buff *skb) ...@@ -40,9 +51,11 @@ int bpf_prog2(struct __sk_buff *skb)
d[6] = 0xe; d[6] = 0xe;
d[7] = 0xf; d[7] = 0xf;
bpf_printk("verdict: data[0] = (%u): local_port %i remote %i redirect 5\n", bpf_printk("verdict: data[0] = redir(%u:%u)\n", map, sk);
d[0], lport, bpf_ntohl(rport));
return bpf_sk_redirect_map(&sock_map, 5, 0); if (!map)
return bpf_sk_redirect_map(&sock_map_rx, sk, 0);
return bpf_sk_redirect_map(&sock_map_tx, sk, 0);
} }
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
...@@ -465,10 +465,10 @@ static void test_sockmap(int task, void *data) ...@@ -465,10 +465,10 @@ static void test_sockmap(int task, void *data)
{ {
int ports[] = {50200, 50201, 50202, 50204}; int ports[] = {50200, 50201, 50202, 50204};
int err, i, fd, sfd[6] = {0xdeadbeef}; int err, i, fd, sfd[6] = {0xdeadbeef};
char buf[] = "hello sockmap user\n"; u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
int one = 1, map_fd, s, sc, rc; int one = 1, map_fd_rx, map_fd_tx, s, sc, rc;
int parse_prog, verdict_prog; int parse_prog, verdict_prog;
struct bpf_map *bpf_map; struct bpf_map *bpf_map_rx, *bpf_map_tx;
struct sockaddr_in addr; struct sockaddr_in addr;
struct bpf_object *obj; struct bpf_object *obj;
struct timeval to; struct timeval to;
...@@ -585,26 +585,38 @@ static void test_sockmap(int task, void *data) ...@@ -585,26 +585,38 @@ static void test_sockmap(int task, void *data)
goto out_sockmap; goto out_sockmap;
} }
bpf_map = bpf_object__find_map_by_name(obj, "sock_map"); bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
if (IS_ERR(bpf_map)) { if (IS_ERR(bpf_map_rx)) {
printf("Failed to load map from verdict prog\n"); printf("Failed to load map rx from verdict prog\n");
goto out_sockmap; goto out_sockmap;
} }
map_fd = bpf_map__fd(bpf_map); map_fd_rx = bpf_map__fd(bpf_map_rx);
if (map_fd < 0) { if (map_fd_rx < 0) {
printf("Failed to get map fd\n"); printf("Failed to get map fd\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_attach(parse_prog, map_fd, bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
if (IS_ERR(bpf_map_tx)) {
printf("Failed to load map tx from verdict prog\n");
goto out_sockmap;
}
map_fd_tx = bpf_map__fd(bpf_map_tx);
if (map_fd_tx < 0) {
printf("Failed to get map tx fd\n");
goto out_sockmap;
}
err = bpf_prog_attach(parse_prog, map_fd_rx,
BPF_SK_SKB_STREAM_PARSER, 0); BPF_SK_SKB_STREAM_PARSER, 0);
if (err) { if (err) {
printf("Failed bpf prog attach\n"); printf("Failed bpf prog attach\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_attach(verdict_prog, map_fd, err = bpf_prog_attach(verdict_prog, map_fd_rx,
BPF_SK_SKB_STREAM_VERDICT, 0); BPF_SK_SKB_STREAM_VERDICT, 0);
if (err) { if (err) {
printf("Failed bpf prog attach\n"); printf("Failed bpf prog attach\n");
...@@ -613,9 +625,15 @@ static void test_sockmap(int task, void *data) ...@@ -613,9 +625,15 @@ static void test_sockmap(int task, void *data)
/* Test map update elem afterwards fd lives in fd and map_fd */ /* Test map update elem afterwards fd lives in fd and map_fd */
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
err = bpf_map_update_elem(map_fd, &i, &sfd[i], BPF_ANY); err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
if (err) { if (err) {
printf("Failed map_fd update sockmap %i '%i:%i'\n", printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
err, i, sfd[i]);
goto out_sockmap;
}
err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
if (err) {
printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
err, i, sfd[i]); err, i, sfd[i]);
goto out_sockmap; goto out_sockmap;
} }
...@@ -623,16 +641,25 @@ static void test_sockmap(int task, void *data) ...@@ -623,16 +641,25 @@ static void test_sockmap(int task, void *data)
/* Test map delete elem and remove send/recv sockets */ /* Test map delete elem and remove send/recv sockets */
for (i = 2; i < 4; i++) { for (i = 2; i < 4; i++) {
err = bpf_map_delete_elem(map_fd, &i); err = bpf_map_delete_elem(map_fd_rx, &i);
if (err) { if (err) {
printf("Failed delete sockmap %i '%i:%i'\n", printf("Failed delete sockmap rx %i '%i:%i'\n",
err, i, sfd[i]);
goto out_sockmap;
}
err = bpf_map_delete_elem(map_fd_tx, &i);
if (err) {
printf("Failed delete sockmap tx %i '%i:%i'\n",
err, i, sfd[i]); err, i, sfd[i]);
goto out_sockmap; goto out_sockmap;
} }
} }
/* Test map send/recv */ /* Test map send/recv */
sc = send(sfd[2], buf, 10, 0); for (i = 0; i < 2; i++) {
buf[0] = i;
buf[1] = 0x5;
sc = send(sfd[2], buf, 20, 0);
if (sc < 0) { if (sc < 0) {
printf("Failed sockmap send\n"); printf("Failed sockmap send\n");
goto out_sockmap; goto out_sockmap;
...@@ -661,6 +688,16 @@ static void test_sockmap(int task, void *data) ...@@ -661,6 +688,16 @@ static void test_sockmap(int task, void *data)
printf("Failed sockmap recv\n"); printf("Failed sockmap recv\n");
goto out_sockmap; goto out_sockmap;
} }
}
/* Negative null entry lookup from datapath should be dropped */
buf[0] = 1;
buf[1] = 12;
sc = send(sfd[2], buf, 20, 0);
if (sc < 0) {
printf("Failed sockmap send\n");
goto out_sockmap;
}
/* Push fd into same slot */ /* Push fd into same slot */
i = 2; i = 2;
...@@ -730,7 +767,7 @@ static void test_sockmap(int task, void *data) ...@@ -730,7 +767,7 @@ static void test_sockmap(int task, void *data)
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++)
close(sfd[i]); close(sfd[i]);
close(fd); close(fd);
close(map_fd); close(map_fd_rx);
bpf_object__close(obj); bpf_object__close(obj);
return; return;
out: out:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment