netdevices.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 kmalloc. If device has registered successfully,
  10. it will be freed on last use by free_netdev. This is required to handle the
  11. pathologic case cleanly (example: rmmod mydriver </sys/class/net/myeth/mtu )
  12. There are routines in net_init.c to handle the common cases of
  13. alloc_etherdev, alloc_netdev. These 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. dev->open:
  41. Synchronization: rtnl_lock() semaphore.
  42. Context: process
  43. dev->stop:
  44. Synchronization: rtnl_lock() semaphore.
  45. Context: process
  46. Note1: netif_running() is guaranteed false
  47. Note2: dev->poll() is guaranteed to be stopped
  48. dev->do_ioctl:
  49. Synchronization: rtnl_lock() semaphore.
  50. Context: process
  51. dev->get_stats:
  52. Synchronization: dev_base_lock rwlock.
  53. Context: nominally process, but don't sleep inside an rwlock
  54. dev->hard_start_xmit:
  55. Synchronization: netif_tx_lock spinlock.
  56. When the driver sets NETIF_F_LLTX in dev->features this will be
  57. called without holding netif_tx_lock. In this case the driver
  58. has to lock by itself when needed. It is recommended to use a try lock
  59. for this and return NETDEV_TX_LOCKED when the spin lock fails.
  60. The locking there should also properly protect against
  61. set_multicast_list. Note that the use of NETIF_F_LLTX is deprecated.
  62. Dont use it for new drivers.
  63. Context: Process with BHs disabled or BH (timer),
  64. will be called with interrupts disabled by netconsole.
  65. Return codes:
  66. o NETDEV_TX_OK everything ok.
  67. o NETDEV_TX_BUSY Cannot transmit packet, try later
  68. Usually a bug, means queue start/stop flow control is broken in
  69. the driver. Note: the driver must NOT put the skb in its DMA ring.
  70. o NETDEV_TX_LOCKED Locking failed, please retry quickly.
  71. Only valid when NETIF_F_LLTX is set.
  72. dev->tx_timeout:
  73. Synchronization: netif_tx_lock spinlock.
  74. Context: BHs disabled
  75. Notes: netif_queue_stopped() is guaranteed true
  76. dev->set_multicast_list:
  77. Synchronization: netif_tx_lock spinlock.
  78. Context: BHs disabled
  79. struct napi_struct synchronization rules
  80. ========================================
  81. napi->poll:
  82. Synchronization: NAPI_STATE_SCHED bit in napi->state. Device
  83. driver's dev->close 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: softirq
  88. will be called with interrupts disabled by netconsole.