/** @file
* @brief Network shell module
*
* Provide some networking shell commands that can be useful to applications.
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <logging/log.h>
LOG_MODULE_REGISTER(net_shell, LOG_LEVEL_DBG);
#include <zephyr.h>
#include <kernel_internal.h>
#include <random/rand32.h>
#include <stdlib.h>
#include <stdio.h>
#include <shell/shell.h>
#include <shell/shell_uart.h>
#include <net/net_if.h>
#include <net/dns_resolve.h>
#include <net/ppp.h>
#include <net/net_stats.h>
#include <sys/printk.h>
#include "route.h"
#include "icmpv6.h"
#include "icmpv4.h"
#include "connection.h"
#if defined(CONFIG_NET_TCP)
#include "tcp_internal.h"
#include <sys/slist.h>
#endif
#include "ipv6.h"
#if defined(CONFIG_NET_ARP)
#include "ethernet/arp.h"
#endif
#if defined(CONFIG_NET_L2_ETHERNET)
#include <net/ethernet.h>
#endif
#if defined(CONFIG_NET_L2_ETHERNET_MGMT)
#include <net/ethernet_mgmt.h>
#endif
#if defined(CONFIG_NET_L2_VIRTUAL)
#include <net/virtual.h>
#endif
#if defined(CONFIG_NET_L2_VIRTUAL_MGMT)
#include <net/virtual_mgmt.h>
#endif
#include <net/capture.h>
#if defined(CONFIG_NET_GPTP)
#include <net/gptp.h>
#include "ethernet/gptp/gptp_messages.h"
#include "ethernet/gptp/gptp_md.h"
#include "ethernet/gptp/gptp_state.h"
#include "ethernet/gptp/gptp_data_set.h"
#include "ethernet/gptp/gptp_private.h"
#endif
#if defined(CONFIG_NET_L2_PPP)
#include <net/ppp.h>
#include "ppp/ppp_internal.h"
#endif
#include "net_shell.h"
#include "net_stats.h"
#include <sys/fdtable.h>
#include "websocket/websocket_internal.h"
#define PR(fmt, ...) \
shell_fprintf(shell, SHELL_NORMAL, fmt, ##__VA_ARGS__)
#define PR_SHELL(shell, fmt, ...) \
shell_fprintf(shell, SHELL_NORMAL, fmt, ##__VA_ARGS__)
#define PR_ERROR(fmt, ...) \
shell_fprintf(shell, SHELL_ERROR, fmt, ##__VA_ARGS__)
#define PR_INFO(fmt, ...) \
shell_fprintf(shell, SHELL_INFO, fmt, ##__VA_ARGS__)
#define PR_WARNING(fmt, ...) \
shell_fprintf(shell, SHELL_WARNING, fmt, ##__VA_ARGS__)
#include "net_private.h"
struct net_shell_user_data {
const struct shell *shell;
void *user_data;
};
static inline const char *addrtype2str(enum net_addr_type addr_type)
{
switch (addr_type) {
case NET_ADDR_ANY:
return "<unknown type>";
case NET_ADDR_AUTOCONF:
return "autoconf";
case NET_ADDR_DHCP:
return "DHCP";
case NET_ADDR_MANUAL:
return "manual";
case NET_ADDR_OVERRIDABLE:
return "overridable";
}
return "<invalid type>";
}
static inline const char *addrstate2str(enum net_addr_state addr_state)
{
switch (addr_state) {
case NET_ADDR_ANY_STATE:
return "<unknown state>";
case NET_ADDR_TENTATIVE:
return "tentative";
case NET_ADDR_PREFERRED:
return "preferred";
case NET_ADDR_DEPRECATED:
return "deprecated";
}
return "<invalid state>";
}
static const char *iface2str(struct net_if *iface, const char **extra)
{
#ifdef CONFIG_NET_L2_IEEE802154
if (net_if_l2(iface) == &NET_L2_GET_NAME(IEEE802154)) {
if (extra) {
*extra = "=============";
}
return "IEEE 802.15.4";
}
#endif
#ifdef CONFIG_NET_L2_ETHERNET
if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
if (extra) {
*extra = "========";
}
return "Ethernet";
}
#endif
#ifdef CONFIG_NET_L2_VIRTUAL
if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
if (extra) {
*extra = "=======";
}
return "Virtual";
}
#endif
#ifdef CONFIG_NET_L2_PPP
if (net_if_l2(iface) == &NET_L2_GET_NAME(PPP)) {
if (extra) {
*extra = "===";
}
return "PPP";
}
#endif
#ifdef CONFIG_NET_L2_DUMMY
if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
if (extra) {
*extra = "=====";
}
return "Dummy";
}
#endif
#ifdef CONFIG_NET_L2_OPENTHREAD
if (net_if_l2(iface) == &NET_L2_GET_NAME(OPENTHREAD)) {
if (extra) {
*extra = "==========";
}
return "OpenThread";
}
#endif
#ifdef CONFIG_NET_L2_BT
if (net_if_l2(iface) == &NET_L2_GET_NAME(BLUETOOTH)) {
if (extra) {
*extra = "=========";
}
return "Bluetooth";
}
#endif
#ifdef CONFIG_NET_OFFLOAD
if (net_if_is_ip_offloaded(iface)) {
if (extra) {
*extra = "==========";
}
return "IP Offload";
}
#endif
#ifdef CONFIG_NET_L2_CANBUS
if (net_if_l2(iface) == &NET_L2_GET_NAME(CANBUS)) {
if (extra) {
*extra = "======";
}
return "CANBUS";
}
#endif
#ifdef CONFIG_NET_L2_CANBUS_RAW
if (net_if_l2(iface) == &NET_L2_GET_NAME(CANBUS_RAW)) {
if (extra) {
*extra = "==========";
}
return "CANBUS_RAW";
}
#endif
if (extra) {
*extra = "==============";
}
return "<unknown type>";
}
#if defined(CONFIG_NET_L2_ETHERNET) && defined(CONFIG_NET_NATIVE)
struct ethernet_capabilities {
enum ethernet_hw_caps capability;
const char * const description;
};
#define EC(cap, desc) { .capability = cap, .description = desc }
static struct ethernet_capabilities eth_hw_caps[] = {
EC(ETHERNET_HW_TX_CHKSUM_OFFLOAD, "TX checksum offload"),
EC(ETHERNET_HW_RX_CHKSUM_OFFLOAD, "RX checksum offload"),
EC(ETHERNET_HW_VLAN, "Virtual LAN"),
EC(ETHERNET_HW_VLAN_TAG_STRIP, "VLAN Tag stripping"),
EC(ETHERNET_AUTO_NEGOTIATION_SET, "Auto negotiation"),
EC(ETHERNET_LINK_10BASE_T, "10 Mbits"),
EC(ETHERNET_LINK_100BASE_T, "100 Mbits"),
EC(ETHERNET_LINK_1000BASE_T, "1 Gbits"),
EC(ETHERNET_DUPLEX_SET, "Half/full duplex"),
EC(ETHERNET_PTP, "IEEE 802.1AS gPTP clock"),
EC(ETHERNET_QAV, "IEEE 802.1Qav (credit shaping)"),
EC(ETHERNET_QBV, "IEEE 802.1Qbv (scheduled traffic)"),
EC(ETHERNET_QBU, "IEEE 802.1Qbu (frame preemption)"),
EC(ETHERNET_TXTIME, "TXTIME"),
EC(ETHERNET_PROMISC_MODE, "Promiscuous mode"),
EC(ETHERNET_PRIORITY_QUEUES, "Priority queues"),
EC(ETHERNET_HW_FILTERING, "MAC address filtering"),
EC(ETHERNET_DSA_SLAVE_PORT, "DSA slave port"),
EC(ETHERNET_DSA_MASTER_PORT, "DSA master port"),
};
static void print_supported_ethernet_capabilities(
const struct shell *shell, struct net_if *iface)
{
enum ethernet_hw_caps caps = net_eth_get_hw_capabilities(iface);
int i;
for (i = 0; i < ARRAY_SIZE(eth_hw_caps); i++) {
if (caps & eth_hw_caps[i].capability) {
PR("\t%s\n", eth_hw_caps[i].description);
}
}
}
#endif /* CONFIG_NET_L2_ETHERNET */
#if defined(CONFIG_NET_NATIVE)
static const char *iface_flags2str(struct net_if *iface)
{
static char str[sizeof("POINTOPOINT") + sizeof("PROMISC") +
sizeof("NO_AUTO_START") + sizeof("SUSPENDED") +
sizeof("MCAST_FORWARD") + sizeof("IPv4") +
sizeof("IPv6")];
int pos = 0;
if (net_if_flag_is_set(iface, NET_IF_POINTOPOINT)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"POINTOPOINT,");
}
if (net_if_flag_is_set(iface, NET_IF_PROMISC)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"PROMISC,");
}
if (net_if_flag_is_set(iface, NET_IF_NO_AUTO_START)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"NO_AUTO_START,");
} else {
pos += snprintk(str + pos, sizeof(str) - pos,
"AUTO_START,");
}
if (net_if_flag_is_set(iface, NET_IF_FORWARD_MULTICASTS)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"MCAST_FORWARD,");
}
if (net_if_flag_is_set(iface, NET_IF_IPV4)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"IPv4,");
}
if (net_if_flag_is_set(iface, NET_IF_IPV6)) {
pos += snprintk(str + pos, sizeof(str) - pos,
"IPv6,");
}
/* get rid of last ',' character */
str[pos - 1] = '\0';
return str;
}
#endif
static void iface_cb(struct net_if *iface, void *user_data)
{
#if defined(CONFIG_NET_NATIVE)
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
#if defined(CONFIG_NET_IPV6)
struct net_if_ipv6_prefix *prefix;
struct net_if_router *router;
struct net_if_ipv6 *ipv6;
#endif
#if defined(CONFIG_NET_IPV4)
struct net_if_ipv4 *ipv4;
#endif
#if defined(CONFIG_NET_VLAN)
struct ethernet_context *eth_ctx;
#endif
#if defined(CONFIG_NET_IPV4) || defined(CONFIG_NET_IPV6)
struct net_if_addr *unicast;
struct net_if_mcast_addr *mcast;
#endif
#if defined(CONFIG_NET_L2_ETHERNET_MGMT)
struct ethernet_req_params params;
int ret;
#endif
const char *extra;
#if defined(CONFIG_NET_IPV4) || defined(CONFIG_NET_IPV6)
int i, count;
#endif
if (data->user_data && data->user_data != iface) {
return;
}
PR("\nInterface %p (%s) [%d]\n", iface, iface2str(iface, &extra),
net_if_get_by_iface(iface));
PR("===========================%s\n", extra);
if (!net_if_is_up(iface)) {
PR_INFO("Interface is down.\n");
/* Show detailed information only when user asks information
* about one specific network interface.
*/
if (data->user_data == NULL) {
return;
}
}
#ifdef CONFIG_NET_POWER_MANAGEMENT
if (net_if_is_suspended(iface)) {
PR_INFO("Interface is suspended, thus not able to tx/rx.\n");
}
#endif
#if defined(CONFIG_NET_L2_VIRTUAL)
if (!sys_slist_is_empty(&iface->config.virtual_interfaces)) {
struct virtual_interface_context *ctx, *tmp;
PR("Virtual interfaces attached to this : ");
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(
&iface->config.virtual_interfaces,
ctx, tmp, node) {
if (ctx->virtual_iface == iface) {
continue;
}
PR("%d ", net_if_get_by_iface(ctx->virtual_iface));
}
PR("\n");
}
if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
struct net_if *orig_iface;
char *name, buf[CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN];
name = net_virtual_get_name(iface, buf, sizeof(buf));
if (!(name && name[0])) {
name = "<unknown>";
}
PR("Name : %s\n", name);
orig_iface = net_virtual_get_iface(iface);
if (orig_iface == NULL) {
PR("No attached network interface.\n");
} else {
PR("Attached : %d (%s / %p)\n",
net_if_get_by_iface(orig_iface),
iface2str(orig_iface, NULL),
orig_iface);
}
}
#endif /* CONFIG_NET_L2_VIRTUAL */
if (net_if_get_link_addr(iface) &&
net_if_get_link_addr(iface)->addr) {
PR("Link addr : %s\n",
net_sprint_ll_addr(net_if_get_link_addr(iface)->addr,
net_if_get_link_addr(iface)->len));
}
PR("MTU : %d\n", net_if_get_mtu(iface));
PR("Flags : %s\n", iface_flags2str(iface));
#if defined(CONFIG_NET_L2_ETHERNET_MGMT)
if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
count = 0;
ret = net_mgmt(NET_REQUEST_ETHERNET_GET_PRIORITY_QUEUES_NUM,
iface, ¶ms,
sizeof(struct ethernet_req_params));
if (!ret && params.priority_queues_num) {
count = params.priority_queues_num;
PR("Priority queues:\n");
for (i = 0; i < count; ++i) {
params.qav_param.queue_id = i;
params.qav_param.type =
ETHERNET_QAV_PARAM_TYPE_STATUS;
ret = net_mgmt(
NET_REQUEST_ETHERNET_GET_QAV_PARAM,
iface, ¶ms,
sizeof(struct ethernet_req_params));
PR("\t%d: Qav ", i);
if (ret) {
PR("not supported\n");
} else {
PR("%s\n",
params.qav_param.enabled ?
"enabled" :
"disabled");
}
}
}
}
#endif
#if defined(CONFIG_NET_PROMISCUOUS_MODE)
PR("Promiscuous mode : %s\n",
net_if_is_promisc(iface) ? "enabled" : "disabled");
#endif
#if defined(CONFIG_NET_VLAN)
if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
eth_ctx = net_if_l2_data(iface);
if (eth_ctx->vlan_enabled) {
for (i = 0; i < CONFIG_NET_VLAN_COUNT; i++) {
if (eth_ctx->vlan[i].iface != iface ||
eth_ctx->vlan[i].tag ==
NET_VLAN_TAG_UNSPEC) {
continue;
}
PR("VLAN tag : %d (0x%x)\n",
eth_ctx->vlan[i].tag,
eth_ctx->vlan[i].tag);
}
} else {
PR("VLAN not enabled\n");
}
}
#endif
#ifdef CONFIG_NET_L2_ETHERNET
if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
PR("Ethernet capabilities supported:\n");
print_supported_ethernet_capabilities(shell, iface);
}
#endif /* CONFIG_NET_L2_ETHERNET */
#if defined(CONFIG_NET_IPV6)
count = 0;
if (!net_if_flag_is_set(iface, NET_IF_IPV6)) {
PR("%s not %s for this interface.\n", "IPv6", "enabled");
ipv6 = NULL;
goto skip_ipv6;
}
ipv6 = iface->config.ip.ipv6;
PR("IPv6 unicast addresses (max %d):\n", NET_IF_MAX_IPV6_ADDR);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_ADDR; i++) {
unicast = &ipv6->unicast[i];
if (!unicast->is_used) {
continue;
}
PR("\t%s %s %s%s%s\n",
net_sprint_ipv6_addr(&unicast->address.in6_addr),
addrtype2str(unicast->addr_type),
addrstate2str(unicast->addr_state),
unicast->is_infinite ? " infinite" : "",
unicast->is_mesh_local ? " meshlocal" : "");
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
count = 0;
PR("IPv6 multicast addresses (max %d):\n", NET_IF_MAX_IPV6_MADDR);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_MADDR; i++) {
mcast = &ipv6->mcast[i];
if (!mcast->is_used) {
continue;
}
PR("\t%s\n", net_sprint_ipv6_addr(&mcast->address.in6_addr));
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
count = 0;
PR("IPv6 prefixes (max %d):\n", NET_IF_MAX_IPV6_PREFIX);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_PREFIX; i++) {
prefix = &ipv6->prefix[i];
if (!prefix->is_used) {
continue;
}
PR("\t%s/%d%s\n",
net_sprint_ipv6_addr(&prefix->prefix),
prefix->len, prefix->is_infinite ? " infinite" : "");
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
router = net_if_ipv6_router_find_default(iface, NULL);
if (router) {
PR("IPv6 default router :\n");
PR("\t%s%s\n",
net_sprint_ipv6_addr(&router->address.in6_addr),
router->is_infinite ? " infinite" : "");
}
skip_ipv6:
if (ipv6) {
PR("IPv6 hop limit : %d\n",
ipv6->hop_limit);
PR("IPv6 base reachable time : %d\n",
ipv6->base_reachable_time);
PR("IPv6 reachable time : %d\n",
ipv6->reachable_time);
PR("IPv6 retransmit timer : %d\n",
ipv6->retrans_timer);
}
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4)
/* No need to print IPv4 information for interface that does not
* support that protocol.
*/
if (
#if defined(CONFIG_NET_L2_IEEE802154)
(net_if_l2(iface) == &NET_L2_GET_NAME(IEEE802154)) ||
#endif
#if defined(CONFIG_NET_L2_BT)
(net_if_l2(iface) == &NET_L2_GET_NAME(BLUETOOTH)) ||
#endif
0) {
PR_WARNING("%s not %s for this interface.\n", "IPv4",
"supported");
return;
}
count = 0;
if (!net_if_flag_is_set(iface, NET_IF_IPV4)) {
PR("%s not %s for this interface.\n", "IPv4", "enabled");
ipv4 = NULL;
goto skip_ipv4;
}
ipv4 = iface->config.ip.ipv4;
PR("IPv4 unicast addresses (max %d):\n", NET_IF_MAX_IPV4_ADDR);
for (i = 0; ipv4 && i < NET_IF_MAX_IPV4_ADDR; i++) {
unicast = &ipv4->unicast[i];
if (!unicast->is_used) {
continue;
}
PR("\t%s %s %s%s\n",
net_sprint_ipv4_addr(&unicast->address.in_addr),
addrtype2str(unicast->addr_type),
addrstate2str(unicast->addr_state),
unicast->is_infinite ? " infinite" : "");
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
count = 0;
PR("IPv4 multicast addresses (max %d):\n", NET_IF_MAX_IPV4_MADDR);
for (i = 0; ipv4 && i < NET_IF_MAX_IPV4_MADDR; i++) {
mcast = &ipv4->mcast[i];
if (!mcast->is_used) {
continue;
}
PR("\t%s\n", net_sprint_ipv4_addr(&mcast->address.in_addr));
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
skip_ipv4:
if (ipv4) {
PR("IPv4 gateway : %s\n",
net_sprint_ipv4_addr(&ipv4->gw));
PR("IPv4 netmask : %s\n",
net_sprint_ipv4_addr(&ipv4->netmask));
}
#endif /* CONFIG_NET_IPV4 */
#if defined(CONFIG_NET_DHCPV4)
PR("DHCPv4 lease time : %u\n",
iface->config.dhcpv4.lease_time);
PR("DHCPv4 renew time : %u\n",
iface->config.dhcpv4.renewal_time);
PR("DHCPv4 server : %s\n",
net_sprint_ipv4_addr(&iface->config.dhcpv4.server_id));
PR("DHCPv4 requested : %s\n",
net_sprint_ipv4_addr(&iface->config.dhcpv4.requested_ip));
PR("DHCPv4 state : %s\n",
net_dhcpv4_state_name(iface->config.dhcpv4.state));
PR("DHCPv4 attempts : %d\n",
iface->config.dhcpv4.attempts);
#endif /* CONFIG_NET_DHCPV4 */
#else
ARG_UNUSED(iface);
ARG_UNUSED(user_data);
#endif /* CONFIG_NET_NATIVE */
}
#if defined(CONFIG_NET_ROUTE) && defined(CONFIG_NET_NATIVE)
static void route_cb(struct net_route_entry *entry, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
struct net_if *iface = data->user_data;
struct net_route_nexthop *nexthop_route;
int count;
if (entry->iface != iface) {
return;
}
PR("IPv6 prefix : %s/%d\n", net_sprint_ipv6_addr(&entry->addr),
entry->prefix_len);
count = 0;
SYS_SLIST_FOR_EACH_CONTAINER(&entry->nexthop, nexthop_route, node) {
struct net_linkaddr_storage *lladdr;
if (!nexthop_route->nbr) {
continue;
}
PR("\tneighbor : %p\t", nexthop_route->nbr);
if (nexthop_route->nbr->idx == NET_NBR_LLADDR_UNKNOWN) {
PR("addr : <unknown>\n");
} else {
lladdr = net_nbr_get_lladdr(nexthop_route->nbr->idx);
PR("addr : %s\n", net_sprint_ll_addr(lladdr->addr,
lladdr->len));
}
count++;
}
if (count == 0) {
PR("\t<none>\n");
}
}
static void iface_per_route_cb(struct net_if *iface, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
const char *extra;
PR("\nIPv6 routes for interface %d (%p) (%s)\n",
net_if_get_by_iface(iface), iface,
iface2str(iface, &extra));
PR("=========================================%s\n", extra);
data->user_data = iface;
net_route_foreach(route_cb, data);
}
#endif /* CONFIG_NET_ROUTE */
#if defined(CONFIG_NET_ROUTE_MCAST) && defined(CONFIG_NET_NATIVE)
static void route_mcast_cb(struct net_route_entry_mcast *entry,
void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
struct net_if *iface = data->user_data;
const char *extra;
if (entry->iface != iface) {
return;
}
PR("IPv6 multicast route %p for interface %d (%p) (%s)\n", entry,
net_if_get_by_iface(iface), iface, iface2str(iface, &extra));
PR("==========================================================="
"%s\n", extra);
PR("IPv6 group : %s\n", net_sprint_ipv6_addr(&entry->group));
PR("IPv6 group len : %d\n", entry->prefix_len);
PR("Lifetime : %u\n", entry->lifetime);
}
static void iface_per_mcast_route_cb(struct net_if *iface, void *user_data)
{
struct net_shell_user_data *data = user_data;
data->user_data = iface;
net_route_mcast_foreach(route_mcast_cb, NULL, data);
}
#endif /* CONFIG_NET_ROUTE_MCAST */
#if defined(CONFIG_NET_STATISTICS)
#if NET_TC_COUNT > 1
static const char *priority2str(enum net_priority priority)
{
switch (priority) {
case NET_PRIORITY_BK:
return "BK"; /* Background */
case NET_PRIORITY_BE:
return "BE"; /* Best effort */
case NET_PRIORITY_EE:
return "EE"; /* Excellent effort */
case NET_PRIORITY_CA:
return "CA"; /* Critical applications */
case NET_PRIORITY_VI:
return "VI"; /* Video, < 100 ms latency and jitter */
case NET_PRIORITY_VO:
return "VO"; /* Voice, < 10 ms latency and jitter */
case NET_PRIORITY_IC:
return "IC"; /* Internetwork control */
case NET_PRIORITY_NC:
return "NC"; /* Network control */
}
return "??";
}
#endif
#if defined(CONFIG_NET_STATISTICS_ETHERNET) && \
defined(CONFIG_NET_STATISTICS_USER_API)
static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
const struct shell *shell)
{
PR("Statistics for Ethernet interface %p [%d]\n", iface,
net_if_get_by_iface(iface));
PR("Bytes received : %u\n", data->bytes.received);
PR("Bytes sent : %u\n", data->bytes.sent);
PR("Packets received : %u\n", data->pkts.rx);
PR("Packets sent : %u\n", data->pkts.tx);
PR("Bcast received : %u\n", data->broadcast.rx);
PR("Bcast sent : %u\n", data->broadcast.tx);
PR("Mcast received : %u\n", data->multicast.rx);
PR("Mcast sent : %u\n", data->multicast.tx);
#if defined(CONFIG_NET_STATISTICS_ETHERNET_VENDOR)
if (data->vendor) {
PR("Vendor specific statistics for Ethernet "
"interface %p [%d]:\n",
iface, net_if_get_by_iface(iface));
size_t i = 0;
do {
PR("%s : %u\n", data->vendor[i].key,
data->vendor[i].value);
i++;
} while (data->vendor[i].key);
}
#endif /* CONFIG_NET_STATISTICS_ETHERNET_VENDOR */
}
#endif /* CONFIG_NET_STATISTICS_ETHERNET && CONFIG_NET_STATISTICS_USER_API */
#if defined(CONFIG_NET_STATISTICS_PPP) && \
defined(CONFIG_NET_STATISTICS_USER_API)
static void print_ppp_stats(struct net_if *iface, struct net_stats_ppp *data,
const struct shell *shell)
{
PR("Frames recv %u\n", data->pkts.rx);
PR("Frames sent %u\n", data->pkts.tx);
PR("Frames dropped %u\n", data->drop);
PR("Bad FCS %u\n", data->chkerr);
}
#endif /* CONFIG_NET_STATISTICS_PPP && CONFIG_NET_STATISTICS_USER_API */
#if !defined(CONFIG_NET_NATIVE)
#define GET_STAT(a, b) 0
#endif
#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
#if (NET_TC_TX_COUNT > 1) || (NET_TC_RX_COUNT > 1)
static char *get_net_pkt_tc_stats_detail(struct net_if *iface, int i,
bool is_tx)
{
static char extra_stats[sizeof("\t[0=xxxx us]") +
sizeof("->xxxx") *
NET_PKT_DETAIL_STATS_COUNT];
int j, total = 0, pos = 0;
pos += snprintk(extra_stats, sizeof(extra_stats), "\t[0");
for (j = 0; j < NET_PKT_DETAIL_STATS_COUNT; j++) {
net_stats_t count = 0;
uint32_t avg;
if (is_tx) {
#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) && (NET_TC_TX_COUNT > 1)
count = GET_STAT(iface,
tc.sent[i].tx_time_detail[j].count);
#endif
} else {
#if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL) && (NET_TC_RX_COUNT > 1)
count = GET_STAT(iface,
tc.recv[i].rx_time_detail[j].count);
#endif
}
if (count == 0) {
break;
}
if (is_tx) {
#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) && (NET_TC_TX_COUNT > 1)
avg = (uint32_t)(GET_STAT(iface,
tc.sent[i].tx_time_detail[j].sum) /
(uint64_t)count);
#endif
} else {
#if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL) && (NET_TC_RX_COUNT > 1)
avg = (uint32_t)(GET_STAT(iface,
tc.recv[i].rx_time_detail[j].sum) /
(uint64_t)count);
#endif
}
if (avg == 0) {
continue;
}
total += avg;
pos += snprintk(extra_stats + pos, sizeof(extra_stats) - pos,
"->%u", avg);
}
if (total == 0U) {
return "\0";
}
pos += snprintk(extra_stats + pos, sizeof(extra_stats) - pos,
"=%u us]", total);
return extra_stats;
}
#endif /* (NET_TC_TX_COUNT > 1) || (NET_TC_RX_COUNT > 1) */
#if (NET_TC_TX_COUNT <= 1) || (NET_TC_RX_COUNT <= 1)
static char *get_net_pkt_stats_detail(struct net_if *iface, bool is_tx)
{
static char extra_stats[sizeof("\t[0=xxxx us]") + sizeof("->xxxx") *
NET_PKT_DETAIL_STATS_COUNT];
int j, total = 0, pos = 0;
pos += snprintk(extra_stats, sizeof(extra_stats), "\t[0");
for (j = 0; j < NET_PKT_DETAIL_STATS_COUNT; j++) {
net_stats_t count;
uint32_t avg;
if (is_tx) {
#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
count = GET_STAT(iface, tx_time_detail[j].count);
#endif
} else {
#if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
count = GET_STAT(iface, rx_time_detail[j].count);
#endif
}
if (count == 0) {
break;
}
if (is_tx) {
#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
avg = (uint32_t)(GET_STAT(iface,
tx_time_detail[j].sum) /
(uint64_t)count);
#endif
} else {
#if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
avg = (uint32_t)(GET_STAT(iface,
rx_time_detail[j].sum) /
(uint64_t)count);
#endif
}
if (avg == 0) {
continue;
}
total += avg;
pos += snprintk(extra_stats + pos,
sizeof(extra_stats) - pos,
"->%u", avg);
}
if (total == 0U) {
return "\0";
}
pos += snprintk(extra_stats + pos, sizeof(extra_stats) - pos,
"=%u us]", total);
return extra_stats;
}
#endif /* (NET_TC_TX_COUNT == 1) || (NET_TC_RX_COUNT == 1) */
#else /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
#if defined(CONFIG_NET_PKT_TXTIME_STATS) || \
defined(CONFIG_NET_PKT_RXTIME_STATS)
#if (NET_TC_TX_COUNT > 1) || (NET_TC_RX_COUNT > 1)
static char *get_net_pkt_tc_stats_detail(struct net_if *iface, int i,
bool is_tx)
{
ARG_UNUSED(iface);
ARG_UNUSED(i);
ARG_UNUSED(is_tx);
return "\0";
}
#endif
#if (NET_TC_TX_COUNT == 1) || (NET_TC_RX_COUNT == 1)
static char *get_net_pkt_stats_detail(struct net_if *iface, bool is_tx)
{
ARG_UNUSED(iface);
ARG_UNUSED(is_tx);
return "\0";
}
#endif
#endif /* CONFIG_NET_PKT_TXTIME_STATS) || CONFIG_NET_PKT_RXTIME_STATS */
#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
static void print_tc_tx_stats(const struct shell *shell, struct net_if *iface)
{
#if NET_TC_TX_COUNT > 1
int i;
PR("TX traffic class statistics:\n");
#if defined(CONFIG_NET_PKT_TXTIME_STATS)
PR("TC Priority\tSent pkts\tbytes\ttime\n");
for (i = 0; i < NET_TC_TX_COUNT; i++) {
net_stats_t count = GET_STAT(iface,
tc.sent[i].tx_time.count);
if (count == 0) {
PR("[%d] %s (%d)\t%d\t\t%d\t-\n", i,
priority2str(GET_STAT(iface, tc.sent[i].priority)),
GET_STAT(iface, tc.sent[i].priority),
GET_STAT(iface, tc.sent[i].pkts),
GET_STAT(iface, tc.sent[i].bytes));
} else {
PR("[%d] %s (%d)\t%d\t\t%d\t%lu us%s\n", i,
priority2str(GET_STAT(iface, tc.sent[i].priority)),
GET_STAT(iface, tc.sent[i].priority),
GET_STAT(iface, tc.sent[i].pkts),
GET_STAT(iface, tc.sent[i].bytes),
(uint32_t)(GET_STAT(iface,
tc.sent[i].tx_time.sum) /
(uint64_t)count),
get_net_pkt_tc_stats_detail(iface, i, true));
}
}
#else
PR("TC Priority\tSent pkts\tbytes\n");
for (i = 0; i < NET_TC_TX_COUNT; i++) {
PR("[%d] %s (%d)\t%d\t\t%d\n", i,
priority2str(GET_STAT(iface, tc.sent[i].priority)),
GET_STAT(iface, tc.sent[i].priority),
GET_STAT(iface, tc.sent[i].pkts),
GET_STAT(iface, tc.sent[i].bytes));
}
#endif /* CONFIG_NET_PKT_TXTIME_STATS */
#else
ARG_UNUSED(shell);
#if defined(CONFIG_NET_PKT_TXTIME_STATS)
net_stats_t count = GET_STAT(iface, tx_time.count);
if (count != 0) {
PR("Avg %s net_pkt (%u) time %lu us%s\n", "TX", count,
(uint32_t)(GET_STAT(iface, tx_time.sum) / (uint64_t)count),
get_net_pkt_stats_detail(iface, true));
}
#else
ARG_UNUSED(iface);
#endif /* CONFIG_NET_PKT_TXTIME_STATS */
#endif /* NET_TC_TX_COUNT > 1 */
}
static void print_tc_rx_stats(const struct shell *shell, struct net_if *iface)
{
#if NET_TC_RX_COUNT > 1
int i;
PR("RX traffic class statistics:\n");
#if defined(CONFIG_NET_PKT_RXTIME_STATS)
PR("TC Priority\tRecv pkts\tbytes\ttime\n");
for (i = 0; i < NET_TC_RX_COUNT; i++) {
net_stats_t count = GET_STAT(iface,
tc.recv[i].rx_time.count);
if (count == 0) {
PR("[%d] %s (%d)\t%d\t\t%d\t-\n", i,
priority2str(GET_STAT(iface, tc.recv[i].priority)),
GET_STAT(iface, tc.recv[i].priority),
GET_STAT(iface, tc.recv[i].pkts),
GET_STAT(iface, tc.recv[i].bytes));
} else {
PR("[%d] %s (%d)\t%d\t\t%d\t%lu us%s\n", i,
priority2str(GET_STAT(iface, tc.recv[i].priority)),
GET_STAT(iface, tc.recv[i].priority),
GET_STAT(iface, tc.recv[i].pkts),
GET_STAT(iface, tc.recv[i].bytes),
(uint32_t)(GET_STAT(iface,
tc.recv[i].rx_time.sum) /
(uint64_t)count),
get_net_pkt_tc_stats_detail(iface, i, false));
}
}
#else
PR("TC Priority\tRecv pkts\tbytes\n");
for (i = 0; i < NET_TC_RX_COUNT; i++) {
PR("[%d] %s (%d)\t%d\t\t%d\n", i,
priority2str(GET_STAT(iface, tc.recv[i].priority)),
GET_STAT(iface, tc.recv[i].priority),
GET_STAT(iface, tc.recv[i].pkts),
GET_STAT(iface, tc.recv[i].bytes));
}
#endif /* CONFIG_NET_PKT_RXTIME_STATS */
#else
ARG_UNUSED(shell);
#if defined(CONFIG_NET_PKT_RXTIME_STATS)
net_stats_t count = GET_STAT(iface, rx_time.count);
if (count != 0) {
PR("Avg %s net_pkt (%u) time %lu us%s\n", "RX", count,
(uint32_t)(GET_STAT(iface, rx_time.sum) / (uint64_t)count),
get_net_pkt_stats_detail(iface, false));
}
#else
ARG_UNUSED(iface);
#endif /* CONFIG_NET_PKT_RXTIME_STATS */
#endif /* NET_TC_RX_COUNT > 1 */
}
static void print_net_pm_stats(const struct shell *shell, struct net_if *iface)
{
#if defined(CONFIG_NET_STATISTICS_POWER_MANAGEMENT)
PR("PM suspend stats:\n");
PR("\tLast time : %u ms\n",
GET_STAT(iface, pm.last_suspend_time));
PR("\tAverage time : %u ms\n",
(uint32_t)(GET_STAT(iface, pm.overall_suspend_time) /
GET_STAT(iface, pm.suspend_count)));
PR("\tTotal time : %llu ms\n",
GET_STAT(iface, pm.overall_suspend_time));
PR("\tHow many times: %u\n",
GET_STAT(iface, pm.suspend_count));
#else
ARG_UNUSED(shell);
ARG_UNUSED(iface);
#endif
}
static void net_shell_print_statistics(struct net_if *iface, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
if (iface) {
const char *extra;
PR("\nInterface %p (%s) [%d]\n", iface,
iface2str(iface, &extra), net_if_get_by_iface(iface));
PR("===========================%s\n", extra);
} else {
PR("\nGlobal statistics\n");
PR("=================\n");
}
#if defined(CONFIG_NET_STATISTICS_IPV6) && defined(CONFIG_NET_NATIVE_IPV6)
PR("IPv6 recv %d\tsent\t%d\tdrop\t%d\tforwarded\t%d\n",
GET_STAT(iface, ipv6.recv),
GET_STAT(iface, ipv6.sent),
GET_STAT(iface, ipv6.drop),
GET_STAT(iface, ipv6.forwarded));
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
PR("IPv6 ND recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, ipv6_nd.recv),
GET_STAT(iface, ipv6_nd.sent),
GET_STAT(iface, ipv6_nd.drop));
#endif /* CONFIG_NET_STATISTICS_IPV6_ND */
#if defined(CONFIG_NET_STATISTICS_MLD)
PR("IPv6 MLD recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, ipv6_mld.recv),
GET_STAT(iface, ipv6_mld.sent),
GET_STAT(iface, ipv6_mld.drop));
#endif /* CONFIG_NET_STATISTICS_MLD */
#endif /* CONFIG_NET_STATISTICS_IPV6 */
#if defined(CONFIG_NET_STATISTICS_IPV4) && defined(CONFIG_NET_NATIVE_IPV4)
PR("IPv4 recv %d\tsent\t%d\tdrop\t%d\tforwarded\t%d\n",
GET_STAT(iface, ipv4.recv),
GET_STAT(iface, ipv4.sent),
GET_STAT(iface, ipv4.drop),
GET_STAT(iface, ipv4.forwarded));
#endif /* CONFIG_NET_STATISTICS_IPV4 */
PR("IP vhlerr %d\thblener\t%d\tlblener\t%d\n",
GET_STAT(iface, ip_errors.vhlerr),
GET_STAT(iface, ip_errors.hblenerr),
GET_STAT(iface, ip_errors.lblenerr));
PR("IP fragerr %d\tchkerr\t%d\tprotoer\t%d\n",
GET_STAT(iface, ip_errors.fragerr),
GET_STAT(iface, ip_errors.chkerr),
GET_STAT(iface, ip_errors.protoerr));
#if defined(CONFIG_NET_STATISTICS_ICMP) && defined(CONFIG_NET_NATIVE_IPV4)
PR("ICMP recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, icmp.recv),
GET_STAT(iface, icmp.sent),
GET_STAT(iface, icmp.drop));
PR("ICMP typeer %d\tchkerr\t%d\n",
GET_STAT(iface, icmp.typeerr),
GET_STAT(iface, icmp.chkerr));
#endif
#if defined(CONFIG_NET_STATISTICS_IGMP)
PR("IGMP recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, ipv4_igmp.recv),
GET_STAT(iface, ipv4_igmp.sent),
GET_STAT(iface, ipv4_igmp.drop));
#endif /* CONFIG_NET_STATISTICS_IGMP */
#if defined(CONFIG_NET_STATISTICS_UDP) && defined(CONFIG_NET_NATIVE_UDP)
PR("UDP recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, udp.recv),
GET_STAT(iface, udp.sent),
GET_STAT(iface, udp.drop));
PR("UDP chkerr %d\n",
GET_STAT(iface, udp.chkerr));
#endif
#if defined(CONFIG_NET_STATISTICS_TCP) && defined(CONFIG_NET_NATIVE_TCP)
PR("TCP bytes recv %u\tsent\t%d\tresent\t%d\n",
GET_STAT(iface, tcp.bytes.received),
GET_STAT(iface, tcp.bytes.sent),
GET_STAT(iface, tcp.resent));
PR("TCP seg recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, tcp.recv),
GET_STAT(iface, tcp.sent),
GET_STAT(iface, tcp.seg_drop));
PR("TCP seg resent %d\tchkerr\t%d\tackerr\t%d\n",
GET_STAT(iface, tcp.rexmit),
GET_STAT(iface, tcp.chkerr),
GET_STAT(iface, tcp.ackerr));
PR("TCP seg rsterr %d\trst\t%d\n",
GET_STAT(iface, tcp.rsterr),
GET_STAT(iface, tcp.rst));
PR("TCP conn drop %d\tconnrst\t%d\n",
GET_STAT(iface, tcp.conndrop),
GET_STAT(iface, tcp.connrst));
PR("TCP pkt drop %d\n", GET_STAT(iface, tcp.drop));
#endif
PR("Bytes received %u\n", GET_STAT(iface, bytes.received));
PR("Bytes sent %u\n", GET_STAT(iface, bytes.sent));
PR("Processing err %d\n", GET_STAT(iface, processing_error));
print_tc_tx_stats(shell, iface);
print_tc_rx_stats(shell, iface);
#if defined(CONFIG_NET_STATISTICS_ETHERNET) && \
defined(CONFIG_NET_STATISTICS_USER_API)
if (iface && net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
struct net_stats_eth eth_data;
int ret;
ret = net_mgmt(NET_REQUEST_STATS_GET_ETHERNET, iface,
ð_data, sizeof(eth_data));
if (!ret) {
print_eth_stats(iface, ð_data, shell);
}
}
#endif /* CONFIG_NET_STATISTICS_ETHERNET && CONFIG_NET_STATISTICS_USER_API */
#if defined(CONFIG_NET_STATISTICS_PPP) && \
defined(CONFIG_NET_STATISTICS_USER_API)
if (iface && net_if_l2(iface) == &NET_L2_GET_NAME(PPP)) {
struct net_stats_ppp ppp_data;
int ret;
ret = net_mgmt(NET_REQUEST_STATS_GET_PPP, iface,
&ppp_data, sizeof(ppp_data));
if (!ret) {
print_ppp_stats(iface, &ppp_data, shell);
}
}
#endif /* CONFIG_NET_STATISTICS_PPP && CONFIG_NET_STATISTICS_USER_API */
print_net_pm_stats(shell, iface);
}
#endif /* CONFIG_NET_STATISTICS */
#if defined(CONFIG_NET_OFFLOAD) || defined(CONFIG_NET_NATIVE)
static void get_addresses(struct net_context *context,
char addr_local[], int local_len,
char addr_remote[], int remote_len)
{
#if defined(CONFIG_NET_IPV6)
if (context->local.family == AF_INET6) {
snprintk(addr_local, local_len, "[%s]:%u",
net_sprint_ipv6_addr(
net_sin6_ptr(&context->local)->sin6_addr),
ntohs(net_sin6_ptr(&context->local)->sin6_port));
snprintk(addr_remote, remote_len, "[%s]:%u",
net_sprint_ipv6_addr(
&net_sin6(&context->remote)->sin6_addr),
ntohs(net_sin6(&context->remote)->sin6_port));
} else
#endif
#if defined(CONFIG_NET_IPV4)
if (context->local.family == AF_INET) {
snprintk(addr_local, local_len, "%s:%d",
net_sprint_ipv4_addr(
net_sin_ptr(&context->local)->sin_addr),
ntohs(net_sin_ptr(&context->local)->sin_port));
snprintk(addr_remote, remote_len, "%s:%d",
net_sprint_ipv4_addr(
&net_sin(&context->remote)->sin_addr),
ntohs(net_sin(&context->remote)->sin_port));
} else
#endif
if (context->local.family == AF_UNSPEC) {
snprintk(addr_local, local_len, "AF_UNSPEC");
} else if (context->local.family == AF_PACKET) {
snprintk(addr_local, local_len, "AF_PACKET");
} else if (context->local.family == AF_CAN) {
snprintk(addr_local, local_len, "AF_CAN");
} else {
snprintk(addr_local, local_len, "AF_UNK(%d)",
context->local.family);
}
}
static void context_cb(struct net_context *context, void *user_data)
{
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
#define ADDR_LEN NET_IPV6_ADDR_LEN
#elif defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
#define ADDR_LEN NET_IPV4_ADDR_LEN
#else
#define ADDR_LEN NET_IPV6_ADDR_LEN
#endif
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
int *count = data->user_data;
/* +7 for []:port */
char addr_local[ADDR_LEN + 7];
char addr_remote[ADDR_LEN + 7] = "";
get_addresses(context, addr_local, sizeof(addr_local),
addr_remote, sizeof(addr_remote));
PR("[%2d] %p\t%d %c%c%c %16s\t%16s\n",
(*count) + 1, context,
net_if_get_by_iface(net_context_get_iface(context)),
net_context_get_family(context) == AF_INET6 ? '6' :
(net_context_get_family(context) == AF_INET ? '4' : ' '),
net_context_get_type(context) == SOCK_DGRAM ? 'D' :
(net_context_get_type(context) == SOCK_STREAM ? 'S' :
(net_context_get_type(context) == SOCK_RAW ? 'R' : ' ')),
net_context_get_ip_proto(context) == IPPROTO_UDP ? 'U' :
(net_context_get_ip_proto(context) == IPPROTO_TCP ? 'T' : ' '),
addr_local, addr_remote);
(*count)++;
}
#endif /* CONFIG_NET_OFFLOAD || CONFIG_NET_NATIVE */
#if CONFIG_NET_CONN_LOG_LEVEL >= LOG_LEVEL_DBG
static void conn_handler_cb(struct net_conn *conn, void *user_data)
{
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
#define ADDR_LEN NET_IPV6_ADDR_LEN
#elif defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
#define ADDR_LEN NET_IPV4_ADDR_LEN
#else
#define ADDR_LEN NET_IPV6_ADDR_LEN
#endif
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
int *count = data->user_data;
/* +7 for []:port */
char addr_local[ADDR_LEN + 7];
char addr_remote[ADDR_LEN + 7] = "";
#if defined(CONFIG_NET_IPV6)
if (conn->local_addr.sa_family == AF_INET6) {
snprintk(addr_local, sizeof(addr_local), "[%s]:%u",
net_sprint_ipv6_addr(
&net_sin6(&conn->local_addr)->sin6_addr),
ntohs(net_sin6(&conn->local_addr)->sin6_port));
snprintk(addr_remote, sizeof(addr_remote), "[%s]:%u",
net_sprint_ipv6_addr(
&net_sin6(&conn->remote_addr)->sin6_addr),
ntohs(net_sin6(&conn->remote_addr)->sin6_port));
} else
#endif
#if defined(CONFIG_NET_IPV4)
if (conn->local_addr.sa_family == AF_INET) {
snprintk(addr_local, sizeof(addr_local), "%s:%d",
net_sprint_ipv4_addr(
&net_sin(&conn->local_addr)->sin_addr),
ntohs(net_sin(&conn->local_addr)->sin_port));
snprintk(addr_remote, sizeof(addr_remote), "%s:%d",
net_sprint_ipv4_addr(
&net_sin(&conn->remote_addr)->sin_addr),
ntohs(net_sin(&conn->remote_addr)->sin_port));
} else
#endif
#ifdef CONFIG_NET_L2_CANBUS
if (conn->local_addr.sa_family == AF_CAN) {
snprintk(addr_local, sizeof(addr_local), "-");
} else
#endif
if (conn->local_addr.sa_family == AF_UNSPEC) {
snprintk(addr_local, sizeof(addr_local), "AF_UNSPEC");
} else {
snprintk(addr_local, sizeof(addr_local), "AF_UNK(%d)",
conn->local_addr.sa_family);
}
PR("[%2d] %p %p\t%s\t%16s\t%16s\n",
(*count) + 1, conn, conn->cb,
net_proto2str(conn->local_addr.sa_family, conn->proto),
addr_local, addr_remote);
(*count)++;
}
#endif /* CONFIG_NET_CONN_LOG_LEVEL >= LOG_LEVEL_DBG */
#if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
struct tcp2_detail_info {
int printed_send_queue_header;
int printed_details;
int count;
};
#endif
#if defined(CONFIG_NET_TCP2) && \
(defined(CONFIG_NET_OFFLOAD) || defined(CONFIG_NET_NATIVE))
static void tcp_cb(struct tcp *conn, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
int *count = data->user_data;
uint16_t recv_mss = net_tcp_get_recv_mss(conn);
PR("%p %p %5u %5u %10u %10u %5u %s\n",
conn, conn->context,
ntohs(net_sin6_ptr(&conn->context->local)->sin6_port),
ntohs(net_sin6(&conn->context->remote)->sin6_port),
conn->seq, conn->ack, recv_mss,
net_tcp_state_str(net_tcp_get_state(conn)));
(*count)++;
}
#if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
static void tcp_sent_list_cb(struct tcp *conn, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
struct tcp2_detail_info *details = data->user_data;
struct net_pkt *pkt;
sys_snode_t *node;
if (conn->state != TCP_LISTEN) {
if (!details->printed_details) {
PR("\nTCP Ref Recv_win Send_win Pending "
"Unacked Flags Queue\n");
details->printed_details = true;
}
PR("%p %d %u\t %u\t %zd\t %d\t %d/%d/%d %s\n",
conn, atomic_get(&conn->ref_count), conn->recv_win,
conn->send_win, conn->send_data_total, conn->unacked_len,
conn->in_retransmission, conn->in_connect, conn->in_close,
sys_slist_is_empty(&conn->send_queue) ? "empty" : "data");
details->count++;
}
if (sys_slist_is_empty(&conn->send_queue)) {
return;
}
if (!details->printed_send_queue_header) {
PR("\nTCP packets waiting ACK:\n");
PR("TCP net_pkt[ref/totlen]->net_buf[ref/len]..."
"\n");
}
PR("%p ", conn);
node = sys_slist_peek_head(&conn->send_queue);
if (node) {
pkt = CONTAINER_OF(node, struct net_pkt, next);
if (pkt) {
struct net_buf *frag = pkt->frags;
if (!details->printed_send_queue_header) {
PR("%p[%d/%zd]", pkt,
atomic_get(&pkt->atomic_ref),
net_pkt_get_len(pkt));
details->printed_send_queue_header = true;
} else {
PR(" %p[%d/%zd]",
pkt, atomic_get(&pkt->atomic_ref),
net_pkt_get_len(pkt));
}
if (frag) {
PR("->");
}
while (frag) {
PR("%p[%d/%d]", frag, frag->ref, frag->len);
frag = frag->frags;
if (frag) {
PR("->");
}
}
PR("\n");
}
}
details->printed_send_queue_header = true;
}
#endif /* CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG */
#endif /* TCP2 */
#if defined(CONFIG_NET_IPV6_FRAGMENT)
static void ipv6_frag_cb(struct net_ipv6_reassembly *reass,
void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
int *count = data->user_data;
char src[ADDR_LEN];
int i;
if (!*count) {
PR("\nIPv6 reassembly Id Remain "
"Src \tDst\n");
}
snprintk(src, ADDR_LEN, "%s", net_sprint_ipv6_addr(&reass->src));
PR("%p 0x%08x %5d %16s\t%16s\n", reass, reass->id,
k_ticks_to_ms_ceil32(k_work_delayable_remaining_get(&reass->timer)),
src, net_sprint_ipv6_addr(&reass->dst));
for (i = 0; i < CONFIG_NET_IPV6_FRAGMENT_MAX_PKT; i++) {
if (reass->pkt[i]) {
struct net_buf *frag = reass->pkt[i]->frags;
PR("[%d] pkt %p->", i, reass->pkt[i]);
while (frag) {
PR("%p", frag);
frag = frag->frags;
if (frag) {
PR("->");
}
}
PR("\n");
}
}
(*count)++;
}
#endif /* CONFIG_NET_IPV6_FRAGMENT */
#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
static void allocs_cb(struct net_pkt *pkt,
struct net_buf *buf,
const char *func_alloc,
int line_alloc,
const char *func_free,
int line_free,
bool in_use,
void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
const char *str;
if (in_use) {
str = "used";
} else {
if (func_alloc) {
str = "free";
} else {
str = "avail";
}
}
if (buf) {
goto buf;
}
if (func_alloc) {
if (in_use) {
PR("%p/%d\t%5s\t%5s\t%s():%d\n",
pkt, atomic_get(&pkt->atomic_ref), str,
net_pkt_slab2str(pkt->slab),
func_alloc, line_alloc);
} else {
PR("%p\t%5s\t%5s\t%s():%d -> %s():%d\n",
pkt, str, net_pkt_slab2str(pkt->slab),
func_alloc, line_alloc, func_free,
line_free);
}
}
return;
buf:
if (func_alloc) {
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
if (in_use) {
PR("%p/%d\t%5s\t%5s\t%s():%d\n",
buf, buf->ref,
str, net_pkt_pool2str(pool), func_alloc,
line_alloc);
} else {
PR("%p\t%5s\t%5s\t%s():%d -> %s():%d\n",
buf, str, net_pkt_pool2str(pool),
func_alloc, line_alloc, func_free,
line_free);
}
}
}
#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
/* Put the actual shell commands after this */
static int cmd_net_allocs(const struct shell *shell, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
struct net_shell_user_data user_data;
#endif
ARG_UNUSED(argc);
ARG_UNUSED(argv);
#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
user_data.shell = shell;
PR("Network memory allocations\n\n");
PR("memory\t\tStatus\tPool\tFunction alloc -> freed\n");
net_pkt_allocs_foreach(allocs_cb, &user_data);
#else
PR_INFO("Set %s to enable %s support.\n",
"CONFIG_NET_DEBUG_NET_PKT_ALLOC", "net_pkt allocation");
#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
return 0;
}
#if defined(CONFIG_NET_ARP) && defined(CONFIG_NET_NATIVE)
static void arp_cb(struct arp_entry *entry, void *user_data)
{
struct net_shell_user_data *data = user_data;
const struct shell *shell = data->shell;
int *count = data->user_data;
if (*count == 0) {
PR(" Interface Link Address\n");
}
PR("[%2d] %d %s %s\n", *count,
net_if_get_by_iface(entry->iface),
net_sprint_ll_addr(entry->eth.addr, sizeof(struct net_eth_addr)),
net_sprint_ipv4_addr(&entry->ip));
(*count)++;
}
#endif /* CONFIG_NET_ARP */
#if !defined(CONFIG_NET_ARP)
static void print_arp_error(const struct shell *shell)
{
PR_INFO("Set %s to enable %s support.\n",
"CONFIG_NET_NATIVE, CONFIG_NET_ARP, CONFIG_NET_IPV4 and"
" CONFIG_NET_L2_ETHERNET", "ARP");
}
#endif
static int cmd_net_arp(const struct shell *shell, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_ARP)
struct net_shell_user_data user_data;
int arg = 1;
#endif
ARG_UNUSED(argc);
#if defined(CONFIG_NET_ARP)
if (!argv[arg]) {
/* ARP cache content */
int count = 0;
user_data.shell = shell;
user_data.user_data = &count;
if (net_arp_foreach(arp_cb, &user_data) == 0) {
PR("ARP cache is empty.\n");
}
}
#else
print_arp_error(shell);
#endif
return 0;
}
static int cmd_net_arp_flush(const struct shell *shell, size_t argc,
char *argv[])
{