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