cpumap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* bpf/cpumap.c
  3. *
  4. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  5. */
  6. /**
  7. * DOC: cpu map
  8. * The 'cpumap' is primarily used as a backend map for XDP BPF helper
  9. * call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
  10. *
  11. * Unlike devmap which redirects XDP frames out to another NIC device,
  12. * this map type redirects raw XDP frames to another CPU. The remote
  13. * CPU will do SKB-allocation and call the normal network stack.
  14. */
  15. /*
  16. * This is a scalability and isolation mechanism, that allow
  17. * separating the early driver network XDP layer, from the rest of the
  18. * netstack, and assigning dedicated CPUs for this stage. This
  19. * basically allows for 10G wirespeed pre-filtering via bpf.
  20. */
  21. #include <linux/bitops.h>
  22. #include <linux/bpf.h>
  23. #include <linux/filter.h>
  24. #include <linux/ptr_ring.h>
  25. #include <net/xdp.h>
  26. #include <net/hotdata.h>
  27. #include <linux/sched.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/kthread.h>
  30. #include <linux/completion.h>
  31. #include <trace/events/xdp.h>
  32. #include <linux/btf_ids.h>
  33. #include <linux/netdevice.h> /* netif_receive_skb_list */
  34. #include <linux/etherdevice.h> /* eth_type_trans */
  35. /* General idea: XDP packets getting XDP redirected to another CPU,
  36. * will maximum be stored/queued for one driver ->poll() call. It is
  37. * guaranteed that queueing the frame and the flush operation happen on
  38. * same CPU. Thus, cpu_map_flush operation can deduct via this_cpu_ptr()
  39. * which queue in bpf_cpu_map_entry contains packets.
  40. */
  41. #define CPU_MAP_BULK_SIZE 8 /* 8 == one cacheline on 64-bit archs */
  42. struct bpf_cpu_map_entry;
  43. struct bpf_cpu_map;
  44. struct xdp_bulk_queue {
  45. void *q[CPU_MAP_BULK_SIZE];
  46. struct list_head flush_node;
  47. struct bpf_cpu_map_entry *obj;
  48. unsigned int count;
  49. };
  50. /* Struct for every remote "destination" CPU in map */
  51. struct bpf_cpu_map_entry {
  52. u32 cpu; /* kthread CPU and map index */
  53. int map_id; /* Back reference to map */
  54. /* XDP can run multiple RX-ring queues, need __percpu enqueue store */
  55. struct xdp_bulk_queue __percpu *bulkq;
  56. /* Queue with potential multi-producers, and single-consumer kthread */
  57. struct ptr_ring *queue;
  58. struct task_struct *kthread;
  59. struct bpf_cpumap_val value;
  60. struct bpf_prog *prog;
  61. struct completion kthread_running;
  62. struct rcu_work free_work;
  63. };
  64. struct bpf_cpu_map {
  65. struct bpf_map map;
  66. /* Below members specific for map type */
  67. struct bpf_cpu_map_entry __rcu **cpu_map;
  68. };
  69. static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
  70. {
  71. u32 value_size = attr->value_size;
  72. struct bpf_cpu_map *cmap;
  73. /* check sanity of attributes */
  74. if (attr->max_entries == 0 || attr->key_size != 4 ||
  75. (value_size != offsetofend(struct bpf_cpumap_val, qsize) &&
  76. value_size != offsetofend(struct bpf_cpumap_val, bpf_prog.fd)) ||
  77. attr->map_flags & ~BPF_F_NUMA_NODE)
  78. return ERR_PTR(-EINVAL);
  79. /* Pre-limit array size based on NR_CPUS, not final CPU check */
  80. if (attr->max_entries > NR_CPUS)
  81. return ERR_PTR(-E2BIG);
  82. cmap = bpf_map_area_alloc(sizeof(*cmap), NUMA_NO_NODE);
  83. if (!cmap)
  84. return ERR_PTR(-ENOMEM);
  85. bpf_map_init_from_attr(&cmap->map, attr);
  86. /* Alloc array for possible remote "destination" CPUs */
  87. cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
  88. sizeof(struct bpf_cpu_map_entry *),
  89. cmap->map.numa_node);
  90. if (!cmap->cpu_map) {
  91. bpf_map_area_free(cmap);
  92. return ERR_PTR(-ENOMEM);
  93. }
  94. return &cmap->map;
  95. }
  96. static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
  97. {
  98. /* The tear-down procedure should have made sure that queue is
  99. * empty. See __cpu_map_entry_replace() and work-queue
  100. * invoked cpu_map_kthread_stop(). Catch any broken behaviour
  101. * gracefully and warn once.
  102. */
  103. void *ptr;
  104. while ((ptr = ptr_ring_consume(ring))) {
  105. WARN_ON_ONCE(1);
  106. if (unlikely(__ptr_test_bit(0, &ptr))) {
  107. __ptr_clear_bit(0, &ptr);
  108. kfree_skb(ptr);
  109. continue;
  110. }
  111. xdp_return_frame(ptr);
  112. }
  113. }
  114. static void cpu_map_bpf_prog_run_skb(struct bpf_cpu_map_entry *rcpu,
  115. struct list_head *listp,
  116. struct xdp_cpumap_stats *stats)
  117. {
  118. struct sk_buff *skb, *tmp;
  119. struct xdp_buff xdp;
  120. u32 act;
  121. int err;
  122. list_for_each_entry_safe(skb, tmp, listp, list) {
  123. act = bpf_prog_run_generic_xdp(skb, &xdp, rcpu->prog);
  124. switch (act) {
  125. case XDP_PASS:
  126. break;
  127. case XDP_REDIRECT:
  128. skb_list_del_init(skb);
  129. err = xdp_do_generic_redirect(skb->dev, skb, &xdp,
  130. rcpu->prog);
  131. if (unlikely(err)) {
  132. kfree_skb(skb);
  133. stats->drop++;
  134. } else {
  135. stats->redirect++;
  136. }
  137. return;
  138. default:
  139. bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
  140. fallthrough;
  141. case XDP_ABORTED:
  142. trace_xdp_exception(skb->dev, rcpu->prog, act);
  143. fallthrough;
  144. case XDP_DROP:
  145. skb_list_del_init(skb);
  146. kfree_skb(skb);
  147. stats->drop++;
  148. return;
  149. }
  150. }
  151. }
  152. static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,
  153. void **frames, int n,
  154. struct xdp_cpumap_stats *stats)
  155. {
  156. struct xdp_rxq_info rxq = {};
  157. struct xdp_buff xdp;
  158. int i, nframes = 0;
  159. xdp_set_return_frame_no_direct();
  160. xdp.rxq = &rxq;
  161. for (i = 0; i < n; i++) {
  162. struct xdp_frame *xdpf = frames[i];
  163. u32 act;
  164. int err;
  165. rxq.dev = xdpf->dev_rx;
  166. rxq.mem = xdpf->mem;
  167. /* TODO: report queue_index to xdp_rxq_info */
  168. xdp_convert_frame_to_buff(xdpf, &xdp);
  169. act = bpf_prog_run_xdp(rcpu->prog, &xdp);
  170. switch (act) {
  171. case XDP_PASS:
  172. err = xdp_update_frame_from_buff(&xdp, xdpf);
  173. if (err < 0) {
  174. xdp_return_frame(xdpf);
  175. stats->drop++;
  176. } else {
  177. frames[nframes++] = xdpf;
  178. stats->pass++;
  179. }
  180. break;
  181. case XDP_REDIRECT:
  182. err = xdp_do_redirect(xdpf->dev_rx, &xdp,
  183. rcpu->prog);
  184. if (unlikely(err)) {
  185. xdp_return_frame(xdpf);
  186. stats->drop++;
  187. } else {
  188. stats->redirect++;
  189. }
  190. break;
  191. default:
  192. bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
  193. fallthrough;
  194. case XDP_DROP:
  195. xdp_return_frame(xdpf);
  196. stats->drop++;
  197. break;
  198. }
  199. }
  200. xdp_clear_return_frame_no_direct();
  201. return nframes;
  202. }
  203. #define CPUMAP_BATCH 8
  204. static int cpu_map_bpf_prog_run(struct bpf_cpu_map_entry *rcpu, void **frames,
  205. int xdp_n, struct xdp_cpumap_stats *stats,
  206. struct list_head *list)
  207. {
  208. struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
  209. int nframes;
  210. if (!rcpu->prog)
  211. return xdp_n;
  212. rcu_read_lock_bh();
  213. bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
  214. nframes = cpu_map_bpf_prog_run_xdp(rcpu, frames, xdp_n, stats);
  215. if (stats->redirect)
  216. xdp_do_flush();
  217. if (unlikely(!list_empty(list)))
  218. cpu_map_bpf_prog_run_skb(rcpu, list, stats);
  219. bpf_net_ctx_clear(bpf_net_ctx);
  220. rcu_read_unlock_bh(); /* resched point, may call do_softirq() */
  221. return nframes;
  222. }
  223. static int cpu_map_kthread_run(void *data)
  224. {
  225. struct bpf_cpu_map_entry *rcpu = data;
  226. unsigned long last_qs = jiffies;
  227. complete(&rcpu->kthread_running);
  228. set_current_state(TASK_INTERRUPTIBLE);
  229. /* When kthread gives stop order, then rcpu have been disconnected
  230. * from map, thus no new packets can enter. Remaining in-flight
  231. * per CPU stored packets are flushed to this queue. Wait honoring
  232. * kthread_stop signal until queue is empty.
  233. */
  234. while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
  235. struct xdp_cpumap_stats stats = {}; /* zero stats */
  236. unsigned int kmem_alloc_drops = 0, sched = 0;
  237. gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
  238. int i, n, m, nframes, xdp_n;
  239. void *frames[CPUMAP_BATCH];
  240. void *skbs[CPUMAP_BATCH];
  241. LIST_HEAD(list);
  242. /* Release CPU reschedule checks */
  243. if (__ptr_ring_empty(rcpu->queue)) {
  244. set_current_state(TASK_INTERRUPTIBLE);
  245. /* Recheck to avoid lost wake-up */
  246. if (__ptr_ring_empty(rcpu->queue)) {
  247. schedule();
  248. sched = 1;
  249. last_qs = jiffies;
  250. } else {
  251. __set_current_state(TASK_RUNNING);
  252. }
  253. } else {
  254. rcu_softirq_qs_periodic(last_qs);
  255. sched = cond_resched();
  256. }
  257. /*
  258. * The bpf_cpu_map_entry is single consumer, with this
  259. * kthread CPU pinned. Lockless access to ptr_ring
  260. * consume side valid as no-resize allowed of queue.
  261. */
  262. n = __ptr_ring_consume_batched(rcpu->queue, frames,
  263. CPUMAP_BATCH);
  264. for (i = 0, xdp_n = 0; i < n; i++) {
  265. void *f = frames[i];
  266. struct page *page;
  267. if (unlikely(__ptr_test_bit(0, &f))) {
  268. struct sk_buff *skb = f;
  269. __ptr_clear_bit(0, &skb);
  270. list_add_tail(&skb->list, &list);
  271. continue;
  272. }
  273. frames[xdp_n++] = f;
  274. page = virt_to_page(f);
  275. /* Bring struct page memory area to curr CPU. Read by
  276. * build_skb_around via page_is_pfmemalloc(), and when
  277. * freed written by page_frag_free call.
  278. */
  279. prefetchw(page);
  280. }
  281. /* Support running another XDP prog on this CPU */
  282. nframes = cpu_map_bpf_prog_run(rcpu, frames, xdp_n, &stats, &list);
  283. if (nframes) {
  284. m = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
  285. gfp, nframes, skbs);
  286. if (unlikely(m == 0)) {
  287. for (i = 0; i < nframes; i++)
  288. skbs[i] = NULL; /* effect: xdp_return_frame */
  289. kmem_alloc_drops += nframes;
  290. }
  291. }
  292. local_bh_disable();
  293. for (i = 0; i < nframes; i++) {
  294. struct xdp_frame *xdpf = frames[i];
  295. struct sk_buff *skb = skbs[i];
  296. skb = __xdp_build_skb_from_frame(xdpf, skb,
  297. xdpf->dev_rx);
  298. if (!skb) {
  299. xdp_return_frame(xdpf);
  300. continue;
  301. }
  302. list_add_tail(&skb->list, &list);
  303. }
  304. /* Feedback loop via tracepoint.
  305. * NB: keep before recv to allow measuring enqueue/dequeue latency.
  306. */
  307. trace_xdp_cpumap_kthread(rcpu->map_id, n, kmem_alloc_drops,
  308. sched, &stats);
  309. netif_receive_skb_list(&list);
  310. local_bh_enable(); /* resched point, may call do_softirq() */
  311. }
  312. __set_current_state(TASK_RUNNING);
  313. return 0;
  314. }
  315. static int __cpu_map_load_bpf_program(struct bpf_cpu_map_entry *rcpu,
  316. struct bpf_map *map, int fd)
  317. {
  318. struct bpf_prog *prog;
  319. prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
  320. if (IS_ERR(prog))
  321. return PTR_ERR(prog);
  322. if (prog->expected_attach_type != BPF_XDP_CPUMAP ||
  323. !bpf_prog_map_compatible(map, prog)) {
  324. bpf_prog_put(prog);
  325. return -EINVAL;
  326. }
  327. rcpu->value.bpf_prog.id = prog->aux->id;
  328. rcpu->prog = prog;
  329. return 0;
  330. }
  331. static struct bpf_cpu_map_entry *
  332. __cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
  333. u32 cpu)
  334. {
  335. int numa, err, i, fd = value->bpf_prog.fd;
  336. gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
  337. struct bpf_cpu_map_entry *rcpu;
  338. struct xdp_bulk_queue *bq;
  339. /* Have map->numa_node, but choose node of redirect target CPU */
  340. numa = cpu_to_node(cpu);
  341. rcpu = bpf_map_kmalloc_node(map, sizeof(*rcpu), gfp | __GFP_ZERO, numa);
  342. if (!rcpu)
  343. return NULL;
  344. /* Alloc percpu bulkq */
  345. rcpu->bulkq = bpf_map_alloc_percpu(map, sizeof(*rcpu->bulkq),
  346. sizeof(void *), gfp);
  347. if (!rcpu->bulkq)
  348. goto free_rcu;
  349. for_each_possible_cpu(i) {
  350. bq = per_cpu_ptr(rcpu->bulkq, i);
  351. bq->obj = rcpu;
  352. }
  353. /* Alloc queue */
  354. rcpu->queue = bpf_map_kmalloc_node(map, sizeof(*rcpu->queue), gfp,
  355. numa);
  356. if (!rcpu->queue)
  357. goto free_bulkq;
  358. err = ptr_ring_init(rcpu->queue, value->qsize, gfp);
  359. if (err)
  360. goto free_queue;
  361. rcpu->cpu = cpu;
  362. rcpu->map_id = map->id;
  363. rcpu->value.qsize = value->qsize;
  364. if (fd > 0 && __cpu_map_load_bpf_program(rcpu, map, fd))
  365. goto free_ptr_ring;
  366. /* Setup kthread */
  367. init_completion(&rcpu->kthread_running);
  368. rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
  369. "cpumap/%d/map:%d", cpu,
  370. map->id);
  371. if (IS_ERR(rcpu->kthread))
  372. goto free_prog;
  373. /* Make sure kthread runs on a single CPU */
  374. kthread_bind(rcpu->kthread, cpu);
  375. wake_up_process(rcpu->kthread);
  376. /* Make sure kthread has been running, so kthread_stop() will not
  377. * stop the kthread prematurely and all pending frames or skbs
  378. * will be handled by the kthread before kthread_stop() returns.
  379. */
  380. wait_for_completion(&rcpu->kthread_running);
  381. return rcpu;
  382. free_prog:
  383. if (rcpu->prog)
  384. bpf_prog_put(rcpu->prog);
  385. free_ptr_ring:
  386. ptr_ring_cleanup(rcpu->queue, NULL);
  387. free_queue:
  388. kfree(rcpu->queue);
  389. free_bulkq:
  390. free_percpu(rcpu->bulkq);
  391. free_rcu:
  392. kfree(rcpu);
  393. return NULL;
  394. }
  395. static void __cpu_map_entry_free(struct work_struct *work)
  396. {
  397. struct bpf_cpu_map_entry *rcpu;
  398. /* This cpu_map_entry have been disconnected from map and one
  399. * RCU grace-period have elapsed. Thus, XDP cannot queue any
  400. * new packets and cannot change/set flush_needed that can
  401. * find this entry.
  402. */
  403. rcpu = container_of(to_rcu_work(work), struct bpf_cpu_map_entry, free_work);
  404. /* kthread_stop will wake_up_process and wait for it to complete.
  405. * cpu_map_kthread_run() makes sure the pointer ring is empty
  406. * before exiting.
  407. */
  408. kthread_stop(rcpu->kthread);
  409. if (rcpu->prog)
  410. bpf_prog_put(rcpu->prog);
  411. /* The queue should be empty at this point */
  412. __cpu_map_ring_cleanup(rcpu->queue);
  413. ptr_ring_cleanup(rcpu->queue, NULL);
  414. kfree(rcpu->queue);
  415. free_percpu(rcpu->bulkq);
  416. kfree(rcpu);
  417. }
  418. /* After the xchg of the bpf_cpu_map_entry pointer, we need to make sure the old
  419. * entry is no longer in use before freeing. We use queue_rcu_work() to call
  420. * __cpu_map_entry_free() in a separate workqueue after waiting for an RCU grace
  421. * period. This means that (a) all pending enqueue and flush operations have
  422. * completed (because of the RCU callback), and (b) we are in a workqueue
  423. * context where we can stop the kthread and wait for it to exit before freeing
  424. * everything.
  425. */
  426. static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
  427. u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
  428. {
  429. struct bpf_cpu_map_entry *old_rcpu;
  430. old_rcpu = unrcu_pointer(xchg(&cmap->cpu_map[key_cpu], RCU_INITIALIZER(rcpu)));
  431. if (old_rcpu) {
  432. INIT_RCU_WORK(&old_rcpu->free_work, __cpu_map_entry_free);
  433. queue_rcu_work(system_wq, &old_rcpu->free_work);
  434. }
  435. }
  436. static long cpu_map_delete_elem(struct bpf_map *map, void *key)
  437. {
  438. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  439. u32 key_cpu = *(u32 *)key;
  440. if (key_cpu >= map->max_entries)
  441. return -EINVAL;
  442. /* notice caller map_delete_elem() uses rcu_read_lock() */
  443. __cpu_map_entry_replace(cmap, key_cpu, NULL);
  444. return 0;
  445. }
  446. static long cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
  447. u64 map_flags)
  448. {
  449. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  450. struct bpf_cpumap_val cpumap_value = {};
  451. struct bpf_cpu_map_entry *rcpu;
  452. /* Array index key correspond to CPU number */
  453. u32 key_cpu = *(u32 *)key;
  454. memcpy(&cpumap_value, value, map->value_size);
  455. if (unlikely(map_flags > BPF_EXIST))
  456. return -EINVAL;
  457. if (unlikely(key_cpu >= cmap->map.max_entries))
  458. return -E2BIG;
  459. if (unlikely(map_flags == BPF_NOEXIST))
  460. return -EEXIST;
  461. if (unlikely(cpumap_value.qsize > 16384)) /* sanity limit on qsize */
  462. return -EOVERFLOW;
  463. /* Make sure CPU is a valid possible cpu */
  464. if (key_cpu >= nr_cpumask_bits || !cpu_possible(key_cpu))
  465. return -ENODEV;
  466. if (cpumap_value.qsize == 0) {
  467. rcpu = NULL; /* Same as deleting */
  468. } else {
  469. /* Updating qsize cause re-allocation of bpf_cpu_map_entry */
  470. rcpu = __cpu_map_entry_alloc(map, &cpumap_value, key_cpu);
  471. if (!rcpu)
  472. return -ENOMEM;
  473. }
  474. rcu_read_lock();
  475. __cpu_map_entry_replace(cmap, key_cpu, rcpu);
  476. rcu_read_unlock();
  477. return 0;
  478. }
  479. static void cpu_map_free(struct bpf_map *map)
  480. {
  481. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  482. u32 i;
  483. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  484. * so the bpf programs (can be more than one that used this map) were
  485. * disconnected from events. Wait for outstanding critical sections in
  486. * these programs to complete. synchronize_rcu() below not only
  487. * guarantees no further "XDP/bpf-side" reads against
  488. * bpf_cpu_map->cpu_map, but also ensure pending flush operations
  489. * (if any) are completed.
  490. */
  491. synchronize_rcu();
  492. /* The only possible user of bpf_cpu_map_entry is
  493. * cpu_map_kthread_run().
  494. */
  495. for (i = 0; i < cmap->map.max_entries; i++) {
  496. struct bpf_cpu_map_entry *rcpu;
  497. rcpu = rcu_dereference_raw(cmap->cpu_map[i]);
  498. if (!rcpu)
  499. continue;
  500. /* Stop kthread and cleanup entry directly */
  501. __cpu_map_entry_free(&rcpu->free_work.work);
  502. }
  503. bpf_map_area_free(cmap->cpu_map);
  504. bpf_map_area_free(cmap);
  505. }
  506. /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
  507. * by local_bh_disable() (from XDP calls inside NAPI). The
  508. * rcu_read_lock_bh_held() below makes lockdep accept both.
  509. */
  510. static void *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
  511. {
  512. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  513. struct bpf_cpu_map_entry *rcpu;
  514. if (key >= map->max_entries)
  515. return NULL;
  516. rcpu = rcu_dereference_check(cmap->cpu_map[key],
  517. rcu_read_lock_bh_held());
  518. return rcpu;
  519. }
  520. static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
  521. {
  522. struct bpf_cpu_map_entry *rcpu =
  523. __cpu_map_lookup_elem(map, *(u32 *)key);
  524. return rcpu ? &rcpu->value : NULL;
  525. }
  526. static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  527. {
  528. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  529. u32 index = key ? *(u32 *)key : U32_MAX;
  530. u32 *next = next_key;
  531. if (index >= cmap->map.max_entries) {
  532. *next = 0;
  533. return 0;
  534. }
  535. if (index == cmap->map.max_entries - 1)
  536. return -ENOENT;
  537. *next = index + 1;
  538. return 0;
  539. }
  540. static long cpu_map_redirect(struct bpf_map *map, u64 index, u64 flags)
  541. {
  542. return __bpf_xdp_redirect_map(map, index, flags, 0,
  543. __cpu_map_lookup_elem);
  544. }
  545. static u64 cpu_map_mem_usage(const struct bpf_map *map)
  546. {
  547. u64 usage = sizeof(struct bpf_cpu_map);
  548. /* Currently the dynamically allocated elements are not counted */
  549. usage += (u64)map->max_entries * sizeof(struct bpf_cpu_map_entry *);
  550. return usage;
  551. }
  552. BTF_ID_LIST_SINGLE(cpu_map_btf_ids, struct, bpf_cpu_map)
  553. const struct bpf_map_ops cpu_map_ops = {
  554. .map_meta_equal = bpf_map_meta_equal,
  555. .map_alloc = cpu_map_alloc,
  556. .map_free = cpu_map_free,
  557. .map_delete_elem = cpu_map_delete_elem,
  558. .map_update_elem = cpu_map_update_elem,
  559. .map_lookup_elem = cpu_map_lookup_elem,
  560. .map_get_next_key = cpu_map_get_next_key,
  561. .map_check_btf = map_check_no_btf,
  562. .map_mem_usage = cpu_map_mem_usage,
  563. .map_btf_id = &cpu_map_btf_ids[0],
  564. .map_redirect = cpu_map_redirect,
  565. };
  566. static void bq_flush_to_queue(struct xdp_bulk_queue *bq)
  567. {
  568. struct bpf_cpu_map_entry *rcpu = bq->obj;
  569. unsigned int processed = 0, drops = 0;
  570. const int to_cpu = rcpu->cpu;
  571. struct ptr_ring *q;
  572. int i;
  573. if (unlikely(!bq->count))
  574. return;
  575. q = rcpu->queue;
  576. spin_lock(&q->producer_lock);
  577. for (i = 0; i < bq->count; i++) {
  578. struct xdp_frame *xdpf = bq->q[i];
  579. int err;
  580. err = __ptr_ring_produce(q, xdpf);
  581. if (err) {
  582. drops++;
  583. xdp_return_frame_rx_napi(xdpf);
  584. }
  585. processed++;
  586. }
  587. bq->count = 0;
  588. spin_unlock(&q->producer_lock);
  589. __list_del_clearprev(&bq->flush_node);
  590. /* Feedback loop via tracepoints */
  591. trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
  592. }
  593. /* Runs under RCU-read-side, plus in softirq under NAPI protection.
  594. * Thus, safe percpu variable access.
  595. */
  596. static void bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
  597. {
  598. struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
  599. if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
  600. bq_flush_to_queue(bq);
  601. /* Notice, xdp_buff/page MUST be queued here, long enough for
  602. * driver to code invoking us to finished, due to driver
  603. * (e.g. ixgbe) recycle tricks based on page-refcnt.
  604. *
  605. * Thus, incoming xdp_frame is always queued here (else we race
  606. * with another CPU on page-refcnt and remaining driver code).
  607. * Queue time is very short, as driver will invoke flush
  608. * operation, when completing napi->poll call.
  609. */
  610. bq->q[bq->count++] = xdpf;
  611. if (!bq->flush_node.prev) {
  612. struct list_head *flush_list = bpf_net_ctx_get_cpu_map_flush_list();
  613. list_add(&bq->flush_node, flush_list);
  614. }
  615. }
  616. int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
  617. struct net_device *dev_rx)
  618. {
  619. /* Info needed when constructing SKB on remote CPU */
  620. xdpf->dev_rx = dev_rx;
  621. bq_enqueue(rcpu, xdpf);
  622. return 0;
  623. }
  624. int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
  625. struct sk_buff *skb)
  626. {
  627. int ret;
  628. __skb_pull(skb, skb->mac_len);
  629. skb_set_redirected(skb, false);
  630. __ptr_set_bit(0, &skb);
  631. ret = ptr_ring_produce(rcpu->queue, skb);
  632. if (ret < 0)
  633. goto trace;
  634. wake_up_process(rcpu->kthread);
  635. trace:
  636. trace_xdp_cpumap_enqueue(rcpu->map_id, !ret, !!ret, rcpu->cpu);
  637. return ret;
  638. }
  639. void __cpu_map_flush(struct list_head *flush_list)
  640. {
  641. struct xdp_bulk_queue *bq, *tmp;
  642. list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
  643. bq_flush_to_queue(bq);
  644. /* If already running, costs spin_lock_irqsave + smb_mb */
  645. wake_up_process(bq->obj->kthread);
  646. }
  647. }