Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2017 Facebook |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | */ |
| 7 | #include <linux/bpf.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/vmalloc.h> |
| 10 | #include <linux/etherdevice.h> |
| 11 | #include <linux/filter.h> |
| 12 | #include <linux/sched/signal.h> |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 13 | #include <net/sock.h> |
| 14 | #include <net/tcp.h> |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 15 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 16 | static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, |
| 17 | u32 *retval, u32 *time) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 18 | { |
Bo YU | 71b91a5 | 2019-03-08 01:45:51 -0500 | [diff] [blame] | 19 | struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL }; |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 20 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 21 | u64 time_start, time_spent = 0; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 22 | int ret = 0; |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 23 | u32 i; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 24 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 25 | for_each_cgroup_storage_type(stype) { |
| 26 | storage[stype] = bpf_cgroup_storage_alloc(prog, stype); |
| 27 | if (IS_ERR(storage[stype])) { |
| 28 | storage[stype] = NULL; |
| 29 | for_each_cgroup_storage_type(stype) |
| 30 | bpf_cgroup_storage_free(storage[stype]); |
| 31 | return -ENOMEM; |
| 32 | } |
| 33 | } |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 34 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 35 | if (!repeat) |
| 36 | repeat = 1; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 37 | |
| 38 | rcu_read_lock(); |
| 39 | preempt_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 40 | time_start = ktime_get_ns(); |
| 41 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 42 | bpf_cgroup_storage_set(storage); |
| 43 | *retval = BPF_PROG_RUN(prog, ctx); |
| 44 | |
| 45 | if (signal_pending(current)) { |
| 46 | ret = -EINTR; |
| 47 | break; |
| 48 | } |
| 49 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 50 | if (need_resched()) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 51 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 52 | preempt_enable(); |
| 53 | rcu_read_unlock(); |
| 54 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 55 | cond_resched(); |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 56 | |
| 57 | rcu_read_lock(); |
| 58 | preempt_disable(); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 59 | time_start = ktime_get_ns(); |
| 60 | } |
| 61 | } |
| 62 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 63 | preempt_enable(); |
| 64 | rcu_read_unlock(); |
| 65 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 66 | do_div(time_spent, repeat); |
| 67 | *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 68 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 69 | for_each_cgroup_storage_type(stype) |
| 70 | bpf_cgroup_storage_free(storage[stype]); |
Roman Gushchin | f42ee09 | 2018-08-02 14:27:27 -0700 | [diff] [blame] | 71 | |
Stanislav Fomichev | df1a2cb | 2019-02-12 15:42:38 -0800 | [diff] [blame] | 72 | return ret; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 75 | static int bpf_test_finish(const union bpf_attr *kattr, |
| 76 | union bpf_attr __user *uattr, const void *data, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 77 | u32 size, u32 retval, u32 duration) |
| 78 | { |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 79 | void __user *data_out = u64_to_user_ptr(kattr->test.data_out); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 80 | int err = -EFAULT; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 81 | u32 copy_size = size; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 82 | |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 83 | /* Clamp copy if the user has provided a size hint, but copy the full |
| 84 | * buffer if not to retain old behaviour. |
| 85 | */ |
| 86 | if (kattr->test.data_size_out && |
| 87 | copy_size > kattr->test.data_size_out) { |
| 88 | copy_size = kattr->test.data_size_out; |
| 89 | err = -ENOSPC; |
| 90 | } |
| 91 | |
| 92 | if (data_out && copy_to_user(data_out, data, copy_size)) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 93 | goto out; |
| 94 | if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) |
| 95 | goto out; |
| 96 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) |
| 97 | goto out; |
| 98 | if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) |
| 99 | goto out; |
Lorenz Bauer | b5a36b1 | 2018-12-03 11:31:23 +0000 | [diff] [blame] | 100 | if (err != -ENOSPC) |
| 101 | err = 0; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 102 | out: |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | static void *bpf_test_init(const union bpf_attr *kattr, u32 size, |
| 107 | u32 headroom, u32 tailroom) |
| 108 | { |
| 109 | void __user *data_in = u64_to_user_ptr(kattr->test.data_in); |
| 110 | void *data; |
| 111 | |
| 112 | if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) |
| 113 | return ERR_PTR(-EINVAL); |
| 114 | |
| 115 | data = kzalloc(size + headroom + tailroom, GFP_USER); |
| 116 | if (!data) |
| 117 | return ERR_PTR(-ENOMEM); |
| 118 | |
| 119 | if (copy_from_user(data + headroom, data_in, size)) { |
| 120 | kfree(data); |
| 121 | return ERR_PTR(-EFAULT); |
| 122 | } |
| 123 | return data; |
| 124 | } |
| 125 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 126 | static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) |
| 127 | { |
| 128 | void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); |
| 129 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 130 | u32 size = kattr->test.ctx_size_in; |
| 131 | void *data; |
| 132 | int err; |
| 133 | |
| 134 | if (!data_in && !data_out) |
| 135 | return NULL; |
| 136 | |
| 137 | data = kzalloc(max_size, GFP_USER); |
| 138 | if (!data) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | |
| 141 | if (data_in) { |
| 142 | err = bpf_check_uarg_tail_zero(data_in, max_size, size); |
| 143 | if (err) { |
| 144 | kfree(data); |
| 145 | return ERR_PTR(err); |
| 146 | } |
| 147 | |
| 148 | size = min_t(u32, max_size, size); |
| 149 | if (copy_from_user(data, data_in, size)) { |
| 150 | kfree(data); |
| 151 | return ERR_PTR(-EFAULT); |
| 152 | } |
| 153 | } |
| 154 | return data; |
| 155 | } |
| 156 | |
| 157 | static int bpf_ctx_finish(const union bpf_attr *kattr, |
| 158 | union bpf_attr __user *uattr, const void *data, |
| 159 | u32 size) |
| 160 | { |
| 161 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); |
| 162 | int err = -EFAULT; |
| 163 | u32 copy_size = size; |
| 164 | |
| 165 | if (!data || !data_out) |
| 166 | return 0; |
| 167 | |
| 168 | if (copy_size > kattr->test.ctx_size_out) { |
| 169 | copy_size = kattr->test.ctx_size_out; |
| 170 | err = -ENOSPC; |
| 171 | } |
| 172 | |
| 173 | if (copy_to_user(data_out, data, copy_size)) |
| 174 | goto out; |
| 175 | if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) |
| 176 | goto out; |
| 177 | if (err != -ENOSPC) |
| 178 | err = 0; |
| 179 | out: |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * range_is_zero - test whether buffer is initialized |
| 185 | * @buf: buffer to check |
| 186 | * @from: check from this position |
| 187 | * @to: check up until (excluding) this position |
| 188 | * |
| 189 | * This function returns true if the there is a non-zero byte |
| 190 | * in the buf in the range [from,to). |
| 191 | */ |
| 192 | static inline bool range_is_zero(void *buf, size_t from, size_t to) |
| 193 | { |
| 194 | return !memchr_inv((u8 *)buf + from, 0, to - from); |
| 195 | } |
| 196 | |
| 197 | static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 198 | { |
| 199 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 200 | |
| 201 | if (!__skb) |
| 202 | return 0; |
| 203 | |
| 204 | /* make sure the fields we don't use are zeroed */ |
| 205 | if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority))) |
| 206 | return -EINVAL; |
| 207 | |
| 208 | /* priority is allowed */ |
| 209 | |
| 210 | if (!range_is_zero(__skb, offsetof(struct __sk_buff, priority) + |
| 211 | FIELD_SIZEOF(struct __sk_buff, priority), |
| 212 | offsetof(struct __sk_buff, cb))) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | /* cb is allowed */ |
| 216 | |
| 217 | if (!range_is_zero(__skb, offsetof(struct __sk_buff, cb) + |
| 218 | FIELD_SIZEOF(struct __sk_buff, cb), |
| 219 | sizeof(struct __sk_buff))) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | skb->priority = __skb->priority; |
| 223 | memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) |
| 229 | { |
| 230 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; |
| 231 | |
| 232 | if (!__skb) |
| 233 | return; |
| 234 | |
| 235 | __skb->priority = skb->priority; |
| 236 | memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); |
| 237 | } |
| 238 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 239 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 240 | union bpf_attr __user *uattr) |
| 241 | { |
| 242 | bool is_l2 = false, is_direct_pkt_access = false; |
| 243 | u32 size = kattr->test.data_size_in; |
| 244 | u32 repeat = kattr->test.repeat; |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 245 | struct __sk_buff *ctx = NULL; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 246 | u32 retval, duration; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 247 | int hh_len = ETH_HLEN; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 248 | struct sk_buff *skb; |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 249 | struct sock *sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 250 | void *data; |
| 251 | int ret; |
| 252 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 253 | data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 254 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); |
| 255 | if (IS_ERR(data)) |
| 256 | return PTR_ERR(data); |
| 257 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 258 | ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); |
| 259 | if (IS_ERR(ctx)) { |
| 260 | kfree(data); |
| 261 | return PTR_ERR(ctx); |
| 262 | } |
| 263 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 264 | switch (prog->type) { |
| 265 | case BPF_PROG_TYPE_SCHED_CLS: |
| 266 | case BPF_PROG_TYPE_SCHED_ACT: |
| 267 | is_l2 = true; |
| 268 | /* fall through */ |
| 269 | case BPF_PROG_TYPE_LWT_IN: |
| 270 | case BPF_PROG_TYPE_LWT_OUT: |
| 271 | case BPF_PROG_TYPE_LWT_XMIT: |
| 272 | is_direct_pkt_access = true; |
| 273 | break; |
| 274 | default: |
| 275 | break; |
| 276 | } |
| 277 | |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 278 | sk = kzalloc(sizeof(struct sock), GFP_USER); |
| 279 | if (!sk) { |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 280 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 281 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 282 | return -ENOMEM; |
| 283 | } |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 284 | sock_net_set(sk, current->nsproxy->net_ns); |
| 285 | sock_init_data(NULL, sk); |
| 286 | |
| 287 | skb = build_skb(data, 0); |
| 288 | if (!skb) { |
| 289 | kfree(data); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 290 | kfree(ctx); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 291 | kfree(sk); |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | skb->sk = sk; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 295 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 296 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 297 | __skb_put(skb, size); |
| 298 | skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev); |
| 299 | skb_reset_network_header(skb); |
| 300 | |
| 301 | if (is_l2) |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 302 | __skb_push(skb, hh_len); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 303 | if (is_direct_pkt_access) |
Daniel Borkmann | 6aaae2b | 2017-09-25 02:25:50 +0200 | [diff] [blame] | 304 | bpf_compute_data_pointers(skb); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 305 | ret = convert___skb_to_skb(skb, ctx); |
| 306 | if (ret) |
| 307 | goto out; |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 308 | ret = bpf_test_run(prog, skb, repeat, &retval, &duration); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 309 | if (ret) |
| 310 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 311 | if (!is_l2) { |
| 312 | if (skb_headroom(skb) < hh_len) { |
| 313 | int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); |
| 314 | |
| 315 | if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 316 | ret = -ENOMEM; |
| 317 | goto out; |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | memset(__skb_push(skb, hh_len), 0, hh_len); |
| 321 | } |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 322 | convert_skb_to___skb(skb, ctx); |
Daniel Borkmann | 6e6fddc | 2018-07-11 15:30:14 +0200 | [diff] [blame] | 323 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 324 | size = skb->len; |
| 325 | /* bpf program can never convert linear skb to non-linear */ |
| 326 | if (WARN_ON_ONCE(skb_is_nonlinear(skb))) |
| 327 | size = skb_headlen(skb); |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 328 | ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 329 | if (!ret) |
| 330 | ret = bpf_ctx_finish(kattr, uattr, ctx, |
| 331 | sizeof(struct __sk_buff)); |
| 332 | out: |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 333 | kfree_skb(skb); |
Song Liu | 2cb494a | 2018-10-19 09:57:58 -0700 | [diff] [blame] | 334 | kfree(sk); |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame^] | 335 | kfree(ctx); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
| 340 | union bpf_attr __user *uattr) |
| 341 | { |
| 342 | u32 size = kattr->test.data_size_in; |
| 343 | u32 repeat = kattr->test.repeat; |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 344 | struct netdev_rx_queue *rxqueue; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 345 | struct xdp_buff xdp = {}; |
| 346 | u32 retval, duration; |
| 347 | void *data; |
| 348 | int ret; |
| 349 | |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 350 | 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] | 351 | if (IS_ERR(data)) |
| 352 | return PTR_ERR(data); |
| 353 | |
| 354 | xdp.data_hard_start = data; |
David Miller | 586f852 | 2017-05-02 11:36:45 -0400 | [diff] [blame] | 355 | xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 356 | xdp.data_meta = xdp.data; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 357 | xdp.data_end = xdp.data + size; |
| 358 | |
Daniel Borkmann | 65073a6 | 2018-01-31 12:58:56 +0100 | [diff] [blame] | 359 | rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); |
| 360 | xdp.rxq = &rxqueue->xdp_rxq; |
| 361 | |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 362 | ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration); |
| 363 | if (ret) |
| 364 | goto out; |
Nikita V. Shirokov | 587b80c | 2018-04-17 21:42:21 -0700 | [diff] [blame] | 365 | if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN || |
| 366 | xdp.data_end != xdp.data + size) |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 367 | size = xdp.data_end - xdp.data; |
David Miller | 78e5227 | 2017-05-02 11:36:33 -0400 | [diff] [blame] | 368 | ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration); |
Roman Gushchin | dcb4059 | 2018-12-01 10:39:44 -0800 | [diff] [blame] | 369 | out: |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 370 | kfree(data); |
| 371 | return ret; |
| 372 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 373 | |
| 374 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 375 | const union bpf_attr *kattr, |
| 376 | union bpf_attr __user *uattr) |
| 377 | { |
| 378 | u32 size = kattr->test.data_size_in; |
| 379 | u32 repeat = kattr->test.repeat; |
| 380 | struct bpf_flow_keys flow_keys; |
| 381 | u64 time_start, time_spent = 0; |
| 382 | struct bpf_skb_data_end *cb; |
| 383 | u32 retval, duration; |
| 384 | struct sk_buff *skb; |
| 385 | struct sock *sk; |
| 386 | void *data; |
| 387 | int ret; |
| 388 | u32 i; |
| 389 | |
| 390 | if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, |
| 394 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); |
| 395 | if (IS_ERR(data)) |
| 396 | return PTR_ERR(data); |
| 397 | |
| 398 | sk = kzalloc(sizeof(*sk), GFP_USER); |
| 399 | if (!sk) { |
| 400 | kfree(data); |
| 401 | return -ENOMEM; |
| 402 | } |
| 403 | sock_net_set(sk, current->nsproxy->net_ns); |
| 404 | sock_init_data(NULL, sk); |
| 405 | |
| 406 | skb = build_skb(data, 0); |
| 407 | if (!skb) { |
| 408 | kfree(data); |
| 409 | kfree(sk); |
| 410 | return -ENOMEM; |
| 411 | } |
| 412 | skb->sk = sk; |
| 413 | |
| 414 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 415 | __skb_put(skb, size); |
| 416 | skb->protocol = eth_type_trans(skb, |
| 417 | current->nsproxy->net_ns->loopback_dev); |
| 418 | skb_reset_network_header(skb); |
| 419 | |
| 420 | cb = (struct bpf_skb_data_end *)skb->cb; |
| 421 | cb->qdisc_cb.flow_keys = &flow_keys; |
| 422 | |
| 423 | if (!repeat) |
| 424 | repeat = 1; |
| 425 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 426 | rcu_read_lock(); |
| 427 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 428 | time_start = ktime_get_ns(); |
| 429 | for (i = 0; i < repeat; i++) { |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 430 | retval = __skb_flow_bpf_dissect(prog, skb, |
| 431 | &flow_keys_dissector, |
| 432 | &flow_keys); |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 433 | |
| 434 | if (signal_pending(current)) { |
| 435 | preempt_enable(); |
| 436 | rcu_read_unlock(); |
| 437 | |
| 438 | ret = -EINTR; |
| 439 | goto out; |
| 440 | } |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 441 | |
| 442 | if (need_resched()) { |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 443 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 444 | preempt_enable(); |
| 445 | rcu_read_unlock(); |
| 446 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 447 | cond_resched(); |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 448 | |
| 449 | rcu_read_lock(); |
| 450 | preempt_disable(); |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 451 | time_start = ktime_get_ns(); |
| 452 | } |
| 453 | } |
| 454 | time_spent += ktime_get_ns() - time_start; |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 455 | preempt_enable(); |
| 456 | rcu_read_unlock(); |
| 457 | |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 458 | do_div(time_spent, repeat); |
| 459 | duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; |
| 460 | |
| 461 | ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), |
| 462 | retval, duration); |
| 463 | |
Stanislav Fomichev | a439184 | 2019-02-19 10:54:17 -0800 | [diff] [blame] | 464 | out: |
Stanislav Fomichev | b7a1848 | 2019-01-28 08:53:54 -0800 | [diff] [blame] | 465 | kfree_skb(skb); |
| 466 | kfree(sk); |
| 467 | return ret; |
| 468 | } |