History

net: tun: track dropped skb via kfree_skb_reason()

The TUN can be used as vhost-net backend. E.g, the tun_net_xmit() is the interface to forward the skb from TUN to vhost-net/virtio-net. However, there are many "goto drop" in the TUN driver. Therefore, the kfree_skb_reason() is involved at each "goto drop" to help userspace ftrace/ebpf to track the reason for the loss of packets. The below reasons are introduced: - SKB_DROP_REASON_DEV_READY - SKB_DROP_REASON_NOMEM - SKB_DROP_REASON_HDR_TRUNC - SKB_DROP_REASON_TAP_FILTER - SKB_DROP_REASON_TAP_TXFILTER

Dongli Zhang committed 4 years ago

Commit 4b4f0521 parent 45a15d83 files · +51 −9Browse files

Unified · Split

3 files
  1. drivers/net/tun.c +28 −9
  2. include/linux/skbuff.h +18 −0
  3. include/trace/events/skb.h +5 −0
drivers/net/tun.c+28−9
@@ -1058,6 +1058,7 @@ static unsigned int run_ebpf_filter(struct tun_struct *tun, · 1057 unmodified lines
1058static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)1058static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1059{1059{
1060 struct tun_struct *tun = netdev_priv(dev);1060 struct tun_struct *tun = netdev_priv(dev);
1061 enum skb_drop_reason drop_reason;
1061 int txq = skb->queue_mapping;1062 int txq = skb->queue_mapping;
1062 struct netdev_queue *queue;1063 struct netdev_queue *queue;
1063 struct tun_file *tfile;1064 struct tun_file *tfile;
@@ -1067,8 +1068,10 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · 3 unmodified lines
1067 tfile = rcu_dereference(tun->tfiles[txq]);1068 tfile = rcu_dereference(tun->tfiles[txq]);
1068 1069
1069 /* Drop packet if interface is not attached */1070 /* Drop packet if interface is not attached */
1070 if (!tfile)1071 if (!tfile) {
1072 drop_reason = SKB_DROP_REASON_DEV_READY;
1071 goto drop;1073 goto drop;
1074 }
1072 1075
1073 if (!rcu_dereference(tun->steering_prog))1076 if (!rcu_dereference(tun->steering_prog))
1074 tun_automq_xmit(tun, skb);1077 tun_automq_xmit(tun, skb);
@@ -1078,22 +1081,32 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · 3 unmodified lines
1078 /* Drop if the filter does not like it.1081 /* Drop if the filter does not like it.
1079 * This is a noop if the filter is disabled.1082 * This is a noop if the filter is disabled.
1080 * Filter can be enabled only for the TAP devices. */1083 * Filter can be enabled only for the TAP devices. */
1081 if (!check_filter(&tun->txflt, skb))1084 if (!check_filter(&tun->txflt, skb)) {
1085 drop_reason = SKB_DROP_REASON_TAP_TXFILTER;
1082 goto drop;1086 goto drop;
1087 }
1083 1088
1084 if (tfile->socket.sk->sk_filter &&1089 if (tfile->socket.sk->sk_filter &&
1085 sk_filter(tfile->socket.sk, skb))1090 sk_filter(tfile->socket.sk, skb)) {
1091 drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
1086 goto drop;1092 goto drop;
1093 }
1087 1094
1088 len = run_ebpf_filter(tun, skb, len);1095 len = run_ebpf_filter(tun, skb, len);
1089 if (len == 0)1096 if (len == 0) {
1097 drop_reason = SKB_DROP_REASON_TAP_FILTER;
1090 goto drop;1098 goto drop;
1099 }
1091 1100
1092 if (pskb_trim(skb, len))1101 if (pskb_trim(skb, len)) {
1102 drop_reason = SKB_DROP_REASON_NOMEM;
1093 goto drop;1103 goto drop;
1104 }
1094 1105
1095 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))1106 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) {
1107 drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
1096 goto drop;1108 goto drop;
1109 }
1097 1110
1098 skb_tx_timestamp(skb);1111 skb_tx_timestamp(skb);
1099 1112
@@ -1104,8 +1117,10 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · 4 unmodified lines
1104 1117
1105 nf_reset_ct(skb);1118 nf_reset_ct(skb);
1106 1119
1107 if (ptr_ring_produce(&tfile->tx_ring, skb))1120 if (ptr_ring_produce(&tfile->tx_ring, skb)) {
1121 drop_reason = SKB_DROP_REASON_FULL_RING;
1108 goto drop;1122 goto drop;
1123 }
1109 1124
1110 /* NETIF_F_LLTX requires to do our own update of trans_start */1125 /* NETIF_F_LLTX requires to do our own update of trans_start */
1111 queue = netdev_get_tx_queue(dev, txq);1126 queue = netdev_get_tx_queue(dev, txq);
@@ -1122,7 +1137,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · 10 unmodified lines
1122drop:1137drop:
1123 atomic_long_inc(&dev->tx_dropped);1138 atomic_long_inc(&dev->tx_dropped);
1124 skb_tx_error(skb);1139 skb_tx_error(skb);
1125 kfree_skb(skb);1140 kfree_skb_reason(skb, drop_reason);
1126 rcu_read_unlock();1141 rcu_read_unlock();
1127 return NET_XMIT_DROP;1142 return NET_XMIT_DROP;
1128}1143}
@@ -1720,6 +1735,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · 591 unmodified lines
1720 u32 rxhash = 0;1735 u32 rxhash = 0;
1721 int skb_xdp = 1;1736 int skb_xdp = 1;
1722 bool frags = tun_napi_frags_enabled(tfile);1737 bool frags = tun_napi_frags_enabled(tfile);
1738 enum skb_drop_reason drop_reason;
1723 1739
1724 if (!(tun->flags & IFF_NO_PI)) {1740 if (!(tun->flags & IFF_NO_PI)) {
1725 if (len < sizeof(pi))1741 if (len < sizeof(pi))
@@ -1823,9 +1839,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · 97 unmodified lines
1823 1839
1824 if (err) {1840 if (err) {
1825 err = -EFAULT;1841 err = -EFAULT;
1842 drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
1826drop:1843drop:
1827 atomic_long_inc(&tun->dev->rx_dropped);1844 atomic_long_inc(&tun->dev->rx_dropped);
1828 kfree_skb(skb);1845 kfree_skb_reason(skb, drop_reason);
1829 if (frags) {1846 if (frags) {
1830 tfile->napi.skb = NULL;1847 tfile->napi.skb = NULL;
1831 mutex_unlock(&tfile->napi_mutex);1848 mutex_unlock(&tfile->napi_mutex);
@@ -1872,6 +1889,7 @@ drop: · 40 unmodified lines
1872 case IFF_TAP:1889 case IFF_TAP:
1873 if (frags && !pskb_may_pull(skb, ETH_HLEN)) {1890 if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
1874 err = -ENOMEM;1891 err = -ENOMEM;
1892 drop_reason = SKB_DROP_REASON_HDR_TRUNC;
1875 goto drop;1893 goto drop;
1876 }1894 }
1877 skb->protocol = eth_type_trans(skb, tun->dev);1895 skb->protocol = eth_type_trans(skb, tun->dev);
@@ -1925,6 +1943,7 @@ drop: · 47 unmodified lines
1925 if (unlikely(!(tun->dev->flags & IFF_UP))) {1943 if (unlikely(!(tun->dev->flags & IFF_UP))) {
1926 err = -EIO;1944 err = -EIO;
1927 rcu_read_unlock();1945 rcu_read_unlock();
1946 drop_reason = SKB_DROP_REASON_DEV_READY;
1928 goto drop;1947 goto drop;
1929 }1948 }
1930 1949
include/linux/skbuff.h+18−0
@@ -424,7 +424,25 @@ enum skb_drop_reason { · 423 unmodified lines
424 SKB_DROP_REASON_DEV_HDR, /* device driver specific424 SKB_DROP_REASON_DEV_HDR, /* device driver specific
425 * header/metadata is invalid425 * header/metadata is invalid
426 */426 */
427 /* the device is not ready to xmit/recv due to any of its data
428 * structure that is not up/ready/initialized, e.g., the IFF_UP is
429 * not set, or driver specific tun->tfiles[txq] is not initialized
430 */
431 SKB_DROP_REASON_DEV_READY,
427 SKB_DROP_REASON_FULL_RING, /* ring buffer is full */432 SKB_DROP_REASON_FULL_RING, /* ring buffer is full */
433 SKB_DROP_REASON_NOMEM, /* error due to OOM */
434 SKB_DROP_REASON_HDR_TRUNC, /* failed to trunc/extract the header
435 * from networking data, e.g., failed
436 * to pull the protocol header from
437 * frags via pskb_may_pull()
438 */
439 SKB_DROP_REASON_TAP_FILTER, /* dropped by (ebpf) filter directly
440 * attached to tun/tap, e.g., via
441 * TUNSETFILTEREBPF
442 */
443 SKB_DROP_REASON_TAP_TXFILTER, /* dropped by tx filter implemented
444 * at tun/tap, e.g., check_filter()
445 */
428 SKB_DROP_REASON_MAX,446 SKB_DROP_REASON_MAX,
429};447};
430 448
include/trace/events/skb.h+5−0
@@ -55,7 +55,12 @@ · 54 unmodified lines
55 EM(SKB_DROP_REASON_SKB_GSO_SEG, SKB_GSO_SEG) \55 EM(SKB_DROP_REASON_SKB_GSO_SEG, SKB_GSO_SEG) \
56 EM(SKB_DROP_REASON_SKB_UCOPY_FAULT, SKB_UCOPY_FAULT) \56 EM(SKB_DROP_REASON_SKB_UCOPY_FAULT, SKB_UCOPY_FAULT) \
57 EM(SKB_DROP_REASON_DEV_HDR, DEV_HDR) \57 EM(SKB_DROP_REASON_DEV_HDR, DEV_HDR) \
58 EM(SKB_DROP_REASON_DEV_READY, DEV_READY) \
58 EM(SKB_DROP_REASON_FULL_RING, FULL_RING) \59 EM(SKB_DROP_REASON_FULL_RING, FULL_RING) \
60 EM(SKB_DROP_REASON_NOMEM, NOMEM) \
61 EM(SKB_DROP_REASON_HDR_TRUNC, HDR_TRUNC) \
62 EM(SKB_DROP_REASON_TAP_FILTER, TAP_FILTER) \
63 EM(SKB_DROP_REASON_TAP_TXFILTER, TAP_TXFILTER) \
59 EMe(SKB_DROP_REASON_MAX, MAX)64 EMe(SKB_DROP_REASON_MAX, MAX)
60 65
61#undef EM66#undef EM

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t