blob: cf6c74550baa53af7bfe6a8c0079174a85ac917d [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 Dumazetc9e6bc62012-09-27 19:29:05 +000035 __skb_queue_tail(&cell->napi_skbs, skb);
36 if (skb_queue_len(&cell->napi_skbs) == 1)
37 napi_schedule(&cell->napi);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000038}
39
Eric Dumazetc42858e2015-08-31 13:57:34 -070040/* called under BH context */
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000041static inline int gro_cell_poll(struct napi_struct *napi, int budget)
42{
43 struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
44 struct sk_buff *skb;
45 int work_done = 0;
46
47 while (work_done < budget) {
Eric Dumazetf8e8f972012-12-10 12:32:03 +000048 skb = __skb_dequeue(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000049 if (!skb)
50 break;
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000051 napi_gro_receive(napi, skb);
52 work_done++;
53 }
54
55 if (work_done < budget)
Eric Dumazetc42858e2015-08-31 13:57:34 -070056 napi_complete_done(napi, work_done);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000057 return work_done;
58}
59
60static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
61{
62 int i;
63
Martin KaFai Lau88340162015-01-16 10:11:00 -080064 gcells->cells = alloc_percpu(struct gro_cell);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000065 if (!gcells->cells)
66 return -ENOMEM;
67
Martin KaFai Lau88340162015-01-16 10:11:00 -080068 for_each_possible_cpu(i) {
69 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000070
Eric Dumazetc42858e2015-08-31 13:57:34 -070071 __skb_queue_head_init(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000072 netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
73 napi_enable(&cell->napi);
74 }
75 return 0;
76}
77
78static inline void gro_cells_destroy(struct gro_cells *gcells)
79{
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000080 int i;
81
Martin KaFai Lau88340162015-01-16 10:11:00 -080082 if (!gcells->cells)
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000083 return;
Martin KaFai Lau88340162015-01-16 10:11:00 -080084 for_each_possible_cpu(i) {
85 struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
Eric Dumazetc42858e2015-08-31 13:57:34 -070086
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000087 netif_napi_del(&cell->napi);
Eric Dumazetc42858e2015-08-31 13:57:34 -070088 __skb_queue_purge(&cell->napi_skbs);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000089 }
Martin KaFai Lau88340162015-01-16 10:11:00 -080090 free_percpu(gcells->cells);
Eric Dumazetc9e6bc62012-09-27 19:29:05 +000091 gcells->cells = NULL;
92}
93
94#endif