Randy Dunlap | 98766fb | 2005-11-21 21:32:31 -0800 | [diff] [blame] | 1 | Document about softnet driver issues |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | Transmit path guidelines: |
| 4 | |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 5 | 1) The ndo_start_xmit method must not return NETDEV_TX_BUSY under |
| 6 | any normal circumstances. It is considered a hard error unless |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | there is no way your device can tell ahead of time when it's |
| 8 | transmit function will become busy. |
| 9 | |
| 10 | Instead it must maintain the queue properly. For example, |
| 11 | for a driver implementing scatter-gather this means: |
| 12 | |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 13 | static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb, |
| 14 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | { |
Wang Chen | b74ca3a | 2008-12-08 01:14:16 -0800 | [diff] [blame] | 16 | struct drv *dp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | lock_tx(dp); |
| 19 | ... |
| 20 | /* This is a hard error log it. */ |
| 21 | if (TX_BUFFS_AVAIL(dp) <= (skb_shinfo(skb)->nr_frags + 1)) { |
| 22 | netif_stop_queue(dev); |
| 23 | unlock_tx(dp); |
| 24 | printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n", |
| 25 | dev->name); |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 26 | return NETDEV_TX_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ... queue packet to card ... |
| 30 | ... update tx consumer index ... |
| 31 | |
| 32 | if (TX_BUFFS_AVAIL(dp) <= (MAX_SKB_FRAGS + 1)) |
| 33 | netif_stop_queue(dev); |
| 34 | |
| 35 | ... |
| 36 | unlock_tx(dp); |
| 37 | ... |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 38 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Matt LaPlante | d6bc8ac | 2006-10-03 22:54:15 +0200 | [diff] [blame] | 41 | And then at the end of your TX reclamation event handling: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (netif_queue_stopped(dp->dev) && |
| 44 | TX_BUFFS_AVAIL(dp) > (MAX_SKB_FRAGS + 1)) |
| 45 | netif_wake_queue(dp->dev); |
| 46 | |
| 47 | For a non-scatter-gather supporting card, the three tests simply become: |
| 48 | |
| 49 | /* This is a hard error log it. */ |
| 50 | if (TX_BUFFS_AVAIL(dp) <= 0) |
| 51 | |
| 52 | and: |
| 53 | |
| 54 | if (TX_BUFFS_AVAIL(dp) == 0) |
| 55 | |
| 56 | and: |
| 57 | |
| 58 | if (netif_queue_stopped(dp->dev) && |
| 59 | TX_BUFFS_AVAIL(dp) > 0) |
| 60 | netif_wake_queue(dp->dev); |
| 61 | |
Ben Hutchings | de7aca1 | 2012-04-05 14:40:06 +0000 | [diff] [blame] | 62 | 2) An ndo_start_xmit method must not modify the shared parts of a |
Matti Linnanvuori | ce3ba13 | 2008-01-15 06:25:27 -0800 | [diff] [blame] | 63 | cloned SKB. |
| 64 | |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 65 | 3) Do not forget that once you return NETDEV_TX_OK from your |
| 66 | ndo_start_xmit method, it is your driver's responsibility to free |
| 67 | up the SKB and in some finite amount of time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | For example, this means that it is not allowed for your TX |
| 70 | mitigation scheme to let TX packets "hang out" in the TX |
| 71 | ring unreclaimed forever if no new TX packets are sent. |
| 72 | This error can deadlock sockets waiting for send buffer room |
| 73 | to be freed up. |
| 74 | |
Ben Hutchings | e34fac1 | 2012-04-05 14:40:25 +0000 | [diff] [blame] | 75 | If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you |
| 76 | must not keep any reference to that SKB and you must not attempt |
| 77 | to free it up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | Probing guidelines: |
| 80 | |
| 81 | 1) Any hardware layer address you obtain for your device should |
| 82 | be verified. For example, for ethernet check it with |
| 83 | linux/etherdevice.h:is_valid_ether_addr() |
| 84 | |
| 85 | Close/stop guidelines: |
| 86 | |
Ben Hutchings | b3cf654 | 2012-04-05 14:39:47 +0000 | [diff] [blame] | 87 | 1) After the ndo_stop routine has been called, the hardware must |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | not receive or transmit any data. All in flight packets must |
| 89 | be aborted. If necessary, poll or wait for completion of |
| 90 | any reset commands. |
| 91 | |
Ben Hutchings | b3cf654 | 2012-04-05 14:39:47 +0000 | [diff] [blame] | 92 | 2) The ndo_stop routine will be called by unregister_netdevice |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if device is still UP. |