netdevices.txt 4.0 KB

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