Skip to content

Commit fa7e93a

Browse files
committed
net: l2: ethernet: Add new stats API
Add a new API that takes stat type, the networking stack only needs NATIVE stats per-packet, it doesn't need to update vendor stats per-packet. This saves unncessary exchanges in case driver needs to query the firmware for the vendor stats. Signed-off-by: Chaitanya Tata <[email protected]>
1 parent 3b38d6c commit fa7e93a

File tree

4 files changed

+97
-12
lines changed

4 files changed

+97
-12
lines changed

doc/releases/release-notes-4.4.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ New APIs and options
130130
* :c:struct:`net_eth_mac_config`
131131
* :c:macro:`NET_ETH_MAC_DT_CONFIG_INIT` and :c:macro:`NET_ETH_MAC_DT_INST_CONFIG_INIT`
132132

133+
* Added :c:enum:`ethernet_stats_type` and optional ``get_stats2`` callback in
134+
:c:struct:`ethernet_api` to allow filtering of ethernet statistics by type
135+
(native, vendor, or all). Drivers that support vendor-specific statistics can
136+
implement ``get_stats2`` to skip expensive FW queries when only native stats
137+
are requested. The existing ``get_stats`` API remains unchanged for backward
138+
compatibility.
139+
133140
* Flash
134141

135142
* :dtcompatible:`jedec,mspi-nor` now allows MSPI configuration of read, write and

include/zephyr/net/ethernet.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,16 @@ struct ethernet_config {
529529

530530
/** @endcond */
531531

532+
/** Ethernet statistics type */
533+
enum ethernet_stats_type {
534+
/** Native statistics only (excludes vendor statistics) */
535+
ETHERNET_STATS_TYPE_NATIVE = 0,
536+
/** Vendor statistics only */
537+
ETHERNET_STATS_TYPE_VENDOR,
538+
/** All statistics (native + vendor) */
539+
ETHERNET_STATS_TYPE_ALL,
540+
};
541+
532542
/** Ethernet L2 API operations. */
533543
struct ethernet_api {
534544
/**
@@ -543,6 +553,14 @@ struct ethernet_api {
543553
*/
544554
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
545555
struct net_stats_eth *(*get_stats)(const struct device *dev);
556+
557+
/** Collect optional ethernet specific statistics with type filter.
558+
* This pointer should be set by driver if statistics needs to be
559+
* collected for that driver. If NULL, get_stats will be called
560+
* with ETHERNET_STATS_TYPE_ALL.
561+
*/
562+
struct net_stats_eth *(*get_stats2)(const struct device *dev,
563+
enum ethernet_stats_type type);
546564
#endif
547565

548566
/** Start the device */

subsys/net/l2/ethernet/eth_stats.h

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ static inline void eth_stats_update_bytes_rx(struct net_if *iface,
2525
return;
2626
}
2727

28-
stats = api->get_stats(net_if_get_device(iface));
28+
if (api->get_stats2 != NULL) {
29+
stats = api->get_stats2(net_if_get_device(iface),
30+
ETHERNET_STATS_TYPE_NATIVE);
31+
} else {
32+
stats = api->get_stats(net_if_get_device(iface));
33+
}
2934
if (!stats) {
3035
return;
3136
}
@@ -44,7 +49,12 @@ static inline void eth_stats_update_bytes_tx(struct net_if *iface,
4449
return;
4550
}
4651

47-
stats = api->get_stats(net_if_get_device(iface));
52+
if (api->get_stats2 != NULL) {
53+
stats = api->get_stats2(net_if_get_device(iface),
54+
ETHERNET_STATS_TYPE_NATIVE);
55+
} else {
56+
stats = api->get_stats(net_if_get_device(iface));
57+
}
4858
if (!stats) {
4959
return;
5060
}
@@ -62,7 +72,12 @@ static inline void eth_stats_update_pkts_rx(struct net_if *iface)
6272
return;
6373
}
6474

65-
stats = api->get_stats(net_if_get_device(iface));
75+
if (api->get_stats2 != NULL) {
76+
stats = api->get_stats2(net_if_get_device(iface),
77+
ETHERNET_STATS_TYPE_NATIVE);
78+
} else {
79+
stats = api->get_stats(net_if_get_device(iface));
80+
}
6681
if (!stats) {
6782
return;
6883
}
@@ -80,7 +95,12 @@ static inline void eth_stats_update_pkts_tx(struct net_if *iface)
8095
return;
8196
}
8297

83-
stats = api->get_stats(net_if_get_device(iface));
98+
if (api->get_stats2 != NULL) {
99+
stats = api->get_stats2(net_if_get_device(iface),
100+
ETHERNET_STATS_TYPE_NATIVE);
101+
} else {
102+
stats = api->get_stats(net_if_get_device(iface));
103+
}
84104
if (!stats) {
85105
return;
86106
}
@@ -98,7 +118,12 @@ static inline void eth_stats_update_broadcast_rx(struct net_if *iface)
98118
return;
99119
}
100120

101-
stats = api->get_stats(net_if_get_device(iface));
121+
if (api->get_stats2 != NULL) {
122+
stats = api->get_stats2(net_if_get_device(iface),
123+
ETHERNET_STATS_TYPE_NATIVE);
124+
} else {
125+
stats = api->get_stats(net_if_get_device(iface));
126+
}
102127
if (!stats) {
103128
return;
104129
}
@@ -116,7 +141,12 @@ static inline void eth_stats_update_broadcast_tx(struct net_if *iface)
116141
return;
117142
}
118143

119-
stats = api->get_stats(net_if_get_device(iface));
144+
if (api->get_stats2 != NULL) {
145+
stats = api->get_stats2(net_if_get_device(iface),
146+
ETHERNET_STATS_TYPE_NATIVE);
147+
} else {
148+
stats = api->get_stats(net_if_get_device(iface));
149+
}
120150
if (!stats) {
121151
return;
122152
}
@@ -134,7 +164,12 @@ static inline void eth_stats_update_multicast_rx(struct net_if *iface)
134164
return;
135165
}
136166

137-
stats = api->get_stats(net_if_get_device(iface));
167+
if (api->get_stats2 != NULL) {
168+
stats = api->get_stats2(net_if_get_device(iface),
169+
ETHERNET_STATS_TYPE_NATIVE);
170+
} else {
171+
stats = api->get_stats(net_if_get_device(iface));
172+
}
138173
if (!stats) {
139174
return;
140175
}
@@ -152,7 +187,12 @@ static inline void eth_stats_update_multicast_tx(struct net_if *iface)
152187
return;
153188
}
154189

155-
stats = api->get_stats(net_if_get_device(iface));
190+
if (api->get_stats2 != NULL) {
191+
stats = api->get_stats2(net_if_get_device(iface),
192+
ETHERNET_STATS_TYPE_NATIVE);
193+
} else {
194+
stats = api->get_stats(net_if_get_device(iface));
195+
}
156196
if (!stats) {
157197
return;
158198
}
@@ -177,7 +217,12 @@ static inline void eth_stats_update_errors_rx(struct net_if *iface)
177217
return;
178218
}
179219

180-
stats = api->get_stats(net_if_get_device(iface));
220+
if (api->get_stats2 != NULL) {
221+
stats = api->get_stats2(net_if_get_device(iface),
222+
ETHERNET_STATS_TYPE_NATIVE);
223+
} else {
224+
stats = api->get_stats(net_if_get_device(iface));
225+
}
181226
if (!stats) {
182227
return;
183228
}
@@ -195,7 +240,12 @@ static inline void eth_stats_update_errors_tx(struct net_if *iface)
195240
return;
196241
}
197242

198-
stats = api->get_stats(net_if_get_device(iface));
243+
if (api->get_stats2 != NULL) {
244+
stats = api->get_stats2(net_if_get_device(iface),
245+
ETHERNET_STATS_TYPE_NATIVE);
246+
} else {
247+
stats = api->get_stats(net_if_get_device(iface));
248+
}
199249
if (!stats) {
200250
return;
201251
}
@@ -213,7 +263,12 @@ static inline void eth_stats_update_unknown_protocol(struct net_if *iface)
213263
return;
214264
}
215265

216-
stats = api->get_stats(net_if_get_device(iface));
266+
if (api->get_stats2 != NULL) {
267+
stats = api->get_stats2(net_if_get_device(iface),
268+
ETHERNET_STATS_TYPE_NATIVE);
269+
} else {
270+
stats = api->get_stats(net_if_get_device(iface));
271+
}
217272
if (!stats) {
218273
return;
219274
}

subsys/net/l2/ethernet/ethernet_stats.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ static int eth_stats_get(uint64_t mgmt_request, struct net_if *iface,
3636
}
3737

3838
len_chk = sizeof(struct net_stats_eth);
39-
src = eth->get_stats(net_if_get_device(iface));
39+
if (eth->get_stats2 != NULL) {
40+
src = eth->get_stats2(net_if_get_device(iface),
41+
ETHERNET_STATS_TYPE_ALL);
42+
} else {
43+
src = eth->get_stats(net_if_get_device(iface));
44+
}
4045
break;
4146
}
4247

0 commit comments

Comments
 (0)