netdevices.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================================
  3. Network Devices, the Kernel, and You!
  4. =====================================
  5. Introduction
  6. ============
  7. The following is a random collection of documentation regarding
  8. network devices.
  9. struct net_device allocation rules
  10. ==================================
  11. Network device structures need to persist even after module is unloaded and
  12. must be allocated with alloc_netdev_mqs() and friends.
  13. If device has registered successfully, it will be freed on last use
  14. by free_netdev(). This is required to handle the pathologic case cleanly
  15. (example: rmmod mydriver </sys/class/net/myeth/mtu )
  16. alloc_netdev_mqs()/alloc_netdev() reserve extra space for driver
  17. private data which gets freed when the network device is freed. If
  18. separately allocated data is attached to the network device
  19. (netdev_priv(dev)) then it is up to the module exit handler to free that.
  20. MTU
  21. ===
  22. Each network device has a Maximum Transfer Unit. The MTU does not
  23. include any link layer protocol overhead. Upper layer protocols must
  24. not pass a socket buffer (skb) to a device to transmit with more data
  25. than the mtu. The MTU does not include link layer header overhead, so
  26. for example on Ethernet if the standard MTU is 1500 bytes used, the
  27. actual skb will contain up to 1514 bytes because of the Ethernet
  28. header. Devices should allow for the 4 byte VLAN header as well.
  29. Segmentation Offload (GSO, TSO) is an exception to this rule. The
  30. upper layer protocol may pass a large socket buffer to the device
  31. transmit routine, and the device will break that up into separate
  32. packets based on the current MTU.
  33. MTU is symmetrical and applies both to receive and transmit. A device
  34. must be able to receive at least the maximum size packet allowed by
  35. the MTU. A network device may use the MTU as mechanism to size receive
  36. buffers, but the device should allow packets with VLAN header. With
  37. standard Ethernet mtu of 1500 bytes, the device should allow up to
  38. 1518 byte packets (1500 + 14 header + 4 tag). The device may either:
  39. drop, truncate, or pass up oversize packets, but dropping oversize
  40. packets is preferred.
  41. struct net_device synchronization rules
  42. =======================================
  43. ndo_open:
  44. Synchronization: rtnl_lock() semaphore.
  45. Context: process
  46. ndo_stop:
  47. Synchronization: rtnl_lock() semaphore.
  48. Context: process
  49. Note: netif_running() is guaranteed false
  50. ndo_do_ioctl:
  51. Synchronization: rtnl_lock() semaphore.
  52. Context: process
  53. ndo_get_stats:
  54. Synchronization: dev_base_lock rwlock.
  55. Context: nominally process, but don't sleep inside an rwlock
  56. ndo_start_xmit:
  57. Synchronization: __netif_tx_lock spinlock.
  58. When the driver sets NETIF_F_LLTX in dev->features this will be
  59. called without holding netif_tx_lock. In this case the driver
  60. has to lock by itself when needed.
  61. The locking there should also properly protect against
  62. set_rx_mode. WARNING: use of NETIF_F_LLTX is deprecated.
  63. Don't use it for new drivers.
  64. Context: Process with BHs disabled or BH (timer),
  65. will be called with interrupts disabled by netconsole.
  66. Return codes:
  67. * NETDEV_TX_OK everything ok.
  68. * NETDEV_TX_BUSY Cannot transmit packet, try later
  69. Usually a bug, means queue start/stop flow control is broken in
  70. the driver. Note: the driver must NOT put the skb in its DMA ring.
  71. ndo_tx_timeout:
  72. Synchronization: netif_tx_lock spinlock; all TX queues frozen.
  73. Context: BHs disabled
  74. Notes: netif_queue_stopped() is guaranteed true
  75. ndo_set_rx_mode:
  76. Synchronization: netif_addr_lock spinlock.
  77. Context: BHs disabled
  78. struct napi_struct synchronization rules
  79. ========================================
  80. napi->poll:
  81. Synchronization:
  82. NAPI_STATE_SCHED bit in napi->state. Device
  83. driver's ndo_stop method will invoke napi_disable() on
  84. all NAPI instances which will do a sleeping poll on the
  85. NAPI_STATE_SCHED napi->state bit, waiting for all pending
  86. NAPI activity to cease.
  87. Context:
  88. softirq
  89. will be called with interrupts disabled by netconsole.