Skip to contentSkip to conversation

packet: add a generic drop reason for receive

PATCH Superseded Yan Zhai 3 participants 4 messages Dec 1, 2023 – Dec 3, 2023

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yan Zhai

author
#0 lore raw

PATCH v3 1/1 packet: add a generic drop reason for receive

Commit da37845fdce2 ("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 da37845fdce2 ("packet: uses kfree_skb() for errors.")

---
v2->v3: removed an unused variable
v1->v2: fixups suggested by Eric Dumazet
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/dropreason-core.h |  6 ++++++
 net/packet/af_packet.c        | 26 +++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
include/net/dropreason-core.h +6 -0
index 3c70ad53a49c..278e4c7d465c 100644
@@ -86,6 +86,7 @@
 	FN(IPV6_NDISC_NS_OTHERHOST)	\
 	FN(QUEUE_PURGE)			\
 	FN(TC_ERROR)			\
+	FN(PACKET_SOCK_ERROR)		\
 	FNe(MAX)
 
 /**
@@ -378,6 +379,11 @@ enum skb_drop_reason {
 	SKB_DROP_REASON_QUEUE_PURGE,
 	/** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */
 	SKB_DROP_REASON_TC_ERROR,
+	/**
+	 * @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors
+	 * after its filter matches an incoming packet.
+	 */
+	SKB_DROP_REASON_PACKET_SOCK_ERROR,
 	/**
 	 * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
 	 * shouldn't be used as a real 'reason' - only for tracing code gen
net/packet/af_packet.c +13 -13
index a84e00b5904b..933fdfaacc44 100644
@@ -2127,7 +2127,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 	u8 *skb_head = skb->data;
 	int skb_len = skb->len;
 	unsigned int snaplen, res;
-	bool is_drop_n_account = false;
+	enum skb_drop_reason drop_reason = SKB_CONSUMED;
 
 	if (skb->pkt_type == PACKET_LOOPBACK)
 		goto drop;
@@ -2161,6 +2161,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 	res = run_filter(skb, sk, snaplen);
 	if (!res)
 		goto drop_n_restore;
+
+	/* skb will only be "consumed" not "dropped" before this */
+	drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
+
 	if (snaplen > res)
 		snaplen = res;
 
@@ -2217,7 +2221,6 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 	return 0;
 
 drop_n_acct:
-	is_drop_n_account = true;
 	atomic_inc(&po->tp_drops);
 	atomic_inc(&sk->sk_drops);
 
@@ -2227,10 +2230,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 		skb->len = skb_len;
 	}
 drop:
-	if (!is_drop_n_account)
-		consume_skb(skb);
-	else
-		kfree_skb(skb);
+	kfree_skb_reason(skb, drop_reason);
 	return 0;
 }
 
@@ -2250,9 +2250,9 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	struct sk_buff *copy_skb = NULL;
 	struct timespec64 ts;
 	__u32 ts_status;
-	bool is_drop_n_account = false;
 	unsigned int slot_id = 0;
 	int vnet_hdr_sz = 0;
+	enum skb_drop_reason drop_reason = SKB_CONSUMED;
 
 	/* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
 	 * We may add members to them until current aligned size without forcing
@@ -2355,6 +2355,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 			vnet_hdr_sz = 0;
 		}
 	}
+
+	/* skb will only be "consumed" not "dropped" before this */
+	drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
+
 	spin_lock(&sk->sk_receive_queue.lock);
 	h.raw = packet_current_rx_frame(po, skb,
 					TP_STATUS_KERNEL, (macoff+snaplen));
@@ -2498,19 +2502,15 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		skb->len = skb_len;
 	}
 drop:
-	if (!is_drop_n_account)
-		consume_skb(skb);
-	else
-		kfree_skb(skb);
+	kfree_skb_reason(skb, drop_reason);
 	return 0;
 
 drop_n_account:
 	spin_unlock(&sk->sk_receive_queue.lock);
 	atomic_inc(&po->tp_drops);
-	is_drop_n_account = true;
 
 	sk->sk_data_ready(sk);
-	kfree_skb(copy_skb);
+	kfree_skb_reason(copy_skb, drop_reason);
 	goto drop_n_restore;
 }
 
Signature · 1 line
2.30.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 7 lines · Yan Zhai

Commit da37845fdce2 ("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.

Quoted 7 lines

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 da37845fdce2 ("packet: uses kfree_skb() for errors.")

---

maintainer
#2 lore raw
In reply to Yan Zhai
Quoted 51 lines · Yan Zhai

Commit da37845fdce2 ("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 da37845fdce2 ("packet: uses kfree_skb() for errors.")

---
v2->v3: removed an unused variable
v1->v2: fixups suggested by Eric Dumazet
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/dropreason-core.h |  6 ++++++
 net/packet/af_packet.c        | 26 +++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
include/net/dropreason-core.h +6 -0
index 3c70ad53a49c..278e4c7d465c 100644
@@ -86,6 +86,7 @@
 	FN(IPV6_NDISC_NS_OTHERHOST)	\
 	FN(QUEUE_PURGE)			\
 	FN(TC_ERROR)			\
+	FN(PACKET_SOCK_ERROR)		\
 	FNe(MAX)
 
 /**
@@ -378,6 +379,11 @@ enum skb_drop_reason {
 	SKB_DROP_REASON_QUEUE_PURGE,
 	/** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */
 	SKB_DROP_REASON_TC_ERROR,
+	/**
+	 * @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors
+	 * after its filter matches an incoming packet.
+	 */
+	SKB_DROP_REASON_PACKET_SOCK_ERROR,
 	/**
 	 * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
 	 * shouldn't be used as a real 'reason' - only for tracing code gen
net/packet/af_packet.c +0 -0
index a84e00b5904b..933fdfaacc44 100644

net/packet/af_packet.c static int packet_rcv(struct sk_buff *skb, struct net_device *dev, · line 2130

Quoted 5 lines
net/packet/af_packet.c +1 -1
@@ -2127,7 +2127,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 	u8 *skb_head = skb->data;
 	int skb_len = skb->len;
 	unsigned int snaplen, res;
-	bool is_drop_n_account = false;
+	enum skb_drop_reason drop_reason = SKB_CONSUMED;

Reverse xmas tree

net/packet/af_packet.c static int packet_rcv(struct sk_buff *skb, struct net_device *dev, · line 2167

Quoted 11 lines
net/packet/af_packet.c +4 -0
 	if (skb->pkt_type == PACKET_LOOPBACK)
 		goto drop;
@@ -2161,6 +2161,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
 	res = run_filter(skb, sk, snaplen);
 	if (!res)
 		goto drop_n_restore;
+
+	/* skb will only be "consumed" not "dropped" before this */
+	drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
+

This can be set in drop_n_account, rather than the common path.

Same in tpacket_rcv.

author
#3 lore raw
Quoted 52 lines · Willem de Bruijn
Quoted 50 lines · Yan Zhai

Commit da37845fdce2 ("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 da37845fdce2 ("packet: uses kfree_skb() for errors.")

---
v2->v3: removed an unused variable
v1->v2: fixups suggested by Eric Dumazet
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/dropreason-core.h |  6 ++++++
 net/packet/af_packet.c        | 26 +++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
include/net/dropreason-core.h +6 -0
index 3c70ad53a49c..278e4c7d465c 100644
@@ -86,6 +86,7 @@
      FN(IPV6_NDISC_NS_OTHERHOST)     \
      FN(QUEUE_PURGE)                 \
      FN(TC_ERROR)                    \
+     FN(PACKET_SOCK_ERROR)           \
      FNe(MAX)
 
 /**
@@ -378,6 +379,11 @@ enum skb_drop_reason {
      SKB_DROP_REASON_QUEUE_PURGE,
      /** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */
      SKB_DROP_REASON_TC_ERROR,
+     /**
+      * @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors
+      * after its filter matches an incoming packet.
+      */
+     SKB_DROP_REASON_PACKET_SOCK_ERROR,
      /**
       * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
       * shouldn't be used as a real 'reason' - only for tracing code gen
net/packet/af_packet.c +0 -0
index a84e00b5904b..933fdfaacc44 100644
Quoted 9 lines

net/packet/af_packet.c static int packet_rcv(struct sk_buff *skb, struct net_device *dev, · line 2130

Quoted 6 lines
net/packet/af_packet.c +1 -1
@@ -2127,7 +2127,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
      u8 *skb_head = skb->data;
      int skb_len = skb->len;
      unsigned int snaplen, res;
-     bool is_drop_n_account = false;
+     enum skb_drop_reason drop_reason = SKB_CONSUMED;

Reverse xmas tree

oh I didn't know we have requirements on variable ordering. Will pay attention in future.

Quoted 14 lines

net/packet/af_packet.c static int packet_rcv(struct sk_buff *skb, struct net_device *dev, · line 2167

Quoted 11 lines
net/packet/af_packet.c +4 -0
      if (skb->pkt_type == PACKET_LOOPBACK)
              goto drop;
@@ -2161,6 +2161,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
      res = run_filter(skb, sk, snaplen);
      if (!res)
              goto drop_n_restore;
+
+     /* skb will only be "consumed" not "dropped" before this */
+     drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
+

This can be set in drop_n_account, rather than the common path.

Quoted 1 line

Same in tpacket_rcv.

Sure, let me shoot a v4 to move it.