xref: /openbmc/linux/net/core/gro_cells.c (revision bbecb07f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/skbuff.h>
3 #include <linux/slab.h>
4 #include <linux/netdevice.h>
5 #include <net/gro_cells.h>
6 
7 struct gro_cell {
8 	struct sk_buff_head	napi_skbs;
9 	struct napi_struct	napi;
10 };
11 
12 int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
13 {
14 	struct net_device *dev = skb->dev;
15 	struct gro_cell *cell;
16 
17 	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev))
18 		return netif_rx(skb);
19 
20 	cell = this_cpu_ptr(gcells->cells);
21 
22 	if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
23 		atomic_long_inc(&dev->rx_dropped);
24 		kfree_skb(skb);
25 		return NET_RX_DROP;
26 	}
27 
28 	__skb_queue_tail(&cell->napi_skbs, skb);
29 	if (skb_queue_len(&cell->napi_skbs) == 1)
30 		napi_schedule(&cell->napi);
31 	return NET_RX_SUCCESS;
32 }
33 EXPORT_SYMBOL(gro_cells_receive);
34 
35 /* called under BH context */
36 static int gro_cell_poll(struct napi_struct *napi, int budget)
37 {
38 	struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
39 	struct sk_buff *skb;
40 	int work_done = 0;
41 
42 	while (work_done < budget) {
43 		skb = __skb_dequeue(&cell->napi_skbs);
44 		if (!skb)
45 			break;
46 		napi_gro_receive(napi, skb);
47 		work_done++;
48 	}
49 
50 	if (work_done < budget)
51 		napi_complete_done(napi, work_done);
52 	return work_done;
53 }
54 
55 int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
56 {
57 	int i;
58 
59 	gcells->cells = alloc_percpu(struct gro_cell);
60 	if (!gcells->cells)
61 		return -ENOMEM;
62 
63 	for_each_possible_cpu(i) {
64 		struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
65 
66 		__skb_queue_head_init(&cell->napi_skbs);
67 
68 		set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
69 
70 		netif_napi_add(dev, &cell->napi, gro_cell_poll,
71 			       NAPI_POLL_WEIGHT);
72 		napi_enable(&cell->napi);
73 	}
74 	return 0;
75 }
76 EXPORT_SYMBOL(gro_cells_init);
77 
78 void gro_cells_destroy(struct gro_cells *gcells)
79 {
80 	int i;
81 
82 	if (!gcells->cells)
83 		return;
84 	for_each_possible_cpu(i) {
85 		struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
86 
87 		netif_napi_del(&cell->napi);
88 		__skb_queue_purge(&cell->napi_skbs);
89 	}
90 	free_percpu(gcells->cells);
91 	gcells->cells = NULL;
92 }
93 EXPORT_SYMBOL(gro_cells_destroy);
94