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