Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_show.c
1 /* 
2  *------------------------------------------------------------------
3  * cnat_show.c - translation database definitions
4  *
5  * Copyright (c) 2007-2014 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vppinfra/vec.h>
23 #include <vppinfra/bitmap.h>
24 #include <vppinfra/hash.h>
25 #include <vppinfra/pool.h>
26 #include <vppinfra/clib.h>
27
28 #include "cnat_db.h"
29 #include "cnat_config.h"
30 #include "cnat_global.h"
31 #include "cnat_logging.h"
32 #include "spp_ctx.h"
33 #include "spp_timers.h"
34 #include "platform_common.h"
35 #include "cnat_syslog.h"
36 #include "cnat_v4_pptp_alg.h"
37 #include "platform_common.h"
38
39 #ifndef TOBE_PORTED
40 /* The following variable is in cnat_config_msg_handler.c which
41  * is to be ported later.. if required
42  */
43 u32 total_address_pool_allocated = 0;
44 #endif
45
46 #ifndef NO_BULK_LOGGING
47 #define CNAT_MY_VRFMAP_PRINT \
48 PLATFORM_DEBUG_PRINT("i-uidx 0x%x o-uidx 0x%x i-vrfid 0x%x o-vrfid 0x%x\n" \
49        "status %d  del time 0x%x tcp mss 0x%x pm list 0x%x\n" \
50        "bulk size %d\n" \
51        "ip n:1 %d\n" \
52        "NFV9 template index 0x%x\n" \
53        "SYSLOG template index 0x%x\n" \
54        "Netflow Session Logging %d \n" \
55        "Syslog Session Logging %d \n" \
56        "PCP Server 0x%x, Port %u \n", \
57        my_vrfmap->i_vrf, my_vrfmap->o_vrf, my_vrfmap->i_vrf_id, \
58        my_vrfmap->o_vrf_id, my_vrfmap->status, my_vrfmap->delete_time, \
59        my_vrfmap->tcp_mss, my_vrfmap->portmap_list, \
60        BULKSIZE_FROM_VRFMAP(my_vrfmap), \
61        my_vrfmap->ip_n_to_1, \
62        my_vrfmap->nfv9_logging_index, \
63        my_vrfmap->syslog_logging_index,\
64        my_vrfmap->nf_logging_policy, \
65        my_vrfmap->syslog_logging_policy, \
66        my_vrfmap->pcp_server_addr, \
67        my_vrfmap->pcp_server_port); 
68 #else
69 #define CNAT_MY_VRFMAP_PRINT \
70 PLATFORM_DEBUG_PRINT("i-uidx 0x%x o-uidx 0x%x i-vrfid 0x%x o-vrfid 0x%x\n" \
71        "status %d  del time 0x%x tcp mss 0x%x pm list 0x%x\n" \
72        "NFV9 template index 0x%x\n ip n:1 %d\n", \
73        my_vrfmap->i_vrf, my_vrfmap->o_vrf, my_vrfmap->i_vrf_id, \
74        my_vrfmap->o_vrf_id, my_vrfmap->status, my_vrfmap->delete_time, \
75        my_vrfmap->tcp_mss, my_vrfmap->portmap_list, \
76        my_vrfmap->nfv9_logging_index, my_vrfmap->ip_n_to_1);
77 #endif  /* NO_BULK_LOGGING */
78
79 #define CNAT_MY_LOGGING_INFO_PRINT \
80 do { \
81     cnat_syslog_logging_info_t *my_syslog_info = 0; \
82     PLATFORM_DEBUG_PRINT("SYSLOG config: \n"); \
83     pool_foreach (my_syslog_info, cnat_syslog_logging_info_pool, ({ \
84         if (my_syslog_info->i_vrf == my_vrfmap->i_vrf) {  \
85             PLATFORM_DEBUG_PRINT(" \
86             ipv4[0x%x], port[%u], hostname[%s]\n", \
87             my_syslog_info->ipv4_address, my_syslog_info->port, \
88             my_syslog_info->header_hostname); \
89         break; \
90         } \
91     })); \
92 }while (0) \
93 ;
94
95
96 void printf_ipv4(u32 ad)
97 {
98     u8 a __attribute__((unused)), b __attribute__((unused)), 
99         c __attribute__((unused)), d __attribute__((unused));
100
101     a = ad>>24;
102     b = (ad>>16) & 0xFF;
103     c = (ad>>8) & 0xFF;
104     d = (ad>>0) & 0xFF;
105
106     PLATFORM_DEBUG_PRINT("%d.%d.%d.%d", a, b, c, d);
107 }
108 void cnat_main_db_entry_dump (cnat_main_db_entry_t *db)
109 {
110     PLATFORM_DEBUG_PRINT("Main DB entry at %p, index %ld dst_ip %x\n",
111             db, db - cnat_main_db, db->dst_ipv4);
112     /* only dump hash next index if it's non EMPTY */
113     if (db->out2in_hash.next != EMPTY || db->in2out_hash.next != EMPTY)
114     PLATFORM_DEBUG_PRINT("out2in hash %u, in2out hash %u\n", 
115             db->out2in_hash.next,
116             db->in2out_hash.next);
117     PLATFORM_DEBUG_PRINT("out2in key ipv4 0x%08X, port 0x%04X (%5d), vrf %d, protocol %s\n",
118             db->out2in_key.k.ipv4,
119             db->out2in_key.k.port,
120             db->out2in_key.k.port,
121             db->out2in_key.k.vrf & CNAT_VRF_MASK,
122             (db->out2in_key.k.vrf & CNAT_PRO_MASK) == CNAT_UDP ? "UDP" :
123             ((db->in2out_key.k.vrf & CNAT_PRO_MASK) == CNAT_TCP ? "TCP" :
124              ((db->in2out_key.k.vrf & CNAT_PRO_MASK) == CNAT_ICMP ? "ICMP" : "PPTP ALG")));
125
126     PLATFORM_DEBUG_PRINT("in2out key ipv4 0x%08X, port 0x%04X (%5d), vrf %d, protocol %s\n",
127             db->in2out_key.k.ipv4,
128             db->in2out_key.k.port,
129             db->in2out_key.k.port,
130             db->in2out_key.k.vrf & CNAT_VRF_MASK,
131             (db->in2out_key.k.vrf & CNAT_PRO_MASK) == CNAT_UDP ? "UDP" :
132             ((db->in2out_key.k.vrf & CNAT_PRO_MASK) == CNAT_TCP ? "TCP" : 
133              ((db->in2out_key.k.vrf & CNAT_PRO_MASK) == CNAT_ICMP ? "ICMP" : "UNKNOWN")));
134
135     PLATFORM_DEBUG_PRINT("user %d, user ports (nxt) %d (prev) %d, vrfmap_index 0x%x\n",
136             db->user_index, db->user_ports.next, db->user_ports.prev,
137             db->vrfmap_index);
138     PLATFORM_DEBUG_PRINT("timeout %d \n", db->timeout);
139     PLATFORM_DEBUG_PRINT("flags 0x%x ", db->flags);
140
141     if (db->flags & CNAT_DB_FLAG_TCP_ACTIVE) {
142         PLATFORM_DEBUG_PRINT(" TCP_ACTIVE ");
143     } else if (db->flags & CNAT_DB_FLAG_UDP_ACTIVE) {
144         PLATFORM_DEBUG_PRINT(" UDP_ACTIVE ");
145     } else if (db->flags & CNAT_DB_FLAG_STATIC_PORT) {
146         PLATFORM_DEBUG_PRINT(" STATIC_PORT ");
147     }
148
149     PLATFORM_DEBUG_PRINT(" ALG dlt0 0x%02X dlt1 0x%02X\n", db->alg.alg_dlt[0], db->alg.alg_dlt[1]);
150     PLATFORM_DEBUG_PRINT("\n");
151
152     PLATFORM_DEBUG_PRINT("out2in_pkts: %u\n", db->out2in_pkts);
153     PLATFORM_DEBUG_PRINT("in2out_pkts: %u\n", db->in2out_pkts);
154     PLATFORM_DEBUG_PRINT("entry_expires: %u  current time: %u\n", db->entry_expires, cnat_current_time);
155     PLATFORM_DEBUG_PRINT("-------------------------\n");
156 }
157
158 void cnat_user_db_entry_dump (cnat_user_db_entry_t *up)
159 {
160     u32 db_entry_index, first_db_entry_index;
161     cnat_main_db_entry_t *ep;
162
163     PLATFORM_DEBUG_PRINT("User DB entry at %p, index %ld\n",
164             up, up - cnat_user_db);
165     PLATFORM_DEBUG_PRINT("translation list head index %u, %u translations portmapindex 0x%x\n",
166             up->translation_list_head_index, 
167             up->ntranslations, up->portmap_index);
168     PLATFORM_DEBUG_PRINT("source ipv4 0x%x, source port 0x%x, vrf %d\n",
169             up->key.k.ipv4, 
170             up->key.k.port,
171             up->key.k.vrf);
172     first_db_entry_index = db_entry_index = up->translation_list_head_index;
173     if (first_db_entry_index != EMPTY) {
174         PLATFORM_DEBUG_PRINT("Port translation list:\n");
175         do {
176             PLATFORM_DEBUG_PRINT("  [%d]\n", db_entry_index);
177             ep = cnat_main_db + db_entry_index;
178             db_entry_index = ep->user_ports.next;
179         } while (first_db_entry_index != db_entry_index);
180     } else {
181         PLATFORM_DEBUG_PRINT("WARNING: empty translation list!\n");
182     }
183     PLATFORM_DEBUG_PRINT("-------------------------\n");
184 }
185
186 void cnat_user_db_entry_dump_summary (cnat_user_db_entry_t *up)
187 {
188     u32 db_entry_index, first_db_entry_index;
189     u32 total_entries = 0;
190
191     PLATFORM_DEBUG_PRINT("User DB entry at %p, index %ld\n",
192             up, up - cnat_user_db);
193     PLATFORM_DEBUG_PRINT("translation list head index %u, %u translations portmapindex 0x%x\n",
194             up->translation_list_head_index, 
195             up->ntranslations, up->portmap_index);
196     PLATFORM_DEBUG_PRINT("source ipv4 0x%x, source port 0x%x, vrf %d\n",
197             up->key.k.ipv4, 
198             up->key.k.port,
199             up->key.k.vrf);
200     first_db_entry_index = db_entry_index = up->translation_list_head_index;
201     if (first_db_entry_index != EMPTY) {
202         PLATFORM_DEBUG_PRINT("Port translation list:\n");
203         do {
204             total_entries++;
205         } while (first_db_entry_index != db_entry_index);
206         PLATFORM_DEBUG_PRINT("TOTAL_ENTRIES: %d\n", total_entries);
207     } else {
208         PLATFORM_DEBUG_PRINT("WARNING: empty translation list!\n");
209     }
210     PLATFORM_DEBUG_PRINT("-------------------------\n");
211 }
212
213 /* for internal development and UT only */
214 void cnat_db_dump_main_by_index (int argc, unsigned long *argv)
215 {
216     u32 index, i, len;
217     u32 active_count, scan_count;
218
219     if (argc != 1) {
220         PLATFORM_DEBUG_PRINT("invalid input %d\n", argc);
221         return;
222     } 
223
224     index = argv[0];        
225
226     len = vec_len(cnat_main_db);
227
228     active_count = pool_elts(cnat_main_db);
229
230     if (index >= active_count) {
231         PLATFORM_DEBUG_PRINT("Index %u >= total active entries %u\n", index, active_count);
232         return;
233     }
234
235     scan_count = 0;
236     for (i=0; i< len; i++) {
237         if(pool_is_free_index(cnat_main_db, i)) continue;
238
239         if (index == scan_count) {
240             cnat_main_db_entry_dump(cnat_main_db + i);
241             break;
242         }
243         scan_count++;
244     }
245 }
246
247 void cnat_db_dump_main (int argc, unsigned long *argv)
248 {
249     cnat_main_db_entry_t *db;
250    
251     pool_foreach(db, cnat_main_db, ({
252         cnat_main_db_entry_dump(db);
253     }));
254 }
255
256 void cnat_db_dump_main_summary (int argc, unsigned long *argv)
257 {
258     cnat_main_db_entry_t *db;
259     u32 num_entries = 0;
260    
261     pool_foreach(db, cnat_main_db, ({
262         num_entries++;
263     }));
264
265     PLATFORM_DEBUG_PRINT("\nNum main entries %d\n", num_entries);
266 }
267
268 void cnat_db_dump_user (int argc, unsigned long *argv)
269 {
270     cnat_user_db_entry_t *up;
271
272     pool_foreach(up, cnat_user_db, ({
273         cnat_user_db_entry_dump(up);
274     }));
275 }
276
277 void cnat_db_dump_user_summary (int argc, unsigned long *argv)
278 {
279     cnat_user_db_entry_t *up;
280
281     pool_foreach(up, cnat_user_db, ({
282         cnat_user_db_entry_dump_summary(up);
283     }));
284 }
285
286 void cnat_db_dump_hashes (int argc, unsigned long *argv)
287 {
288     int i;
289
290     PLATFORM_DEBUG_PRINT("Main DB out2in hash:\n");
291     for (i = 0; i < vec_len(cnat_out2in_hash); i++) {
292         if (cnat_out2in_hash[i].next != EMPTY) {
293             PLATFORM_DEBUG_PRINT("[%d]: %u\n", i, cnat_out2in_hash[i].next);
294         }
295     }
296     PLATFORM_DEBUG_PRINT("Main DB in2out hash:\n");
297     for (i = 0; i < vec_len(cnat_in2out_hash); i++) {
298         if (cnat_in2out_hash[i].next != EMPTY) {
299             PLATFORM_DEBUG_PRINT("[%d]: %u\n", i, cnat_in2out_hash[i].next);
300         }
301     }
302
303     PLATFORM_DEBUG_PRINT("User hash:\n");
304     for (i = 0; i < vec_len(cnat_user_hash); i++) {
305         if (cnat_user_hash[i].next != EMPTY) {
306             PLATFORM_DEBUG_PRINT("[%d]: %u\n", i, cnat_user_hash[i].next);
307         }
308     }
309     PLATFORM_DEBUG_PRINT("-------------------------\n");
310 }
311
312
313 #ifdef OLD_VRFMAP
314
315 void cnat_db_dump_cdb (int argc, unsigned long *argv)
316 {
317     int k;
318     int verbose=0;
319     int all = 0;
320
321     if (argc > 0) {
322         verbose = 1;
323     }
324
325     if (argc > 1) {
326         all = 1;
327     }
328
329     PLATFORM_DEBUG_PRINT ("%d vrfmap vectors  \n", vec_len(cnat_portmap_by_vrf));
330
331     for (k = 0; k < vec_len(cnat_portmap_by_vrf); k++) {
332         PLATFORM_DEBUG_PRINT("index%d: status %d i_vrf 0x%x o_vrf 0x%x\n", k,
333                cnat_portmap_by_vrf[k].status, 
334                cnat_portmap_by_vrf[k].i_vrf,
335                cnat_portmap_by_vrf[k].o_vrf);
336         cnat_db_dump_address_portmap(verbose, all,
337                                      cnat_portmaps[k],
338                                      cnat_portmaps_inuse[k]);
339     }
340 }
341
342 void cnat_db_dump_i_vrf (int argc, unsigned long *argv)
343 {
344     u32 k;
345     u32 vrf =0;
346     int verbose=0;
347     int all = 0;
348
349     if (!argc) {
350         PLATFORM_DEBUG_PRINT("need vrf input ,return\n");
351         return;
352     }
353
354     if (argc > 0) {
355          vrf = argv[0];
356     }
357    
358     if (argc > 1) {
359         verbose = 1;
360     }
361
362     if (argc > 2) {
363         all = 1;
364     }
365
366     PLATFORM_DEBUG_PRINT ("%d vrfmap vectors  \n", vec_len(cnat_portmap_by_vrf));
367
368     for (k = 0; k < vec_len(cnat_portmap_by_vrf); k++) {
369         if (cnat_portmap_by_vrf[k].i_vrf == vrf) {
370             PLATFORM_DEBUG_PRINT("%d:  i_vrf 0x%x o_vrf 0x%x\n", k,
371                cnat_portmap_by_vrf[k].i_vrf,
372                cnat_portmap_by_vrf[k].o_vrf);
373             cnat_db_dump_address_portmap(verbose, all,
374                                      cnat_portmaps[k],
375                                      cnat_portmaps_inuse[k]);
376             return;
377         }
378     }
379     PLATFORM_DEBUG_PRINT("not found\n");
380 }
381
382 void cnat_db_dump_o_vrf (int argc, unsigned long *argv)
383 {
384     u32 k;
385     int verbose=0;
386     int all = 0;
387     u32 vrf =0;
388
389     if (!argc) {
390         PLATFORM_DEBUG_PRINT("need vrf input ,return\n");
391         return;
392     }
393
394     if (argc > 0) {
395          vrf = argv[0];
396     }
397
398     if (argc > 1) {
399         verbose = 1;
400     }
401
402     if (argc > 2) {
403         all = 1;
404     }
405
406     PLATFORM_DEBUG_PRINT ("%d vrfmap vectors  \n", vec_len(cnat_portmap_by_vrf));
407
408     for (k = 0; k < vec_len(cnat_portmap_by_vrf); k++) {
409         if (cnat_portmap_by_vrf[k].o_vrf == vrf) {
410             PLATFORM_DEBUG_PRINT("index%d: status %d i_vrf 0x%x o_vrf 0x%x\n", k,
411                cnat_portmap_by_vrf[k].status,
412                cnat_portmap_by_vrf[k].i_vrf,
413                cnat_portmap_by_vrf[k].o_vrf);
414             cnat_db_dump_address_portmap(verbose, all,
415                                      cnat_portmaps[k],
416                                      cnat_portmaps_inuse[k]);
417             return;
418         }
419     }
420     PLATFORM_DEBUG_PRINT("not found\n");
421 }
422 #endif
423
424 #ifdef TOBE_PORTED
425 /* This does not seem to be used */
426 void cnat_db_mem_usage_cmd (int argc, unsigned long *argv)
427 {
428     pool_header_t * p;
429     _VEC *_v;
430     u32 bitmap_bytes=0, free_indices_bytes=0, vec_bytes=0, total_bytes=0;
431
432     if (cnat_main_db) {
433         p = pool_header(cnat_main_db);
434         if (p->free_bitmap) {
435             _v = _vec_find(p->free_bitmap);
436             bitmap_bytes = _v->alen;
437         } else {
438             bitmap_bytes = 0;
439         }
440         if (p->free_indices) {
441             _v = _vec_find(p->free_indices);
442             free_indices_bytes = _v->alen;
443         } else {
444             free_indices_bytes = 0;
445         }
446         _v = _vec_find(cnat_main_db);
447         vec_bytes = _v->alen;
448     } else {
449         vec_bytes = 0;
450     }
451     
452     total_bytes = bitmap_bytes + free_indices_bytes + vec_bytes;
453
454     PLATFORM_DEBUG_PRINT ("Main DB: %d total bytes, %d bitmap, %d indices, %d vec\n", 
455             total_bytes, bitmap_bytes, free_indices_bytes, vec_bytes);
456     PLATFORM_DEBUG_PRINT ("  vector length %d\n", vec_len(cnat_main_db));
457
458     if (cnat_user_db) {
459         p = pool_header(cnat_user_db);
460         if (p->free_bitmap) {
461             _v = _vec_find(p->free_bitmap);
462             bitmap_bytes = _v->alen;
463         } else {
464             bitmap_bytes = 0;
465         }
466         if (p->free_indices) {
467             _v = _vec_find(p->free_indices);
468             free_indices_bytes = _v->alen;
469         } else {
470             free_indices_bytes = 0;
471         }
472         _v = _vec_find(cnat_user_db);
473         vec_bytes = _v->alen;
474     } else {
475         vec_bytes = 0;
476     }
477     
478     total_bytes = bitmap_bytes + free_indices_bytes + vec_bytes;
479
480     PLATFORM_DEBUG_PRINT ("User DB: %d total bytes, %d bitmap, %d indices, %d vec\n", 
481             total_bytes, bitmap_bytes, free_indices_bytes, vec_bytes);
482     PLATFORM_DEBUG_PRINT ("  vector length %d\n", vec_len(cnat_user_db));
483
484     _v = _vec_find(cnat_out2in_hash);
485     PLATFORM_DEBUG_PRINT("out2in hash: %d total bytes\n", _v->alen);
486
487     _v = _vec_find(cnat_in2out_hash);
488     PLATFORM_DEBUG_PRINT("in2out hash: %d total bytes\n", _v->alen);
489 }
490 #endif
491
492 static void print_server_ip_address (vlib_main_t *vm, u32 ip)
493 {
494     unsigned char bytes[4];
495     bytes[0] = ip & 0xFF;
496     bytes[1] = (ip >> 8) & 0xFF;
497     bytes[2] = (ip >> 16) & 0xFF;
498     bytes[3] = (ip >> 24) & 0xFF;       
499     vlib_cli_output(vm, "\tIP Address %d.%d.%d.%d\n", bytes[0], bytes[1], bytes[2], bytes[3]); 
500 }
501
502 void cnat_nfv9_show_collector (vlib_main_t *vm, cnat_nfv9_logging_info_t *my_nfv9_logging_info)
503 {
504     nfv9_server_info_t *server =  nfv9_server_info_pool +
505         my_nfv9_logging_info->server_index;
506
507     vlib_cli_output(vm,"\tVRF - 0x%x - %s\n", my_nfv9_logging_info->i_vrf,
508            my_nfv9_logging_info->deleted?"DELETED":"ACTIVE");
509     print_server_ip_address(vm, clib_net_to_host_u32(server->ipv4_address)); 
510     vlib_cli_output(vm,"\tIP port  %d\n", server->port);
511     vlib_cli_output(vm,"\tTimeout  %d\n", server->timeout_rate);
512     vlib_cli_output(vm,"\tRefresh  %d\n", server->refresh_rate);
513     vlib_cli_output(vm,"\tMax PkSz %d\n", my_nfv9_logging_info->max_length_minus_max_record_size);
514 }
515
516 void cnat_db_dump_policy (int argc, unsigned long *argv) 
517 {
518
519     PLATFORM_CNAT_DB_DUMP_POLICY_PRINT();
520
521     if (cnat_nfv9_global_info.cnat_nfv9_init_done) {
522         if (cnat_nfv9_global_info.cnat_nfv9_global_collector_index != EMPTY) {
523             cnat_nfv9_logging_info_t *my_nfv9_logging_info;
524             nfv9_server_info_t *server __attribute__((unused));
525
526             my_nfv9_logging_info = cnat_nfv9_logging_info_pool +
527                         cnat_nfv9_global_info.cnat_nfv9_global_collector_index;
528             server =  nfv9_server_info_pool + 
529                 my_nfv9_logging_info->server_index;
530
531             PLATFORM_DEBUG_PRINT("NFv9 logging ip 0x%x port 0x%x refresh-rate %d timeout %d\n",
532                    server->ipv4_address,
533                    server->port,
534                    server->refresh_rate,
535                    server->timeout_rate);
536             PLATFORM_DEBUG_PRINT("NFv9 path_mtu = %d\n", 
537                    my_nfv9_logging_info->max_length_minus_max_record_size);
538         } else {
539             PLATFORM_DEBUG_PRINT("NFv9 global logging is not configured\n");
540         }
541     } else {
542         PLATFORM_DEBUG_PRINT("NFv9 LOGGING is not configured\n");
543     }
544            
545 }
546
547 #ifdef OLD_VRFMAP
548 void cnat_show_cdb (int verbose)
549 {
550     int k, l, i;
551     for (i = 0; i < vec_len(cnat_portmap_by_vrf); i++) {
552         PLATFORM_DEBUG_PRINT("i_vrf %d : o_vrf %d\n",
553                 cnat_portmap_by_vrf[i].i_vrf,
554                 cnat_portmap_by_vrf[i].o_vrf);
555     }
556
557     PLATFORM_DEBUG_PRINT("port limit %d\n", cnat_main_db_max_ports_per_user);
558
559     PLATFORM_DEBUG_PRINT ("%d portmap vectors\n", vec_len(cnat_portmaps));
560
561     for (k = 0; k < vec_len(cnat_portmaps); k++) {
562         cnat_portmap_t *pm;
563         u16 *inuse;
564         pm = cnat_portmaps[k];
565         inuse = cnat_portmaps_inuse[k];
566         for (l = 0; l < vec_len(pm); l++) {
567             if (inuse[l] || verbose ) {
568                 u32 net_address;
569                 net_address =
570                     spp_host_to_net_byte_order_32((pm+l)->ipv4_address);
571                 printf_ipv4(net_address);
572                 PLATFORM_DEBUG_PRINT (": %d inuse\n", inuse[l]);
573                 if (verbose && inuse[l]) {
574                     cnat_portmap_dump (pm+l, inuse+l);
575                 }
576             }
577         }
578     }
579 }
580 #endif
581
582
583
584 /* v2 show command */
585 void cnat_show_address_portmap_sumary (cnat_portmap_v2_t *pm)
586 {
587     cnat_portmap_v2_t *my_pm =0;
588     u32 first_address = 0; 
589     u32 second_address = 0;
590     u32 last_address = 0;
591     u32 i, pm_len;
592
593     if ((pm_len = vec_len(pm))) {
594         PLATFORM_DEBUG_PRINT("%d portmap in this list 0x%lx\n", 
595                              pm_len, (u32)pm);
596         for (i = 0; i < pm_len; i++) {
597             my_pm = pm + i;
598             if (!first_address) {
599                 first_address = my_pm->ipv4_address;
600             } else if (!second_address) {
601                 second_address = my_pm->ipv4_address;
602             }
603             last_address = my_pm->ipv4_address;
604         }
605
606         if (first_address) {
607             PLATFORM_DEBUG_PRINT("1. 0x%08x", first_address);
608         }
609         if (second_address) {
610             PLATFORM_DEBUG_PRINT(", 2. 0x%08x", second_address);
611         }
612
613         if ((last_address != first_address) && 
614             (last_address != second_address)) {
615             PLATFORM_DEBUG_PRINT(",  .....,  %d. 0x%08x", pm_len, last_address);
616         }
617         PLATFORM_DEBUG_PRINT("\n");
618     } else {
619         PLATFORM_DEBUG_PRINT("ZERO POOL ADDRESSES in this list 0x%x \n", (u32)pm);
620     }
621 }
622
623
624 void cnat_show_address_portmap (int verbose, int all, 
625                                 cnat_portmap_v2_t *pm, u16 port_limit)
626 {
627     cnat_portmap_v2_t *my_pm =0;
628     u32 i, pm_len;
629
630     pm_len = vec_len(pm);
631     if (!all) {
632         cnat_show_address_portmap_sumary(pm);
633     } else {
634         PLATFORM_DEBUG_PRINT("%d portmap in this list 0x%x \n", pm_len, (u32)pm);
635     }
636
637     for (i = 0; i < pm_len; i++) {
638
639         my_pm = pm + i;
640         if (all) {
641             PLATFORM_DEBUG_PRINT("pm:0x%x ip address:0x%x del_time 0x%x inuse:%d\n",
642             (u32)my_pm, my_pm->ipv4_address, my_pm->delete_time, my_pm->inuse);
643         } else if (my_pm->inuse) {
644             PLATFORM_DEBUG_PRINT("pm:0x%x ip address:0x%x inuse:%d\n",
645             (u32)my_pm, my_pm->ipv4_address, my_pm->inuse); 
646         }
647
648         if (verbose && (my_pm->inuse)) {
649             if(PREDICT_FALSE(!port_limit)) {
650                cnat_portmap_dump_v2 (my_pm, cnat_main_db_max_ports_per_user);
651             }
652             else {
653               cnat_portmap_dump_v2 (my_pm, port_limit);
654             }
655         }
656     }
657
658     PLATFORM_DEBUG_PRINT("\n");
659 }
660
661
662 void cnat_show_cdb_v2 (int verbose, int all)
663 {
664     cnat_vrfmap_t *my_vrfmap = 0;
665     cnat_portmap_v2_t *pm =0;
666     PLATFORM_DEBUG_PRINT("port limit %d\n", cnat_main_db_max_ports_per_user);
667     PLATFORM_DEBUG_PRINT("total address pool allocated %d\n", total_address_pool_allocated);
668     PLATFORM_DEBUG_PRINT("icmp rate limit %d (per core %d)\n",
669         cnat_main_db_icmp_rate_limit, cnat_main_db_icmp_rate_limit_core);
670     PLATFORM_DEBUG_PRINT("dynamic port range start %d\n", cnat_static_port_range);
671     if (pptp_cfg.enable == PPTP_DISABLED) {
672         PLATFORM_DEBUG_PRINT("PPTP alg disabled \n");
673     } else {
674         PLATFORM_DEBUG_PRINT("PPTP alg enabled \n");
675     }
676
677     if (ftp_alg_enabled) {
678         PLATFORM_DEBUG_PRINT("FTP alg enabled\n");
679     } else {
680         PLATFORM_DEBUG_PRINT("FTP alg disabled\n");
681     }
682
683     pool_foreach (my_vrfmap, cnat_map_by_vrf, ({
684         CNAT_MY_VRFMAP_PRINT
685         CNAT_MY_LOGGING_INFO_PRINT
686         PLATFORM_DEBUG_PRINT("per vrf port limit %d\n", my_vrfmap->port_limit);
687         pm = my_vrfmap->portmap_list;
688         cnat_show_address_portmap(verbose, all, pm, my_vrfmap->port_limit);
689
690     }));
691 }
692
693
694 void cnat_show_cdb_command_v2(int argc, unsigned long *argv)
695 {
696     int verbose=0;
697     int all = 0;
698
699     if (argc > 0) {
700         verbose = 1;
701     }
702
703     if (argc > 1) {
704         all = 1;
705     }
706
707     cnat_show_cdb_v2(verbose, all);
708 }
709
710 void cnat_show_ivrf_command_v2 (int argc, unsigned long *argv)
711 {
712     u32 vrf =0;
713     int verbose=0;
714     int all = 0;
715     cnat_vrfmap_t *my_vrfmap = 0;
716     cnat_portmap_v2_t *pm =0;
717
718     if (!argc) {
719         PLATFORM_DEBUG_PRINT("need vrf input ,return\n");
720         return;
721     }
722     if (argc > 0) {
723          vrf = argv[0];
724     }
725     if (argc > 1) {
726         verbose = 1;
727     }
728     if (argc > 2) {
729         all = 1;
730     }
731     PLATFORM_DEBUG_PRINT ("%lld vrfmap vectors  \n", pool_elts(cnat_map_by_vrf));
732     pool_foreach (my_vrfmap, cnat_map_by_vrf, ({
733         if (my_vrfmap->i_vrf == vrf) {
734             CNAT_MY_VRFMAP_PRINT
735             pm = my_vrfmap->portmap_list;
736             cnat_show_address_portmap(verbose, all, pm,my_vrfmap->port_limit);
737             return;
738         }
739     }));
740     PLATFORM_DEBUG_PRINT("not found\n");
741 }
742
743 void cnat_show_ovrf_command_v2 (int argc, unsigned long *argv)
744 {
745     u32 not_found =1;
746     u32 vrf =0;
747     int verbose=0;
748     int all = 0;
749     cnat_vrfmap_t *my_vrfmap = 0;
750     cnat_portmap_v2_t *pm =0;
751
752     if (!argc) {
753         PLATFORM_DEBUG_PRINT("need vrf input ,return\n");
754         return;
755     }
756     if (argc > 0) {
757          vrf = argv[0];
758     }
759     if (argc > 1) {
760         verbose = 1;
761     }
762     if (argc > 2) {
763         all = 1;
764     }
765     PLATFORM_DEBUG_PRINT("%d vrfmap vectors  \n", pool_elts(cnat_map_by_vrf));
766     pool_foreach (my_vrfmap, cnat_map_by_vrf, ({
767         if (my_vrfmap->o_vrf == vrf) {
768             CNAT_MY_VRFMAP_PRINT
769             pm = my_vrfmap->portmap_list;
770             cnat_show_address_portmap(verbose, all, pm,my_vrfmap->port_limit);
771             not_found = 0; 
772         }
773     }));
774     if (not_found) {
775         PLATFORM_DEBUG_PRINT("not found\n");
776     }
777 }
778
779 void cnat_timeout_db_entry_dump (cnat_timeout_db_entry_t *up)
780 {
781     u32 db_entry_index __attribute__((unused)), 
782         first_db_entry_index __attribute__((unused));
783
784     PLATFORM_DEBUG_PRINT("Timeout DB entry at index %ld\n", up - cnat_timeout_db);
785     PLATFORM_DEBUG_PRINT("Desnt key 0x%16llx\n", up->t_key.timeout_key.key64);
786     PLATFORM_DEBUG_PRINT("Timeout value %d\n", up->t_key.timeout_value);
787     PLATFORM_DEBUG_PRINT("Hash Next  0x%x\n", up->t_hash.next);
788
789 }
790
791 void cnat_db_dump_timeout ()
792 {
793     cnat_timeout_db_entry_t *up;
794     pool_header_t     *h;
795     u32 used __attribute__((unused)), free __attribute__((unused));
796
797     h = pool_header(cnat_timeout_db);
798     free = vec_len(h->free_indices);
799     used = (vec_len(cnat_timeout_db) - free);
800
801     PLATFORM_DEBUG_PRINT("Timeout DB Free %d, Used %d\n",free, used);
802
803     pool_foreach(up, cnat_timeout_db, ({
804         cnat_timeout_db_entry_dump(up);
805     }));
806 }
807