Skip to content

Commit

Permalink
bpf: test_sockmap add options to use msg_push_data
Browse files Browse the repository at this point in the history
Add options to run msg_push_data, this patch creates two more flags
in test_sockmap that can be used to specify the offset and length
of bytes to be added. The new options are --txmsg_start_push to
specify where bytes should be inserted and --txmsg_end_push to
specify how many bytes. This is analagous to the options that are
used to pull data, --txmsg_start and --txmsg_end.

In addition to adding the options tests are added to the test
suit to run the tests similar to what was done for msg_pull_data.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
jrfastab authored and borkmann committed Oct 20, 2018
1 parent f908d26 commit 84fbfe0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 26 deletions.
58 changes: 55 additions & 3 deletions tools/testing/selftests/bpf/test_sockmap.c
Expand Up @@ -77,6 +77,8 @@ int txmsg_apply;
int txmsg_cork;
int txmsg_start;
int txmsg_end;
int txmsg_start_push;
int txmsg_end_push;
int txmsg_ingress;
int txmsg_skb;
int ktls;
Expand All @@ -100,6 +102,8 @@ static const struct option long_options[] = {
{"txmsg_cork", required_argument, NULL, 'k'},
{"txmsg_start", required_argument, NULL, 's'},
{"txmsg_end", required_argument, NULL, 'e'},
{"txmsg_start_push", required_argument, NULL, 'p'},
{"txmsg_end_push", required_argument, NULL, 'q'},
{"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
{"txmsg_skb", no_argument, &txmsg_skb, 1 },
{"ktls", no_argument, &ktls, 1 },
Expand Down Expand Up @@ -903,6 +907,30 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test)
}
}

if (txmsg_start_push) {
i = 2;
err = bpf_map_update_elem(map_fd[5],
&i, &txmsg_start_push, BPF_ANY);
if (err) {
fprintf(stderr,
"ERROR: bpf_map_update_elem (txmsg_start_push): %d (%s)\n",
err, strerror(errno));
goto out;
}
}

if (txmsg_end_push) {
i = 3;
err = bpf_map_update_elem(map_fd[5],
&i, &txmsg_end_push, BPF_ANY);
if (err) {
fprintf(stderr,
"ERROR: bpf_map_update_elem %i@%i (txmsg_end_push): %d (%s)\n",
txmsg_end_push, i, err, strerror(errno));
goto out;
}
}

if (txmsg_ingress) {
int in = BPF_F_INGRESS;

Expand Down Expand Up @@ -1235,6 +1263,8 @@ static int test_mixed(int cgrp)
txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
txmsg_apply = txmsg_cork = 0;
txmsg_start = txmsg_end = 0;
txmsg_start_push = txmsg_end_push = 0;

/* Test small and large iov_count values with pass/redir/apply/cork */
txmsg_pass = 1;
txmsg_redir = 0;
Expand Down Expand Up @@ -1351,6 +1381,8 @@ static int test_start_end(int cgrp)
/* Test basic start/end with lots of iov_count and iov_lengths */
txmsg_start = 1;
txmsg_end = 2;
txmsg_start_push = 1;
txmsg_end_push = 2;
err = test_txmsg(cgrp);
if (err)
goto out;
Expand All @@ -1364,6 +1396,8 @@ static int test_start_end(int cgrp)
for (i = 99; i <= 1600; i += 500) {
txmsg_start = 0;
txmsg_end = i;
txmsg_start_push = 0;
txmsg_end_push = i;
err = test_exec(cgrp, &opt);
if (err)
goto out;
Expand All @@ -1373,6 +1407,8 @@ static int test_start_end(int cgrp)
for (i = 199; i <= 1600; i += 500) {
txmsg_start = 100;
txmsg_end = i;
txmsg_start_push = 100;
txmsg_end_push = i;
err = test_exec(cgrp, &opt);
if (err)
goto out;
Expand All @@ -1381,34 +1417,44 @@ static int test_start_end(int cgrp)
/* Test start/end with cork pulling last sg entry */
txmsg_start = 1500;
txmsg_end = 1600;
txmsg_start_push = 1500;
txmsg_end_push = 1600;
err = test_exec(cgrp, &opt);
if (err)
goto out;

/* Test start/end pull of single byte in last page */
txmsg_start = 1111;
txmsg_end = 1112;
txmsg_start_push = 1111;
txmsg_end_push = 1112;
err = test_exec(cgrp, &opt);
if (err)
goto out;

/* Test start/end with end < start */
txmsg_start = 1111;
txmsg_end = 0;
txmsg_start_push = 1111;
txmsg_end_push = 0;
err = test_exec(cgrp, &opt);
if (err)
goto out;

/* Test start/end with end > data */
txmsg_start = 0;
txmsg_end = 1601;
txmsg_start_push = 0;
txmsg_end_push = 1601;
err = test_exec(cgrp, &opt);
if (err)
goto out;

/* Test start/end with start > data */
txmsg_start = 1601;
txmsg_end = 1600;
txmsg_start_push = 1601;
txmsg_end_push = 1600;
err = test_exec(cgrp, &opt);

out:
Expand All @@ -1424,7 +1470,7 @@ char *map_names[] = {
"sock_map_redir",
"sock_apply_bytes",
"sock_cork_bytes",
"sock_pull_bytes",
"sock_bytes",
"sock_redir_flags",
"sock_skb_opts",
};
Expand Down Expand Up @@ -1531,7 +1577,7 @@ static int __test_suite(int cg_fd, char *bpf_file)
}

/* Tests basic commands and APIs with range of iov values */
txmsg_start = txmsg_end = 0;
txmsg_start = txmsg_end = txmsg_start_push = txmsg_end_push = 0;
err = test_txmsg(cg_fd);
if (err)
goto out;
Expand Down Expand Up @@ -1580,7 +1626,7 @@ int main(int argc, char **argv)
if (argc < 2)
return test_suite(-1);

while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:p:q:",
long_options, &longindex)) != -1) {
switch (opt) {
case 's':
Expand All @@ -1589,6 +1635,12 @@ int main(int argc, char **argv)
case 'e':
txmsg_end = atoi(optarg);
break;
case 'p':
txmsg_start_push = atoi(optarg);
break;
case 'q':
txmsg_end_push = atoi(optarg);
break;
case 'a':
txmsg_apply = atoi(optarg);
break;
Expand Down
97 changes: 74 additions & 23 deletions tools/testing/selftests/bpf/test_sockmap_kern.h
Expand Up @@ -70,11 +70,11 @@ struct bpf_map_def SEC("maps") sock_cork_bytes = {
.max_entries = 1
};

struct bpf_map_def SEC("maps") sock_pull_bytes = {
struct bpf_map_def SEC("maps") sock_bytes = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 2
.max_entries = 4
};

struct bpf_map_def SEC("maps") sock_redir_flags = {
Expand Down Expand Up @@ -181,27 +181,33 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
SEC("sk_msg1")
int bpf_prog4(struct sk_msg_md *msg)
{
int *bytes, zero = 0, one = 1;
int *start, *end;
int *bytes, zero = 0, one = 1, two = 2, three = 3;
int *start, *end, *start_push, *end_push;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
bpf_msg_apply_bytes(msg, *bytes);
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push)
bpf_msg_push_data(msg, *start_push, *end_push, 0);
return SK_PASS;
}

SEC("sk_msg2")
int bpf_prog5(struct sk_msg_md *msg)
{
int err1 = -1, err2 = -1, zero = 0, one = 1;
int *bytes, *start, *end, len1, len2;
int zero = 0, one = 1, two = 2, three = 3;
int *start, *end, *start_push, *end_push;
int *bytes, len1, len2 = 0, len3;
int err1 = -1, err2 = -1;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
Expand All @@ -210,8 +216,8 @@ int bpf_prog5(struct sk_msg_md *msg)
if (bytes)
err2 = bpf_msg_cork_bytes(msg, *bytes);
len1 = (__u64)msg->data_end - (__u64)msg->data;
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end) {
int err;

Expand All @@ -225,6 +231,23 @@ int bpf_prog5(struct sk_msg_md *msg)
bpf_printk("sk_msg2: length update %i->%i\n",
len1, len2);
}

start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push) {
int err;

bpf_printk("sk_msg2: push(%i:%i)\n",
start_push ? *start_push : 0,
end_push ? *end_push : 0);
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
if (err)
bpf_printk("sk_msg2: push_data err %i\n", err);
len3 = (__u64)msg->data_end - (__u64)msg->data;
bpf_printk("sk_msg2: length push_update %i->%i\n",
len2 ? len2 : len1, len3);
}

bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n",
len1, err1, err2);
return SK_PASS;
Expand All @@ -233,8 +256,8 @@ int bpf_prog5(struct sk_msg_md *msg)
SEC("sk_msg3")
int bpf_prog6(struct sk_msg_md *msg)
{
int *bytes, zero = 0, one = 1, key = 0;
int *start, *end, *f;
int *bytes, *start, *end, *start_push, *end_push, *f;
int zero = 0, one = 1, two = 2, three = 3, key = 0;
__u64 flags = 0;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
Expand All @@ -243,10 +266,17 @@ int bpf_prog6(struct sk_msg_md *msg)
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);

start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);

start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push)
bpf_msg_push_data(msg, *start_push, *end_push, 0);

f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
if (f && *f) {
key = 2;
Expand All @@ -262,8 +292,9 @@ int bpf_prog6(struct sk_msg_md *msg)
SEC("sk_msg4")
int bpf_prog7(struct sk_msg_md *msg)
{
int err1 = 0, err2 = 0, zero = 0, one = 1, key = 0;
int *f, *bytes, *start, *end, len1, len2;
int zero = 0, one = 1, two = 2, three = 3, len1, len2 = 0, len3;
int *bytes, *start, *end, *start_push, *end_push, *f;
int err1 = 0, err2 = 0, key = 0;
__u64 flags = 0;

int err;
Expand All @@ -274,10 +305,10 @@ int bpf_prog7(struct sk_msg_md *msg)
if (bytes)
err2 = bpf_msg_cork_bytes(msg, *bytes);
len1 = (__u64)msg->data_end - (__u64)msg->data;
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
if (start && end) {

start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end) {
bpf_printk("sk_msg2: pull(%i:%i)\n",
start ? *start : 0, end ? *end : 0);
err = bpf_msg_pull_data(msg, *start, *end, 0);
Expand All @@ -288,6 +319,22 @@ int bpf_prog7(struct sk_msg_md *msg)
bpf_printk("sk_msg2: length update %i->%i\n",
len1, len2);
}

start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push) {
bpf_printk("sk_msg4: push(%i:%i)\n",
start_push ? *start_push : 0,
end_push ? *end_push : 0);
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
if (err)
bpf_printk("sk_msg4: push_data err %i\n",
err);
len3 = (__u64)msg->data_end - (__u64)msg->data;
bpf_printk("sk_msg4: length push_update %i->%i\n",
len2 ? len2 : len1, len3);
}

f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
if (f && *f) {
key = 2;
Expand Down Expand Up @@ -342,19 +389,23 @@ int bpf_prog9(struct sk_msg_md *msg)
SEC("sk_msg7")
int bpf_prog10(struct sk_msg_md *msg)
{
int *bytes, zero = 0, one = 1;
int *start, *end;
int *bytes, *start, *end, *start_push, *end_push;
int zero = 0, one = 1, two = 2, three = 3;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
bpf_msg_apply_bytes(msg, *bytes);
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push)
bpf_msg_push_data(msg, *start_push, *end_push, 0);

return SK_DROP;
}
Expand Down

0 comments on commit 84fbfe0

Please sign in to comment.