blob: 2a1abbf8da74368cd01adc40cef6c0644e059ef2 [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;
21
Paolo Abeni5f652bb2016-07-20 18:11:31 +020022 if (!gcells->cells || skb_cloned(skb) || !(dev->features & NETIF_F_GRO))
23 return netif_rx(skb);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000024
Martin KaFai Lau88340162015-01-16 10:11:00 -080025 cell = this_cpu_ptr(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000026
27 if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
28 atomic_long_inc(&dev->rx_dropped);
29 kfree_skb(skb);
Paolo Abeni5f652bb2016-07-20 18:11:31 +020030 return NET_RX_DROP;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000031 }
32
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000033 __skb_queue_tail(&cell->napi_skbs, skb);
34 if (skb_queue_len(&cell->napi_skbs) == 1)
35 napi_schedule(&cell->napi);
Paolo Abeni5f652bb2016-07-20 18:11:31 +020036 return NET_RX_SUCCESS;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000037}
38
Eric Dumazetc42858e2015-08-31 13:57:34 -070039/* called under BH context */
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000040static inline int gro_cell_poll(struct napi_struct *napi, int budget)
41{
42 struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
43 struct sk_buff *skb;
44 int work_done = 0;
45
46 while (work_done < budget) {
Eric Dumazetf8e8f972012-12-10 12:32:03 +000047 skb = __skb_dequeue(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000048 if (!skb)
49 break;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000050 napi_gro_receive(napi, skb);
51 work_done++;
52 }
53
54 if (work_done < budget)
Eric Dumazetc42858e2015-08-31 13:57:34 -070055 napi_complete_done(napi, work_done);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000056 return work_done;
57}
58
59static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
60{
61 int i;
62
Martin KaFai Lau88340162015-01-16 10:11:00 -080063 gcells->cells = alloc_percpu(struct gro_cell);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000064 if (!gcells->cells)
65 return -ENOMEM;
66
Martin KaFai Lau88340162015-01-16 10:11:00 -080067 for_each_possible_cpu(i) {
68 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000069
Eric Dumazetc42858e2015-08-31 13:57:34 -070070 __skb_queue_head_init(&cell->napi_skbs);
Eric Dumazete88a2762016-11-14 16:28:42 -080071
72 set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
73
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000074 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
75 napi_enable(&cell->napi);
76 }
77 return 0;
78}
79
80static inline void gro_cells_destroy(struct gro_cells *gcells)
81{
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000082 int i;
83
Martin KaFai Lau88340162015-01-16 10:11:00 -080084 if (!gcells->cells)
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000085 return;
Martin KaFai Lau88340162015-01-16 10:11:00 -080086 for_each_possible_cpu(i) {
87 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc42858e2015-08-31 13:57:34 -070088
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000089 netif_napi_del(&cell->napi);
Eric Dumazetc42858e2015-08-31 13:57:34 -070090 __skb_queue_purge(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000091 }
Martin KaFai Lau88340162015-01-16 10:11:00 -080092 free_percpu(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000093 gcells->cells = NULL;
94}
95
96#endif