Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2017 Facebook |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/vmalloc.h> |
| 7 | #include <linux/etherdevice.h> |
| 8 | #include <linux/filter.h> |
| 9 | #include <linux/sched/signal.h> |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 10 | #include <net/bpf_sk_storage.h> |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 11 | #include <net/sock.h> |
| 12 | #include <net/tcp.h> |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 13 | #include <linux/error-injection.h> |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 14 | |
Matt Mullins | e950e84 | 2019-04-26 11:49:51 -0700 | [diff] [blame] | 15 | #define CREATE_TRACE_POINTS |
| 16 | #include <trace/events/bpf_test_run.h> |
| 17 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 18 | static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 19 | u32 *retval, u32 *time, bool xdp) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 20 | { |
Bo YU | 71b91a5 | 2019-03-08 01:45:51 -0500 | [diff] [blame] | 21 | struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL }; |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 22 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 23 | u64 time_start, time_spent = 0; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 24 | int ret = 0; |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 25 | u32 i; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 26 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 27 | for_each_cgroup_storage_type(stype) { |
| 28 | storage[stype] = bpf_cgroup_storage_alloc(prog, stype); |
| 29 | if (IS_ERR(storage[stype])) { |
| 30 | storage[stype] = NULL; |
| 31 | for_each_cgroup_storage_type(stype) |
| 32 | bpf_cgroup_storage_free(storage[stype]); |
| 33 | return -ENOMEM; |
| 34 | } |
| 35 | } |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 36 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 37 | if (!repeat) |
| 38 | repeat = 1; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 39 | |
| 40 | rcu_read_lock(); |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 41 | migrate_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 42 | time_start = ktime_get_ns(); |
| 43 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 44 | bpf_cgroup_storage_set(storage); |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 45 | |
| 46 | if (xdp) |
| 47 | *retval = bpf_prog_run_xdp(prog, ctx); |
| 48 | else |
| 49 | *retval = BPF_PROG_RUN(prog, ctx); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 50 | |
| 51 | if (signal_pending(current)) { |
| 52 | ret = -EINTR; |
| 53 | break; |
| 54 | } |
| 55 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 56 | if (need_resched()) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 57 | time_spent += ktime_get_ns() - time_start; |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 58 | migrate_enable(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 59 | rcu_read_unlock(); |
| 60 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 61 | cond_resched(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 62 | |
| 63 | rcu_read_lock(); |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 64 | migrate_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 65 | time_start = ktime_get_ns(); |
| 66 | } |
| 67 | } |
| 68 | time_spent += ktime_get_ns() - time_start; |
David Miller | 6eac779 | 2020-02-24 15:01:44 +0100 | [diff] [blame] | 69 | migrate_enable(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 70 | rcu_read_unlock(); |
| 71 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 72 | do_div(time_spent, repeat); |
| 73 | *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 74 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 75 | for_each_cgroup_storage_type(stype) |
| 76 | bpf_cgroup_storage_free(storage[stype]); |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 77 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 78 | return ret; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 81 | static int bpf_test_finish(const union bpf_attr *kattr, |
| 82 | union bpf_attr __user *uattr, const void *data, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 83 | u32 size, u32 retval, u32 duration) |
| 84 | { |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 85 | void __user *data_out = u64_to_user_ptr(kattr->test.data_out); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 86 | int err = -EFAULT; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 87 | u32 copy_size = size; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 88 | |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 89 | /* Clamp copy if the user has provided a size hint, but copy the full |
| 90 | * buffer if not to retain old behaviour. |
| 91 | */ |
| 92 | if (kattr->test.data_size_out && |
| 93 | copy_size > kattr->test.data_size_out) { |
| 94 | copy_size = kattr->test.data_size_out; |
| 95 | err = -ENOSPC; |
| 96 | } |
| 97 | |
| 98 | if (data_out && copy_to_user(data_out, data, copy_size)) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 99 | goto out; |
| 100 | if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) |
| 101 | goto out; |
| 102 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) |
| 103 | goto out; |
| 104 | if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) |
| 105 | goto out; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 106 | if (err != -ENOSPC) |
| 107 | err = 0; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 108 | out: |
Matt Mullins | e950e84 | 2019-04-26 11:49:51 -0700 | [diff] [blame] | 109 | trace_bpf_test_finish(&err); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 110 | return err; |
| 111 | } |
| 112 | |
Alexei Starovoitov | faeb2dc | 2019-11-14 10:57:08 -0800 | [diff] [blame] | 113 | /* Integer types of various sizes and pointer combinations cover variety of |
| 114 | * architecture dependent calling conventions. 7+ can be supported in the |
| 115 | * future. |
| 116 | */ |
Jean-Philippe Menil | e9ff9d5 | 2020-03-27 21:47:13 +0100 | [diff] [blame] | 117 | __diag_push(); |
| 118 | __diag_ignore(GCC, 8, "-Wmissing-prototypes", |
| 119 | "Global functions as their definitions will be in vmlinux BTF"); |
Alexei Starovoitov | faeb2dc | 2019-11-14 10:57:08 -0800 | [diff] [blame] | 120 | int noinline bpf_fentry_test1(int a) |
| 121 | { |
| 122 | return a + 1; |
| 123 | } |
| 124 | |
| 125 | int noinline bpf_fentry_test2(int a, u64 b) |
| 126 | { |
| 127 | return a + b; |
| 128 | } |
| 129 | |
| 130 | int noinline bpf_fentry_test3(char a, int b, u64 c) |
| 131 | { |
| 132 | return a + b + c; |
| 133 | } |
| 134 | |
| 135 | int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) |
| 136 | { |
| 137 | return (long)a + b + c + d; |
| 138 | } |
| 139 | |
| 140 | int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) |
| 141 | { |
| 142 | return a + (long)b + c + d + e; |
| 143 | } |
| 144 | |
| 145 | int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) |
| 146 | { |
| 147 | return a + (long)b + c + d + (long)e + f; |
| 148 | } |
| 149 | |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 150 | int noinline bpf_modify_return_test(int a, int *b) |
| 151 | { |
| 152 | *b += 1; |
| 153 | return a + *b; |
| 154 | } |
Jean-Philippe Menil | e9ff9d5 | 2020-03-27 21:47:13 +0100 | [diff] [blame] | 155 | __diag_pop(); |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 156 | |
| 157 | ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); |
| 158 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 159 | static void *bpf_test_init(const union bpf_attr *kattr, u32 size, |
| 160 | u32 headroom, u32 tailroom) |
| 161 | { |
| 162 | void __user *data_in = u64_to_user_ptr(kattr->test.data_in); |
| 163 | void *data; |
| 164 | |
| 165 | if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) |
| 166 | return ERR_PTR(-EINVAL); |
| 167 | |
| 168 | data = kzalloc(size + headroom + tailroom, GFP_USER); |
| 169 | if (!data) |
| 170 | return ERR_PTR(-ENOMEM); |
| 171 | |
| 172 | if (copy_from_user(data + headroom, data_in, size)) { |
| 173 | kfree(data); |
| 174 | return ERR_PTR(-EFAULT); |
| 175 | } |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 176 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 177 | return data; |
| 178 | } |
| 179 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 180 | int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
| 181 | const union bpf_attr *kattr, |
| 182 | union bpf_attr __user *uattr) |
| 183 | { |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 184 | u16 side_effect = 0, ret = 0; |
| 185 | int b = 2, err = -EFAULT; |
| 186 | u32 retval = 0; |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 187 | |
| 188 | switch (prog->expected_attach_type) { |
| 189 | case BPF_TRACE_FENTRY: |
| 190 | case BPF_TRACE_FEXIT: |
| 191 | if (bpf_fentry_test1(1) != 2 || |
| 192 | bpf_fentry_test2(2, 3) != 5 || |
| 193 | bpf_fentry_test3(4, 5, 6) != 15 || |
| 194 | bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || |
| 195 | bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || |
| 196 | bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111) |
| 197 | goto out; |
| 198 | break; |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 199 | case BPF_MODIFY_RETURN: |
| 200 | ret = bpf_modify_return_test(1, &b); |
| 201 | if (b != 2) |
| 202 | side_effect = 1; |
| 203 | break; |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 204 | default: |
| 205 | goto out; |
| 206 | } |
| 207 | |
KP Singh | 3d08b6f | 2020-03-04 20:18:53 +0100 | [diff] [blame] | 208 | retval = ((u32)side_effect << 16) | ret; |
| 209 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) |
| 210 | goto out; |
| 211 | |
KP Singh | da00d2f | 2020-03-04 20:18:52 +0100 | [diff] [blame] | 212 | err = 0; |
| 213 | out: |
| 214 | trace_bpf_test_finish(&err); |
| 215 | return err; |
| 216 | } |
| 217 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 218 | static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) |
| 219 | { |
| 220 | void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); |
| 221 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 222 | u32 size = kattr->test.ctx_size_in; |
| 223 | void *data; |
| 224 | int err; |
| 225 | |
| 226 | if (!data_in && !data_out) |
| 227 | return NULL; |
| 228 | |
| 229 | data = kzalloc(max_size, GFP_USER); |
| 230 | if (!data) |
| 231 | return ERR_PTR(-ENOMEM); |
| 232 | |
| 233 | if (data_in) { |
| 234 | err = bpf_check_uarg_tail_zero(data_in, max_size, size); |
| 235 | if (err) { |
| 236 | kfree(data); |
| 237 | return ERR_PTR(err); |
| 238 | } |
| 239 | |
| 240 | size = min_t(u32, max_size, size); |
| 241 | if (copy_from_user(data, data_in, size)) { |
| 242 | kfree(data); |
| 243 | return ERR_PTR(-EFAULT); |
| 244 | } |
| 245 | } |
| 246 | return data; |
| 247 | } |
| 248 | |
| 249 | static int bpf_ctx_finish(const union bpf_attr *kattr, |
| 250 | union bpf_attr __user *uattr, const void *data, |
| 251 | u32 size) |
| 252 | { |
| 253 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 254 | int err = -EFAULT; |
| 255 | u32 copy_size = size; |
| 256 | |
| 257 | if (!data || !data_out) |
| 258 | return 0; |
| 259 | |
| 260 | if (copy_size > kattr->test.ctx_size_out) { |
| 261 | copy_size = kattr->test.ctx_size_out; |
| 262 | err = -ENOSPC; |
| 263 | } |
| 264 | |
| 265 | if (copy_to_user(data_out, data, copy_size)) |
| 266 | goto out; |
| 267 | if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) |
| 268 | goto out; |
| 269 | if (err != -ENOSPC) |
| 270 | err = 0; |
| 271 | out: |
| 272 | return err; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * range_is_zero - test whether buffer is initialized |
| 277 | * @buf: buffer to check |
| 278 | * @from: check from this position |
| 279 | * @to: check up until (excluding) this position |
| 280 | * |
| 281 | * This function returns true if the there is a non-zero byte |
| 282 | * in the buf in the range [from,to). |
| 283 | */ |
| 284 | static inline bool range_is_zero(void *buf, size_t from, size_t to) |
| 285 | { |
| 286 | return !memchr_inv((u8 *)buf + from, 0, to - from); |
| 287 | } |
| 288 | |
| 289 | static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 290 | { |
| 291 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 292 | |
| 293 | if (!__skb) |
| 294 | return 0; |
| 295 | |
| 296 | /* make sure the fields we don't use are zeroed */ |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 297 | if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | /* mark is allowed */ |
| 301 | |
| 302 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), |
| 303 | offsetof(struct __sk_buff, priority))) |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 304 | return -EINVAL; |
| 305 | |
| 306 | /* priority is allowed */ |
| 307 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 308 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, priority), |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 309 | offsetof(struct __sk_buff, cb))) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | /* cb is allowed */ |
| 313 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 314 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 315 | offsetof(struct __sk_buff, tstamp))) |
| 316 | return -EINVAL; |
| 317 | |
| 318 | /* tstamp is allowed */ |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 319 | /* wire_len is allowed */ |
| 320 | /* gso_segs is allowed */ |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 321 | |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 322 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), |
Willem de Bruijn | cf62089 | 2020-03-03 15:05:01 -0500 | [diff] [blame] | 323 | offsetof(struct __sk_buff, gso_size))) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | /* gso_size is allowed */ |
| 327 | |
| 328 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 329 | sizeof(struct __sk_buff))) |
| 330 | return -EINVAL; |
| 331 | |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 332 | skb->mark = __skb->mark; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 333 | skb->priority = __skb->priority; |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 334 | skb->tstamp = __skb->tstamp; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 335 | memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); |
| 336 | |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 337 | if (__skb->wire_len == 0) { |
| 338 | cb->pkt_len = skb->len; |
| 339 | } else { |
| 340 | if (__skb->wire_len < skb->len || |
| 341 | __skb->wire_len > GSO_MAX_SIZE) |
| 342 | return -EINVAL; |
| 343 | cb->pkt_len = __skb->wire_len; |
| 344 | } |
| 345 | |
| 346 | if (__skb->gso_segs > GSO_MAX_SEGS) |
| 347 | return -EINVAL; |
| 348 | skb_shinfo(skb)->gso_segs = __skb->gso_segs; |
Willem de Bruijn | cf62089 | 2020-03-03 15:05:01 -0500 | [diff] [blame] | 349 | skb_shinfo(skb)->gso_size = __skb->gso_size; |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 350 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 355 | { |
| 356 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 357 | |
| 358 | if (!__skb) |
| 359 | return; |
| 360 | |
Nikita V. Shirokov | 6de6c1f | 2019-12-18 12:57:47 -0800 | [diff] [blame] | 361 | __skb->mark = skb->mark; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 362 | __skb->priority = skb->priority; |
Stanislav Fomichev | ba94094 | 2019-10-15 11:31:24 -0700 | [diff] [blame] | 363 | __skb->tstamp = skb->tstamp; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 364 | memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); |
Stanislav Fomichev | 850a88c | 2019-12-13 14:30:27 -0800 | [diff] [blame] | 365 | __skb->wire_len = cb->pkt_len; |
| 366 | __skb->gso_segs = skb_shinfo(skb)->gso_segs; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 369 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 370 | union bpf_attr __user *uattr) |
| 371 | { |
| 372 | bool is_l2 = false, is_direct_pkt_access = false; |
| 373 | u32 size = kattr->test.data_size_in; |
| 374 | u32 repeat = kattr->test.repeat; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 375 | struct __sk_buff *ctx = NULL; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 376 | u32 retval, duration; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 377 | int hh_len = ETH_HLEN; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 378 | struct sk_buff *skb; |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 379 | struct sock *sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 380 | void *data; |
| 381 | int ret; |
| 382 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 383 | data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 384 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); |
| 385 | if (IS_ERR(data)) |
| 386 | return PTR_ERR(data); |
| 387 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 388 | ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); |
| 389 | if (IS_ERR(ctx)) { |
| 390 | kfree(data); |
| 391 | return PTR_ERR(ctx); |
| 392 | } |
| 393 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 394 | switch (prog->type) { |
| 395 | case BPF_PROG_TYPE_SCHED_CLS: |
| 396 | case BPF_PROG_TYPE_SCHED_ACT: |
| 397 | is_l2 = true; |
| 398 | /* fall through */ |
| 399 | case BPF_PROG_TYPE_LWT_IN: |
| 400 | case BPF_PROG_TYPE_LWT_OUT: |
| 401 | case BPF_PROG_TYPE_LWT_XMIT: |
| 402 | is_direct_pkt_access = true; |
| 403 | break; |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 408 | sk = kzalloc(sizeof(struct sock), GFP_USER); |
| 409 | if (!sk) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 410 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 411 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 412 | return -ENOMEM; |
| 413 | } |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 414 | sock_net_set(sk, current->nsproxy->net_ns); |
| 415 | sock_init_data(NULL, sk); |
| 416 | |
| 417 | skb = build_skb(data, 0); |
| 418 | if (!skb) { |
| 419 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 420 | kfree(ctx); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 421 | kfree(sk); |
| 422 | return -ENOMEM; |
| 423 | } |
| 424 | skb->sk = sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 425 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 426 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 427 | __skb_put(skb, size); |
| 428 | skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev); |
| 429 | skb_reset_network_header(skb); |
| 430 | |
| 431 | if (is_l2) |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 432 | __skb_push(skb, hh_len); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 433 | if (is_direct_pkt_access) |
Daniel Borkmann | 6aaae2b | 2017-09-25 02:25:50 +0200 | [diff] [blame] | 434 | bpf_compute_data_pointers(skb); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 435 | ret = convert___skb_to_skb(skb, ctx); |
| 436 | if (ret) |
| 437 | goto out; |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 438 | ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 439 | if (ret) |
| 440 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 441 | if (!is_l2) { |
| 442 | if (skb_headroom(skb) < hh_len) { |
| 443 | int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); |
| 444 | |
| 445 | if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 446 | ret = -ENOMEM; |
| 447 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | memset(__skb_push(skb, hh_len), 0, hh_len); |
| 451 | } |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 452 | convert_skb_to___skb(skb, ctx); |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 453 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 454 | size = skb->len; |
| 455 | /* bpf program can never convert linear skb to non-linear */ |
| 456 | if (WARN_ON_ONCE(skb_is_nonlinear(skb))) |
| 457 | size = skb_headlen(skb); |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 458 | ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 459 | if (!ret) |
| 460 | ret = bpf_ctx_finish(kattr, uattr, ctx, |
| 461 | sizeof(struct __sk_buff)); |
| 462 | out: |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 463 | kfree_skb(skb); |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 464 | bpf_sk_storage_free(sk); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 465 | kfree(sk); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 466 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 471 | union bpf_attr __user *uattr) |
| 472 | { |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 473 | u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 474 | u32 headroom = XDP_PACKET_HEADROOM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 475 | u32 size = kattr->test.data_size_in; |
| 476 | u32 repeat = kattr->test.repeat; |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 477 | struct netdev_rx_queue *rxqueue; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 478 | struct xdp_buff xdp = {}; |
| 479 | u32 retval, duration; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 480 | u32 max_data_sz; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 481 | void *data; |
| 482 | int ret; |
| 483 | |
Stanislav Fomichev | 947e8b5 | 2019-04-11 15:47:07 -0700 | [diff] [blame] | 484 | if (kattr->test.ctx_in || kattr->test.ctx_out) |
| 485 | return -EINVAL; |
| 486 | |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 487 | /* XDP have extra tailroom as (most) drivers use full page */ |
| 488 | max_data_sz = 4096 - headroom - tailroom; |
| 489 | if (size > max_data_sz) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | data = bpf_test_init(kattr, max_data_sz, headroom, tailroom); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 493 | if (IS_ERR(data)) |
| 494 | return PTR_ERR(data); |
| 495 | |
| 496 | xdp.data_hard_start = data; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 497 | xdp.data = data + headroom; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 498 | xdp.data_meta = xdp.data; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 499 | xdp.data_end = xdp.data + size; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 500 | xdp.frame_sz = headroom + max_data_sz + tailroom; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 501 | |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 502 | rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); |
| 503 | xdp.rxq = &rxqueue->xdp_rxq; |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 504 | bpf_prog_change_xdp(NULL, prog); |
| 505 | ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 506 | if (ret) |
| 507 | goto out; |
Jesper Dangaard Brouer | bc56c91 | 2020-05-14 12:51:35 +0200 | [diff] [blame^] | 508 | if (xdp.data != data + headroom || xdp.data_end != xdp.data + size) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 509 | size = xdp.data_end - xdp.data; |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 510 | ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration); |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 511 | out: |
Björn Töpel | f23c4b3 | 2019-12-13 18:51:10 +0100 | [diff] [blame] | 512 | bpf_prog_change_xdp(prog, NULL); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 513 | kfree(data); |
| 514 | return ret; |
| 515 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 516 | |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 517 | static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) |
| 518 | { |
| 519 | /* make sure the fields we don't use are zeroed */ |
| 520 | if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | /* flags is allowed */ |
| 524 | |
Stanislav Fomichev | b590cb5 | 2019-12-10 11:19:33 -0800 | [diff] [blame] | 525 | if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 526 | sizeof(struct bpf_flow_keys))) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 532 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 533 | const union bpf_attr *kattr, |
| 534 | union bpf_attr __user *uattr) |
| 535 | { |
| 536 | u32 size = kattr->test.data_size_in; |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 537 | struct bpf_flow_dissector ctx = {}; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 538 | u32 repeat = kattr->test.repeat; |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 539 | struct bpf_flow_keys *user_ctx; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 540 | struct bpf_flow_keys flow_keys; |
| 541 | u64 time_start, time_spent = 0; |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 542 | const struct ethhdr *eth; |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 543 | unsigned int flags = 0; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 544 | u32 retval, duration; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 545 | void *data; |
| 546 | int ret; |
| 547 | u32 i; |
| 548 | |
| 549 | if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) |
| 550 | return -EINVAL; |
| 551 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 552 | if (size < ETH_HLEN) |
| 553 | return -EINVAL; |
| 554 | |
| 555 | data = bpf_test_init(kattr, size, 0, 0); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 556 | if (IS_ERR(data)) |
| 557 | return PTR_ERR(data); |
| 558 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 559 | eth = (struct ethhdr *)data; |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 560 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 561 | if (!repeat) |
| 562 | repeat = 1; |
| 563 | |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 564 | user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); |
| 565 | if (IS_ERR(user_ctx)) { |
| 566 | kfree(data); |
| 567 | return PTR_ERR(user_ctx); |
| 568 | } |
| 569 | if (user_ctx) { |
| 570 | ret = verify_user_bpf_flow_keys(user_ctx); |
| 571 | if (ret) |
| 572 | goto out; |
| 573 | flags = user_ctx->flags; |
| 574 | } |
| 575 | |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 576 | ctx.flow_keys = &flow_keys; |
| 577 | ctx.data = data; |
| 578 | ctx.data_end = (__u8 *)data + size; |
| 579 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 580 | rcu_read_lock(); |
| 581 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 582 | time_start = ktime_get_ns(); |
| 583 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 584 | retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 585 | size, flags); |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 586 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 587 | if (signal_pending(current)) { |
| 588 | preempt_enable(); |
| 589 | rcu_read_unlock(); |
| 590 | |
| 591 | ret = -EINTR; |
| 592 | goto out; |
| 593 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 594 | |
| 595 | if (need_resched()) { |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 596 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 597 | preempt_enable(); |
| 598 | rcu_read_unlock(); |
| 599 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 600 | cond_resched(); |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 601 | |
| 602 | rcu_read_lock(); |
| 603 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 604 | time_start = ktime_get_ns(); |
| 605 | } |
| 606 | } |
| 607 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 608 | preempt_enable(); |
| 609 | rcu_read_unlock(); |
| 610 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 611 | do_div(time_spent, repeat); |
| 612 | duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 613 | |
| 614 | ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), |
| 615 | retval, duration); |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 616 | if (!ret) |
| 617 | ret = bpf_ctx_finish(kattr, uattr, user_ctx, |
| 618 | sizeof(struct bpf_flow_keys)); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 619 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 620 | out: |
Stanislav Fomichev | b2ca4e1 | 2019-07-25 15:52:27 -0700 | [diff] [blame] | 621 | kfree(user_ctx); |
Stanislav Fomichev | 7b8a130 | 2019-04-22 08:55:45 -0700 | [diff] [blame] | 622 | kfree(data); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 623 | return ret; |
| 624 | } |