GNU Linux-libre 4.9.309-gnu1
[releases.git] / include / net / gro_cells.h
1 #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
8 struct gro_cell {
9         struct sk_buff_head     napi_skbs;
10         struct napi_struct      napi;
11 };
12
13 struct gro_cells {
14         struct gro_cell __percpu        *cells;
15 };
16
17 static inline int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
18 {
19         struct gro_cell *cell;
20         struct net_device *dev = skb->dev;
21         int res;
22
23         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         }
31
32         cell = this_cpu_ptr(gcells->cells);
33
34         if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
35 drop:
36                 atomic_long_inc(&dev->rx_dropped);
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         rcu_read_unlock();
50         return res;
51 }
52
53 /* called under BH context */
54 static 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) {
61                 skb = __skb_dequeue(&cell->napi_skbs);
62                 if (!skb)
63                         break;
64                 napi_gro_receive(napi, skb);
65                 work_done++;
66         }
67
68         if (work_done < budget)
69                 napi_complete_done(napi, work_done);
70         return work_done;
71 }
72
73 static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
74 {
75         int i;
76
77         gcells->cells = alloc_percpu(struct gro_cell);
78         if (!gcells->cells)
79                 return -ENOMEM;
80
81         for_each_possible_cpu(i) {
82                 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
83
84                 __skb_queue_head_init(&cell->napi_skbs);
85
86                 set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
87
88                 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
89                 napi_enable(&cell->napi);
90         }
91         return 0;
92 }
93
94 static inline void gro_cells_destroy(struct gro_cells *gcells)
95 {
96         int i;
97
98         if (!gcells->cells)
99                 return;
100         for_each_possible_cpu(i) {
101                 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
102
103                 napi_disable(&cell->napi);
104                 netif_napi_del(&cell->napi);
105                 __skb_queue_purge(&cell->napi_skbs);
106         }
107         free_percpu(gcells->cells);
108         gcells->cells = NULL;
109 }
110
111 #endif