From: John Fastabend <john.fastabend@gmail.com>
To: davejwatson@fb.com, davem@davemloft.net, daniel@iogearbox.net,
ast@kernel.org
Cc: netdev@vger.kernel.org
Subject: [bpf-next PATCH v3 14/18] bpf: sockmap, add sample option to test apply_bytes helper
Date: Sun, 18 Mar 2018 12:57:56 -0700 [thread overview]
Message-ID: <20180318195756.14466.76764.stgit@john-Precision-Tower-5810> (raw)
In-Reply-To: <20180318195501.14466.25366.stgit@john-Precision-Tower-5810>
This adds an option to test the apply_bytes helper. This option lets
the user specify an int on the command line specifying how much data
each verdict should apply to.
When this is set a map entry is set with the bytes input by the user
and then the specified program --txmsg or --txmsg_redir will use the
value and set the applied data. If no other option is set then a
default --txmsg_apply program is run. This program will drop pkts
if an error is detected on the bytes map lookup. Useful to verify
the map lookup and apply helper are working and causing a hard
error if it is not.
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: David S. Miller <davem@davemloft.net>
---
samples/sockmap/sockmap_kern.c | 54 ++++++++++++++++++++++++++---
samples/sockmap/sockmap_user.c | 19 ++++++++++
tools/testing/selftests/bpf/bpf_helpers.h | 3 +-
3 files changed, 68 insertions(+), 8 deletions(-)
diff --git a/samples/sockmap/sockmap_kern.c b/samples/sockmap/sockmap_kern.c
index 75edb2f..205ec36 100644
--- a/samples/sockmap/sockmap_kern.c
+++ b/samples/sockmap/sockmap_kern.c
@@ -57,6 +57,13 @@ struct bpf_map_def SEC("maps") sock_map_redir = {
.max_entries = 1,
};
+struct bpf_map_def SEC("maps") sock_apply_bytes = {
+ .type = BPF_MAP_TYPE_ARRAY,
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .max_entries = 1
+};
+
SEC("sk_skb1")
int bpf_prog1(struct __sk_buff *skb)
{
@@ -123,6 +130,11 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
SEC("sk_msg1")
int bpf_prog4(struct sk_msg_md *msg)
{
+ int *bytes, zero = 0;
+
+ bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
+ if (bytes)
+ bpf_msg_apply_bytes(msg, *bytes);
return SK_PASS;
}
@@ -131,8 +143,13 @@ int bpf_prog5(struct sk_msg_md *msg)
{
void *data_end = (void *)(long) msg->data_end;
void *data = (void *)(long) msg->data;
+ int *bytes, err = 0, zero = 0;
- bpf_printk("sk_msg2: data length %i\n", (__u32)data_end - (__u32)data);
+ bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
+ if (bytes)
+ err = bpf_msg_apply_bytes(msg, *bytes);
+ bpf_printk("sk_msg2: data length %i err %i\n",
+ (__u64)data_end - (__u64)data, err);
return SK_PASS;
}
@@ -141,9 +158,12 @@ int bpf_prog6(struct sk_msg_md *msg)
{
void *data_end = (void *)(long) msg->data_end;
void *data = (void *)(long) msg->data;
- int ret = 0;
+ int *bytes, zero = 0;
- return bpf_msg_redirect_map(msg, &sock_map_redir, ret, 0);
+ bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
+ if (bytes)
+ bpf_msg_apply_bytes(msg, *bytes);
+ return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
}
SEC("sk_msg4")
@@ -151,10 +171,32 @@ int bpf_prog7(struct sk_msg_md *msg)
{
void *data_end = (void *)(long) msg->data_end;
void *data = (void *)(long) msg->data;
- int ret = 0;
+ int *bytes, err = 0, zero = 0;
+
+ bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
+ if (bytes)
+ err = bpf_msg_apply_bytes(msg, *bytes);
+ bpf_printk("sk_msg3: redirect(%iB) err=%i\n",
+ (__u64)data_end - (__u64)data, err);
+ return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
+}
- bpf_printk("sk_msg3: redirect(%iB)\n", (__u32)data_end - (__u32)data);
- return bpf_msg_redirect_map(msg, &sock_map_redir, ret, 0);
+SEC("sk_msg5")
+int bpf_prog8(struct sk_msg_md *msg)
+{
+ void *data_end = (void *)(long) msg->data_end;
+ void *data = (void *)(long) msg->data;
+ int ret = 0, *bytes, zero = 0;
+
+ bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
+ if (bytes) {
+ ret = bpf_msg_apply_bytes(msg, *bytes);
+ if (ret)
+ return SK_DROP;
+ } else {
+ return SK_DROP;
+ }
+ return SK_PASS;
}
char _license[] SEC("license") = "GPL";
diff --git a/samples/sockmap/sockmap_user.c b/samples/sockmap/sockmap_user.c
index 8017ad7a..41774ec 100644
--- a/samples/sockmap/sockmap_user.c
+++ b/samples/sockmap/sockmap_user.c
@@ -59,6 +59,7 @@
int txmsg_noisy;
int txmsg_redir;
int txmsg_redir_noisy;
+int txmsg_apply;
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h' },
@@ -73,6 +74,7 @@
{"txmsg_noisy", no_argument, &txmsg_noisy, 1 },
{"txmsg_redir", no_argument, &txmsg_redir, 1 },
{"txmsg_redir_noisy", no_argument, &txmsg_redir_noisy, 1},
+ {"txmsg_apply", required_argument, NULL, 'a'},
{0, 0, NULL, 0 }
};
@@ -546,7 +548,9 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
long_options, &longindex)) != -1) {
switch (opt) {
- /* Cgroup configuration */
+ case 'a':
+ txmsg_apply = atoi(optarg);
+ break;
case 'c':
cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
if (cg_fd < 0) {
@@ -665,6 +669,8 @@ int main(int argc, char **argv)
tx_prog_fd = prog_fd[5];
else if (txmsg_redir_noisy)
tx_prog_fd = prog_fd[6];
+ else if (txmsg_apply)
+ tx_prog_fd = prog_fd[7];
else
tx_prog_fd = 0;
@@ -699,6 +705,17 @@ int main(int argc, char **argv)
err, strerror(errno));
return err;
}
+
+ if (txmsg_apply) {
+ err = bpf_map_update_elem(map_fd[3],
+ &i, &txmsg_apply, BPF_ANY);
+ if (err) {
+ fprintf(stderr,
+ "ERROR: bpf_map_update_elem (apply_bytes): %d (%s\n",
+ err, strerror(errno));
+ return err;
+ }
+ }
}
if (test == PING_PONG)
err = forever_ping_pong(rate, &options);
diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h
index bba7ee6..4713de4 100644
--- a/tools/testing/selftests/bpf/bpf_helpers.h
+++ b/tools/testing/selftests/bpf/bpf_helpers.h
@@ -88,7 +88,8 @@ static int (*bpf_override_return)(void *ctx, unsigned long rc) =
(void *) BPF_FUNC_override_return;
static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) =
(void *) BPF_FUNC_msg_redirect_map;
-
+static int (*bpf_msg_apply_bytes)(void *ctx, int len) =
+ (void *) BPF_FUNC_msg_apply_bytes;
/* llvm builtin functions that eBPF C program may use to
* emit BPF_LD_ABS and BPF_LD_IND instructions
next prev parent reply other threads:[~2018-03-18 19:58 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-18 19:56 [bpf-next PATCH v3 00/18] bpf,sockmap: sendmsg/sendfile ULP John Fastabend
2018-03-18 19:56 ` [bpf-next PATCH v3 01/18] sock: make static tls function alloc_sg generic sock helper John Fastabend
2018-03-18 19:56 ` [bpf-next PATCH v3 02/18] sockmap: convert refcnt to an atomic refcnt John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 03/18] net: do_tcp_sendpages flag to avoid SKBTX_SHARED_FRAG John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 04/18] net: generalize sk_alloc_sg to work with scatterlist rings John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 05/18] bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data John Fastabend
2018-03-18 20:30 ` David Miller
2018-03-19 16:27 ` Alexei Starovoitov
2018-03-18 19:57 ` [bpf-next PATCH v3 06/18] bpf: sockmap, add bpf_msg_apply_bytes() helper John Fastabend
2018-03-18 20:30 ` David Miller
2018-03-19 16:27 ` Alexei Starovoitov
2018-03-18 19:57 ` [bpf-next PATCH v3 07/18] bpf: sockmap, add msg_cork_bytes() helper John Fastabend
2018-03-18 20:30 ` David Miller
2018-03-19 16:30 ` Alexei Starovoitov
2018-03-19 20:00 ` John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 08/18] bpf: sk_msg program helper bpf_sk_msg_pull_data John Fastabend
2018-03-18 20:31 ` David Miller
2018-03-19 20:24 ` Alexei Starovoitov
2018-03-20 5:54 ` John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 09/18] bpf: add map tests for BPF_PROG_TYPE_SK_MSG John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 10/18] bpf: add verifier " John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 11/18] bpf: sockmap sample, add option to attach SK_MSG program John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 12/18] bpf: sockmap sample, add sendfile test John Fastabend
2018-03-18 19:57 ` [bpf-next PATCH v3 13/18] bpf: sockmap sample, add data verification option John Fastabend
2018-03-18 19:57 ` John Fastabend [this message]
2018-03-18 19:58 ` [bpf-next PATCH v3 15/18] bpf: sockmap sample support for bpf_msg_cork_bytes() John Fastabend
2018-03-18 19:58 ` [bpf-next PATCH v3 16/18] bpf: sockmap add SK_DROP tests John Fastabend
2018-03-18 19:58 ` [bpf-next PATCH v3 17/18] bpf: sockmap sample test for bpf_msg_pull_data John Fastabend
2018-03-18 19:58 ` [bpf-next PATCH v3 18/18] bpf: sockmap test script John Fastabend
2018-03-19 20:20 ` [bpf-next PATCH v3 00/18] bpf,sockmap: sendmsg/sendfile ULP Daniel Borkmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180318195756.14466.76764.stgit@john-Precision-Tower-5810 \
--to=john.fastabend@gmail.com \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=davejwatson@fb.com \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox