kasan: rename KASAN_SHADOW_* to KASAN_GRANULE_*
[linux-2.6-microblaze.git] / Documentation / networking / netdevices.rst
1 .. SPDX-License-Identifier: GPL-2.0
2
3 =====================================
4 Network Devices, the Kernel, and You!
5 =====================================
6
7
8 Introduction
9 ============
10 The following is a random collection of documentation regarding
11 network devices.
12
13 struct net_device allocation rules
14 ==================================
15 Network device structures need to persist even after module is unloaded and
16 must be allocated with alloc_netdev_mqs() and friends.
17 If device has registered successfully, it will be freed on last use
18 by free_netdev(). This is required to handle the pathologic case cleanly
19 (example: rmmod mydriver </sys/class/net/myeth/mtu )
20
21 alloc_netdev_mqs()/alloc_netdev() reserve extra space for driver
22 private data which gets freed when the network device is freed. If
23 separately 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
26 MTU
27 ===
28 Each network device has a Maximum Transfer Unit. The MTU does not
29 include any link layer protocol overhead. Upper layer protocols must
30 not pass a socket buffer (skb) to a device to transmit with more data
31 than the mtu. The MTU does not include link layer header overhead, so
32 for example on Ethernet if the standard MTU is 1500 bytes used, the
33 actual skb will contain up to 1514 bytes because of the Ethernet
34 header. Devices should allow for the 4 byte VLAN header as well.
35
36 Segmentation Offload (GSO, TSO) is an exception to this rule.  The
37 upper layer protocol may pass a large socket buffer to the device
38 transmit routine, and the device will break that up into separate
39 packets based on the current MTU.
40
41 MTU is symmetrical and applies both to receive and transmit. A device
42 must be able to receive at least the maximum size packet allowed by
43 the MTU. A network device may use the MTU as mechanism to size receive
44 buffers, but the device should allow packets with VLAN header. With
45 standard Ethernet mtu of 1500 bytes, the device should allow up to
46 1518 byte packets (1500 + 14 header + 4 tag).  The device may either:
47 drop, truncate, or pass up oversize packets, but dropping oversize
48 packets is preferred.
49
50
51 struct net_device synchronization rules
52 =======================================
53 ndo_open:
54         Synchronization: rtnl_lock() semaphore.
55         Context: process
56
57 ndo_stop:
58         Synchronization: rtnl_lock() semaphore.
59         Context: process
60         Note: netif_running() is guaranteed false
61
62 ndo_do_ioctl:
63         Synchronization: rtnl_lock() semaphore.
64         Context: process
65
66 ndo_get_stats:
67         Synchronization: dev_base_lock rwlock.
68         Context: nominally process, but don't sleep inside an rwlock
69
70 ndo_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
90 ndo_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
95 ndo_set_rx_mode:
96         Synchronization: netif_addr_lock spinlock.
97         Context: BHs disabled
98
99 struct napi_struct synchronization rules
100 ========================================
101 napi->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.