netdevices.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. It is intended for driver developers.
  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 or 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. In addition, netdev instance
  166. lock if the driver implements queue management or shaper API.
  167. Context: process
  168. ndo_stop:
  169. Synchronization: rtnl_lock() semaphore. In addition, netdev instance
  170. lock if the driver implements queue management or shaper API.
  171. Context: process
  172. Note: netif_running() is guaranteed false
  173. ndo_do_ioctl:
  174. Synchronization: rtnl_lock() semaphore.
  175. This is only called by network subsystems internally,
  176. not by user space calling ioctl as it was in before
  177. linux-5.14.
  178. ndo_siocbond:
  179. Synchronization: rtnl_lock() semaphore. In addition, netdev instance
  180. lock if the driver implements queue management or shaper API.
  181. Context: process
  182. Used by the bonding driver for the SIOCBOND family of
  183. ioctl commands.
  184. ndo_siocwandev:
  185. Synchronization: rtnl_lock() semaphore. In addition, netdev instance
  186. lock if the driver implements queue management or shaper API.
  187. Context: process
  188. Used by the drivers/net/wan framework to handle
  189. the SIOCWANDEV ioctl with the if_settings structure.
  190. ndo_siocdevprivate:
  191. Synchronization: rtnl_lock() semaphore. In addition, netdev instance
  192. lock if the driver implements queue management or shaper API.
  193. Context: process
  194. This is used to implement SIOCDEVPRIVATE ioctl helpers.
  195. These should not be added to new drivers, so don't use.
  196. ndo_eth_ioctl:
  197. Synchronization: rtnl_lock() semaphore. In addition, netdev instance
  198. lock if the driver implements queue management or shaper API.
  199. Context: process
  200. ndo_get_stats:
  201. Synchronization: RCU (can be called concurrently with the stats
  202. update path).
  203. Context: atomic (can't sleep under RCU)
  204. ndo_start_xmit:
  205. Synchronization: __netif_tx_lock spinlock.
  206. When the driver sets dev->lltx this will be
  207. called without holding netif_tx_lock. In this case the driver
  208. has to lock by itself when needed.
  209. The locking there should also properly protect against
  210. set_rx_mode. WARNING: use of dev->lltx is deprecated.
  211. Don't use it for new drivers.
  212. Context: Process with BHs disabled or BH (timer),
  213. will be called with interrupts disabled by netconsole.
  214. Return codes:
  215. * NETDEV_TX_OK everything ok.
  216. * NETDEV_TX_BUSY Cannot transmit packet, try later
  217. Usually a bug, means queue start/stop flow control is broken in
  218. the driver. Note: the driver must NOT put the skb in its DMA ring.
  219. ndo_tx_timeout:
  220. Synchronization: netif_tx_lock spinlock; all TX queues frozen.
  221. Context: BHs disabled
  222. Notes: netif_queue_stopped() is guaranteed true
  223. ndo_set_rx_mode:
  224. Synchronization: netif_addr_lock spinlock.
  225. Context: BHs disabled
  226. ndo_setup_tc:
  227. ``TC_SETUP_BLOCK`` and ``TC_SETUP_FT`` are running under NFT locks
  228. (i.e. no ``rtnl_lock`` and no device instance lock). The rest of
  229. ``tc_setup_type`` types run under netdev instance lock if the driver
  230. implements queue management or shaper API.
  231. Most ndo callbacks not specified in the list above are running
  232. under ``rtnl_lock``. In addition, netdev instance lock is taken as well if
  233. the driver implements queue management or shaper API.
  234. struct napi_struct synchronization rules
  235. ========================================
  236. napi->poll:
  237. Synchronization:
  238. NAPI_STATE_SCHED bit in napi->state. Device
  239. driver's ndo_stop method will invoke napi_disable() on
  240. all NAPI instances which will do a sleeping poll on the
  241. NAPI_STATE_SCHED napi->state bit, waiting for all pending
  242. NAPI activity to cease.
  243. Context:
  244. softirq
  245. will be called with interrupts disabled by netconsole.
  246. netdev instance lock
  247. ====================
  248. Historically, all networking control operations were protected by a single
  249. global lock known as ``rtnl_lock``. There is an ongoing effort to replace this
  250. global lock with separate locks for each network namespace. Additionally,
  251. properties of individual netdev are increasingly protected by per-netdev locks.
  252. For device drivers that implement shaping or queue management APIs, all control
  253. operations will be performed under the netdev instance lock.
  254. Drivers can also explicitly request instance lock to be held during ops
  255. by setting ``request_ops_lock`` to true. Code comments and docs refer
  256. to drivers which have ops called under the instance lock as "ops locked".
  257. See also the documentation of the ``lock`` member of struct net_device.
  258. In the future, there will be an option for individual
  259. drivers to opt out of using ``rtnl_lock`` and instead perform their control
  260. operations directly under the netdev instance lock.
  261. Device drivers are encouraged to rely on the instance lock where possible.
  262. For the (mostly software) drivers that need to interact with the core stack,
  263. there are two sets of interfaces: ``dev_xxx``/``netdev_xxx`` and ``netif_xxx``
  264. (e.g., ``dev_set_mtu`` and ``netif_set_mtu``). The ``dev_xxx``/``netdev_xxx``
  265. functions handle acquiring the instance lock themselves, while the
  266. ``netif_xxx`` functions assume that the driver has already acquired
  267. the instance lock.
  268. struct net_device_ops
  269. ---------------------
  270. ``ndos`` are called without holding the instance lock for most drivers.
  271. "Ops locked" drivers will have most of the ``ndos`` invoked under
  272. the instance lock.
  273. struct ethtool_ops
  274. ------------------
  275. Similarly to ``ndos`` the instance lock is only held for select drivers.
  276. For "ops locked" drivers all ethtool ops without exceptions should
  277. be called under the instance lock.
  278. struct netdev_stat_ops
  279. ----------------------
  280. "qstat" ops are invoked under the instance lock for "ops locked" drivers,
  281. and under rtnl_lock for all other drivers.
  282. struct net_shaper_ops
  283. ---------------------
  284. All net shaper callbacks are invoked while holding the netdev instance
  285. lock. ``rtnl_lock`` may or may not be held.
  286. Note that supporting net shapers automatically enables "ops locking".
  287. struct netdev_queue_mgmt_ops
  288. ----------------------------
  289. All queue management callbacks are invoked while holding the netdev instance
  290. lock. ``rtnl_lock`` may or may not be held.
  291. Note that supporting struct netdev_queue_mgmt_ops automatically enables
  292. "ops locking".
  293. Notifiers and netdev instance lock
  294. ----------------------------------
  295. For device drivers that implement shaping or queue management APIs,
  296. some of the notifiers (``enum netdev_cmd``) are running under the netdev
  297. instance lock.
  298. The following netdev notifiers are always run under the instance lock:
  299. * ``NETDEV_XDP_FEAT_CHANGE``
  300. For devices with locked ops, currently only the following notifiers are
  301. running under the lock:
  302. * ``NETDEV_CHANGE``
  303. * ``NETDEV_REGISTER``
  304. * ``NETDEV_UP``
  305. The following notifiers are running without the lock:
  306. * ``NETDEV_UNREGISTER``
  307. There are no clear expectations for the remaining notifiers. Notifiers not on
  308. the list may run with or without the instance lock, potentially even invoking
  309. the same notifier type with and without the lock from different code paths.
  310. The goal is to eventually ensure that all (or most, with a few documented
  311. exceptions) notifiers run under the instance lock. Please extend this
  312. documentation whenever you make explicit assumption about lock being held
  313. from a notifier.
  314. NETDEV_INTERNAL symbol namespace
  315. ================================
  316. Symbols exported as NETDEV_INTERNAL can only be used in networking
  317. core and drivers which exclusively flow via the main networking list and trees.
  318. Note that the inverse is not true, most symbols outside of NETDEV_INTERNAL
  319. are not expected to be used by random code outside netdev either.
  320. Symbols may lack the designation because they predate the namespaces,
  321. or simply due to an oversight.