Skip to content

Commit de55cc5

Browse files
Yanjun ZhangNipaLocal
authored andcommitted
tap/tun: add stats accounting when failed to transfer data to user
To more accurately detect packet dropped, we add the dropped packet counter with the device when kfree_skb is called because of failing to transfer data to user space. Signed-off-by: Yanjun Zhang <zhangyanjun@cestc.cn> Signed-off-by: NipaLocal <nipa@local>
1 parent bdcfc1c commit de55cc5

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

drivers/net/tap.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ static ssize_t tap_do_read(struct tap_queue *q,
759759
{
760760
DEFINE_WAIT(wait);
761761
ssize_t ret = 0;
762+
struct tap_dev *tap;
763+
enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
762764

763765
if (!iov_iter_count(to)) {
764766
kfree_skb(skb);
@@ -794,10 +796,16 @@ static ssize_t tap_do_read(struct tap_queue *q,
794796
put:
795797
if (skb) {
796798
ret = tap_put_user(q, skb, to);
797-
if (unlikely(ret < 0))
798-
kfree_skb(skb);
799-
else
799+
if (unlikely(ret < 0)) {
800+
kfree_skb_reason(skb, drop_reason);
801+
rcu_read_lock();
802+
tap = rcu_dereference(q->tap);
803+
if (tap && tap->count_rx_dropped)
804+
tap->count_rx_dropped(tap);
805+
rcu_read_unlock();
806+
} else {
800807
consume_skb(skb);
808+
}
801809
}
802810
return ret;
803811
}

drivers/net/tun.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
21572157
{
21582158
ssize_t ret;
21592159
int err;
2160+
enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
21602161

21612162
if (!iov_iter_count(to)) {
21622163
tun_ptr_free(ptr);
@@ -2179,10 +2180,12 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
21792180
struct sk_buff *skb = ptr;
21802181

21812182
ret = tun_put_user(tun, tfile, skb, to);
2182-
if (unlikely(ret < 0))
2183-
kfree_skb(skb);
2184-
else
2183+
if (unlikely(ret < 0)) {
2184+
dev_core_stats_tx_dropped_inc(tun->dev);
2185+
kfree_skb_reason(skb, drop_reason);
2186+
} else {
21852187
consume_skb(skb);
2188+
}
21862189
}
21872190

21882191
return ret;

0 commit comments

Comments
 (0)