Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 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; |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 11 | }; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 12 | |
| 13 | struct gro_cells { |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 14 | struct gro_cell __percpu *cells; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 15 | }; |
| 16 | |
Paolo Abeni | 5f652bb | 2016-07-20 18:11:31 +0200 | [diff] [blame] | 17 | static inline int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 18 | { |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 19 | struct gro_cell *cell; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 20 | struct net_device *dev = skb->dev; |
| 21 | |
Paolo Abeni | 5f652bb | 2016-07-20 18:11:31 +0200 | [diff] [blame] | 22 | if (!gcells->cells || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) |
| 23 | return netif_rx(skb); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 24 | |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 25 | cell = this_cpu_ptr(gcells->cells); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 26 | |
| 27 | if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) { |
| 28 | atomic_long_inc(&dev->rx_dropped); |
| 29 | kfree_skb(skb); |
Paolo Abeni | 5f652bb | 2016-07-20 18:11:31 +0200 | [diff] [blame] | 30 | return NET_RX_DROP; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 33 | __skb_queue_tail(&cell->napi_skbs, skb); |
| 34 | if (skb_queue_len(&cell->napi_skbs) == 1) |
| 35 | napi_schedule(&cell->napi); |
Paolo Abeni | 5f652bb | 2016-07-20 18:11:31 +0200 | [diff] [blame] | 36 | return NET_RX_SUCCESS; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Eric Dumazet | c42858e | 2015-08-31 13:57:34 -0700 | [diff] [blame] | 39 | /* called under BH context */ |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 40 | static 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 Dumazet | f8e8f97 | 2012-12-10 12:32:03 +0000 | [diff] [blame] | 47 | skb = __skb_dequeue(&cell->napi_skbs); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 48 | if (!skb) |
| 49 | break; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 50 | napi_gro_receive(napi, skb); |
| 51 | work_done++; |
| 52 | } |
| 53 | |
| 54 | if (work_done < budget) |
Eric Dumazet | c42858e | 2015-08-31 13:57:34 -0700 | [diff] [blame] | 55 | napi_complete_done(napi, work_done); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 56 | return work_done; |
| 57 | } |
| 58 | |
| 59 | static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev) |
| 60 | { |
| 61 | int i; |
| 62 | |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 63 | gcells->cells = alloc_percpu(struct gro_cell); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 64 | if (!gcells->cells) |
| 65 | return -ENOMEM; |
| 66 | |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 67 | for_each_possible_cpu(i) { |
| 68 | struct gro_cell *cell = per_cpu_ptr(gcells->cells, i); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 69 | |
Eric Dumazet | c42858e | 2015-08-31 13:57:34 -0700 | [diff] [blame] | 70 | __skb_queue_head_init(&cell->napi_skbs); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 71 | netif_napi_add(dev, &cell->napi, gro_cell_poll, 64); |
| 72 | napi_enable(&cell->napi); |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static inline void gro_cells_destroy(struct gro_cells *gcells) |
| 78 | { |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 79 | int i; |
| 80 | |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 81 | if (!gcells->cells) |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 82 | return; |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 83 | for_each_possible_cpu(i) { |
| 84 | struct gro_cell *cell = per_cpu_ptr(gcells->cells, i); |
Eric Dumazet | c42858e | 2015-08-31 13:57:34 -0700 | [diff] [blame] | 85 | |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 86 | netif_napi_del(&cell->napi); |
Eric Dumazet | c42858e | 2015-08-31 13:57:34 -0700 | [diff] [blame] | 87 | __skb_queue_purge(&cell->napi_skbs); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 88 | } |
Martin KaFai Lau | 8834016 | 2015-01-16 10:11:00 -0800 | [diff] [blame] | 89 | free_percpu(gcells->cells); |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 90 | gcells->cells = NULL; |
| 91 | } |
| 92 | |
| 93 | #endif |