| 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> |
| 7 |
|
| 8 |
struct gro_cell { |
| 9 |
struct sk_buff_head napi_skbs; |
| 10 |
struct napi_struct napi; |
| 11 |
local_lock_t bh_lock; |
| 12 |
}; |
| 13 |
|
| 14 |
int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) |
| 15 |
{ |
| 16 |
struct net_device *dev = skb->dev; |
| 17 |
bool have_bh_lock = false; |
| 18 |
struct gro_cell *cell; |
| 19 |
int res; |
| 20 |
|
| 21 |
rcu_read_lock(); |
| 22 |
if (unlikely(!(dev->flags & IFF_UP))) |
| 23 |
goto drop; |
| 24 |
|
| 25 |
if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) { |
| 26 |
res = netif_rx(skb); |
| 27 |
goto unlock; |
| 28 |
} |
| 29 |
|
| 30 |
local_lock_nested_bh(&gcells->cells->bh_lock); |
| 31 |
have_bh_lock = true; |
| 32 |
cell = this_cpu_ptr(gcells->cells); |
| 33 |
|
| 34 |
if (skb_queue_len(&cell->napi_skbs) > READ_ONCE(net_hotdata.max_backlog)) { |
| 35 |
drop: |
| 36 |
dev_core_stats_rx_dropped_inc(dev); |
| 37 |
kfree_skb(skb); |
| 38 |
res = NET_RX_DROP; |
| 39 |
goto unlock; |
| 40 |
} |
| 41 |
|
| 42 |
__skb_queue_tail(&cell->napi_skbs, skb); |
| 43 |
if (skb_queue_len(&cell->napi_skbs) == 1) |
| 44 |
napi_schedule(&cell->napi); |
| 45 |
|
| 46 |
res = NET_RX_SUCCESS; |
| 47 |
|
| 48 |
unlock: |
| 49 |
if (have_bh_lock) |
| 50 |
local_unlock_nested_bh(&gcells->cells->bh_lock); |
| 51 |
rcu_read_unlock(); |
| 52 |
return res; |
| 53 |
} |
| 54 |
EXPORT_SYMBOL(gro_cells_receive); |
| 55 |
|
| 56 |
/* called under BH context */ |
| 57 |
static int gro_cell_poll(struct napi_struct *napi, int budget) |
| 58 |
{ |
| 59 |
struct gro_cell *cell = container_of(napi, struct gro_cell, napi); |
| 60 |
struct sk_buff *skb; |
| 61 |
int work_done = 0; |
| 62 |
|
| 63 |
while (work_done < budget) { |
| 64 |
__local_lock_nested_bh(&cell->bh_lock); |
| 65 |
skb = __skb_dequeue(&cell->napi_skbs); |
| 66 |
__local_unlock_nested_bh(&cell->bh_lock); |
| 67 |
if (!skb) |
| 68 |
break; |
| 69 |
napi_gro_receive(napi, skb); |
| 70 |
work_done++; |
| 71 |
} |
| 72 |
|
| 73 |
if (work_done < budget) |
| 74 |
napi_complete_done(napi, work_done); |
| 75 |
return work_done; |
| 76 |
} |
| 77 |
|
| 78 |
int gro_cells_init(struct gro_cells *gcells, struct net_device *dev) |
| 79 |
{ |
| 80 |
int i; |
| 81 |
|
| 82 |
gcells->cells = alloc_percpu(struct gro_cell); |
| 83 |
if (!gcells->cells) |
| 84 |
return -ENOMEM; |
| 85 |
|
| 86 |
for_each_possible_cpu(i) { |
| 87 |
struct gro_cell *cell = per_cpu_ptr(gcells->cells, i); |
| 88 |
|
| 89 |
__skb_queue_head_init(&cell->napi_skbs); |
| 90 |
local_lock_init(&cell->bh_lock); |
| 91 |
|
| 92 |
set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state); |
| 93 |
|
| 94 |
netif_napi_add(dev, &cell->napi, gro_cell_poll); |
| 95 |
napi_enable(&cell->napi); |
| 96 |
} |
| 97 |
return 0; |
| 98 |
} |
| 99 |
EXPORT_SYMBOL(gro_cells_init); |
| 100 |
|
| 101 |
struct percpu_free_defer { |
| 102 |
struct rcu_head rcu; |
| 103 |
void __percpu *ptr; |
| 104 |
}; |
| 105 |
|
| 106 |
static void percpu_free_defer_callback(struct rcu_head *head) |
| 107 |
{ |
| 108 |
struct percpu_free_defer *defer; |
| 109 |
|
| 110 |
defer = container_of(head, struct percpu_free_defer, rcu); |
| 111 |
free_percpu(defer->ptr); |
| 112 |
kfree(defer); |
| 113 |
} |
| 114 |
|
| 115 |
void gro_cells_destroy(struct gro_cells *gcells) |
| 116 |
{ |
| 117 |
struct percpu_free_defer *defer; |
| 118 |
int i; |
| 119 |
|
| 120 |
if (!gcells->cells) |
| 121 |
return; |
| 122 |
for_each_possible_cpu(i) { |
| 123 |
struct gro_cell *cell = per_cpu_ptr(gcells->cells, i); |
| 124 |
|
| 125 |
napi_disable(&cell->napi); |
| 126 |
__netif_napi_del(&cell->napi); |
| 127 |
__skb_queue_purge(&cell->napi_skbs); |
| 128 |
} |
| 129 |
/* We need to observe an rcu grace period before freeing ->cells, |
| 130 |
* because netpoll could access dev->napi_list under rcu protection. |
| 131 |
* Try hard using call_rcu() instead of synchronize_rcu(), |
| 132 |
* because we might be called from cleanup_net(), and we |
| 133 |
* definitely do not want to block this critical task. |
| 134 |
*/ |
| 135 |
defer = kmalloc_obj(*defer, GFP_KERNEL | __GFP_NOWARN); |
| 136 |
if (likely(defer)) { |
| 137 |
defer->ptr = gcells->cells; |
| 138 |
call_rcu(&defer->rcu, percpu_free_defer_callback); |
| 139 |
} else { |
| 140 |
/* We do not hold RTNL at this point, synchronize_net() |
| 141 |
* would not be able to expedite this sync. |
| 142 |
*/ |
| 143 |
synchronize_rcu_expedited(); |
| 144 |
free_percpu(gcells->cells); |
| 145 |
} |
| 146 |
gcells->cells = NULL; |
| 147 |
} |
| 148 |
EXPORT_SYMBOL(gro_cells_destroy); |