blob: 734d9b5f577a8036594251ced6e1af265fbc1c6a [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;
11} ____cacheline_aligned_in_smp;
12
13struct gro_cells {
14 unsigned int gro_cells_mask;
15 struct gro_cell *cells;
16};
17
18static inline void gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
19{
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000020 struct gro_cell *cell = gcells->cells;
21 struct net_device *dev = skb->dev;
22
23 if (!cell || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) {
24 netif_rx(skb);
25 return;
26 }
27
28 if (skb_rx_queue_recorded(skb))
29 cell += skb_get_rx_queue(skb) & gcells->gro_cells_mask;
30
31 if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
32 atomic_long_inc(&dev->rx_dropped);
33 kfree_skb(skb);
34 return;
35 }
36
Eric Dumazetf8e8f972012-12-10 12:32:03 +000037 /* We run in BH context */
38 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000039
40 __skb_queue_tail(&cell->napi_skbs, skb);
41 if (skb_queue_len(&cell->napi_skbs) == 1)
42 napi_schedule(&cell->napi);
43
Eric Dumazetf8e8f972012-12-10 12:32:03 +000044 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000045}
46
Eric Dumazetf8e8f972012-12-10 12:32:03 +000047/* called unser BH context */
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000048static inline int gro_cell_poll(struct napi_struct *napi, int budget)
49{
50 struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
51 struct sk_buff *skb;
52 int work_done = 0;
53
Eric Dumazetf8e8f972012-12-10 12:32:03 +000054 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000055 while (work_done < budget) {
Eric Dumazetf8e8f972012-12-10 12:32:03 +000056 skb = __skb_dequeue(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000057 if (!skb)
58 break;
Eric Dumazetf8e8f972012-12-10 12:32:03 +000059 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000060 napi_gro_receive(napi, skb);
61 work_done++;
Eric Dumazetf8e8f972012-12-10 12:32:03 +000062 spin_lock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000063 }
64
65 if (work_done < budget)
66 napi_complete(napi);
Eric Dumazetf8e8f972012-12-10 12:32:03 +000067 spin_unlock(&cell->napi_skbs.lock);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000068 return work_done;
69}
70
71static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
72{
73 int i;
74
75 gcells->gro_cells_mask = roundup_pow_of_two(netif_get_num_default_rss_queues()) - 1;
Joe Perchesa1b1add2013-01-26 09:24:19 +000076 gcells->cells = kcalloc(gcells->gro_cells_mask + 1,
77 sizeof(struct gro_cell),
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000078 GFP_KERNEL);
79 if (!gcells->cells)
80 return -ENOMEM;
81
82 for (i = 0; i <= gcells->gro_cells_mask; i++) {
83 struct gro_cell *cell = gcells->cells + i;
84
85 skb_queue_head_init(&cell->napi_skbs);
86 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
87 napi_enable(&cell->napi);
88 }
89 return 0;
90}
91
92static inline void gro_cells_destroy(struct gro_cells *gcells)
93{
94 struct gro_cell *cell = gcells->cells;
95 int i;
96
97 if (!cell)
98 return;
99 for (i = 0; i <= gcells->gro_cells_mask; i++,cell++) {
100 netif_napi_del(&cell->napi);
101 skb_queue_purge(&cell->napi_skbs);
102 }
103 kfree(gcells->cells);
104 gcells->cells = NULL;
105}
106
107#endif