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
10581058 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
10591059 {
10601060 struct tun_struct *tun = netdev_priv(dev);
1061+ enum skb_drop_reason drop_reason;
10611062 int txq = skb->queue_mapping;
10621063 struct netdev_queue *queue;
10631064 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
10671068 tfile = rcu_dereference(tun->tfiles[txq]);
10681069
10691070 /* Drop packet if interface is not attached */
1070− if (!tfile)
1071+ if (!tfile) {
1072+ drop_reason = SKB_DROP_REASON_DEV_READY;
10711073 goto drop;
1074+ }
10721075
10731076 if (!rcu_dereference(tun->steering_prog))
10741077 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
10781081 /* Drop if the filter does not like it.
10791082 * This is a noop if the filter is disabled.
10801083 * 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;
10821086 goto drop;
1087+ }
10831088
10841089 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;
10861092 goto drop;
1093+ }
10871094
10881095 len = run_ebpf_filter(tun, skb, len);
1089− if (len == 0)
1096+ if (len == 0) {
1097+ drop_reason = SKB_DROP_REASON_TAP_FILTER;
10901098 goto drop;
1099+ }
10911100
1092− if (pskb_trim(skb, len))
1101+ if (pskb_trim(skb, len)) {
1102+ drop_reason = SKB_DROP_REASON_NOMEM;
10931103 goto drop;
1104+ }
10941105
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;
10961108 goto drop;
1109+ }
10971110
10981111 skb_tx_timestamp(skb);
10991112
@@ -1104,8 +1117,10 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · 4 unmodified lines
11041117
11051118 nf_reset_ct(skb);
11061119
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;
11081122 goto drop;
1123+ }
11091124
11101125 /* NETIF_F_LLTX requires to do our own update of trans_start */
11111126 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
11221137 drop:
11231138 atomic_long_inc(&dev->tx_dropped);
11241139 skb_tx_error(skb);
1125− kfree_skb(skb);
1140+ kfree_skb_reason(skb, drop_reason);
11261141 rcu_read_unlock();
11271142 return NET_XMIT_DROP;
11281143 }
@@ -1720,6 +1735,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · 591 unmodified lines
17201735 u32 rxhash = 0;
17211736 int skb_xdp = 1;
17221737 bool frags = tun_napi_frags_enabled(tfile);
1738+ enum skb_drop_reason drop_reason;
17231739
17241740 if (!(tun->flags & IFF_NO_PI)) {
17251741 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
18231839
18241840 if (err) {
18251841 err = -EFAULT;
1842+ drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
18261843 drop:
18271844 atomic_long_inc(&tun->dev->rx_dropped);
1828− kfree_skb(skb);
1845+ kfree_skb_reason(skb, drop_reason);
18291846 if (frags) {
18301847 tfile->napi.skb = NULL;
18311848 mutex_unlock(&tfile->napi_mutex);
@@ -1872,6 +1889,7 @@ drop: · 40 unmodified lines
18721889 case IFF_TAP:
18731890 if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
18741891 err = -ENOMEM;
1892+ drop_reason = SKB_DROP_REASON_HDR_TRUNC;
18751893 goto drop;
18761894 }
18771895 skb->protocol = eth_type_trans(skb, tun->dev);
@@ -1925,6 +1943,7 @@ drop: · 47 unmodified lines
19251943 if (unlikely(!(tun->dev->flags & IFF_UP))) {
19261944 err = -EIO;
19271945 rcu_read_unlock();
1946+ drop_reason = SKB_DROP_REASON_DEV_READY;
19281947 goto drop;
19291948 }
19301949
include/linux/skbuff.h+18−0
@@ -424,7 +424,25 @@ enum skb_drop_reason { · 423 unmodified lines
424424 SKB_DROP_REASON_DEV_HDR, /* device driver specific
425425 * header/metadata is invalid
426426 */
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,
427432 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+ */
428446 SKB_DROP_REASON_MAX,
429447 };
430448
include/trace/events/skb.h+5−0
@@ -55,7 +55,12 @@ · 54 unmodified lines
5555 EM(SKB_DROP_REASON_SKB_GSO_SEG, SKB_GSO_SEG) \
5656 EM(SKB_DROP_REASON_SKB_UCOPY_FAULT, SKB_UCOPY_FAULT) \
5757 EM(SKB_DROP_REASON_DEV_HDR, DEV_HDR) \
58+ EM(SKB_DROP_REASON_DEV_READY, DEV_READY) \
5859 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) \
5964 EMe(SKB_DROP_REASON_MAX, MAX)
6065
6166 #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