driver.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================
  3. Softnet Driver Issues
  4. =====================
  5. Probing guidelines
  6. ==================
  7. Address validation
  8. ------------------
  9. Any hardware layer address you obtain for your device should
  10. be verified. For example, for ethernet check it with
  11. linux/etherdevice.h:is_valid_ether_addr()
  12. Close/stop guidelines
  13. =====================
  14. Quiescence
  15. ----------
  16. After the ndo_stop routine has been called, the hardware must
  17. not receive or transmit any data. All in flight packets must
  18. be aborted. If necessary, poll or wait for completion of
  19. any reset commands.
  20. Auto-close
  21. ----------
  22. The ndo_stop routine will be called by unregister_netdevice
  23. if device is still UP.
  24. Transmit path guidelines
  25. ========================
  26. Stop queues in advance
  27. ----------------------
  28. The ndo_start_xmit method must not return NETDEV_TX_BUSY under
  29. any normal circumstances. It is considered a hard error unless
  30. there is no way your device can tell ahead of time when its
  31. transmit function will become busy.
  32. Instead it must maintain the queue properly. For example,
  33. for a driver implementing scatter-gather this means:
  34. .. code-block:: c
  35. static u32 drv_tx_avail(struct drv_ring *dr)
  36. {
  37. u32 used = READ_ONCE(dr->prod) - READ_ONCE(dr->cons);
  38. return dr->tx_ring_size - (used & bp->tx_ring_mask);
  39. }
  40. static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
  41. struct net_device *dev)
  42. {
  43. struct drv *dp = netdev_priv(dev);
  44. struct netdev_queue *txq;
  45. struct drv_ring *dr;
  46. int idx;
  47. idx = skb_get_queue_mapping(skb);
  48. dr = dp->tx_rings[idx];
  49. txq = netdev_get_tx_queue(dev, idx);
  50. //...
  51. /* This should be a very rare race - log it. */
  52. if (drv_tx_avail(dr) <= skb_shinfo(skb)->nr_frags + 1) {
  53. netif_stop_queue(dev);
  54. netdev_warn(dev, "Tx Ring full when queue awake!\n");
  55. return NETDEV_TX_BUSY;
  56. }
  57. //... queue packet to card ...
  58. netdev_tx_sent_queue(txq, skb->len);
  59. //... update tx producer index using WRITE_ONCE() ...
  60. if (!netif_txq_maybe_stop(txq, drv_tx_avail(dr),
  61. MAX_SKB_FRAGS + 1, 2 * MAX_SKB_FRAGS))
  62. dr->stats.stopped++;
  63. //...
  64. return NETDEV_TX_OK;
  65. }
  66. And then at the end of your TX reclamation event handling:
  67. .. code-block:: c
  68. //... update tx consumer index using WRITE_ONCE() ...
  69. netif_txq_completed_wake(txq, cmpl_pkts, cmpl_bytes,
  70. drv_tx_avail(dr), 2 * MAX_SKB_FRAGS);
  71. Lockless queue stop / wake helper macros
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. .. kernel-doc:: include/net/netdev_queues.h
  74. :doc: Lockless queue stopping / waking helpers.
  75. No exclusive ownership
  76. ----------------------
  77. An ndo_start_xmit method must not modify the shared parts of a
  78. cloned SKB.
  79. Timely completions
  80. ------------------
  81. Do not forget that once you return NETDEV_TX_OK from your
  82. ndo_start_xmit method, it is your driver's responsibility to free
  83. up the SKB and in some finite amount of time.
  84. For example, this means that it is not allowed for your TX
  85. mitigation scheme to let TX packets "hang out" in the TX
  86. ring unreclaimed forever if no new TX packets are sent.
  87. This error can deadlock sockets waiting for send buffer room
  88. to be freed up.
  89. If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
  90. must not keep any reference to that SKB and you must not attempt
  91. to free it up.