test_run.c 39 KB

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