summaryrefslogtreecommitdiff
path: root/samples/sockmap/sockmap_kern.c
diff options
context:
space:
mode:
authorJohn Fastabend <john.fastabend@gmail.com>2018-03-18 12:57:56 -0700
committerDaniel Borkmann <daniel@iogearbox.net>2018-03-19 21:14:40 +0100
commit1c16c3126ac938562a68ab7041fe74ce0de53166 (patch)
tree5792e6806284fb388783223e3d9cd2d9ae70fdaf /samples/sockmap/sockmap_kern.c
parent6bce9d2ca65238d37890186bf41acd7f7d9a9820 (diff)
bpf: sockmap, add sample option to test apply_bytes helper
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> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'samples/sockmap/sockmap_kern.c')
-rw-r--r--samples/sockmap/sockmap_kern.c54
1 files changed, 48 insertions, 6 deletions
diff --git a/samples/sockmap/sockmap_kern.c b/samples/sockmap/sockmap_kern.c
index 75edb2f151d4..205ec36449d1 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";