test_run.c 41 KB

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