test_run.c 18 KB

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