Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(net_shell);
#include "net_shell_private.h"
struct ctx_info {
int pos;
bool are_external_pools;
struct k_mem_slab *tx_slabs[CONFIG_NET_MAX_CONTEXTS];
struct net_buf_pool *data_pools[CONFIG_NET_MAX_CONTEXTS];
};
#if defined(CONFIG_NET_OFFLOAD) || defined(CONFIG_NET_NATIVE)
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
static bool slab_pool_found_already(struct ctx_info *info,
struct k_mem_slab *slab,
struct net_buf_pool *pool)
{
int i;
for (i = 0; i < CONFIG_NET_MAX_CONTEXTS; i++) {
if (slab) {
if (info->tx_slabs[i] == slab) {
return true;
}
} else {
if (info->data_pools[i] == pool) {
return true;
}
}
}
return false;
}
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
static void context_info(struct net_context *context, void *user_data)
{
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
struct net_shell_user_data *data = user_data;
const struct shell *sh = data->sh;
struct ctx_info *info = data->user_data;
struct k_mem_slab *slab;
struct net_buf_pool *pool;
if (!net_context_is_used(context)) {
return;
}
if (context->tx_slab) {
slab = context->tx_slab();
if (slab_pool_found_already(info, slab, NULL)) {
return;
}
#if defined(CONFIG_NET_BUF_POOL_USAGE)
PR("%p\t%u\t%u\tETX\n",
slab, slab->info.num_blocks, k_mem_slab_num_free_get(slab));
#else
PR("%p\t%d\tETX\n", slab, slab->info.num_blocks);
#endif
info->are_external_pools = true;
info->tx_slabs[info->pos] = slab;
}
if (context->data_pool) {
pool = context->data_pool();
if (slab_pool_found_already(info, NULL, pool)) {
return;
}
#if defined(CONFIG_NET_BUF_POOL_USAGE)
PR("%p\t%d\t%ld\tEDATA (%s)\n", pool, pool->buf_count,
atomic_get(&pool->avail_count), pool->name);
#else
PR("%p\t%d\tEDATA\n", pool, pool->buf_count);
#endif
info->are_external_pools = true;
info->data_pools[info->pos] = pool;
}
info->pos++;
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
}
#endif /* CONFIG_NET_OFFLOAD || CONFIG_NET_NATIVE */
static int cmd_net_mem(const struct shell *sh, size_t argc, char *argv[])
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
#if defined(CONFIG_NET_OFFLOAD) || defined(CONFIG_NET_NATIVE)
struct k_mem_slab *rx, *tx;
struct net_buf_pool *rx_data, *tx_data;
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
#if defined(CONFIG_NET_BUF_FIXED_DATA_SIZE)
PR("Fragment length %d bytes\n", CONFIG_NET_BUF_DATA_SIZE);
#else
PR("Fragment data pool size %d bytes\n", CONFIG_NET_BUF_DATA_POOL_SIZE);
#endif /* CONFIG_NET_BUF_FIXED_DATA_SIZE */
PR("Network buffer pools:\n");
#if defined(CONFIG_NET_BUF_POOL_USAGE)
PR("Address\t\tTotal\tAvail\tName\n");
PR("%p\t%d\t%u\tRX\n",
rx, rx->info.num_blocks, k_mem_slab_num_free_get(rx));
PR("%p\t%d\t%u\tTX\n",
tx, tx->info.num_blocks, k_mem_slab_num_free_get(tx));
PR("%p\t%d\t%ld\tRX DATA (%s)\n", rx_data, rx_data->buf_count,
atomic_get(&rx_data->avail_count), rx_data->name);
PR("%p\t%d\t%ld\tTX DATA (%s)\n", tx_data, tx_data->buf_count,
atomic_get(&tx_data->avail_count), tx_data->name);
#else
PR("Address\t\tTotal\tName\n");
PR("%p\t%d\tRX\n", rx, rx->info.num_blocks);
PR("%p\t%d\tTX\n", tx, tx->info.num_blocks);
PR("%p\t%d\tRX DATA\n", rx_data, rx_data->buf_count);
PR("%p\t%d\tTX DATA\n", tx_data, tx_data->buf_count);
PR_INFO("Set %s to enable %s support.\n",
"CONFIG_NET_BUF_POOL_USAGE", "net_buf allocation");
#endif /* CONFIG_NET_BUF_POOL_USAGE */
if (IS_ENABLED(CONFIG_NET_CONTEXT_NET_PKT_POOL)) {
struct net_shell_user_data user_data;
struct ctx_info info;
(void)memset(&info, 0, sizeof(info));
user_data.sh = sh;
user_data.user_data = &info;
net_context_foreach(context_info, &user_data);
if (!info.are_external_pools) {
PR("No external memory pools found.\n");
}
}
#else
PR_INFO("Set %s to enable %s support.\n",
"CONFIG_NET_OFFLOAD or CONFIG_NET_NATIVE", "memory usage");
#endif /* CONFIG_NET_OFFLOAD || CONFIG_NET_NATIVE */
return 0;
}
SHELL_SUBCMD_ADD((net), mem, NULL,
"Print information about network memory usage.",
cmd_net_mem, 1, 0);
|