Skip to content

Commit 6a6e3e9

Browse files
yanzhai-cfopsiff
authored andcommitted
packet: add a generic drop reason for receive
mainline inclusion from mainline-v6.8-rc1 category: bugfix Commit da37845 ("packet: uses kfree_skb() for errors.") switches from consume_skb to kfree_skb to improve error handling. However, this could bring a lot of noises when we monitor real packet drops in kfree_skb[1], because in tpacket_rcv or packet_rcv only packet clones can be freed, not actual packets. Adding a generic drop reason to allow distinguish these "clone drops". [1]: https://lore.kernel.org/netdev/CABWYdi00L+O30Q=Zah28QwZ_5RU-xcxLFUK2Zj08A8MrLk9jzg@mail.gmail.com/ Fixes: da37845 ("packet: uses kfree_skb() for errors.") Suggested-by: Eric Dumazet <edumazet@google.com> Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com> Signed-off-by: Yan Zhai <yan@cloudflare.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://lore.kernel.org/r/ZW4piNbx3IenYnuw@debian.debian Signed-off-by: Jakub Kicinski <kuba@kernel.org> (cherry picked from commit 2f57dd9) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 69ded34 commit 6a6e3e9

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

include/net/dropreason-core.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
FN(IPV6_NDISC_NS_OTHERHOST) \
8282
FN(QUEUE_PURGE) \
8383
FN(TC_ERROR) \
84+
FN(PACKET_SOCK_ERROR) \
8485
FNe(MAX)
8586

8687
/**
@@ -348,6 +349,11 @@ enum skb_drop_reason {
348349
SKB_DROP_REASON_QUEUE_PURGE,
349350
/** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */
350351
SKB_DROP_REASON_TC_ERROR,
352+
/**
353+
* @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors
354+
* after its filter matches an incoming packet.
355+
*/
356+
SKB_DROP_REASON_PACKET_SOCK_ERROR,
351357
/**
352358
* @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
353359
* shouldn't be used as a real 'reason' - only for tracing code gen

net/packet/af_packet.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,13 +2168,13 @@ static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
21682168
static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
21692169
struct packet_type *pt, struct net_device *orig_dev)
21702170
{
2171+
enum skb_drop_reason drop_reason = SKB_CONSUMED;
21712172
struct sock *sk;
21722173
struct sockaddr_ll *sll;
21732174
struct packet_sock *po;
21742175
u8 *skb_head = skb->data;
21752176
int skb_len = skb->len;
21762177
unsigned int snaplen, res;
2177-
bool is_drop_n_account = false;
21782178

21792179
if (skb->pkt_type == PACKET_LOOPBACK)
21802180
goto drop;
@@ -2264,26 +2264,24 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
22642264
return 0;
22652265

22662266
drop_n_acct:
2267-
is_drop_n_account = true;
22682267
atomic_inc(&po->tp_drops);
22692268
atomic_inc(&sk->sk_drops);
2269+
drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
22702270

22712271
drop_n_restore:
22722272
if (skb_head != skb->data && skb_shared(skb)) {
22732273
skb->data = skb_head;
22742274
skb->len = skb_len;
22752275
}
22762276
drop:
2277-
if (!is_drop_n_account)
2278-
consume_skb(skb);
2279-
else
2280-
kfree_skb(skb);
2277+
kfree_skb_reason(skb, drop_reason);
22812278
return 0;
22822279
}
22832280

22842281
static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
22852282
struct packet_type *pt, struct net_device *orig_dev)
22862283
{
2284+
enum skb_drop_reason drop_reason = SKB_CONSUMED;
22872285
struct sock *sk;
22882286
struct packet_sock *po;
22892287
struct sockaddr_ll *sll;
@@ -2297,7 +2295,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
22972295
struct sk_buff *copy_skb = NULL;
22982296
struct timespec64 ts;
22992297
__u32 ts_status;
2300-
bool is_drop_n_account = false;
23012298
unsigned int slot_id = 0;
23022299
int vnet_hdr_sz = 0;
23032300

@@ -2550,19 +2547,16 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
25502547
skb->len = skb_len;
25512548
}
25522549
drop:
2553-
if (!is_drop_n_account)
2554-
consume_skb(skb);
2555-
else
2556-
kfree_skb(skb);
2550+
kfree_skb_reason(skb, drop_reason);
25572551
return 0;
25582552

25592553
drop_n_account:
25602554
spin_unlock(&sk->sk_receive_queue.lock);
25612555
atomic_inc(&po->tp_drops);
2562-
is_drop_n_account = true;
2556+
drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
25632557

25642558
sk->sk_data_ready(sk);
2565-
kfree_skb(copy_skb);
2559+
kfree_skb_reason(copy_skb, drop_reason);
25662560
goto drop_n_restore;
25672561
}
25682562

0 commit comments

Comments
 (0)