1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/bpf.h>
20#include <linux/filter.h>
21#include <linux/ptr_ring.h>
22#include <net/xdp.h>
23
24#include <linux/sched.h>
25#include <linux/workqueue.h>
26#include <linux/kthread.h>
27#include <linux/capability.h>
28#include <trace/events/xdp.h>
29
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32
33
34
35
36
37
38
39
40#define CPU_MAP_BULK_SIZE 8
41struct xdp_bulk_queue {
42 void *q[CPU_MAP_BULK_SIZE];
43 unsigned int count;
44};
45
46
47struct bpf_cpu_map_entry {
48 u32 cpu;
49 int map_id;
50 u32 qsize;
51
52
53 struct xdp_bulk_queue __percpu *bulkq;
54
55
56 struct ptr_ring *queue;
57 struct task_struct *kthread;
58 struct work_struct kthread_stop_wq;
59
60 atomic_t refcnt;
61 struct rcu_head rcu;
62};
63
64struct bpf_cpu_map {
65 struct bpf_map map;
66
67 struct bpf_cpu_map_entry **cpu_map;
68 unsigned long __percpu *flush_needed;
69};
70
71static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,
72 struct xdp_bulk_queue *bq, bool in_napi_ctx);
73
74static u64 cpu_map_bitmap_size(const union bpf_attr *attr)
75{
76 return BITS_TO_LONGS(attr->max_entries) * sizeof(unsigned long);
77}
78
79static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
80{
81 struct bpf_cpu_map *cmap;
82 int err = -ENOMEM;
83 u64 cost;
84 int ret;
85
86 if (!capable(CAP_SYS_ADMIN))
87 return ERR_PTR(-EPERM);
88
89
90 if (attr->max_entries == 0 || attr->key_size != 4 ||
91 attr->value_size != 4 || attr->map_flags & ~BPF_F_NUMA_NODE)
92 return ERR_PTR(-EINVAL);
93
94 cmap = kzalloc(sizeof(*cmap), GFP_USER);
95 if (!cmap)
96 return ERR_PTR(-ENOMEM);
97
98 bpf_map_init_from_attr(&cmap->map, attr);
99
100
101 if (cmap->map.max_entries > NR_CPUS) {
102 err = -E2BIG;
103 goto free_cmap;
104 }
105
106
107 cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *);
108 cost += cpu_map_bitmap_size(attr) * num_possible_cpus();
109 if (cost >= U32_MAX - PAGE_SIZE)
110 goto free_cmap;
111 cmap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
112
113
114 ret = bpf_map_precharge_memlock(cmap->map.pages);
115 if (ret) {
116 err = ret;
117 goto free_cmap;
118 }
119
120
121 cmap->flush_needed = __alloc_percpu(cpu_map_bitmap_size(attr),
122 __alignof__(unsigned long));
123 if (!cmap->flush_needed)
124 goto free_cmap;
125
126
127 cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
128 sizeof(struct bpf_cpu_map_entry *),
129 cmap->map.numa_node);
130 if (!cmap->cpu_map)
131 goto free_percpu;
132
133 return &cmap->map;
134free_percpu:
135 free_percpu(cmap->flush_needed);
136free_cmap:
137 kfree(cmap);
138 return ERR_PTR(err);
139}
140
141static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
142{
143 atomic_inc(&rcpu->refcnt);
144}
145
146
147static void cpu_map_kthread_stop(struct work_struct *work)
148{
149 struct bpf_cpu_map_entry *rcpu;
150
151 rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq);
152
153
154
155
156 rcu_barrier();
157
158
159 kthread_stop(rcpu->kthread);
160}
161
162static struct sk_buff *cpu_map_build_skb(struct bpf_cpu_map_entry *rcpu,
163 struct xdp_frame *xdpf,
164 struct sk_buff *skb)
165{
166 unsigned int hard_start_headroom;
167 unsigned int frame_size;
168 void *pkt_data_start;
169
170
171 hard_start_headroom = sizeof(struct xdp_frame) + xdpf->headroom;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 frame_size = SKB_DATA_ALIGN(xdpf->len + hard_start_headroom) +
191 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
192
193 pkt_data_start = xdpf->data - hard_start_headroom;
194 skb = build_skb_around(skb, pkt_data_start, frame_size);
195 if (unlikely(!skb))
196 return NULL;
197
198 skb_reserve(skb, hard_start_headroom);
199 __skb_put(skb, xdpf->len);
200 if (xdpf->metasize)
201 skb_metadata_set(skb, xdpf->metasize);
202
203
204 skb->protocol = eth_type_trans(skb, xdpf->dev_rx);
205
206
207
208
209
210
211
212
213 xdp_scrub_frame(xdpf);
214
215 return skb;
216}
217
218static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
219{
220
221
222
223
224
225 struct xdp_frame *xdpf;
226
227 while ((xdpf = ptr_ring_consume(ring)))
228 if (WARN_ON_ONCE(xdpf))
229 xdp_return_frame(xdpf);
230}
231
232static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
233{
234 if (atomic_dec_and_test(&rcpu->refcnt)) {
235
236 __cpu_map_ring_cleanup(rcpu->queue);
237 ptr_ring_cleanup(rcpu->queue, NULL);
238 kfree(rcpu->queue);
239 kfree(rcpu);
240 }
241}
242
243#define CPUMAP_BATCH 8
244
245static int cpu_map_kthread_run(void *data)
246{
247 struct bpf_cpu_map_entry *rcpu = data;
248
249 set_current_state(TASK_INTERRUPTIBLE);
250
251
252
253
254
255
256 while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
257 unsigned int drops = 0, sched = 0;
258 void *frames[CPUMAP_BATCH];
259 void *skbs[CPUMAP_BATCH];
260 gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
261 int i, n, m;
262
263
264 if (__ptr_ring_empty(rcpu->queue)) {
265 set_current_state(TASK_INTERRUPTIBLE);
266
267 if (__ptr_ring_empty(rcpu->queue)) {
268 schedule();
269 sched = 1;
270 } else {
271 __set_current_state(TASK_RUNNING);
272 }
273 } else {
274 sched = cond_resched();
275 }
276
277
278
279
280
281
282 n = ptr_ring_consume_batched(rcpu->queue, frames, CPUMAP_BATCH);
283
284 for (i = 0; i < n; i++) {
285 void *f = frames[i];
286 struct page *page = virt_to_page(f);
287
288
289
290
291
292 prefetchw(page);
293 }
294
295 m = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, n, skbs);
296 if (unlikely(m == 0)) {
297 for (i = 0; i < n; i++)
298 skbs[i] = NULL;
299 drops = n;
300 }
301
302 local_bh_disable();
303 for (i = 0; i < n; i++) {
304 struct xdp_frame *xdpf = frames[i];
305 struct sk_buff *skb = skbs[i];
306 int ret;
307
308 skb = cpu_map_build_skb(rcpu, xdpf, skb);
309 if (!skb) {
310 xdp_return_frame(xdpf);
311 continue;
312 }
313
314
315 ret = netif_receive_skb_core(skb);
316 if (ret == NET_RX_DROP)
317 drops++;
318 }
319
320 trace_xdp_cpumap_kthread(rcpu->map_id, n, drops, sched);
321
322 local_bh_enable();
323 }
324 __set_current_state(TASK_RUNNING);
325
326 put_cpu_map_entry(rcpu);
327 return 0;
328}
329
330static struct bpf_cpu_map_entry *__cpu_map_entry_alloc(u32 qsize, u32 cpu,
331 int map_id)
332{
333 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
334 struct bpf_cpu_map_entry *rcpu;
335 int numa, err;
336
337
338 numa = cpu_to_node(cpu);
339
340 rcpu = kzalloc_node(sizeof(*rcpu), gfp, numa);
341 if (!rcpu)
342 return NULL;
343
344
345 rcpu->bulkq = __alloc_percpu_gfp(sizeof(*rcpu->bulkq),
346 sizeof(void *), gfp);
347 if (!rcpu->bulkq)
348 goto free_rcu;
349
350
351 rcpu->queue = kzalloc_node(sizeof(*rcpu->queue), gfp, numa);
352 if (!rcpu->queue)
353 goto free_bulkq;
354
355 err = ptr_ring_init(rcpu->queue, qsize, gfp);
356 if (err)
357 goto free_queue;
358
359 rcpu->cpu = cpu;
360 rcpu->map_id = map_id;
361 rcpu->qsize = qsize;
362
363
364 rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
365 "cpumap/%d/map:%d", cpu, map_id);
366 if (IS_ERR(rcpu->kthread))
367 goto free_ptr_ring;
368
369 get_cpu_map_entry(rcpu);
370 get_cpu_map_entry(rcpu);
371
372
373 kthread_bind(rcpu->kthread, cpu);
374 wake_up_process(rcpu->kthread);
375
376 return rcpu;
377
378free_ptr_ring:
379 ptr_ring_cleanup(rcpu->queue, NULL);
380free_queue:
381 kfree(rcpu->queue);
382free_bulkq:
383 free_percpu(rcpu->bulkq);
384free_rcu:
385 kfree(rcpu);
386 return NULL;
387}
388
389static void __cpu_map_entry_free(struct rcu_head *rcu)
390{
391 struct bpf_cpu_map_entry *rcpu;
392 int cpu;
393
394
395
396
397
398
399 rcpu = container_of(rcu, struct bpf_cpu_map_entry, rcu);
400
401
402 for_each_online_cpu(cpu) {
403 struct xdp_bulk_queue *bq = per_cpu_ptr(rcpu->bulkq, cpu);
404
405
406 bq_flush_to_queue(rcpu, bq, false);
407 }
408 free_percpu(rcpu->bulkq);
409
410 put_cpu_map_entry(rcpu);
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
433 u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
434{
435 struct bpf_cpu_map_entry *old_rcpu;
436
437 old_rcpu = xchg(&cmap->cpu_map[key_cpu], rcpu);
438 if (old_rcpu) {
439 call_rcu(&old_rcpu->rcu, __cpu_map_entry_free);
440 INIT_WORK(&old_rcpu->kthread_stop_wq, cpu_map_kthread_stop);
441 schedule_work(&old_rcpu->kthread_stop_wq);
442 }
443}
444
445static int cpu_map_delete_elem(struct bpf_map *map, void *key)
446{
447 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
448 u32 key_cpu = *(u32 *)key;
449
450 if (key_cpu >= map->max_entries)
451 return -EINVAL;
452
453
454 __cpu_map_entry_replace(cmap, key_cpu, NULL);
455 return 0;
456}
457
458static int cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
459 u64 map_flags)
460{
461 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
462 struct bpf_cpu_map_entry *rcpu;
463
464
465 u32 key_cpu = *(u32 *)key;
466
467 u32 qsize = *(u32 *)value;
468
469 if (unlikely(map_flags > BPF_EXIST))
470 return -EINVAL;
471 if (unlikely(key_cpu >= cmap->map.max_entries))
472 return -E2BIG;
473 if (unlikely(map_flags == BPF_NOEXIST))
474 return -EEXIST;
475 if (unlikely(qsize > 16384))
476 return -EOVERFLOW;
477
478
479 if (!cpu_possible(key_cpu))
480 return -ENODEV;
481
482 if (qsize == 0) {
483 rcpu = NULL;
484 } else {
485
486 rcpu = __cpu_map_entry_alloc(qsize, key_cpu, map->id);
487 if (!rcpu)
488 return -ENOMEM;
489 }
490 rcu_read_lock();
491 __cpu_map_entry_replace(cmap, key_cpu, rcpu);
492 rcu_read_unlock();
493 return 0;
494}
495
496static void cpu_map_free(struct bpf_map *map)
497{
498 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
499 int cpu;
500 u32 i;
501
502
503
504
505
506
507
508
509
510
511 bpf_clear_redirect_map(map);
512 synchronize_rcu();
513
514
515
516
517
518
519 for_each_online_cpu(cpu) {
520 unsigned long *bitmap = per_cpu_ptr(cmap->flush_needed, cpu);
521
522 while (!bitmap_empty(bitmap, cmap->map.max_entries))
523 cond_resched();
524 }
525
526
527
528
529 for (i = 0; i < cmap->map.max_entries; i++) {
530 struct bpf_cpu_map_entry *rcpu;
531
532 rcpu = READ_ONCE(cmap->cpu_map[i]);
533 if (!rcpu)
534 continue;
535
536
537 __cpu_map_entry_replace(cmap, i, NULL);
538 }
539 free_percpu(cmap->flush_needed);
540 bpf_map_area_free(cmap->cpu_map);
541 kfree(cmap);
542}
543
544struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
545{
546 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
547 struct bpf_cpu_map_entry *rcpu;
548
549 if (key >= map->max_entries)
550 return NULL;
551
552 rcpu = READ_ONCE(cmap->cpu_map[key]);
553 return rcpu;
554}
555
556static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
557{
558 struct bpf_cpu_map_entry *rcpu =
559 __cpu_map_lookup_elem(map, *(u32 *)key);
560
561 return rcpu ? &rcpu->qsize : NULL;
562}
563
564static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
565{
566 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
567 u32 index = key ? *(u32 *)key : U32_MAX;
568 u32 *next = next_key;
569
570 if (index >= cmap->map.max_entries) {
571 *next = 0;
572 return 0;
573 }
574
575 if (index == cmap->map.max_entries - 1)
576 return -ENOENT;
577 *next = index + 1;
578 return 0;
579}
580
581const struct bpf_map_ops cpu_map_ops = {
582 .map_alloc = cpu_map_alloc,
583 .map_free = cpu_map_free,
584 .map_delete_elem = cpu_map_delete_elem,
585 .map_update_elem = cpu_map_update_elem,
586 .map_lookup_elem = cpu_map_lookup_elem,
587 .map_get_next_key = cpu_map_get_next_key,
588 .map_check_btf = map_check_no_btf,
589};
590
591static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,
592 struct xdp_bulk_queue *bq, bool in_napi_ctx)
593{
594 unsigned int processed = 0, drops = 0;
595 const int to_cpu = rcpu->cpu;
596 struct ptr_ring *q;
597 int i;
598
599 if (unlikely(!bq->count))
600 return 0;
601
602 q = rcpu->queue;
603 spin_lock(&q->producer_lock);
604
605 for (i = 0; i < bq->count; i++) {
606 struct xdp_frame *xdpf = bq->q[i];
607 int err;
608
609 err = __ptr_ring_produce(q, xdpf);
610 if (err) {
611 drops++;
612 if (likely(in_napi_ctx))
613 xdp_return_frame_rx_napi(xdpf);
614 else
615 xdp_return_frame(xdpf);
616 }
617 processed++;
618 }
619 bq->count = 0;
620 spin_unlock(&q->producer_lock);
621
622
623 trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
624 return 0;
625}
626
627
628
629
630static int bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
631{
632 struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq);
633
634 if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
635 bq_flush_to_queue(rcpu, bq, true);
636
637
638
639
640
641
642
643
644
645
646 bq->q[bq->count++] = xdpf;
647 return 0;
648}
649
650int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
651 struct net_device *dev_rx)
652{
653 struct xdp_frame *xdpf;
654
655 xdpf = convert_to_xdp_frame(xdp);
656 if (unlikely(!xdpf))
657 return -EOVERFLOW;
658
659
660 xdpf->dev_rx = dev_rx;
661
662 bq_enqueue(rcpu, xdpf);
663 return 0;
664}
665
666void __cpu_map_insert_ctx(struct bpf_map *map, u32 bit)
667{
668 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
669 unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed);
670
671 __set_bit(bit, bitmap);
672}
673
674void __cpu_map_flush(struct bpf_map *map)
675{
676 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
677 unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed);
678 u32 bit;
679
680
681
682
683
684 for_each_set_bit(bit, bitmap, map->max_entries) {
685 struct bpf_cpu_map_entry *rcpu = READ_ONCE(cmap->cpu_map[bit]);
686 struct xdp_bulk_queue *bq;
687
688
689
690
691 if (unlikely(!rcpu))
692 continue;
693
694 __clear_bit(bit, bitmap);
695
696
697 bq = this_cpu_ptr(rcpu->bulkq);
698 bq_flush_to_queue(rcpu, bq, true);
699
700
701 wake_up_process(rcpu->kthread);
702 }
703}
704