Integration changes with latest NWAL
[keystone-rtos/netapi.git] / ti / runtime / netapi / test / net_test_router.c
1 /******************************************************************************
2  * File: net_test_router.c
3  * Purpose: net_test_router application
4  ******************************************************************************
5  * FILE:  net_test_router.c
6  * 
7  * DESCRIPTION:  netapi user space transport
8  *               library  net_test_router application
9  * 
10  * REVISION HISTORY:
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2013
13  * 
14  *  Redistribution and use in source and binary forms, with or without 
15  *  modification, are permitted provided that the following conditions 
16  *  are met:
17  *
18  *    Redistributions of source code must retain the above copyright 
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  *    Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the 
23  *    documentation and/or other materials provided with the   
24  *    distribution.
25  *
26  *    Neither the name of Texas Instruments Incorporated nor the names of
27  *    its contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
31  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
32  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
34  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
35  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
36  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
39  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
40  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************************************/
44 #include "trie.h"
46 #include <signal.h>
47 #include <pthread.h>
48 #include <sys/resource.h>
49 #include "router.h"
50 extern int QUIT;
53 #if defined(DEVICE_K2H)
54 #include <ti/runtime/hplib/device/k2h/src/hplib_device.c>
55 #include <ti/runtime/netapi/device/k2h/src/netapi_device.c>
56 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
57 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
58 #elif defined (DEVICE_K2K)
59 #include <ti/runtime/hplib/device/k2k/src/hplib_device.c>
60 #include <ti/runtime/netapi/device/k2k/src/netapi_device.c>
61 #include <ti/drv/qmss/device/k2k/src/qmss_device.c>
62 #include <ti/drv/cppi/device/k2k/src/cppi_device.c>
63 #elif defined (DEVICE_K2L)
64 #include <ti/runtime/hplib/device/k2l/src/hplib_device.c>
65 #include <ti/runtime/netapi/device/k2l/src/netapi_device.c>
66 #include <ti/drv/qmss/device/k2l/src/qmss_device.c>
67 #include <ti/drv/cppi/device/k2l/src/cppi_device.c>
68 #elif defined (DEVICE_K2E)
69 #include <ti/runtime/hplib/device/k2e/src/hplib_device.c>
70 #include <ti/runtime/netapi/device/k2e/src/netapi_device.c>
71 #include <ti/drv/qmss/device/k2e/src/qmss_device.c>
72 #include <ti/drv/cppi/device/k2e/src/cppi_device.c>
73 #else /*Default */
74 #include <ti/runtime/hplib/device/k2h/src/hplib_device.c>
75 #include <ti/drv/nwal/device/k2h/src/nwal_device.c>
76 #include <ti/runtime/netapi/device/k2h/src/netapi_device.c>
77 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
78 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
79 #endif
80 /* Global definitions */
82 netTestConfig_t netTestCfg;
83 static  netTestConfigFile_t config_file;
85 char    input_file_name[] = "/etc/netapi/net_test_config.txt";
87 nwal_RetValue       nwalRetVal;
88 Pktlib_HeapHandle ourHeap;
90 PKTIO_HANDLE_T *netcp_tx_chan_no_crypto;
91 PKTIO_HANDLE_T *netcp_rx_chan;
92 PKTIO_HANDLE_T *netcp_tx_chan_esp;
93 PKTIO_HANDLE_T *netcp_tx_chan_ah;
98 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
99 PKTIO_CFG_T netcp_tx_cfg={PKTIO_TX, PKTIO_NA, PKTIO_NA, 8};
103 NETCP_CFG_EXCEPTION_PKT_T expPkt_appid;
105 Trie *p_trie_sa_rx;
106 Trie *p_trie_sa_tx;
109 #include "router.c"
110 Trie * our_router;
112 OUR_ROUTE_T routes[MAX_ROUTES]=
115     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
116     {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
117     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
118     {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
119     {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}
120 };
121 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
123 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
124                          PKTIO_METADATA_T meta[], int n_pkts,
125                          uint64_t ts );
128 extern netTestStats_T stats[TUNE_NETAPI_NUM_CORES];
129 extern paSysStats_t netcp_stats;
131 /*******************************************
132  *************NETAPI OBJECTS***************
133  *****************************************/
134 static NETAPI_CFG_T our_netapi_default_cfg=
136 TUNE_NETAPI_PERM_MEM_SZ,
137 128,  //start of packet offset for hw to place data on rx for default flow
138 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
139 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
140 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
141 64, //#descriptors w/o buffers in default heap
142 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
143 128,  //tail room
144 256,  //extra room
146 };
150 void house(NETAPI_SCHED_HANDLE_T *s);
151 NETAPI_T netapi_handle;
152 NETAPI_SCHED_HANDLE_T * our_sched;
153 #ifdef netTest_MULTI_THREAD
154 NETAPI_SCHED_HANDLE_T * scheduler[TUNE_NETAPI_NUM_CORES];
155 #endif
156 NETAPI_SCHED_CONFIG_T our_sched_cfg={
157   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
158 };
161 NETCP_CFG_IP_T ip_rule[NET_TEST_MAX_IP];
162 NETCP_CFG_MACIF_T mac[NET_TEST_MAX_MAC];
166 /* security objects. (for loopback mode) */
167 netTestSA_t sa_info[MAX_SEC_INDEX];
170 NETCP_CFG_IPSEC_POLICY_T rx_policy[MAX_SEC_INDEX];
175 /*************************END NETAPI OBJECTS***********************/
177 void update_header(netTestHead_T * p_head, int len)
179    unsigned char *p = (unsigned char *) &p_head->udp[1];
180    len -= (20+14);
181    /* update ip checksum */
182    /* update udp checksum */
183    /* update length */
184    *p= (len&0xff00)>>8;
185    *(p+1) = len&0xff;
188 #ifdef netTest_MULTI_THREAD
189 /* Templates to build command labels at startup up time, required by open_pktio_tx_channels() */
190 nwalTxPktInfo_t txPktInfoESP = 
192     NULL,                                                                                               /* p_pkt */
193     NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
194     0,                                                                                                  /* lpbackPass */
195     1,                                                                                                  /* enetport */
196     0,                                                                                                  /* msuSize */
197     0,                                                                                                   /* startOffset */
198     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
199     0,                                                                                                  /* saPayLoadLen */
200     0               ,                                                                                    /* saAhIcvOffBytes */
201     0,                                                                                                 /* saAhMacSize */
202     0,                                                                                                  /* etherLenOffBytes */
203     netTest_MAC_HEADER_LEN,                                                        /* ipOffBytes */
204     0,                          /* l4OffBytes */
205     0,                         /* l4HdrLen */
206     0,                                                                                   /* pseudoHdrChecksum */
207     0                                                                                                   /* pLoadLen */
208 };
211 nwalTxPktInfo_t txPktInfoAH = 
213     NULL,                                                                                               /* p_pkt */
214     NWAL_TX_FLAG1_DO_IPSEC_AH_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
215     0,                                                                                                  /* lpbackPass */
216     0,                                                                                                  /* enetport */
217     0,                                                                                                  /* msuSize */
218     0,                                                                                                   /* startOffset */
219     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
220     0,                                                                                                  /* saPayLoadLen */
221     netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN + netTest_IPSEC_AH_FIXED_HDR_SIZE,                    /* saAhIcvOffBytes */
222     12,                                                                                                 /* saAhMacSize */
223     0,                                                                                                  /* etherLenOffBytes */
224     netTest_MAC_HEADER_LEN,   /* ipOffBytes */
225     0,                    /* l4OffBytes */
226     netTest_UDP_HEADER_LEN,                                                                            /* l4HdrLen */
227     0,                                                                                                  /* pseudoHdrChecksum */
228     0                                                                                                   /* pLoadLen */
229 };
231 nwalTxPktInfo_t txPktInfoNoCrypto = 
233     NULL,                                                                                               /* p_pkt */
234     NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
235     0,                                                                                                  /* lpbackPass */
236     0,                                                                                                  /* enetport */
237     0,                                                                                                  /* msuSize */
238     0,                                                                                                   /* startOffset */
239     0,                                                    /* saOffBytes */
240     0,                                                                                                  /* saPayLoadLen */
241     0               ,                                                                                    /* saAhIcvOffBytes */
242     0,                                                                                                 /* saAhMacSize */
243     0,                                                                                                  /* etherLenOffBytes */
244     netTest_MAC_HEADER_LEN,         /* ipOffBytes */
245     netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN,                                        /* l4OffBytes */
246     netTest_UDP_HEADER_LEN,                                                             /* l4HdrLen */
247     0,                                                                         /* pseudoHdrChecksum */
248     0                                                                                                   /* pLoadLen */
249 };
251 void close_pktio_channels(void)
253     int err;
254     netapi_pktioClose(netcp_tx_chan_no_crypto ,&err);
255     netapi_pktioClose(netcp_tx_chan_esp ,&err);
256     netapi_pktioClose(netcp_tx_chan_ah ,&err);
261 void open_pktio_tx_channels()
263     int err;
264     /* open netcp default TX channels for non-ipsec*/
265     netcp_tx_chan_no_crypto= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
266     if (!netcp_tx_chan_no_crypto)
267     {
268         netapi_Log("pktio open TX failed err=%d\n",err);
269         exit(1);
270     }
271     else
272     {
273             PKTIO_CONTROL_T control;
274             control.op = PKTIO_UPDATE_FAST_PATH;
275             PKTIO_CFG_T cfg;
276             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_NO_CRYPTO_NO_CKSUM_PORT;
277             cfg.fast_path_cfg.txPktInfo= &txPktInfoNoCrypto;
278             netapi_pktioControl(netcp_tx_chan_no_crypto, NULL, &cfg, &control, &err);
279     }
280     /* open netcp default TX for ESP packets */
281     netcp_tx_chan_esp= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
282     if (!netcp_tx_chan_esp)
283     {
284         netapi_Log("pktio open TX failed err=%d\n",err);
285         exit(1);
286     }
287     else
288     {
289         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
290         {
291             PKTIO_CONTROL_T control;
292             control.op = PKTIO_UPDATE_FAST_PATH;
293             PKTIO_CFG_T cfg;
294             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_ESP_L3CKSUM_PORT;
295             cfg.fast_path_cfg.txPktInfo= &txPktInfoESP;
296             netapi_pktioControl(netcp_tx_chan_esp, NULL, &cfg, &control, &err);
297         }
298     }
300     /*/* open netcp default  TX for AH packets */
301     netcp_tx_chan_ah= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
302     if (!netcp_tx_chan_ah)
303     {
304         netapi_Log("pktio open TX failed err=%d\n",err);
305         exit(1);
306     }
307     else
308     {
309         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
310         {
311             PKTIO_CONTROL_T control;
312             control.op = PKTIO_UPDATE_FAST_PATH;
313             PKTIO_CFG_T cfg;
314             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_AH_PORT;
315             cfg.fast_path_cfg.txPktInfo= &txPktInfoAH;
316             netapi_pktioControl(netcp_tx_chan_ah, NULL, &cfg, &control, &err);
317         }
318     }
324 NETAPI_T worker_nh[TUNE_NETAPI_NUM_CORES];
325 void slow_path_thread(uint32_t index)
327     int err,i;
328     uint32_t thread_num;
329     PKTIO_HANDLE_T *rx_chan;
330     PKTIO_HANDLE_T *sb_tx_chan;
331     PKTIO_HANDLE_T *sb_rx_chan;
332     cpu_set_t cpu_set;
334     thread_num = netTestCfg.sp_thread_num[index];
335     printf("slow_path_thread for index %d  called for thread %d\n", index, thread_num);
337     CPU_ZERO( &cpu_set);
338 #ifdef CORTEX_A8
339    for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
340     {
341         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
342         CPU_SET( i, &cpu_set);
343     }
344     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
345 #else
346     for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
347     {
348         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
349         CPU_SET( i, &cpu_set);
350     }
351     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
352 #endif
353     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
354                                       NULL,
355                                       &hplibDeviceGblCfgParam,
356                                       &netapiDeviceGblCfgParam);
357     if (worker_nh[thread_num] == NULL)
358     {
359         printf("slow_path_thread: netapi_init failure, exiting\n");
360         exit(1);
361     }
362     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
364    /* open netcp RX channel */
365     //rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
369     netapi_setCookie(worker_nh[thread_num],(void*) (thread_num | NET_TEST_SP_THREAD_MASK));
370       
371     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
372     if (!scheduler[thread_num]) 
373     {
374         netapi_Log("sched create failed for core%d\n",thread_num); 
375         exit(1);
376     }
377       scheduler[thread_num]->config.yield = FALSE;
378       scheduler[thread_num]->config.pollGarbageQ = TRUE;
379       scheduler[thread_num]->config.pollCtrlQ = TRUE;
380             /*********************************************/
381     /**************Entry point into scheduler ****/
382     /*********************************************/
383     netapi_schedRun(scheduler[thread_num], &err);
384     netapi_Log("slow_path_thread: core %d worker thread done\n",thread_num);
386     //netapi_pktioClose(rx_chan, &err);
387     netapi_shutdown(worker_nh[thread_num]);
391 void fast_path_thread(uint32_t index)
393     int err,i;
394     uint32_t thread_num;
395     PKTIO_HANDLE_T *rx_chan;
396     PKTIO_HANDLE_T *sb_tx_chan;
398     cpu_set_t cpu_set;
400     thread_num = netTestCfg.fp_thread_num[index];
401     printf("fast_path_thread for index %d  called for thread %d\n", index, thread_num);
402     CPU_ZERO( &cpu_set);
403 #ifdef CORTEX_A8
404     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
405     {
406         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
407         CPU_SET( i, &cpu_set);
408     }
409     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
410 #else
411     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
412     {
413         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
414         CPU_SET( i, &cpu_set);
415     }
416     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
417 #endif
418     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
419                                       NULL,
420                                       &hplibDeviceGblCfgParam,
421                                       &netapiDeviceGblCfgParam);
422     if (worker_nh[thread_num] == NULL)
423     {
424         printf("fast_path_thread: netapi_init failure, exiting\n");
425         exit(1);
426     }
427     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
429    /* open netcp RX channel */
430     rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
431     netapi_setCookie(worker_nh[thread_num],(void*)thread_num);
433     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
434     if (!scheduler[thread_num]) 
435         {
436         netapi_Log("sched create failed for core%d\n",thread_num); 
437         exit(1);
438     }
440     /*********************************************/
441     /**************Entry point into scheduler ****/
442     /*********************************************/
443     scheduler[thread_num]->config.yield = FALSE;
444      scheduler[thread_num]->config.pollGarbageQ = FALSE;
445      scheduler[thread_num]->config.pollCtrlQ = FALSE;
446      //sleep(100000);
447     netapi_schedRun(scheduler[thread_num], &err);
448     netapi_Log("fast_path_thread: core %d worker thread done\n",thread_num);
450     netapi_pktioClose(rx_chan, &err);
451     netapi_shutdown(worker_nh[thread_num]);
453 #endif
455 /***************************************
456  ********** test driver*****************
457  ***************************************/
458 int main(int argc, char **argv)
460     int err,i;
461     Pktlib_HeapCfg      heapCfg;
462     int32_t             errCode;
463     Pktlib_HeapIfTable*  pPktifTable;
464     FILE * fpr = NULL;
466     cpu_set_t cpu_set;
467     /* install signal handler for ^c */
468     signal(SIGINT,netTest_utilMySig);
470     if (argc == 2)
471     {
472         fpr = fopen(argv[1], "r");
473     }
474     else
475     {
476         fpr = fopen(input_file_name, "r");
477     }
478     if (fpr == NULL)
479     {
480         exit(1);
481     }
482     else
483     {
484         memset(&config_file, 0, sizeof(netTestConfigFile_t));
485         memset(&netTestCfg, 0, sizeof(netTestConfig_t));
486         netTest_utilProcessConfigFile(fpr,&config_file);
487         netTest_utilParseMac(&config_file);
489         /* parse slow path/fast path thread configuration parameters */
490         netTest_utilParseThreadParams(&config_file);
492         netTest_utilParseIP(&config_file);
494         netTest_utilParseIpsecMode(&config_file);
496         /* DSP mac processing */
497         parse_dsp_mac(&config_file.dsp_mac[0]);
499        /* DSP IP processing */
500         parse_dsp_ip(&config_file.dsp_ip[0]);
502         netTest_utilParseRoutes(&config_file, &routes[0], &our_router);
503         
504         /* IPSEC interface number processing */
505         parse_simple_param_u32((char*)&config_file.ipsec_if_no[0], &netTestCfg.ipsec_if_no);
507         netTest_utilParseSA(&config_file);
508     }
510     memset(&sa_info, 0, sizeof(sa_info));
512 #ifdef netTest_MULTI_THREAD
513     /* assign main net_test thread to run on core 0 */
514     CPU_ZERO( &cpu_set);
515     CPU_SET( 0, &cpu_set);
516     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
517 #endif
518     /* create netapi */
519     netapi_handle = netapi_init(NETAPI_SYS_MASTER,
520                                 &our_netapi_default_cfg,
521                                 &hplibDeviceGblCfgParam,
522                                 &netapiDeviceGblCfgParam);
524     if (netapi_handle == NULL)
525     {
526         printf("main: netapi_init failure, exiting\n");
527         exit(1);
528     }
529     /* configure expection packet handling with netapi */
530     netapi_netcpCfgExceptions(netapi_handle, NETCP_CFG_ALL_EXCEPTIONS, NETCP_CFG_ACTION_DISCARD, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
531     expPkt_appid = netapi_netcpCfgExceptions(netapi_handle, 7, NETCP_CFG_ACTION_TO_SW, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
533     /* open the main heap */
534     ourHeap = Pktlib_findHeapByName("netapi");
535     if (!ourHeap)
536     {
537         netapi_Log("Pktlib_findHeapByName()  fail\n");
538         exit(1);
539     }
541     /* Open all required PKTIO TX channels */
542     open_pktio_tx_channels();
544     netapi_Log("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
545     /* create scheduler instance */
546     our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
547     if (!our_sched) {netapi_Log("sched create failed\n"); exit(1);}
550     /*create net_test MAC interfaces, attach IP to created MAC interfaces */
551     netTest_utilCreateInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
553     /* Lookup Database for SA context, this is used by packet processing routines to get RX and TX SA information*/
554     p_trie_sa_rx = trie_new();
555     p_trie_sa_tx = trie_new();
556    if (!p_trie_sa_rx || !p_trie_sa_tx)
557         {netapi_Log("trie alloc for SA  failed\n"); exit(1);}
559     /* Create RX SA's, RX Policy and TX SA's, all SA configuration parameters are read from net_test_config.txt file */
560     netTest_utilCreateSecAssoc();
563 #ifdef netTest_MULTI_THREAD
565     char c;
566     /* create and set affinity of slow path and fast path threads to
567     * specific CPU cores as specified in the net_test_config.txt file */
568      netTest_utilCreateSpFpThreads(netTestCfg.num_sp_threads, 
569                                   (NET_TEST_FUNC_PTR) slow_path_thread,
570                                   netTestCfg.num_fp_threads,
571                                   (NET_TEST_FUNC_PTR) fast_path_thread);
573         //this thread of execution (main) now just waits on user input
574         for(;;)
575         {
576            printf(">");
577            c=getchar();
578            if (c=='q') {QUIT=1;break;}
579            else if (c=='s') netTest_utilsStatsCb(netapi_handle, &netcp_stats);
580            else if (c=='h') printf("'q' to quit,  's' for stats, 'h' for help\n");
581         }
582         netTest_utilRemoveSpFpThreads(netTestCfg.num_sp_threads, netTestCfg.num_fp_threads);
584 #else
585     /*********************************************/
586     /**************Entry point into scheduler ****/
587     /*********************************************/
588     netapi_schedRun(our_sched, &err);
589 #endif
591 /* done */
592 netTest_utilsStatsCb(netapi_handle, NULL);
596     /* cleanup*/
597     netTest_utilDeleteSecAssoc();
598     netTest_utilDeleteInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
600     /* close pktio channels we opened via open_pktio_tx_channels() */
601     close_pktio_channels();
602     netapi_shutdown(netapi_handle);
605 static inline void send_it(Ti_Pkt *tip, int len, netTestSA_t * p_sec, int out_port)
607   unsigned long st1;
608   unsigned long st2;
609   int err=0;
610   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
611   nwalTxPktInfo_t meta_tx2={0};
612   st1=hplib_mUtilGetPmuCCNT();
613 #ifdef netTest_MULTI_THREAD
614     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
615     //int coreid = our_core;
616 #else
617     int coreid=0;
618 #endif
619   if (len<60) 
620   { 
621      unsigned int templen;
622      char * p_pkt;
623      len=60; 
624      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
625      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
626      stats[coreid].tx_min+=1;
627   }
628   Pktlib_setPacketLen(tip,len);
629   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
630   meta_tx2.startOffset = 0;
631   meta_tx2.ipOffBytes = netTest_MAC_HEADER_LEN;
632   meta_tx2.ploadLen = len ;
633   meta_tx2.enetPort=out_port;
634   if(p_sec)
635   {   
636        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO ;
637        meta2.sa_handle = (void*)p_sec->tx_tunnel;
638        meta_tx2.saOffBytes=netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN;
639        meta_tx2.saPayloadLen=len-netTest_MAC_HEADER_LEN - netTest_IP_HEADER_LEN;   //don't include tag, mac and outer header
640        meta2.u.tx_meta=&meta_tx2;
641        netapi_pktioSend(netcp_tx_chan_esp,tip,&meta2,&err);
642        stats[coreid].sec_tx+=1;
643   }
644   else
645   {
646       meta2.u.tx_meta=&meta_tx2;
647       netapi_pktioSend(netcp_tx_chan_no_crypto,tip,&meta2,&err);
649   }
650   stats[coreid].tx +=1;
651   st2=hplib_mUtilGetPmuCCNT();
652   stats[coreid].send_cycles += (unsigned long long) (st2-st1);  
653
654 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
655                          PKTIO_METADATA_T meta[], int n_pkts,
656                          uint64_t ts )
658     int i;
659     int len;
660     int p;
661     Ti_Pkt * tip;
662     unsigned int templen;
663     char * p_pkt;
664     netTestHead_T temp_head;
665     unsigned int appid;
666     IP_netTestHead_T th;
667     netTestSA_t *sec_data=NULL;
668     unsigned long t1;
669     unsigned long t2;
670     unsigned long long ct1;
671     unsigned long long ct2;
672     unsigned short ip_pl;
673     unsigned long long n_c_ops;
674     int ifno;
675     int out_port;
676 #ifdef netTest_MULTI_THREAD
677         int coreid=Osal_nwalGetProcId();  //who we are(thread local)
678         //int coreid = our_core;
679 #else
680     int coreid=0;
681 #endif
682     t1=hplib_mUtilGetPmuCCNT();
683     ct1 =Osal_cache_op_measure(&n_c_ops);
684     for(i=0;i<n_pkts;i++)
685     {
686         ifno = ((unsigned int)meta[i].u.rx_meta->appId)&0xff;
687         if(coreid<TUNE_NETAPI_NUM_CORES) stats[coreid].rx+=1;
688         if (ifno < TUNE_NETAPI_MAX_NUM_MAC) stats[coreid].if_rx[ifno]+=1;
689         tip = p_recv[i];
690         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
691         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
692         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
693         switch(appid)
694         {
695            case(NETAPI_NETCP_MATCH_IPSEC):
696            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
697            {
698              int tailen=12+2;
699              memcpy(&temp_head,&p_pkt[14],sizeof(netTestHead_T));
700              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
701                 stats[coreid].n_bad+=1;
702                 Pktlib_freePacket(tip); 
703                 continue;
704              }
705              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
706              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
707              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
708             
709              //now check inner headder.
710              memcpy(&th,&p_pkt[14],20);
711              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
712                 stats[coreid].n_bad+=1;
713                 Pktlib_freePacket(tip);
714                 continue;
715              }
716              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
717              Pktlib_setPacketLen(tip,len);
718              
719              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
720               {
721                 stats[coreid].n_bad+=1;
722                 Pktlib_freePacket(tip);
723              }
724              else
725              {
726                send_it(tip,len,sec_data,out_port);
727              }
728              break;
729            }
730            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
731               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
732               {
733                 stats[coreid].n_new+=1;
734                 Pktlib_freePacket(tip); 
735                 continue;
736               }
737               if (!netTest_utilCheckHeader(&temp_head,&meta[i])) 
738               {
739                 stats[coreid].n_bad+=1;
740                 Pktlib_freePacket(tip);
741                 continue;
742               }
743               memcpy(&th,&p_pkt[14],20);
744               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
745               if ((ip_pl+14)<60)
746               {
747                 len-= (60-(ip_pl+14));
748                 stats[coreid].rx_min+=1;
749               }
750               Pktlib_setPacketLen(tip,len);
751               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
752               {
753                 stats[coreid].n_bad+=1;
754                 Pktlib_freePacket(tip);
755               }
756               else
757               {
758                  send_it(tip,len,sec_data,out_port);
759               }
760               break;
761            case(NETAPI_NETCP_MATCH_GENERIC_IP):
762               Pktlib_freePacket(tip); 
763               stats[coreid].n_new=1;
764               break;
765            default:
766               stats[coreid].n_new+=1;
767               Pktlib_freePacket(tip);
768               break;
769        }
771 t2=hplib_mUtilGetPmuCCNT();
772 ct2 =Osal_cache_op_measure(&n_c_ops);
773 stats[coreid].app_cycles +=  (unsigned long long) (t2-t1);
774 stats[coreid].tx_cache_cycles += (unsigned long long) (ct2-ct1);
775 return;
778 /* STUB functions required for compilation */
779 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
780                          PKTIO_METADATA_T meta[], int n_pkts,
781                          uint64_t ts )
785 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
786                          PKTIO_METADATA_T meta[], int n_pkts,
787                          uint64_t ts )