gro_cells.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef _NET_GRO_CELLS_H
  2. #define _NET_GRO_CELLS_H
  3. #include <linux/skbuff.h>
  4. #include <linux/slab.h>
  5. #include <linux/netdevice.h>
  6. struct gro_cell {
  7. struct sk_buff_head napi_skbs;
  8. struct napi_struct napi;
  9. };
  10. struct gro_cells {
  11. struct gro_cell __percpu *cells;
  12. };
  13. static inline int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
  14. {
  15. struct gro_cell *cell;
  16. struct net_device *dev = skb->dev;
  17. if (!gcells->cells || skb_cloned(skb) || !(dev->features & NETIF_F_GRO))
  18. return netif_rx(skb);
  19. cell = this_cpu_ptr(gcells->cells);
  20. if (skb_queue_len(&cell->napi_skbs) > netdev_max_backlog) {
  21. atomic_long_inc(&dev->rx_dropped);
  22. kfree_skb(skb);
  23. return NET_RX_DROP;
  24. }
  25. __skb_queue_tail(&cell->napi_skbs, skb);
  26. if (skb_queue_len(&cell->napi_skbs) == 1)
  27. napi_schedule(&cell->napi);
  28. return NET_RX_SUCCESS;
  29. }
  30. /* called under BH context */
  31. static inline int gro_cell_poll(struct napi_struct *napi, int budget)
  32. {
  33. struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
  34. struct sk_buff *skb;
  35. int work_done = 0;
  36. while (work_done < budget) {
  37. skb = __skb_dequeue(&cell->napi_skbs);
  38. if (!skb)
  39. break;
  40. napi_gro_receive(napi, skb);
  41. work_done++;
  42. }
  43. if (work_done < budget)
  44. napi_complete_done(napi, work_done);
  45. return work_done;
  46. }
  47. static inline int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
  48. {
  49. int i;
  50. gcells->cells = alloc_percpu(struct gro_cell);
  51. if (!gcells->cells)
  52. return -ENOMEM;
  53. for_each_possible_cpu(i) {
  54. struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
  55. __skb_queue_head_init(&cell->napi_skbs);
  56. set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
  57. netif_napi_add(dev, &cell->napi, gro_cell_poll, 64);
  58. napi_enable(&cell->napi);
  59. }
  60. return 0;
  61. }
  62. static inline void gro_cells_destroy(struct gro_cells *gcells)
  63. {
  64. int i;
  65. if (!gcells->cells)
  66. return;
  67. for_each_possible_cpu(i) {
  68. struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
  69. netif_napi_del(&cell->napi);
  70. __skb_queue_purge(&cell->napi_skbs);
  71. }
  72. free_percpu(gcells->cells);
  73. gcells->cells = NULL;
  74. }
  75. #endif