netdevices.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 lifetime 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 pathological 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()) then it is up to the module exit handler to free that.
  20. There are two groups of APIs for registering struct net_device.
  21. First group can be used in normal contexts where ``rtnl_lock`` is not already
  22. held: register_netdev(), unregister_netdev().
  23. Second group can be used when ``rtnl_lock`` is already held:
  24. register_netdevice(), unregister_netdevice(), free_netdevice().
  25. Simple drivers
  26. --------------
  27. Most drivers (especially device drivers) handle lifetime of struct net_device
  28. in context where ``rtnl_lock`` is not held (e.g. driver probe and remove paths).
  29. In that case the struct net_device registration is done using
  30. the register_netdev(), and unregister_netdev() functions:
  31. .. code-block:: c
  32. int probe()
  33. {
  34. struct my_device_priv *priv;
  35. int err;
  36. dev = alloc_netdev_mqs(...);
  37. if (!dev)
  38. return -ENOMEM;
  39. priv = netdev_priv(dev);
  40. /* ... do all device setup before calling register_netdev() ...
  41. */
  42. err = register_netdev(dev);
  43. if (err)
  44. goto err_undo;
  45. /* net_device is visible to the user! */
  46. err_undo:
  47. /* ... undo the device setup ... */
  48. free_netdev(dev);
  49. return err;
  50. }
  51. void remove()
  52. {
  53. unregister_netdev(dev);
  54. free_netdev(dev);
  55. }
  56. Note that after calling register_netdev() the device is visible in the system.
  57. Users can open it and start sending / receiving traffic immediately,
  58. or run any other callback, so all initialization must be done prior to
  59. registration.
  60. unregister_netdev() closes the device and waits for all users to be done
  61. with it. The memory of struct net_device itself may still be referenced
  62. by sysfs but all operations on that device will fail.
  63. free_netdev() can be called after unregister_netdev() returns on when
  64. register_netdev() failed.
  65. Device management under RTNL
  66. ----------------------------
  67. Registering struct net_device while in context which already holds
  68. the ``rtnl_lock`` requires extra care. In those scenarios most drivers
  69. will want to make use of struct net_device's ``needs_free_netdev``
  70. and ``priv_destructor`` members for freeing of state.
  71. Example flow of netdev handling under ``rtnl_lock``:
  72. .. code-block:: c
  73. static void my_setup(struct net_device *dev)
  74. {
  75. dev->needs_free_netdev = true;
  76. }
  77. static void my_destructor(struct net_device *dev)
  78. {
  79. some_obj_destroy(priv->obj);
  80. some_uninit(priv);
  81. }
  82. int create_link()
  83. {
  84. struct my_device_priv *priv;
  85. int err;
  86. ASSERT_RTNL();
  87. dev = alloc_netdev(sizeof(*priv), "net%d", NET_NAME_UNKNOWN, my_setup);
  88. if (!dev)
  89. return -ENOMEM;
  90. priv = netdev_priv(dev);
  91. /* Implicit constructor */
  92. err = some_init(priv);
  93. if (err)
  94. goto err_free_dev;
  95. priv->obj = some_obj_create();
  96. if (!priv->obj) {
  97. err = -ENOMEM;
  98. goto err_some_uninit;
  99. }
  100. /* End of constructor, set the destructor: */
  101. dev->priv_destructor = my_destructor;
  102. err = register_netdevice(dev);
  103. if (err)
  104. /* register_netdevice() calls destructor on failure */
  105. goto err_free_dev;
  106. /* If anything fails now unregister_netdevice() (or unregister_netdev())
  107. * will take care of calling my_destructor and free_netdev().
  108. */
  109. return 0;
  110. err_some_uninit:
  111. some_uninit(priv);
  112. err_free_dev:
  113. free_netdev(dev);
  114. return err;
  115. }
  116. If struct net_device.priv_destructor is set it will be called by the core
  117. some time after unregister_netdevice(), it will also be called if
  118. register_netdevice() fails. The callback may be invoked with or without
  119. ``rtnl_lock`` held.
  120. There is no explicit constructor callback, driver "constructs" the private
  121. netdev state after allocating it and before registration.
  122. Setting struct net_device.needs_free_netdev makes core call free_netdevice()
  123. automatically after unregister_netdevice() when all references to the device
  124. are gone. It only takes effect after a successful call to register_netdevice()
  125. so if register_netdevice() fails driver is responsible for calling
  126. free_netdev().
  127. free_netdev() is safe to call on error paths right after unregister_netdevice()
  128. or when register_netdevice() fails. Parts of netdev (de)registration process
  129. happen after ``rtnl_lock`` is released, therefore in those cases free_netdev()
  130. will defer some of the processing until ``rtnl_lock`` is released.
  131. Devices spawned from struct rtnl_link_ops should never free the
  132. struct net_device directly.
  133. .ndo_init and .ndo_uninit
  134. ~~~~~~~~~~~~~~~~~~~~~~~~~
  135. ``.ndo_init`` and ``.ndo_uninit`` callbacks are called during net_device
  136. registration and de-registration, under ``rtnl_lock``. Drivers can use
  137. those e.g. when parts of their init process need to run under ``rtnl_lock``.
  138. ``.ndo_init`` runs before device is visible in the system, ``.ndo_uninit``
  139. runs during de-registering after device is closed but other subsystems
  140. may still have outstanding references to the netdevice.
  141. MTU
  142. ===
  143. Each network device has a Maximum Transfer Unit. The MTU does not
  144. include any link layer protocol overhead. Upper layer protocols must
  145. not pass a socket buffer (skb) to a device to transmit with more data
  146. than the mtu. The MTU does not include link layer header overhead, so
  147. for example on Ethernet if the standard MTU is 1500 bytes used, the
  148. actual skb will contain up to 1514 bytes because of the Ethernet
  149. header. Devices should allow for the 4 byte VLAN header as well.
  150. Segmentation Offload (GSO, TSO) is an exception to this rule. The
  151. upper layer protocol may pass a large socket buffer to the device
  152. transmit routine, and the device will break that up into separate
  153. packets based on the current MTU.
  154. MTU is symmetrical and applies both to receive and transmit. A device
  155. must be able to receive at least the maximum size packet allowed by
  156. the MTU. A network device may use the MTU as mechanism to size receive
  157. buffers, but the device should allow packets with VLAN header. With
  158. standard Ethernet mtu of 1500 bytes, the device should allow up to
  159. 1518 byte packets (1500 + 14 header + 4 tag). The device may either:
  160. drop, truncate, or pass up oversize packets, but dropping oversize
  161. packets is preferred.
  162. struct net_device synchronization rules
  163. =======================================
  164. ndo_open:
  165. Synchronization: rtnl_lock() semaphore.
  166. Context: process
  167. ndo_stop:
  168. Synchronization: rtnl_lock() semaphore.
  169. Context: process
  170. Note: netif_running() is guaranteed false
  171. ndo_do_ioctl:
  172. Synchronization: rtnl_lock() semaphore.
  173. Context: process
  174. This is only called by network subsystems internally,
  175. not by user space calling ioctl as it was in before
  176. linux-5.14.
  177. ndo_siocbond:
  178. Synchronization: rtnl_lock() semaphore.
  179. Context: process
  180. Used by the bonding driver for the SIOCBOND family of
  181. ioctl commands.
  182. ndo_siocwandev:
  183. Synchronization: rtnl_lock() semaphore.
  184. Context: process
  185. Used by the drivers/net/wan framework to handle
  186. the SIOCWANDEV ioctl with the if_settings structure.
  187. ndo_siocdevprivate:
  188. Synchronization: rtnl_lock() semaphore.
  189. Context: process
  190. This is used to implement SIOCDEVPRIVATE ioctl helpers.
  191. These should not be added to new drivers, so don't use.
  192. ndo_eth_ioctl:
  193. Synchronization: rtnl_lock() semaphore.
  194. Context: process
  195. ndo_get_stats:
  196. Synchronization: rtnl_lock() semaphore, or RCU.
  197. Context: atomic (can't sleep under RCU)
  198. ndo_start_xmit:
  199. Synchronization: __netif_tx_lock spinlock.
  200. When the driver sets dev->lltx this will be
  201. called without holding netif_tx_lock. In this case the driver
  202. has to lock by itself when needed.
  203. The locking there should also properly protect against
  204. set_rx_mode. WARNING: use of dev->lltx is deprecated.
  205. Don't use it for new drivers.
  206. Context: Process with BHs disabled or BH (timer),
  207. will be called with interrupts disabled by netconsole.
  208. Return codes:
  209. * NETDEV_TX_OK everything ok.
  210. * NETDEV_TX_BUSY Cannot transmit packet, try later
  211. Usually a bug, means queue start/stop flow control is broken in
  212. the driver. Note: the driver must NOT put the skb in its DMA ring.
  213. ndo_tx_timeout:
  214. Synchronization: netif_tx_lock spinlock; all TX queues frozen.
  215. Context: BHs disabled
  216. Notes: netif_queue_stopped() is guaranteed true
  217. ndo_set_rx_mode:
  218. Synchronization: netif_addr_lock spinlock.
  219. Context: BHs disabled
  220. struct napi_struct synchronization rules
  221. ========================================
  222. napi->poll:
  223. Synchronization:
  224. NAPI_STATE_SCHED bit in napi->state. Device
  225. driver's ndo_stop method will invoke napi_disable() on
  226. all NAPI instances which will do a sleeping poll on the
  227. NAPI_STATE_SCHED napi->state bit, waiting for all pending
  228. NAPI activity to cease.
  229. Context:
  230. softirq
  231. will be called with interrupts disabled by netconsole.