blob: bef0997f79fbfaffe5fee56a411cd91f3d5f252e [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Memory management routine
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra 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
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "log.h"
26#include "memory.h"
27
28void alloc_inc (int);
29void alloc_dec (int);
30
31struct message mstr [] =
32{
33 { MTYPE_THREAD, "thread" },
34 { MTYPE_THREAD_MASTER, "thread_master" },
35 { MTYPE_VECTOR, "vector" },
36 { MTYPE_VECTOR_INDEX, "vector_index" },
37 { MTYPE_IF, "interface" },
38 { 0, NULL },
39};
40
41/* Fatal memory allocation error occured. */
42static void
43zerror (const char *fname, int type, size_t size)
44{
45 fprintf (stderr, "%s : can't allocate memory for `%s' size %d\n",
46 fname, lookup (mstr, type), (int) size);
47 exit (1);
48}
49
50/* Memory allocation. */
51void *
52zmalloc (int type, size_t size)
53{
54 void *memory;
55
56 memory = malloc (size);
57
58 if (memory == NULL)
59 zerror ("malloc", type, size);
60
61 alloc_inc (type);
62
63 return memory;
64}
65
66/* Memory allocation with num * size with cleared. */
67void *
68zcalloc (int type, size_t size)
69{
70 void *memory;
71
72 memory = calloc (1, size);
73
74 if (memory == NULL)
75 zerror ("calloc", type, size);
76
77 alloc_inc (type);
78
79 return memory;
80}
81
82/* Memory reallocation. */
83void *
84zrealloc (int type, void *ptr, size_t size)
85{
86 void *memory;
87
88 memory = realloc (ptr, size);
89 if (memory == NULL)
90 zerror ("realloc", type, size);
91 return memory;
92}
93
94/* Memory free. */
95void
96zfree (int type, void *ptr)
97{
98 alloc_dec (type);
99 free (ptr);
100}
101
102/* String duplication. */
103char *
hassob04c6992004-10-04 19:10:31 +0000104zstrdup (int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000105{
106 void *dup;
107
108 dup = strdup (str);
109 if (dup == NULL)
110 zerror ("strdup", type, strlen (str));
111 alloc_inc (type);
112 return dup;
113}
114
115#ifdef MEMORY_LOG
116struct
117{
hassob04c6992004-10-04 19:10:31 +0000118 const char *name;
paul718e3742002-12-13 20:15:29 +0000119 unsigned long alloc;
120 unsigned long t_malloc;
121 unsigned long c_malloc;
122 unsigned long t_calloc;
123 unsigned long c_calloc;
124 unsigned long t_realloc;
125 unsigned long t_free;
126 unsigned long c_strdup;
127} mstat [MTYPE_MAX];
128
129void
130mtype_log (char *func, void *memory, const char *file, int line, int type)
131{
132 zlog_info ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
133}
134
135void *
136mtype_zmalloc (const char *file, int line, int type, size_t size)
137{
138 void *memory;
139
140 mstat[type].c_malloc++;
141 mstat[type].t_malloc++;
142
143 memory = zmalloc (type, size);
144 mtype_log ("zmalloc", memory, file, line, type);
145
146 return memory;
147}
148
149void *
150mtype_zcalloc (const char *file, int line, int type, size_t size)
151{
152 void *memory;
153
154 mstat[type].c_calloc++;
155 mstat[type].t_calloc++;
156
157 memory = zcalloc (type, size);
158 mtype_log ("xcalloc", memory, file, line, type);
159
160 return memory;
161}
162
163void *
164mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
165{
166 void *memory;
167
168 /* Realloc need before allocated pointer. */
169 mstat[type].t_realloc++;
170
171 memory = zrealloc (type, ptr, size);
172
173 mtype_log ("xrealloc", memory, file, line, type);
174
175 return memory;
176}
177
178/* Important function. */
179void
180mtype_zfree (const char *file, int line, int type, void *ptr)
181{
182 mstat[type].t_free++;
183
184 mtype_log ("xfree", ptr, file, line, type);
185
186 zfree (type, ptr);
187}
188
189char *
hassob04c6992004-10-04 19:10:31 +0000190mtype_zstrdup (const char *file, int line, int type, const char *str)
paul718e3742002-12-13 20:15:29 +0000191{
192 char *memory;
193
194 mstat[type].c_strdup++;
195
196 memory = zstrdup (type, str);
197
198 mtype_log ("xstrdup", memory, file, line, type);
199
200 return memory;
201}
202#else
203struct
204{
205 char *name;
206 unsigned long alloc;
207} mstat [MTYPE_MAX];
208#endif /* MTPYE_LOG */
209
210/* Increment allocation counter. */
211void
212alloc_inc (int type)
213{
214 mstat[type].alloc++;
215}
216
217/* Decrement allocation counter. */
218void
219alloc_dec (int type)
220{
221 mstat[type].alloc--;
222}
223
224/* Looking up memory status from vty interface. */
225#include "vector.h"
226#include "vty.h"
227#include "command.h"
228
229/* For pretty printng of memory allocate information. */
230struct memory_list
231{
232 int index;
hassob04c6992004-10-04 19:10:31 +0000233 const char *format;
paul718e3742002-12-13 20:15:29 +0000234};
235
236struct memory_list memory_list_lib[] =
237{
238 { MTYPE_TMP, "Temporary memory" },
239 { MTYPE_ROUTE_TABLE, "Route table " },
240 { MTYPE_ROUTE_NODE, "Route node " },
241 { MTYPE_RIB, "RIB " },
paul9035efa2004-10-10 11:56:56 +0000242 { MTYPE_DISTRIBUTE, "Distribute list " },
243 { MTYPE_DISTRIBUTE_IFNAME, "Dist-list ifname" },
paul718e3742002-12-13 20:15:29 +0000244 { MTYPE_NEXTHOP, "Nexthop " },
245 { MTYPE_LINK_LIST, "Link List " },
246 { MTYPE_LINK_NODE, "Link Node " },
247 { MTYPE_HASH, "Hash " },
248 { MTYPE_HASH_BACKET, "Hash Bucket " },
249 { MTYPE_ACCESS_LIST, "Access List " },
250 { MTYPE_ACCESS_LIST_STR, "Access List Str " },
251 { MTYPE_ACCESS_FILTER, "Access Filter " },
252 { MTYPE_PREFIX_LIST, "Prefix List " },
253 { MTYPE_PREFIX_LIST_STR, "Prefix List Str " },
254 { MTYPE_PREFIX_LIST_ENTRY, "Prefix List Entry "},
255 { MTYPE_ROUTE_MAP, "Route map " },
256 { MTYPE_ROUTE_MAP_NAME, "Route map name " },
257 { MTYPE_ROUTE_MAP_INDEX, "Route map index " },
258 { MTYPE_ROUTE_MAP_RULE, "Route map rule " },
259 { MTYPE_ROUTE_MAP_RULE_STR, "Route map rule str" },
hassoa94434b2003-05-25 17:10:12 +0000260 { MTYPE_ROUTE_MAP_COMPILED, "Route map compiled" },
paul718e3742002-12-13 20:15:29 +0000261 { MTYPE_DESC, "Command desc " },
262 { MTYPE_BUFFER, "Buffer " },
263 { MTYPE_BUFFER_DATA, "Buffer data " },
264 { MTYPE_STREAM, "Stream " },
265 { MTYPE_KEYCHAIN, "Key chain " },
266 { MTYPE_KEY, "Key " },
267 { MTYPE_VTY, "VTY " },
268 { -1, NULL }
269};
270
271struct memory_list memory_list_bgp[] =
272{
273 { MTYPE_BGP_PEER, "BGP peer" },
274 { MTYPE_ATTR, "BGP attribute" },
275 { MTYPE_AS_PATH, "BGP aspath" },
276 { MTYPE_AS_SEG, "BGP aspath seg" },
277 { MTYPE_AS_STR, "BGP aspath str" },
278 { 0, NULL },
279 { MTYPE_BGP_TABLE, "BGP table" },
280 { MTYPE_BGP_NODE, "BGP node" },
281 { MTYPE_BGP_ADVERTISE_ATTR, "BGP adv attr" },
282 { MTYPE_BGP_ADVERTISE, "BGP adv" },
283 { MTYPE_BGP_ADJ_IN, "BGP adj in" },
284 { MTYPE_BGP_ADJ_OUT, "BGP adj out" },
285 { 0, NULL },
286 { MTYPE_AS_LIST, "BGP AS list" },
287 { MTYPE_AS_FILTER, "BGP AS filter" },
288 { MTYPE_AS_FILTER_STR, "BGP AS filter str" },
289 { 0, NULL },
290 { MTYPE_COMMUNITY, "community" },
291 { MTYPE_COMMUNITY_VAL, "community val" },
292 { MTYPE_COMMUNITY_STR, "community str" },
293 { 0, NULL },
294 { MTYPE_ECOMMUNITY, "extcommunity" },
295 { MTYPE_ECOMMUNITY_VAL, "extcommunity val" },
296 { MTYPE_ECOMMUNITY_STR, "extcommunity str" },
297 { 0, NULL },
298 { MTYPE_COMMUNITY_LIST, "community-list" },
299 { MTYPE_COMMUNITY_LIST_NAME, "community-list name" },
300 { MTYPE_COMMUNITY_LIST_ENTRY, "community-list entry" },
301 { MTYPE_COMMUNITY_LIST_CONFIG, "community-list config" },
302 { 0, NULL },
303 { MTYPE_CLUSTER, "Cluster list" },
304 { MTYPE_CLUSTER_VAL, "Cluster list val" },
305 { 0, NULL },
306 { MTYPE_TRANSIT, "BGP transit attr" },
307 { MTYPE_TRANSIT_VAL, "BGP transit val" },
308 { 0, NULL },
309 { MTYPE_BGP_DISTANCE, "BGP distance" },
310 { MTYPE_BGP_NEXTHOP_CACHE, "BGP nexthop" },
311 { MTYPE_BGP_CONFED_LIST, "BGP confed list" },
312 { MTYPE_PEER_UPDATE_SOURCE, "peer update if" },
313 { MTYPE_BGP_DAMP_INFO, "Dampening info" },
314 { MTYPE_BGP_REGEXP, "BGP regexp" },
315 { -1, NULL }
316};
317
318struct memory_list memory_list_rip[] =
319{
320 { MTYPE_RIP, "RIP structure " },
321 { MTYPE_RIP_INFO, "RIP route info " },
322 { MTYPE_RIP_INTERFACE, "RIP interface " },
323 { MTYPE_RIP_PEER, "RIP peer " },
324 { MTYPE_RIP_OFFSET_LIST, "RIP offset list " },
325 { MTYPE_RIP_DISTANCE, "RIP distance " },
326 { -1, NULL }
327};
328
hassoa94434b2003-05-25 17:10:12 +0000329struct memory_list memory_list_ripng[] =
330{
331 { MTYPE_RIPNG, "RIPng structure " },
332 { MTYPE_RIPNG_ROUTE, "RIPng route info" },
333 { MTYPE_RIPNG_AGGREGATE, "RIPng aggregate " },
334 { MTYPE_RIPNG_PEER, "RIPng peer " },
335 { MTYPE_RIPNG_OFFSET_LIST, "RIPng offset lst" },
336 { MTYPE_RIPNG_RTE_DATA, "RIPng rte data " },
337 { -1, NULL }
338};
339
paul718e3742002-12-13 20:15:29 +0000340struct memory_list memory_list_ospf[] =
341{
342 { MTYPE_OSPF_TOP, "OSPF top " },
343 { MTYPE_OSPF_AREA, "OSPF area " },
344 { MTYPE_OSPF_AREA_RANGE, "OSPF area range " },
345 { MTYPE_OSPF_NETWORK, "OSPF network " },
346#ifdef NBMA_ENABLE
347 { MTYPE_OSPF_NEIGHBOR_STATIC,"OSPF static nbr " },
348#endif /* NBMA_ENABLE */
349 { MTYPE_OSPF_IF, "OSPF interface " },
350 { MTYPE_OSPF_NEIGHBOR, "OSPF neighbor " },
351 { MTYPE_OSPF_ROUTE, "OSPF route " },
352 { MTYPE_OSPF_TMP, "OSPF tmp mem " },
353 { MTYPE_OSPF_LSA, "OSPF LSA " },
354 { MTYPE_OSPF_LSA_DATA, "OSPF LSA data " },
355 { MTYPE_OSPF_LSDB, "OSPF LSDB " },
356 { MTYPE_OSPF_PACKET, "OSPF packet " },
357 { MTYPE_OSPF_FIFO, "OSPF FIFO queue " },
358 { MTYPE_OSPF_VERTEX, "OSPF vertex " },
359 { MTYPE_OSPF_NEXTHOP, "OSPF nexthop " },
360 { MTYPE_OSPF_PATH, "OSPF path " },
361 { MTYPE_OSPF_VL_DATA, "OSPF VL data " },
362 { MTYPE_OSPF_CRYPT_KEY, "OSPF crypt key " },
363 { MTYPE_OSPF_EXTERNAL_INFO, "OSPF ext. info " },
364 { MTYPE_OSPF_DISTANCE, "OSPF distance " },
365 { MTYPE_OSPF_IF_INFO, "OSPF if info " },
366 { MTYPE_OSPF_IF_PARAMS, "OSPF if params " },
367 { -1, NULL },
368};
369
370struct memory_list memory_list_ospf6[] =
371{
372 { MTYPE_OSPF6_TOP, "OSPF6 top " },
373 { MTYPE_OSPF6_AREA, "OSPF6 area " },
374 { MTYPE_OSPF6_IF, "OSPF6 interface " },
375 { MTYPE_OSPF6_NEIGHBOR, "OSPF6 neighbor " },
376 { MTYPE_OSPF6_ROUTE, "OSPF6 route " },
377 { MTYPE_OSPF6_PREFIX, "OSPF6 prefix " },
378 { MTYPE_OSPF6_MESSAGE, "OSPF6 message " },
379 { MTYPE_OSPF6_LSA, "OSPF6 LSA " },
380 { MTYPE_OSPF6_LSA_SUMMARY, "OSPF6 LSA summary " },
381 { MTYPE_OSPF6_LSDB, "OSPF6 LSA database" },
382 { MTYPE_OSPF6_VERTEX, "OSPF6 vertex " },
383 { MTYPE_OSPF6_SPFTREE, "OSPF6 SPF tree " },
384 { MTYPE_OSPF6_NEXTHOP, "OSPF6 nexthop " },
385 { MTYPE_OSPF6_EXTERNAL_INFO,"OSPF6 ext. info " },
386 { MTYPE_OSPF6_OTHER, "OSPF6 other " },
387 { -1, NULL },
388};
389
jardin9e867fe2003-12-23 08:56:18 +0000390struct memory_list memory_list_isis[] =
391{
392 { MTYPE_ISIS, "ISIS " },
393 { MTYPE_ISIS_TMP, "ISIS TMP " },
394 { MTYPE_ISIS_CIRCUIT, "ISIS circuit " },
395 { MTYPE_ISIS_LSP, "ISIS LSP " },
396 { MTYPE_ISIS_ADJACENCY, "ISIS adjacency " },
397 { MTYPE_ISIS_AREA, "ISIS area " },
398 { MTYPE_ISIS_AREA_ADDR, "ISIS area address " },
399 { MTYPE_ISIS_TLV, "ISIS TLV " },
400 { MTYPE_ISIS_DYNHN, "ISIS dyn hostname " },
401 { MTYPE_ISIS_SPFTREE, "ISIS SPFtree " },
402 { MTYPE_ISIS_VERTEX, "ISIS vertex " },
403 { MTYPE_ISIS_ROUTE_INFO, "ISIS route info " },
404 { MTYPE_ISIS_NEXTHOP, "ISIS nexthop " },
405 { MTYPE_ISIS_NEXTHOP6, "ISIS nexthop6 " },
406 { -1, NULL },
407};
408
paul718e3742002-12-13 20:15:29 +0000409struct memory_list memory_list_separator[] =
410{
411 { 0, NULL},
412 {-1, NULL}
413};
414
415void
416show_memory_vty (struct vty *vty, struct memory_list *list)
417{
418 struct memory_list *m;
419
420 for (m = list; m->index >= 0; m++)
421 if (m->index == 0)
422 vty_out (vty, "-----------------------------\r\n");
423 else
424 vty_out (vty, "%-22s: %5ld\r\n", m->format, mstat[m->index].alloc);
425}
426
427DEFUN (show_memory_all,
428 show_memory_all_cmd,
429 "show memory all",
430 "Show running system information\n"
431 "Memory statistics\n"
432 "All memory statistics\n")
433{
434 show_memory_vty (vty, memory_list_lib);
435 show_memory_vty (vty, memory_list_separator);
436 show_memory_vty (vty, memory_list_rip);
437 show_memory_vty (vty, memory_list_separator);
hassoa94434b2003-05-25 17:10:12 +0000438 show_memory_vty (vty, memory_list_ripng);
439 show_memory_vty (vty, memory_list_separator);
paul718e3742002-12-13 20:15:29 +0000440 show_memory_vty (vty, memory_list_ospf);
441 show_memory_vty (vty, memory_list_separator);
442 show_memory_vty (vty, memory_list_ospf6);
443 show_memory_vty (vty, memory_list_separator);
jardin9e867fe2003-12-23 08:56:18 +0000444 show_memory_vty (vty, memory_list_isis);
445 show_memory_vty (vty, memory_list_separator);
paul718e3742002-12-13 20:15:29 +0000446 show_memory_vty (vty, memory_list_bgp);
447
448 return CMD_SUCCESS;
449}
450
451ALIAS (show_memory_all,
452 show_memory_cmd,
453 "show memory",
454 "Show running system information\n"
455 "Memory statistics\n")
456
457DEFUN (show_memory_lib,
458 show_memory_lib_cmd,
459 "show memory lib",
460 SHOW_STR
461 "Memory statistics\n"
462 "Library memory\n")
463{
464 show_memory_vty (vty, memory_list_lib);
465 return CMD_SUCCESS;
466}
467
468DEFUN (show_memory_rip,
469 show_memory_rip_cmd,
470 "show memory rip",
471 SHOW_STR
472 "Memory statistics\n"
473 "RIP memory\n")
474{
475 show_memory_vty (vty, memory_list_rip);
476 return CMD_SUCCESS;
477}
478
hassoa94434b2003-05-25 17:10:12 +0000479DEFUN (show_memory_ripng,
480 show_memory_ripng_cmd,
481 "show memory ripng",
482 SHOW_STR
483 "Memory statistics\n"
484 "RIPng memory\n")
485{
486 show_memory_vty (vty, memory_list_ripng);
487 return CMD_SUCCESS;
488}
489
paul718e3742002-12-13 20:15:29 +0000490DEFUN (show_memory_bgp,
491 show_memory_bgp_cmd,
492 "show memory bgp",
493 SHOW_STR
494 "Memory statistics\n"
495 "BGP memory\n")
496{
497 show_memory_vty (vty, memory_list_bgp);
498 return CMD_SUCCESS;
499}
500
501DEFUN (show_memory_ospf,
502 show_memory_ospf_cmd,
503 "show memory ospf",
504 SHOW_STR
505 "Memory statistics\n"
506 "OSPF memory\n")
507{
508 show_memory_vty (vty, memory_list_ospf);
509 return CMD_SUCCESS;
510}
511
512DEFUN (show_memory_ospf6,
513 show_memory_ospf6_cmd,
514 "show memory ospf6",
515 SHOW_STR
516 "Memory statistics\n"
517 "OSPF6 memory\n")
518{
519 show_memory_vty (vty, memory_list_ospf6);
520 return CMD_SUCCESS;
521}
522
jardin9e867fe2003-12-23 08:56:18 +0000523DEFUN (show_memory_isis,
524 show_memory_isis_cmd,
525 "show memory isis",
526 SHOW_STR
527 "Memory statistics\n"
528 "ISIS memory\n")
529{
530 show_memory_vty (vty, memory_list_isis);
531 return CMD_SUCCESS;
532}
533
paul718e3742002-12-13 20:15:29 +0000534void
535memory_init ()
536{
537 install_element (VIEW_NODE, &show_memory_cmd);
538 install_element (VIEW_NODE, &show_memory_all_cmd);
539 install_element (VIEW_NODE, &show_memory_lib_cmd);
540 install_element (VIEW_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000541 install_element (VIEW_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000542 install_element (VIEW_NODE, &show_memory_bgp_cmd);
543 install_element (VIEW_NODE, &show_memory_ospf_cmd);
544 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000545 install_element (VIEW_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000546
547 install_element (ENABLE_NODE, &show_memory_cmd);
548 install_element (ENABLE_NODE, &show_memory_all_cmd);
549 install_element (ENABLE_NODE, &show_memory_lib_cmd);
550 install_element (ENABLE_NODE, &show_memory_rip_cmd);
hassoa94434b2003-05-25 17:10:12 +0000551 install_element (ENABLE_NODE, &show_memory_ripng_cmd);
paul718e3742002-12-13 20:15:29 +0000552 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
553 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
554 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
jardin9e867fe2003-12-23 08:56:18 +0000555 install_element (ENABLE_NODE, &show_memory_isis_cmd);
paul718e3742002-12-13 20:15:29 +0000556}