cpumap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 frame_size;
  137. void *pkt_data_start;
  138. struct sk_buff *skb;
  139. /* build_skb need to place skb_shared_info after SKB end, and
  140. * also want to know the memory "truesize". Thus, need to
  141. * know the memory frame size backing xdp_buff.
  142. *
  143. * XDP was designed to have PAGE_SIZE frames, but this
  144. * assumption is not longer true with ixgbe and i40e. It
  145. * would be preferred to set frame_size to 2048 or 4096
  146. * depending on the driver.
  147. * frame_size = 2048;
  148. * frame_len = frame_size - sizeof(*xdp_frame);
  149. *
  150. * Instead, with info avail, skb_shared_info in placed after
  151. * packet len. This, unfortunately fakes the truesize.
  152. * Another disadvantage of this approach, the skb_shared_info
  153. * is not at a fixed memory location, with mixed length
  154. * packets, which is bad for cache-line hotness.
  155. */
  156. frame_size = SKB_DATA_ALIGN(xdpf->len) + xdpf->headroom +
  157. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  158. pkt_data_start = xdpf->data - xdpf->headroom;
  159. skb = build_skb(pkt_data_start, frame_size);
  160. if (!skb)
  161. return NULL;
  162. skb_reserve(skb, xdpf->headroom);
  163. __skb_put(skb, xdpf->len);
  164. if (xdpf->metasize)
  165. skb_metadata_set(skb, xdpf->metasize);
  166. /* Essential SKB info: protocol and skb->dev */
  167. skb->protocol = eth_type_trans(skb, xdpf->dev_rx);
  168. /* Optional SKB info, currently missing:
  169. * - HW checksum info (skb->ip_summed)
  170. * - HW RX hash (skb_set_hash)
  171. * - RX ring dev queue index (skb_record_rx_queue)
  172. */
  173. return skb;
  174. }
  175. static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
  176. {
  177. /* The tear-down procedure should have made sure that queue is
  178. * empty. See __cpu_map_entry_replace() and work-queue
  179. * invoked cpu_map_kthread_stop(). Catch any broken behaviour
  180. * gracefully and warn once.
  181. */
  182. struct xdp_frame *xdpf;
  183. while ((xdpf = ptr_ring_consume(ring)))
  184. if (WARN_ON_ONCE(xdpf))
  185. xdp_return_frame(xdpf);
  186. }
  187. static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
  188. {
  189. if (atomic_dec_and_test(&rcpu->refcnt)) {
  190. /* The queue should be empty at this point */
  191. __cpu_map_ring_cleanup(rcpu->queue);
  192. ptr_ring_cleanup(rcpu->queue, NULL);
  193. kfree(rcpu->queue);
  194. kfree(rcpu);
  195. }
  196. }
  197. static int cpu_map_kthread_run(void *data)
  198. {
  199. struct bpf_cpu_map_entry *rcpu = data;
  200. set_current_state(TASK_INTERRUPTIBLE);
  201. /* When kthread gives stop order, then rcpu have been disconnected
  202. * from map, thus no new packets can enter. Remaining in-flight
  203. * per CPU stored packets are flushed to this queue. Wait honoring
  204. * kthread_stop signal until queue is empty.
  205. */
  206. while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
  207. unsigned int processed = 0, drops = 0, sched = 0;
  208. struct xdp_frame *xdpf;
  209. /* Release CPU reschedule checks */
  210. if (__ptr_ring_empty(rcpu->queue)) {
  211. set_current_state(TASK_INTERRUPTIBLE);
  212. /* Recheck to avoid lost wake-up */
  213. if (__ptr_ring_empty(rcpu->queue)) {
  214. schedule();
  215. sched = 1;
  216. } else {
  217. __set_current_state(TASK_RUNNING);
  218. }
  219. } else {
  220. sched = cond_resched();
  221. }
  222. /* Process packets in rcpu->queue */
  223. local_bh_disable();
  224. /*
  225. * The bpf_cpu_map_entry is single consumer, with this
  226. * kthread CPU pinned. Lockless access to ptr_ring
  227. * consume side valid as no-resize allowed of queue.
  228. */
  229. while ((xdpf = __ptr_ring_consume(rcpu->queue))) {
  230. struct sk_buff *skb;
  231. int ret;
  232. skb = cpu_map_build_skb(rcpu, xdpf);
  233. if (!skb) {
  234. xdp_return_frame(xdpf);
  235. continue;
  236. }
  237. /* Inject into network stack */
  238. ret = netif_receive_skb_core(skb);
  239. if (ret == NET_RX_DROP)
  240. drops++;
  241. /* Limit BH-disable period */
  242. if (++processed == 8)
  243. break;
  244. }
  245. /* Feedback loop via tracepoint */
  246. trace_xdp_cpumap_kthread(rcpu->map_id, processed, drops, sched);
  247. local_bh_enable(); /* resched point, may call do_softirq() */
  248. }
  249. __set_current_state(TASK_RUNNING);
  250. put_cpu_map_entry(rcpu);
  251. return 0;
  252. }
  253. static struct bpf_cpu_map_entry *__cpu_map_entry_alloc(u32 qsize, u32 cpu,
  254. int map_id)
  255. {
  256. gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
  257. struct bpf_cpu_map_entry *rcpu;
  258. int numa, err;
  259. /* Have map->numa_node, but choose node of redirect target CPU */
  260. numa = cpu_to_node(cpu);
  261. rcpu = kzalloc_node(sizeof(*rcpu), gfp, numa);
  262. if (!rcpu)
  263. return NULL;
  264. /* Alloc percpu bulkq */
  265. rcpu->bulkq = __alloc_percpu_gfp(sizeof(*rcpu->bulkq),
  266. sizeof(void *), gfp);
  267. if (!rcpu->bulkq)
  268. goto free_rcu;
  269. /* Alloc queue */
  270. rcpu->queue = kzalloc_node(sizeof(*rcpu->queue), gfp, numa);
  271. if (!rcpu->queue)
  272. goto free_bulkq;
  273. err = ptr_ring_init(rcpu->queue, qsize, gfp);
  274. if (err)
  275. goto free_queue;
  276. rcpu->cpu = cpu;
  277. rcpu->map_id = map_id;
  278. rcpu->qsize = qsize;
  279. /* Setup kthread */
  280. rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
  281. "cpumap/%d/map:%d", cpu, map_id);
  282. if (IS_ERR(rcpu->kthread))
  283. goto free_ptr_ring;
  284. get_cpu_map_entry(rcpu); /* 1-refcnt for being in cmap->cpu_map[] */
  285. get_cpu_map_entry(rcpu); /* 1-refcnt for kthread */
  286. /* Make sure kthread runs on a single CPU */
  287. kthread_bind(rcpu->kthread, cpu);
  288. wake_up_process(rcpu->kthread);
  289. return rcpu;
  290. free_ptr_ring:
  291. ptr_ring_cleanup(rcpu->queue, NULL);
  292. free_queue:
  293. kfree(rcpu->queue);
  294. free_bulkq:
  295. free_percpu(rcpu->bulkq);
  296. free_rcu:
  297. kfree(rcpu);
  298. return NULL;
  299. }
  300. static void __cpu_map_entry_free(struct rcu_head *rcu)
  301. {
  302. struct bpf_cpu_map_entry *rcpu;
  303. int cpu;
  304. /* This cpu_map_entry have been disconnected from map and one
  305. * RCU graze-period have elapsed. Thus, XDP cannot queue any
  306. * new packets and cannot change/set flush_needed that can
  307. * find this entry.
  308. */
  309. rcpu = container_of(rcu, struct bpf_cpu_map_entry, rcu);
  310. /* Flush remaining packets in percpu bulkq */
  311. for_each_online_cpu(cpu) {
  312. struct xdp_bulk_queue *bq = per_cpu_ptr(rcpu->bulkq, cpu);
  313. /* No concurrent bq_enqueue can run at this point */
  314. bq_flush_to_queue(rcpu, bq, false);
  315. }
  316. free_percpu(rcpu->bulkq);
  317. /* Cannot kthread_stop() here, last put free rcpu resources */
  318. put_cpu_map_entry(rcpu);
  319. }
  320. /* After xchg pointer to bpf_cpu_map_entry, use the call_rcu() to
  321. * ensure any driver rcu critical sections have completed, but this
  322. * does not guarantee a flush has happened yet. Because driver side
  323. * rcu_read_lock/unlock only protects the running XDP program. The
  324. * atomic xchg and NULL-ptr check in __cpu_map_flush() makes sure a
  325. * pending flush op doesn't fail.
  326. *
  327. * The bpf_cpu_map_entry is still used by the kthread, and there can
  328. * still be pending packets (in queue and percpu bulkq). A refcnt
  329. * makes sure to last user (kthread_stop vs. call_rcu) free memory
  330. * resources.
  331. *
  332. * The rcu callback __cpu_map_entry_free flush remaining packets in
  333. * percpu bulkq to queue. Due to caller map_delete_elem() disable
  334. * preemption, cannot call kthread_stop() to make sure queue is empty.
  335. * Instead a work_queue is started for stopping kthread,
  336. * cpu_map_kthread_stop, which waits for an RCU graze period before
  337. * stopping kthread, emptying the queue.
  338. */
  339. static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
  340. u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
  341. {
  342. struct bpf_cpu_map_entry *old_rcpu;
  343. old_rcpu = xchg(&cmap->cpu_map[key_cpu], rcpu);
  344. if (old_rcpu) {
  345. call_rcu(&old_rcpu->rcu, __cpu_map_entry_free);
  346. INIT_WORK(&old_rcpu->kthread_stop_wq, cpu_map_kthread_stop);
  347. schedule_work(&old_rcpu->kthread_stop_wq);
  348. }
  349. }
  350. static int cpu_map_delete_elem(struct bpf_map *map, void *key)
  351. {
  352. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  353. u32 key_cpu = *(u32 *)key;
  354. if (key_cpu >= map->max_entries)
  355. return -EINVAL;
  356. /* notice caller map_delete_elem() use preempt_disable() */
  357. __cpu_map_entry_replace(cmap, key_cpu, NULL);
  358. return 0;
  359. }
  360. static int cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
  361. u64 map_flags)
  362. {
  363. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  364. struct bpf_cpu_map_entry *rcpu;
  365. /* Array index key correspond to CPU number */
  366. u32 key_cpu = *(u32 *)key;
  367. /* Value is the queue size */
  368. u32 qsize = *(u32 *)value;
  369. if (unlikely(map_flags > BPF_EXIST))
  370. return -EINVAL;
  371. if (unlikely(key_cpu >= cmap->map.max_entries))
  372. return -E2BIG;
  373. if (unlikely(map_flags == BPF_NOEXIST))
  374. return -EEXIST;
  375. if (unlikely(qsize > 16384)) /* sanity limit on qsize */
  376. return -EOVERFLOW;
  377. /* Make sure CPU is a valid possible cpu */
  378. if (!cpu_possible(key_cpu))
  379. return -ENODEV;
  380. if (qsize == 0) {
  381. rcpu = NULL; /* Same as deleting */
  382. } else {
  383. /* Updating qsize cause re-allocation of bpf_cpu_map_entry */
  384. rcpu = __cpu_map_entry_alloc(qsize, key_cpu, map->id);
  385. if (!rcpu)
  386. return -ENOMEM;
  387. }
  388. rcu_read_lock();
  389. __cpu_map_entry_replace(cmap, key_cpu, rcpu);
  390. rcu_read_unlock();
  391. return 0;
  392. }
  393. static void cpu_map_free(struct bpf_map *map)
  394. {
  395. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  396. int cpu;
  397. u32 i;
  398. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  399. * so the bpf programs (can be more than one that used this map) were
  400. * disconnected from events. Wait for outstanding critical sections in
  401. * these programs to complete. The rcu critical section only guarantees
  402. * no further "XDP/bpf-side" reads against bpf_cpu_map->cpu_map.
  403. * It does __not__ ensure pending flush operations (if any) are
  404. * complete.
  405. */
  406. synchronize_rcu();
  407. /* To ensure all pending flush operations have completed wait for flush
  408. * bitmap to indicate all flush_needed bits to be zero on _all_ cpus.
  409. * Because the above synchronize_rcu() ensures the map is disconnected
  410. * from the program we can assume no new bits will be set.
  411. */
  412. for_each_online_cpu(cpu) {
  413. unsigned long *bitmap = per_cpu_ptr(cmap->flush_needed, cpu);
  414. while (!bitmap_empty(bitmap, cmap->map.max_entries))
  415. cond_resched();
  416. }
  417. /* For cpu_map the remote CPUs can still be using the entries
  418. * (struct bpf_cpu_map_entry).
  419. */
  420. for (i = 0; i < cmap->map.max_entries; i++) {
  421. struct bpf_cpu_map_entry *rcpu;
  422. rcpu = READ_ONCE(cmap->cpu_map[i]);
  423. if (!rcpu)
  424. continue;
  425. /* bq flush and cleanup happens after RCU graze-period */
  426. __cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */
  427. }
  428. free_percpu(cmap->flush_needed);
  429. bpf_map_area_free(cmap->cpu_map);
  430. kfree(cmap);
  431. }
  432. struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
  433. {
  434. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  435. struct bpf_cpu_map_entry *rcpu;
  436. if (key >= map->max_entries)
  437. return NULL;
  438. rcpu = READ_ONCE(cmap->cpu_map[key]);
  439. return rcpu;
  440. }
  441. static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
  442. {
  443. struct bpf_cpu_map_entry *rcpu =
  444. __cpu_map_lookup_elem(map, *(u32 *)key);
  445. return rcpu ? &rcpu->qsize : NULL;
  446. }
  447. static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  448. {
  449. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  450. u32 index = key ? *(u32 *)key : U32_MAX;
  451. u32 *next = next_key;
  452. if (index >= cmap->map.max_entries) {
  453. *next = 0;
  454. return 0;
  455. }
  456. if (index == cmap->map.max_entries - 1)
  457. return -ENOENT;
  458. *next = index + 1;
  459. return 0;
  460. }
  461. const struct bpf_map_ops cpu_map_ops = {
  462. .map_alloc = cpu_map_alloc,
  463. .map_free = cpu_map_free,
  464. .map_delete_elem = cpu_map_delete_elem,
  465. .map_update_elem = cpu_map_update_elem,
  466. .map_lookup_elem = cpu_map_lookup_elem,
  467. .map_get_next_key = cpu_map_get_next_key,
  468. .map_check_btf = map_check_no_btf,
  469. };
  470. static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,
  471. struct xdp_bulk_queue *bq, bool in_napi_ctx)
  472. {
  473. unsigned int processed = 0, drops = 0;
  474. const int to_cpu = rcpu->cpu;
  475. struct ptr_ring *q;
  476. int i;
  477. if (unlikely(!bq->count))
  478. return 0;
  479. q = rcpu->queue;
  480. spin_lock(&q->producer_lock);
  481. for (i = 0; i < bq->count; i++) {
  482. struct xdp_frame *xdpf = bq->q[i];
  483. int err;
  484. err = __ptr_ring_produce(q, xdpf);
  485. if (err) {
  486. drops++;
  487. if (likely(in_napi_ctx))
  488. xdp_return_frame_rx_napi(xdpf);
  489. else
  490. xdp_return_frame(xdpf);
  491. }
  492. processed++;
  493. }
  494. bq->count = 0;
  495. spin_unlock(&q->producer_lock);
  496. /* Feedback loop via tracepoints */
  497. trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
  498. return 0;
  499. }
  500. /* Runs under RCU-read-side, plus in softirq under NAPI protection.
  501. * Thus, safe percpu variable access.
  502. */
  503. static int bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
  504. {
  505. struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
  506. if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
  507. bq_flush_to_queue(rcpu, bq, true);
  508. /* Notice, xdp_buff/page MUST be queued here, long enough for
  509. * driver to code invoking us to finished, due to driver
  510. * (e.g. ixgbe) recycle tricks based on page-refcnt.
  511. *
  512. * Thus, incoming xdp_frame is always queued here (else we race
  513. * with another CPU on page-refcnt and remaining driver code).
  514. * Queue time is very short, as driver will invoke flush
  515. * operation, when completing napi->poll call.
  516. */
  517. bq->q[bq->count++] = xdpf;
  518. return 0;
  519. }
  520. int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
  521. struct net_device *dev_rx)
  522. {
  523. struct xdp_frame *xdpf;
  524. xdpf = convert_to_xdp_frame(xdp);
  525. if (unlikely(!xdpf))
  526. return -EOVERFLOW;
  527. /* Info needed when constructing SKB on remote CPU */
  528. xdpf->dev_rx = dev_rx;
  529. bq_enqueue(rcpu, xdpf);
  530. return 0;
  531. }
  532. void __cpu_map_insert_ctx(struct bpf_map *map, u32 bit)
  533. {
  534. struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
  535. unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed);
  536. __set_bit(bit, bitmap);
  537. }
  538. void __cpu_map_flush(struct bpf_map *map)
  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. u32 bit;
  543. /* The napi->poll softirq makes sure __cpu_map_insert_ctx()
  544. * and __cpu_map_flush() happen on same CPU. Thus, the percpu
  545. * bitmap indicate which percpu bulkq have packets.
  546. */
  547. for_each_set_bit(bit, bitmap, map->max_entries) {
  548. struct bpf_cpu_map_entry *rcpu = READ_ONCE(cmap->cpu_map[bit]);
  549. struct xdp_bulk_queue *bq;
  550. /* This is possible if entry is removed by user space
  551. * between xdp redirect and flush op.
  552. */
  553. if (unlikely(!rcpu))
  554. continue;
  555. __clear_bit(bit, bitmap);
  556. /* Flush all frames in bulkq to real queue */
  557. bq = this_cpu_ptr(rcpu->bulkq);
  558. bq_flush_to_queue(rcpu, bq, true);
  559. /* If already running, costs spin_lock_irqsave + smb_mb */
  560. wake_up_process(rcpu->kthread);
  561. }
  562. }