1/*
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
28#ifdef FOX_RIP_DEBUG
29void alloc_inc (int);
30void alloc_dec (int);
31#endif /* FOX_RIP_DEBUG */
32
33
34struct message mstr [] =
35{
36  { MTYPE_THREAD, "thread" },
37  { MTYPE_THREAD_MASTER, "thread_master" },
38  { MTYPE_VECTOR, "vector" },
39  { MTYPE_VECTOR_INDEX, "vector_index" },
40  { MTYPE_IF, "interface" },
41  { 0, NULL },
42};
43
44
45#ifdef FOX_RIP_DEBUG
46/* Fatal memory allocation error occured. */
47static void
48zerror (const char *fname, int type, size_t size)
49{
50  fprintf (stderr, "%s : can't allocate memory for `%s' size %d\n",
51	   fname, lookup (mstr, type), (int) size);
52  exit (1);
53}
54#endif /* FOX_RIP_DEBUG */
55
56/* Memory allocation. */
57void *
58zmalloc (int type, size_t size)
59{
60  void *memory;
61
62  memory = malloc (size);
63#ifdef FOX_RIP_DEBUG
64  if (memory == NULL)
65    zerror ("malloc", type, size);
66
67  alloc_inc (type);
68#endif /* FOX_RIP_DEBUG */
69  return memory;
70}
71
72/* Memory allocation with num * size with cleared. */
73void *
74zcalloc (int type, size_t size)
75{
76  void *memory;
77
78  memory = calloc (1, size);
79#ifdef FOX_RIP_DEBUG
80  if (memory == NULL)
81    zerror ("calloc", type, size);
82
83  alloc_inc (type);
84#endif /* FOX_RIP_DEBUG */
85
86  return memory;
87}
88
89/* Memory reallocation. */
90void *
91zrealloc (int type, void *ptr, size_t size)
92{
93  void *memory;
94
95  memory = realloc (ptr, size);
96#ifdef FOX_RIP_DEBUG
97  if (memory == NULL)
98    zerror ("realloc", type, size);
99#endif /* FOX_RIP_DEBUG */
100  return memory;
101}
102
103/* Memory free. */
104void
105zfree (int type, void *ptr)
106{
107#ifdef FOX_RIP_DEBUG
108  alloc_dec (type);
109#endif /* FOX_RIP_DEBUG */
110  free (ptr);
111}
112
113/* String duplication. */
114char *
115zstrdup (int type, char *str)
116{
117  void *dup;
118
119  dup = strdup (str);
120#ifdef FOX_RIP_DEBUG
121  if (dup == NULL)
122    zerror ("strdup", type, strlen (str));
123  alloc_inc (type);
124#endif /* FOX_RIP_DEBUG */
125  return dup;
126}
127
128#ifdef MEMORY_LOG
129struct
130{
131  char *name;
132  unsigned long alloc;
133  unsigned long t_malloc;
134  unsigned long c_malloc;
135  unsigned long t_calloc;
136  unsigned long c_calloc;
137  unsigned long t_realloc;
138  unsigned long t_free;
139  unsigned long c_strdup;
140} mstat [MTYPE_MAX];
141
142void
143mtype_log (char *func, void *memory, const char *file, int line, int type)
144{
145#ifdef FOX_RIP_DEBUG
146  zlog_info ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
147#endif /* FOX_RIP_DEBUG */
148}
149
150void *
151mtype_zmalloc (const char *file, int line, int type, size_t size)
152{
153  void *memory;
154
155  mstat[type].c_malloc++;
156  mstat[type].t_malloc++;
157
158  memory = zmalloc (type, size);
159  mtype_log ("zmalloc", memory, file, line, type);
160
161  return memory;
162}
163
164void *
165mtype_zcalloc (const char *file, int line, int type, size_t size)
166{
167  void *memory;
168
169  mstat[type].c_calloc++;
170  mstat[type].t_calloc++;
171
172  memory = zcalloc (type, size);
173  mtype_log ("xcalloc", memory, file, line, type);
174
175  return memory;
176}
177
178void *
179mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
180{
181  void *memory;
182
183  /* Realloc need before allocated pointer. */
184  mstat[type].t_realloc++;
185
186  memory = zrealloc (type, ptr, size);
187
188  mtype_log ("xrealloc", memory, file, line, type);
189
190  return memory;
191}
192
193/* Important function. */
194void
195mtype_zfree (const char *file, int line, int type, void *ptr)
196{
197  mstat[type].t_free++;
198
199  mtype_log ("xfree", ptr, file, line, type);
200
201  zfree (type, ptr);
202}
203
204char *
205mtype_zstrdup (const char *file, int line, int type, char *str)
206{
207  char *memory;
208
209  mstat[type].c_strdup++;
210
211  memory = zstrdup (type, str);
212
213  mtype_log ("xstrdup", memory, file, line, type);
214
215  return memory;
216}
217#else
218struct
219{
220  char *name;
221  unsigned long alloc;
222} mstat [MTYPE_MAX];
223#endif /* MTPYE_LOG */
224
225#ifdef FOX_RIP_DEBUG
226/* Increment allocation counter. */
227void
228alloc_inc (int type)
229{
230  mstat[type].alloc++;
231}
232
233/* Decrement allocation counter. */
234void
235alloc_dec (int type)
236{
237  mstat[type].alloc--;
238}
239#endif /* FOX_RIP_DEBUG */
240
241#ifdef FOX_CMD_SUPPORT
242
243/* Looking up memory status from vty interface. */
244#include "vector.h"
245#include "vty.h"
246#include "command.h"
247
248/* For pretty printng of memory allocate information. */
249struct memory_list
250{
251  int index;
252  char *format;
253};
254
255struct memory_list memory_list_lib[] =
256{
257  { MTYPE_TMP,                "Temporary memory" },
258  { MTYPE_ROUTE_TABLE,        "Route table     " },
259  { MTYPE_ROUTE_NODE,         "Route node      " },
260  { MTYPE_RIB,                "RIB             " },
261  { MTYPE_NEXTHOP,            "Nexthop         " },
262  { MTYPE_LINK_LIST,          "Link List       " },
263  { MTYPE_LINK_NODE,          "Link Node       " },
264  { MTYPE_HASH,               "Hash            " },
265  { MTYPE_HASH_BACKET,        "Hash Bucket     " },
266  { MTYPE_ACCESS_LIST,        "Access List     " },
267  { MTYPE_ACCESS_LIST_STR,    "Access List Str " },
268  { MTYPE_ACCESS_FILTER,      "Access Filter   " },
269  { MTYPE_PREFIX_LIST,        "Prefix List     " },
270  { MTYPE_PREFIX_LIST_STR,    "Prefix List Str " },
271  { MTYPE_PREFIX_LIST_ENTRY,  "Prefix List Entry "},
272  { MTYPE_ROUTE_MAP,          "Route map       " },
273  { MTYPE_ROUTE_MAP_NAME,     "Route map name  " },
274  { MTYPE_ROUTE_MAP_INDEX,    "Route map index " },
275  { MTYPE_ROUTE_MAP_RULE,     "Route map rule  " },
276  { MTYPE_ROUTE_MAP_RULE_STR, "Route map rule str" },
277  { MTYPE_DESC,               "Command desc    " },
278  { MTYPE_BUFFER,             "Buffer          " },
279  { MTYPE_BUFFER_DATA,        "Buffer data     " },
280  { MTYPE_STREAM,             "Stream          " },
281  { MTYPE_KEYCHAIN,           "Key chain       " },
282  { MTYPE_KEY,                "Key             " },
283  { MTYPE_VTY,                "VTY             " },
284  { -1, NULL }
285};
286
287struct memory_list memory_list_bgp[] =
288{
289  { MTYPE_BGP_PEER,               "BGP peer" },
290  { MTYPE_ATTR,                   "BGP attribute" },
291  { MTYPE_AS_PATH,                "BGP aspath" },
292  { MTYPE_AS_SEG,                 "BGP aspath seg" },
293  { MTYPE_AS_STR,                 "BGP aspath str" },
294  { 0, NULL },
295  { MTYPE_BGP_TABLE,              "BGP table" },
296  { MTYPE_BGP_NODE,               "BGP node" },
297  { MTYPE_BGP_ADVERTISE_ATTR,     "BGP adv attr" },
298  { MTYPE_BGP_ADVERTISE,          "BGP adv" },
299  { MTYPE_BGP_ADJ_IN,             "BGP adj in" },
300  { MTYPE_BGP_ADJ_OUT,            "BGP adj out" },
301  { 0, NULL },
302  { MTYPE_AS_LIST,                "BGP AS list" },
303  { MTYPE_AS_FILTER,              "BGP AS filter" },
304  { MTYPE_AS_FILTER_STR,          "BGP AS filter str" },
305  { 0, NULL },
306  { MTYPE_COMMUNITY,              "community" },
307  { MTYPE_COMMUNITY_VAL,          "community val" },
308  { MTYPE_COMMUNITY_STR,          "community str" },
309  { 0, NULL },
310  { MTYPE_ECOMMUNITY,             "extcommunity" },
311  { MTYPE_ECOMMUNITY_VAL,         "extcommunity val" },
312  { MTYPE_ECOMMUNITY_STR,         "extcommunity str" },
313  { 0, NULL },
314  { MTYPE_COMMUNITY_LIST,         "community-list" },
315  { MTYPE_COMMUNITY_LIST_NAME,    "community-list name" },
316  { MTYPE_COMMUNITY_LIST_ENTRY,   "community-list entry" },
317  { MTYPE_COMMUNITY_LIST_CONFIG,  "community-list config" },
318  { 0, NULL },
319  { MTYPE_CLUSTER,                "Cluster list" },
320  { MTYPE_CLUSTER_VAL,            "Cluster list val" },
321  { 0, NULL },
322  { MTYPE_TRANSIT,                "BGP transit attr" },
323  { MTYPE_TRANSIT_VAL,            "BGP transit val" },
324  { 0, NULL },
325  { MTYPE_BGP_DISTANCE,           "BGP distance" },
326  { MTYPE_BGP_NEXTHOP_CACHE,      "BGP nexthop" },
327  { MTYPE_BGP_CONFED_LIST,        "BGP confed list" },
328  { MTYPE_PEER_UPDATE_SOURCE,     "peer update if" },
329  { MTYPE_BGP_DAMP_INFO,          "Dampening info" },
330  { MTYPE_BGP_REGEXP,             "BGP regexp" },
331  { -1, NULL }
332};
333
334struct memory_list memory_list_rip[] =
335{
336  { MTYPE_RIP,                "RIP structure   " },
337  { MTYPE_RIP_INFO,           "RIP route info  " },
338  { MTYPE_RIP_INTERFACE,      "RIP interface   " },
339  { MTYPE_RIP_PEER,           "RIP peer        " },
340  { MTYPE_RIP_OFFSET_LIST,    "RIP offset list " },
341  { MTYPE_RIP_DISTANCE,       "RIP distance    " },
342  { -1, NULL }
343};
344
345struct memory_list memory_list_ospf[] =
346{
347  { MTYPE_OSPF_TOP,           "OSPF top        " },
348  { MTYPE_OSPF_AREA,          "OSPF area       " },
349  { MTYPE_OSPF_AREA_RANGE,    "OSPF area range " },
350  { MTYPE_OSPF_NETWORK,       "OSPF network    " },
351#ifdef NBMA_ENABLE
352  { MTYPE_OSPF_NEIGHBOR_STATIC,"OSPF static nbr " },
353#endif  /* NBMA_ENABLE */
354  { MTYPE_OSPF_IF,            "OSPF interface  " },
355  { MTYPE_OSPF_NEIGHBOR,      "OSPF neighbor   " },
356  { MTYPE_OSPF_ROUTE,         "OSPF route      " },
357  { MTYPE_OSPF_TMP,           "OSPF tmp mem    " },
358  { MTYPE_OSPF_LSA,           "OSPF LSA        " },
359  { MTYPE_OSPF_LSA_DATA,      "OSPF LSA data   " },
360  { MTYPE_OSPF_LSDB,          "OSPF LSDB       " },
361  { MTYPE_OSPF_PACKET,        "OSPF packet     " },
362  { MTYPE_OSPF_FIFO,          "OSPF FIFO queue " },
363  { MTYPE_OSPF_VERTEX,        "OSPF vertex     " },
364  { MTYPE_OSPF_NEXTHOP,       "OSPF nexthop    " },
365  { MTYPE_OSPF_PATH,	      "OSPF path       " },
366  { MTYPE_OSPF_VL_DATA,       "OSPF VL data    " },
367  { MTYPE_OSPF_CRYPT_KEY,     "OSPF crypt key  " },
368  { MTYPE_OSPF_EXTERNAL_INFO, "OSPF ext. info  " },
369  { MTYPE_OSPF_DISTANCE,      "OSPF distance   " },
370  { MTYPE_OSPF_IF_INFO,       "OSPF if info    " },
371  { MTYPE_OSPF_IF_PARAMS,     "OSPF if params  " },
372  { -1, NULL },
373};
374
375struct memory_list memory_list_ospf6[] =
376{
377  { MTYPE_OSPF6_TOP,          "OSPF6 top         " },
378  { MTYPE_OSPF6_AREA,         "OSPF6 area        " },
379  { MTYPE_OSPF6_IF,           "OSPF6 interface   " },
380  { MTYPE_OSPF6_NEIGHBOR,     "OSPF6 neighbor    " },
381  { MTYPE_OSPF6_ROUTE,        "OSPF6 route       " },
382  { MTYPE_OSPF6_PREFIX,       "OSPF6 prefix      " },
383  { MTYPE_OSPF6_MESSAGE,      "OSPF6 message     " },
384  { MTYPE_OSPF6_LSA,          "OSPF6 LSA         " },
385  { MTYPE_OSPF6_LSA_SUMMARY,  "OSPF6 LSA summary " },
386  { MTYPE_OSPF6_LSDB,         "OSPF6 LSA database" },
387  { MTYPE_OSPF6_VERTEX,       "OSPF6 vertex      " },
388  { MTYPE_OSPF6_SPFTREE,      "OSPF6 SPF tree    " },
389  { MTYPE_OSPF6_NEXTHOP,      "OSPF6 nexthop     " },
390  { MTYPE_OSPF6_EXTERNAL_INFO,"OSPF6 ext. info   " },
391  { MTYPE_OSPF6_OTHER,        "OSPF6 other       " },
392  { -1, NULL },
393};
394
395struct memory_list memory_list_separator[] =
396{
397  { 0, NULL},
398  {-1, NULL}
399};
400
401void
402show_memory_vty (struct vty *vty, struct memory_list *list)
403{
404  struct memory_list *m;
405
406  for (m = list; m->index >= 0; m++)
407    if (m->index == 0)
408      vty_out (vty, "-----------------------------\r\n");
409    else
410      vty_out (vty, "%-22s: %5ld\r\n", m->format, mstat[m->index].alloc);
411}
412
413DEFUN (show_memory_all,
414       show_memory_all_cmd,
415       "show memory all",
416       "Show running system information\n"
417       "Memory statistics\n"
418       "All memory statistics\n")
419{
420  show_memory_vty (vty, memory_list_lib);
421  show_memory_vty (vty, memory_list_separator);
422  show_memory_vty (vty, memory_list_rip);
423  show_memory_vty (vty, memory_list_separator);
424  show_memory_vty (vty, memory_list_ospf);
425  show_memory_vty (vty, memory_list_separator);
426  show_memory_vty (vty, memory_list_ospf6);
427  show_memory_vty (vty, memory_list_separator);
428  show_memory_vty (vty, memory_list_bgp);
429
430  return CMD_SUCCESS;
431}
432
433ALIAS (show_memory_all,
434       show_memory_cmd,
435       "show memory",
436       "Show running system information\n"
437       "Memory statistics\n")
438
439DEFUN (show_memory_lib,
440       show_memory_lib_cmd,
441       "show memory lib",
442       SHOW_STR
443       "Memory statistics\n"
444       "Library memory\n")
445{
446  show_memory_vty (vty, memory_list_lib);
447  return CMD_SUCCESS;
448}
449
450DEFUN (show_memory_rip,
451       show_memory_rip_cmd,
452       "show memory rip",
453       SHOW_STR
454       "Memory statistics\n"
455       "RIP memory\n")
456{
457  show_memory_vty (vty, memory_list_rip);
458  return CMD_SUCCESS;
459}
460
461DEFUN (show_memory_bgp,
462       show_memory_bgp_cmd,
463       "show memory bgp",
464       SHOW_STR
465       "Memory statistics\n"
466       "BGP memory\n")
467{
468  show_memory_vty (vty, memory_list_bgp);
469  return CMD_SUCCESS;
470}
471
472DEFUN (show_memory_ospf,
473       show_memory_ospf_cmd,
474       "show memory ospf",
475       SHOW_STR
476       "Memory statistics\n"
477       "OSPF memory\n")
478{
479  show_memory_vty (vty, memory_list_ospf);
480  return CMD_SUCCESS;
481}
482
483DEFUN (show_memory_ospf6,
484       show_memory_ospf6_cmd,
485       "show memory ospf6",
486       SHOW_STR
487       "Memory statistics\n"
488       "OSPF6 memory\n")
489{
490  show_memory_vty (vty, memory_list_ospf6);
491  return CMD_SUCCESS;
492}
493#endif /* FOX_CMD_SUPPORT */
494
495void
496memory_init ()
497{
498#ifdef FOX_CMD_SUPPORT
499  install_element (VIEW_NODE, &show_memory_cmd);
500  install_element (VIEW_NODE, &show_memory_all_cmd);
501  install_element (VIEW_NODE, &show_memory_lib_cmd);
502  install_element (VIEW_NODE, &show_memory_rip_cmd);
503  install_element (VIEW_NODE, &show_memory_bgp_cmd);
504  install_element (VIEW_NODE, &show_memory_ospf_cmd);
505  install_element (VIEW_NODE, &show_memory_ospf6_cmd);
506
507  install_element (ENABLE_NODE, &show_memory_cmd);
508  install_element (ENABLE_NODE, &show_memory_all_cmd);
509  install_element (ENABLE_NODE, &show_memory_lib_cmd);
510  install_element (ENABLE_NODE, &show_memory_rip_cmd);
511  install_element (ENABLE_NODE, &show_memory_bgp_cmd);
512  install_element (ENABLE_NODE, &show_memory_ospf_cmd);
513  install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
514#endif /* FOX_CMD_SUPPORT */
515}
516