1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_ethernet_stats, CONFIG_NET_L2_ETHERNET_LOG_LEVEL);
9
10 #include <zephyr/kernel.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <zephyr/net/net_core.h>
14 #include <zephyr/net/ethernet.h>
15
16 #include "net_stats.h"
17
18 #if defined(CONFIG_NET_STATISTICS_USER_API)
19
eth_stats_get(uint32_t mgmt_request,struct net_if * iface,void * data,size_t len)20 static int eth_stats_get(uint32_t mgmt_request, struct net_if *iface,
21 void *data, size_t len)
22 {
23 size_t len_chk = 0;
24 void *src = NULL;
25 const struct ethernet_api *eth;
26
27 switch (NET_MGMT_GET_COMMAND(mgmt_request)) {
28 case NET_REQUEST_STATS_CMD_GET_ETHERNET:
29 if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
30 return -ENOENT;
31 }
32
33 eth = net_if_get_device(iface)->api;
34 if (eth == NULL || eth->get_stats == NULL) {
35 return -ENOENT;
36 }
37
38 len_chk = sizeof(struct net_stats_eth);
39 src = eth->get_stats(net_if_get_device(iface));
40 break;
41 }
42
43 if (len != len_chk || !src) {
44 return -EINVAL;
45 }
46
47 memcpy(data, src, len);
48
49 return 0;
50 }
51
52 NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ETHERNET,
53 eth_stats_get);
54
55 #endif /* CONFIG_NET_STATISTICS_USER_API */
56