Linux Audio

Check our new training course

Loading...
v6.13.7
  1.. SPDX-License-Identifier: GPL-2.0
  2
  3=====================
  4Softnet Driver Issues
  5=====================
  6
  7Probing guidelines
  8==================
  9
 10Address validation
 11------------------
 
 
 12
 13Any hardware layer address you obtain for your device should
 14be verified.  For example, for ethernet check it with
 15linux/etherdevice.h:is_valid_ether_addr()
 16
 17Close/stop guidelines
 18=====================
 19
 20Quiescence
 21----------
 22
 23After the ndo_stop routine has been called, the hardware must
 24not receive or transmit any data.  All in flight packets must
 25be aborted. If necessary, poll or wait for completion of
 26any reset commands.
 27
 28Auto-close
 29----------
 30
 31The ndo_stop routine will be called by unregister_netdevice
 32if device is still UP.
 33
 34Transmit path guidelines
 35========================
 36
 37Stop queues in advance
 38----------------------
 39
 40The ndo_start_xmit method must not return NETDEV_TX_BUSY under
 41any normal circumstances.  It is considered a hard error unless
 42there is no way your device can tell ahead of time when its
 43transmit function will become busy.
 44
 45Instead it must maintain the queue properly.  For example,
 46for a driver implementing scatter-gather this means:
 47
 48.. code-block:: c
 49
 50	static u32 drv_tx_avail(struct drv_ring *dr)
 51	{
 52		u32 used = READ_ONCE(dr->prod) - READ_ONCE(dr->cons);
 53
 54		return dr->tx_ring_size - (used & bp->tx_ring_mask);
 55	}
 56
 57	static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
 58					       struct net_device *dev)
 59	{
 60		struct drv *dp = netdev_priv(dev);
 61		struct netdev_queue *txq;
 62		struct drv_ring *dr;
 63		int idx;
 64
 65		idx = skb_get_queue_mapping(skb);
 66		dr = dp->tx_rings[idx];
 67		txq = netdev_get_tx_queue(dev, idx);
 68
 69		//...
 70		/* This should be a very rare race - log it. */
 71		if (drv_tx_avail(dr) <= skb_shinfo(skb)->nr_frags + 1) {
 72			netif_stop_queue(dev);
 73			netdev_warn(dev, "Tx Ring full when queue awake!\n");
 
 
 74			return NETDEV_TX_BUSY;
 75		}
 76
 77		//... queue packet to card ...
 
 78
 79		netdev_tx_sent_queue(txq, skb->len);
 80
 81		//... update tx producer index using WRITE_ONCE() ...
 82
 83		if (!netif_txq_maybe_stop(txq, drv_tx_avail(dr),
 84					  MAX_SKB_FRAGS + 1, 2 * MAX_SKB_FRAGS))
 85			dr->stats.stopped++;
 86
 87		//...
 
 
 88		return NETDEV_TX_OK;
 89	}
 90
 91And then at the end of your TX reclamation event handling:
 
 
 
 
 
 
 
 
 
 
 
 
 
 92
 93.. code-block:: c
 94
 95	//... update tx consumer index using WRITE_ONCE() ...
 
 
 96
 97	netif_txq_completed_wake(txq, cmpl_pkts, cmpl_bytes,
 98				 drv_tx_avail(dr), 2 * MAX_SKB_FRAGS);
 99
100Lockless queue stop / wake helper macros
101~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
102
103.. kernel-doc:: include/net/netdev_queues.h
104   :doc: Lockless queue stopping / waking helpers.
 
 
 
105
106No exclusive ownership
107----------------------
 
108
109An ndo_start_xmit method must not modify the shared parts of a
110cloned SKB.
111
112Timely completions
113------------------
 
114
115Do not forget that once you return NETDEV_TX_OK from your
116ndo_start_xmit method, it is your driver's responsibility to free
117up the SKB and in some finite amount of time.
118
119For example, this means that it is not allowed for your TX
120mitigation scheme to let TX packets "hang out" in the TX
121ring unreclaimed forever if no new TX packets are sent.
122This error can deadlock sockets waiting for send buffer room
123to be freed up.
124
125If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
126must not keep any reference to that SKB and you must not attempt
127to free it up.
v5.9
 1.. SPDX-License-Identifier: GPL-2.0
 2
 3=====================
 4Softnet Driver Issues
 5=====================
 6
 7Transmit path guidelines:
 
 8
 91) The ndo_start_xmit method must not return NETDEV_TX_BUSY under
10   any normal circumstances.  It is considered a hard error unless
11   there is no way your device can tell ahead of time when it's
12   transmit function will become busy.
13
14   Instead it must maintain the queue properly.  For example,
15   for a driver implementing scatter-gather this means::
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17	static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
18					       struct net_device *dev)
19	{
20		struct drv *dp = netdev_priv(dev);
21
22		lock_tx(dp);
23		...
24		/* This is a hard error log it. */
25		if (TX_BUFFS_AVAIL(dp) <= (skb_shinfo(skb)->nr_frags + 1)) {
 
 
 
 
 
 
26			netif_stop_queue(dev);
27			unlock_tx(dp);
28			printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
29			       dev->name);
30			return NETDEV_TX_BUSY;
31		}
32
33		... queue packet to card ...
34		... update tx consumer index ...
35
36		if (TX_BUFFS_AVAIL(dp) <= (MAX_SKB_FRAGS + 1))
37			netif_stop_queue(dev);
 
 
 
 
 
38
39		...
40		unlock_tx(dp);
41		...
42		return NETDEV_TX_OK;
43	}
44
45   And then at the end of your TX reclamation event handling::
46
47	if (netif_queue_stopped(dp->dev) &&
48	    TX_BUFFS_AVAIL(dp) > (MAX_SKB_FRAGS + 1))
49		netif_wake_queue(dp->dev);
50
51   For a non-scatter-gather supporting card, the three tests simply become::
52
53		/* This is a hard error log it. */
54		if (TX_BUFFS_AVAIL(dp) <= 0)
55
56   and::
57
58		if (TX_BUFFS_AVAIL(dp) == 0)
59
60   and::
61
62	if (netif_queue_stopped(dp->dev) &&
63	    TX_BUFFS_AVAIL(dp) > 0)
64		netif_wake_queue(dp->dev);
65
662) An ndo_start_xmit method must not modify the shared parts of a
67   cloned SKB.
68
693) Do not forget that once you return NETDEV_TX_OK from your
70   ndo_start_xmit method, it is your driver's responsibility to free
71   up the SKB and in some finite amount of time.
72
73   For example, this means that it is not allowed for your TX
74   mitigation scheme to let TX packets "hang out" in the TX
75   ring unreclaimed forever if no new TX packets are sent.
76   This error can deadlock sockets waiting for send buffer room
77   to be freed up.
78
79   If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
80   must not keep any reference to that SKB and you must not attempt
81   to free it up.
82
83Probing guidelines:
 
84
851) Any hardware layer address you obtain for your device should
86   be verified.  For example, for ethernet check it with
87   linux/etherdevice.h:is_valid_ether_addr()
88
89Close/stop guidelines:
 
 
90
911) After the ndo_stop routine has been called, the hardware must
92   not receive or transmit any data.  All in flight packets must
93   be aborted. If necessary, poll or wait for completion of
94   any reset commands.
 
95
962) The ndo_stop routine will be called by unregister_netdevice
97   if device is still UP.