]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
1c0d8808 | 2 | * Assorted library VTY commands |
718e3744 | 3 | * |
b249c083 DL |
4 | * Copyright (C) 1998 Kunihiro Ishiguro |
5 | * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc. | |
718e3744 | 6 | * |
b249c083 DL |
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) | |
10 | * any later version. | |
718e3744 | 11 | * |
b249c083 DL |
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 | |
15 | * more details. | |
718e3744 | 16 | * |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
d09552d0 | 23 | /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */ |
fa896a1d | 24 | #ifdef HAVE_MALLOC_H |
41be32bf | 25 | #include <malloc.h> |
fa896a1d DL |
26 | #endif |
27 | #ifdef HAVE_MALLOC_MALLOC_H | |
28 | #include <malloc/malloc.h> | |
29 | #endif | |
b249c083 | 30 | #include <dlfcn.h> |
7b90f00c | 31 | #ifdef HAVE_LINK_H |
b249c083 | 32 | #include <link.h> |
7b90f00c | 33 | #endif |
718e3744 | 34 | |
35 | #include "log.h" | |
36 | #include "memory.h" | |
b249c083 | 37 | #include "module.h" |
ac4adef4 | 38 | #include "defaults.h" |
1c0d8808 | 39 | #include "lib_vty.h" |
fd396924 | 40 | #include "northbound_cli.h" |
6b0655a2 | 41 | |
718e3744 | 42 | /* Looking up memory status from vty interface. */ |
43 | #include "vector.h" | |
44 | #include "vty.h" | |
45 | #include "command.h" | |
46 | ||
51ab4dba | 47 | #if defined(HAVE_MALLINFO2) || defined(HAVE_MALLINFO) |
d62a17ae | 48 | static int show_memory_mallinfo(struct vty *vty) |
41be32bf | 49 | { |
51ab4dba QY |
50 | #if defined(HAVE_MALLINFO2) |
51 | struct mallinfo2 minfo = mallinfo2(); | |
52 | #elif defined(HAVE_MALLINFO) | |
d62a17ae | 53 | struct mallinfo minfo = mallinfo(); |
51ab4dba | 54 | #endif |
d62a17ae | 55 | char buf[MTYPE_MEMSTR_LEN]; |
56 | ||
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"); | |
77 | return 1; | |
41be32bf PJ |
78 | } |
79 | #endif /* HAVE_MALLINFO */ | |
80 | ||
3b4cd783 DL |
81 | static int qmem_walker(void *arg, struct memgroup *mg, struct memtype *mt) |
82 | { | |
83 | struct vty *vty = arg; | |
13f1ba41 | 84 | if (!mt) { |
d62a17ae | 85 | vty_out(vty, "--- qmem %s ---\n", mg->name); |
13f1ba41 LB |
86 | vty_out(vty, "%-30s: %8s %-8s%s %8s %9s\n", |
87 | "Type", "Current#", " Size", | |
88 | #ifdef HAVE_MALLOC_USABLE_SIZE | |
89 | " Total", | |
90 | #else | |
91 | "", | |
92 | #endif | |
93 | "Max#", | |
94 | #ifdef HAVE_MALLOC_USABLE_SIZE | |
95 | "MaxBytes" | |
96 | #else | |
97 | "" | |
98 | #endif | |
99 | ); | |
100 | } else { | |
a0c5e446 | 101 | if (mt->n_max != 0) { |
3694c43a DS |
102 | char size[32]; |
103 | snprintf(size, sizeof(size), "%6zu", mt->size); | |
602a6584 DL |
104 | #ifdef HAVE_MALLOC_USABLE_SIZE |
105 | #define TSTR " %9zu" | |
106 | #define TARG , mt->total | |
13f1ba41 | 107 | #define TARG2 , mt->max_size |
602a6584 DL |
108 | #else |
109 | #define TSTR "" | |
110 | #define TARG | |
13f1ba41 | 111 | #define TARG2 |
602a6584 | 112 | #endif |
13f1ba41 LB |
113 | vty_out(vty, "%-30s: %8zu %-8s"TSTR" %8zu"TSTR"\n", |
114 | mt->name, | |
d62a17ae | 115 | mt->n_alloc, |
116 | mt->size == 0 ? "" | |
117 | : mt->size == SIZE_VAR | |
13f1ba41 | 118 | ? "variable" |
602a6584 | 119 | : size |
13f1ba41 LB |
120 | TARG, |
121 | mt->n_max | |
122 | TARG2); | |
3694c43a | 123 | } |
3b4cd783 DL |
124 | } |
125 | return 0; | |
126 | } | |
127 | ||
128 | ||
1c0d8808 DL |
129 | DEFUN_NOSH (show_memory, |
130 | show_memory_cmd, | |
131 | "show memory", | |
132 | "Show running system information\n" | |
133 | "Memory statistics\n") | |
718e3744 | 134 | { |
41be32bf | 135 | #ifdef HAVE_MALLINFO |
d62a17ae | 136 | show_memory_mallinfo(vty); |
41be32bf | 137 | #endif /* HAVE_MALLINFO */ |
fc7948fa | 138 | |
d62a17ae | 139 | qmem_walk(qmem_walker, vty); |
140 | return CMD_SUCCESS; | |
718e3744 | 141 | } |
142 | ||
1c0d8808 DL |
143 | DEFUN_NOSH (show_modules, |
144 | show_modules_cmd, | |
145 | "show modules", | |
146 | "Show running system information\n" | |
147 | "Loaded modules\n") | |
b249c083 | 148 | { |
d62a17ae | 149 | struct frrmod_runtime *plug = frrmod_list; |
b249c083 | 150 | |
d62a17ae | 151 | vty_out(vty, "%-12s %-25s %s\n\n", "Module Name", "Version", |
152 | "Description"); | |
153 | while (plug) { | |
154 | const struct frrmod_info *i = plug->info; | |
b249c083 | 155 | |
d62a17ae | 156 | vty_out(vty, "%-12s %-25s %s\n", i->name, i->version, |
157 | i->description); | |
158 | if (plug->dl_handle) { | |
b249c083 | 159 | #ifdef HAVE_DLINFO_ORIGIN |
d62a17ae | 160 | char origin[MAXPATHLEN] = ""; |
161 | dlinfo(plug->dl_handle, RTLD_DI_ORIGIN, &origin); | |
162 | #ifdef HAVE_DLINFO_LINKMAP | |
163 | const char *name; | |
164 | struct link_map *lm = NULL; | |
165 | dlinfo(plug->dl_handle, RTLD_DI_LINKMAP, &lm); | |
166 | if (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); | |
170 | } | |
b249c083 | 171 | #else |
d62a17ae | 172 | vty_out(vty, "\tfrom: %s \n", origin, plug->load_name); |
b249c083 | 173 | #endif |
d62a17ae | 174 | #else |
175 | vty_out(vty, "\tfrom: %s\n", plug->load_name); | |
176 | #endif | |
177 | } | |
178 | plug = plug->next; | |
179 | } | |
bfae97a6 MS |
180 | |
181 | vty_out(vty, "pid: %u\n", (uint32_t)(getpid())); | |
182 | ||
d62a17ae | 183 | return CMD_SUCCESS; |
b249c083 DL |
184 | } |
185 | ||
ac4adef4 DL |
186 | DEFUN (frr_defaults, |
187 | frr_defaults_cmd, | |
188 | "frr defaults PROFILE...", | |
189 | "FRRouting global parameters\n" | |
190 | "set of configuration defaults used\n" | |
191 | "profile string\n") | |
192 | { | |
193 | char *profile = argv_concat(argv, argc, 2); | |
194 | int rv = CMD_SUCCESS; | |
195 | ||
196 | if (!frr_defaults_profile_valid(profile)) { | |
197 | vty_out(vty, "%% WARNING: profile %s is not known in this version\n", | |
198 | profile); | |
199 | rv = CMD_WARNING; | |
200 | } | |
201 | frr_defaults_profile_set(profile); | |
202 | XFREE(MTYPE_TMP, profile); | |
203 | return rv; | |
204 | } | |
205 | ||
206 | DEFUN (frr_version, | |
207 | frr_version_cmd, | |
208 | "frr version VERSION...", | |
209 | "FRRouting global parameters\n" | |
210 | "version configuration was written by\n" | |
211 | "version string\n") | |
212 | { | |
213 | char *version = argv_concat(argv, argc, 2); | |
214 | ||
215 | frr_defaults_version_set(version); | |
216 | XFREE(MTYPE_TMP, version); | |
217 | return CMD_SUCCESS; | |
218 | } | |
219 | ||
f8e6ada8 | 220 | static struct call_back { |
16ae941f DS |
221 | time_t readin_time; |
222 | ||
f8e6ada8 DS |
223 | void (*start_config)(void); |
224 | void (*end_config)(void); | |
225 | } callback; | |
226 | ||
16ae941f | 227 | |
f8e6ada8 DS |
228 | DEFUN_HIDDEN (start_config, |
229 | start_config_cmd, | |
8de2b3d9 | 230 | "XFRR_start_configuration", |
f8e6ada8 DS |
231 | "The Beginning of Configuration\n") |
232 | { | |
16ae941f DS |
233 | callback.readin_time = monotime(NULL); |
234 | ||
fd396924 CH |
235 | vty->pending_allowed = 1; |
236 | ||
f8e6ada8 DS |
237 | if (callback.start_config) |
238 | (*callback.start_config)(); | |
239 | ||
240 | return CMD_SUCCESS; | |
241 | } | |
242 | ||
243 | DEFUN_HIDDEN (end_config, | |
244 | end_config_cmd, | |
8de2b3d9 | 245 | "XFRR_end_configuration", |
f8e6ada8 DS |
246 | "The End of Configuration\n") |
247 | { | |
16ae941f DS |
248 | time_t readin_time; |
249 | char readin_time_str[MONOTIME_STRLEN]; | |
fd396924 | 250 | int ret; |
16ae941f DS |
251 | |
252 | readin_time = monotime(NULL); | |
253 | readin_time -= callback.readin_time; | |
254 | ||
255 | frrtime_to_interval(readin_time, readin_time_str, | |
256 | sizeof(readin_time_str)); | |
257 | ||
fd396924 CH |
258 | vty->pending_allowed = 0; |
259 | ret = nb_cli_pending_commit_check(vty); | |
260 | ||
16ae941f DS |
261 | zlog_info("Configuration Read in Took: %s", readin_time_str); |
262 | ||
f8e6ada8 DS |
263 | if (callback.end_config) |
264 | (*callback.end_config)(); | |
265 | ||
fd396924 | 266 | return ret; |
f8e6ada8 DS |
267 | } |
268 | ||
62b43962 IR |
269 | void cmd_init_config_callbacks(void (*start_config_cb)(void), |
270 | void (*end_config_cb)(void)) | |
f8e6ada8 | 271 | { |
62b43962 IR |
272 | callback.start_config = start_config_cb; |
273 | callback.end_config = end_config_cb; | |
f8e6ada8 DS |
274 | } |
275 | ||
276 | ||
ac4adef4 DL |
277 | static void defaults_autocomplete(vector comps, struct cmd_token *token) |
278 | { | |
279 | const char **p; | |
280 | ||
281 | for (p = frr_defaults_profiles; *p; p++) | |
282 | vector_set(comps, XSTRDUP(MTYPE_COMPLETION, *p)); | |
283 | } | |
284 | ||
285 | static const struct cmd_variable_handler default_var_handlers[] = { | |
286 | {.tokenname = "PROFILE", .completions = defaults_autocomplete}, | |
287 | {.completions = NULL}, | |
288 | }; | |
289 | ||
1c0d8808 | 290 | void lib_cmd_init(void) |
718e3744 | 291 | { |
ac4adef4 DL |
292 | cmd_variable_handler_register(default_var_handlers); |
293 | ||
294 | install_element(CONFIG_NODE, &frr_defaults_cmd); | |
295 | install_element(CONFIG_NODE, &frr_version_cmd); | |
296 | ||
d62a17ae | 297 | install_element(VIEW_NODE, &show_memory_cmd); |
298 | install_element(VIEW_NODE, &show_modules_cmd); | |
f8e6ada8 DS |
299 | |
300 | install_element(CONFIG_NODE, &start_config_cmd); | |
301 | install_element(CONFIG_NODE, &end_config_cmd); | |
718e3744 | 302 | } |
6b0655a2 | 303 | |
41be32bf PJ |
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. | |
311 | */ | |
d62a17ae | 312 | const char *mtype_memstr(char *buf, size_t len, unsigned long bytes) |
41be32bf | 313 | { |
d62a17ae | 314 | unsigned int m, k; |
315 | ||
316 | /* easy cases */ | |
317 | if (!bytes) | |
318 | return "0 bytes"; | |
319 | if (bytes == 1) | |
320 | return "1 byte"; | |
321 | ||
322 | /* | |
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 | |
328 | */ | |
329 | if (bytes > 0x7fffffff) | |
330 | return "> 2GB"; | |
331 | ||
332 | m = bytes >> 20; | |
333 | k = bytes >> 10; | |
334 | ||
335 | if (m > 10) { | |
336 | if (bytes & (1 << 19)) | |
337 | m++; | |
338 | snprintf(buf, len, "%d MiB", m); | |
339 | } else if (k > 10) { | |
340 | if (bytes & (1 << 9)) | |
341 | k++; | |
342 | snprintf(buf, len, "%d KiB", k); | |
343 | } else | |
344 | snprintf(buf, len, "%ld bytes", bytes); | |
345 | ||
346 | return buf; | |
41be32bf | 347 | } |