blob: 7fec2061a334fd489acc127ca1630a298eb3bda7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2Network Devices, the Kernel, and You!
3
4
5Introduction
6============
7The following is a random collection of documentation regarding
8network devices.
9
10struct net_device allocation rules
11==================================
12Network device structures need to persist even after module is unloaded and
Eric Dumazet74d332c2013-10-30 13:10:44 -070013must be allocated with alloc_netdev_mqs() and friends.
14If device has registered successfully, it will be freed on last use
15by free_netdev(). This is required to handle the pathologic case cleanly
16(example: rmmod mydriver </sys/class/net/myeth/mtu )
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Eric Dumazet74d332c2013-10-30 13:10:44 -070018alloc_netdev_mqs()/alloc_netdev() reserve extra space for driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070019private data which gets freed when the network device is freed. If
20separately allocated data is attached to the network device
Wang Chenb74ca3a2008-12-08 01:14:16 -080021(netdev_priv(dev)) then it is up to the module exit handler to free that.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Stephen Hemminger1c8c7d62007-07-07 23:03:44 -070023MTU
24===
25Each network device has a Maximum Transfer Unit. The MTU does not
26include any link layer protocol overhead. Upper layer protocols must
27not pass a socket buffer (skb) to a device to transmit with more data
28than the mtu. The MTU does not include link layer header overhead, so
29for example on Ethernet if the standard MTU is 1500 bytes used, the
30actual skb will contain up to 1514 bytes because of the Ethernet
31header. Devices should allow for the 4 byte VLAN header as well.
32
33Segmentation Offload (GSO, TSO) is an exception to this rule. The
34upper layer protocol may pass a large socket buffer to the device
35transmit routine, and the device will break that up into separate
36packets based on the current MTU.
37
38MTU is symmetrical and applies both to receive and transmit. A device
39must be able to receive at least the maximum size packet allowed by
40the MTU. A network device may use the MTU as mechanism to size receive
41buffers, but the device should allow packets with VLAN header. With
42standard Ethernet mtu of 1500 bytes, the device should allow up to
431518 byte packets (1500 + 14 header + 4 tag). The device may either:
44drop, truncate, or pass up oversize packets, but dropping oversize
45packets is preferred.
46
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct net_device synchronization rules
49=======================================
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000050ndo_open:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 Synchronization: rtnl_lock() semaphore.
52 Context: process
53
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000054ndo_stop:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 Synchronization: rtnl_lock() semaphore.
56 Context: process
Ben Hutchings93b6a3a2012-04-05 14:39:10 +000057 Note: netif_running() is guaranteed false
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000059ndo_do_ioctl:
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 Synchronization: rtnl_lock() semaphore.
61 Context: process
62
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000063ndo_get_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 Synchronization: dev_base_lock rwlock.
65 Context: nominally process, but don't sleep inside an rwlock
66
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000067ndo_start_xmit:
Ben Hutchings04fd3d352012-04-05 14:39:30 +000068 Synchronization: __netif_tx_lock spinlock.
Stephen Hemminger17229332007-07-07 22:59:14 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 When the driver sets NETIF_F_LLTX in dev->features this will be
Herbert Xu932ff272006-06-09 12:20:56 -070071 called without holding netif_tx_lock. In this case the driver
Florian Westphalf0cdf762016-04-24 21:38:14 +020072 has to lock by itself when needed.
73 The locking there should also properly protect against
74 set_rx_mode. WARNING: use of NETIF_F_LLTX is deprecated.
Matt LaPlante19f59462009-04-27 15:06:31 +020075 Don't use it for new drivers.
Stephen Hemminger17229332007-07-07 22:59:14 -070076
77 Context: Process with BHs disabled or BH (timer),
78 will be called with interrupts disabled by netconsole.
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 Return codes:
81 o NETDEV_TX_OK everything ok.
82 o NETDEV_TX_BUSY Cannot transmit packet, try later
83 Usually a bug, means queue start/stop flow control is broken in
84 the driver. Note: the driver must NOT put the skb in its DMA ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000086ndo_tx_timeout:
Ben Hutchings04fd3d352012-04-05 14:39:30 +000087 Synchronization: netif_tx_lock spinlock; all TX queues frozen.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 Context: BHs disabled
89 Notes: netif_queue_stopped() is guaranteed true
90
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000091ndo_set_rx_mode:
Ben Hutchings04fd3d352012-04-05 14:39:30 +000092 Synchronization: netif_addr_lock spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 Context: BHs disabled
94
Stephen Hemmingerbea33482007-10-03 16:41:36 -070095struct napi_struct synchronization rules
96========================================
97napi->poll:
98 Synchronization: NAPI_STATE_SCHED bit in napi->state. Device
Ben Hutchingsb3cf6542012-04-05 14:39:47 +000099 driver's ndo_stop method will invoke napi_disable() on
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700100 all NAPI instances which will do a sleeping poll on the
101 NAPI_STATE_SCHED napi->state bit, waiting for all pending
102 NAPI activity to cease.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 Context: softirq
Stephen Hemminger17229332007-07-07 22:59:14 -0700104 will be called with interrupts disabled by netconsole.