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