gro_cells.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/skbuff.h>
  3. #include <linux/slab.h>
  4. #include <linux/netdevice.h>
  5. #include <net/gro_cells.h>
  6. #include <net/hotdata.h>
  7. struct gro_cell {
  8. struct sk_buff_head napi_skbs;
  9. struct napi_struct napi;
  10. };
  11. int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
  12. {
  13. struct net_device *dev = skb->dev;
  14. struct gro_cell *cell;
  15. int res;
  16. rcu_read_lock();
  17. if (unlikely(!(dev->flags & IFF_UP)))
  18. goto drop;
  19. if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
  20. res = netif_rx(skb);
  21. goto unlock;
  22. }
  23. cell = this_cpu_ptr(gcells->cells);
  24. if (skb_queue_len(&cell->napi_skbs) > READ_ONCE(net_hotdata.max_backlog)) {
  25. drop:
  26. dev_core_stats_rx_dropped_inc(dev);
  27. kfree_skb(skb);
  28. res = NET_RX_DROP;
  29. goto unlock;
  30. }
  31. __skb_queue_tail(&cell->napi_skbs, skb);
  32. if (skb_queue_len(&cell->napi_skbs) == 1)
  33. napi_schedule(&cell->napi);
  34. res = NET_RX_SUCCESS;
  35. unlock:
  36. rcu_read_unlock();
  37. return res;
  38. }
  39. EXPORT_SYMBOL(gro_cells_receive);
  40. /* called under BH context */
  41. static 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. while (work_done < budget) {
  47. skb = __skb_dequeue(&cell->napi_skbs);
  48. if (!skb)
  49. break;
  50. napi_gro_receive(napi, skb);
  51. work_done++;
  52. }
  53. if (work_done < budget)
  54. napi_complete_done(napi, work_done);
  55. return work_done;
  56. }
  57. int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
  58. {
  59. int i;
  60. gcells->cells = alloc_percpu(struct gro_cell);
  61. if (!gcells->cells)
  62. return -ENOMEM;
  63. for_each_possible_cpu(i) {
  64. struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
  65. __skb_queue_head_init(&cell->napi_skbs);
  66. set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
  67. netif_napi_add(dev, &cell->napi, gro_cell_poll);
  68. napi_enable(&cell->napi);
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(gro_cells_init);
  73. struct percpu_free_defer {
  74. struct rcu_head rcu;
  75. void __percpu *ptr;
  76. };
  77. static void percpu_free_defer_callback(struct rcu_head *head)
  78. {
  79. struct percpu_free_defer *defer;
  80. defer = container_of(head, struct percpu_free_defer, rcu);
  81. free_percpu(defer->ptr);
  82. kfree(defer);
  83. }
  84. void gro_cells_destroy(struct gro_cells *gcells)
  85. {
  86. struct percpu_free_defer *defer;
  87. int i;
  88. if (!gcells->cells)
  89. return;
  90. for_each_possible_cpu(i) {
  91. struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
  92. napi_disable(&cell->napi);
  93. __netif_napi_del(&cell->napi);
  94. __skb_queue_purge(&cell->napi_skbs);
  95. }
  96. /* We need to observe an rcu grace period before freeing ->cells,
  97. * because netpoll could access dev->napi_list under rcu protection.
  98. * Try hard using call_rcu() instead of synchronize_rcu(),
  99. * because we might be called from cleanup_net(), and we
  100. * definitely do not want to block this critical task.
  101. */
  102. defer = kmalloc(sizeof(*defer), GFP_KERNEL | __GFP_NOWARN);
  103. if (likely(defer)) {
  104. defer->ptr = gcells->cells;
  105. call_rcu(&defer->rcu, percpu_free_defer_callback);
  106. } else {
  107. /* We do not hold RTNL at this point, synchronize_net()
  108. * would not be able to expedite this sync.
  109. */
  110. synchronize_rcu_expedited();
  111. free_percpu(gcells->cells);
  112. }
  113. gcells->cells = NULL;
  114. }
  115. EXPORT_SYMBOL(gro_cells_destroy);