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 #include <net/hotdata.h>
9 struct sk_buff_head napi_skbs
;
10 struct napi_struct napi
;
13 int gro_cells_receive(struct gro_cells
*gcells
, struct sk_buff
*skb
)
15 struct net_device
*dev
= skb
->dev
;
16 struct gro_cell
*cell
;
20 if (unlikely(!(dev
->flags
& IFF_UP
)))
23 if (!gcells
->cells
|| skb_cloned(skb
) || netif_elide_gro(dev
)) {
28 cell
= this_cpu_ptr(gcells
->cells
);
30 if (skb_queue_len(&cell
->napi_skbs
) > READ_ONCE(net_hotdata
.max_backlog
)) {
32 dev_core_stats_rx_dropped_inc(dev
);
38 __skb_queue_tail(&cell
->napi_skbs
, skb
);
39 if (skb_queue_len(&cell
->napi_skbs
) == 1)
40 napi_schedule(&cell
->napi
);
48 EXPORT_SYMBOL(gro_cells_receive
);
50 /* called under BH context */
51 static int gro_cell_poll(struct napi_struct
*napi
, int budget
)
53 struct gro_cell
*cell
= container_of(napi
, struct gro_cell
, napi
);
57 while (work_done
< budget
) {
58 skb
= __skb_dequeue(&cell
->napi_skbs
);
61 napi_gro_receive(napi
, skb
);
65 if (work_done
< budget
)
66 napi_complete_done(napi
, work_done
);
70 int gro_cells_init(struct gro_cells
*gcells
, struct net_device
*dev
)
74 gcells
->cells
= alloc_percpu(struct gro_cell
);
78 for_each_possible_cpu(i
) {
79 struct gro_cell
*cell
= per_cpu_ptr(gcells
->cells
, i
);
81 __skb_queue_head_init(&cell
->napi_skbs
);
83 set_bit(NAPI_STATE_NO_BUSY_POLL
, &cell
->napi
.state
);
85 netif_napi_add(dev
, &cell
->napi
, gro_cell_poll
);
86 napi_enable(&cell
->napi
);
90 EXPORT_SYMBOL(gro_cells_init
);
92 struct percpu_free_defer
{
97 static void percpu_free_defer_callback(struct rcu_head
*head
)
99 struct percpu_free_defer
*defer
;
101 defer
= container_of(head
, struct percpu_free_defer
, rcu
);
102 free_percpu(defer
->ptr
);
106 void gro_cells_destroy(struct gro_cells
*gcells
)
108 struct percpu_free_defer
*defer
;
113 for_each_possible_cpu(i
) {
114 struct gro_cell
*cell
= per_cpu_ptr(gcells
->cells
, i
);
116 napi_disable(&cell
->napi
);
117 __netif_napi_del(&cell
->napi
);
118 __skb_queue_purge(&cell
->napi_skbs
);
120 /* We need to observe an rcu grace period before freeing ->cells,
121 * because netpoll could access dev->napi_list under rcu protection.
122 * Try hard using call_rcu() instead of synchronize_rcu(),
123 * because we might be called from cleanup_net(), and we
124 * definitely do not want to block this critical task.
126 defer
= kmalloc(sizeof(*defer
), GFP_KERNEL
| __GFP_NOWARN
);
128 defer
->ptr
= gcells
->cells
;
129 call_rcu(&defer
->rcu
, percpu_free_defer_callback
);
131 /* We do not hold RTNL at this point, synchronize_net()
132 * would not be able to expedite this sync.
134 synchronize_rcu_expedited();
135 free_percpu(gcells
->cells
);
137 gcells
->cells
= NULL
;
139 EXPORT_SYMBOL(gro_cells_destroy
);