drivers: net: loopback: Add counting of number of dropped packets

When the loopback drops driver packets, the number of dropped
packets is counted and can be requested externally.

Signed-off-by: Sjors Hettinga <s.a.hettinga@gmail.com>
This commit is contained in:
Sjors Hettinga 2022-04-04 11:45:46 +02:00 committed by Carles Cufí
commit ae31773ddd
2 changed files with 15 additions and 0 deletions

View file

@ -64,6 +64,7 @@ static void loopback_init(struct net_if *iface)
#ifdef CONFIG_NET_LOOPBACK_SIMULATE_PACKET_DROP
static float loopback_packet_drop_ratio = 0.0f;
static float loopback_packet_drop_state = 0.0f;
static int loopback_packet_dropped_count;
int loopback_set_packet_drop_ratio(float ratio)
{
@ -73,6 +74,12 @@ int loopback_set_packet_drop_ratio(float ratio)
loopback_packet_drop_ratio = ratio;
return 0;
}
int loopback_get_num_dropped_packets(void)
{
return loopback_packet_dropped_count;
}
#endif
static int loopback_send(const struct device *dev, struct net_pkt *pkt)
@ -126,6 +133,7 @@ static int loopback_send(const struct device *dev, struct net_pkt *pkt)
if (loopback_packet_drop_state >= 1.0f) {
/* Administrate we dropped a packet */
loopback_packet_drop_state -= 1.0f;
loopback_packet_dropped_count++;
/* Clean up the packet */
net_pkt_unref(cloned);