net/af_packet: provide packet drop stats
PATCH 1/1 net/af_packet: provide packet drop stats
The Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with PACKET_STATISTICS resets the internal counters. So the caller needs to keep track of the total count on its own.
Next, I have added a counter for the case when mbuf couldn't be allocated.
--- drivers/net/af_packet/rte_eth_af_packet.c | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
drivers/net/af_packet/rte_eth_af_packet.c +30 -2
@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; };struct __rte_cache_aligned pkt_tx_queue {
@@ -145,8 +147,10 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf) = ppd->tp_snaplen;
@@ -417,17 +421,37 @@ static int eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats) { unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; +unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ; +unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +struct tpacket_stats iface_stats; +socklen_t iface_stats_len =sizeof (struct tpacket_stats ); + imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS );for (i =0 ; i < imax; i++) { +/* query dropped packets counter from socket */ +if (internal->rx_queue[i].sockfd !=-1 && +getsockopt (internal->rx_queue[i].sockfd,SOL_PACKET , +PACKET_STATISTICS , &iface_stats, + &iface_stats_len) >-1 ) { +/* +* keep total because each call to getsocketopt with PACKET_STATISTICS +* reset the counter of the socket +*/ + internal->rx_queue[i].rx_dropped_pkts += iface_stats.tp_drops; + } + igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + igb_stats->q_errors[i] = internal->rx_queue[i].rx_dropped_pkts; + rx_total += igb_stats->q_ipackets[i]; rx_bytes_total += igb_stats->q_ibytes[i]; + rx_dropped_total += igb_stats->q_errors[i]; + rx_nombuf_total += internal->rx_queue[i].rx_nombuf; } imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
@@ -442,6 +466,8 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats) igb_stats->ipackets = rx_total; igb_stats->ibytes = rx_bytes_total; + igb_stats->imissed = rx_dropped_total; + igb_stats->rx_nombuf = rx_nombuf_total; igb_stats->opackets = tx_total; igb_stats->oerrors = tx_err_total; igb_stats->obytes = tx_bytes_total;
@@ -457,6 +483,8 @@ eth_stats_reset(struct rte_eth_dev *dev)for (i =0 ; i < internal->nb_queues; i++) { internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; + internal->rx_queue[i].rx_nombuf =0 ; + internal->rx_queue[i].rx_dropped_pkts =0 ; }for (i =0 ; i < internal->nb_queues; i++) {
Signature · 1 line
2.34.1
Quoted 43 lines · On Thu, 16 Jan 2025 17:17:03 +0100 Stefan Laesser…
The Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with
PACKET_STATISTICSresets the internal counters. So the caller needs to keep track of the total count on its own.Next, I have added a counter for the case when mbuf couldn't be allocated.
Signed-off-by Stefan Laesser <[email protected]>--- drivers/net/af_packet/rte_eth_af_packet.c | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)drivers/net/af_packet/rte_eth_af_packet.c +5
-1@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; };struct __rte_cache_aligned pkt_tx_queue {@@ -145,8 +147,10 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf) = ppd->tp_snaplen;@@ -417,17 +421,37 @@ static int eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
drivers/net/af_packet/rte_eth_af_packet.c static int · line 430
Quoted 10 lines
drivers/net/af_packet/rte_eth_af_packet.c +4
-1{unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; +unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ; +unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +struct tpacket_stats iface_stats; +socklen_t iface_stats_len =sizeof (struct tpacket_stats );
This declaration could be moved inside the loop.
Quoted 9 lines
drivers/net/af_packet/rte_eth_af_packet.c +6
-0+ imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS );for (i =0 ; i < imax; i++) { +/* query dropped packets counter from socket */ +if (internal->rx_queue[i].sockfd !=-1 && +getsockopt (internal->rx_queue[i].sockfd,SOL_PACKET , +PACKET_STATISTICS , &iface_stats, + &iface_stats_len) >-1 ) {
drivers/net/af_packet/rte_eth_af_packet.c static int · line 449
Quoted 10 lines
drivers/net/af_packet/rte_eth_af_packet.c +8
-0+/* +* keep total because each call to getsocketopt with PACKET_STATISTICS +* reset the counter of the socket +*/ + internal->rx_queue[i].rx_dropped_pkts += iface_stats.tp_drops; + } + igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + igb_stats->q_errors[i] = internal->rx_queue[i].rx_dropped_pkts;
Dropped packets are not errors; at least most other drivers do not report missed packets as errors. Should be imissed statistic.
Quoted 18 lines
drivers/net/af_packet/rte_eth_af_packet.c +5
-0+ rx_total += igb_stats->q_ipackets[i]; rx_bytes_total += igb_stats->q_ibytes[i]; + rx_dropped_total += igb_stats->q_errors[i]; + rx_nombuf_total += internal->rx_queue[i].rx_nombuf; } imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?@@ -442,6 +466,8 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats) igb_stats->ipackets = rx_total; igb_stats->ibytes = rx_bytes_total; + igb_stats->imissed = rx_dropped_total; + igb_stats->rx_nombuf = rx_nombuf_total; igb_stats->opackets = tx_total; igb_stats->oerrors = tx_err_total; igb_stats->obytes = tx_bytes_total;
drivers/net/af_packet/rte_eth_af_packet.c eth_stats_reset(struct rte_eth_dev *dev) · line 490
Quoted 8 lines
drivers/net/af_packet/rte_eth_af_packet.c +2
-0@@ -457,6 +483,8 @@ eth_stats_reset(struct rte_eth_dev *dev)for (i =0 ; i < internal->nb_queues; i++) { internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; + internal->rx_queue[i].rx_nombuf =0 ; + internal->rx_queue[i].rx_dropped_pkts =0 ; }for (i =0 ; i < internal->nb_queues; i++) {
What about stats reset?
Quoted 62 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Thursday, January 16, 2025 5:25 PM
To: Stefan Lässer <[email protected]>
Cc: John W. Linville <[email protected]>; [email protected]
Subject: Re: [PATCH]net/af_packet: provide packet drop statsQuoted 34 lines · On Thu, 16 Jan 2025 17:17:03 +0100 Stefan Laesser…
The Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with
PACKET_STATISTICSresets the internal counters. So the caller needs to keep track of the total count on its own.Next, I have added a counter for the case when mbuf couldn't be allocated.
Signed-off-by Stefan Laesser <[email protected]>--- drivers/net/af_packet/rte_eth_af_packet.c | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)drivers/net/af_packet/rte_eth_af_packet.c +2
-0@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; }; struct __rte_cache_aligned pkt_tx_queue { @@ -145,8 +147,10 @@
eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)drivers/net/af_packet/rte_eth_af_packet.c eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
Quoted 10 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-1/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf)=
drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 6 lines
ppd->tp_snaplen; @@ -417,17 +421,37 @@ static inteth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats){drivers/net/af_packet/rte_eth_af_packet.c +1
-1unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; + unsigned long rx_total = 0, rx_dropped_total = 0, rx_nombuf_total =0;
Quoted 8 lines
drivers/net/af_packet/rte_eth_af_packet.c static int · line 430
Quoted 6 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +struct tpacket_stats iface_stats; +socklen_t iface_stats_len =sizeof (struct tpacket_stats );This declaration could be moved inside the loop.
Yes, you are right - I will move it inside the loop.
Quoted 18 lines
drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 7 lines
drivers/net/af_packet/rte_eth_af_packet.c +4
-0+ imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); for (i = 0; i < imax; i++) { + /* query dropped packets counter from socket */ + if (internal->rx_queue[i].sockfd != -1 && + getsockopt(internal->rx_queue[i].sockfd,
SOL_PACKET,drivers/net/af_packet/rte_eth_af_packet.c
Quoted 1 line
drivers/net/af_packet/rte_eth_af_packet.c +1
-0+ PACKET_STATISTICS,
&iface_stats,drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 3 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+ &iface_stats_len) > -1) { +/* +* keep total because each call to getsocketopt with
PACKET_STATISTICSdrivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 3 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+* reset the counter of the socket +*/ + internal->rx_queue[i].rx_dropped_pkts +=iface_stats.tp_drops;
Quoted 9 lines
Quoted 6 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+ } + igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + igb_stats->q_errors[i] = internal-rx_queue[i].rx_dropped_pkts;
Dropped packets are not errors; at least most other drivers do not report missed packets as errors. Should be imissed statistic.
The struct rte_eth_stats currently does not contain q_imissed. It only has q_ipackets, q_opackets, q_ibytes, q_obytes and q_errors. The latter is described as "Total number of queue packets received that are dropped.". This is why I have choosen q_errors because the field comment sounds like a good match to me.
As there is no q_imissed, I suggest removing this line from my patch and just adding the imissed total counter:
igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;- igb_stats->q_errors[i] = internal->rx_queue[i].rx_dropped_pkts;
rx_total += igb_stats->q_ipackets[i];
rx_bytes_total += igb_stats->q_ibytes[i];
- rx_dropped_total += igb_stats->q_errors[i];
+ rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts;
rx_nombuf_total += internal->rx_queue[i].rx_nombuf;Do you agree with that?
Quoted 26 lines
Quoted 26 lines
drivers/net/af_packet/rte_eth_af_packet.c +5
-0+ rx_total += igb_stats->q_ipackets[i]; rx_bytes_total += igb_stats->q_ibytes[i]; + rx_dropped_total += igb_stats->q_errors[i]; + rx_nombuf_total += internal->rx_queue[i].rx_nombuf; } imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?@@ -442,6 +466,8 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats) igb_stats->ipackets = rx_total; igb_stats->ibytes = rx_bytes_total; + igb_stats->imissed = rx_dropped_total; + igb_stats->rx_nombuf = rx_nombuf_total; igb_stats->opackets = tx_total; igb_stats->oerrors = tx_err_total;drivers/net/af_packet/rte_eth_af_packet.c +2
-0igb_stats->obytes = tx_bytes_total;@@ -457,6 +483,8 @@ eth_stats_reset(struct rte_eth_dev *dev)for (i =0 ; i < internal->nb_queues; i++) { internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; + internal->rx_queue[i].rx_nombuf =0 ; + internal->rx_queue[i].rx_dropped_pkts =0 ; }
Quoted 3 lines
drivers/net/af_packet/rte_eth_af_packet.c eth_stats_reset(struct rte_eth_dev *dev) · line 490
Quoted 1 line
drivers/net/af_packet/rte_eth_af_packet.c +0
-0for (i =0 ; i < internal->nb_queues; i++) {What about stats reset?
I'm sorry, I don't understand what you mean. The two newly added queue counters are reset in the eth_stats_reset() function. Isn't that the right place to do it?
Thanks for your feedback!
Quoted 101 lines · On Fri, 17 Jan 2025 07:20:05 +0000 Stefan Lässer…
Quoted 62 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Thursday, January 16, 2025 5:25 PM
To: Stefan Lässer <[email protected]>
Cc: John W. Linville <[email protected]>; [email protected]
Subject: Re: [PATCH]net/af_packet: provide packet drop statsQuoted 34 lines · On Thu, 16 Jan 2025 17:17:03 +0100 Stefan Laesser…
The Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with
PACKET_STATISTICSresets the internal counters. So the caller needs to keep track of the total count on its own.Next, I have added a counter for the case when mbuf couldn't be allocated.
Signed-off-by Stefan Laesser <[email protected]>--- drivers/net/af_packet/rte_eth_af_packet.c | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)drivers/net/af_packet/rte_eth_af_packet.c +2
-0@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; }; struct __rte_cache_aligned pkt_tx_queue { @@ -145,8 +147,10 @@
eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)drivers/net/af_packet/rte_eth_af_packet.c eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
Quoted 10 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-1/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf)=
drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 6 lines
ppd->tp_snaplen; @@ -417,17 +421,37 @@ static inteth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats){drivers/net/af_packet/rte_eth_af_packet.c +1
-1unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; + unsigned long rx_total = 0, rx_dropped_total = 0, rx_nombuf_total =0;
Quoted 8 lines
drivers/net/af_packet/rte_eth_af_packet.c static int · line 430
Quoted 6 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; +struct tpacket_stats iface_stats; +socklen_t iface_stats_len =sizeof (struct tpacket_stats );This declaration could be moved inside the loop.
Yes, you are right - I will move it inside the loop.
Quoted 18 lines
drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 7 lines
drivers/net/af_packet/rte_eth_af_packet.c +4
-0+ imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); for (i = 0; i < imax; i++) { + /* query dropped packets counter from socket */ + if (internal->rx_queue[i].sockfd != -1 && + getsockopt(internal->rx_queue[i].sockfd,
SOL_PACKET,drivers/net/af_packet/rte_eth_af_packet.c
Quoted 1 line
drivers/net/af_packet/rte_eth_af_packet.c +1
-0+ PACKET_STATISTICS,
&iface_stats,drivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 3 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+ &iface_stats_len) > -1) { +/* +* keep total because each call to getsocketopt with
PACKET_STATISTICSdrivers/net/af_packet/rte_eth_af_packet.c static int
Quoted 3 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+* reset the counter of the socket +*/ + internal->rx_queue[i].rx_dropped_pkts +=iface_stats.tp_drops;
Quoted 9 lines
Quoted 6 lines
drivers/net/af_packet/rte_eth_af_packet.c +3
-0+ } + igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + igb_stats->q_errors[i] = internal-rx_queue[i].rx_dropped_pkts;
Dropped packets are not errors; at least most other drivers do not report missed packets as errors. Should be imissed statistic.
Quoted 4 lines
The struct
rte_eth_statscurrently does not containq_imissed. It only hasq_ipackets,q_opackets,q_ibytes,q_obytesandq_errors. The latter is described as "Total number of queue packets received that are dropped.". This is why I have choosenq_errorsbecause the field comment sounds like a good match to me.
The comment is open to interpretation.
My preference is that all drivers behave the same, and the reference is one of the original Intel drivers (ixge, e1000, etc) since they were the first.
Quoted 13 lines
As there is no
q_imissed, I suggest removing this line from my patch and just adding the imissed total counter:igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;(quoted diff) +1
-2- igb_stats->q_errors[i] = internal->rx_queue[i].rx_dropped_pkts; rx_total += igb_stats->q_ipackets[i]; rx_bytes_total += igb_stats->q_ibytes[i]; - rx_dropped_total += igb_stats->q_errors[i]; + rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts; rx_nombuf_total += internal->rx_queue[i].rx_nombuf;
Quoted 1 line
Do you agree with that?
Yes, that would be good.
Probably worth renaming igb_stats variable since it is a left over copy/paste from somewhere.
PATCH v2 1/1 net/af_packet: provide packet drop stats
In reply to Stefan LaesserThe Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with PACKET_STATISTICS resets the internal counters. So the caller needs to keep track of the total count on its own.
Next, I have added a counter for the case when mbuf could not be allocated.
---
v2:
* Limited scope of tpacket_stats and socklen_t variables and moved them inside the for-loop in function eth_stats_get.
* Removed q_errors counting as dropped packets should not be counted as errors.
* Renamed parameter igb_stats of function eth_stats_get to stats since it was a copy/paste issue.
drivers/net/af_packet/rte_eth_af_packet.c | 59 +++++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-)
drivers/net/af_packet/rte_eth_af_packet.c +43 -16
@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; };struct __rte_cache_aligned pkt_tx_queue {
@@ -145,8 +147,10 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf) = ppd->tp_snaplen;
@@ -414,37 +418,58 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) }static int -eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *igb_stats) +eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *stats) {unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; +unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ; +unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS );for (i =0 ; i < imax; i++) { - igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; - igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; - rx_total += igb_stats->q_ipackets[i]; - rx_bytes_total += igb_stats->q_ibytes[i]; +struct tpacket_stats iface_stats; +socklen_t iface_stats_len =sizeof (struct tpacket_stats ); + +/* query dropped packets counter from socket */ +if (internal->rx_queue[i].sockfd !=-1 && +getsockopt (internal->rx_queue[i].sockfd,SOL_PACKET , +PACKET_STATISTICS , &iface_stats, + &iface_stats_len) >-1 ) { +/* +* keep total because each call to getsocketopt with PACKET_STATISTICS +* reset the counter of the socket +*/ + internal->rx_queue[i].rx_dropped_pkts += iface_stats.tp_drops; + } + + stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; + stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + + rx_total += stats->q_ipackets[i]; + rx_bytes_total += stats->q_ibytes[i]; + rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts; + rx_nombuf_total += internal->rx_queue[i].rx_nombuf; } imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS );for (i =0 ; i < imax; i++) { - igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; - igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; - tx_total += igb_stats->q_opackets[i]; + stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; + stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; + tx_total += stats->q_opackets[i]; tx_err_total += internal->tx_queue[i].err_pkts; - tx_bytes_total += igb_stats->q_obytes[i]; + tx_bytes_total += stats->q_obytes[i]; } - igb_stats->ipackets = rx_total; - igb_stats->ibytes = rx_bytes_total; - igb_stats->opackets = tx_total; - igb_stats->oerrors = tx_err_total; - igb_stats->obytes = tx_bytes_total; + stats->ipackets = rx_total; + stats->ibytes = rx_bytes_total; + stats->imissed = rx_dropped_total; + stats->rx_nombuf = rx_nombuf_total; + stats->opackets = tx_total; + stats->oerrors = tx_err_total; + stats->obytes = tx_bytes_total;return 0 ; }
@@ -457,6 +482,8 @@ eth_stats_reset(struct rte_eth_dev *dev)for (i =0 ; i < internal->nb_queues; i++) { internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; + internal->rx_queue[i].rx_nombuf =0 ; + internal->rx_queue[i].rx_dropped_pkts =0 ; }for (i =0 ; i < internal->nb_queues; i++) {
Signature · 1 line
2.34.1
Review
Quoted 11 lines · On Tue, 21 Jan 2025 13:26:16 +0100 Stefan Laesser…
(quoted diff) +11
-0+ /* query dropped packets counter from socket */ + if (internal->rx_queue[i].sockfd != -1 && + getsockopt(internal->rx_queue[i].sockfd, SOL_PACKET, + PACKET_STATISTICS, &iface_stats, + &iface_stats_len) > -1) { + /* + * keep total because each call to getsocketopt with PACKET_STATISTICS + * reset the counter of the socket + */ + internal->rx_queue[i].rx_dropped_pkts += iface_stats.tp_drops; + }
Since reading the value clears it, maybe stats_reset should call as well?
Also, queues greater that the RTE_ETHDEV_STAT_CNTRS should still count against the total. And the two loops could be combined.
Maybe something like this?
drivers/net/af_packet/rte_eth_af_packet.c +29 -27
@@ -417,50 +417,51 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)return 0 ; } +/* query dropped packets counter from socket */ +static unsigned int +packet_drop_count (int sockfd) +{ +struct tpacket_stats pkt_stats; +socklen_t pkt_stats_len =sizeof (struct tpacket_stats ); + +if (sockfd ==-1 ) +return 0 ; + +if (getsockopt (sockfd,SOL_PACKET ,PACKET_STATISTICS , &pkt_stats, &pkt_stats_len) <-1 ) +return 0 ; + +return pkt_stats.tp_drops; +} +static int eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *stats) { -unsigned i, imax; +unsigned int i;unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ;unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { -struct tpacket_stats iface_stats; -socklen_t iface_stats_len =sizeof (struct tpacket_stats ); - -/* query dropped packets counter from socket */ -if (internal->rx_queue[i].sockfd !=-1 && -getsockopt (internal->rx_queue[i].sockfd,SOL_PACKET , -PACKET_STATISTICS , &iface_stats, - &iface_stats_len) >-1 ) { -/* -* keep total because each call to getsocketopt with PACKET_STATISTICS -* reset the counter of the socket -*/ - internal->rx_queue[i].rx_dropped_pkts += iface_stats.tp_drops; - } - - stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; - stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; +for (i =0 ; i < internal->nb_queues; i++) { +/* reading drop count clears the value */ + internal->rx_queue[i].rx_dropped_pkts += +packet_drop_count (internal->rx_queue[i].sockfd); rx_total += stats->q_ipackets[i]; rx_bytes_total += stats->q_ibytes[i]; rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts; rx_nombuf_total += internal->rx_queue[i].rx_nombuf; - } - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { - stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; - stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; tx_total += stats->q_opackets[i]; tx_err_total += internal->tx_queue[i].err_pkts; tx_bytes_total += stats->q_obytes[i]; + +if (i <RTE_ETHDEV_QUEUE_STAT_CNTRS ) { + stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; + stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; + stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; + } } stats->ipackets = rx_total;
@@ -480,6 +481,7 @@ eth_stats_reset(struct rte_eth_dev *dev)struct pmd_internals *internal = dev->data->dev_private;for (i =0 ; i < internal->nb_queues; i++) { +packet_drop_count (internal->rx_queue[i].sockfd); internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; internal->rx_queue[i].rx_nombuf =0 ;
Quoted 25 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Tuesday, January 21, 2025 4:48 PM
To: Stefan Lässer <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v2]net/af_packet: provide packet drop statsReview
Quoted 4 lines · On Tue, 21 Jan 2025 13:26:16 +0100 Stefan Laesser…
(quoted diff) +4
-0+ /* query dropped packets counter from socket */ + if (internal->rx_queue[i].sockfd != -1 && + getsockopt(internal->rx_queue[i].sockfd, SOL_PACKET, + PACKET_STATISTICS,
&iface_stats,Review
Quoted 3 lines
(quoted diff) +3
-0+ &iface_stats_len) > -1) { + /* + * keep total because each call to getsocketopt with
PACKET_STATISTICSReview
Quoted 3 lines
(quoted diff) +3
-0+ * reset the counter of the socket + */ + internal->rx_queue[i].rx_dropped_pkts +=iface_stats.tp_drops;
Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ }Since reading the value clears it, maybe
stats_resetshould call as well?Also, queues greater that the
RTE_ETHDEV_STAT_CNTRSshould still count against the total. And the two loops could be combined.Maybe something like this?
That looks very reasonable - I will update my patch.
Quoted 95 lines
drivers/net/af_packet/rte_eth_af_packet.c +15
-14@@ -417,50 +417,51 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)return 0 ; } +/* query dropped packets counter from socket */ static unsigned int +packet_drop_count (int sockfd) { +struct tpacket_stats pkt_stats; +socklen_t pkt_stats_len =sizeof (struct tpacket_stats ); + +if (sockfd ==-1 ) +return 0 ; + +if (getsockopt (sockfd,SOL_PACKET ,PACKET_STATISTICS , &pkt_stats, &pkt_stats_len) <-1 ) +return 0 ; + +return pkt_stats.tp_drops; +} +static int eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *stats) { -unsigned i, imax; +unsigned int i;unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ;unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { -struct tpacket_stats iface_stats; -socklen_t iface_stats_len =sizeof (struct tpacket_stats ); - -/* query dropped packets counter from socket */ -if (internal->rx_queue[i].sockfd !=-1 && -getsockopt (internal->rx_queue[i].sockfd,SOL_PACKET , -PACKET_STATISTICS , &iface_stats, - &iface_stats_len) >-1 ) { -/* -* keep total because each call to getsocketopt with
PACKET_STATISTICSdrivers/net/af_packet/rte_eth_af_packet.c +0
-3-* reset the counter of the socket -*/ - internal->rx_queue[i].rx_dropped_pkts +=iface_stats.tp_drops;
drivers/net/af_packet/rte_eth_af_packet.c +12
-10- } - - stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; - stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; +for (i =0 ; i < internal->nb_queues; i++) { +/* reading drop count clears the value */ + internal->rx_queue[i].rx_dropped_pkts += +packet_drop_count (internal->rx_queue[i].sockfd); rx_total += stats->q_ipackets[i]; rx_bytes_total += stats->q_ibytes[i]; rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts; rx_nombuf_total += internal->rx_queue[i].rx_nombuf; - } - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { - stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; - stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; tx_total += stats->q_opackets[i]; tx_err_total += internal->tx_queue[i].err_pkts; tx_bytes_total += stats->q_obytes[i]; + +if (i <RTE_ETHDEV_QUEUE_STAT_CNTRS ) { + stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; + stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; + stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; + } } stats->ipackets = rx_total;@@ -480,6 +481,7 @@ eth_stats_reset(struct rte_eth_dev *dev)struct pmd_internals *internal = dev->data->dev_private;for (i =0 ; i < internal->nb_queues; i++) { +packet_drop_count (internal->rx_queue[i].sockfd); internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; internal->rx_queue[i].rx_nombuf =0 ;
PATCH v3 1/1 net/af_packet: provide packet drop stats
In reply to Stefan LaesserThe Linux kernel provides the ability to query the packet drop counter of a socket. This information can be provided when the user requests stats.
It is important to note that each call to getsockopt with PACKET_STATISTICS resets the internal counters. So the caller needs to keep track of the total count on its own.
Next, I have added a counter for the case when mbuf could not be allocated.
---
v3:
* Introduced function for querying socket dropped counter and use it also to reset internal state when stats are reset.
* Combined the for-loops in in eth_stats_get and eth_stats_reset.
* Count all queues to total counters, but only add up to RTE_ETHDEV_QUEUE_STAT_CNTRS values to queue stats.
v2:
* Limited scope of tpacket_stats and socklen_t variables and moved them inside the for-loop in function eth_stats_get.
* Removed q_errors counting as dropped packets should not be counted as errors.
* Renamed parameter igb_stats of function eth_stats_get to stats since it was a copy/paste issue.
drivers/net/af_packet/rte_eth_af_packet.c | 86 ++++++++++++++++------- 1 file changed, 60 insertions(+), 26 deletions(-)
drivers/net/af_packet/rte_eth_af_packet.c +60 -26
@@ -58,6 +58,8 @@ struct __rte_cache_aligned pkt_rx_queue {volatile unsigned long rx_pkts;volatile unsigned long rx_bytes; +volatile unsigned long rx_nombuf; +volatile unsigned long rx_dropped_pkts; };struct __rte_cache_aligned pkt_tx_queue {
@@ -145,8 +147,10 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)/* allocate the next mbuf */ mbuf =rte_pktmbuf_alloc (pkt_q->mb_pool); -if (unlikely (mbuf ==NULL )) +if (unlikely (mbuf ==NULL )) { + pkt_q->rx_nombuf++;break ; + }/* packet will fit in the mbuf, go ahead and receive it */ rte_pktmbuf_pkt_len (mbuf) =rte_pktmbuf_data_len (mbuf) = ppd->tp_snaplen;
@@ -413,38 +417,65 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)return 0 ; } + +/* +* Query dropped packets counter from socket. +* Reading drop count clears the value of the socket! +*/ +static unsigned int +packet_drop_count (int sockfd) +{ +struct tpacket_stats pkt_stats; +socklen_t pkt_stats_len =sizeof (struct tpacket_stats ); + +if (sockfd ==-1 ) +return 0 ; + +if (getsockopt (sockfd,SOL_PACKET ,PACKET_STATISTICS , &pkt_stats, + &pkt_stats_len) <-1 ) +return 0 ; + +return pkt_stats.tp_drops; +} +static int -eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *igb_stats) +eth_stats_get (struct rte_eth_dev *dev,struct rte_eth_stats *stats) { -unsigned i, imax; -unsigned long rx_total =0 , tx_total =0 , tx_err_total =0 ; +unsigned int i; +unsigned long rx_total =0 , rx_dropped_total =0 , rx_nombuf_total =0 ; +unsigned long tx_total =0 , tx_err_total =0 ;unsigned long rx_bytes_total =0 , tx_bytes_total =0 ;const struct pmd_internals *internal = dev->data->dev_private; - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { - igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; - igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; - rx_total += igb_stats->q_ipackets[i]; - rx_bytes_total += igb_stats->q_ibytes[i]; - } +for (i =0 ; i < internal->nb_queues; i++) { +/* reading drop count clears the value, therefore keep total value */ + internal->rx_queue[i].rx_dropped_pkts += +packet_drop_count (internal->rx_queue[i].sockfd); + + rx_total += internal->rx_queue[i].rx_pkts; + rx_bytes_total += internal->rx_queue[i].rx_bytes; + rx_dropped_total += internal->rx_queue[i].rx_dropped_pkts; + rx_nombuf_total += internal->rx_queue[i].rx_nombuf; - imax = (internal->nb_queues <RTE_ETHDEV_QUEUE_STAT_CNTRS ? - internal->nb_queues :RTE_ETHDEV_QUEUE_STAT_CNTRS ); -for (i =0 ; i < imax; i++) { - igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; - igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; - tx_total += igb_stats->q_opackets[i]; + tx_total += internal->tx_queue[i].tx_pkts; tx_err_total += internal->tx_queue[i].err_pkts; - tx_bytes_total += igb_stats->q_obytes[i]; + tx_bytes_total += internal->tx_queue[i].tx_bytes; + +if (i <RTE_ETHDEV_QUEUE_STAT_CNTRS ) { + stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; + stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; + stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; + stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; + } } - igb_stats->ipackets = rx_total; - igb_stats->ibytes = rx_bytes_total; - igb_stats->opackets = tx_total; - igb_stats->oerrors = tx_err_total; - igb_stats->obytes = tx_bytes_total; + stats->ipackets = rx_total; + stats->ibytes = rx_bytes_total; + stats->imissed = rx_dropped_total; + stats->rx_nombuf = rx_nombuf_total; + stats->opackets = tx_total; + stats->oerrors = tx_err_total; + stats->obytes = tx_bytes_total;return 0 ; }
@@ -455,11 +486,14 @@ eth_stats_reset(struct rte_eth_dev *dev)struct pmd_internals *internal = dev->data->dev_private;for (i =0 ; i < internal->nb_queues; i++) { +/* clear socket counter */ +packet_drop_count (internal->rx_queue[i].sockfd); + internal->rx_queue[i].rx_pkts =0 ; internal->rx_queue[i].rx_bytes =0 ; - } + internal->rx_queue[i].rx_nombuf =0 ; + internal->rx_queue[i].rx_dropped_pkts =0 ; -for (i =0 ; i < internal->nb_queues; i++) { internal->tx_queue[i].tx_pkts =0 ; internal->tx_queue[i].err_pkts =0 ; internal->tx_queue[i].tx_bytes =0 ;
Signature · 1 line
2.34.1
PATCH 1/1 doc: add release not for af_packet
Add short release not about enhancements to af_packet.
--- doc/guides/rel_notes/release_25_03.rst | 5 +++++ 1 file changed, 5 insertions(+)
doc/guides/rel_notes/release_25_03.rst +5 -0
@@ -63,6 +63,11 @@ New Features and even substantial part of its code. It can be viewed as an extension of rte_ring functionality. +* **Enhancements to af_packet net driver.** + + Added ability to option to configure receive packet fanout mode. + Improved statistics including failed receive buffer allocation + and missed packets. Removed Items -------------
Signature · 1 line
2.45.2
Re: [PATCH] doc: add release not for af_packet
22/01/2025 16:56, Stephen Hemminger:
doc/guides/rel_notes/release_25_03.rst New Features · line 70
Quoted 5 lines
doc/guides/rel_notes/release_25_03.rst +5
-0+* **Enhancements to af_packet net driver.** + + Added ability to option to configure receive packet fanout mode. + Improved statistics including failed receive buffer allocation + and missed packets.
Was squashed in relevant commits, thanks.