test_run.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017 Facebook
  3. */
  4. #include <linux/bpf.h>
  5. #include <linux/btf.h>
  6. #include <linux/btf_ids.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/filter.h>
  12. #include <linux/rcupdate_trace.h>
  13. #include <linux/sched/signal.h>
  14. #include <net/bpf_sk_storage.h>
  15. #include <net/hotdata.h>
  16. #include <net/sock.h>
  17. #include <net/tcp.h>
  18. #include <net/net_namespace.h>
  19. #include <net/page_pool/helpers.h>
  20. #include <linux/error-injection.h>
  21. #include <linux/smp.h>
  22. #include <linux/sock_diag.h>
  23. #include <linux/netfilter.h>
  24. #include <net/netdev_rx_queue.h>
  25. #include <net/xdp.h>
  26. #include <net/netfilter/nf_bpf_link.h>
  27. #define CREATE_TRACE_POINTS
  28. #include <trace/events/bpf_test_run.h>
  29. struct bpf_test_timer {
  30. enum { NO_PREEMPT, NO_MIGRATE } mode;
  31. u32 i;
  32. u64 time_start, time_spent;
  33. };
  34. static void bpf_test_timer_enter(struct bpf_test_timer *t)
  35. __acquires(rcu)
  36. {
  37. rcu_read_lock();
  38. if (t->mode == NO_PREEMPT)
  39. preempt_disable();
  40. else
  41. migrate_disable();
  42. t->time_start = ktime_get_ns();
  43. }
  44. static void bpf_test_timer_leave(struct bpf_test_timer *t)
  45. __releases(rcu)
  46. {
  47. t->time_start = 0;
  48. if (t->mode == NO_PREEMPT)
  49. preempt_enable();
  50. else
  51. migrate_enable();
  52. rcu_read_unlock();
  53. }
  54. static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
  55. u32 repeat, int *err, u32 *duration)
  56. __must_hold(rcu)
  57. {
  58. t->i += iterations;
  59. if (t->i >= repeat) {
  60. /* We're done. */
  61. t->time_spent += ktime_get_ns() - t->time_start;
  62. do_div(t->time_spent, t->i);
  63. *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent;
  64. *err = 0;
  65. goto reset;
  66. }
  67. if (signal_pending(current)) {
  68. /* During iteration: we've been cancelled, abort. */
  69. *err = -EINTR;
  70. goto reset;
  71. }
  72. if (need_resched()) {
  73. /* During iteration: we need to reschedule between runs. */
  74. t->time_spent += ktime_get_ns() - t->time_start;
  75. bpf_test_timer_leave(t);
  76. cond_resched();
  77. bpf_test_timer_enter(t);
  78. }
  79. /* Do another round. */
  80. return true;
  81. reset:
  82. t->i = 0;
  83. return false;
  84. }
  85. /* We put this struct at the head of each page with a context and frame
  86. * initialised when the page is allocated, so we don't have to do this on each
  87. * repetition of the test run.
  88. */
  89. struct xdp_page_head {
  90. struct xdp_buff orig_ctx;
  91. struct xdp_buff ctx;
  92. union {
  93. /* ::data_hard_start starts here */
  94. DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
  95. DECLARE_FLEX_ARRAY(u8, data);
  96. };
  97. };
  98. struct xdp_test_data {
  99. struct xdp_buff *orig_ctx;
  100. struct xdp_rxq_info rxq;
  101. struct net_device *dev;
  102. struct page_pool *pp;
  103. struct xdp_frame **frames;
  104. struct sk_buff **skbs;
  105. struct xdp_mem_info mem;
  106. u32 batch_size;
  107. u32 frame_cnt;
  108. };
  109. /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
  110. * must be updated accordingly this gets changed, otherwise BPF selftests
  111. * will fail.
  112. */
  113. #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
  114. #define TEST_XDP_MAX_BATCH 256
  115. static void xdp_test_run_init_page(netmem_ref netmem, void *arg)
  116. {
  117. struct xdp_page_head *head =
  118. phys_to_virt(page_to_phys(netmem_to_page(netmem)));
  119. struct xdp_buff *new_ctx, *orig_ctx;
  120. u32 headroom = XDP_PACKET_HEADROOM;
  121. struct xdp_test_data *xdp = arg;
  122. size_t frm_len, meta_len;
  123. struct xdp_frame *frm;
  124. void *data;
  125. orig_ctx = xdp->orig_ctx;
  126. frm_len = orig_ctx->data_end - orig_ctx->data_meta;
  127. meta_len = orig_ctx->data - orig_ctx->data_meta;
  128. headroom -= meta_len;
  129. new_ctx = &head->ctx;
  130. frm = head->frame;
  131. data = head->data;
  132. memcpy(data + headroom, orig_ctx->data_meta, frm_len);
  133. xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
  134. xdp_prepare_buff(new_ctx, data, headroom, frm_len, true);
  135. new_ctx->data = new_ctx->data_meta + meta_len;
  136. xdp_update_frame_from_buff(new_ctx, frm);
  137. frm->mem = new_ctx->rxq->mem;
  138. memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx));
  139. }
  140. static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx)
  141. {
  142. struct page_pool *pp;
  143. int err = -ENOMEM;
  144. struct page_pool_params pp_params = {
  145. .order = 0,
  146. .flags = 0,
  147. .pool_size = xdp->batch_size,
  148. .nid = NUMA_NO_NODE,
  149. .init_callback = xdp_test_run_init_page,
  150. .init_arg = xdp,
  151. };
  152. xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
  153. if (!xdp->frames)
  154. return -ENOMEM;
  155. xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
  156. if (!xdp->skbs)
  157. goto err_skbs;
  158. pp = page_pool_create(&pp_params);
  159. if (IS_ERR(pp)) {
  160. err = PTR_ERR(pp);
  161. goto err_pp;
  162. }
  163. /* will copy 'mem.id' into pp->xdp_mem_id */
  164. err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp);
  165. if (err)
  166. goto err_mmodel;
  167. xdp->pp = pp;
  168. /* We create a 'fake' RXQ referencing the original dev, but with an
  169. * xdp_mem_info pointing to our page_pool
  170. */
  171. xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0);
  172. xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL;
  173. xdp->rxq.mem.id = pp->xdp_mem_id;
  174. xdp->dev = orig_ctx->rxq->dev;
  175. xdp->orig_ctx = orig_ctx;
  176. return 0;
  177. err_mmodel:
  178. page_pool_destroy(pp);
  179. err_pp:
  180. kvfree(xdp->skbs);
  181. err_skbs:
  182. kvfree(xdp->frames);
  183. return err;
  184. }
  185. static void xdp_test_run_teardown(struct xdp_test_data *xdp)
  186. {
  187. xdp_unreg_mem_model(&xdp->mem);
  188. page_pool_destroy(xdp->pp);
  189. kfree(xdp->frames);
  190. kfree(xdp->skbs);
  191. }
  192. static bool frame_was_changed(const struct xdp_page_head *head)
  193. {
  194. /* xdp_scrub_frame() zeroes the data pointer, flags is the last field,
  195. * i.e. has the highest chances to be overwritten. If those two are
  196. * untouched, it's most likely safe to skip the context reset.
  197. */
  198. return head->frame->data != head->orig_ctx.data ||
  199. head->frame->flags != head->orig_ctx.flags;
  200. }
  201. static bool ctx_was_changed(struct xdp_page_head *head)
  202. {
  203. return head->orig_ctx.data != head->ctx.data ||
  204. head->orig_ctx.data_meta != head->ctx.data_meta ||
  205. head->orig_ctx.data_end != head->ctx.data_end;
  206. }
  207. static void reset_ctx(struct xdp_page_head *head)
  208. {
  209. if (likely(!frame_was_changed(head) && !ctx_was_changed(head)))
  210. return;
  211. head->ctx.data = head->orig_ctx.data;
  212. head->ctx.data_meta = head->orig_ctx.data_meta;
  213. head->ctx.data_end = head->orig_ctx.data_end;
  214. xdp_update_frame_from_buff(&head->ctx, head->frame);
  215. head->frame->mem = head->orig_ctx.rxq->mem;
  216. }
  217. static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
  218. struct sk_buff **skbs,
  219. struct net_device *dev)
  220. {
  221. gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
  222. int i, n;
  223. LIST_HEAD(list);
  224. n = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, gfp, nframes,
  225. (void **)skbs);
  226. if (unlikely(n == 0)) {
  227. for (i = 0; i < nframes; i++)
  228. xdp_return_frame(frames[i]);
  229. return -ENOMEM;
  230. }
  231. for (i = 0; i < nframes; i++) {
  232. struct xdp_frame *xdpf = frames[i];
  233. struct sk_buff *skb = skbs[i];
  234. skb = __xdp_build_skb_from_frame(xdpf, skb, dev);
  235. if (!skb) {
  236. xdp_return_frame(xdpf);
  237. continue;
  238. }
  239. list_add_tail(&skb->list, &list);
  240. }
  241. netif_receive_skb_list(&list);
  242. return 0;
  243. }
  244. static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
  245. u32 repeat)
  246. {
  247. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  248. int err = 0, act, ret, i, nframes = 0, batch_sz;
  249. struct xdp_frame **frames = xdp->frames;
  250. struct bpf_redirect_info *ri;
  251. struct xdp_page_head *head;
  252. struct xdp_frame *frm;
  253. bool redirect = false;
  254. struct xdp_buff *ctx;
  255. struct page *page;
  256. batch_sz = min_t(u32, repeat, xdp->batch_size);
  257. local_bh_disable();
  258. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  259. ri = bpf_net_ctx_get_ri();
  260. xdp_set_return_frame_no_direct();
  261. for (i = 0; i < batch_sz; i++) {
  262. page = page_pool_dev_alloc_pages(xdp->pp);
  263. if (!page) {
  264. err = -ENOMEM;
  265. goto out;
  266. }
  267. head = phys_to_virt(page_to_phys(page));
  268. reset_ctx(head);
  269. ctx = &head->ctx;
  270. frm = head->frame;
  271. xdp->frame_cnt++;
  272. act = bpf_prog_run_xdp(prog, ctx);
  273. /* if program changed pkt bounds we need to update the xdp_frame */
  274. if (unlikely(ctx_was_changed(head))) {
  275. ret = xdp_update_frame_from_buff(ctx, frm);
  276. if (ret) {
  277. xdp_return_buff(ctx);
  278. continue;
  279. }
  280. }
  281. switch (act) {
  282. case XDP_TX:
  283. /* we can't do a real XDP_TX since we're not in the
  284. * driver, so turn it into a REDIRECT back to the same
  285. * index
  286. */
  287. ri->tgt_index = xdp->dev->ifindex;
  288. ri->map_id = INT_MAX;
  289. ri->map_type = BPF_MAP_TYPE_UNSPEC;
  290. fallthrough;
  291. case XDP_REDIRECT:
  292. redirect = true;
  293. ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog);
  294. if (ret)
  295. xdp_return_buff(ctx);
  296. break;
  297. case XDP_PASS:
  298. frames[nframes++] = frm;
  299. break;
  300. default:
  301. bpf_warn_invalid_xdp_action(NULL, prog, act);
  302. fallthrough;
  303. case XDP_DROP:
  304. xdp_return_buff(ctx);
  305. break;
  306. }
  307. }
  308. out:
  309. if (redirect)
  310. xdp_do_flush();
  311. if (nframes) {
  312. ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev);
  313. if (ret)
  314. err = ret;
  315. }
  316. xdp_clear_return_frame_no_direct();
  317. bpf_net_ctx_clear(bpf_net_ctx);
  318. local_bh_enable();
  319. return err;
  320. }
  321. static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
  322. u32 repeat, u32 batch_size, u32 *time)
  323. {
  324. struct xdp_test_data xdp = { .batch_size = batch_size };
  325. struct bpf_test_timer t = { .mode = NO_MIGRATE };
  326. int ret;
  327. if (!repeat)
  328. repeat = 1;
  329. ret = xdp_test_run_setup(&xdp, ctx);
  330. if (ret)
  331. return ret;
  332. bpf_test_timer_enter(&t);
  333. do {
  334. xdp.frame_cnt = 0;
  335. ret = xdp_test_run_batch(&xdp, prog, repeat - t.i);
  336. if (unlikely(ret < 0))
  337. break;
  338. } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time));
  339. bpf_test_timer_leave(&t);
  340. xdp_test_run_teardown(&xdp);
  341. return ret;
  342. }
  343. static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
  344. u32 *retval, u32 *time, bool xdp)
  345. {
  346. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  347. struct bpf_prog_array_item item = {.prog = prog};
  348. struct bpf_run_ctx *old_ctx;
  349. struct bpf_cg_run_ctx run_ctx;
  350. struct bpf_test_timer t = { NO_MIGRATE };
  351. enum bpf_cgroup_storage_type stype;
  352. int ret;
  353. for_each_cgroup_storage_type(stype) {
  354. item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
  355. if (IS_ERR(item.cgroup_storage[stype])) {
  356. item.cgroup_storage[stype] = NULL;
  357. for_each_cgroup_storage_type(stype)
  358. bpf_cgroup_storage_free(item.cgroup_storage[stype]);
  359. return -ENOMEM;
  360. }
  361. }
  362. if (!repeat)
  363. repeat = 1;
  364. bpf_test_timer_enter(&t);
  365. old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  366. do {
  367. run_ctx.prog_item = &item;
  368. local_bh_disable();
  369. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  370. if (xdp)
  371. *retval = bpf_prog_run_xdp(prog, ctx);
  372. else
  373. *retval = bpf_prog_run(prog, ctx);
  374. bpf_net_ctx_clear(bpf_net_ctx);
  375. local_bh_enable();
  376. } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time));
  377. bpf_reset_run_ctx(old_ctx);
  378. bpf_test_timer_leave(&t);
  379. for_each_cgroup_storage_type(stype)
  380. bpf_cgroup_storage_free(item.cgroup_storage[stype]);
  381. return ret;
  382. }
  383. static int bpf_test_finish(const union bpf_attr *kattr,
  384. union bpf_attr __user *uattr, const void *data,
  385. struct skb_shared_info *sinfo, u32 size,
  386. u32 retval, u32 duration)
  387. {
  388. void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
  389. int err = -EFAULT;
  390. u32 copy_size = size;
  391. /* Clamp copy if the user has provided a size hint, but copy the full
  392. * buffer if not to retain old behaviour.
  393. */
  394. if (kattr->test.data_size_out &&
  395. copy_size > kattr->test.data_size_out) {
  396. copy_size = kattr->test.data_size_out;
  397. err = -ENOSPC;
  398. }
  399. if (data_out) {
  400. int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
  401. if (len < 0) {
  402. err = -ENOSPC;
  403. goto out;
  404. }
  405. if (copy_to_user(data_out, data, len))
  406. goto out;
  407. if (sinfo) {
  408. int i, offset = len;
  409. u32 data_len;
  410. for (i = 0; i < sinfo->nr_frags; i++) {
  411. skb_frag_t *frag = &sinfo->frags[i];
  412. if (offset >= copy_size) {
  413. err = -ENOSPC;
  414. break;
  415. }
  416. data_len = min_t(u32, copy_size - offset,
  417. skb_frag_size(frag));
  418. if (copy_to_user(data_out + offset,
  419. skb_frag_address(frag),
  420. data_len))
  421. goto out;
  422. offset += data_len;
  423. }
  424. }
  425. }
  426. if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
  427. goto out;
  428. if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
  429. goto out;
  430. if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
  431. goto out;
  432. if (err != -ENOSPC)
  433. err = 0;
  434. out:
  435. trace_bpf_test_finish(&err);
  436. return err;
  437. }
  438. /* Integer types of various sizes and pointer combinations cover variety of
  439. * architecture dependent calling conventions. 7+ can be supported in the
  440. * future.
  441. */
  442. __bpf_kfunc_start_defs();
  443. __bpf_kfunc int bpf_fentry_test1(int a)
  444. {
  445. return a + 1;
  446. }
  447. EXPORT_SYMBOL_GPL(bpf_fentry_test1);
  448. int noinline bpf_fentry_test2(int a, u64 b)
  449. {
  450. return a + b;
  451. }
  452. int noinline bpf_fentry_test3(char a, int b, u64 c)
  453. {
  454. return a + b + c;
  455. }
  456. int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
  457. {
  458. return (long)a + b + c + d;
  459. }
  460. int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
  461. {
  462. return a + (long)b + c + d + e;
  463. }
  464. int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
  465. {
  466. return a + (long)b + c + d + (long)e + f;
  467. }
  468. struct bpf_fentry_test_t {
  469. struct bpf_fentry_test_t *a;
  470. };
  471. int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
  472. {
  473. asm volatile ("": "+r"(arg));
  474. return (long)arg;
  475. }
  476. int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
  477. {
  478. return (long)arg->a;
  479. }
  480. __bpf_kfunc u32 bpf_fentry_test9(u32 *a)
  481. {
  482. return *a;
  483. }
  484. void noinline bpf_fentry_test_sinfo(struct skb_shared_info *sinfo)
  485. {
  486. }
  487. __bpf_kfunc int bpf_modify_return_test(int a, int *b)
  488. {
  489. *b += 1;
  490. return a + *b;
  491. }
  492. __bpf_kfunc int bpf_modify_return_test2(int a, int *b, short c, int d,
  493. void *e, char f, int g)
  494. {
  495. *b += 1;
  496. return a + *b + c + d + (long)e + f + g;
  497. }
  498. __bpf_kfunc int bpf_modify_return_test_tp(int nonce)
  499. {
  500. trace_bpf_trigger_tp(nonce);
  501. return nonce;
  502. }
  503. int noinline bpf_fentry_shadow_test(int a)
  504. {
  505. return a + 1;
  506. }
  507. struct prog_test_member1 {
  508. int a;
  509. };
  510. struct prog_test_member {
  511. struct prog_test_member1 m;
  512. int c;
  513. };
  514. struct prog_test_ref_kfunc {
  515. int a;
  516. int b;
  517. struct prog_test_member memb;
  518. struct prog_test_ref_kfunc *next;
  519. refcount_t cnt;
  520. };
  521. __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
  522. {
  523. refcount_dec(&p->cnt);
  524. }
  525. __bpf_kfunc void bpf_kfunc_call_test_release_dtor(void *p)
  526. {
  527. bpf_kfunc_call_test_release(p);
  528. }
  529. CFI_NOSEAL(bpf_kfunc_call_test_release_dtor);
  530. __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
  531. {
  532. }
  533. __bpf_kfunc void bpf_kfunc_call_memb_release_dtor(void *p)
  534. {
  535. }
  536. CFI_NOSEAL(bpf_kfunc_call_memb_release_dtor);
  537. __bpf_kfunc_end_defs();
  538. BTF_KFUNCS_START(bpf_test_modify_return_ids)
  539. BTF_ID_FLAGS(func, bpf_modify_return_test)
  540. BTF_ID_FLAGS(func, bpf_modify_return_test2)
  541. BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
  542. BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
  543. BTF_KFUNCS_END(bpf_test_modify_return_ids)
  544. static const struct btf_kfunc_id_set bpf_test_modify_return_set = {
  545. .owner = THIS_MODULE,
  546. .set = &bpf_test_modify_return_ids,
  547. };
  548. BTF_KFUNCS_START(test_sk_check_kfunc_ids)
  549. BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
  550. BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
  551. BTF_KFUNCS_END(test_sk_check_kfunc_ids)
  552. static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
  553. u32 size, u32 headroom, u32 tailroom)
  554. {
  555. void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
  556. void *data;
  557. if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
  558. return ERR_PTR(-EINVAL);
  559. if (user_size > size)
  560. return ERR_PTR(-EMSGSIZE);
  561. size = SKB_DATA_ALIGN(size);
  562. data = kzalloc(size + headroom + tailroom, GFP_USER);
  563. if (!data)
  564. return ERR_PTR(-ENOMEM);
  565. if (copy_from_user(data + headroom, data_in, user_size)) {
  566. kfree(data);
  567. return ERR_PTR(-EFAULT);
  568. }
  569. return data;
  570. }
  571. int bpf_prog_test_run_tracing(struct bpf_prog *prog,
  572. const union bpf_attr *kattr,
  573. union bpf_attr __user *uattr)
  574. {
  575. struct bpf_fentry_test_t arg = {};
  576. u16 side_effect = 0, ret = 0;
  577. int b = 2, err = -EFAULT;
  578. u32 retval = 0;
  579. if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
  580. return -EINVAL;
  581. switch (prog->expected_attach_type) {
  582. case BPF_TRACE_FENTRY:
  583. case BPF_TRACE_FEXIT:
  584. if (bpf_fentry_test1(1) != 2 ||
  585. bpf_fentry_test2(2, 3) != 5 ||
  586. bpf_fentry_test3(4, 5, 6) != 15 ||
  587. bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
  588. bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
  589. bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
  590. bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
  591. bpf_fentry_test8(&arg) != 0 ||
  592. bpf_fentry_test9(&retval) != 0)
  593. goto out;
  594. break;
  595. case BPF_MODIFY_RETURN:
  596. ret = bpf_modify_return_test(1, &b);
  597. if (b != 2)
  598. side_effect++;
  599. b = 2;
  600. ret += bpf_modify_return_test2(1, &b, 3, 4, (void *)5, 6, 7);
  601. if (b != 2)
  602. side_effect++;
  603. break;
  604. default:
  605. goto out;
  606. }
  607. retval = ((u32)side_effect << 16) | ret;
  608. if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
  609. goto out;
  610. err = 0;
  611. out:
  612. trace_bpf_test_finish(&err);
  613. return err;
  614. }
  615. struct bpf_raw_tp_test_run_info {
  616. struct bpf_prog *prog;
  617. void *ctx;
  618. u32 retval;
  619. };
  620. static void
  621. __bpf_prog_test_run_raw_tp(void *data)
  622. {
  623. struct bpf_raw_tp_test_run_info *info = data;
  624. struct bpf_trace_run_ctx run_ctx = {};
  625. struct bpf_run_ctx *old_run_ctx;
  626. old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  627. rcu_read_lock();
  628. info->retval = bpf_prog_run(info->prog, info->ctx);
  629. rcu_read_unlock();
  630. bpf_reset_run_ctx(old_run_ctx);
  631. }
  632. int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
  633. const union bpf_attr *kattr,
  634. union bpf_attr __user *uattr)
  635. {
  636. void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
  637. __u32 ctx_size_in = kattr->test.ctx_size_in;
  638. struct bpf_raw_tp_test_run_info info;
  639. int cpu = kattr->test.cpu, err = 0;
  640. int current_cpu;
  641. /* doesn't support data_in/out, ctx_out, duration, or repeat */
  642. if (kattr->test.data_in || kattr->test.data_out ||
  643. kattr->test.ctx_out || kattr->test.duration ||
  644. kattr->test.repeat || kattr->test.batch_size)
  645. return -EINVAL;
  646. if (ctx_size_in < prog->aux->max_ctx_offset ||
  647. ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
  648. return -EINVAL;
  649. if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
  650. return -EINVAL;
  651. if (ctx_size_in) {
  652. info.ctx = memdup_user(ctx_in, ctx_size_in);
  653. if (IS_ERR(info.ctx))
  654. return PTR_ERR(info.ctx);
  655. } else {
  656. info.ctx = NULL;
  657. }
  658. info.prog = prog;
  659. current_cpu = get_cpu();
  660. if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
  661. cpu == current_cpu) {
  662. __bpf_prog_test_run_raw_tp(&info);
  663. } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  664. /* smp_call_function_single() also checks cpu_online()
  665. * after csd_lock(). However, since cpu is from user
  666. * space, let's do an extra quick check to filter out
  667. * invalid value before smp_call_function_single().
  668. */
  669. err = -ENXIO;
  670. } else {
  671. err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
  672. &info, 1);
  673. }
  674. put_cpu();
  675. if (!err &&
  676. copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
  677. err = -EFAULT;
  678. kfree(info.ctx);
  679. return err;
  680. }
  681. static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
  682. {
  683. void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
  684. void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
  685. u32 size = kattr->test.ctx_size_in;
  686. void *data;
  687. int err;
  688. if (!data_in && !data_out)
  689. return NULL;
  690. data = kzalloc(max_size, GFP_USER);
  691. if (!data)
  692. return ERR_PTR(-ENOMEM);
  693. if (data_in) {
  694. err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
  695. if (err) {
  696. kfree(data);
  697. return ERR_PTR(err);
  698. }
  699. size = min_t(u32, max_size, size);
  700. if (copy_from_user(data, data_in, size)) {
  701. kfree(data);
  702. return ERR_PTR(-EFAULT);
  703. }
  704. }
  705. return data;
  706. }
  707. static int bpf_ctx_finish(const union bpf_attr *kattr,
  708. union bpf_attr __user *uattr, const void *data,
  709. u32 size)
  710. {
  711. void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
  712. int err = -EFAULT;
  713. u32 copy_size = size;
  714. if (!data || !data_out)
  715. return 0;
  716. if (copy_size > kattr->test.ctx_size_out) {
  717. copy_size = kattr->test.ctx_size_out;
  718. err = -ENOSPC;
  719. }
  720. if (copy_to_user(data_out, data, copy_size))
  721. goto out;
  722. if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
  723. goto out;
  724. if (err != -ENOSPC)
  725. err = 0;
  726. out:
  727. return err;
  728. }
  729. /**
  730. * range_is_zero - test whether buffer is initialized
  731. * @buf: buffer to check
  732. * @from: check from this position
  733. * @to: check up until (excluding) this position
  734. *
  735. * This function returns true if the there is a non-zero byte
  736. * in the buf in the range [from,to).
  737. */
  738. static inline bool range_is_zero(void *buf, size_t from, size_t to)
  739. {
  740. return !memchr_inv((u8 *)buf + from, 0, to - from);
  741. }
  742. static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
  743. {
  744. struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
  745. if (!__skb)
  746. return 0;
  747. /* make sure the fields we don't use are zeroed */
  748. if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
  749. return -EINVAL;
  750. /* mark is allowed */
  751. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
  752. offsetof(struct __sk_buff, priority)))
  753. return -EINVAL;
  754. /* priority is allowed */
  755. /* ingress_ifindex is allowed */
  756. /* ifindex is allowed */
  757. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
  758. offsetof(struct __sk_buff, cb)))
  759. return -EINVAL;
  760. /* cb is allowed */
  761. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
  762. offsetof(struct __sk_buff, tstamp)))
  763. return -EINVAL;
  764. /* tstamp is allowed */
  765. /* wire_len is allowed */
  766. /* gso_segs is allowed */
  767. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
  768. offsetof(struct __sk_buff, gso_size)))
  769. return -EINVAL;
  770. /* gso_size is allowed */
  771. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
  772. offsetof(struct __sk_buff, hwtstamp)))
  773. return -EINVAL;
  774. /* hwtstamp is allowed */
  775. if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
  776. sizeof(struct __sk_buff)))
  777. return -EINVAL;
  778. skb->mark = __skb->mark;
  779. skb->priority = __skb->priority;
  780. skb->skb_iif = __skb->ingress_ifindex;
  781. skb->tstamp = __skb->tstamp;
  782. memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
  783. if (__skb->wire_len == 0) {
  784. cb->pkt_len = skb->len;
  785. } else {
  786. if (__skb->wire_len < skb->len ||
  787. __skb->wire_len > GSO_LEGACY_MAX_SIZE)
  788. return -EINVAL;
  789. cb->pkt_len = __skb->wire_len;
  790. }
  791. if (__skb->gso_segs > GSO_MAX_SEGS)
  792. return -EINVAL;
  793. skb_shinfo(skb)->gso_segs = __skb->gso_segs;
  794. skb_shinfo(skb)->gso_size = __skb->gso_size;
  795. skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
  796. return 0;
  797. }
  798. static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
  799. {
  800. struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
  801. if (!__skb)
  802. return;
  803. __skb->mark = skb->mark;
  804. __skb->priority = skb->priority;
  805. __skb->ingress_ifindex = skb->skb_iif;
  806. __skb->ifindex = skb->dev->ifindex;
  807. __skb->tstamp = skb->tstamp;
  808. memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
  809. __skb->wire_len = cb->pkt_len;
  810. __skb->gso_segs = skb_shinfo(skb)->gso_segs;
  811. __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
  812. }
  813. static struct proto bpf_dummy_proto = {
  814. .name = "bpf_dummy",
  815. .owner = THIS_MODULE,
  816. .obj_size = sizeof(struct sock),
  817. };
  818. int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
  819. union bpf_attr __user *uattr)
  820. {
  821. bool is_l2 = false, is_direct_pkt_access = false;
  822. struct net *net = current->nsproxy->net_ns;
  823. struct net_device *dev = net->loopback_dev;
  824. u32 size = kattr->test.data_size_in;
  825. u32 repeat = kattr->test.repeat;
  826. struct __sk_buff *ctx = NULL;
  827. u32 retval, duration;
  828. int hh_len = ETH_HLEN;
  829. struct sk_buff *skb;
  830. struct sock *sk;
  831. void *data;
  832. int ret;
  833. if ((kattr->test.flags & ~BPF_F_TEST_SKB_CHECKSUM_COMPLETE) ||
  834. kattr->test.cpu || kattr->test.batch_size)
  835. return -EINVAL;
  836. data = bpf_test_init(kattr, kattr->test.data_size_in,
  837. size, NET_SKB_PAD + NET_IP_ALIGN,
  838. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  839. if (IS_ERR(data))
  840. return PTR_ERR(data);
  841. ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
  842. if (IS_ERR(ctx)) {
  843. kfree(data);
  844. return PTR_ERR(ctx);
  845. }
  846. switch (prog->type) {
  847. case BPF_PROG_TYPE_SCHED_CLS:
  848. case BPF_PROG_TYPE_SCHED_ACT:
  849. is_l2 = true;
  850. fallthrough;
  851. case BPF_PROG_TYPE_LWT_IN:
  852. case BPF_PROG_TYPE_LWT_OUT:
  853. case BPF_PROG_TYPE_LWT_XMIT:
  854. is_direct_pkt_access = true;
  855. break;
  856. default:
  857. break;
  858. }
  859. sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
  860. if (!sk) {
  861. kfree(data);
  862. kfree(ctx);
  863. return -ENOMEM;
  864. }
  865. sock_init_data(NULL, sk);
  866. skb = slab_build_skb(data);
  867. if (!skb) {
  868. kfree(data);
  869. kfree(ctx);
  870. sk_free(sk);
  871. return -ENOMEM;
  872. }
  873. skb->sk = sk;
  874. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  875. __skb_put(skb, size);
  876. if (ctx && ctx->ifindex > 1) {
  877. dev = dev_get_by_index(net, ctx->ifindex);
  878. if (!dev) {
  879. ret = -ENODEV;
  880. goto out;
  881. }
  882. }
  883. skb->protocol = eth_type_trans(skb, dev);
  884. skb_reset_network_header(skb);
  885. switch (skb->protocol) {
  886. case htons(ETH_P_IP):
  887. sk->sk_family = AF_INET;
  888. if (sizeof(struct iphdr) <= skb_headlen(skb)) {
  889. sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
  890. sk->sk_daddr = ip_hdr(skb)->daddr;
  891. }
  892. break;
  893. #if IS_ENABLED(CONFIG_IPV6)
  894. case htons(ETH_P_IPV6):
  895. sk->sk_family = AF_INET6;
  896. if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
  897. sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
  898. sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
  899. }
  900. break;
  901. #endif
  902. default:
  903. break;
  904. }
  905. if (is_l2)
  906. __skb_push(skb, hh_len);
  907. if (is_direct_pkt_access)
  908. bpf_compute_data_pointers(skb);
  909. ret = convert___skb_to_skb(skb, ctx);
  910. if (ret)
  911. goto out;
  912. if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
  913. const int off = skb_network_offset(skb);
  914. int len = skb->len - off;
  915. skb->csum = skb_checksum(skb, off, len, 0);
  916. skb->ip_summed = CHECKSUM_COMPLETE;
  917. }
  918. ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
  919. if (ret)
  920. goto out;
  921. if (!is_l2) {
  922. if (skb_headroom(skb) < hh_len) {
  923. int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
  924. if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
  925. ret = -ENOMEM;
  926. goto out;
  927. }
  928. }
  929. memset(__skb_push(skb, hh_len), 0, hh_len);
  930. }
  931. if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
  932. const int off = skb_network_offset(skb);
  933. int len = skb->len - off;
  934. __wsum csum;
  935. csum = skb_checksum(skb, off, len, 0);
  936. if (csum_fold(skb->csum) != csum_fold(csum)) {
  937. ret = -EBADMSG;
  938. goto out;
  939. }
  940. }
  941. convert_skb_to___skb(skb, ctx);
  942. size = skb->len;
  943. /* bpf program can never convert linear skb to non-linear */
  944. if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
  945. size = skb_headlen(skb);
  946. ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
  947. duration);
  948. if (!ret)
  949. ret = bpf_ctx_finish(kattr, uattr, ctx,
  950. sizeof(struct __sk_buff));
  951. out:
  952. if (dev && dev != net->loopback_dev)
  953. dev_put(dev);
  954. kfree_skb(skb);
  955. sk_free(sk);
  956. kfree(ctx);
  957. return ret;
  958. }
  959. static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
  960. {
  961. unsigned int ingress_ifindex, rx_queue_index;
  962. struct netdev_rx_queue *rxqueue;
  963. struct net_device *device;
  964. if (!xdp_md)
  965. return 0;
  966. if (xdp_md->egress_ifindex != 0)
  967. return -EINVAL;
  968. ingress_ifindex = xdp_md->ingress_ifindex;
  969. rx_queue_index = xdp_md->rx_queue_index;
  970. if (!ingress_ifindex && rx_queue_index)
  971. return -EINVAL;
  972. if (ingress_ifindex) {
  973. device = dev_get_by_index(current->nsproxy->net_ns,
  974. ingress_ifindex);
  975. if (!device)
  976. return -ENODEV;
  977. if (rx_queue_index >= device->real_num_rx_queues)
  978. goto free_dev;
  979. rxqueue = __netif_get_rx_queue(device, rx_queue_index);
  980. if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
  981. goto free_dev;
  982. xdp->rxq = &rxqueue->xdp_rxq;
  983. /* The device is now tracked in the xdp->rxq for later
  984. * dev_put()
  985. */
  986. }
  987. xdp->data = xdp->data_meta + xdp_md->data;
  988. return 0;
  989. free_dev:
  990. dev_put(device);
  991. return -EINVAL;
  992. }
  993. static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
  994. {
  995. if (!xdp_md)
  996. return;
  997. xdp_md->data = xdp->data - xdp->data_meta;
  998. xdp_md->data_end = xdp->data_end - xdp->data_meta;
  999. if (xdp_md->ingress_ifindex)
  1000. dev_put(xdp->rxq->dev);
  1001. }
  1002. int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
  1003. union bpf_attr __user *uattr)
  1004. {
  1005. bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
  1006. u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1007. u32 batch_size = kattr->test.batch_size;
  1008. u32 retval = 0, duration, max_data_sz;
  1009. u32 size = kattr->test.data_size_in;
  1010. u32 headroom = XDP_PACKET_HEADROOM;
  1011. u32 repeat = kattr->test.repeat;
  1012. struct netdev_rx_queue *rxqueue;
  1013. struct skb_shared_info *sinfo;
  1014. struct xdp_buff xdp = {};
  1015. int i, ret = -EINVAL;
  1016. struct xdp_md *ctx;
  1017. void *data;
  1018. if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
  1019. prog->expected_attach_type == BPF_XDP_CPUMAP)
  1020. return -EINVAL;
  1021. if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES)
  1022. return -EINVAL;
  1023. if (bpf_prog_is_dev_bound(prog->aux))
  1024. return -EINVAL;
  1025. if (do_live) {
  1026. if (!batch_size)
  1027. batch_size = NAPI_POLL_WEIGHT;
  1028. else if (batch_size > TEST_XDP_MAX_BATCH)
  1029. return -E2BIG;
  1030. headroom += sizeof(struct xdp_page_head);
  1031. } else if (batch_size) {
  1032. return -EINVAL;
  1033. }
  1034. ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
  1035. if (IS_ERR(ctx))
  1036. return PTR_ERR(ctx);
  1037. if (ctx) {
  1038. /* There can't be user provided data before the meta data */
  1039. if (ctx->data_meta || ctx->data_end != size ||
  1040. ctx->data > ctx->data_end ||
  1041. unlikely(xdp_metalen_invalid(ctx->data)) ||
  1042. (do_live && (kattr->test.data_out || kattr->test.ctx_out)))
  1043. goto free_ctx;
  1044. /* Meta data is allocated from the headroom */
  1045. headroom -= ctx->data;
  1046. }
  1047. max_data_sz = 4096 - headroom - tailroom;
  1048. if (size > max_data_sz) {
  1049. /* disallow live data mode for jumbo frames */
  1050. if (do_live)
  1051. goto free_ctx;
  1052. size = max_data_sz;
  1053. }
  1054. data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
  1055. if (IS_ERR(data)) {
  1056. ret = PTR_ERR(data);
  1057. goto free_ctx;
  1058. }
  1059. rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
  1060. rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
  1061. xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
  1062. xdp_prepare_buff(&xdp, data, headroom, size, true);
  1063. sinfo = xdp_get_shared_info_from_buff(&xdp);
  1064. ret = xdp_convert_md_to_buff(ctx, &xdp);
  1065. if (ret)
  1066. goto free_data;
  1067. if (unlikely(kattr->test.data_size_in > size)) {
  1068. void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
  1069. while (size < kattr->test.data_size_in) {
  1070. struct page *page;
  1071. skb_frag_t *frag;
  1072. u32 data_len;
  1073. if (sinfo->nr_frags == MAX_SKB_FRAGS) {
  1074. ret = -ENOMEM;
  1075. goto out;
  1076. }
  1077. page = alloc_page(GFP_KERNEL);
  1078. if (!page) {
  1079. ret = -ENOMEM;
  1080. goto out;
  1081. }
  1082. frag = &sinfo->frags[sinfo->nr_frags++];
  1083. data_len = min_t(u32, kattr->test.data_size_in - size,
  1084. PAGE_SIZE);
  1085. skb_frag_fill_page_desc(frag, page, 0, data_len);
  1086. if (copy_from_user(page_address(page), data_in + size,
  1087. data_len)) {
  1088. ret = -EFAULT;
  1089. goto out;
  1090. }
  1091. sinfo->xdp_frags_size += data_len;
  1092. size += data_len;
  1093. }
  1094. xdp_buff_set_frags_flag(&xdp);
  1095. }
  1096. if (repeat > 1)
  1097. bpf_prog_change_xdp(NULL, prog);
  1098. if (do_live)
  1099. ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration);
  1100. else
  1101. ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
  1102. /* We convert the xdp_buff back to an xdp_md before checking the return
  1103. * code so the reference count of any held netdevice will be decremented
  1104. * even if the test run failed.
  1105. */
  1106. xdp_convert_buff_to_md(&xdp, ctx);
  1107. if (ret)
  1108. goto out;
  1109. size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
  1110. ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
  1111. retval, duration);
  1112. if (!ret)
  1113. ret = bpf_ctx_finish(kattr, uattr, ctx,
  1114. sizeof(struct xdp_md));
  1115. out:
  1116. if (repeat > 1)
  1117. bpf_prog_change_xdp(prog, NULL);
  1118. free_data:
  1119. for (i = 0; i < sinfo->nr_frags; i++)
  1120. __free_page(skb_frag_page(&sinfo->frags[i]));
  1121. kfree(data);
  1122. free_ctx:
  1123. kfree(ctx);
  1124. return ret;
  1125. }
  1126. static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
  1127. {
  1128. /* make sure the fields we don't use are zeroed */
  1129. if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
  1130. return -EINVAL;
  1131. /* flags is allowed */
  1132. if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
  1133. sizeof(struct bpf_flow_keys)))
  1134. return -EINVAL;
  1135. return 0;
  1136. }
  1137. int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
  1138. const union bpf_attr *kattr,
  1139. union bpf_attr __user *uattr)
  1140. {
  1141. struct bpf_test_timer t = { NO_PREEMPT };
  1142. u32 size = kattr->test.data_size_in;
  1143. struct bpf_flow_dissector ctx = {};
  1144. u32 repeat = kattr->test.repeat;
  1145. struct bpf_flow_keys *user_ctx;
  1146. struct bpf_flow_keys flow_keys;
  1147. const struct ethhdr *eth;
  1148. unsigned int flags = 0;
  1149. u32 retval, duration;
  1150. void *data;
  1151. int ret;
  1152. if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
  1153. return -EINVAL;
  1154. if (size < ETH_HLEN)
  1155. return -EINVAL;
  1156. data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0);
  1157. if (IS_ERR(data))
  1158. return PTR_ERR(data);
  1159. eth = (struct ethhdr *)data;
  1160. if (!repeat)
  1161. repeat = 1;
  1162. user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
  1163. if (IS_ERR(user_ctx)) {
  1164. kfree(data);
  1165. return PTR_ERR(user_ctx);
  1166. }
  1167. if (user_ctx) {
  1168. ret = verify_user_bpf_flow_keys(user_ctx);
  1169. if (ret)
  1170. goto out;
  1171. flags = user_ctx->flags;
  1172. }
  1173. ctx.flow_keys = &flow_keys;
  1174. ctx.data = data;
  1175. ctx.data_end = (__u8 *)data + size;
  1176. bpf_test_timer_enter(&t);
  1177. do {
  1178. retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
  1179. size, flags);
  1180. } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
  1181. bpf_test_timer_leave(&t);
  1182. if (ret < 0)
  1183. goto out;
  1184. ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
  1185. sizeof(flow_keys), retval, duration);
  1186. if (!ret)
  1187. ret = bpf_ctx_finish(kattr, uattr, user_ctx,
  1188. sizeof(struct bpf_flow_keys));
  1189. out:
  1190. kfree(user_ctx);
  1191. kfree(data);
  1192. return ret;
  1193. }
  1194. int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
  1195. union bpf_attr __user *uattr)
  1196. {
  1197. struct bpf_test_timer t = { NO_PREEMPT };
  1198. struct bpf_prog_array *progs = NULL;
  1199. struct bpf_sk_lookup_kern ctx = {};
  1200. u32 repeat = kattr->test.repeat;
  1201. struct bpf_sk_lookup *user_ctx;
  1202. u32 retval, duration;
  1203. int ret = -EINVAL;
  1204. if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
  1205. return -EINVAL;
  1206. if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
  1207. kattr->test.data_size_out)
  1208. return -EINVAL;
  1209. if (!repeat)
  1210. repeat = 1;
  1211. user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
  1212. if (IS_ERR(user_ctx))
  1213. return PTR_ERR(user_ctx);
  1214. if (!user_ctx)
  1215. return -EINVAL;
  1216. if (user_ctx->sk)
  1217. goto out;
  1218. if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
  1219. goto out;
  1220. if (user_ctx->local_port > U16_MAX) {
  1221. ret = -ERANGE;
  1222. goto out;
  1223. }
  1224. ctx.family = (u16)user_ctx->family;
  1225. ctx.protocol = (u16)user_ctx->protocol;
  1226. ctx.dport = (u16)user_ctx->local_port;
  1227. ctx.sport = user_ctx->remote_port;
  1228. switch (ctx.family) {
  1229. case AF_INET:
  1230. ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
  1231. ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
  1232. break;
  1233. #if IS_ENABLED(CONFIG_IPV6)
  1234. case AF_INET6:
  1235. ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
  1236. ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
  1237. break;
  1238. #endif
  1239. default:
  1240. ret = -EAFNOSUPPORT;
  1241. goto out;
  1242. }
  1243. progs = bpf_prog_array_alloc(1, GFP_KERNEL);
  1244. if (!progs) {
  1245. ret = -ENOMEM;
  1246. goto out;
  1247. }
  1248. progs->items[0].prog = prog;
  1249. bpf_test_timer_enter(&t);
  1250. do {
  1251. ctx.selected_sk = NULL;
  1252. retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
  1253. } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
  1254. bpf_test_timer_leave(&t);
  1255. if (ret < 0)
  1256. goto out;
  1257. user_ctx->cookie = 0;
  1258. if (ctx.selected_sk) {
  1259. if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
  1260. ret = -EOPNOTSUPP;
  1261. goto out;
  1262. }
  1263. user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
  1264. }
  1265. ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
  1266. if (!ret)
  1267. ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
  1268. out:
  1269. bpf_prog_array_free(progs);
  1270. kfree(user_ctx);
  1271. return ret;
  1272. }
  1273. int bpf_prog_test_run_syscall(struct bpf_prog *prog,
  1274. const union bpf_attr *kattr,
  1275. union bpf_attr __user *uattr)
  1276. {
  1277. void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
  1278. __u32 ctx_size_in = kattr->test.ctx_size_in;
  1279. void *ctx = NULL;
  1280. u32 retval;
  1281. int err = 0;
  1282. /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
  1283. if (kattr->test.data_in || kattr->test.data_out ||
  1284. kattr->test.ctx_out || kattr->test.duration ||
  1285. kattr->test.repeat || kattr->test.flags ||
  1286. kattr->test.batch_size)
  1287. return -EINVAL;
  1288. if (ctx_size_in < prog->aux->max_ctx_offset ||
  1289. ctx_size_in > U16_MAX)
  1290. return -EINVAL;
  1291. if (ctx_size_in) {
  1292. ctx = memdup_user(ctx_in, ctx_size_in);
  1293. if (IS_ERR(ctx))
  1294. return PTR_ERR(ctx);
  1295. }
  1296. rcu_read_lock_trace();
  1297. retval = bpf_prog_run_pin_on_cpu(prog, ctx);
  1298. rcu_read_unlock_trace();
  1299. if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
  1300. err = -EFAULT;
  1301. goto out;
  1302. }
  1303. if (ctx_size_in)
  1304. if (copy_to_user(ctx_in, ctx, ctx_size_in))
  1305. err = -EFAULT;
  1306. out:
  1307. kfree(ctx);
  1308. return err;
  1309. }
  1310. static int verify_and_copy_hook_state(struct nf_hook_state *state,
  1311. const struct nf_hook_state *user,
  1312. struct net_device *dev)
  1313. {
  1314. if (user->in || user->out)
  1315. return -EINVAL;
  1316. if (user->net || user->sk || user->okfn)
  1317. return -EINVAL;
  1318. switch (user->pf) {
  1319. case NFPROTO_IPV4:
  1320. case NFPROTO_IPV6:
  1321. switch (state->hook) {
  1322. case NF_INET_PRE_ROUTING:
  1323. state->in = dev;
  1324. break;
  1325. case NF_INET_LOCAL_IN:
  1326. state->in = dev;
  1327. break;
  1328. case NF_INET_FORWARD:
  1329. state->in = dev;
  1330. state->out = dev;
  1331. break;
  1332. case NF_INET_LOCAL_OUT:
  1333. state->out = dev;
  1334. break;
  1335. case NF_INET_POST_ROUTING:
  1336. state->out = dev;
  1337. break;
  1338. }
  1339. break;
  1340. default:
  1341. return -EINVAL;
  1342. }
  1343. state->pf = user->pf;
  1344. state->hook = user->hook;
  1345. return 0;
  1346. }
  1347. static __be16 nfproto_eth(int nfproto)
  1348. {
  1349. switch (nfproto) {
  1350. case NFPROTO_IPV4:
  1351. return htons(ETH_P_IP);
  1352. case NFPROTO_IPV6:
  1353. break;
  1354. }
  1355. return htons(ETH_P_IPV6);
  1356. }
  1357. int bpf_prog_test_run_nf(struct bpf_prog *prog,
  1358. const union bpf_attr *kattr,
  1359. union bpf_attr __user *uattr)
  1360. {
  1361. struct net *net = current->nsproxy->net_ns;
  1362. struct net_device *dev = net->loopback_dev;
  1363. struct nf_hook_state *user_ctx, hook_state = {
  1364. .pf = NFPROTO_IPV4,
  1365. .hook = NF_INET_LOCAL_OUT,
  1366. };
  1367. u32 size = kattr->test.data_size_in;
  1368. u32 repeat = kattr->test.repeat;
  1369. struct bpf_nf_ctx ctx = {
  1370. .state = &hook_state,
  1371. };
  1372. struct sk_buff *skb = NULL;
  1373. u32 retval, duration;
  1374. void *data;
  1375. int ret;
  1376. if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
  1377. return -EINVAL;
  1378. if (size < sizeof(struct iphdr))
  1379. return -EINVAL;
  1380. data = bpf_test_init(kattr, kattr->test.data_size_in, size,
  1381. NET_SKB_PAD + NET_IP_ALIGN,
  1382. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  1383. if (IS_ERR(data))
  1384. return PTR_ERR(data);
  1385. if (!repeat)
  1386. repeat = 1;
  1387. user_ctx = bpf_ctx_init(kattr, sizeof(struct nf_hook_state));
  1388. if (IS_ERR(user_ctx)) {
  1389. kfree(data);
  1390. return PTR_ERR(user_ctx);
  1391. }
  1392. if (user_ctx) {
  1393. ret = verify_and_copy_hook_state(&hook_state, user_ctx, dev);
  1394. if (ret)
  1395. goto out;
  1396. }
  1397. skb = slab_build_skb(data);
  1398. if (!skb) {
  1399. ret = -ENOMEM;
  1400. goto out;
  1401. }
  1402. data = NULL; /* data released via kfree_skb */
  1403. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  1404. __skb_put(skb, size);
  1405. ret = -EINVAL;
  1406. if (hook_state.hook != NF_INET_LOCAL_OUT) {
  1407. if (size < ETH_HLEN + sizeof(struct iphdr))
  1408. goto out;
  1409. skb->protocol = eth_type_trans(skb, dev);
  1410. switch (skb->protocol) {
  1411. case htons(ETH_P_IP):
  1412. if (hook_state.pf == NFPROTO_IPV4)
  1413. break;
  1414. goto out;
  1415. case htons(ETH_P_IPV6):
  1416. if (size < ETH_HLEN + sizeof(struct ipv6hdr))
  1417. goto out;
  1418. if (hook_state.pf == NFPROTO_IPV6)
  1419. break;
  1420. goto out;
  1421. default:
  1422. ret = -EPROTO;
  1423. goto out;
  1424. }
  1425. skb_reset_network_header(skb);
  1426. } else {
  1427. skb->protocol = nfproto_eth(hook_state.pf);
  1428. }
  1429. ctx.skb = skb;
  1430. ret = bpf_test_run(prog, &ctx, repeat, &retval, &duration, false);
  1431. if (ret)
  1432. goto out;
  1433. ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
  1434. out:
  1435. kfree(user_ctx);
  1436. kfree_skb(skb);
  1437. kfree(data);
  1438. return ret;
  1439. }
  1440. static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
  1441. .owner = THIS_MODULE,
  1442. .set = &test_sk_check_kfunc_ids,
  1443. };
  1444. BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
  1445. BTF_ID(struct, prog_test_ref_kfunc)
  1446. BTF_ID(func, bpf_kfunc_call_test_release_dtor)
  1447. BTF_ID(struct, prog_test_member)
  1448. BTF_ID(func, bpf_kfunc_call_memb_release_dtor)
  1449. static int __init bpf_prog_test_run_init(void)
  1450. {
  1451. const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
  1452. {
  1453. .btf_id = bpf_prog_test_dtor_kfunc_ids[0],
  1454. .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
  1455. },
  1456. {
  1457. .btf_id = bpf_prog_test_dtor_kfunc_ids[2],
  1458. .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
  1459. },
  1460. };
  1461. int ret;
  1462. ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set);
  1463. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
  1464. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set);
  1465. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set);
  1466. return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
  1467. ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
  1468. THIS_MODULE);
  1469. }
  1470. late_initcall(bpf_prog_test_run_init);