cpumap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* bpf/cpumap.c
  2. *
  3. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  4. * Released under terms in GPL version 2. See COPYING.
  5. */
  6. /* The 'cpumap' is primarily used as a backend map for XDP BPF helper
  7. * call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
  8. *
  9. * Unlike devmap which redirects XDP frames out another NIC device,
  10. * this map type redirects raw XDP frames to another CPU. The remote
  11. * CPU will do SKB-allocation and call the normal network stack.
  12. *
  13. * This is a scalability and isolation mechanism, that allow
  14. * separating the early driver network XDP layer, from the rest of the
  15. * netstack, and assigning dedicated CPUs for this stage. This
  16. * basically allows for 10G wirespeed pre-filtering via bpf.
  17. */
  18. #include <linux/bpf.h>
  19. #include <linux/filter.h>
  20. #include <linux/ptr_ring.h>
  21. #include <net/xdp.h>
  22. #include <linux/sched.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/kthread.h>
  25. #include <linux/capability.h>
  26. #include <trace/events/xdp.h>
  27. #include <linux/netdevice.h> /* netif_receive_skb_core */
  28. #include <linux/etherdevice.h> /* eth_type_trans */
  29. /* General idea: XDP packets getting XDP redirected to another CPU,
  30. * will maximum be stored/queued for one driver ->poll() call. It is
  31. * guaranteed that setting flush bit and flush operation happen on
  32. * same CPU. Thus, cpu_map_flush operation can deduct via this_cpu_ptr()
  33. * which queue in bpf_cpu_map_entry contains packets.
  34. */
  35. #define CPU_MAP_BULK_SIZE 8 /* 8 == one cacheline on 64-bit archs */
  36. struct xdp_bulk_queue {
  37. void *q[CPU_MAP_BULK_SIZE];
  38. unsigned int count;
  39. };
  40. /* Struct for every remote "destination" CPU in map */
  41. struct bpf_cpu_map_entry {
  42. u32 cpu; /* kthread CPU and map index */
  43. int map_id; /* Back reference to map */
  44. u32 qsize; /* Queue size placeholder for map lookup */
  45. /* XDP can run multiple RX-ring queues, need __percpu enqueue store */
  46. struct xdp_bulk_queue __percpu *bulkq;
  47. /* Queue with potential multi-producers, and single-consumer kthread */
  48. struct ptr_ring *queue;
  49. struct task_struct *kthread;
  50. struct work_struct kthread_stop_wq;
  51. atomic_t refcnt; /* Control when this struct can be free'ed */
  52. struct rcu_head rcu;
  53. };
  54. struct bpf_cpu_map {
  55. struct bpf_map map;
  56. /* Below members specific for map type */
  57. struct bpf_cpu_map_entry **cpu_map;
  58. unsigned long __percpu *flush_needed;
  59. };
  60. static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,
  61. struct xdp_bulk_queue *bq, bool in_napi_ctx);
  62. static u64 cpu_map_bitmap_size(const union bpf_attr *attr)
  63. {
  64. return BITS_TO_LONGS(attr->max_entries) * sizeof(unsigned long);
  65. }
  66. static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
  67. {
  68. struct bpf_cpu_map *cmap;
  69. int err = -ENOMEM;
  70. u64 cost;
  71. int ret;
  72. if (!capable(CAP_SYS_ADMIN))
  73. return ERR_PTR(-EPERM);
  74. /* check sanity of attributes */
  75. if (attr->max_entries == 0 || attr->key_size != 4 ||
  76. attr->value_size != 4 || attr->map_flags & ~BPF_F_NUMA_NODE)
  77. return ERR_PTR(-EINVAL);
  78. cmap = kzalloc(sizeof(*cmap), GFP_USER);
  79. if (!cmap)
  80. return ERR_PTR(-ENOMEM);
  81. bpf_map_init_from_attr(&cmap->map, attr);
  82. /* Pre-limit array size based on NR_CPUS, not final CPU check */
  83. if (cmap->map.max_entries > NR_CPUS) {
  84. err = -E2BIG;
  85. goto free_cmap;
  86. }
  87. /* make sure page count doesn't overflow */
  88. cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *);
  89. cost += cpu_map_bitmap_size(attr) * num_possible_cpus();
  90. if (cost >= U32_MAX - PAGE_SIZE)
  91. goto free_cmap;
  92. cmap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  93. /* Notice returns -EPERM on if map size is larger than memlock limit */
  94. ret = bpf_map_precharge_memlock(cmap->map.pages);
  95. if (ret) {
  96. err = ret;
  97. goto free_cmap;
  98. }
  99. /* A per cpu bitfield with a bit per possible CPU in map */
  100. cmap->flush_needed = __alloc_percpu(cpu_map_bitmap_size(attr),
  101. __alignof__(unsigned long));
  102. if (!cmap->flush_needed)
  103. goto free_cmap;
  104. /* Alloc array for possible remote "destination" CPUs */
  105. cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
  106. sizeof(struct bpf_cpu_map_entry *),
  107. cmap->map.numa_node);
  108. if (!cmap->cpu_map)
  109. goto free_percpu;
  110. return &cmap->map;
  111. free_percpu:
  112. free_percpu(cmap->flush_needed);
  113. free_cmap:
  114. kfree(cmap);
  115. return ERR_PTR(err);
  116. }
  117. static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
  118. {
  119. atomic_inc(&rcpu->refcnt);
  120. }
  121. /* called from workqueue, to workaround syscall using preempt_disable */
  122. static void cpu_map_kthread_stop(struct work_struct *work)
  123. {
  124. struct bpf_cpu_map_entry *rcpu;
  125. rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq);
  126. /* Wait for flush in __cpu_map_entry_free(), via full RCU barrier,
  127. * as it waits until all in-flight call_rcu() callbacks complete.
  128. */
  129. rcu_barrier();
  130. /* kthread_stop will wake_up_process and wait for it to complete */
  131. kthread_stop(rcpu->kthread);
  132. }
  133. static struct sk_buff *cpu_map_build_skb(struct bpf_cpu_map_entry *rcpu,
  134. struct xdp_frame *xdpf)
  135. {
  136. unsigned int hard_start_headroom;
  137. unsigned int frame_size;
  138. void *pkt_data_start;
  139. struct sk_buff *skb;
  140. /* Part of headroom was reserved to xdpf */
  141. hard_start_headroom = sizeof(struct xdp_frame) + xdpf->headroom;
  142. /* build_skb need to place skb_shared_info after SKB end, and
  143. * also want to know the memory "truesize". Thus, need to
  144. * know the memory frame size backing xdp_buff.
  145. *
  146. * XDP was designed to have PAGE_SIZE frames, but this
  147. * assumption is not longer true with ixgbe and i40e. It
  148. * would be preferred to set frame_size to 2048 or 4096
  149. * depending on the driver.
  150. * frame_size = 2048;
  151. * frame_len = frame_size - sizeof(*xdp_frame);
  152. *
  153. * Instead, with info avail, skb_shared_info in placed after
  154. * packet len. This, unfortunately fakes the truesize.
  155. * Another disadvantage of this approach, the skb_shared_info
  156. * is not at a fixed memory location, with mixed length
  157. * packets, which is bad for cache-line hotness.
  158. */
  159. frame_size = SKB_DATA_ALIGN(xdpf->len + hard_start_headroom) +
  160. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  161. pkt_data_start = xdpf->data - hard_start_headroom;
  162. skb = build_skb(pkt_data_start, frame_size);
  163. if (!skb)
  164. return NULL;
  165. skb_reserve(skb, hard_start_headroom);
  166. __skb_put(skb, xdpf->len);
  167. if (xdpf->metasize)
  168. skb_metadata_set(skb, xdpf->metasize);
  169. /* Essential SKB info: protocol and skb->dev */
  170. skb->protocol = eth_type_trans(skb, xdpf->dev_rx);
  171. /* Optional SKB info, currently missing:
  172. * - HW checksum info (skb->ip_summed)
  173. * - HW RX hash (skb_set_hash)
  174. * - RX ring dev queue index (skb_record_rx_queue)
  175. */
  176. /* Allow SKB to reuse area used by xdp_frame */
  177. xdp_scrub_frame(xdpf);
  178. return skb;
  179. }
  180. static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
  181. {
  182. /* The tear-down procedure should have made sure that queue is
  183. * empty. See __cpu_map_entry_replace() and work-queue
  184. * invoked cpu_map_kthread_stop(). Catch any broken behaviour
  185. * gracefully and warn once.
  186. */
  187. struct xdp_frame *xdpf;
  188. while ((xdpf = ptr_ring_consume(ring)))
  189. if (WARN_ON_ONCE(xdpf))
  190. xdp_return_frame(xdpf);
  191. }
  192. static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
  193. {
  194. if (atomic_dec_and_test(&rcpu->refcnt)) {
  195. /* The queue should be empty at this point */
  196. __cpu_map_ring_cleanup(rcpu->queue);
  197. ptr_ring_cleanup(rcpu->queue, NULL);
  198. kfree(rcpu->queue);
  199. kfree(rcpu);
  200. }
  201. }
  202. static int cpu_map_kthread_run(void *data)
  203. {
  204. struct bpf_cpu_map_entry *rcpu = data;
  205. set_current_state(TASK_INTERRUPTIBLE);
  206. /* When kthread gives stop order, then rcpu have been disconnected
  207. * from map, thus no new packets can enter. Remaining in-flight
  208. * per CPU stored packets are flushed to this queue. Wait honoring
  209. * kthread_stop signal until queue is empty.
  210. */
  211. while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
  212. unsigned int processed = 0, drops = 0, sched = 0;
  213. struct xdp_frame *xdpf;
  214. /* Release CPU reschedule checks */
  215. if (__ptr_ring_empty(rcpu->queue)) {
  216. set_current_state(TASK_INTERRUPTIBLE);
  217. /* Recheck to avoid lost wake-up */
  218. if (__ptr_ring_empty(rcpu->queue)) {
  219. schedule();
  220. sched = 1;
  221. } else {
  222. __set_current_state(TASK_RUNNING);
  223. }
  224. } else {
  225. sched = cond_resched();
  226. }
  227. /* Process packets in rcpu->queue */
  228. local_bh_disable();
  229. /*
  230. * The bpf_cpu_map_entry is single consumer, with this
  231. * kthread CPU pinned. Lockless access to ptr_ring
  232. * consume side valid as no-resize allowed of queue.
  233. */
  234. while ((xdpf = __ptr_ring_consume(rcpu->queue))) {
  235. struct sk_buff *skb;
  236. int ret;
  237. skb = cpu_map_build_skb(rcpu, xdpf);
  238. if (!skb) {
  239. xdp_return_frame(xdpf);
  240. continue;
  241. }
  242. /* Inject into network stack */
  243. ret = netif_receive_skb_core(skb);
  244. if (ret == NET_RX_DROP)
  245. drops++;
  246. /* Limit BH-disable period */
  247. if (++processed == 8)
  248. break;
  249. }
  250. /* Feedback loop via tracepoint */
  251. trace_xdp_cpumap_kthread(rcpu->map_id, processed, drops, sched);
  252. local_bh_enable(); /* resched point, may call do_softirq() */
  253. }
  254. __set_current_state(TASK_RUNNING);
  255. put_cpu_map_entry(rcpu);
  256. return 0;
  257. }
  258. static struct bpf_cpu_map_entry *__cpu_map_entry_alloc(u32 qsize, u32 cpu,
  259. int map_id)
  260. {
  261. gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
  262. struct bpf_cpu_map_entry *rcpu;
  263. int numa, err;
  264. /* Have map->numa_node, but choose node of redirect target CPU */
  265. numa = cpu_to_node(cpu);
  266. rcpu = kzalloc_node(sizeof(*rcpu), gfp, numa);
  267. if (!rcpu)
  268. return NULL;
  269. /* Alloc percpu bulkq */
  270. rcpu->bulkq = __alloc_percpu_gfp(sizeof(*rcpu->bulkq),
  271. sizeof(void *), gfp);
  272. if (!rcpu->bulkq)
  273. goto free_rcu;
  274. /* Alloc queue */
  275. rcpu->queue = kzalloc_node(sizeof(*rcpu->queue), gfp, numa);
  276. if (!rcpu->queue)
  277. goto free_bulkq;
  278. err = ptr_ring_init(rcpu->queue, qsize, gfp);
  279. if (err)
  280. goto free_queue;
  281. rcpu->cpu = cpu;
  282. rcpu->map_id = map_id;
  283. rcpu->qsize = qsize;
  284. /* Setup kthread */
  285. rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
  286. "cpumap/%d/map:%d", cpu, map_id);
  287. if (IS_ERR(rcpu->kthread))
  288. goto free_ptr_ring;
  289. get_cpu_map_entry(rcpu); /* 1-refcnt for being in cmap->cpu_map[] */
  290. get_cpu_map_entry(rcpu); /* 1-refcnt for kthread */
  291. /* Make sure kthread runs on a single CPU */
  292. kthread_bind(rcpu->kthread, cpu);
  293. wake_up_process(rcpu->kthread);
  294. return rcpu;
  295. free_ptr_ring:
  296. ptr_ring_cleanup(rcpu->queue, NULL);
  297. free_queue:
  298. kfree(rcpu->queue);
  299. free_bulkq:
  300. free_percpu(rcpu->bulkq);
  301. free_rcu:
  302. kfree(rcpu);
  303. return NULL;
  304. }
  305. static void __cpu_map_entry_free(struct rcu_head *rcu)
  306. {
  307. struct bpf_cpu_map_entry *rcpu;
  308. int cpu;
  309. /* This cpu_map_entry have been disconnected from map and one
  310. * RCU graze-period have elapsed. Thus, XDP cannot queue any
  311. * new packets and cannot change/set flush_needed that can
  312. * find this entry.
  313. */
  314. rcpu = container_of(rcu, struct bpf_cpu_map_entry, rcu);
  315. /* Flush remaining packets in percpu bulkq */
  316. for_each_online_cpu(cpu) {
  317. struct xdp_bulk_queue *bq = per_cpu_ptr(rcpu->bulkq, cpu);
  318. /* No concurrent bq_enqueue can run at this point */
  319. bq_flush_to_queue(rcpu, bq, false);
  320. }
  321. free_percpu(rcpu->bulkq);
  322. /* Cannot kthread_stop() here, last put free rcpu resources */
  323. put_cpu_map_entry(rcpu);
  324. }
  325. /* After xchg pointer to bpf_cpu_map_entry, use the call_rcu() to
  326. * ensure any driver rcu critical sections have completed, but this
  327. * does not guarantee a flush has happened yet. Because driver side
  328. * rcu_read_lock/unlock only protects the running XDP program. The
  329. * atomic xchg and NULL-ptr check in __cpu_map_flush() makes sure a
  330. * pending flush op doesn't fail.
  331. *
  332. * The bpf_cpu_map_entry is still used by the kthread, and there can
  333. * still be pending packets (in queue and percpu bulkq). A refcnt
  334. * makes sure to last user (kthread_stop vs. call_rcu) free memory
  335. * resources.
  336. *
  337. * The rcu callback __cpu_map_entry_free flush remaining packets in
  338. * percpu bulkq to queue. Due to caller map_delete_elem() disable
  339. * preemption, cannot call kthread_stop() to make sure queue is empty.
  340. * Instead a work_queue is started for stopping kthread,
  341. * cpu_map_kthread_stop, which waits for an RCU graze period before
  342. * stopping kthread, emptying the queue.
  343. */
  344. static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
  345. u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
  346. {
  347. struct bpf_cpu_map_entry *old_rcpu;
  348. old_rcpu = xchg(&cmap->cpu_map[key_cpu], rcpu);
  349. if (old_rcpu) {
  350. call_rcu(&old_rcpu->rcu, __cpu_map_entry_free);
  351. INIT_WORK(&old_rcpu->kthread_stop_wq, cpu_map_kthread_stop);
  352. schedule_work(&old_rcpu->kthread_stop_wq);
  353. }
  354. }
  355. static int cpu_map_delete_elem(struct bpf_map *map, void *key)
  356. {
  357. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  358. u32 key_cpu = *(u32 *)key;
  359. if (key_cpu >= map->max_entries)
  360. return -EINVAL;
  361. /* notice caller map_delete_elem() use preempt_disable() */
  362. __cpu_map_entry_replace(cmap, key_cpu, NULL);
  363. return 0;
  364. }
  365. static int cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
  366. u64 map_flags)
  367. {
  368. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  369. struct bpf_cpu_map_entry *rcpu;
  370. /* Array index key correspond to CPU number */
  371. u32 key_cpu = *(u32 *)key;
  372. /* Value is the queue size */
  373. u32 qsize = *(u32 *)value;
  374. if (unlikely(map_flags > BPF_EXIST))
  375. return -EINVAL;
  376. if (unlikely(key_cpu >= cmap->map.max_entries))
  377. return -E2BIG;
  378. if (unlikely(map_flags == BPF_NOEXIST))
  379. return -EEXIST;
  380. if (unlikely(qsize > 16384)) /* sanity limit on qsize */
  381. return -EOVERFLOW;
  382. /* Make sure CPU is a valid possible cpu */
  383. if (key_cpu >= nr_cpumask_bits || !cpu_possible(key_cpu))
  384. return -ENODEV;
  385. if (qsize == 0) {
  386. rcpu = NULL; /* Same as deleting */
  387. } else {
  388. /* Updating qsize cause re-allocation of bpf_cpu_map_entry */
  389. rcpu = __cpu_map_entry_alloc(qsize, key_cpu, map->id);
  390. if (!rcpu)
  391. return -ENOMEM;
  392. }
  393. rcu_read_lock();
  394. __cpu_map_entry_replace(cmap, key_cpu, rcpu);
  395. rcu_read_unlock();
  396. return 0;
  397. }
  398. static void cpu_map_free(struct bpf_map *map)
  399. {
  400. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  401. int cpu;
  402. u32 i;
  403. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  404. * so the bpf programs (can be more than one that used this map) were
  405. * disconnected from events. Wait for outstanding critical sections in
  406. * these programs to complete. The rcu critical section only guarantees
  407. * no further "XDP/bpf-side" reads against bpf_cpu_map->cpu_map.
  408. * It does __not__ ensure pending flush operations (if any) are
  409. * complete.
  410. */
  411. bpf_clear_redirect_map(map);
  412. synchronize_rcu();
  413. /* To ensure all pending flush operations have completed wait for flush
  414. * bitmap to indicate all flush_needed bits to be zero on _all_ cpus.
  415. * Because the above synchronize_rcu() ensures the map is disconnected
  416. * from the program we can assume no new bits will be set.
  417. */
  418. for_each_online_cpu(cpu) {
  419. unsigned long *bitmap = per_cpu_ptr(cmap->flush_needed, cpu);
  420. while (!bitmap_empty(bitmap, cmap->map.max_entries))
  421. cond_resched();
  422. }
  423. /* For cpu_map the remote CPUs can still be using the entries
  424. * (struct bpf_cpu_map_entry).
  425. */
  426. for (i = 0; i < cmap->map.max_entries; i++) {
  427. struct bpf_cpu_map_entry *rcpu;
  428. rcpu = READ_ONCE(cmap->cpu_map[i]);
  429. if (!rcpu)
  430. continue;
  431. /* bq flush and cleanup happens after RCU graze-period */
  432. __cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */
  433. }
  434. free_percpu(cmap->flush_needed);
  435. bpf_map_area_free(cmap->cpu_map);
  436. kfree(cmap);
  437. }
  438. struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
  439. {
  440. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  441. struct bpf_cpu_map_entry *rcpu;
  442. if (key >= map->max_entries)
  443. return NULL;
  444. rcpu = READ_ONCE(cmap->cpu_map[key]);
  445. return rcpu;
  446. }
  447. static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
  448. {
  449. struct bpf_cpu_map_entry *rcpu =
  450. __cpu_map_lookup_elem(map, *(u32 *)key);
  451. return rcpu ? &rcpu->qsize : NULL;
  452. }
  453. static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  454. {
  455. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  456. u32 index = key ? *(u32 *)key : U32_MAX;
  457. u32 *next = next_key;
  458. if (index >= cmap->map.max_entries) {
  459. *next = 0;
  460. return 0;
  461. }
  462. if (index == cmap->map.max_entries - 1)
  463. return -ENOENT;
  464. *next = index + 1;
  465. return 0;
  466. }
  467. const struct bpf_map_ops cpu_map_ops = {
  468. .map_alloc = cpu_map_alloc,
  469. .map_free = cpu_map_free,
  470. .map_delete_elem = cpu_map_delete_elem,
  471. .map_update_elem = cpu_map_update_elem,
  472. .map_lookup_elem = cpu_map_lookup_elem,
  473. .map_get_next_key = cpu_map_get_next_key,
  474. .map_check_btf = map_check_no_btf,
  475. };
  476. static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,
  477. struct xdp_bulk_queue *bq, bool in_napi_ctx)
  478. {
  479. unsigned int processed = 0, drops = 0;
  480. const int to_cpu = rcpu->cpu;
  481. struct ptr_ring *q;
  482. int i;
  483. if (unlikely(!bq->count))
  484. return 0;
  485. q = rcpu->queue;
  486. spin_lock(&q->producer_lock);
  487. for (i = 0; i < bq->count; i++) {
  488. struct xdp_frame *xdpf = bq->q[i];
  489. int err;
  490. err = __ptr_ring_produce(q, xdpf);
  491. if (err) {
  492. drops++;
  493. if (likely(in_napi_ctx))
  494. xdp_return_frame_rx_napi(xdpf);
  495. else
  496. xdp_return_frame(xdpf);
  497. }
  498. processed++;
  499. }
  500. bq->count = 0;
  501. spin_unlock(&q->producer_lock);
  502. /* Feedback loop via tracepoints */
  503. trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
  504. return 0;
  505. }
  506. /* Runs under RCU-read-side, plus in softirq under NAPI protection.
  507. * Thus, safe percpu variable access.
  508. */
  509. static int bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
  510. {
  511. struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
  512. if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
  513. bq_flush_to_queue(rcpu, bq, true);
  514. /* Notice, xdp_buff/page MUST be queued here, long enough for
  515. * driver to code invoking us to finished, due to driver
  516. * (e.g. ixgbe) recycle tricks based on page-refcnt.
  517. *
  518. * Thus, incoming xdp_frame is always queued here (else we race
  519. * with another CPU on page-refcnt and remaining driver code).
  520. * Queue time is very short, as driver will invoke flush
  521. * operation, when completing napi->poll call.
  522. */
  523. bq->q[bq->count++] = xdpf;
  524. return 0;
  525. }
  526. int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
  527. struct net_device *dev_rx)
  528. {
  529. struct xdp_frame *xdpf;
  530. xdpf = convert_to_xdp_frame(xdp);
  531. if (unlikely(!xdpf))
  532. return -EOVERFLOW;
  533. /* Info needed when constructing SKB on remote CPU */
  534. xdpf->dev_rx = dev_rx;
  535. bq_enqueue(rcpu, xdpf);
  536. return 0;
  537. }
  538. void __cpu_map_insert_ctx(struct bpf_map *map, u32 bit)
  539. {
  540. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  541. unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed);
  542. __set_bit(bit, bitmap);
  543. }
  544. void __cpu_map_flush(struct bpf_map *map)
  545. {
  546. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  547. unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed);
  548. u32 bit;
  549. /* The napi->poll softirq makes sure __cpu_map_insert_ctx()
  550. * and __cpu_map_flush() happen on same CPU. Thus, the percpu
  551. * bitmap indicate which percpu bulkq have packets.
  552. */
  553. for_each_set_bit(bit, bitmap, map->max_entries) {
  554. struct bpf_cpu_map_entry *rcpu = READ_ONCE(cmap->cpu_map[bit]);
  555. struct xdp_bulk_queue *bq;
  556. /* This is possible if entry is removed by user space
  557. * between xdp redirect and flush op.
  558. */
  559. if (unlikely(!rcpu))
  560. continue;
  561. __clear_bit(bit, bitmap);
  562. /* Flush all frames in bulkq to real queue */
  563. bq = this_cpu_ptr(rcpu->bulkq);
  564. bq_flush_to_queue(rcpu, bq, true);
  565. /* If already running, costs spin_lock_irqsave + smb_mb */
  566. wake_up_process(rcpu->kthread);
  567. }
  568. }