2020-04-28 06:01:33 +08:00
|
|
|
.. SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
=====================
|
|
|
|
Softnet Driver Issues
|
|
|
|
=====================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:31 +08:00
|
|
|
Probing guidelines
|
|
|
|
==================
|
|
|
|
|
|
|
|
Address validation
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Any hardware layer address you obtain for your device should
|
|
|
|
be verified. For example, for ethernet check it with
|
|
|
|
linux/etherdevice.h:is_valid_ether_addr()
|
|
|
|
|
|
|
|
Close/stop guidelines
|
|
|
|
=====================
|
|
|
|
|
|
|
|
Quiescence
|
|
|
|
----------
|
|
|
|
|
|
|
|
After the ndo_stop routine has been called, the hardware must
|
|
|
|
not receive or transmit any data. All in flight packets must
|
|
|
|
be aborted. If necessary, poll or wait for completion of
|
|
|
|
any reset commands.
|
|
|
|
|
|
|
|
Auto-close
|
|
|
|
----------
|
|
|
|
|
|
|
|
The ndo_stop routine will be called by unregister_netdevice
|
|
|
|
if device is still UP.
|
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
Transmit path guidelines
|
|
|
|
========================
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
Stop queues in advance
|
|
|
|
----------------------
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
The ndo_start_xmit method must not return NETDEV_TX_BUSY under
|
|
|
|
any normal circumstances. It is considered a hard error unless
|
|
|
|
there is no way your device can tell ahead of time when its
|
|
|
|
transmit function will become busy.
|
|
|
|
|
|
|
|
Instead it must maintain the queue properly. For example,
|
2023-04-07 09:25:32 +08:00
|
|
|
for a driver implementing scatter-gather this means:
|
|
|
|
|
|
|
|
.. code-block:: c
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-12 09:50:36 +08:00
|
|
|
static u32 drv_tx_avail(struct drv_ring *dr)
|
|
|
|
{
|
|
|
|
u32 used = READ_ONCE(dr->prod) - READ_ONCE(dr->cons);
|
|
|
|
|
|
|
|
return dr->tx_ring_size - (used & bp->tx_ring_mask);
|
|
|
|
}
|
|
|
|
|
2012-04-05 22:40:25 +08:00
|
|
|
static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
|
|
|
|
struct net_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-08 17:14:16 +08:00
|
|
|
struct drv *dp = netdev_priv(dev);
|
2023-04-12 09:50:36 +08:00
|
|
|
struct netdev_queue *txq;
|
|
|
|
struct drv_ring *dr;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = skb_get_queue_mapping(skb);
|
|
|
|
dr = dp->tx_rings[idx];
|
|
|
|
txq = netdev_get_tx_queue(dev, idx);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:32 +08:00
|
|
|
//...
|
2023-04-12 09:50:36 +08:00
|
|
|
/* This should be a very rare race - log it. */
|
|
|
|
if (drv_tx_avail(dr) <= skb_shinfo(skb)->nr_frags + 1) {
|
2005-04-17 06:20:36 +08:00
|
|
|
netif_stop_queue(dev);
|
2023-04-12 09:50:36 +08:00
|
|
|
netdev_warn(dev, "Tx Ring full when queue awake!\n");
|
2012-04-05 22:40:25 +08:00
|
|
|
return NETDEV_TX_BUSY;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2023-04-07 09:25:32 +08:00
|
|
|
//... queue packet to card ...
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-12 09:50:36 +08:00
|
|
|
netdev_tx_sent_queue(txq, skb->len);
|
|
|
|
|
|
|
|
//... update tx producer index using WRITE_ONCE() ...
|
|
|
|
|
|
|
|
if (!netif_txq_maybe_stop(txq, drv_tx_avail(dr),
|
|
|
|
MAX_SKB_FRAGS + 1, 2 * MAX_SKB_FRAGS))
|
|
|
|
dr->stats.stopped++;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:32 +08:00
|
|
|
//...
|
2012-04-05 22:40:25 +08:00
|
|
|
return NETDEV_TX_OK;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2023-04-07 09:25:32 +08:00
|
|
|
And then at the end of your TX reclamation event handling:
|
|
|
|
|
|
|
|
.. code-block:: c
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-12 09:50:36 +08:00
|
|
|
//... update tx consumer index using WRITE_ONCE() ...
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-12 09:50:36 +08:00
|
|
|
netif_txq_completed_wake(txq, cmpl_pkts, cmpl_bytes,
|
|
|
|
drv_tx_avail(dr), 2 * MAX_SKB_FRAGS);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:33 +08:00
|
|
|
Lockless queue stop / wake helper macros
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. kernel-doc:: include/net/netdev_queues.h
|
|
|
|
:doc: Lockless queue stopping / waking helpers.
|
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
No exclusive ownership
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
An ndo_start_xmit method must not modify the shared parts of a
|
|
|
|
cloned SKB.
|
|
|
|
|
|
|
|
Timely completions
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Do not forget that once you return NETDEV_TX_OK from your
|
|
|
|
ndo_start_xmit method, it is your driver's responsibility to free
|
|
|
|
up the SKB and in some finite amount of time.
|
2008-01-15 22:25:27 +08:00
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
For example, this means that it is not allowed for your TX
|
|
|
|
mitigation scheme to let TX packets "hang out" in the TX
|
|
|
|
ring unreclaimed forever if no new TX packets are sent.
|
|
|
|
This error can deadlock sockets waiting for send buffer room
|
|
|
|
to be freed up.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-04-07 09:25:30 +08:00
|
|
|
If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
|
|
|
|
must not keep any reference to that SKB and you must not attempt
|
|
|
|
to free it up.
|