blob: 0f712c0bc0bf27a910ca224ba050de5e7135fb30 [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
17static inline void gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
18{
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
Martin KaFai Lau88340162015-01-16 10:11:00 -080022 if (!gcells->cells || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) {
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000023 netif_rx(skb);
24 return;
25 }
26
Martin KaFai Lau88340162015-01-16 10:11:00 -080027 cell = this_cpu_ptr(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000028
29 if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
30 atomic_long_inc(&dev->rx_dropped);
31 kfree_skb(skb);
32 return;
33 }
34
Eric Dumazetf8e8f972012-12-10 12:32:03 +000035 /* We run in BH context */
36 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000037
38 __skb_queue_tail(&cell->napi_skbs, skb);
39 if (skb_queue_len(&cell->napi_skbs) == 1)
40 napi_schedule(&cell->napi);
41
Eric Dumazetf8e8f972012-12-10 12:32:03 +000042 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000043}
44
Eric Dumazetf8e8f972012-12-10 12:32:03 +000045/* called unser BH context */
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000046static inline int gro_cell_poll(struct napi_struct *napi, int budget)
47{
48 struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
49 struct sk_buff *skb;
50 int work_done = 0;
51
Eric Dumazetf8e8f972012-12-10 12:32:03 +000052 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000053 while (work_done < budget) {
Eric Dumazetf8e8f972012-12-10 12:32:03 +000054 skb = __skb_dequeue(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000055 if (!skb)
56 break;
Eric Dumazetf8e8f972012-12-10 12:32:03 +000057 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000058 napi_gro_receive(napi, skb);
59 work_done++;
Eric Dumazetf8e8f972012-12-10 12:32:03 +000060 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000061 }
62
63 if (work_done < budget)
64 napi_complete(napi);
Eric Dumazetf8e8f972012-12-10 12:32:03 +000065 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000066 return work_done;
67}
68
69static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
70{
71 int i;
72
Martin KaFai Lau88340162015-01-16 10:11:00 -080073 gcells->cells = alloc_percpu(struct gro_cell);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000074 if (!gcells->cells)
75 return -ENOMEM;
76
Martin KaFai Lau88340162015-01-16 10:11:00 -080077 for_each_possible_cpu(i) {
78 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000079
80 skb_queue_head_init(&cell->napi_skbs);
81 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
82 napi_enable(&cell->napi);
83 }
84 return 0;
85}
86
87static inline void gro_cells_destroy(struct gro_cells *gcells)
88{
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000089 int i;
90
Martin KaFai Lau88340162015-01-16 10:11:00 -080091 if (!gcells->cells)
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000092 return;
Martin KaFai Lau88340162015-01-16 10:11:00 -080093 for_each_possible_cpu(i) {
94 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000095 netif_napi_del(&cell->napi);
96 skb_queue_purge(&cell->napi_skbs);
97 }
Martin KaFai Lau88340162015-01-16 10:11:00 -080098 free_percpu(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000099 gcells->cells = NULL;
100}
101
102#endif