test_run.c 43 KB

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