2 * Assorted library VTY commands
4 * Copyright (C) 1998 Kunihiro Ishiguro
5 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
27 #ifdef HAVE_MALLOC_MALLOC_H
28 #include <malloc/malloc.h>
40 #include "northbound_cli.h"
42 /* Looking up memory status from vty interface. */
47 #if defined(HAVE_MALLINFO2) || defined(HAVE_MALLINFO)
48 static int show_memory_mallinfo(struct vty
*vty
)
50 #if defined(HAVE_MALLINFO2)
51 struct mallinfo2 minfo
= mallinfo2();
52 #elif defined(HAVE_MALLINFO)
53 struct mallinfo minfo
= mallinfo();
55 char buf
[MTYPE_MEMSTR_LEN
];
57 vty_out(vty
, "System allocator statistics:\n");
58 vty_out(vty
, " Total heap allocated: %s\n",
59 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.arena
));
60 vty_out(vty
, " Holding block headers: %s\n",
61 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.hblkhd
));
62 vty_out(vty
, " Used small blocks: %s\n",
63 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.usmblks
));
64 vty_out(vty
, " Used ordinary blocks: %s\n",
65 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.uordblks
));
66 vty_out(vty
, " Free small blocks: %s\n",
67 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.fsmblks
));
68 vty_out(vty
, " Free ordinary blocks: %s\n",
69 mtype_memstr(buf
, MTYPE_MEMSTR_LEN
, minfo
.fordblks
));
70 vty_out(vty
, " Ordinary blocks: %ld\n",
71 (unsigned long)minfo
.ordblks
);
72 vty_out(vty
, " Small blocks: %ld\n",
73 (unsigned long)minfo
.smblks
);
74 vty_out(vty
, " Holding blocks: %ld\n",
75 (unsigned long)minfo
.hblks
);
76 vty_out(vty
, "(see system documentation for 'mallinfo' for meaning)\n");
79 #endif /* HAVE_MALLINFO */
81 static int qmem_walker(void *arg
, struct memgroup
*mg
, struct memtype
*mt
)
83 struct vty
*vty
= arg
;
85 vty_out(vty
, "--- qmem %s ---\n", mg
->name
);
86 vty_out(vty
, "%-30s: %8s %-8s%s %8s %9s\n",
87 "Type", "Current#", " Size",
88 #ifdef HAVE_MALLOC_USABLE_SIZE
94 #ifdef HAVE_MALLOC_USABLE_SIZE
101 if (mt
->n_max
!= 0) {
103 snprintf(size
, sizeof(size
), "%6zu", mt
->size
);
104 #ifdef HAVE_MALLOC_USABLE_SIZE
106 #define TARG , mt->total
107 #define TARG2 , mt->max_size
113 vty_out(vty
, "%-30s: %8zu %-8s"TSTR
" %8zu"TSTR
"\n",
117 : mt
->size
== SIZE_VAR
129 DEFUN_NOSH (show_memory
,
132 "Show running system information\n"
133 "Memory statistics\n")
136 show_memory_mallinfo(vty
);
137 #endif /* HAVE_MALLINFO */
139 qmem_walk(qmem_walker
, vty
);
143 DEFUN_NOSH (show_modules
,
146 "Show running system information\n"
149 struct frrmod_runtime
*plug
= frrmod_list
;
151 vty_out(vty
, "%-12s %-25s %s\n\n", "Module Name", "Version",
154 const struct frrmod_info
*i
= plug
->info
;
156 vty_out(vty
, "%-12s %-25s %s\n", i
->name
, i
->version
,
158 if (plug
->dl_handle
) {
159 #ifdef HAVE_DLINFO_ORIGIN
160 char origin
[MAXPATHLEN
] = "";
161 dlinfo(plug
->dl_handle
, RTLD_DI_ORIGIN
, &origin
);
162 #ifdef HAVE_DLINFO_LINKMAP
164 struct link_map
*lm
= NULL
;
165 dlinfo(plug
->dl_handle
, RTLD_DI_LINKMAP
, &lm
);
167 name
= strrchr(lm
->l_name
, '/');
168 name
= name
? name
+ 1 : lm
->l_name
;
169 vty_out(vty
, "\tfrom: %s/%s\n", origin
, name
);
172 vty_out(vty
, "\tfrom: %s \n", origin
, plug
->load_name
);
175 vty_out(vty
, "\tfrom: %s\n", plug
->load_name
);
181 vty_out(vty
, "pid: %u\n", (uint32_t)(getpid()));
188 "frr defaults PROFILE...",
189 "FRRouting global parameters\n"
190 "set of configuration defaults used\n"
193 char *profile
= argv_concat(argv
, argc
, 2);
194 int rv
= CMD_SUCCESS
;
196 if (!frr_defaults_profile_valid(profile
)) {
197 vty_out(vty
, "%% WARNING: profile %s is not known in this version\n",
201 frr_defaults_profile_set(profile
);
202 XFREE(MTYPE_TMP
, profile
);
208 "frr version VERSION...",
209 "FRRouting global parameters\n"
210 "version configuration was written by\n"
213 char *version
= argv_concat(argv
, argc
, 2);
215 frr_defaults_version_set(version
);
216 XFREE(MTYPE_TMP
, version
);
220 static struct call_back
{
223 void (*start_config
)(void);
224 void (*end_config
)(void);
228 DEFUN_HIDDEN (start_config
,
230 "XFRR_start_configuration",
231 "The Beginning of Configuration\n")
233 callback
.readin_time
= monotime(NULL
);
235 vty
->pending_allowed
= 1;
237 if (callback
.start_config
)
238 (*callback
.start_config
)();
243 DEFUN_HIDDEN (end_config
,
245 "XFRR_end_configuration",
246 "The End of Configuration\n")
249 char readin_time_str
[MONOTIME_STRLEN
];
252 readin_time
= monotime(NULL
);
253 readin_time
-= callback
.readin_time
;
255 frrtime_to_interval(readin_time
, readin_time_str
,
256 sizeof(readin_time_str
));
258 vty
->pending_allowed
= 0;
259 ret
= nb_cli_pending_commit_check(vty
);
261 zlog_info("Configuration Read in Took: %s", readin_time_str
);
263 if (callback
.end_config
)
264 (*callback
.end_config
)();
269 void cmd_init_config_callbacks(void (*start_config_cb
)(void),
270 void (*end_config_cb
)(void))
272 callback
.start_config
= start_config_cb
;
273 callback
.end_config
= end_config_cb
;
277 static void defaults_autocomplete(vector comps
, struct cmd_token
*token
)
281 for (p
= frr_defaults_profiles
; *p
; p
++)
282 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, *p
));
285 static const struct cmd_variable_handler default_var_handlers
[] = {
286 {.tokenname
= "PROFILE", .completions
= defaults_autocomplete
},
287 {.completions
= NULL
},
290 void lib_cmd_init(void)
292 cmd_variable_handler_register(default_var_handlers
);
294 install_element(CONFIG_NODE
, &frr_defaults_cmd
);
295 install_element(CONFIG_NODE
, &frr_version_cmd
);
297 install_element(VIEW_NODE
, &show_memory_cmd
);
298 install_element(VIEW_NODE
, &show_modules_cmd
);
300 install_element(CONFIG_NODE
, &start_config_cmd
);
301 install_element(CONFIG_NODE
, &end_config_cmd
);
304 /* Stats querying from users */
305 /* Return a pointer to a human friendly string describing
306 * the byte count passed in. E.g:
307 * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
308 * Up to 4 significant figures will be given.
309 * The pointer returned may be NULL (indicating an error)
310 * or point to the given buffer, or point to static storage.
312 const char *mtype_memstr(char *buf
, size_t len
, unsigned long bytes
)
323 * When we pass the 2gb barrier mallinfo() can no longer report
324 * correct data so it just does something odd...
325 * Reporting like Terrabytes of data. Which makes users...
326 * edgy.. yes edgy that's the term for it.
327 * So let's just give up gracefully
329 if (bytes
> 0x7fffffff)
336 if (bytes
& (1 << 19))
338 snprintf(buf
, len
, "%d MiB", m
);
340 if (bytes
& (1 << 9))
342 snprintf(buf
, len
, "%d KiB", k
);
344 snprintf(buf
, len
, "%ld bytes", bytes
);