cpumap.c 20 KB

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