cpumap.c 20 KB

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