blob: 6db0e8534127d28458664fd89c9cc4ec8e20908f [file] [log] [blame]
Eric Dumazetc9e6bc62012-09-27 19:29:05 +00001#ifndef _NET_GRO_CELLS_H
2#define _NET_GRO_CELLS_H
3
4#include <linux/skbuff.h>
5#include <linux/slab.h>
6#include <linux/netdevice.h>
7
8struct gro_cell {
9 struct sk_buff_head napi_skbs;
10 struct napi_struct napi;
Martin KaFai Lau88340162015-01-16 10:11:00 -080011};
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000012
13struct gro_cells {
Martin KaFai Lau88340162015-01-16 10:11:00 -080014 struct gro_cell __percpu *cells;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000015};
16
Paolo Abeni5f652bb2016-07-20 18:11:31 +020017static inline int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000018{
Martin KaFai Lau88340162015-01-16 10:11:00 -080019 struct gro_cell *cell;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000020 struct net_device *dev = skb->dev;
Eric Dumazet7cbb0ab2019-03-10 10:39:37 -070021 int res;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000022
Eric Dumazet7cbb0ab2019-03-10 10:39:37 -070023 rcu_read_lock();
24 if (unlikely(!(dev->flags & IFF_UP)))
25 goto drop;
26
27 if (!gcells->cells || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) {
28 res = netif_rx(skb);
29 goto unlock;
30 }
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000031
Martin KaFai Lau88340162015-01-16 10:11:00 -080032 cell = this_cpu_ptr(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000033
34 if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
Eric Dumazet7cbb0ab2019-03-10 10:39:37 -070035drop:
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000036 atomic_long_inc(&dev->rx_dropped);
37 kfree_skb(skb);
Eric Dumazet7cbb0ab2019-03-10 10:39:37 -070038 res = NET_RX_DROP;
39 goto unlock;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000040 }
41
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000042 __skb_queue_tail(&cell->napi_skbs, skb);
43 if (skb_queue_len(&cell->napi_skbs) == 1)
44 napi_schedule(&cell->napi);
Eric Dumazet7cbb0ab2019-03-10 10:39:37 -070045
46 res = NET_RX_SUCCESS;
47
48unlock:
49 rcu_read_unlock();
50 return res;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000051}
52
Eric Dumazetc42858e2015-08-31 13:57:34 -070053/* called under BH context */
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000054static inline int gro_cell_poll(struct napi_struct *napi, int budget)
55{
56 struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
57 struct sk_buff *skb;
58 int work_done = 0;
59
60 while (work_done < budget) {
Eric Dumazetf8e8f972012-12-10 12:32:03 +000061 skb = __skb_dequeue(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000062 if (!skb)
63 break;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000064 napi_gro_receive(napi, skb);
65 work_done++;
66 }
67
68 if (work_done < budget)
Eric Dumazetc42858e2015-08-31 13:57:34 -070069 napi_complete_done(napi, work_done);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000070 return work_done;
71}
72
73static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
74{
75 int i;
76
Martin KaFai Lau88340162015-01-16 10:11:00 -080077 gcells->cells = alloc_percpu(struct gro_cell);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000078 if (!gcells->cells)
79 return -ENOMEM;
80
Martin KaFai Lau88340162015-01-16 10:11:00 -080081 for_each_possible_cpu(i) {
82 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000083
Eric Dumazetc42858e2015-08-31 13:57:34 -070084 __skb_queue_head_init(&cell->napi_skbs);
Eric Dumazete88a2762016-11-14 16:28:42 -080085
86 set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
87
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000088 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
89 napi_enable(&cell->napi);
90 }
91 return 0;
92}
93
94static inline void gro_cells_destroy(struct gro_cells *gcells)
95{
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000096 int i;
97
Martin KaFai Lau88340162015-01-16 10:11:00 -080098 if (!gcells->cells)
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000099 return;
Martin KaFai Lau88340162015-01-16 10:11:00 -0800100 for_each_possible_cpu(i) {
101 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc42858e2015-08-31 13:57:34 -0700102
Lorenzo Bianconi52f563e2018-12-19 23:23:00 +0100103 napi_disable(&cell->napi);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +0000104 netif_napi_del(&cell->napi);
Eric Dumazetc42858e2015-08-31 13:57:34 -0700105 __skb_queue_purge(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +0000106 }
Martin KaFai Lau88340162015-01-16 10:11:00 -0800107 free_percpu(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +0000108 gcells->cells = NULL;
109}
110
111#endif