diff options
| author | John Fastabend <john.fastabend@gmail.com> | 2018-03-28 15:49:20 -0400 |
|---|---|---|
| committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-03-29 18:09:43 -0400 |
| commit | 2596f64cb2ff5767166bee4e812734747c7a0474 (patch) | |
| tree | bab85758f35f1036680347ffb84115f9b5aa35d7 /samples | |
| parent | 8934ce2fd08171e8605f7fada91ee7619fe17ab8 (diff) | |
bpf: sockmap, add BPF_F_INGRESS tests
Add a set of tests to verify ingress flag in redirect helpers
works correctly with various msg sizes.
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/sockmap/sockmap_kern.c | 41 | ||||
| -rwxr-xr-x | samples/sockmap/sockmap_test.sh | 22 | ||||
| -rw-r--r-- | samples/sockmap/sockmap_user.c | 35 |
3 files changed, 87 insertions, 11 deletions
diff --git a/samples/sockmap/sockmap_kern.c b/samples/sockmap/sockmap_kern.c index 9ad5ba79c85a..ca2872260e39 100644 --- a/samples/sockmap/sockmap_kern.c +++ b/samples/sockmap/sockmap_kern.c | |||
| @@ -54,7 +54,7 @@ struct bpf_map_def SEC("maps") sock_map_redir = { | |||
| 54 | .type = BPF_MAP_TYPE_SOCKMAP, | 54 | .type = BPF_MAP_TYPE_SOCKMAP, |
| 55 | .key_size = sizeof(int), | 55 | .key_size = sizeof(int), |
| 56 | .value_size = sizeof(int), | 56 | .value_size = sizeof(int), |
| 57 | .max_entries = 1, | 57 | .max_entries = 20, |
| 58 | }; | 58 | }; |
| 59 | 59 | ||
| 60 | struct bpf_map_def SEC("maps") sock_apply_bytes = { | 60 | struct bpf_map_def SEC("maps") sock_apply_bytes = { |
| @@ -78,6 +78,13 @@ struct bpf_map_def SEC("maps") sock_pull_bytes = { | |||
| 78 | .max_entries = 2 | 78 | .max_entries = 2 |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | struct bpf_map_def SEC("maps") sock_redir_flags = { | ||
| 82 | .type = BPF_MAP_TYPE_ARRAY, | ||
| 83 | .key_size = sizeof(int), | ||
| 84 | .value_size = sizeof(int), | ||
| 85 | .max_entries = 1 | ||
| 86 | }; | ||
| 87 | |||
| 81 | 88 | ||
| 82 | SEC("sk_skb1") | 89 | SEC("sk_skb1") |
| 83 | int bpf_prog1(struct __sk_buff *skb) | 90 | int bpf_prog1(struct __sk_buff *skb) |
| @@ -197,8 +204,9 @@ int bpf_prog5(struct sk_msg_md *msg) | |||
| 197 | SEC("sk_msg3") | 204 | SEC("sk_msg3") |
| 198 | int bpf_prog6(struct sk_msg_md *msg) | 205 | int bpf_prog6(struct sk_msg_md *msg) |
| 199 | { | 206 | { |
| 200 | int *bytes, zero = 0, one = 1; | 207 | int *bytes, zero = 0, one = 1, key = 0; |
| 201 | int *start, *end; | 208 | int *start, *end, *f; |
| 209 | __u64 flags = 0; | ||
| 202 | 210 | ||
| 203 | bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); | 211 | bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); |
| 204 | if (bytes) | 212 | if (bytes) |
| @@ -210,15 +218,22 @@ int bpf_prog6(struct sk_msg_md *msg) | |||
| 210 | end = bpf_map_lookup_elem(&sock_pull_bytes, &one); | 218 | end = bpf_map_lookup_elem(&sock_pull_bytes, &one); |
| 211 | if (start && end) | 219 | if (start && end) |
| 212 | bpf_msg_pull_data(msg, *start, *end, 0); | 220 | bpf_msg_pull_data(msg, *start, *end, 0); |
| 213 | return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0); | 221 | f = bpf_map_lookup_elem(&sock_redir_flags, &zero); |
| 222 | if (f && *f) { | ||
| 223 | key = 2; | ||
| 224 | flags = *f; | ||
| 225 | } | ||
| 226 | return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags); | ||
| 214 | } | 227 | } |
| 215 | 228 | ||
| 216 | SEC("sk_msg4") | 229 | SEC("sk_msg4") |
| 217 | int bpf_prog7(struct sk_msg_md *msg) | 230 | int bpf_prog7(struct sk_msg_md *msg) |
| 218 | { | 231 | { |
| 219 | int err1 = 0, err2 = 0, zero = 0, one = 1; | 232 | int err1 = 0, err2 = 0, zero = 0, one = 1, key = 0; |
| 220 | int *bytes, *start, *end, len1, len2; | 233 | int *f, *bytes, *start, *end, len1, len2; |
| 234 | __u64 flags = 0; | ||
| 221 | 235 | ||
| 236 | int err; | ||
| 222 | bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); | 237 | bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); |
| 223 | if (bytes) | 238 | if (bytes) |
| 224 | err1 = bpf_msg_apply_bytes(msg, *bytes); | 239 | err1 = bpf_msg_apply_bytes(msg, *bytes); |
| @@ -229,7 +244,6 @@ int bpf_prog7(struct sk_msg_md *msg) | |||
| 229 | start = bpf_map_lookup_elem(&sock_pull_bytes, &zero); | 244 | start = bpf_map_lookup_elem(&sock_pull_bytes, &zero); |
| 230 | end = bpf_map_lookup_elem(&sock_pull_bytes, &one); | 245 | end = bpf_map_lookup_elem(&sock_pull_bytes, &one); |
| 231 | if (start && end) { | 246 | if (start && end) { |
| 232 | int err; | ||
| 233 | 247 | ||
| 234 | bpf_printk("sk_msg2: pull(%i:%i)\n", | 248 | bpf_printk("sk_msg2: pull(%i:%i)\n", |
| 235 | start ? *start : 0, end ? *end : 0); | 249 | start ? *start : 0, end ? *end : 0); |
| @@ -241,9 +255,16 @@ int bpf_prog7(struct sk_msg_md *msg) | |||
| 241 | bpf_printk("sk_msg2: length update %i->%i\n", | 255 | bpf_printk("sk_msg2: length update %i->%i\n", |
| 242 | len1, len2); | 256 | len1, len2); |
| 243 | } | 257 | } |
| 244 | bpf_printk("sk_msg3: redirect(%iB) err1=%i err2=%i\n", | 258 | f = bpf_map_lookup_elem(&sock_redir_flags, &zero); |
| 245 | len1, err1, err2); | 259 | if (f && *f) { |
| 246 | return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0); | 260 | key = 2; |
| 261 | flags = *f; | ||
| 262 | } | ||
| 263 | bpf_printk("sk_msg3: redirect(%iB) flags=%i err=%i\n", | ||
| 264 | len1, flags, err1 ? err1 : err2); | ||
| 265 | err = bpf_msg_redirect_map(msg, &sock_map_redir, key, flags); | ||
| 266 | bpf_printk("sk_msg3: err %i\n", err); | ||
| 267 | return err; | ||
| 247 | } | 268 | } |
| 248 | 269 | ||
| 249 | SEC("sk_msg5") | 270 | SEC("sk_msg5") |
diff --git a/samples/sockmap/sockmap_test.sh b/samples/sockmap/sockmap_test.sh index 6d8cc40cca22..13b205fdb0f0 100755 --- a/samples/sockmap/sockmap_test.sh +++ b/samples/sockmap/sockmap_test.sh | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #Test a bunch of positive cases to verify basic functionality | 1 | #Test a bunch of positive cases to verify basic functionality |
| 2 | for prog in "--txmsg" "--txmsg_redir" "--txmsg_drop"; do | 2 | for prog in "--txmsg_redir --txmsg_ingress" "--txmsg" "--txmsg_redir" "--txmsg_redir --txmsg_ingress" "--txmsg_drop"; do |
| 3 | for t in "sendmsg" "sendpage"; do | 3 | for t in "sendmsg" "sendpage"; do |
| 4 | for r in 1 10 100; do | 4 | for r in 1 10 100; do |
| 5 | for i in 1 10 100; do | 5 | for i in 1 10 100; do |
| @@ -100,6 +100,16 @@ for t in "sendmsg" "sendpage"; do | |||
| 100 | sleep 2 | 100 | sleep 2 |
| 101 | done | 101 | done |
| 102 | 102 | ||
| 103 | prog="--txmsg_redir --txmsg_apply 1 --txmsg_ingress" | ||
| 104 | |||
| 105 | for t in "sendmsg" "sendpage"; do | ||
| 106 | TEST="./sockmap --cgroup /mnt/cgroup2/ -t $t -r $r -i $i -l $l $prog" | ||
| 107 | echo $TEST | ||
| 108 | $TEST | ||
| 109 | sleep 2 | ||
| 110 | done | ||
| 111 | |||
| 112 | |||
| 103 | # Test apply and redirect with larger value than send | 113 | # Test apply and redirect with larger value than send |
| 104 | r=1 | 114 | r=1 |
| 105 | i=8 | 115 | i=8 |
| @@ -113,6 +123,16 @@ for t in "sendmsg" "sendpage"; do | |||
| 113 | sleep 2 | 123 | sleep 2 |
| 114 | done | 124 | done |
| 115 | 125 | ||
| 126 | prog="--txmsg_redir --txmsg_apply 2048 --txmsg_ingress" | ||
| 127 | |||
| 128 | for t in "sendmsg" "sendpage"; do | ||
| 129 | TEST="./sockmap --cgroup /mnt/cgroup2/ -t $t -r $r -i $i -l $l $prog" | ||
| 130 | echo $TEST | ||
| 131 | $TEST | ||
| 132 | sleep 2 | ||
| 133 | done | ||
| 134 | |||
| 135 | |||
| 116 | # Test apply and redirect with apply that never reaches limit | 136 | # Test apply and redirect with apply that never reaches limit |
| 117 | r=1024 | 137 | r=1024 |
| 118 | i=1 | 138 | i=1 |
diff --git a/samples/sockmap/sockmap_user.c b/samples/sockmap/sockmap_user.c index 07aa237221d1..f7503f44b209 100644 --- a/samples/sockmap/sockmap_user.c +++ b/samples/sockmap/sockmap_user.c | |||
| @@ -64,6 +64,7 @@ int txmsg_apply; | |||
| 64 | int txmsg_cork; | 64 | int txmsg_cork; |
| 65 | int txmsg_start; | 65 | int txmsg_start; |
| 66 | int txmsg_end; | 66 | int txmsg_end; |
| 67 | int txmsg_ingress; | ||
| 67 | 68 | ||
| 68 | static const struct option long_options[] = { | 69 | static const struct option long_options[] = { |
| 69 | {"help", no_argument, NULL, 'h' }, | 70 | {"help", no_argument, NULL, 'h' }, |
| @@ -83,6 +84,7 @@ static const struct option long_options[] = { | |||
| 83 | {"txmsg_cork", required_argument, NULL, 'k'}, | 84 | {"txmsg_cork", required_argument, NULL, 'k'}, |
| 84 | {"txmsg_start", required_argument, NULL, 's'}, | 85 | {"txmsg_start", required_argument, NULL, 's'}, |
| 85 | {"txmsg_end", required_argument, NULL, 'e'}, | 86 | {"txmsg_end", required_argument, NULL, 'e'}, |
| 87 | {"txmsg_ingress", no_argument, &txmsg_ingress, 1 }, | ||
| 86 | {0, 0, NULL, 0 } | 88 | {0, 0, NULL, 0 } |
| 87 | }; | 89 | }; |
| 88 | 90 | ||
| @@ -793,6 +795,39 @@ run: | |||
| 793 | return err; | 795 | return err; |
| 794 | } | 796 | } |
| 795 | } | 797 | } |
| 798 | |||
| 799 | if (txmsg_ingress) { | ||
| 800 | int in = BPF_F_INGRESS; | ||
| 801 | |||
| 802 | i = 0; | ||
| 803 | err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY); | ||
| 804 | if (err) { | ||
| 805 | fprintf(stderr, | ||
| 806 | "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n", | ||
| 807 | err, strerror(errno)); | ||
| 808 | } | ||
| 809 | i = 1; | ||
| 810 | err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY); | ||
| 811 | if (err) { | ||
| 812 | fprintf(stderr, | ||
| 813 | "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n", | ||
| 814 | err, strerror(errno)); | ||
| 815 | } | ||
| 816 | |||
