Skip to content

Commit

Permalink
net: eth: Convert to use callbacks to query stats
Browse files Browse the repository at this point in the history
The advantage to this approach allows drivers for
devices that already keep statistics data on hardware
registers to use those instead, rather than try to
replicate it the same counters again within the driver
itself.

The eth_native_posix.c driver though do not benefit
from this, is modified to use the new callback system.

Suggested-by: Jukka Rissanen <jukka.rissanen@intel.com>
Signed-off-by: Jonathan Yong <jonathan.yong@intel.com>
  • Loading branch information
jyong2 authored and jukkar committed Jul 19, 2018
1 parent 16155ad commit 40f7436
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
11 changes: 10 additions & 1 deletion drivers/ethernet/eth_native_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,23 @@ static struct device *eth_get_ptp_clock(struct device *dev)
}
#endif

#if defined(CONFIG_NET_STATISTICS_ETHERNET)
static struct net_stats_eth *get_stats(struct net_if *iface)
{
struct eth_context *context = net_if_get_device(iface)->driver_data;

return &(context->stats);
}
#endif

static const struct ethernet_api eth_if_api = {
.iface_api.init = eth_iface_init,
.iface_api.send = eth_send,

.get_capabilities = eth_posix_native_get_capabilities,

#if defined(CONFIG_NET_STATISTICS_ETHERNET)
.stats = &eth_context_data.stats,
.get_stats = get_stats,
#endif
#if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK)
.get_ptp_clock = eth_get_ptp_clock,
Expand Down
2 changes: 1 addition & 1 deletion include/net/ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ struct ethernet_api {
* should be set by driver if statistics needs to be collected
* for that driver.
*/
struct net_stats_eth *stats;
struct net_stats_eth *(*get_stats)(struct net_if *iface);
#endif

/** Get the device capabilities */
Expand Down
72 changes: 56 additions & 16 deletions subsys/net/l2/ethernet/eth_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ static inline void eth_stats_update_bytes_rx(struct net_if *iface,
u32_t bytes)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
if (!api->get_stats) {
return;
}

stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -31,9 +36,14 @@ static inline void eth_stats_update_bytes_tx(struct net_if *iface,
u32_t bytes)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

if (!api->get_stats) {
return;
}

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -44,9 +54,14 @@ static inline void eth_stats_update_bytes_tx(struct net_if *iface,
static inline void eth_stats_update_pkts_rx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
if (!api->get_stats) {
return;
}

stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -57,9 +72,14 @@ static inline void eth_stats_update_pkts_rx(struct net_if *iface)
static inline void eth_stats_update_pkts_tx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

if (!api->get_stats) {
return;
}

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -70,9 +90,14 @@ static inline void eth_stats_update_pkts_tx(struct net_if *iface)
static inline void eth_stats_update_broadcast_rx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
if (!api->get_stats) {
return;
}

stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -83,9 +108,14 @@ static inline void eth_stats_update_broadcast_rx(struct net_if *iface)
static inline void eth_stats_update_broadcast_tx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

if (!api->get_stats) {
return;
}

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -96,9 +126,14 @@ static inline void eth_stats_update_broadcast_tx(struct net_if *iface)
static inline void eth_stats_update_multicast_rx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
if (!api->get_stats) {
return;
}

stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand All @@ -109,9 +144,14 @@ static inline void eth_stats_update_multicast_rx(struct net_if *iface)
static inline void eth_stats_update_multicast_tx(struct net_if *iface)
{
struct net_stats_eth *stats;
const struct ethernet_api *api = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api);

if (!api->get_stats) {
return;
}

stats = ((const struct ethernet_api *)
net_if_get_device(iface)->driver_api)->stats;
stats = api->get_stats(iface);
if (!stats) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion subsys/net/l2/ethernet/ethernet_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ static int eth_stats_get(u32_t mgmt_request, struct net_if *iface,
}

eth = net_if_get_device(iface)->driver_api;

if (eth->get_stats == NULL) {
return -ENOENT;
}

len_chk = sizeof(struct net_stats_eth);
src = eth->stats;
src = eth->get_stats(iface);
break;
}

Expand Down

0 comments on commit 40f7436

Please sign in to comment.